]> The Tcpdump Group git mirrors - tcpdump/blob - addrtoname.c
Merge pull request #455 from brooksdavis/gndo-cleanup
[tcpdump] / addrtoname.c
1 /*
2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * Internet, ethernet, port, and protocol string to address
22 * and address to string conversion routines
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <tcpdump-stdinc.h>
30
31 #ifdef USE_ETHER_NTOHOST
32 #ifdef HAVE_NETINET_IF_ETHER_H
33 struct mbuf; /* Squelch compiler warnings on some platforms for */
34 struct rtentry; /* declarations in <net/if.h> */
35 #include <net/if.h> /* for "struct ifnet" in "struct arpcom" on Solaris */
36 #include <netinet/if_ether.h>
37 #endif /* HAVE_NETINET_IF_ETHER_H */
38 #ifdef NETINET_ETHER_H_DECLARES_ETHER_NTOHOST
39 #include <netinet/ether.h>
40 #endif /* NETINET_ETHER_H_DECLARES_ETHER_NTOHOST */
41
42 #if !defined(HAVE_DECL_ETHER_NTOHOST) || !HAVE_DECL_ETHER_NTOHOST
43 #ifndef HAVE_STRUCT_ETHER_ADDR
44 struct ether_addr {
45 unsigned char ether_addr_octet[6];
46 };
47 #endif
48 extern int ether_ntohost(char *, const struct ether_addr *);
49 #endif
50
51 #endif /* USE_ETHER_NTOHOST */
52
53 #include <pcap.h>
54 #include <pcap-namedb.h>
55 #include <signal.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <stdlib.h>
59
60 #include "interface.h"
61 #include "addrtoname.h"
62 #include "llc.h"
63 #include "setsignal.h"
64 #include "extract.h"
65 #include "oui.h"
66
67 #ifndef ETHER_ADDR_LEN
68 #define ETHER_ADDR_LEN 6
69 #endif
70
71 /*
72 * hash tables for whatever-to-name translations
73 *
74 * XXX there has to be error checks against strdup(3) failure
75 */
76
77 #define HASHNAMESIZE 4096
78
79 struct hnamemem {
80 uint32_t addr;
81 const char *name;
82 struct hnamemem *nxt;
83 };
84
85 static struct hnamemem hnametable[HASHNAMESIZE];
86 static struct hnamemem tporttable[HASHNAMESIZE];
87 static struct hnamemem uporttable[HASHNAMESIZE];
88 static struct hnamemem eprototable[HASHNAMESIZE];
89 static struct hnamemem dnaddrtable[HASHNAMESIZE];
90 static struct hnamemem ipxsaptable[HASHNAMESIZE];
91
92 #if defined(INET6) && defined(WIN32)
93 /*
94 * fake gethostbyaddr for Win2k/XP
95 * gethostbyaddr() returns incorrect value when AF_INET6 is passed
96 * to 3rd argument.
97 *
98 * h_name in struct hostent is only valid.
99 */
100 static struct hostent *
101 win32_gethostbyaddr(const char *addr, int len, int type)
102 {
103 static struct hostent host;
104 static char hostbuf[NI_MAXHOST];
105 char hname[NI_MAXHOST];
106 struct sockaddr_in6 addr6;
107
108 host.h_name = hostbuf;
109 switch (type) {
110 case AF_INET:
111 return gethostbyaddr(addr, len, type);
112 break;
113 case AF_INET6:
114 memset(&addr6, 0, sizeof(addr6));
115 addr6.sin6_family = AF_INET6;
116 memcpy(&addr6.sin6_addr, addr, len);
117 if (getnameinfo((struct sockaddr *)&addr6, sizeof(addr6),
118 hname, sizeof(hname), NULL, 0, 0)) {
119 return NULL;
120 } else {
121 strcpy(host.h_name, hname);
122 return &host;
123 }
124 break;
125 default:
126 return NULL;
127 }
128 }
129 #define gethostbyaddr win32_gethostbyaddr
130 #endif /* INET6 & WIN32 */
131
132 #ifdef INET6
133 struct h6namemem {
134 struct in6_addr addr;
135 char *name;
136 struct h6namemem *nxt;
137 };
138
139 static struct h6namemem h6nametable[HASHNAMESIZE];
140 #endif /* INET6 */
141
142 struct enamemem {
143 u_short e_addr0;
144 u_short e_addr1;
145 u_short e_addr2;
146 const char *e_name;
147 u_char *e_nsap; /* used only for nsaptable[] */
148 #define e_bs e_nsap /* for bytestringtable */
149 struct enamemem *e_nxt;
150 };
151
152 static struct enamemem enametable[HASHNAMESIZE];
153 static struct enamemem nsaptable[HASHNAMESIZE];
154 static struct enamemem bytestringtable[HASHNAMESIZE];
155
156 struct protoidmem {
157 uint32_t p_oui;
158 u_short p_proto;
159 const char *p_name;
160 struct protoidmem *p_nxt;
161 };
162
163 static struct protoidmem protoidtable[HASHNAMESIZE];
164
165 /*
166 * A faster replacement for inet_ntoa().
167 */
168 const char *
169 intoa(uint32_t addr)
170 {
171 register char *cp;
172 register u_int byte;
173 register int n;
174 static char buf[sizeof(".xxx.xxx.xxx.xxx")];
175
176 NTOHL(addr);
177 cp = buf + sizeof(buf);
178 *--cp = '\0';
179
180 n = 4;
181 do {
182 byte = addr & 0xff;
183 *--cp = byte % 10 + '0';
184 byte /= 10;
185 if (byte > 0) {
186 *--cp = byte % 10 + '0';
187 byte /= 10;
188 if (byte > 0)
189 *--cp = byte + '0';
190 }
191 *--cp = '.';
192 addr >>= 8;
193 } while (--n > 0);
194
195 return cp + 1;
196 }
197
198 static uint32_t f_netmask;
199 static uint32_t f_localnet;
200
201 /*
202 * Return a name for the IP address pointed to by ap. This address
203 * is assumed to be in network byte order.
204 *
205 * NOTE: ap is *NOT* necessarily part of the packet data (not even if
206 * this is being called with the "ipaddr_string()" macro), so you
207 * *CANNOT* use the ND_TCHECK{2}/ND_TTEST{2} macros on it. Furthermore,
208 * even in cases where it *is* part of the packet data, the caller
209 * would still have to check for a null return value, even if it's
210 * just printing the return value with "%s" - not all versions of
211 * printf print "(null)" with "%s" and a null pointer, some of them
212 * don't check for a null pointer and crash in that case.
213 *
214 * The callers of this routine should, before handing this routine
215 * a pointer to packet data, be sure that the data is present in
216 * the packet buffer. They should probably do those checks anyway,
217 * as other data at that layer might not be IP addresses, and it
218 * also needs to check whether they're present in the packet buffer.
219 */
220 const char *
221 getname(netdissect_options *ndo, const u_char *ap)
222 {
223 register struct hostent *hp;
224 uint32_t addr;
225 static struct hnamemem *p; /* static for longjmp() */
226
227 memcpy(&addr, ap, sizeof(addr));
228 p = &hnametable[addr & (HASHNAMESIZE-1)];
229 for (; p->nxt; p = p->nxt) {
230 if (p->addr == addr)
231 return (p->name);
232 }
233 p->addr = addr;
234 p->nxt = newhnamemem();
235
236 /*
237 * Print names unless:
238 * (1) -n was given.
239 * (2) Address is foreign and -f was given. (If -f was not
240 * given, f_netmask and f_localnet are 0 and the test
241 * evaluates to true)
242 */
243 if (!ndo->ndo_nflag &&
244 (addr & f_netmask) == f_localnet) {
245 hp = gethostbyaddr((char *)&addr, 4, AF_INET);
246 if (hp) {
247 char *dotp;
248
249 p->name = strdup(hp->h_name);
250 if (ndo->ndo_Nflag) {
251 /* Remove domain qualifications */
252 dotp = strchr(p->name, '.');
253 if (dotp)
254 *dotp = '\0';
255 }
256 return (p->name);
257 }
258 }
259 p->name = strdup(intoa(addr));
260 return (p->name);
261 }
262
263 #ifdef INET6
264 /*
265 * Return a name for the IP6 address pointed to by ap. This address
266 * is assumed to be in network byte order.
267 */
268 const char *
269 getname6(netdissect_options *ndo, const u_char *ap)
270 {
271 register struct hostent *hp;
272 union {
273 struct in6_addr addr;
274 struct for_hash_addr {
275 char fill[14];
276 uint16_t d;
277 } addra;
278 } addr;
279 static struct h6namemem *p; /* static for longjmp() */
280 register const char *cp;
281 char ntop_buf[INET6_ADDRSTRLEN];
282
283 memcpy(&addr, ap, sizeof(addr));
284 p = &h6nametable[addr.addra.d & (HASHNAMESIZE-1)];
285 for (; p->nxt; p = p->nxt) {
286 if (memcmp(&p->addr, &addr, sizeof(addr)) == 0)
287 return (p->name);
288 }
289 p->addr = addr.addr;
290 p->nxt = newh6namemem();
291
292 /*
293 * Do not print names if -n was given.
294 */
295 if (!ndo->ndo_nflag) {
296 hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET6);
297 if (hp) {
298 char *dotp;
299
300 p->name = strdup(hp->h_name);
301 if (ndo->ndo_Nflag) {
302 /* Remove domain qualifications */
303 dotp = strchr(p->name, '.');
304 if (dotp)
305 *dotp = '\0';
306 }
307 return (p->name);
308 }
309 }
310 cp = inet_ntop(AF_INET6, &addr, ntop_buf, sizeof(ntop_buf));
311 p->name = strdup(cp);
312 return (p->name);
313 }
314 #endif /* INET6 */
315
316 static const char hex[] = "0123456789abcdef";
317
318
319 /* Find the hash node that corresponds the ether address 'ep' */
320
321 static inline struct enamemem *
322 lookup_emem(const u_char *ep)
323 {
324 register u_int i, j, k;
325 struct enamemem *tp;
326
327 k = (ep[0] << 8) | ep[1];
328 j = (ep[2] << 8) | ep[3];
329 i = (ep[4] << 8) | ep[5];
330
331 tp = &enametable[(i ^ j) & (HASHNAMESIZE-1)];
332 while (tp->e_nxt)
333 if (tp->e_addr0 == i &&
334 tp->e_addr1 == j &&
335 tp->e_addr2 == k)
336 return tp;
337 else
338 tp = tp->e_nxt;
339 tp->e_addr0 = i;
340 tp->e_addr1 = j;
341 tp->e_addr2 = k;
342 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
343 if (tp->e_nxt == NULL)
344 error("lookup_emem: calloc");
345
346 return tp;
347 }
348
349 /*
350 * Find the hash node that corresponds to the bytestring 'bs'
351 * with length 'nlen'
352 */
353
354 static inline struct enamemem *
355 lookup_bytestring(register const u_char *bs, const unsigned int nlen)
356 {
357 struct enamemem *tp;
358 register u_int i, j, k;
359
360 if (nlen >= 6) {
361 k = (bs[0] << 8) | bs[1];
362 j = (bs[2] << 8) | bs[3];
363 i = (bs[4] << 8) | bs[5];
364 } else if (nlen >= 4) {
365 k = (bs[0] << 8) | bs[1];
366 j = (bs[2] << 8) | bs[3];
367 i = 0;
368 } else
369 i = j = k = 0;
370
371 tp = &bytestringtable[(i ^ j) & (HASHNAMESIZE-1)];
372 while (tp->e_nxt)
373 if (tp->e_addr0 == i &&
374 tp->e_addr1 == j &&
375 tp->e_addr2 == k &&
376 memcmp((const char *)bs, (const char *)(tp->e_bs), nlen) == 0)
377 return tp;
378 else
379 tp = tp->e_nxt;
380
381 tp->e_addr0 = i;
382 tp->e_addr1 = j;
383 tp->e_addr2 = k;
384
385 tp->e_bs = (u_char *) calloc(1, nlen + 1);
386 if (tp->e_bs == NULL)
387 error("lookup_bytestring: calloc");
388
389 memcpy(tp->e_bs, bs, nlen);
390 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
391 if (tp->e_nxt == NULL)
392 error("lookup_bytestring: calloc");
393
394 return tp;
395 }
396
397 /* Find the hash node that corresponds the NSAP 'nsap' */
398
399 static inline struct enamemem *
400 lookup_nsap(register const u_char *nsap)
401 {
402 register u_int i, j, k;
403 unsigned int nlen = *nsap;
404 struct enamemem *tp;
405 const u_char *ensap = nsap + nlen - 6;
406
407 if (nlen > 6) {
408 k = (ensap[0] << 8) | ensap[1];
409 j = (ensap[2] << 8) | ensap[3];
410 i = (ensap[4] << 8) | ensap[5];
411 }
412 else
413 i = j = k = 0;
414
415 tp = &nsaptable[(i ^ j) & (HASHNAMESIZE-1)];
416 while (tp->e_nxt)
417 if (tp->e_addr0 == i &&
418 tp->e_addr1 == j &&
419 tp->e_addr2 == k &&
420 tp->e_nsap[0] == nlen &&
421 memcmp((const char *)&(nsap[1]),
422 (char *)&(tp->e_nsap[1]), nlen) == 0)
423 return tp;
424 else
425 tp = tp->e_nxt;
426 tp->e_addr0 = i;
427 tp->e_addr1 = j;
428 tp->e_addr2 = k;
429 tp->e_nsap = (u_char *)malloc(nlen + 1);
430 if (tp->e_nsap == NULL)
431 error("lookup_nsap: malloc");
432 memcpy((char *)tp->e_nsap, (const char *)nsap, nlen + 1);
433 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
434 if (tp->e_nxt == NULL)
435 error("lookup_nsap: calloc");
436
437 return tp;
438 }
439
440 /* Find the hash node that corresponds the protoid 'pi'. */
441
442 static inline struct protoidmem *
443 lookup_protoid(const u_char *pi)
444 {
445 register u_int i, j;
446 struct protoidmem *tp;
447
448 /* 5 octets won't be aligned */
449 i = (((pi[0] << 8) + pi[1]) << 8) + pi[2];
450 j = (pi[3] << 8) + pi[4];
451 /* XXX should be endian-insensitive, but do big-endian testing XXX */
452
453 tp = &protoidtable[(i ^ j) & (HASHNAMESIZE-1)];
454 while (tp->p_nxt)
455 if (tp->p_oui == i && tp->p_proto == j)
456 return tp;
457 else
458 tp = tp->p_nxt;
459 tp->p_oui = i;
460 tp->p_proto = j;
461 tp->p_nxt = (struct protoidmem *)calloc(1, sizeof(*tp));
462 if (tp->p_nxt == NULL)
463 error("lookup_protoid: calloc");
464
465 return tp;
466 }
467
468 const char *
469 etheraddr_string(netdissect_options *ndo, register const u_char *ep)
470 {
471 register int i;
472 register char *cp;
473 register struct enamemem *tp;
474 int oui;
475 char buf[BUFSIZE];
476
477 tp = lookup_emem(ep);
478 if (tp->e_name)
479 return (tp->e_name);
480 #ifdef USE_ETHER_NTOHOST
481 if (!ndo->ndo_nflag) {
482 char buf2[BUFSIZE];
483
484 if (ether_ntohost(buf2, (const struct ether_addr *)ep) == 0) {
485 tp->e_name = strdup(buf2);
486 return (tp->e_name);
487 }
488 }
489 #endif
490 cp = buf;
491 oui = EXTRACT_24BITS(ep);
492 *cp++ = hex[*ep >> 4 ];
493 *cp++ = hex[*ep++ & 0xf];
494 for (i = 5; --i >= 0;) {
495 *cp++ = ':';
496 *cp++ = hex[*ep >> 4 ];
497 *cp++ = hex[*ep++ & 0xf];
498 }
499
500 if (!ndo->ndo_nflag) {
501 snprintf(cp, BUFSIZE - (2 + 5*3), " (oui %s)",
502 tok2str(oui_values, "Unknown", oui));
503 } else
504 *cp = '\0';
505 tp->e_name = strdup(buf);
506 return (tp->e_name);
507 }
508
509 const char *
510 le64addr_string(const u_char *ep)
511 {
512 const unsigned int len = 8;
513 register u_int i;
514 register char *cp;
515 register struct enamemem *tp;
516 char buf[BUFSIZE];
517
518 tp = lookup_bytestring(ep, len);
519 if (tp->e_name)
520 return (tp->e_name);
521
522 cp = buf;
523 for (i = len; i > 0 ; --i) {
524 *cp++ = hex[*(ep + i - 1) >> 4];
525 *cp++ = hex[*(ep + i - 1) & 0xf];
526 *cp++ = ':';
527 }
528 cp --;
529
530 *cp = '\0';
531
532 tp->e_name = strdup(buf);
533
534 return (tp->e_name);
535 }
536
537 const char *
538 linkaddr_string(netdissect_options *ndo, const u_char *ep, const unsigned int type, const unsigned int len)
539 {
540 register u_int i;
541 register char *cp;
542 register struct enamemem *tp;
543
544 if (len == 0)
545 return ("<empty>");
546
547 if (type == LINKADDR_ETHER && len == ETHER_ADDR_LEN)
548 return (etheraddr_string(ndo, ep));
549
550 if (type == LINKADDR_FRELAY)
551 return (q922_string(ndo, ep, len));
552
553 tp = lookup_bytestring(ep, len);
554 if (tp->e_name)
555 return (tp->e_name);
556
557 tp->e_name = cp = (char *)malloc(len*3);
558 if (tp->e_name == NULL)
559 error("linkaddr_string: malloc");
560 *cp++ = hex[*ep >> 4];
561 *cp++ = hex[*ep++ & 0xf];
562 for (i = len-1; i > 0 ; --i) {
563 *cp++ = ':';
564 *cp++ = hex[*ep >> 4];
565 *cp++ = hex[*ep++ & 0xf];
566 }
567 *cp = '\0';
568 return (tp->e_name);
569 }
570
571 const char *
572 etherproto_string(u_short port)
573 {
574 register char *cp;
575 register struct hnamemem *tp;
576 register uint32_t i = port;
577 char buf[sizeof("0000")];
578
579 for (tp = &eprototable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
580 if (tp->addr == i)
581 return (tp->name);
582
583 tp->addr = i;
584 tp->nxt = newhnamemem();
585
586 cp = buf;
587 NTOHS(port);
588 *cp++ = hex[port >> 12 & 0xf];
589 *cp++ = hex[port >> 8 & 0xf];
590 *cp++ = hex[port >> 4 & 0xf];
591 *cp++ = hex[port & 0xf];
592 *cp++ = '\0';
593 tp->name = strdup(buf);
594 return (tp->name);
595 }
596
597 const char *
598 protoid_string(register const u_char *pi)
599 {
600 register u_int i, j;
601 register char *cp;
602 register struct protoidmem *tp;
603 char buf[sizeof("00:00:00:00:00")];
604
605 tp = lookup_protoid(pi);
606 if (tp->p_name)
607 return tp->p_name;
608
609 cp = buf;
610 if ((j = *pi >> 4) != 0)
611 *cp++ = hex[j];
612 *cp++ = hex[*pi++ & 0xf];
613 for (i = 4; (int)--i >= 0;) {
614 *cp++ = ':';
615 if ((j = *pi >> 4) != 0)
616 *cp++ = hex[j];
617 *cp++ = hex[*pi++ & 0xf];
618 }
619 *cp = '\0';
620 tp->p_name = strdup(buf);
621 return (tp->p_name);
622 }
623
624 #define ISONSAP_MAX_LENGTH 20
625 const char *
626 isonsap_string(const u_char *nsap, register u_int nsap_length)
627 {
628 register u_int nsap_idx;
629 register char *cp;
630 register struct enamemem *tp;
631
632 if (nsap_length < 1 || nsap_length > ISONSAP_MAX_LENGTH)
633 return ("isonsap_string: illegal length");
634
635 tp = lookup_nsap(nsap);
636 if (tp->e_name)
637 return tp->e_name;
638
639 tp->e_name = cp = (char *)malloc(sizeof("xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx"));
640 if (cp == NULL)
641 error("isonsap_string: malloc");
642
643 for (nsap_idx = 0; nsap_idx < nsap_length; nsap_idx++) {
644 *cp++ = hex[*nsap >> 4];
645 *cp++ = hex[*nsap++ & 0xf];
646 if (((nsap_idx & 1) == 0) &&
647 (nsap_idx + 1 < nsap_length)) {
648 *cp++ = '.';
649 }
650 }
651 *cp = '\0';
652 return (tp->e_name);
653 }
654
655 const char *
656 tcpport_string(u_short port)
657 {
658 register struct hnamemem *tp;
659 register uint32_t i = port;
660 char buf[sizeof("00000")];
661
662 for (tp = &tporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
663 if (tp->addr == i)
664 return (tp->name);
665
666 tp->addr = i;
667 tp->nxt = newhnamemem();
668
669 (void)snprintf(buf, sizeof(buf), "%u", i);
670 tp->name = strdup(buf);
671 return (tp->name);
672 }
673
674 const char *
675 udpport_string(register u_short port)
676 {
677 register struct hnamemem *tp;
678 register uint32_t i = port;
679 char buf[sizeof("00000")];
680
681 for (tp = &uporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
682 if (tp->addr == i)
683 return (tp->name);
684
685 tp->addr = i;
686 tp->nxt = newhnamemem();
687
688 (void)snprintf(buf, sizeof(buf), "%u", i);
689 tp->name = strdup(buf);
690 return (tp->name);
691 }
692
693 const char *
694 ipxsap_string(u_short port)
695 {
696 register char *cp;
697 register struct hnamemem *tp;
698 register uint32_t i = port;
699 char buf[sizeof("0000")];
700
701 for (tp = &ipxsaptable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
702 if (tp->addr == i)
703 return (tp->name);
704
705 tp->addr = i;
706 tp->nxt = newhnamemem();
707
708 cp = buf;
709 NTOHS(port);
710 *cp++ = hex[port >> 12 & 0xf];
711 *cp++ = hex[port >> 8 & 0xf];
712 *cp++ = hex[port >> 4 & 0xf];
713 *cp++ = hex[port & 0xf];
714 *cp++ = '\0';
715 tp->name = strdup(buf);
716 return (tp->name);
717 }
718
719 static void
720 init_servarray(netdissect_options *ndo)
721 {
722 struct servent *sv;
723 register struct hnamemem *table;
724 register int i;
725 char buf[sizeof("0000000000")];
726
727 while ((sv = getservent()) != NULL) {
728 int port = ntohs(sv->s_port);
729 i = port & (HASHNAMESIZE-1);
730 if (strcmp(sv->s_proto, "tcp") == 0)
731 table = &tporttable[i];
732 else if (strcmp(sv->s_proto, "udp") == 0)
733 table = &uporttable[i];
734 else
735 continue;
736
737 while (table->name)
738 table = table->nxt;
739 if (ndo->ndo_nflag) {
740 (void)snprintf(buf, sizeof(buf), "%d", port);
741 table->name = strdup(buf);
742 } else
743 table->name = strdup(sv->s_name);
744 table->addr = port;
745 table->nxt = newhnamemem();
746 }
747 endservent();
748 }
749
750 /* in libpcap.a (nametoaddr.c) */
751 #if defined(WIN32) && !defined(USE_STATIC_LIBPCAP)
752 extern __declspec(dllimport)
753 #else
754 extern
755 #endif
756 const struct eproto {
757 const char *s;
758 u_short p;
759 } eproto_db[];
760
761 static void
762 init_eprotoarray(void)
763 {
764 register int i;
765 register struct hnamemem *table;
766
767 for (i = 0; eproto_db[i].s; i++) {
768 int j = htons(eproto_db[i].p) & (HASHNAMESIZE-1);
769 table = &eprototable[j];
770 while (table->name)
771 table = table->nxt;
772 table->name = eproto_db[i].s;
773 table->addr = htons(eproto_db[i].p);
774 table->nxt = newhnamemem();
775 }
776 }
777
778 static const struct protoidlist {
779 const u_char protoid[5];
780 const char *name;
781 } protoidlist[] = {
782 {{ 0x00, 0x00, 0x0c, 0x01, 0x07 }, "CiscoMLS" },
783 {{ 0x00, 0x00, 0x0c, 0x20, 0x00 }, "CiscoCDP" },
784 {{ 0x00, 0x00, 0x0c, 0x20, 0x01 }, "CiscoCGMP" },
785 {{ 0x00, 0x00, 0x0c, 0x20, 0x03 }, "CiscoVTP" },
786 {{ 0x00, 0xe0, 0x2b, 0x00, 0xbb }, "ExtremeEDP" },
787 {{ 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
788 };
789
790 /*
791 * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
792 * types.
793 */
794 static void
795 init_protoidarray(void)
796 {
797 register int i;
798 register struct protoidmem *tp;
799 const struct protoidlist *pl;
800 u_char protoid[5];
801
802 protoid[0] = 0;
803 protoid[1] = 0;
804 protoid[2] = 0;
805 for (i = 0; eproto_db[i].s; i++) {
806 u_short etype = htons(eproto_db[i].p);
807
808 memcpy((char *)&protoid[3], (char *)&etype, 2);
809 tp = lookup_protoid(protoid);
810 tp->p_name = strdup(eproto_db[i].s);
811 }
812 /* Hardwire some SNAP proto ID names */
813 for (pl = protoidlist; pl->name != NULL; ++pl) {
814 tp = lookup_protoid(pl->protoid);
815 /* Don't override existing name */
816 if (tp->p_name != NULL)
817 continue;
818
819 tp->p_name = pl->name;
820 }
821 }
822
823 static const struct etherlist {
824 const u_char addr[6];
825 const char *name;
826 } etherlist[] = {
827 {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
828 {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
829 };
830
831 /*
832 * Initialize the ethers hash table. We take two different approaches
833 * depending on whether or not the system provides the ethers name
834 * service. If it does, we just wire in a few names at startup,
835 * and etheraddr_string() fills in the table on demand. If it doesn't,
836 * then we suck in the entire /etc/ethers file at startup. The idea
837 * is that parsing the local file will be fast, but spinning through
838 * all the ethers entries via NIS & next_etherent might be very slow.
839 *
840 * XXX pcap_next_etherent doesn't belong in the pcap interface, but
841 * since the pcap module already does name-to-address translation,
842 * it's already does most of the work for the ethernet address-to-name
843 * translation, so we just pcap_next_etherent as a convenience.
844 */
845 static void
846 init_etherarray(void)
847 {
848 register const struct etherlist *el;
849 register struct enamemem *tp;
850 #ifdef USE_ETHER_NTOHOST
851 char name[256];
852 #else
853 register struct pcap_etherent *ep;
854 register FILE *fp;
855
856 /* Suck in entire ethers file */
857 fp = fopen(PCAP_ETHERS_FILE, "r");
858 if (fp != NULL) {
859 while ((ep = pcap_next_etherent(fp)) != NULL) {
860 tp = lookup_emem(ep->addr);
861 tp->e_name = strdup(ep->name);
862 }
863 (void)fclose(fp);
864 }
865 #endif
866
867 /* Hardwire some ethernet names */
868 for (el = etherlist; el->name != NULL; ++el) {
869 tp = lookup_emem(el->addr);
870 /* Don't override existing name */
871 if (tp->e_name != NULL)
872 continue;
873
874 #ifdef USE_ETHER_NTOHOST
875 /*
876 * Use YP/NIS version of name if available.
877 */
878 if (ether_ntohost(name, (const struct ether_addr *)el->addr) == 0) {
879 tp->e_name = strdup(name);
880 continue;
881 }
882 #endif
883 tp->e_name = el->name;
884 }
885 }
886
887 static const struct tok ipxsap_db[] = {
888 { 0x0000, "Unknown" },
889 { 0x0001, "User" },
890 { 0x0002, "User Group" },
891 { 0x0003, "PrintQueue" },
892 { 0x0004, "FileServer" },
893 { 0x0005, "JobServer" },
894 { 0x0006, "Gateway" },
895 { 0x0007, "PrintServer" },
896 { 0x0008, "ArchiveQueue" },
897 { 0x0009, "ArchiveServer" },
898 { 0x000a, "JobQueue" },
899 { 0x000b, "Administration" },
900 { 0x000F, "Novell TI-RPC" },
901 { 0x0017, "Diagnostics" },
902 { 0x0020, "NetBIOS" },
903 { 0x0021, "NAS SNA Gateway" },
904 { 0x0023, "NACS AsyncGateway" },
905 { 0x0024, "RemoteBridge/RoutingService" },
906 { 0x0026, "BridgeServer" },
907 { 0x0027, "TCP/IP Gateway" },
908 { 0x0028, "Point-to-point X.25 BridgeServer" },
909 { 0x0029, "3270 Gateway" },
910 { 0x002a, "CHI Corp" },
911 { 0x002c, "PC Chalkboard" },
912 { 0x002d, "TimeSynchServer" },
913 { 0x002e, "ARCserve5.0/PalindromeBackup" },
914 { 0x0045, "DI3270 Gateway" },
915 { 0x0047, "AdvertisingPrintServer" },
916 { 0x004a, "NetBlazerModems" },
917 { 0x004b, "BtrieveVAP" },
918 { 0x004c, "NetwareSQL" },
919 { 0x004d, "XtreeNetwork" },
920 { 0x0050, "BtrieveVAP4.11" },
921 { 0x0052, "QuickLink" },
922 { 0x0053, "PrintQueueUser" },
923 { 0x0058, "Multipoint X.25 Router" },
924 { 0x0060, "STLB/NLM" },
925 { 0x0064, "ARCserve" },
926 { 0x0066, "ARCserve3.0" },
927 { 0x0072, "WAN CopyUtility" },
928 { 0x007a, "TES-NetwareVMS" },
929 { 0x0092, "WATCOM Debugger/EmeraldTapeBackupServer" },
930 { 0x0095, "DDA OBGYN" },
931 { 0x0098, "NetwareAccessServer" },
932 { 0x009a, "Netware for VMS II/NamedPipeServer" },
933 { 0x009b, "NetwareAccessServer" },
934 { 0x009e, "PortableNetwareServer/SunLinkNVT" },
935 { 0x00a1, "PowerchuteAPC UPS" },
936 { 0x00aa, "LAWserve" },
937 { 0x00ac, "CompaqIDA StatusMonitor" },
938 { 0x0100, "PIPE STAIL" },
939 { 0x0102, "LAN ProtectBindery" },
940 { 0x0103, "OracleDataBaseServer" },
941 { 0x0107, "Netware386/RSPX RemoteConsole" },
942 { 0x010f, "NovellSNA Gateway" },
943 { 0x0111, "TestServer" },
944 { 0x0112, "HP PrintServer" },
945 { 0x0114, "CSA MUX" },
946 { 0x0115, "CSA LCA" },
947 { 0x0116, "CSA CM" },
948 { 0x0117, "CSA SMA" },
949 { 0x0118, "CSA DBA" },
950 { 0x0119, "CSA NMA" },
951 { 0x011a, "CSA SSA" },
952 { 0x011b, "CSA STATUS" },
953 { 0x011e, "CSA APPC" },
954 { 0x0126, "SNA TEST SSA Profile" },
955 { 0x012a, "CSA TRACE" },
956 { 0x012b, "NetwareSAA" },
957 { 0x012e, "IKARUS VirusScan" },
958 { 0x0130, "CommunicationsExecutive" },
959 { 0x0133, "NNS DomainServer/NetwareNamingServicesDomain" },
960 { 0x0135, "NetwareNamingServicesProfile" },
961 { 0x0137, "Netware386 PrintQueue/NNS PrintQueue" },
962 { 0x0141, "LAN SpoolServer" },
963 { 0x0152, "IRMALAN Gateway" },
964 { 0x0154, "NamedPipeServer" },
965 { 0x0166, "NetWareManagement" },
966 { 0x0168, "Intel PICKIT CommServer/Intel CAS TalkServer" },
967 { 0x0173, "Compaq" },
968 { 0x0174, "Compaq SNMP Agent" },
969 { 0x0175, "Compaq" },
970 { 0x0180, "XTreeServer/XTreeTools" },
971 { 0x018A, "NASI ServicesBroadcastServer" },
972 { 0x01b0, "GARP Gateway" },
973 { 0x01b1, "Binfview" },
974 { 0x01bf, "IntelLanDeskManager" },
975 { 0x01ca, "AXTEC" },
976 { 0x01cb, "ShivaNetModem/E" },
977 { 0x01cc, "ShivaLanRover/E" },
978 { 0x01cd, "ShivaLanRover/T" },
979 { 0x01ce, "ShivaUniversal" },
980 { 0x01d8, "CastelleFAXPressServer" },
981 { 0x01da, "CastelleLANPressPrintServer" },
982 { 0x01dc, "CastelleFAX/Xerox7033 FaxServer/ExcelLanFax" },
983 { 0x01f0, "LEGATO" },
984 { 0x01f5, "LEGATO" },
985 { 0x0233, "NMS Agent/NetwareManagementAgent" },
986 { 0x0237, "NMS IPX Discovery/LANternReadWriteChannel" },
987 { 0x0238, "NMS IP Discovery/LANternTrapAlarmChannel" },
988 { 0x023a, "LANtern" },
989 { 0x023c, "MAVERICK" },
990 { 0x023f, "NovellSMDR" },
991 { 0x024e, "NetwareConnect" },
992 { 0x024f, "NASI ServerBroadcast Cisco" },
993 { 0x026a, "NMS ServiceConsole" },
994 { 0x026b, "TimeSynchronizationServer Netware 4.x" },
995 { 0x0278, "DirectoryServer Netware 4.x" },
996 { 0x027b, "NetwareManagementAgent" },
997 { 0x0280, "Novell File and Printer Sharing Service for PC" },
998 { 0x0304, "NovellSAA Gateway" },
999 { 0x0308, "COM/VERMED" },
1000 { 0x030a, "GalacticommWorldgroupServer" },
1001 { 0x030c, "IntelNetport2/HP JetDirect/HP Quicksilver" },
1002 { 0x0320, "AttachmateGateway" },
1003 { 0x0327, "MicrosoftDiagnostiocs" },
1004 { 0x0328, "WATCOM SQL Server" },
1005 { 0x0335, "MultiTechSystems MultisynchCommServer" },
1006 { 0x0343, "Xylogics RemoteAccessServer/LANModem" },
1007 { 0x0355, "ArcadaBackupExec" },
1008 { 0x0358, "MSLCD1" },
1009 { 0x0361, "NETINELO" },
1010 { 0x037e, "Powerchute UPS Monitoring" },
1011 { 0x037f, "ViruSafeNotify" },
1012 { 0x0386, "HP Bridge" },
1013 { 0x0387, "HP Hub" },
1014 { 0x0394, "NetWare SAA Gateway" },
1015 { 0x039b, "LotusNotes" },
1016 { 0x03b7, "CertusAntiVirus" },
1017 { 0x03c4, "ARCserve4.0" },
1018 { 0x03c7, "LANspool3.5" },
1019 { 0x03d7, "LexmarkPrinterServer" },
1020 { 0x03d8, "LexmarkXLE PrinterServer" },
1021 { 0x03dd, "BanyanENS NetwareClient" },
1022 { 0x03de, "GuptaSequelBaseServer/NetWareSQL" },
1023 { 0x03e1, "UnivelUnixware" },
1024 { 0x03e4, "UnivelUnixware" },
1025 { 0x03fc, "IntelNetport" },
1026 { 0x03fd, "PrintServerQueue" },
1027 { 0x040A, "ipnServer" },
1028 { 0x040D, "LVERRMAN" },
1029 { 0x040E, "LVLIC" },
1030 { 0x0414, "NET Silicon (DPI)/Kyocera" },
1031 { 0x0429, "SiteLockVirus" },
1032 { 0x0432, "UFHELPR???" },
1033 { 0x0433, "Synoptics281xAdvancedSNMPAgent" },
1034 { 0x0444, "MicrosoftNT SNA Server" },
1035 { 0x0448, "Oracle" },
1036 { 0x044c, "ARCserve5.01" },
1037 { 0x0457, "CanonGP55" },
1038 { 0x045a, "QMS Printers" },
1039 { 0x045b, "DellSCSI Array" },
1040 { 0x0491, "NetBlazerModems" },
1041 { 0x04ac, "OnTimeScheduler" },
1042 { 0x04b0, "CD-Net" },
1043 { 0x0513, "EmulexNQA" },
1044 { 0x0520, "SiteLockChecks" },
1045 { 0x0529, "SiteLockChecks" },
1046 { 0x052d, "CitrixOS2 AppServer" },
1047 { 0x0535, "Tektronix" },
1048 { 0x0536, "Milan" },
1049 { 0x055d, "Attachmate SNA gateway" },
1050 { 0x056b, "IBM8235 ModemServer" },
1051 { 0x056c, "ShivaLanRover/E PLUS" },
1052 { 0x056d, "ShivaLanRover/T PLUS" },
1053 { 0x0580, "McAfeeNetShield" },
1054 { 0x05B8, "NLM to workstation communication (Revelation Software)" },
1055 { 0x05BA, "CompatibleSystemsRouters" },
1056 { 0x05BE, "CheyenneHierarchicalStorageManager" },
1057 { 0x0606, "JCWatermarkImaging" },
1058 { 0x060c, "AXISNetworkPrinter" },
1059 { 0x0610, "AdaptecSCSIManagement" },
1060 { 0x0621, "IBM AntiVirus" },
1061 { 0x0640, "Windows95 RemoteRegistryService" },
1062 { 0x064e, "MicrosoftIIS" },
1063 { 0x067b, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1064 { 0x067c, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1065 { 0x076C, "Xerox" },
1066 { 0x079b, "ShivaLanRover/E 115" },
1067 { 0x079c, "ShivaLanRover/T 115" },
1068 { 0x07B4, "CubixWorldDesk" },
1069 { 0x07c2, "Quarterdeck IWare Connect V2.x NLM" },
1070 { 0x07c1, "Quarterdeck IWare Connect V3.x NLM" },
1071 { 0x0810, "ELAN License Server Demo" },
1072 { 0x0824, "ShivaLanRoverAccessSwitch/E" },
1073 { 0x086a, "ISSC Collector" },
1074 { 0x087f, "ISSC DAS AgentAIX" },
1075 { 0x0880, "Intel Netport PRO" },
1076 { 0x0881, "Intel Netport PRO" },
1077 { 0x0b29, "SiteLock" },
1078 { 0x0c29, "SiteLockApplications" },
1079 { 0x0c2c, "LicensingServer" },
1080 { 0x2101, "PerformanceTechnologyInstantInternet" },
1081 { 0x2380, "LAI SiteLock" },
1082 { 0x238c, "MeetingMaker" },
1083 { 0x4808, "SiteLockServer/SiteLockMetering" },
1084 { 0x5555, "SiteLockUser" },
1085 { 0x6312, "Tapeware" },
1086 { 0x6f00, "RabbitGateway" },
1087 { 0x7703, "MODEM" },
1088 { 0x8002, "NetPortPrinters" },
1089 { 0x8008, "WordPerfectNetworkVersion" },
1090 { 0x85BE, "Cisco EIGRP" },
1091 { 0x8888, "WordPerfectNetworkVersion/QuickNetworkManagement" },
1092 { 0x9000, "McAfeeNetShield" },
1093 { 0x9604, "CSA-NT_MON" },
1094 { 0xb6a8, "OceanIsleReachoutRemoteControl" },
1095 { 0xf11f, "SiteLockMetering" },
1096 { 0xf1ff, "SiteLock" },
1097 { 0xf503, "Microsoft SQL Server" },
1098 { 0xF905, "IBM TimeAndPlace" },
1099 { 0xfbfb, "TopCallIII FaxServer" },
1100 { 0xffff, "AnyService/Wildcard" },
1101 { 0, (char *)0 }
1102 };
1103
1104 static void
1105 init_ipxsaparray(void)
1106 {
1107 register int i;
1108 register struct hnamemem *table;
1109
1110 for (i = 0; ipxsap_db[i].s != NULL; i++) {
1111 int j = htons(ipxsap_db[i].v) & (HASHNAMESIZE-1);
1112 table = &ipxsaptable[j];
1113 while (table->name)
1114 table = table->nxt;
1115 table->name = ipxsap_db[i].s;
1116 table->addr = htons(ipxsap_db[i].v);
1117 table->nxt = newhnamemem();
1118 }
1119 }
1120
1121 /*
1122 * Initialize the address to name translation machinery. We map all
1123 * non-local IP addresses to numeric addresses if ndo->ndo_fflag is true
1124 * (i.e., to prevent blocking on the nameserver). localnet is the IP address
1125 * of the local network. mask is its subnet mask.
1126 */
1127 void
1128 init_addrtoname(netdissect_options *ndo, uint32_t localnet, uint32_t mask)
1129 {
1130 if (ndo->ndo_fflag) {
1131 f_localnet = localnet;
1132 f_netmask = mask;
1133 }
1134 if (ndo->ndo_nflag)
1135 /*
1136 * Simplest way to suppress names.
1137 */
1138 return;
1139
1140 init_etherarray();
1141 init_servarray(ndo);
1142 init_eprotoarray();
1143 init_protoidarray();
1144 init_ipxsaparray();
1145 }
1146
1147 const char *
1148 dnaddr_string(netdissect_options *ndo, u_short dnaddr)
1149 {
1150 register struct hnamemem *tp;
1151
1152 for (tp = &dnaddrtable[dnaddr & (HASHNAMESIZE-1)]; tp->nxt != 0;
1153 tp = tp->nxt)
1154 if (tp->addr == dnaddr)
1155 return (tp->name);
1156
1157 tp->addr = dnaddr;
1158 tp->nxt = newhnamemem();
1159 if (ndo->ndo_nflag)
1160 tp->name = dnnum_string(dnaddr);
1161 else
1162 tp->name = dnname_string(dnaddr);
1163
1164 return(tp->name);
1165 }
1166
1167 /* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
1168 struct hnamemem *
1169 newhnamemem(void)
1170 {
1171 register struct hnamemem *p;
1172 static struct hnamemem *ptr = NULL;
1173 static u_int num = 0;
1174
1175 if (num <= 0) {
1176 num = 64;
1177 ptr = (struct hnamemem *)calloc(num, sizeof (*ptr));
1178 if (ptr == NULL)
1179 error("newhnamemem: calloc");
1180 }
1181 --num;
1182 p = ptr++;
1183 return (p);
1184 }
1185
1186 #ifdef INET6
1187 /* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
1188 struct h6namemem *
1189 newh6namemem(void)
1190 {
1191 register struct h6namemem *p;
1192 static struct h6namemem *ptr = NULL;
1193 static u_int num = 0;
1194
1195 if (num <= 0) {
1196 num = 64;
1197 ptr = (struct h6namemem *)calloc(num, sizeof (*ptr));
1198 if (ptr == NULL)
1199 error("newh6namemem: calloc");
1200 }
1201 --num;
1202 p = ptr++;
1203 return (p);
1204 }
1205 #endif /* INET6 */
1206
1207 /* Represent TCI part of the 802.1Q 4-octet tag as text. */
1208 const char *
1209 ieee8021q_tci_string(const uint16_t tci)
1210 {
1211 static char buf[128];
1212 snprintf(buf, sizeof(buf), "vlan %u, p %u%s",
1213 tci & 0xfff,
1214 tci >> 13,
1215 (tci & 0x1000) ? ", DEI" : "");
1216 return buf;
1217 }