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