1 /*
2 * collectd - src/utils_dns.c
3 * Modifications Copyright (C) 2006 Florian octo Forster
4 * Copyright (C) 2002 The Measurement Factory, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 * 3. Neither the name of The Measurement Factory nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * Authors:
32 * The Measurement Factory, Inc. <http://www.measurement-factory.com/>
33 * Florian octo Forster <octo at verplant.org>
34 */
36 #include <sys/types.h>
37 #include <sys/time.h>
38 #include <sys/stat.h>
40 #include <netinet/in.h>
42 #include <pcap.h>
43 #include <signal.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <time.h>
49 #include <ctype.h>
50 #include <curses.h>
51 #include <assert.h>
52 #include <arpa/inet.h>
53 #include <arpa/nameser.h>
54 #ifdef __APPLE__
55 #include <arpa/nameser_compat.h>
56 #endif
58 #include <sys/socket.h>
59 #include <net/if_arp.h>
60 #include <net/if.h>
61 #include <netinet/if_ether.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/ip6.h>
66 #include <netinet/udp.h>
68 #include <netdb.h>
70 #define PCAP_SNAPLEN 1460
71 #ifndef ETHER_HDR_LEN
72 #define ETHER_ADDR_LEN 6
73 #define ETHER_TYPE_LEN 2
74 #define ETHER_HDR_LEN (ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN)
75 #endif
76 #ifndef ETHERTYPE_8021Q
77 # define ETHERTYPE_8021Q 0x8100
78 #endif
79 #ifndef ETHERTYPE_IPV6
80 # define ETHERTYPE_IPV6 0x86DD
81 #endif
83 #include <net/if_ppp.h>
84 #ifndef PPP_ADDRESS_VAL
85 # define PPP_ADDRESS_VAL 0xff /* The address byte value */
86 #endif
87 #ifndef PPP_CONTROL_VAL
88 # define PPP_CONTROL_VAL 0x03 /* The control byte value */
89 #endif
91 #ifdef __linux__
92 #define uh_sport source
93 #define uh_dport dest
94 #endif
96 #include "utils_dns.h"
98 /*
99 * Type definitions
100 */
101 struct ip_list_s
102 {
103 struct in6_addr addr;
104 void *data;
105 struct ip_list_s *next;
106 };
107 typedef struct ip_list_s ip_list_t;
109 typedef int (printer)(const char *, ...);
111 /*
112 * flags/features for non-interactive mode
113 */
115 #ifndef T_A6
116 #define T_A6 38
117 #endif
118 #ifndef T_SRV
119 #define T_SRV 33
120 #endif
122 /*
123 * Global variables
124 */
125 int qtype_counts[T_MAX];
126 int opcode_counts[OP_MAX];
127 int qclass_counts[C_MAX];
129 static pcap_t *pcap_obj = NULL;
131 static ip_list_t *IgnoreList = NULL;
133 static void (*Callback) (const rfc1035_header_t *) = NULL;
135 static int query_count_intvl = 0;
136 static int query_count_total = 0;
137 #ifdef __OpenBSD__
138 static struct bpf_timeval last_ts;
139 #else
140 static struct timeval last_ts;
141 #endif
143 static int cmp_in6_addr (const struct in6_addr *a,
144 const struct in6_addr *b)
145 {
146 int i;
148 for (i = 0; i < 4; i++)
149 if (a->s6_addr32[i] != b->s6_addr32[i])
150 break;
152 if (i >= 4)
153 return (0);
155 return (a->s6_addr32[i] > b->s6_addr32[i] ? 1 : -1);
156 } /* int cmp_addrinfo */
158 static inline int ignore_list_match (const struct in6_addr *addr)
159 {
160 ip_list_t *ptr;
162 for (ptr = IgnoreList; ptr != NULL; ptr = ptr->next)
163 if (cmp_in6_addr (addr, &ptr->addr) == 0)
164 return (1);
165 return (0);
166 } /* int ignore_list_match */
168 static void ignore_list_add (const struct in6_addr *addr)
169 {
170 ip_list_t *new;
172 if (ignore_list_match (addr) != 0)
173 return;
175 new = malloc (sizeof (ip_list_t));
176 if (new == NULL)
177 {
178 perror ("malloc");
179 return;
180 }
182 memcpy (&new->addr, addr, sizeof (struct in6_addr));
183 new->next = IgnoreList;
185 IgnoreList = new;
186 } /* void ignore_list_add */
188 void ignore_list_add_name (const char *name)
189 {
190 struct addrinfo *ai_list;
191 struct addrinfo *ai_ptr;
192 struct in6_addr addr;
193 int status;
195 status = getaddrinfo (name, NULL, NULL, &ai_list);
196 if (status != 0)
197 return;
199 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
200 {
201 if (ai_ptr->ai_family == AF_INET)
202 {
203 addr.s6_addr32[0] = 0;
204 addr.s6_addr32[1] = 0;
205 addr.s6_addr32[2] = htonl (0x0000FFFF);
206 addr.s6_addr32[3] = ((struct sockaddr_in *) ai_ptr->ai_addr)->sin_addr.s_addr;
208 ignore_list_add (&addr);
209 }
210 else
211 {
212 ignore_list_add (&((struct sockaddr_in6 *) ai_ptr->ai_addr)->sin6_addr);
213 }
214 } /* for */
216 freeaddrinfo (ai_list);
217 }
219 static void in6_addr_from_buffer (struct in6_addr *ia,
220 const void *buf, size_t buf_len,
221 int family)
222 {
223 memset (ia, 0, sizeof (struct in6_addr));
224 if ((AF_INET == family) && (sizeof (uint32_t) == buf_len))
225 {
226 ia->s6_addr32[2] = htonl (0x0000FFFF);
227 ia->s6_addr32[3] = *((uint32_t *) buf);
228 }
229 else if ((AF_INET6 == family) && (sizeof (struct in6_addr) == buf_len))
230 {
231 memcpy (ia, buf, buf_len);
232 }
233 } /* void in6_addr_from_buffer */
235 void dnstop_set_pcap_obj (pcap_t *po)
236 {
237 pcap_obj = po;
238 }
240 void dnstop_set_callback (void (*cb) (const rfc1035_header_t *))
241 {
242 Callback = cb;
243 }
245 #define RFC1035_MAXLABELSZ 63
246 static int
247 rfc1035NameUnpack(const char *buf, size_t sz, off_t * off, char *name, size_t ns
248 )
249 {
250 off_t no = 0;
251 unsigned char c;
252 size_t len;
253 assert(ns > 0);
254 do {
255 if ((*off) >= sz)
256 break;
257 c = *(buf + (*off));
258 if (c > 191) {
259 /* blasted compression */
260 unsigned short s;
261 off_t ptr;
262 memcpy(&s, buf + (*off), sizeof(s));
263 s = ntohs(s);
264 (*off) += sizeof(s);
265 /* Sanity check */
266 if ((*off) >= sz)
267 return 1;
268 ptr = s & 0x3FFF;
269 /* Make sure the pointer is inside this message */
270 if (ptr >= sz)
271 return 2;
272 return rfc1035NameUnpack(buf, sz, &ptr, name + no, ns - no);
273 } else if (c > RFC1035_MAXLABELSZ) {
274 /*
275 * "(The 10 and 01 combinations are reserved for future use.)"
276 */
277 break;
278 return 3;
279 } else {
280 (*off)++;
281 len = (size_t) c;
282 if (len == 0)
283 break;
284 if (len > (ns - 1))
285 len = ns - 1;
286 if ((*off) + len > sz) /* message is too short */
287 return 4;
288 memcpy(name + no, buf + (*off), len);
289 (*off) += len;
290 no += len;
291 *(name + (no++)) = '.';
292 }
293 } while (c > 0);
294 *(name + no - 1) = '\0';
295 /* make sure we didn't allow someone to overflow the name buffer */
296 assert(no <= ns);
297 return 0;
298 }
300 static int
301 handle_dns(const char *buf, int len,
302 const struct in6_addr *s_addr,
303 const struct in6_addr *d_addr)
304 {
305 rfc1035_header_t qh;
306 uint16_t us;
307 off_t offset;
308 char *t;
309 int x;
311 /* The DNS header is 12 bytes long */
312 if (len < 12)
313 return 0;
315 memcpy(&us, buf + 0, 2);
316 qh.id = ntohs(us);
318 memcpy(&us, buf + 2, 2);
319 us = ntohs(us);
320 fprintf (stderr, "Bytes 0, 1: 0x%04hx\n", us);
321 qh.qr = (us >> 15) & 0x01;
322 qh.opcode = (us >> 11) & 0x0F;
323 qh.aa = (us >> 10) & 0x01;
324 qh.tc = (us >> 9) & 0x01;
325 qh.rd = (us >> 8) & 0x01;
326 qh.ra = (us >> 7) & 0x01;
327 qh.z = (us >> 6) & 0x01;
328 qh.ad = (us >> 5) & 0x01;
329 qh.cd = (us >> 4) & 0x01;
330 qh.rcode = us & 0x0F;
332 memcpy(&us, buf + 4, 2);
333 qh.qdcount = ntohs(us);
335 memcpy(&us, buf + 6, 2);
336 qh.ancount = ntohs(us);
338 memcpy(&us, buf + 8, 2);
339 qh.nscount = ntohs(us);
341 memcpy(&us, buf + 10, 2);
342 qh.arcount = ntohs(us);
344 offset = 12;
345 memset(qh.qname, '\0', MAX_QNAME_SZ);
346 x = rfc1035NameUnpack(buf, len, &offset, qh.qname, MAX_QNAME_SZ);
347 if (0 != x)
348 return 0;
349 if ('\0' == qh.qname[0])
350 strcpy(qh.qname, ".");
351 while ((t = strchr(qh.qname, '\n')))
352 *t = ' ';
353 while ((t = strchr(qh.qname, '\r')))
354 *t = ' ';
355 for (t = qh.qname; *t; t++)
356 *t = tolower(*t);
358 memcpy(&us, buf + offset, 2);
359 qh.qtype = ntohs(us);
360 memcpy(&us, buf + offset + 2, 2);
361 qh.qclass = ntohs(us);
363 qh.length = (uint16_t) len;
365 /* gather stats */
366 qtype_counts[qh.qtype]++;
367 qclass_counts[qh.qclass]++;
368 opcode_counts[qh.opcode]++;
370 if (Callback != NULL)
371 Callback (&qh);
373 return 1;
374 }
376 static int
377 handle_udp(const struct udphdr *udp, int len,
378 const struct in6_addr *s_addr,
379 const struct in6_addr *d_addr)
380 {
381 char buf[PCAP_SNAPLEN];
382 if ((ntohs (udp->uh_dport) != 53)
383 && (ntohs (udp->uh_sport) != 53))
384 return 0;
385 memcpy(buf, udp + 1, len - sizeof(*udp));
386 if (0 == handle_dns(buf, len - sizeof(*udp), s_addr, d_addr))
387 return 0;
388 return 1;
389 }
391 static int
392 handle_ipv6 (struct ip6_hdr *ipv6, int len)
393 {
394 char buf[PCAP_SNAPLEN];
395 int offset;
396 int nexthdr;
398 struct in6_addr s_addr;
399 struct in6_addr d_addr;
400 uint16_t payload_len;
402 offset = sizeof (struct ip6_hdr);
403 nexthdr = ipv6->ip6_nxt;
404 s_addr = ipv6->ip6_src;
405 d_addr = ipv6->ip6_dst;
406 payload_len = ntohs (ipv6->ip6_plen);
408 if (ignore_list_match (&s_addr))
409 return (0);
411 /* Parse extension headers. This only handles the standard headers, as
412 * defined in RFC 2460, correctly. Fragments are discarded. */
413 while ((IPPROTO_ROUTING == nexthdr) /* routing header */
414 || (IPPROTO_HOPOPTS == nexthdr) /* Hop-by-Hop options. */
415 || (IPPROTO_FRAGMENT == nexthdr) /* fragmentation header. */
416 || (IPPROTO_DSTOPTS == nexthdr) /* destination options. */
417 || (IPPROTO_DSTOPTS == nexthdr) /* destination options. */
418 || (IPPROTO_AH == nexthdr) /* destination options. */
419 || (IPPROTO_ESP == nexthdr)) /* encapsulating security payload. */
420 {
421 struct ip6_ext ext_hdr;
422 uint16_t ext_hdr_len;
424 /* Catch broken packets */
425 if ((offset + sizeof (struct ip6_ext)) > len)
426 return (0);
428 /* Cannot handle fragments. */
429 if (IPPROTO_FRAGMENT == nexthdr)
430 return (0);
432 memcpy (&ext_hdr, (char *) ipv6 + offset, sizeof (struct ip6_ext));
433 nexthdr = ext_hdr.ip6e_nxt;
434 ext_hdr_len = (8 * (ntohs (ext_hdr.ip6e_len) + 1));
436 /* This header is longer than the packets payload.. WTF? */
437 if (ext_hdr_len > payload_len)
438 return (0);
440 offset += ext_hdr_len;
441 payload_len -= ext_hdr_len;
442 } /* while */
444 /* Catch broken and empty packets */
445 if (((offset + payload_len) > len)
446 || (payload_len == 0)
447 || (payload_len > PCAP_SNAPLEN))
448 return (0);
450 if (IPPROTO_UDP != nexthdr)
451 return (0);
453 memcpy (buf, (char *) ipv6 + offset, payload_len);
454 if (handle_udp ((struct udphdr *) buf, payload_len, &s_addr, &d_addr) == 0)
455 return (0);
457 return (1); /* Success */
458 } /* int handle_ipv6 */
460 static int
461 handle_ip(const struct ip *ip, int len)
462 {
463 char buf[PCAP_SNAPLEN];
464 int offset = ip->ip_hl << 2;
465 struct in6_addr s_addr;
466 struct in6_addr d_addr;
468 if (ip->ip_v == 6)
469 return (handle_ipv6 ((struct ip6_hdr *) ip, len));
471 in6_addr_from_buffer (&s_addr, &ip->ip_src.s_addr, sizeof (ip->ip_src.s_addr), AF_INET);
472 in6_addr_from_buffer (&d_addr, &ip->ip_dst.s_addr, sizeof (ip->ip_dst.s_addr), AF_INET);
473 if (ignore_list_match (&s_addr))
474 return (0);
475 if (IPPROTO_UDP != ip->ip_p)
476 return 0;
477 memcpy(buf, (void *) ip + offset, len - offset);
478 if (0 == handle_udp((struct udphdr *) buf, len - offset, &s_addr, &d_addr))
479 return 0;
480 return 1;
481 }
483 static int
484 handle_ppp(const u_char * pkt, int len)
485 {
486 char buf[PCAP_SNAPLEN];
487 unsigned short us;
488 unsigned short proto;
489 if (len < 2)
490 return 0;
491 if (*pkt == PPP_ADDRESS_VAL && *(pkt + 1) == PPP_CONTROL_VAL) {
492 pkt += 2; /* ACFC not used */
493 len -= 2;
494 }
495 if (len < 2)
496 return 0;
497 if (*pkt % 2) {
498 proto = *pkt; /* PFC is used */
499 pkt++;
500 len--;
501 } else {
502 memcpy(&us, pkt, sizeof(us));
503 proto = ntohs(us);
504 pkt += 2;
505 len -= 2;
506 }
507 if (ETHERTYPE_IP != proto && PPP_IP != proto)
508 return 0;
509 memcpy(buf, pkt, len);
510 return handle_ip((struct ip *) buf, len);
511 }
513 static int
514 handle_null(const u_char * pkt, int len)
515 {
516 unsigned int family;
517 memcpy(&family, pkt, sizeof(family));
518 if (AF_INET != family)
519 return 0;
520 return handle_ip((struct ip *) (pkt + 4), len - 4);
521 }
523 #ifdef DLT_LOOP
524 static int
525 handle_loop(const u_char * pkt, int len)
526 {
527 unsigned int family;
528 memcpy(&family, pkt, sizeof(family));
529 if (AF_INET != ntohl(family))
530 return 0;
531 return handle_ip((struct ip *) (pkt + 4), len - 4);
532 }
534 #endif
536 #ifdef DLT_RAW
537 static int
538 handle_raw(const u_char * pkt, int len)
539 {
540 return handle_ip((struct ip *) pkt, len);
541 }
543 #endif
545 static int
546 handle_ether(const u_char * pkt, int len)
547 {
548 char buf[PCAP_SNAPLEN];
549 struct ether_header *e = (void *) pkt;
550 unsigned short etype = ntohs(e->ether_type);
551 if (len < ETHER_HDR_LEN)
552 return 0;
553 pkt += ETHER_HDR_LEN;
554 len -= ETHER_HDR_LEN;
555 if (ETHERTYPE_8021Q == etype) {
556 etype = ntohs(*(unsigned short *) (pkt + 2));
557 pkt += 4;
558 len -= 4;
559 }
560 if ((ETHERTYPE_IP != etype)
561 && (ETHERTYPE_IPV6 != etype))
562 return 0;
563 memcpy(buf, pkt, len);
564 if (ETHERTYPE_IPV6 == etype)
565 return (handle_ipv6 ((struct ip6_hdr *) buf, len));
566 else
567 return handle_ip((struct ip *) buf, len);
568 }
570 /* public function */
571 void handle_pcap(u_char *udata, const struct pcap_pkthdr *hdr, const u_char *pkt)
572 {
573 int status;
575 fprintf (stderr, "handle_pcap (udata = %p, hdr = %p, pkt = %p): hdr->caplen = %i\n",
576 (void *) udata, (void *) hdr, (void *) pkt,
577 hdr->caplen);
579 if (hdr->caplen < ETHER_HDR_LEN)
580 return;
582 switch (pcap_datalink (pcap_obj))
583 {
584 case DLT_EN10MB:
585 status = handle_ether (pkt, hdr->caplen);
586 break;
587 case DLT_PPP:
588 status = handle_ppp (pkt, hdr->caplen);
589 break;
590 #ifdef DLT_LOOP
591 case DLT_LOOP:
592 status = handle_loop (pkt, hdr->caplen);
593 break;
594 #endif
595 #ifdef DLT_RAW
596 case DLT_RAW:
597 status = handle_raw (pkt, hdr->caplen);
598 break;
599 #endif
600 case DLT_NULL:
601 status = handle_null (pkt, hdr->caplen);
602 break;
604 default:
605 fprintf (stderr, "unsupported data link type %d\n",
606 pcap_datalink(pcap_obj));
607 status = 0;
608 break;
609 } /* switch (pcap_datalink(pcap_obj)) */
611 if (0 == status)
612 return;
614 query_count_intvl++;
615 query_count_total++;
616 last_ts = hdr->ts;
617 }
619 const char *qtype_str(int t)
620 {
621 static char buf[32];
622 switch (t) {
623 case ns_t_a: return ("A");
624 case ns_t_ns: return ("NS");
625 case ns_t_md: return ("MD");
626 case ns_t_mf: return ("MF");
627 case ns_t_cname: return ("CNAME");
628 case ns_t_soa: return ("SOA");
629 case ns_t_mb: return ("MB");
630 case ns_t_mg: return ("MG");
631 case ns_t_mr: return ("MR");
632 case ns_t_null: return ("NULL");
633 case ns_t_wks: return ("WKS");
634 case ns_t_ptr: return ("PTR");
635 case ns_t_hinfo: return ("HINFO");
636 case ns_t_minfo: return ("MINFO");
637 case ns_t_mx: return ("MX");
638 case ns_t_txt: return ("TXT");
639 case ns_t_rp: return ("RP");
640 case ns_t_afsdb: return ("AFSDB");
641 case ns_t_x25: return ("X25");
642 case ns_t_isdn: return ("ISDN");
643 case ns_t_rt: return ("RT");
644 case ns_t_nsap: return ("NSAP");
645 case ns_t_nsap_ptr: return ("NSAP-PTR");
646 case ns_t_sig: return ("SIG");
647 case ns_t_key: return ("KEY");
648 case ns_t_px: return ("PX");
649 case ns_t_gpos: return ("GPOS");
650 case ns_t_aaaa: return ("AAAA");
651 case ns_t_loc: return ("LOC");
652 case ns_t_nxt: return ("NXT");
653 case ns_t_eid: return ("EID");
654 case ns_t_nimloc: return ("NIMLOC");
655 case ns_t_srv: return ("SRV");
656 case ns_t_atma: return ("ATMA");
657 case ns_t_naptr: return ("NAPTR");
658 case ns_t_kx: return ("KX");
659 case ns_t_cert: return ("CERT");
660 case ns_t_a6: return ("A6");
661 case ns_t_dname: return ("DNAME");
662 case ns_t_sink: return ("SINK");
663 case ns_t_opt: return ("OPT");
664 case ns_t_tsig: return ("TSIG");
665 case ns_t_ixfr: return ("IXFR");
666 case ns_t_axfr: return ("AXFR");
667 case ns_t_mailb: return ("MAILB");
668 case ns_t_maila: return ("MAILA");
669 case ns_t_any: return ("ANY");
670 case ns_t_zxfr: return ("ZXFR");
671 default:
672 snprintf (buf, 32, "#%i", t);
673 buf[31] = '\0';
674 return (buf);
675 }; /* switch (t) */
676 /* NOTREACHED */
677 return (NULL);
678 }
680 const char *opcode_str (int o)
681 {
682 static char buf[30];
683 switch (o) {
684 case 0:
685 return "Query";
686 break;
687 case 1:
688 return "Iquery";
689 break;
690 case 2:
691 return "Status";
692 break;
693 case 4:
694 return "Notify";
695 break;
696 case 5:
697 return "Update";
698 break;
699 default:
700 snprintf(buf, 30, "Opcode%d", o);
701 return buf;
702 }
703 /* NOTREACHED */
704 }
706 const char *rcode_str (int rcode)
707 {
708 static char buf[32];
709 switch (rcode)
710 {
711 case ns_r_noerror: return ("NOERROR");
712 case ns_r_formerr: return ("FORMERR");
713 case ns_r_servfail: return ("SERVFAIL");
714 case ns_r_nxdomain: return ("NXDOMAIN");
715 case ns_r_notimpl: return ("NOTIMPL");
716 case ns_r_refused: return ("REFUSED");
717 case ns_r_yxdomain: return ("YXDOMAIN");
718 case ns_r_yxrrset: return ("YXRRSET");
719 case ns_r_nxrrset: return ("NXRRSET");
720 case ns_r_notauth: return ("NOTAUTH");
721 case ns_r_notzone: return ("NOTZONE");
722 case ns_r_max: return ("MAX");
723 case ns_r_badsig: return ("BADSIG");
724 case ns_r_badkey: return ("BADKEY");
725 case ns_r_badtime: return ("BADTIME");
726 default:
727 snprintf (buf, 32, "RCode%i", rcode);
728 buf[31] = '\0';
729 return (buf);
730 }
731 /* Never reached */
732 return (NULL);
733 } /* const char *rcode_str (int rcode) */
735 #if 0
736 static int
737 main(int argc, char *argv[])
738 {
739 char errbuf[PCAP_ERRBUF_SIZE];
740 int x;
741 struct stat sb;
742 int readfile_state = 0;
743 struct bpf_program fp;
745 port53 = htons(53);
746 SubReport = Sources_report;
747 ignore_addr.s_addr = 0;
748 progname = strdup(strrchr(argv[0], '/') ? strchr(argv[0], '/') + 1 : argv[0]);
749 srandom(time(NULL));
750 ResetCounters();
752 while ((x = getopt(argc, argv, "ab:f:i:pst")) != -1) {
753 switch (x) {
754 case 'a':
755 anon_flag = 1;
756 break;
757 case 's':
758 sld_flag = 1;
759 break;
760 case 't':
761 nld_flag = 1;
762 break;
763 case 'p':
764 promisc_flag = 0;
765 break;
766 case 'b':
767 bpf_program_str = strdup(optarg);
768 break;
769 case 'i':
770 ignore_addr.s_addr = inet_addr(optarg);
771 break;
772 case 'f':
773 set_filter(optarg);
774 break;
775 default:
776 usage();
777 break;
778 }
779 }
780 argc -= optind;
781 argv += optind;
783 if (argc < 1)
784 usage();
785 device = strdup(argv[0]);
787 if (0 == stat(device, &sb))
788 readfile_state = 1;
789 if (readfile_state) {
790 pcap_obj = pcap_open_offline(device, errbuf);
791 } else {
792 pcap_obj = pcap_open_live(device, PCAP_SNAPLEN, promisc_flag, 1000, errbuf);
793 }
794 if (NULL == pcap_obj) {
795 fprintf(stderr, "pcap_open_*: %s\n", errbuf);
796 exit(1);
797 }
799 if (0 == isatty(1)) {
800 if (0 == readfile_state) {
801 fprintf(stderr, "Non-interactive mode requires savefile argument\n");
802 exit(1);
803 }
804 interactive = 0;
805 print_func = printf;
806 }
808 memset(&fp, '\0', sizeof(fp));
809 x = pcap_compile(pcap_obj, &fp, bpf_program_str, 1, 0);
810 if (x < 0) {
811 fprintf(stderr, "pcap_compile failed\n");
812 exit(1);
813 }
814 x = pcap_setfilter(pcap_obj, &fp);
815 if (x < 0) {
816 fprintf(stderr, "pcap_setfilter failed\n");
817 exit(1);
818 }
820 /*
821 * non-blocking call added for Mac OS X bugfix. Sent by Max Horn.
822 * ref http://www.tcpdump.org/lists/workers/2002/09/msg00033.html
823 */
824 x = pcap_setnonblock(pcap_obj, 1, errbuf);
825 if (x < 0) {
826 fprintf(stderr, "pcap_setnonblock failed: %s\n", errbuf);
827 exit(1);
828 }
830 switch (pcap_datalink(pcap_obj)) {
831 case DLT_EN10MB:
832 handle_datalink = handle_ether;
833 break;
834 #if USE_PPP
835 case DLT_PPP:
836 handle_datalink = handle_ppp;
837 break;
838 #endif
839 #ifdef DLT_LOOP
840 case DLT_LOOP:
841 handle_datalink = handle_loop;
842 break;
843 #endif
844 #ifdef DLT_RAW
845 case DLT_RAW:
846 handle_datalink = handle_raw;
847 break;
848 #endif
849 case DLT_NULL:
850 handle_datalink = handle_null;
851 break;
852 default:
853 fprintf(stderr, "unsupported data link type %d\n",
854 pcap_datalink(pcap_obj));
855 return 1;
856 break;
857 }
858 if (interactive) {
859 init_curses();
860 while (0 == Quit) {
861 if (readfile_state < 2) {
862 /*
863 * On some OSes select() might return 0 even when
864 * there are packets to process. Thus, we always
865 * ignore its return value and just call pcap_dispatch()
866 * anyway.
867 */
868 if (0 == readfile_state) /* interactive */
869 pcap_select(pcap_obj, 1, 0);
870 x = pcap_dispatch(pcap_obj, 50, handle_pcap, NULL);
871 }
872 if (0 == x && 1 == readfile_state) {
873 /* block on keyboard until user quits */
874 readfile_state++;
875 nodelay(w, 0);
876 }
877 keyboard();
878 cron_pre();
879 report();
880 cron_post();
881 }
882 endwin(); /* klin, Thu Nov 28 08:56:51 2002 */
883 } else {
884 while (pcap_dispatch(pcap_obj, 50, handle_pcap, NULL))
885 (void) 0;
886 cron_pre();
887 Sources_report(); print_func("\n");
888 Destinatioreport(); print_func("\n");
889 Qtypes_report(); print_func("\n");
890 Opcodes_report(); print_func("\n");
891 Tld_report(); print_func("\n");
892 Sld_report(); print_func("\n");
893 Nld_report(); print_func("\n");
894 SldBySource_report();
895 }
897 pcap_close(pcap_obj);
898 return 0;
899 } /* static int main(int argc, char *argv[]) */
900 #endif