File: ec_conntrack.c

package info (click to toggle)
ettercap 1:0.8.2-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,468 kB
  • ctags: 6,333
  • sloc: ansic: 47,337; yacc: 310; lex: 204; makefile: 121; xml: 31; sh: 24
file content (806 lines) | stat: -rw-r--r-- 20,855 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
/*
    ettercap -- connection list handling module

    Copyright (C) ALoR & NaGA

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

*/

#include <ec.h>
#include <ec_threads.h>
#include <ec_packet.h>
#include <ec_proto.h>
#include <ec_hook.h>
#include <ec_conntrack.h>
#include <ec_hash.h>
#include <ec_sleep.h>

/* globals */

/* the hash table used to index the tailq */
struct conn_hash_search {
   struct conn_tail *cl;
   LIST_ENTRY(conn_hash_search) next;
};

#define TABBIT    10             /* 2^10 bit tab entries: 1024 LISTS */
#define TABSIZE   (1 << TABBIT)
#define TABMASK   (TABSIZE - 1)

/*
 * the connection list.
 * this list is created adding new element in the tail.
 * the search method is established in the search function. 
 * an hash table is used and it is double-linked with the 
 * tailq so from each element you can delete the corresponding
 * in the tailq or viceversa
 */
static TAILQ_HEAD(conn_head, conn_tail) conntrack_tail_head = TAILQ_HEAD_INITIALIZER(conntrack_tail_head);
static LIST_HEAD(, conn_hash_search) conntrack_search_head[TABSIZE];

/* global mutex on connections list */

static pthread_mutex_t conntrack_mutex = PTHREAD_MUTEX_INITIALIZER;
#define CONNTRACK_LOCK     do{ pthread_mutex_lock(&conntrack_mutex); }while(0)
#define CONNTRACK_UNLOCK   do{ pthread_mutex_unlock(&conntrack_mutex); }while(0)

/* protos */

void __init conntrack_init(void);

static void conntrack_parse(struct packet_object *po);
static u_int32 conntrack_hash(struct packet_object *po);
static struct conn_object *conntrack_search(struct packet_object *po);
static void conntrack_update(struct conn_object *co, struct packet_object *po);
static void conntrack_add(struct packet_object *po);
static void conntrack_del(struct conn_object *co);
static int conntrack_match(struct conn_object *co, struct packet_object *po);
void conntrack_hook(struct conn_object *co, struct packet_object *po);

/************************************************/
  
/*
 * add the hook function
 */
void __init conntrack_init(void)
{
   /* receive all the top half packets */
   hook_add(HOOK_DISPATCHER, &conntrack_parse);
}

/*
 * the conntrack main()
 */
static void conntrack_parse(struct packet_object *po)
{
   struct conn_object *conn;

   CONNTRACK_LOCK;
   
   /* search if the connection already exists */
   conn = conntrack_search(po);

   /* update if it was found, else add to the list */
   if (conn)
      conntrack_update(conn, po);
   else
      conntrack_add(po);
   
   CONNTRACK_UNLOCK;
}

/* 
 * calculate the hash for a packet object 
 */
static u_int32 conntrack_hash(struct packet_object *po)
{
   u_int32 hash_array[3];

   /* 
    * put them in an array and then compute the hash on the array.
    * use XOR on src and dst because the hash must be equal for 
    * packets from dst to src and viceversa
    */
   hash_array[0] = fnv_32((u_char *)&po->L3.src, sizeof(struct ip_addr)) ^
                   fnv_32((u_char *)&po->L3.dst, sizeof(struct ip_addr));
   hash_array[1] = po->L4.src ^ po->L4.dst;
   hash_array[2] = po->L4.proto;
   
   /* compute the resulting hash */
   return fnv_32((u_char *)&hash_array, sizeof(hash_array)) & TABMASK;
}

/* 
 * search the connection in the connection table
 * and return the pointer.
 */
static struct conn_object *conntrack_search(struct packet_object *po)
{
   struct conn_hash_search *cs;
   u_int32 h;
  
   /* use the hash table to find the connection in the tailq */
   h = conntrack_hash(po);
   
   LIST_FOREACH(cs, &conntrack_search_head[h], next) {
      if (conntrack_match(cs->cl->co, po) == E_SUCCESS) {
         return cs->cl->co;
      }
   }

   return NULL;
#if 0   
   struct conn_tail *cl;
   
   /* search in the list sequentially */
   TAILQ_FOREACH(cl, &conntrack_tail_head, next) {
      if (conntrack_match(cl->co, po) == E_SUCCESS) {
         return cl->co;
      }
   }

   return NULL;
#endif
}


/*
 * update the variable parameters in the connection struct.
 * the status, the buffer and the timestamp will be updated
 */
static void conntrack_update(struct conn_object *co, struct packet_object *po)
{
   /* update the timestamp */
   gettimeofday(&co->ts, 0);
  
   /* update the status for TCP conn */
   if (po->L4.flags & TH_SYN)
      co->status = CONN_OPENING;
   else if (po->L4.flags & TH_FIN)
      co->status = CONN_CLOSING;
   else if (po->L4.flags & TH_ACK) { 
      /* syn + ack,  ack */
      if (co->status == CONN_OPENING )
         co->status = CONN_OPEN;
      /* fin + ack,  ack */
      else if (co->status == CONN_CLOSING)
         co->status = CONN_CLOSED;
   } 

   if (po->L4.flags & TH_PSH)
      co->status = CONN_ACTIVE;
   
   if (po->L4.flags & TH_RST)
      co->status = CONN_KILLED;
      
   /* update the buffer */
   connbuf_add(&co->data, po);
  

   /* update the status for UDP conn */
   if (po->L4.proto == NL_TYPE_UDP)
      co->status = CONN_ACTIVE;
   
   /* 
    * update the byte count 
    * use DATA.len and not DATA.disp_len to have an
    * effective count of byte trasferred, disp_data
    * may be longer or shorted than DATA.data
    */
   co->xferred += po->DATA.len;
   if(!ip_addr_cmp(&co->L3_addr1, &po->L3.src))
      co->tx += po->DATA.len;
   else
      co->rx += po->DATA.len;

   /*
    * update the flags:
    * if the packet was modified or dropped, the connection
    * must be marked as well.
    */
   if (po->flags & PO_MODIFIED || po->flags & PO_DROPPED)
      co->flags |= CONN_MODIFIED;

   /* 
    * update the password 
    * always overwrite the old one, a better one may
    * has been collected...
    */
   if (po->DISSECTOR.user) {
      SAFE_FREE(co->DISSECTOR.user);
      SAFE_FREE(co->DISSECTOR.pass);
      SAFE_FREE(co->DISSECTOR.info);
      co->DISSECTOR.user = strdup(po->DISSECTOR.user);
      if (po->DISSECTOR.pass)
         co->DISSECTOR.pass = strdup(po->DISSECTOR.pass);
      if (po->DISSECTOR.info)
         co->DISSECTOR.info = strdup(po->DISSECTOR.info);
   }
   
   /* execute the hookpoint */
   conntrack_hook(co, po);
}


/*
 * create a new entry in the tail 
 */
static void conntrack_add(struct packet_object *po)
{
   struct conn_tail *cl;
   struct conn_hash_search *cs;

   DEBUG_MSG("conntrack_add: NEW CONNECTION");
   
   /* alloc the list element */
   SAFE_CALLOC(cl, 1, sizeof(struct conn_tail));

   /* alloc the conn object in the element */
   SAFE_CALLOC(cl->co, 1, sizeof(struct conn_object));

   /* 
    * here we create the connection.
    * this is the first packet seen...
    * addr1 will be the source and addr2 the dest 
    */

   /* fill the addresses */
   memcpy(&cl->co->L2_addr1, &po->L2.src, MEDIA_ADDR_LEN);
   memcpy(&cl->co->L2_addr2, &po->L2.dst, MEDIA_ADDR_LEN);

   memcpy(&cl->co->L3_addr1, &po->L3.src, sizeof(struct ip_addr));
   memcpy(&cl->co->L3_addr2, &po->L3.dst, sizeof(struct ip_addr));

   /* copy the port */
   cl->co->L4_addr1 = po->L4.src;
   cl->co->L4_addr2 = po->L4.dst;
   cl->co->L4_proto = po->L4.proto;
 
   /* initialize the connection buffer */
   connbuf_init(&cl->co->data, GBL_CONF->connection_buffer);
   
   /* update the connection entry */
   conntrack_update(cl->co, po);

   /* alloc the hash table element */
   SAFE_CALLOC(cs, 1, sizeof(struct conn_hash_search));
   
   /* set the pointer to the list */
   cs->cl = cl;
  
   /* 
    * set the pointer to the element in the hash table 
    * it is used when a connection is deleted because
    * even the element in the hash table must be deleted
    */
   cl->cs = cs;
   
   /* insert the new connection in the tail */
   TAILQ_INSERT_TAIL(&conntrack_tail_head, cl, next);
   /* insert the new connection in the tail */
   LIST_INSERT_HEAD(&conntrack_search_head[conntrack_hash(po)], cs, next);
}

/*
 * is the packet object belonging to this connection ?
 */
static int conntrack_match(struct conn_object *co, struct packet_object *po)
{
   /* different protocol, they don't match */
   if (co->L4_proto != po->L4.proto)
      return -E_INVALID;

   /* match it in one way... */
   if (!ip_addr_cmp(&co->L3_addr1, &po->L3.src) &&
       !ip_addr_cmp(&co->L3_addr2, &po->L3.dst) &&
       co->L4_addr1 == po->L4.src &&
       co->L4_addr2 == po->L4.dst)
      return E_SUCCESS;

   /* ...and in the other */
   if (!ip_addr_cmp(&co->L3_addr1, &po->L3.dst) &&
       !ip_addr_cmp(&co->L3_addr2, &po->L3.src) &&
       co->L4_addr1 == po->L4.dst &&
       co->L4_addr2 == po->L4.src)
      return E_SUCCESS;
   
   return -E_NOMATCH;
}

/* 
 * erase a connection object
 */
static void conntrack_del(struct conn_object *co)
{
   struct ct_hook_list *h, *tmp;

   /* remove the hooks */
   SLIST_FOREACH_SAFE(h, &co->hook_head, next, tmp) {
      SLIST_REMOVE(&co->hook_head, h, ct_hook_list, next);
      SAFE_FREE(h);
   }

   /* wipe the associated buffer */
   connbuf_wipe(&co->data);
   
   SAFE_FREE(co);
}

/*
 * erase the whole connections list
 */
void conntrack_purge(void)
{
   struct conn_tail *cl, *tmp;

   DEBUG_MSG("conntrack_purge");
   
   TAILQ_FOREACH_SAFE(cl, &conntrack_tail_head, next, tmp) {
      /* don't erase the connection if it is viewed */
      if (cl->co->flags & CONN_VIEWING)
         continue;
      
      CONNTRACK_LOCK;
      
      /* wipe the connection */
      conntrack_del(cl->co);
      /* remove the element in the hash table */
      LIST_REMOVE(cl->cs, next);
      SAFE_FREE(cl->cs);
      /* remove the element in the tailq */
      TAILQ_REMOVE(&conntrack_tail_head, cl, next);
      SAFE_FREE(cl);

      CONNTRACK_UNLOCK;
   }
   
}


EC_THREAD_FUNC(conntrack_timeouter)
{
   struct timeval ts;
   struct timeval diff;
   struct conn_tail *cl;
   struct conn_tail *tmp = NULL;
   size_t sec;
   
   /* variable not used */
   (void) EC_THREAD_PARAM;

   /* initialize the thread */
   ec_thread_init();
   
   DEBUG_MSG("conntrack_timeouter: activated !");

   LOOP {

      /* 
       * sleep for the maximum time possible
       * (determined as the minumum of the timeouts)
       */
      sec = MIN(GBL_CONF->connection_idle, GBL_CONF->connection_timeout);

      DEBUG_MSG("conntrack_timeouter: sleeping for %lu sec", (unsigned long)sec);
      
      /* always check if a cancel is requested */
      CANCELLATION_POINT();

      ec_usleep(SEC2MICRO(sec));
     
      DEBUG_MSG("conntrack_timeouter: woke up");
      
      /* get current time */
      gettimeofday(&ts, NULL);
     
      /*
       * the timeouter is the only thread that erase a connection
       * so we are sure that the list will be consistent till the
       * end.
       * we can lock and unlock every time we handle an element of 
       * the list to permit the conntrack functions to operate on the
       * list even when timeouter goes thru the list
       */
      TAILQ_FOREACH_SAFE(cl, &conntrack_tail_head, next, tmp) {

         /* don't erase the connection if it is viewed */
         if (cl->co->flags & CONN_VIEWING)
            continue;
         
         CONNTRACK_LOCK;
         
         /* calculate the difference */
         time_sub(&ts, &cl->co->ts, &diff);
         
         /* 
          * update it only if the staus is active,
          * all the other status must be left as they are
          */
         if (cl->co->status == CONN_ACTIVE && diff.tv_sec >= GBL_CONF->connection_idle)
            cl->co->status = CONN_IDLE;
         
         /* delete the timeouted connections */
         if (diff.tv_sec >= GBL_CONF->connection_timeout) {
            /* wipe the connection */
            conntrack_del(cl->co);
            /* remove the element in the hash table */
            LIST_REMOVE(cl->cs, next);
            SAFE_FREE(cl->cs);
            /* remove the element in the tailq */
            TAILQ_REMOVE(&conntrack_tail_head, cl, next);
            SAFE_FREE(cl);
         }

         CONNTRACK_UNLOCK;
   
         CANCELLATION_POINT();
      }
   }
   
   return NULL;
}


/*
 * add the fucntion 'func' to the hookpoint of the
 * connection owning the packet object
 */
int conntrack_hook_packet_add(struct packet_object *po, void (*func)(struct packet_object *po))
{
   struct conn_object *conn;

   CONNTRACK_LOCK;
   
   /* search the connection already exists */
   conn = conntrack_search(po);

   /* 
    * if the connection already exist, add the hook function 
    * else create the entry for the connection and add the hook
    * this is useful to add hooks for future connections
    */

   /* create the fake connection */
   if (!conn) {
      
      DEBUG_MSG("conntrack_hook_packet_add: ephemeral connection");
      conntrack_add(po);
      conn = conntrack_search(po);
   }
  
   /* add the hook point */
   if (conn) {
      struct ct_hook_list *h;
      
      DEBUG_MSG("conntrack_hook_packet_add: existing connection");
      
      SAFE_CALLOC(h, 1, sizeof(struct ct_hook_list));
      
      /* set the hook function */
      h->func = func;
      
      SLIST_INSERT_HEAD(&conn->hook_head, h, next);
      
      CONNTRACK_UNLOCK;
      return E_SUCCESS;
   } 
   
   CONNTRACK_UNLOCK;

   return -E_NOTFOUND;
}


/* 
 * removes a hook from a connection 
 */
int conntrack_hook_packet_del(struct packet_object *po, void (*func)(struct packet_object *po))
{
   struct conn_object *conn;

   DEBUG_MSG("conntrack_hook_packet_del");
   
   CONNTRACK_LOCK;
   
   /* search the connection already exists */
   conn = conntrack_search(po);

   /* remove the hook function only if the connection exists */
   if (conn) {
      struct ct_hook_list *h;
      
      SLIST_FOREACH(h, &conn->hook_head, next) {
         if (h->func == func) {
            SLIST_REMOVE(&conn->hook_head, h, ct_hook_list, next);
            SAFE_FREE(h);
            break;
         }
      }
      
      CONNTRACK_UNLOCK;
      return E_SUCCESS;
   }
   
   CONNTRACK_UNLOCK;

   return -E_NOTFOUND;
}

/*
 * add the fucntion 'func' to the hookpoint of the
 * connection 'co' 
 */
int conntrack_hook_conn_add(struct conn_object *co, void (*func)(struct packet_object *po))
{
   struct ct_hook_list *h;

   CONNTRACK_LOCK;
   
   /* add the hook point */
   
   DEBUG_MSG("conntrack_hook_conn_add");
   
   SAFE_CALLOC(h, 1, sizeof(struct ct_hook_list));
   
   /* set the hook function */
   h->func = func;
   
   SLIST_INSERT_HEAD(&co->hook_head, h, next);
      
   CONNTRACK_UNLOCK;

   return E_SUCCESS;
}


/* 
 * removes a hook from a connection 
 */
int conntrack_hook_conn_del(struct conn_object *co, void (*func)(struct packet_object *po))
{
   struct ct_hook_list *h;

   DEBUG_MSG("conntrack_hook_conn_del");
   
   CONNTRACK_LOCK;
   
   SLIST_FOREACH(h, &co->hook_head, next) {
      if (h->func == func) {
         SLIST_REMOVE(&co->hook_head, h, ct_hook_list, next);
         SAFE_FREE(h);
         break;
      }
   }
   
   CONNTRACK_UNLOCK;
   return E_SUCCESS;
}


/*
 * executes the hook point
 */
void conntrack_hook(struct conn_object *co, struct packet_object *po)
{
   struct ct_hook_list *h;

   /* pass the po to all the hooked functions */
   SLIST_FOREACH(h, &co->hook_head, next) {
      h->func(po);
   }
   
}

/*
 * fill the desc and return the next/prev element
 */
void * conntrack_print(int mode, void *list, char **desc, size_t len)
{
   struct conn_tail *c = (struct conn_tail *)list;
   struct conn_tail *cl;
   char src[MAX_ASCII_ADDR_LEN];
   char dst[MAX_ASCII_ADDR_LEN];
   char proto[2], status[8], flags[2];

   /* NULL is used to retrieve the first element */
   if (list == NULL)
      return TAILQ_FIRST(&conntrack_tail_head);

   /* the caller wants the description */
   if (desc != NULL) {
         
      /* IP address to string */
      ip_addr_ntoa(&c->co->L3_addr1, src);
      ip_addr_ntoa(&c->co->L3_addr2, dst);

      /* determine the protocol */
      conntrack_protostr(c->co, proto, sizeof(proto));
      
      /* determine the status */
      conntrack_statusstr(c->co, status, sizeof(status));
      
      /* determine the flags */
      conntrack_flagstr(c->co, flags, sizeof(flags));
      
      snprintf(*desc, len, "%1s %15s:%-5d - %15s:%-5d %1s %s TX: %lu RX: %lu", flags, 
                                           src, ntohs(c->co->L4_addr1), dst, ntohs(c->co->L4_addr2),
                                           proto, status, (unsigned long)c->co->tx, (unsigned long)c->co->rx);
   }
  
   /* return the next/prev/current to the caller */
   switch (mode) {
      case -1:
         return TAILQ_PREV(c, conn_head, next);
         break;
      case +1:
         return TAILQ_NEXT(c, next);
         break;
      case 0:
         /* if exists in the list, return it */
         TAILQ_FOREACH(cl, &conntrack_tail_head, next) {
            if (cl == c)
               return c;
         }
         /* else, return NULL */
         return NULL;
      default:
         return list;
         break;
   }
         
   return NULL;
}

/* 
 * copy the connection object pointer to conn and return the next/prev element
 */
void * conntrack_get(int mode, void *list, struct conn_object **conn)
{
   struct conn_tail *c = (struct conn_tail *)list;
   struct conn_tail *cl;

   /* NULL is used to retrieve the first element */
   if (list == NULL)
      return TAILQ_FIRST(&conntrack_tail_head);

   /* the caller wants the connection object */
   if (conn != NULL) 
       *conn = c->co;
  
   /* return the next/prev/current to the caller */
   switch (mode) {
      case -1:
         return TAILQ_PREV(c, conn_head, next);
         break;
      case +1:
         return TAILQ_NEXT(c, next);
         break;
      case 0:
         /* if exists in the list, return it */
         TAILQ_FOREACH(cl, &conntrack_tail_head, next) {
            if (cl == c)
               return c;
         }
         /* else, return NULL */
         return NULL;
      default:
         return list;
         break;
   }
         
   return NULL;
}


/*
 * copies the protocol string of a given connection object into pstr
 * E_SUCCESS is returned on success
 * -E_INVALID is returned if parameters are not initialized
 */
int conntrack_protostr(struct conn_object *conn, char *pstr, int len)
{
    if (pstr == NULL || conn == NULL)
        return -E_INVALID;

    memset(pstr, 0, len);

    switch (conn->L4_proto) {
        case NL_TYPE_UDP:
           strncpy(pstr, "UDP", len - 1);
           break;
        case NL_TYPE_TCP:
           strncpy(pstr, "TCP", len - 1);
           break;
        default:
           strncpy(pstr, "   ", len - 1);
    }

    return E_SUCCESS;
}

/*
 * copies the flags string of a given connection object into pstr
 * E_SUCCESS is returned on success
 * -E_INVALID is returned if parameters are not initialized
 */
int conntrack_flagstr(struct conn_object *conn, char *pstr, int len)
{
    if (pstr == NULL || conn == NULL)
        return -E_INVALID;

    memset(pstr, 0, len);

    /*
     * account collection has precedence over injection/modification
     */
    if (conn->flags & CONN_MODIFIED)
        strncpy(pstr, "M", len - 1);

    if (conn->flags & CONN_INJECTED)
        strncpy(pstr, "I", len - 1);

    if (conn->DISSECTOR.user)
        strncpy(pstr, "*", len - 1);

    return E_SUCCESS;
}


/*
 * copies the status string of a given connection object into pstr
 * E_SUCCESS is returned on success
 * -E_INVALID is returned if parameters are not initialized
 */
int conntrack_statusstr(struct conn_object *conn, char *pstr, int len)
{
    if (pstr == NULL || conn == NULL)
        return -E_INVALID;

    memset(pstr, 0, len);

    /* determine the status */
    switch (conn->status) {
        case CONN_IDLE:
           strncpy(pstr, "idle   ", len - 1);
           break;
        case CONN_OPENING:
           strncpy(pstr, "opening", len - 1);
           break;
        case CONN_OPEN:
           strncpy(pstr, "open   ", len - 1);
           break;
        case CONN_ACTIVE:
           strncpy(pstr, "active ", len - 1);
           break;
        case CONN_CLOSING:
           strncpy(pstr, "closing", len - 1);
           break;
        case CONN_CLOSED:
           strncpy(pstr, "closed ", len - 1);
           break;
        case CONN_KILLED:
           strncpy(pstr, "killed ", len - 1);
           break;
    }

    return E_SUCCESS;
}


/* EOF */

// vim:ts=3:expandtab