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