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