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