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 "collectd.h"
38 #if HAVE_NETINET_IN_SYSTM_H
39 # include <netinet/in_systm.h>
40 #endif
41 #if HAVE_NETINET_IN_H
42 # include <netinet/in.h>
43 #endif
44 #if HAVE_PCAP_H
45 # include <pcap.h>
46 #endif
47 #if HAVE_ARPA_INET_H
48 # include <arpa/inet.h>
49 #endif
51 #if HAVE_ARPA_NAMESER_H
52 # include <arpa/nameser.h>
53 #elif HAVE_ARPA_NAMESER_COMPAT_H
54 # include <arpa/nameser_compat.h>
55 #endif
57 #if HAVE_NET_IF_ARP_H
58 # include <net/if_arp.h>
59 #endif
60 #if HAVE_NET_IF_H
61 # include <net/if.h>
62 #endif
63 #if HAVE_NETINET_IF_ETHER_H
64 # include <netinet/if_ether.h>
65 #endif
66 #if HAVE_NET_IF_PPP_H
67 # include <net/if_ppp.h>
68 #endif
70 #if HAVE_SYS_SOCKET_H
71 # include <sys/socket.h>
72 #endif
73 #if HAVE_NETDB_H
74 # include <netdb.h>
75 #endif
77 #if HAVE_NETINET_IP_H
78 # include <netinet/ip.h>
79 #endif
80 #ifdef HAVE_NETINET_IP_VAR_H
81 # include <netinet/ip_var.h>
82 #endif
83 #if HAVE_NETINET_IP6_H
84 # include <netinet/ip6.h>
85 #endif
86 #if HAVE_NETINET_UDP_H
87 # include <netinet/udp.h>
88 #endif
90 #define PCAP_SNAPLEN 1460
91 #ifndef ETHER_HDR_LEN
92 #define ETHER_ADDR_LEN 6
93 #define ETHER_TYPE_LEN 2
94 #define ETHER_HDR_LEN (ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN)
95 #endif
96 #ifndef ETHERTYPE_8021Q
97 # define ETHERTYPE_8021Q 0x8100
98 #endif
99 #ifndef ETHERTYPE_IPV6
100 # define ETHERTYPE_IPV6 0x86DD
101 #endif
103 #ifndef PPP_ADDRESS_VAL
104 # define PPP_ADDRESS_VAL 0xff /* The address byte value */
105 #endif
106 #ifndef PPP_CONTROL_VAL
107 # define PPP_CONTROL_VAL 0x03 /* The control byte value */
108 #endif
110 #if HAVE_STRUCT_UDPHDR_UH_DPORT && HAVE_STRUCT_UDPHDR_UH_SPORT
111 # define UDP_DEST uh_dport
112 # define UDP_SRC uh_dport
113 #elif HAVE_STRUCT_UDPHDR_DEST && HAVE_STRUCT_UDPHDR_SOURCE
114 # define UDP_DEST dest
115 # define UDP_SRC source
116 #else
117 # error "`struct udphdr' is unusable."
118 #endif
120 #include "utils_dns.h"
122 /*
123 * Type definitions
124 */
125 struct ip_list_s
126 {
127 struct in6_addr addr;
128 void *data;
129 struct ip_list_s *next;
130 };
131 typedef struct ip_list_s ip_list_t;
133 typedef int (printer)(const char *, ...);
135 /*
136 * flags/features for non-interactive mode
137 */
139 #ifndef T_A6
140 #define T_A6 38
141 #endif
142 #ifndef T_SRV
143 #define T_SRV 33
144 #endif
146 /*
147 * Global variables
148 */
149 int qtype_counts[T_MAX];
150 int opcode_counts[OP_MAX];
151 int qclass_counts[C_MAX];
153 #if HAVE_PCAP_H
154 static pcap_t *pcap_obj = NULL;
155 #endif
157 static ip_list_t *IgnoreList = NULL;
159 #if HAVE_PCAP_H
160 static void (*Callback) (const rfc1035_header_t *) = NULL;
162 static int query_count_intvl = 0;
163 static int query_count_total = 0;
164 # ifdef __OpenBSD__
165 static struct bpf_timeval last_ts;
166 # else
167 static struct timeval last_ts;
168 # endif /* __OpenBSD__ */
169 #endif /* HAVE_PCAP_H */
171 static int cmp_in6_addr (const struct in6_addr *a,
172 const struct in6_addr *b)
173 {
174 int i;
176 assert (sizeof (struct in6_addr) == 16);
178 for (i = 0; i < 16; i++)
179 if (a->s6_addr[i] != b->s6_addr[i])
180 break;
182 if (i >= 16)
183 return (0);
185 return (a->s6_addr[i] > b->s6_addr[i] ? 1 : -1);
186 } /* int cmp_addrinfo */
188 static inline int ignore_list_match (const struct in6_addr *addr)
189 {
190 ip_list_t *ptr;
192 for (ptr = IgnoreList; ptr != NULL; ptr = ptr->next)
193 if (cmp_in6_addr (addr, &ptr->addr) == 0)
194 return (1);
195 return (0);
196 } /* int ignore_list_match */
198 static void ignore_list_add (const struct in6_addr *addr)
199 {
200 ip_list_t *new;
202 if (ignore_list_match (addr) != 0)
203 return;
205 new = malloc (sizeof (ip_list_t));
206 if (new == NULL)
207 {
208 perror ("malloc");
209 return;
210 }
212 memcpy (&new->addr, addr, sizeof (struct in6_addr));
213 new->next = IgnoreList;
215 IgnoreList = new;
216 } /* void ignore_list_add */
218 void ignore_list_add_name (const char *name)
219 {
220 struct addrinfo *ai_list;
221 struct addrinfo *ai_ptr;
222 struct in6_addr addr;
223 int status;
225 status = getaddrinfo (name, NULL, NULL, &ai_list);
226 if (status != 0)
227 return;
229 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
230 {
231 if (ai_ptr->ai_family == AF_INET)
232 {
233 memset (&addr, '\0', sizeof (addr));
234 addr.s6_addr[10] = 0xFF;
235 addr.s6_addr[11] = 0xFF;
236 memcpy (addr.s6_addr + 12, &((struct sockaddr_in *) ai_ptr->ai_addr)->sin_addr, 4);
238 ignore_list_add (&addr);
239 }
240 else
241 {
242 ignore_list_add (&((struct sockaddr_in6 *) ai_ptr->ai_addr)->sin6_addr);
243 }
244 } /* for */
246 freeaddrinfo (ai_list);
247 }
249 #if HAVE_PCAP_H
250 static void in6_addr_from_buffer (struct in6_addr *ia,
251 const void *buf, size_t buf_len,
252 int family)
253 {
254 memset (ia, 0, sizeof (struct in6_addr));
255 if ((AF_INET == family) && (sizeof (uint32_t) == buf_len))
256 {
257 ia->s6_addr[10] = 0xFF;
258 ia->s6_addr[11] = 0xFF;
259 memcpy (ia->s6_addr + 12, buf, buf_len);
260 }
261 else if ((AF_INET6 == family) && (sizeof (struct in6_addr) == buf_len))
262 {
263 memcpy (ia, buf, buf_len);
264 }
265 } /* void in6_addr_from_buffer */
267 void dnstop_set_pcap_obj (pcap_t *po)
268 {
269 pcap_obj = po;
270 }
272 void dnstop_set_callback (void (*cb) (const rfc1035_header_t *))
273 {
274 Callback = cb;
275 }
277 #define RFC1035_MAXLABELSZ 63
278 static int
279 rfc1035NameUnpack(const char *buf, size_t sz, off_t * off, char *name, size_t ns
280 )
281 {
282 off_t no = 0;
283 unsigned char c;
284 size_t len;
285 assert(ns > 0);
286 do {
287 if ((*off) >= sz)
288 break;
289 c = *(buf + (*off));
290 if (c > 191) {
291 /* blasted compression */
292 unsigned short s;
293 off_t ptr;
294 memcpy(&s, buf + (*off), sizeof(s));
295 s = ntohs(s);
296 (*off) += sizeof(s);
297 /* Sanity check */
298 if ((*off) >= sz)
299 return 1;
300 ptr = s & 0x3FFF;
301 /* Make sure the pointer is inside this message */
302 if (ptr >= sz)
303 return 2;
304 return rfc1035NameUnpack(buf, sz, &ptr, name + no, ns - no);
305 } else if (c > RFC1035_MAXLABELSZ) {
306 /*
307 * "(The 10 and 01 combinations are reserved for future use.)"
308 */
309 break;
310 return 3;
311 } else {
312 (*off)++;
313 len = (size_t) c;
314 if (len == 0)
315 break;
316 if (len > (ns - 1))
317 len = ns - 1;
318 if ((*off) + len > sz) /* message is too short */
319 return 4;
320 memcpy(name + no, buf + (*off), len);
321 (*off) += len;
322 no += len;
323 *(name + (no++)) = '.';
324 }
325 } while (c > 0);
326 *(name + no - 1) = '\0';
327 /* make sure we didn't allow someone to overflow the name buffer */
328 assert(no <= ns);
329 return 0;
330 }
332 static int
333 handle_dns(const char *buf, int len,
334 const struct in6_addr *s_addr,
335 const struct in6_addr *d_addr)
336 {
337 rfc1035_header_t qh;
338 uint16_t us;
339 off_t offset;
340 char *t;
341 int x;
343 /* The DNS header is 12 bytes long */
344 if (len < 12)
345 return 0;
347 memcpy(&us, buf + 0, 2);
348 qh.id = ntohs(us);
350 memcpy(&us, buf + 2, 2);
351 us = ntohs(us);
352 fprintf (stderr, "Bytes 0, 1: 0x%04hx\n", us);
353 qh.qr = (us >> 15) & 0x01;
354 qh.opcode = (us >> 11) & 0x0F;
355 qh.aa = (us >> 10) & 0x01;
356 qh.tc = (us >> 9) & 0x01;
357 qh.rd = (us >> 8) & 0x01;
358 qh.ra = (us >> 7) & 0x01;
359 qh.z = (us >> 6) & 0x01;
360 qh.ad = (us >> 5) & 0x01;
361 qh.cd = (us >> 4) & 0x01;
362 qh.rcode = us & 0x0F;
364 memcpy(&us, buf + 4, 2);
365 qh.qdcount = ntohs(us);
367 memcpy(&us, buf + 6, 2);
368 qh.ancount = ntohs(us);
370 memcpy(&us, buf + 8, 2);
371 qh.nscount = ntohs(us);
373 memcpy(&us, buf + 10, 2);
374 qh.arcount = ntohs(us);
376 offset = 12;
377 memset(qh.qname, '\0', MAX_QNAME_SZ);
378 x = rfc1035NameUnpack(buf, len, &offset, qh.qname, MAX_QNAME_SZ);
379 if (0 != x)
380 return 0;
381 if ('\0' == qh.qname[0])
382 strcpy(qh.qname, ".");
383 while ((t = strchr(qh.qname, '\n')))
384 *t = ' ';
385 while ((t = strchr(qh.qname, '\r')))
386 *t = ' ';
387 for (t = qh.qname; *t; t++)
388 *t = tolower(*t);
390 memcpy(&us, buf + offset, 2);
391 qh.qtype = ntohs(us);
392 memcpy(&us, buf + offset + 2, 2);
393 qh.qclass = ntohs(us);
395 qh.length = (uint16_t) len;
397 /* gather stats */
398 qtype_counts[qh.qtype]++;
399 qclass_counts[qh.qclass]++;
400 opcode_counts[qh.opcode]++;
402 if (Callback != NULL)
403 Callback (&qh);
405 return 1;
406 }
408 static int
409 handle_udp(const struct udphdr *udp, int len,
410 const struct in6_addr *s_addr,
411 const struct in6_addr *d_addr)
412 {
413 char buf[PCAP_SNAPLEN];
414 if ((ntohs (udp->UDP_DEST) != 53)
415 && (ntohs (udp->UDP_SRC) != 53))
416 return 0;
417 memcpy(buf, udp + 1, len - sizeof(*udp));
418 if (0 == handle_dns(buf, len - sizeof(*udp), s_addr, d_addr))
419 return 0;
420 return 1;
421 }
423 static int
424 handle_ipv6 (struct ip6_hdr *ipv6, int len)
425 {
426 char buf[PCAP_SNAPLEN];
427 int offset;
428 int nexthdr;
430 struct in6_addr s_addr;
431 struct in6_addr d_addr;
432 uint16_t payload_len;
434 offset = sizeof (struct ip6_hdr);
435 nexthdr = ipv6->ip6_nxt;
436 s_addr = ipv6->ip6_src;
437 d_addr = ipv6->ip6_dst;
438 payload_len = ntohs (ipv6->ip6_plen);
440 if (ignore_list_match (&s_addr))
441 return (0);
443 /* Parse extension headers. This only handles the standard headers, as
444 * defined in RFC 2460, correctly. Fragments are discarded. */
445 while ((IPPROTO_ROUTING == nexthdr) /* routing header */
446 || (IPPROTO_HOPOPTS == nexthdr) /* Hop-by-Hop options. */
447 || (IPPROTO_FRAGMENT == nexthdr) /* fragmentation header. */
448 || (IPPROTO_DSTOPTS == nexthdr) /* destination options. */
449 || (IPPROTO_DSTOPTS == nexthdr) /* destination options. */
450 || (IPPROTO_AH == nexthdr) /* destination options. */
451 || (IPPROTO_ESP == nexthdr)) /* encapsulating security payload. */
452 {
453 struct ip6_ext ext_hdr;
454 uint16_t ext_hdr_len;
456 /* Catch broken packets */
457 if ((offset + sizeof (struct ip6_ext)) > len)
458 return (0);
460 /* Cannot handle fragments. */
461 if (IPPROTO_FRAGMENT == nexthdr)
462 return (0);
464 memcpy (&ext_hdr, (char *) ipv6 + offset, sizeof (struct ip6_ext));
465 nexthdr = ext_hdr.ip6e_nxt;
466 ext_hdr_len = (8 * (ntohs (ext_hdr.ip6e_len) + 1));
468 /* This header is longer than the packets payload.. WTF? */
469 if (ext_hdr_len > payload_len)
470 return (0);
472 offset += ext_hdr_len;
473 payload_len -= ext_hdr_len;
474 } /* while */
476 /* Catch broken and empty packets */
477 if (((offset + payload_len) > len)
478 || (payload_len == 0)
479 || (payload_len > PCAP_SNAPLEN))
480 return (0);
482 if (IPPROTO_UDP != nexthdr)
483 return (0);
485 memcpy (buf, (char *) ipv6 + offset, payload_len);
486 if (handle_udp ((struct udphdr *) buf, payload_len, &s_addr, &d_addr) == 0)
487 return (0);
489 return (1); /* Success */
490 } /* int handle_ipv6 */
492 static int
493 handle_ip(const struct ip *ip, int len)
494 {
495 char buf[PCAP_SNAPLEN];
496 int offset = ip->ip_hl << 2;
497 struct in6_addr s_addr;
498 struct in6_addr d_addr;
500 if (ip->ip_v == 6)
501 return (handle_ipv6 ((struct ip6_hdr *) ip, len));
503 in6_addr_from_buffer (&s_addr, &ip->ip_src.s_addr, sizeof (ip->ip_src.s_addr), AF_INET);
504 in6_addr_from_buffer (&d_addr, &ip->ip_dst.s_addr, sizeof (ip->ip_dst.s_addr), AF_INET);
505 if (ignore_list_match (&s_addr))
506 return (0);
507 if (IPPROTO_UDP != ip->ip_p)
508 return 0;
509 memcpy(buf, (void *) ip + offset, len - offset);
510 if (0 == handle_udp((struct udphdr *) buf, len - offset, &s_addr, &d_addr))
511 return 0;
512 return 1;
513 }
515 #if HAVE_NET_IF_PPP_H
516 static int
517 handle_ppp(const u_char * pkt, int len)
518 {
519 char buf[PCAP_SNAPLEN];
520 unsigned short us;
521 unsigned short proto;
522 if (len < 2)
523 return 0;
524 if (*pkt == PPP_ADDRESS_VAL && *(pkt + 1) == PPP_CONTROL_VAL) {
525 pkt += 2; /* ACFC not used */
526 len -= 2;
527 }
528 if (len < 2)
529 return 0;
530 if (*pkt % 2) {
531 proto = *pkt; /* PFC is used */
532 pkt++;
533 len--;
534 } else {
535 memcpy(&us, pkt, sizeof(us));
536 proto = ntohs(us);
537 pkt += 2;
538 len -= 2;
539 }
540 if (ETHERTYPE_IP != proto && PPP_IP != proto)
541 return 0;
542 memcpy(buf, pkt, len);
543 return handle_ip((struct ip *) buf, len);
544 }
545 #endif /* HAVE_NET_IF_PPP_H */
547 static int
548 handle_null(const u_char * pkt, int len)
549 {
550 unsigned int family;
551 memcpy(&family, pkt, sizeof(family));
552 if (AF_INET != family)
553 return 0;
554 return handle_ip((struct ip *) (pkt + 4), len - 4);
555 }
557 #ifdef DLT_LOOP
558 static int
559 handle_loop(const u_char * pkt, int len)
560 {
561 unsigned int family;
562 memcpy(&family, pkt, sizeof(family));
563 if (AF_INET != ntohl(family))
564 return 0;
565 return handle_ip((struct ip *) (pkt + 4), len - 4);
566 }
568 #endif
570 #ifdef DLT_RAW
571 static int
572 handle_raw(const u_char * pkt, int len)
573 {
574 return handle_ip((struct ip *) pkt, len);
575 }
577 #endif
579 static int
580 handle_ether(const u_char * pkt, int len)
581 {
582 char buf[PCAP_SNAPLEN];
583 struct ether_header *e = (void *) pkt;
584 unsigned short etype = ntohs(e->ether_type);
585 if (len < ETHER_HDR_LEN)
586 return 0;
587 pkt += ETHER_HDR_LEN;
588 len -= ETHER_HDR_LEN;
589 if (ETHERTYPE_8021Q == etype) {
590 etype = ntohs(*(unsigned short *) (pkt + 2));
591 pkt += 4;
592 len -= 4;
593 }
594 if ((ETHERTYPE_IP != etype)
595 && (ETHERTYPE_IPV6 != etype))
596 return 0;
597 memcpy(buf, pkt, len);
598 if (ETHERTYPE_IPV6 == etype)
599 return (handle_ipv6 ((struct ip6_hdr *) buf, len));
600 else
601 return handle_ip((struct ip *) buf, len);
602 }
604 #ifdef DLT_LINUX_SLL
605 static int
606 handle_linux_sll (const u_char *pkt, int len)
607 {
608 struct sll_header
609 {
610 uint16_t pkt_type;
611 uint16_t dev_type;
612 uint16_t addr_len;
613 uint8_t addr[8];
614 uint16_t proto_type;
615 } *hdr;
616 uint16_t etype;
618 if (len < sizeof (struct sll_header))
619 return (0);
621 hdr = (struct sll_header *) pkt;
622 pkt = (u_char *) (hdr + 1);
623 len -= sizeof (struct sll_header);
625 etype = ntohs (hdr->proto_type);
627 if ((ETHERTYPE_IP != etype)
628 && (ETHERTYPE_IPV6 != etype))
629 return 0;
631 if (ETHERTYPE_IPV6 == etype)
632 return (handle_ipv6 ((struct ip6_hdr *) pkt, len));
633 else
634 return handle_ip((struct ip *) pkt, len);
635 }
636 #endif /* DLT_LINUX_SLL */
638 /* public function */
639 void handle_pcap(u_char *udata, const struct pcap_pkthdr *hdr, const u_char *pkt)
640 {
641 int status;
643 fprintf (stderr, "handle_pcap (udata = %p, hdr = %p, pkt = %p): hdr->caplen = %i\n",
644 (void *) udata, (void *) hdr, (void *) pkt,
645 hdr->caplen);
647 if (hdr->caplen < ETHER_HDR_LEN)
648 return;
650 switch (pcap_datalink (pcap_obj))
651 {
652 case DLT_EN10MB:
653 status = handle_ether (pkt, hdr->caplen);
654 break;
655 #if HAVE_NET_IF_PPP_H
656 case DLT_PPP:
657 status = handle_ppp (pkt, hdr->caplen);
658 break;
659 #endif
660 #ifdef DLT_LOOP
661 case DLT_LOOP:
662 status = handle_loop (pkt, hdr->caplen);
663 break;
664 #endif
665 #ifdef DLT_RAW
666 case DLT_RAW:
667 status = handle_raw (pkt, hdr->caplen);
668 break;
669 #endif
670 #ifdef DLT_LINUX_SLL
671 case DLT_LINUX_SLL:
672 status = handle_linux_sll (pkt, hdr->caplen);
673 break;
674 #endif
675 case DLT_NULL:
676 status = handle_null (pkt, hdr->caplen);
677 break;
679 default:
680 fprintf (stderr, "unsupported data link type %d\n",
681 pcap_datalink(pcap_obj));
682 status = 0;
683 break;
684 } /* switch (pcap_datalink(pcap_obj)) */
686 if (0 == status)
687 return;
689 query_count_intvl++;
690 query_count_total++;
691 last_ts = hdr->ts;
692 }
693 #endif /* HAVE_PCAP_H */
695 const char *qtype_str(int t)
696 {
697 static char buf[32];
698 switch (t) {
699 case ns_t_a: return ("A");
700 case ns_t_ns: return ("NS");
701 case ns_t_md: return ("MD");
702 case ns_t_mf: return ("MF");
703 case ns_t_cname: return ("CNAME");
704 case ns_t_soa: return ("SOA");
705 case ns_t_mb: return ("MB");
706 case ns_t_mg: return ("MG");
707 case ns_t_mr: return ("MR");
708 case ns_t_null: return ("NULL");
709 case ns_t_wks: return ("WKS");
710 case ns_t_ptr: return ("PTR");
711 case ns_t_hinfo: return ("HINFO");
712 case ns_t_minfo: return ("MINFO");
713 case ns_t_mx: return ("MX");
714 case ns_t_txt: return ("TXT");
715 case ns_t_rp: return ("RP");
716 case ns_t_afsdb: return ("AFSDB");
717 case ns_t_x25: return ("X25");
718 case ns_t_isdn: return ("ISDN");
719 case ns_t_rt: return ("RT");
720 case ns_t_nsap: return ("NSAP");
721 case ns_t_nsap_ptr: return ("NSAP-PTR");
722 case ns_t_sig: return ("SIG");
723 case ns_t_key: return ("KEY");
724 case ns_t_px: return ("PX");
725 case ns_t_gpos: return ("GPOS");
726 case ns_t_aaaa: return ("AAAA");
727 case ns_t_loc: return ("LOC");
728 case ns_t_nxt: return ("NXT");
729 case ns_t_eid: return ("EID");
730 case ns_t_nimloc: return ("NIMLOC");
731 case ns_t_srv: return ("SRV");
732 case ns_t_atma: return ("ATMA");
733 case ns_t_naptr: return ("NAPTR");
734 case ns_t_kx: return ("KX");
735 case ns_t_cert: return ("CERT");
736 case ns_t_a6: return ("A6");
737 case ns_t_dname: return ("DNAME");
738 case ns_t_sink: return ("SINK");
739 case ns_t_opt: return ("OPT");
740 case ns_t_tsig: return ("TSIG");
741 case ns_t_ixfr: return ("IXFR");
742 case ns_t_axfr: return ("AXFR");
743 case ns_t_mailb: return ("MAILB");
744 case ns_t_maila: return ("MAILA");
745 case ns_t_any: return ("ANY");
746 case ns_t_zxfr: return ("ZXFR");
747 default:
748 snprintf (buf, 32, "#%i", t);
749 buf[31] = '\0';
750 return (buf);
751 }; /* switch (t) */
752 /* NOTREACHED */
753 return (NULL);
754 }
756 const char *opcode_str (int o)
757 {
758 static char buf[30];
759 switch (o) {
760 case 0:
761 return "Query";
762 break;
763 case 1:
764 return "Iquery";
765 break;
766 case 2:
767 return "Status";
768 break;
769 case 4:
770 return "Notify";
771 break;
772 case 5:
773 return "Update";
774 break;
775 default:
776 snprintf(buf, 30, "Opcode%d", o);
777 return buf;
778 }
779 /* NOTREACHED */
780 }
782 const char *rcode_str (int rcode)
783 {
784 static char buf[32];
785 switch (rcode)
786 {
787 case ns_r_noerror: return ("NOERROR");
788 case ns_r_formerr: return ("FORMERR");
789 case ns_r_servfail: return ("SERVFAIL");
790 case ns_r_nxdomain: return ("NXDOMAIN");
791 case ns_r_notimpl: return ("NOTIMPL");
792 case ns_r_refused: return ("REFUSED");
793 case ns_r_yxdomain: return ("YXDOMAIN");
794 case ns_r_yxrrset: return ("YXRRSET");
795 case ns_r_nxrrset: return ("NXRRSET");
796 case ns_r_notauth: return ("NOTAUTH");
797 case ns_r_notzone: return ("NOTZONE");
798 case ns_r_max: return ("MAX");
799 case ns_r_badsig: return ("BADSIG");
800 case ns_r_badkey: return ("BADKEY");
801 case ns_r_badtime: return ("BADTIME");
802 default:
803 snprintf (buf, 32, "RCode%i", rcode);
804 buf[31] = '\0';
805 return (buf);
806 }
807 /* Never reached */
808 return (NULL);
809 } /* const char *rcode_str (int rcode) */
811 #if 0
812 static int
813 main(int argc, char *argv[])
814 {
815 char errbuf[PCAP_ERRBUF_SIZE];
816 int x;
817 struct stat sb;
818 int readfile_state = 0;
819 struct bpf_program fp;
821 port53 = htons(53);
822 SubReport = Sources_report;
823 ignore_addr.s_addr = 0;
824 progname = strdup(strrchr(argv[0], '/') ? strchr(argv[0], '/') + 1 : argv[0]);
825 srandom(time(NULL));
826 ResetCounters();
828 while ((x = getopt(argc, argv, "ab:f:i:pst")) != -1) {
829 switch (x) {
830 case 'a':
831 anon_flag = 1;
832 break;
833 case 's':
834 sld_flag = 1;
835 break;
836 case 't':
837 nld_flag = 1;
838 break;
839 case 'p':
840 promisc_flag = 0;
841 break;
842 case 'b':
843 bpf_program_str = strdup(optarg);
844 break;
845 case 'i':
846 ignore_addr.s_addr = inet_addr(optarg);
847 break;
848 case 'f':
849 set_filter(optarg);
850 break;
851 default:
852 usage();
853 break;
854 }
855 }
856 argc -= optind;
857 argv += optind;
859 if (argc < 1)
860 usage();
861 device = strdup(argv[0]);
863 if (0 == stat(device, &sb))
864 readfile_state = 1;
865 if (readfile_state) {
866 pcap_obj = pcap_open_offline(device, errbuf);
867 } else {
868 pcap_obj = pcap_open_live(device, PCAP_SNAPLEN, promisc_flag, 1000, errbuf);
869 }
870 if (NULL == pcap_obj) {
871 fprintf(stderr, "pcap_open_*: %s\n", errbuf);
872 exit(1);
873 }
875 if (0 == isatty(1)) {
876 if (0 == readfile_state) {
877 fprintf(stderr, "Non-interactive mode requires savefile argument\n");
878 exit(1);
879 }
880 interactive = 0;
881 print_func = printf;
882 }
884 memset(&fp, '\0', sizeof(fp));
885 x = pcap_compile(pcap_obj, &fp, bpf_program_str, 1, 0);
886 if (x < 0) {
887 fprintf(stderr, "pcap_compile failed\n");
888 exit(1);
889 }
890 x = pcap_setfilter(pcap_obj, &fp);
891 if (x < 0) {
892 fprintf(stderr, "pcap_setfilter failed\n");
893 exit(1);
894 }
896 /*
897 * non-blocking call added for Mac OS X bugfix. Sent by Max Horn.
898 * ref http://www.tcpdump.org/lists/workers/2002/09/msg00033.html
899 */
900 x = pcap_setnonblock(pcap_obj, 1, errbuf);
901 if (x < 0) {
902 fprintf(stderr, "pcap_setnonblock failed: %s\n", errbuf);
903 exit(1);
904 }
906 switch (pcap_datalink(pcap_obj)) {
907 case DLT_EN10MB:
908 handle_datalink = handle_ether;
909 break;
910 #if HAVE_NET_IF_PPP_H
911 case DLT_PPP:
912 handle_datalink = handle_ppp;
913 break;
914 #endif
915 #ifdef DLT_LOOP
916 case DLT_LOOP:
917 handle_datalink = handle_loop;
918 break;
919 #endif
920 #ifdef DLT_RAW
921 case DLT_RAW:
922 handle_datalink = handle_raw;
923 break;
924 #endif
925 case DLT_NULL:
926 handle_datalink = handle_null;
927 break;
928 default:
929 fprintf(stderr, "unsupported data link type %d\n",
930 pcap_datalink(pcap_obj));
931 return 1;
932 break;
933 }
934 if (interactive) {
935 init_curses();
936 while (0 == Quit) {
937 if (readfile_state < 2) {
938 /*
939 * On some OSes select() might return 0 even when
940 * there are packets to process. Thus, we always
941 * ignore its return value and just call pcap_dispatch()
942 * anyway.
943 */
944 if (0 == readfile_state) /* interactive */
945 pcap_select(pcap_obj, 1, 0);
946 x = pcap_dispatch(pcap_obj, 50, handle_pcap, NULL);
947 }
948 if (0 == x && 1 == readfile_state) {
949 /* block on keyboard until user quits */
950 readfile_state++;
951 nodelay(w, 0);
952 }
953 keyboard();
954 cron_pre();
955 report();
956 cron_post();
957 }
958 endwin(); /* klin, Thu Nov 28 08:56:51 2002 */
959 } else {
960 while (pcap_dispatch(pcap_obj, 50, handle_pcap, NULL))
961 (void) 0;
962 cron_pre();
963 Sources_report(); print_func("\n");
964 Destinatioreport(); print_func("\n");
965 Qtypes_report(); print_func("\n");
966 Opcodes_report(); print_func("\n");
967 Tld_report(); print_func("\n");
968 Sld_report(); print_func("\n");
969 Nld_report(); print_func("\n");
970 SldBySource_report();
971 }
973 pcap_close(pcap_obj);
974 return 0;
975 } /* static int main(int argc, char *argv[]) */
976 #endif
977 /*
978 * vim:shiftwidth=4:tabstop=8:softtabstop=4
979 */