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