]> gitweb.fperrin.net Git - iftop.git/blob - iftop.c
f887d93316b7c0c7c050fac7ac192ed7c09721d9
[iftop.git] / iftop.c
1 /*
2  * iftop.c:
3  *
4  */
5
6 #include "integers.h"
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <time.h>
11 #include <sys/types.h>
12 #include <sys/ioctl.h>
13 #include <sys/socket.h>
14 #include <net/if.h>
15 /* include <net/bpf.h> -- this was added by the PFLOG patch but seems
16  * superfluous and breaks on Slackware */
17 #if defined(HAVE_PCAP_H)
18 #   include <pcap.h>
19 #elif defined(HAVE_PCAP_PCAP_H)
20 #   include <pcap/pcap.h>
21 #else
22 #   error No pcap.h
23 #endif
24
25 #include <pthread.h>
26 #include <curses.h>
27 #include <signal.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <locale.h>
31
32 #include "iftop.h"
33 #include "addr_hash.h"
34 #include "resolver.h"
35 #include "ui_common.h"
36 #include "ui.h"
37 #include "tui.h"
38 #include "options.h"
39 #ifdef DLT_LINUX_SLL
40 #include "sll.h"
41 #endif /* DLT_LINUX_SLL */
42 #include "threadprof.h"
43 #include "ether.h"
44 #include "ip.h"
45 #include "tcp.h"
46 #include "token.h"
47 #include "llc.h"
48 #include "extract.h"
49 #include "ethertype.h"
50 #include "cfgfile.h"
51 #include "ppp.h"
52 #include "addrs_ioctl.h"
53
54 #include <netinet/ip6.h>
55
56 /* ethernet address of interface. */
57 int have_hw_addr = 0;
58 char if_hw_addr[6];    
59
60 /* IP address of interface */
61 int have_ip_addr = 0;
62 int have_ip6_addr = 0;
63 struct in_addr if_ip_addr;
64 struct in6_addr if_ip6_addr;
65
66 extern options_t options;
67
68 hash_type* history;
69 history_type history_totals;
70 time_t last_timestamp;
71 time_t first_timestamp;
72 int history_pos = 0;
73 int history_len = 1;
74 pthread_mutex_t tick_mutex;
75
76 pcap_t* pd; /* pcap descriptor */
77 struct bpf_program pcap_filter;
78 pcap_handler packet_handler;
79
80 sig_atomic_t foad;
81
82 static void finish(int sig) {
83     foad = sig;
84 }
85
86
87
88
89 /* Only need ethernet (plus optional 4 byte VLAN) and IP headers (48) + first 2
90  * bytes of tcp/udp header */
91 /* Increase with a further 20 to account for IPv6 header length.  */
92 /* IEEE 802.11 radiotap throws in a variable length header plus 8 (radiotap
93  * header header) plus 34 (802.11 MAC) plus 40 (IPv6) = 78, plus whatever's in
94  * the radiotap payload */
95 /*#define CAPTURE_LENGTH 92 */
96 #define CAPTURE_LENGTH 256
97
98 void init_history() {
99     history = addr_hash_create();
100     last_timestamp = time(NULL);
101     memset(&history_totals, 0, sizeof history_totals);
102 }
103
104 history_type* history_create() {
105     history_type* h;
106     h = xcalloc(1, sizeof *h);
107     return h;
108 }
109
110 void history_rotate() {
111     hash_node_type* n = NULL;
112     history_pos = (history_pos + 1) % HISTORY_LENGTH;
113     hash_next_item(history, &n);
114     while(n != NULL) {
115         hash_node_type* next = n;
116         history_type* d = (history_type*)n->rec;
117         hash_next_item(history, &next);
118
119         if(d->last_write == history_pos) {
120             addr_pair key = *(addr_pair*)(n->key);
121             hash_delete(history, &key);
122             free(d);
123         }
124         else {
125             d->recv[history_pos] = 0;
126             d->sent[history_pos] = 0;
127         }
128         n = next; 
129     }
130
131     history_totals.sent[history_pos] = 0;
132     history_totals.recv[history_pos] = 0;
133
134     if(history_len < HISTORY_LENGTH) {
135         history_len++;
136     }
137 }
138
139
140 void tick(int print) {
141     time_t t;
142
143     pthread_mutex_lock(&tick_mutex);
144    
145     t = time(NULL);
146     if(t - last_timestamp >= RESOLUTION) {
147         analyse_data();
148         if (options.no_curses) {
149           if (!options.timed_output || (options.timed_output && t - first_timestamp >= options.timed_output)) {
150             tui_print();
151             if (options.timed_output) {
152               finish(SIGINT);
153             }
154           }
155         }
156         else {
157           ui_print();
158         }
159         history_rotate();
160         last_timestamp = t;
161     }
162     else {
163       if (options.no_curses) {
164         tui_tick(print);
165       }
166       else {
167         ui_tick(print);
168       }
169     }
170
171     pthread_mutex_unlock(&tick_mutex);
172 }
173
174 int in_filter_net(struct in_addr addr) {
175     int ret;
176     ret = ((addr.s_addr & options.netfiltermask.s_addr) == options.netfilternet.s_addr);
177     return ret;
178 }
179
180 static int __inline__ ip_addr_match(struct in_addr addr) {
181     return addr.s_addr == if_ip_addr.s_addr;
182 }
183
184 static int __inline__ ip6_addr_match(struct in6_addr *addr) {
185     return IN6_ARE_ADDR_EQUAL(addr, &if_ip6_addr);
186 }
187
188 /**
189  * Creates an addr_pair from an ip (and tcp/udp) header, swapping src and dst
190  * if required
191  */
192 void assign_addr_pair(addr_pair* ap, struct ip* iptr, int flip) {
193   unsigned short int src_port = 0;
194   unsigned short int dst_port = 0;
195
196   /* Arrange for predictable values. */
197   memset(ap, '\0', sizeof(*ap));
198
199   if(IP_V(iptr) == 4) {
200     ap->af = AF_INET;
201   /* Does this protocol use ports? */
202   if(iptr->ip_p == IPPROTO_TCP || iptr->ip_p == IPPROTO_UDP) {
203     /* We take a slight liberty here by treating UDP the same as TCP */
204
205     /* Find the TCP/UDP header */
206     struct tcphdr* thdr = ((void*)iptr) + IP_HL(iptr) * 4;
207     src_port = ntohs(thdr->th_sport);
208     dst_port = ntohs(thdr->th_dport);
209   }
210
211   if(flip == 0) {
212     ap->src = iptr->ip_src;
213     ap->src_port = src_port;
214     ap->dst = iptr->ip_dst;
215     ap->dst_port = dst_port;
216   }
217   else {
218     ap->src = iptr->ip_dst;
219     ap->src_port = dst_port;
220     ap->dst = iptr->ip_src;
221     ap->dst_port = src_port;
222   }
223   } /* IPv4 */
224   else if (IP_V(iptr) == 6) {
225     /* IPv6 packet seen. */
226     struct ip6_hdr *ip6tr = (struct ip6_hdr *) iptr;
227
228     ap->af = AF_INET6;
229
230     if( (ip6tr->ip6_nxt == IPPROTO_TCP) || (ip6tr->ip6_nxt == IPPROTO_UDP) ) {
231       struct tcphdr *thdr = ((void *) ip6tr) + 40;
232
233       src_port = ntohs(thdr->th_sport);
234       dst_port = ntohs(thdr->th_dport);
235     }
236
237     if(flip == 0) {
238       memcpy(&ap->src6, &ip6tr->ip6_src, sizeof(ap->src6));
239       ap->src_port = src_port;
240       memcpy(&ap->dst6, &ip6tr->ip6_dst, sizeof(ap->dst6));
241       ap->dst_port = dst_port;
242     }
243     else {
244       memcpy(&ap->src6, &ip6tr->ip6_dst, sizeof(ap->src6));
245       ap->src_port = dst_port;
246       memcpy(&ap->dst6, &ip6tr->ip6_src, sizeof(ap->dst6));
247       ap->dst_port = src_port;
248     }
249   }
250 }
251
252 static void handle_ip_packet(struct ip* iptr, int hw_dir)
253 {
254     int direction = 0; /* incoming */
255     history_type* ht;
256     union {
257       history_type **ht_pp;
258       void **void_pp;
259     } u_ht = { &ht };
260     addr_pair ap;
261     unsigned int len = 0;
262     struct in6_addr scribdst;   /* Scratch pad. */
263     struct in6_addr scribsrc;   /* Scratch pad. */
264     /* Reinterpret packet type. */
265     struct ip6_hdr* ip6tr = (struct ip6_hdr *) iptr;
266
267     memset(&ap, '\0', sizeof(ap));
268
269     tick(0);
270
271     if( (IP_V(iptr) ==4 && options.netfilter == 0)
272             || (IP_V(iptr) == 6 && options.netfilter6 == 0) ) { 
273         /*
274          * Net filter is off, so assign direction based on MAC address
275          */
276         if(hw_dir == 1) {
277             /* Packet leaving this interface. */
278             assign_addr_pair(&ap, iptr, 0);
279             direction = 1;
280         }
281         else if(hw_dir == 0) {
282             /* Packet incoming */
283             assign_addr_pair(&ap, iptr, 1);
284             direction = 0;
285         }
286         /* Packet direction is not given away by h/ware layer.  Try IP
287          * layer
288          */
289         else if((IP_V(iptr) == 4) && have_ip_addr && ip_addr_match(iptr->ip_src)) {
290             /* outgoing */
291             assign_addr_pair(&ap, iptr, 0);
292             direction = 1;
293         }
294         else if((IP_V(iptr) == 4) && have_ip_addr && ip_addr_match(iptr->ip_dst)) {
295             /* incoming */
296             assign_addr_pair(&ap, iptr, 1);
297             direction = 0;
298         }
299         else if((IP_V(iptr) == 6) && have_ip6_addr && ip6_addr_match(&ip6tr->ip6_src)) {
300             /* outgoing */
301             assign_addr_pair(&ap, iptr, 0);
302             direction = 1;
303         }
304         else if((IP_V(iptr) == 6) && have_ip6_addr && ip6_addr_match(&ip6tr->ip6_dst)) {
305             /* incoming */
306             assign_addr_pair(&ap, iptr, 1);
307             direction = 0;
308         }
309         else if (IP_V(iptr) == 4 && IN_MULTICAST(iptr->ip_dst.s_addr)) {
310             assign_addr_pair(&ap, iptr, 1);
311             direction = 0;
312         }
313         else if (IP_V(iptr) == 6 && IN6_IS_ADDR_MULTICAST(&ip6tr->ip6_dst)) {
314             assign_addr_pair(&ap, iptr, 1);
315             direction = 0;
316         }
317         /*
318          * Cannot determine direction from hardware or IP levels.  Therefore 
319          * assume that it was a packet between two other machines, assign
320          * source and dest arbitrarily (by numerical value) and account as 
321          * incoming.
322          */
323         else if (options.promiscuous_but_choosy) {
324             return;             /* junk it */
325         }
326         else if((IP_V(iptr) == 4) && (iptr->ip_src.s_addr < iptr->ip_dst.s_addr)) {
327             assign_addr_pair(&ap, iptr, 1);
328             direction = 0;
329         }
330         else if(IP_V(iptr) == 4) {
331             assign_addr_pair(&ap, iptr, 0);
332             direction = 0;
333         }
334         /* Drop other uncertain packages. */
335         else
336             return;
337     }
338
339     if(IP_V(iptr) == 4 && options.netfilter != 0) {
340         /* 
341          * Net filter on, assign direction according to netmask 
342          */ 
343         if(in_filter_net(iptr->ip_src) && !in_filter_net(iptr->ip_dst)) {
344             /* out of network */
345             assign_addr_pair(&ap, iptr, 0);
346             direction = 1;
347         }
348         else if(in_filter_net(iptr->ip_dst) && !in_filter_net(iptr->ip_src)) {
349             /* into network */
350             assign_addr_pair(&ap, iptr, 1);
351             direction = 0;
352         }
353         else {
354             /* drop packet */
355             return ;
356         }
357     }
358
359     if(IP_V(iptr) == 6 && options.netfilter6 != 0) {
360         /*
361          * Net filter IPv6 active.
362          */
363         int j;
364         //else if((IP_V(iptr) == 6) && have_ip6_addr && ip6_addr_match(&ip6tr->ip6_dst)) {
365         /* First reduce the participating addresses using the netfilter prefix.
366          * We need scratch pads to do this.
367          */
368         for (j=0; j < 16; ++j) {
369             scribdst.s6_addr[j] = ip6tr->ip6_dst.s6_addr[j]
370                                         & options.netfilter6mask.s6_addr[j];
371             scribsrc.s6_addr[j] = ip6tr->ip6_src.s6_addr[j]
372                                         & options.netfilter6mask.s6_addr[j];
373         }
374
375         /* Now look for any hits. */
376         //if(in_filter_net(iptr->ip_src) && !in_filter_net(iptr->ip_dst)) {
377         if (IN6_ARE_ADDR_EQUAL(&scribsrc, &options.netfilter6net)
378                 && ! IN6_ARE_ADDR_EQUAL(&scribdst, &options.netfilter6net)) {
379             /* out of network */
380             assign_addr_pair(&ap, iptr, 0);
381             direction = 1;
382         }
383         //else if(in_filter_net(iptr->ip_dst) && !in_filter_net(iptr->ip_src)) {
384         else if (! IN6_ARE_ADDR_EQUAL(&scribsrc, &options.netfilter6net)
385                     && IN6_ARE_ADDR_EQUAL(&scribdst, &options.netfilter6net)) {
386             /* into network */
387             assign_addr_pair(&ap, iptr, 1);
388             direction = 0;
389         }
390         else {
391             /* drop packet */
392             return ;
393         }
394     }
395
396 #if 1
397     /* Test if link-local IPv6 packets should be dropped. */
398     if( IP_V(iptr) == 6 && !options.link_local
399             && (IN6_IS_ADDR_LINKLOCAL(&ip6tr->ip6_dst)
400                 || IN6_IS_ADDR_LINKLOCAL(&ip6tr->ip6_src)) )
401         return;
402 #endif
403
404     /* Do address resolving. */
405     switch (IP_V(iptr)) {
406       case 4:
407           ap.protocol = iptr->ip_p;
408           /* Add the addresses to be resolved */
409           /* The IPv4 address is embedded in a in6_addr structure,
410            * so it need be copied, and delivered to resolve(). */
411           memset(&scribdst, '\0', sizeof(scribdst));
412           memcpy(&scribdst, &iptr->ip_dst, sizeof(struct in_addr));
413           resolve(ap.af, &scribdst, NULL, 0);
414           memset(&scribsrc, '\0', sizeof(scribsrc));
415           memcpy(&scribsrc, &iptr->ip_src, sizeof(struct in_addr));
416           resolve(ap.af, &scribsrc, NULL, 0);
417           break;
418       case 6:
419           ap.protocol = ip6tr->ip6_nxt;
420           /* Add the addresses to be resolved */
421           resolve(ap.af, &ip6tr->ip6_dst, NULL, 0);
422           resolve(ap.af, &ip6tr->ip6_src, NULL, 0);
423       default:
424           break;
425     }
426
427
428     if(hash_find(history, &ap, u_ht.void_pp) == HASH_STATUS_KEY_NOT_FOUND) {
429         ht = history_create();
430         hash_insert(history, &ap, ht);
431     }
432
433     /* Do accounting. */
434     switch (options.bandwidth_unit) {
435       case OPTION_BW_BITS:
436       case OPTION_BW_BYTES:
437           switch (IP_V(iptr)) {
438             case 4:
439                 len = ntohs(iptr->ip_len);
440                 break;
441             case 6:
442                 len = ntohs(ip6tr->ip6_plen) + 40;
443             default:
444                 break;
445           }
446           break;
447       case OPTION_BW_PKTS:
448           len = 1;
449           break;
450     }
451
452     /* Update record */
453     ht->last_write = history_pos;
454     if( ((IP_V(iptr) == 4) && (iptr->ip_src.s_addr == ap.src.s_addr))
455        || ((IP_V(iptr) == 6) && !memcmp(&ip6tr->ip6_src, &ap.src6, sizeof(ap.src6))) )
456     {
457         ht->sent[history_pos] += len;
458         ht->total_sent += len;
459     }
460     else {
461         ht->recv[history_pos] += len;
462         ht->total_recv += len;
463     }
464
465     if(direction == 0) {
466         /* incoming */
467         history_totals.recv[history_pos] += len;
468         history_totals.total_recv += len;
469     }
470     else {
471         history_totals.sent[history_pos] += len;
472         history_totals.total_sent += len;
473     }
474     
475 }
476
477 static void handle_raw_packet(unsigned char* args, const struct pcap_pkthdr* pkthdr, const unsigned char* packet)
478 {
479     handle_ip_packet((struct ip*)packet, -1);
480 }
481
482 #ifdef DLT_PFLOG
483 static void handle_pflog_packet(unsigned char* args, const struct pcap_pkthdr* pkthdr, const unsigned char* packet)
484 {
485         register u_int length = pkthdr->len;
486         u_int hdrlen;
487         const struct pfloghdr *hdr;
488         
489         hdr = (struct pfloghdr *)packet;
490         hdrlen = BPF_WORDALIGN(hdr->length);
491         length -= hdrlen;
492         packet += hdrlen;
493         handle_ip_packet((struct ip*)packet, -1);
494 }
495 #endif
496
497 static void handle_null_packet(unsigned char* args, const struct pcap_pkthdr* pkthdr, const unsigned char* packet)
498 {
499     handle_ip_packet((struct ip*)(packet + 4), -1);
500 }
501
502 static void handle_llc_packet(const struct llc* llc, int dir) {
503
504     struct ip* ip = (struct ip*)((void*)llc + sizeof(struct llc));
505
506     /* Taken from tcpdump/print-llc.c */
507     if(llc->ssap == LLCSAP_SNAP && llc->dsap == LLCSAP_SNAP
508        && llc->llcui == LLC_UI) {
509         u_int32_t orgcode;
510         u_int16_t et;
511         orgcode = EXTRACT_24BITS(&llc->llc_orgcode[0]);
512         et = (llc->llc_ethertype[0] << 8) + llc->llc_ethertype[1];
513         switch(orgcode) {
514           case OUI_ENCAP_ETHER:
515           case OUI_CISCO_90:
516             handle_ip_packet(ip, dir);
517             break;
518           case OUI_APPLETALK:
519             if(et == ETHERTYPE_ATALK) {
520               handle_ip_packet(ip, dir);
521             }
522             break;
523           default:;
524             /* Not a lot we can do */
525         }
526     }
527 }
528
529 static void handle_tokenring_packet(unsigned char* args, const struct pcap_pkthdr* pkthdr, const unsigned char* packet)
530 {
531     struct token_header *trp;
532     int dir = -1;
533     trp = (struct token_header *)packet;
534
535     if(IS_SOURCE_ROUTED(trp)) {
536       packet += RIF_LENGTH(trp);
537     }
538     packet += TOKEN_HDRLEN;
539
540     if(memcmp(trp->token_shost, if_hw_addr, 6) == 0 ) {
541       /* packet leaving this i/f */
542       dir = 1;
543     } 
544         else if(memcmp(trp->token_dhost, if_hw_addr, 6) == 0 || memcmp("\xFF\xFF\xFF\xFF\xFF\xFF", trp->token_dhost, 6) == 0) {
545       /* packet entering this i/f */
546       dir = 0;
547     }
548
549     /* Only know how to deal with LLC encapsulated packets */
550     if(FRAME_TYPE(trp) == TOKEN_FC_LLC) {
551       handle_llc_packet((struct llc*)packet, dir);
552     }
553 }
554
555 static void handle_ppp_packet(unsigned char* args, const struct pcap_pkthdr* pkthdr, const unsigned char* packet)
556 {
557         register u_int length = pkthdr->len;
558         register u_int caplen = pkthdr->caplen;
559         u_int proto;
560
561         if (caplen < 2) 
562         return;
563
564         if(packet[0] == PPP_ADDRESS) {
565                 if (caplen < 4) 
566             return;
567
568                 packet += 2;
569                 length -= 2;
570
571                 proto = EXTRACT_16BITS(packet);
572                 packet += 2;
573                 length -= 2;
574
575         if(proto == PPP_IP || proto == ETHERTYPE_IP || proto == ETHERTYPE_IPV6) {
576             handle_ip_packet((struct ip*)packet, -1);
577         }
578     }
579 }
580
581 #ifdef DLT_LINUX_SLL
582 static void handle_cooked_packet(unsigned char *args, const struct pcap_pkthdr * thdr, const unsigned char * packet)
583 {
584     struct sll_header *sptr;
585     int dir = -1;
586     sptr = (struct sll_header *) packet;
587
588     switch (ntohs(sptr->sll_pkttype))
589     {
590     case LINUX_SLL_HOST:
591         /*entering this interface*/
592         dir = 0;
593         break;
594     case LINUX_SLL_OUTGOING:
595         /*leaving this interface */
596         dir=1;
597         break;
598     }
599     handle_ip_packet((struct ip*)(packet+SLL_HDR_LEN), dir);
600 }
601 #endif /* DLT_LINUX_SLL */
602
603 static void handle_eth_packet(unsigned char* args, const struct pcap_pkthdr* pkthdr, const unsigned char* packet)
604 {
605     struct ether_header *eptr;
606     int ether_type;
607     const unsigned char *payload;
608     eptr = (struct ether_header*)packet;
609     ether_type = ntohs(eptr->ether_type);
610     payload = packet + sizeof(struct ether_header);
611
612     if(ether_type == ETHERTYPE_8021Q) {
613         struct vlan_8021q_header* vptr;
614         vptr = (struct vlan_8021q_header*)payload;
615         ether_type = ntohs(vptr->ether_type);
616         payload += sizeof(struct vlan_8021q_header);
617     }
618
619     if(ether_type == ETHERTYPE_IP || ether_type == ETHERTYPE_IPV6) {
620         struct ip* iptr;
621         int dir = -1;
622         
623         /*
624          * Is a direction implied by the MAC addresses?
625          */
626         if(have_hw_addr && memcmp(eptr->ether_shost, if_hw_addr, 6) == 0 ) {
627             /* packet leaving this i/f */
628             dir = 1;
629         }
630         else if(have_hw_addr && memcmp(eptr->ether_dhost, if_hw_addr, 6) == 0 ) {
631             /* packet entering this i/f */
632             dir = 0;
633         }
634         else if (memcmp("\xFF\xFF\xFF\xFF\xFF\xFF", eptr->ether_dhost, 6) == 0) {
635             /* broadcast packet, count as incoming */
636             dir = 0;
637         }
638
639         /* Distinguishing ip_hdr and ip6_hdr will be done later. */
640         iptr = (struct ip*)(payload); /* alignment? */
641         handle_ip_packet(iptr, dir);
642     }
643 }
644
645 #ifdef DLT_IEEE802_11_RADIO
646 /*
647  * Packets with a bonus radiotap header.
648  * See http://www.gsp.com/cgi-bin/man.cgi?section=9&topic=ieee80211_radiotap
649  */
650 static void handle_radiotap_packet(unsigned char* args, const struct pcap_pkthdr* pkthdr, const unsigned char* packet)
651 {
652     /* 802.11 MAC header is = 34 bytes (not sure if that's universally true) */
653     /* We could try harder to figure out hardware direction from the MAC header */
654     handle_ip_packet((struct ip*)(packet + ((struct radiotap_header *)packet)->it_len + 34),-1);
655 }
656
657
658 #endif
659
660 /* set_filter_code:
661  * Install some filter code. Returns NULL on success or an error message on
662  * failure. */
663 char *set_filter_code(const char *filter) {
664     char *x;
665     if (filter) {
666         x = xmalloc(strlen(filter) + sizeof "() and (ip or ip6)");
667         sprintf(x, "(%s) and (ip or ip6)", filter);
668     } else
669         x = xstrdup("ip or ip6");
670     if (pcap_compile(pd, &pcap_filter, x, 1, 0) == -1) {
671         xfree(x);
672         return pcap_geterr(pd);
673     }
674     xfree(x);
675     if (pcap_setfilter(pd, &pcap_filter) == -1)
676         return pcap_geterr(pd);
677     else
678         return NULL;
679 }
680
681
682
683 /*
684  * packet_init:
685  *
686  * performs pcap initialisation, called before ui is initialised
687  */
688 void packet_init() {
689     char errbuf[PCAP_ERRBUF_SIZE];
690     char *m;
691     int i;
692     int dlt;
693     int result;
694
695 #ifdef HAVE_DLPI
696     result = get_addrs_dlpi(options.interface, if_hw_addr, &if_ip_addr);
697 #else
698     result = get_addrs_ioctl(options.interface, if_hw_addr,
699           &if_ip_addr, &if_ip6_addr);
700 #endif
701
702     if (result < 0) {
703       exit(1);
704     }
705
706     have_hw_addr = result & 0x01;
707     have_ip_addr = result & 0x02;
708     have_ip6_addr = result & 0x04;
709     
710     if(have_ip_addr) {
711       fprintf(stderr, "IP address is: %s\n", inet_ntoa(if_ip_addr));
712     }
713     if(have_ip6_addr) {
714        char ip6str[INET6_ADDRSTRLEN];
715
716        ip6str[0] = '\0';
717        inet_ntop(AF_INET6, &if_ip6_addr, ip6str, sizeof(ip6str));
718        fprintf(stderr, "IPv6 address is: %s\n", ip6str);
719     }
720
721     if(have_hw_addr) {
722       fprintf(stderr, "MAC address is:");
723       for (i = 0; i < 6; ++i)
724         fprintf(stderr, "%c%02x", i ? ':' : ' ', (unsigned int)if_hw_addr[i]);
725       fprintf(stderr, "\n");
726     }
727     
728     //    exit(0);
729     resolver_initialise();
730
731     pd = pcap_open_live(options.interface, CAPTURE_LENGTH, options.promiscuous, 1000, errbuf);
732     // DEBUG: pd = pcap_open_offline("tcpdump.out", errbuf);
733     if(pd == NULL) { 
734         fprintf(stderr, "pcap_open_live(%s): %s\n", options.interface, errbuf); 
735         exit(1);
736     }
737     dlt = pcap_datalink(pd);
738     if(dlt == DLT_EN10MB) {
739         packet_handler = handle_eth_packet;
740     }
741 #ifdef DLT_PFLOG
742     else if (dlt == DLT_PFLOG) {
743                 packet_handler = handle_pflog_packet;
744     }
745 #endif
746     else if(dlt == DLT_RAW) {
747         packet_handler = handle_raw_packet;
748     } 
749     else if(dlt == DLT_NULL) {
750         packet_handler = handle_null_packet;
751     } 
752 #ifdef DLT_LOOP
753     else if(dlt == DLT_LOOP) {
754         packet_handler = handle_null_packet;
755     }
756 #endif
757 #ifdef DLT_IEEE802_11_RADIO
758     else if(dlt == DLT_IEEE802_11_RADIO) {
759         packet_handler = handle_radiotap_packet;
760     }
761 #endif
762     else if(dlt == DLT_IEEE802) {
763         packet_handler = handle_tokenring_packet;
764     }
765     else if(dlt == DLT_PPP) {
766         packet_handler = handle_ppp_packet;
767     }
768 /* 
769  * SLL support not available in older libpcaps
770  */
771 #ifdef DLT_LINUX_SLL
772     else if(dlt == DLT_LINUX_SLL) {
773       packet_handler = handle_cooked_packet;
774     }
775 #endif
776     else {
777         fprintf(stderr, "Unsupported datalink type: %d\n"
778                 "Please email pdw@ex-parrot.com, quoting the datalink type and what you were\n"
779                 "trying to do at the time\n.", dlt);
780         exit(1);
781     }
782
783     if ((m = set_filter_code(options.filtercode))) {
784         fprintf(stderr, "set_filter_code: %s\n", m);
785         exit(1);
786         return;
787     }
788 }
789
790 /* packet_loop:
791  * Worker function for packet capture thread. */
792 void packet_loop(void* ptr) {
793     pcap_loop(pd,-1,(pcap_handler)packet_handler,NULL);
794 }
795
796
797 /* main:
798  * Entry point. See usage(). */
799 int main(int argc, char **argv) {
800     pthread_t thread;
801     struct sigaction sa = {};
802
803     setlocale(LC_ALL, "");
804
805     /* TODO: tidy this up */
806     /* read command line options and config file */   
807     config_init();
808     options_set_defaults();
809     options_read_args(argc, argv);
810     /* If a config was explicitly specified, whinge if it can't be found */
811     read_config(options.config_file, options.config_file_specified);
812     options_make();
813     
814     sa.sa_handler = finish;
815     sigaction(SIGINT, &sa, NULL);
816
817     pthread_mutex_init(&tick_mutex, NULL);
818
819     packet_init();
820
821     init_history();
822
823     if (options.no_curses) {
824       tui_init();
825     }
826     else {
827       ui_init();
828     }
829
830     pthread_create(&thread, NULL, (void*)&packet_loop, NULL);
831
832     /* Keep the starting time (used for timed termination) */
833     first_timestamp = time(NULL);
834
835     if (options.no_curses) {
836       if (options.timed_output) {
837         while(!foad) {
838           sleep(1);
839         }
840       }
841       else {
842         tui_loop();
843       }
844     }
845     else {
846       ui_loop();
847     }
848
849     pthread_cancel(thread);
850
851     ui_finish();
852     
853     return 0;
854 }