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