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