]> The Tcpdump Group git mirrors - tcpdump/blob - addrtoname.c
Replace some command name 'Tcpdump' with 'tcpdump'
[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[MAC48_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 } else
535 i = j = k = 0;
536
537 tp = &nsaptable[(i ^ j) & (HASHNAMESIZE-1)];
538 while (tp->e_nxt)
539 if (nsap_length == tp->e_nsap[0] &&
540 tp->e_addr0 == i &&
541 tp->e_addr1 == j &&
542 tp->e_addr2 == k &&
543 memcmp((const char *)nsap,
544 (char *)&(tp->e_nsap[1]), nsap_length) == 0)
545 return tp;
546 else
547 tp = tp->e_nxt;
548 tp->e_addr0 = (u_short)i;
549 tp->e_addr1 = (u_short)j;
550 tp->e_addr2 = (u_short)k;
551 tp->e_nsap = (u_char *)malloc(nsap_length + 1);
552 if (tp->e_nsap == NULL)
553 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, "%s: malloc", __func__);
554 tp->e_nsap[0] = (u_char)nsap_length; /* guaranteed < ISONSAP_MAX_LENGTH */
555 memcpy((char *)&tp->e_nsap[1], (const char *)nsap, nsap_length);
556 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
557 if (tp->e_nxt == NULL)
558 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, "%s: calloc", __func__);
559
560 return tp;
561 }
562
563 /* Find the hash node that corresponds the protoid 'pi'. */
564
565 static struct protoidmem *
566 lookup_protoid(netdissect_options *ndo, const u_char *pi)
567 {
568 u_int i, j;
569 struct protoidmem *tp;
570
571 /* 5 octets won't be aligned */
572 i = (((pi[0] << 8) + pi[1]) << 8) + pi[2];
573 j = (pi[3] << 8) + pi[4];
574 /* XXX should be endian-insensitive, but do big-endian testing XXX */
575
576 tp = &protoidtable[(i ^ j) & (HASHNAMESIZE-1)];
577 while (tp->p_nxt)
578 if (tp->p_oui == i && tp->p_proto == j)
579 return tp;
580 else
581 tp = tp->p_nxt;
582 tp->p_oui = i;
583 tp->p_proto = (u_short)j;
584 tp->p_nxt = (struct protoidmem *)calloc(1, sizeof(*tp));
585 if (tp->p_nxt == NULL)
586 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, "%s: calloc", __func__);
587
588 return tp;
589 }
590
591 const char *
592 mac48_string(netdissect_options *ndo, const uint8_t *ep)
593 {
594 int i;
595 char *cp;
596 struct enamemem *tp;
597 int oui;
598 char buf[BUFSIZE];
599
600 tp = lookup_emem(ndo, ep);
601 if (tp->e_name)
602 return (tp->e_name);
603 #ifdef USE_ETHER_NTOHOST
604 if (!ndo->ndo_nflag) {
605 char buf2[BUFSIZE];
606 /*
607 * This is a non-const copy of ep for ether_ntohost(), which
608 * has its second argument non-const in OpenBSD. Also saves a
609 * type cast.
610 */
611 struct ether_addr ea;
612
613 memcpy (&ea, ep, MAC48_LEN);
614 if (ether_ntohost(buf2, &ea) == 0) {
615 tp->e_name = strdup(buf2);
616 if (tp->e_name == NULL)
617 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
618 "%s: strdup(buf2)", __func__);
619 return (tp->e_name);
620 }
621 }
622 #endif
623 cp = buf;
624 oui = EXTRACT_BE_U_3(ep);
625 cp = octet_to_hex(cp, *ep++);
626 for (i = 5; --i >= 0;) {
627 *cp++ = ':';
628 cp = octet_to_hex(cp, *ep++);
629 }
630
631 if (!ndo->ndo_nflag) {
632 snprintf(cp, BUFSIZE - (2 + 5*3), " (oui %s)",
633 tok2str(oui_values, "Unknown", oui));
634 } else
635 *cp = '\0';
636 tp->e_name = strdup(buf);
637 if (tp->e_name == NULL)
638 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
639 "%s: strdup(buf)", __func__);
640 return (tp->e_name);
641 }
642
643 const char *
644 eui64_string(netdissect_options *ndo, const uint8_t *ep)
645 {
646 return (linkaddr_string(ndo, ep, LINKADDR_EUI64, EUI64_LEN));
647 }
648
649 /*
650 * EUI-64 with the rightmost octet first.
651 */
652 const char *
653 eui64le_string(netdissect_options *ndo, const uint8_t *ep)
654 {
655 const unsigned int len = 8;
656 u_int i;
657 char *cp;
658 struct bsnamemem *tp;
659 char buf[BUFSIZE];
660
661 tp = lookup_bytestring(ndo, ep, len);
662 if (tp->bs_name)
663 return (tp->bs_name);
664
665 cp = buf;
666 for (i = len; i > 0 ; --i) {
667 cp = octet_to_hex(cp, *(ep + i - 1));
668 *cp++ = ':';
669 }
670 cp --;
671
672 *cp = '\0';
673
674 tp->bs_name = strdup(buf);
675 if (tp->bs_name == NULL)
676 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
677 "%s: strdup(buf)", __func__);
678
679 return (tp->bs_name);
680 }
681
682 const char *
683 linkaddr_string(netdissect_options *ndo, const uint8_t *ep,
684 const unsigned int type, const unsigned int len)
685 {
686 u_int i;
687 char *cp;
688 struct bsnamemem *tp;
689
690 if (len == 0)
691 return ("<empty>");
692
693 if (type == LINKADDR_MAC48 && len == MAC48_LEN)
694 return (mac48_string(ndo, ep));
695
696 if (type == LINKADDR_FRELAY)
697 return (q922_string(ndo, ep, len));
698
699 tp = lookup_bytestring(ndo, ep, len);
700 if (tp->bs_name)
701 return (tp->bs_name);
702
703 tp->bs_name = cp = (char *)malloc(len*3);
704 if (tp->bs_name == NULL)
705 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
706 "%s: malloc", __func__);
707 cp = octet_to_hex(cp, *ep++);
708 for (i = len-1; i > 0 ; --i) {
709 *cp++ = ':';
710 cp = octet_to_hex(cp, *ep++);
711 }
712 *cp = '\0';
713 return (tp->bs_name);
714 }
715
716 #define ISONSAP_MAX_LENGTH 20
717 const char *
718 isonsap_string(netdissect_options *ndo, const uint8_t *nsap,
719 u_int nsap_length)
720 {
721 u_int nsap_idx;
722 char *cp;
723 struct enamemem *tp;
724
725 if (nsap_length < 1 || nsap_length > ISONSAP_MAX_LENGTH)
726 return ("isonsap_string: illegal length");
727
728 tp = lookup_nsap(ndo, nsap, nsap_length);
729 if (tp->e_name)
730 return tp->e_name;
731
732 tp->e_name = cp = (char *)malloc(sizeof("xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx"));
733 if (cp == NULL)
734 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
735 "%s: malloc", __func__);
736
737 for (nsap_idx = 0; nsap_idx < nsap_length; nsap_idx++) {
738 cp = octet_to_hex(cp, *nsap++);
739 if (((nsap_idx & 1) == 0) &&
740 (nsap_idx + 1 < nsap_length)) {
741 *cp++ = '.';
742 }
743 }
744 *cp = '\0';
745 return (tp->e_name);
746 }
747
748 const char *
749 tcpport_string(netdissect_options *ndo, u_short port)
750 {
751 struct hnamemem *tp;
752 uint32_t i = port;
753 char buf[sizeof("00000")];
754
755 for (tp = &tporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
756 if (tp->addr == i)
757 return (tp->name);
758
759 tp->addr = i;
760 tp->nxt = newhnamemem(ndo);
761
762 (void)snprintf(buf, sizeof(buf), "%u", i);
763 tp->name = strdup(buf);
764 if (tp->name == NULL)
765 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
766 "%s: strdup(buf)", __func__);
767 return (tp->name);
768 }
769
770 const char *
771 udpport_string(netdissect_options *ndo, u_short port)
772 {
773 struct hnamemem *tp;
774 uint32_t i = port;
775 char buf[sizeof("00000")];
776
777 for (tp = &uporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
778 if (tp->addr == i)
779 return (tp->name);
780
781 tp->addr = i;
782 tp->nxt = newhnamemem(ndo);
783
784 (void)snprintf(buf, sizeof(buf), "%u", i);
785 tp->name = strdup(buf);
786 if (tp->name == NULL)
787 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
788 "%s: strdup(buf)", __func__);
789 return (tp->name);
790 }
791
792 const char *
793 ipxsap_string(netdissect_options *ndo, u_short port)
794 {
795 char *cp;
796 struct hnamemem *tp;
797 uint32_t i = port;
798 char buf[sizeof("0000")];
799
800 for (tp = &ipxsaptable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
801 if (tp->addr == i)
802 return (tp->name);
803
804 tp->addr = i;
805 tp->nxt = newhnamemem(ndo);
806
807 cp = buf;
808 port = ntohs(port);
809 *cp++ = hex[port >> 12 & 0xf];
810 *cp++ = hex[port >> 8 & 0xf];
811 *cp++ = hex[port >> 4 & 0xf];
812 *cp++ = hex[port & 0xf];
813 *cp++ = '\0';
814 tp->name = strdup(buf);
815 if (tp->name == NULL)
816 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
817 "%s: strdup(buf)", __func__);
818 return (tp->name);
819 }
820
821 static void
822 init_servarray(netdissect_options *ndo)
823 {
824 struct servent *sv;
825 struct hnamemem *table;
826 int i;
827 char buf[sizeof("0000000000")];
828
829 while ((sv = getservent()) != NULL) {
830 int port = ntohs(sv->s_port);
831 i = port & (HASHNAMESIZE-1);
832 if (strcmp(sv->s_proto, "tcp") == 0)
833 table = &tporttable[i];
834 else if (strcmp(sv->s_proto, "udp") == 0)
835 table = &uporttable[i];
836 else
837 continue;
838
839 while (table->name)
840 table = table->nxt;
841 if (ndo->ndo_nflag) {
842 (void)snprintf(buf, sizeof(buf), "%d", port);
843 table->name = strdup(buf);
844 } else
845 table->name = strdup(sv->s_name);
846 if (table->name == NULL)
847 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
848 "%s: strdup", __func__);
849
850 table->addr = port;
851 table->nxt = newhnamemem(ndo);
852 }
853 endservent();
854 }
855
856 static const struct eproto {
857 const char *s;
858 u_short p;
859 } eproto_db[] = {
860 { "aarp", ETHERTYPE_AARP },
861 { "arp", ETHERTYPE_ARP },
862 { "atalk", ETHERTYPE_ATALK },
863 { "decnet", ETHERTYPE_DN },
864 { "ip", ETHERTYPE_IP },
865 { "ip6", ETHERTYPE_IPV6 },
866 { "lat", ETHERTYPE_LAT },
867 { "loopback", ETHERTYPE_LOOPBACK },
868 { "mopdl", ETHERTYPE_MOPDL },
869 { "moprc", ETHERTYPE_MOPRC },
870 { "rarp", ETHERTYPE_REVARP },
871 { "sca", ETHERTYPE_SCA },
872 { (char *)0, 0 }
873 };
874
875 static void
876 init_eprotoarray(netdissect_options *ndo)
877 {
878 int i;
879 struct hnamemem *table;
880
881 for (i = 0; eproto_db[i].s; i++) {
882 int j = htons(eproto_db[i].p) & (HASHNAMESIZE-1);
883 table = &eprototable[j];
884 while (table->name)
885 table = table->nxt;
886 table->name = eproto_db[i].s;
887 table->addr = htons(eproto_db[i].p);
888 table->nxt = newhnamemem(ndo);
889 }
890 }
891
892 static const struct protoidlist {
893 const u_char protoid[5];
894 const char *name;
895 } protoidlist[] = {
896 {{ 0x00, 0x00, 0x0c, 0x01, 0x07 }, "CiscoMLS" },
897 {{ 0x00, 0x00, 0x0c, 0x20, 0x00 }, "CiscoCDP" },
898 {{ 0x00, 0x00, 0x0c, 0x20, 0x01 }, "CiscoCGMP" },
899 {{ 0x00, 0x00, 0x0c, 0x20, 0x03 }, "CiscoVTP" },
900 {{ 0x00, 0xe0, 0x2b, 0x00, 0xbb }, "ExtremeEDP" },
901 {{ 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
902 };
903
904 /*
905 * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
906 * types.
907 */
908 static void
909 init_protoidarray(netdissect_options *ndo)
910 {
911 int i;
912 struct protoidmem *tp;
913 const struct protoidlist *pl;
914 u_char protoid[5];
915
916 protoid[0] = 0;
917 protoid[1] = 0;
918 protoid[2] = 0;
919 for (i = 0; eproto_db[i].s; i++) {
920 u_short etype = htons(eproto_db[i].p);
921
922 memcpy((char *)&protoid[3], (char *)&etype, 2);
923 tp = lookup_protoid(ndo, protoid);
924 tp->p_name = strdup(eproto_db[i].s);
925 if (tp->p_name == NULL)
926 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
927 "%s: strdup(eproto_db[i].s)", __func__);
928 }
929 /* Hardwire some SNAP proto ID names */
930 for (pl = protoidlist; pl->name != NULL; ++pl) {
931 tp = lookup_protoid(ndo, pl->protoid);
932 /* Don't override existing name */
933 if (tp->p_name != NULL)
934 continue;
935
936 tp->p_name = pl->name;
937 }
938 }
939
940 static const struct etherlist {
941 const nd_mac48 addr;
942 const char *name;
943 } etherlist[] = {
944 {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
945 {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
946 };
947
948 /*
949 * Initialize the ethers hash table. We take two different approaches
950 * depending on whether or not the system provides the ethers name
951 * service. If it does, we just wire in a few names at startup,
952 * and mac48_string() fills in the table on demand. If it doesn't,
953 * then we suck in the entire /etc/ethers file at startup. The idea
954 * is that parsing the local file will be fast, but spinning through
955 * all the ethers entries via NIS & next_etherent might be very slow.
956 *
957 * XXX pcap_next_etherent doesn't belong in the pcap interface, but
958 * since the pcap module already does name-to-address translation,
959 * it's already does most of the work for the ethernet address-to-name
960 * translation, so we just pcap_next_etherent as a convenience.
961 */
962 static void
963 init_etherarray(netdissect_options *ndo)
964 {
965 const struct etherlist *el;
966 struct enamemem *tp;
967 #ifdef USE_ETHER_NTOHOST
968 char name[256];
969 #else
970 struct pcap_etherent *ep;
971 FILE *fp;
972
973 /* Suck in entire ethers file */
974 fp = fopen(PCAP_ETHERS_FILE, "r");
975 if (fp != NULL) {
976 while ((ep = pcap_next_etherent(fp)) != NULL) {
977 tp = lookup_emem(ndo, ep->addr);
978 tp->e_name = strdup(ep->name);
979 if (tp->e_name == NULL)
980 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
981 "%s: strdup(ep->addr)", __func__);
982 }
983 (void)fclose(fp);
984 }
985 #endif
986
987 /* Hardwire some ethernet names */
988 for (el = etherlist; el->name != NULL; ++el) {
989 tp = lookup_emem(ndo, el->addr);
990 /* Don't override existing name */
991 if (tp->e_name != NULL)
992 continue;
993
994 #ifdef USE_ETHER_NTOHOST
995 /*
996 * Use YP/NIS version of name if available.
997 */
998 /* Same workaround as in mac48_string(). */
999 struct ether_addr ea;
1000 memcpy (&ea, el->addr, MAC48_LEN);
1001 if (ether_ntohost(name, &ea) == 0) {
1002 tp->e_name = strdup(name);
1003 if (tp->e_name == NULL)
1004 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
1005 "%s: strdup(name)", __func__);
1006 continue;
1007 }
1008 #endif
1009 tp->e_name = el->name;
1010 }
1011 }
1012
1013 static const struct ipxsap_ent {
1014 uint16_t v;
1015 const char *s;
1016 } ipxsap_db[] = {
1017 { 0x0000, "Unknown" },
1018 { 0x0001, "User" },
1019 { 0x0002, "User Group" },
1020 { 0x0003, "PrintQueue" },
1021 { 0x0004, "FileServer" },
1022 { 0x0005, "JobServer" },
1023 { 0x0006, "Gateway" },
1024 { 0x0007, "PrintServer" },
1025 { 0x0008, "ArchiveQueue" },
1026 { 0x0009, "ArchiveServer" },
1027 { 0x000a, "JobQueue" },
1028 { 0x000b, "Administration" },
1029 { 0x000F, "Novell TI-RPC" },
1030 { 0x0017, "Diagnostics" },
1031 { 0x0020, "NetBIOS" },
1032 { 0x0021, "NAS SNA Gateway" },
1033 { 0x0023, "NACS AsyncGateway" },
1034 { 0x0024, "RemoteBridge/RoutingService" },
1035 { 0x0026, "BridgeServer" },
1036 { 0x0027, "TCP/IP Gateway" },
1037 { 0x0028, "Point-to-point X.25 BridgeServer" },
1038 { 0x0029, "3270 Gateway" },
1039 { 0x002a, "CHI Corp" },
1040 { 0x002c, "PC Chalkboard" },
1041 { 0x002d, "TimeSynchServer" },
1042 { 0x002e, "ARCserve5.0/PalindromeBackup" },
1043 { 0x0045, "DI3270 Gateway" },
1044 { 0x0047, "AdvertisingPrintServer" },
1045 { 0x004a, "NetBlazerModems" },
1046 { 0x004b, "BtrieveVAP" },
1047 { 0x004c, "NetwareSQL" },
1048 { 0x004d, "XtreeNetwork" },
1049 { 0x0050, "BtrieveVAP4.11" },
1050 { 0x0052, "QuickLink" },
1051 { 0x0053, "PrintQueueUser" },
1052 { 0x0058, "Multipoint X.25 Router" },
1053 { 0x0060, "STLB/NLM" },
1054 { 0x0064, "ARCserve" },
1055 { 0x0066, "ARCserve3.0" },
1056 { 0x0072, "WAN CopyUtility" },
1057 { 0x007a, "TES-NetwareVMS" },
1058 { 0x0092, "WATCOM Debugger/EmeraldTapeBackupServer" },
1059 { 0x0095, "DDA OBGYN" },
1060 { 0x0098, "NetwareAccessServer" },
1061 { 0x009a, "Netware for VMS II/NamedPipeServer" },
1062 { 0x009b, "NetwareAccessServer" },
1063 { 0x009e, "PortableNetwareServer/SunLinkNVT" },
1064 { 0x00a1, "PowerchuteAPC UPS" },
1065 { 0x00aa, "LAWserve" },
1066 { 0x00ac, "CompaqIDA StatusMonitor" },
1067 { 0x0100, "PIPE STAIL" },
1068 { 0x0102, "LAN ProtectBindery" },
1069 { 0x0103, "OracleDataBaseServer" },
1070 { 0x0107, "Netware386/RSPX RemoteConsole" },
1071 { 0x010f, "NovellSNA Gateway" },
1072 { 0x0111, "TestServer" },
1073 { 0x0112, "HP PrintServer" },
1074 { 0x0114, "CSA MUX" },
1075 { 0x0115, "CSA LCA" },
1076 { 0x0116, "CSA CM" },
1077 { 0x0117, "CSA SMA" },
1078 { 0x0118, "CSA DBA" },
1079 { 0x0119, "CSA NMA" },
1080 { 0x011a, "CSA SSA" },
1081 { 0x011b, "CSA STATUS" },
1082 { 0x011e, "CSA APPC" },
1083 { 0x0126, "SNA TEST SSA Profile" },
1084 { 0x012a, "CSA TRACE" },
1085 { 0x012b, "NetwareSAA" },
1086 { 0x012e, "IKARUS VirusScan" },
1087 { 0x0130, "CommunicationsExecutive" },
1088 { 0x0133, "NNS DomainServer/NetwareNamingServicesDomain" },
1089 { 0x0135, "NetwareNamingServicesProfile" },
1090 { 0x0137, "Netware386 PrintQueue/NNS PrintQueue" },
1091 { 0x0141, "LAN SpoolServer" },
1092 { 0x0152, "IRMALAN Gateway" },
1093 { 0x0154, "NamedPipeServer" },
1094 { 0x0166, "NetWareManagement" },
1095 { 0x0168, "Intel PICKIT CommServer/Intel CAS TalkServer" },
1096 { 0x0173, "Compaq" },
1097 { 0x0174, "Compaq SNMP Agent" },
1098 { 0x0175, "Compaq" },
1099 { 0x0180, "XTreeServer/XTreeTools" },
1100 { 0x018A, "NASI ServicesBroadcastServer" },
1101 { 0x01b0, "GARP Gateway" },
1102 { 0x01b1, "Binfview" },
1103 { 0x01bf, "IntelLanDeskManager" },
1104 { 0x01ca, "AXTEC" },
1105 { 0x01cb, "ShivaNetModem/E" },
1106 { 0x01cc, "ShivaLanRover/E" },
1107 { 0x01cd, "ShivaLanRover/T" },
1108 { 0x01ce, "ShivaUniversal" },
1109 { 0x01d8, "CastelleFAXPressServer" },
1110 { 0x01da, "CastelleLANPressPrintServer" },
1111 { 0x01dc, "CastelleFAX/Xerox7033 FaxServer/ExcelLanFax" },
1112 { 0x01f0, "LEGATO" },
1113 { 0x01f5, "LEGATO" },
1114 { 0x0233, "NMS Agent/NetwareManagementAgent" },
1115 { 0x0237, "NMS IPX Discovery/LANternReadWriteChannel" },
1116 { 0x0238, "NMS IP Discovery/LANternTrapAlarmChannel" },
1117 { 0x023a, "LANtern" },
1118 { 0x023c, "MAVERICK" },
1119 { 0x023f, "NovellSMDR" },
1120 { 0x024e, "NetwareConnect" },
1121 { 0x024f, "NASI ServerBroadcast Cisco" },
1122 { 0x026a, "NMS ServiceConsole" },
1123 { 0x026b, "TimeSynchronizationServer Netware 4.x" },
1124 { 0x0278, "DirectoryServer Netware 4.x" },
1125 { 0x027b, "NetwareManagementAgent" },
1126 { 0x0280, "Novell File and Printer Sharing Service for PC" },
1127 { 0x0304, "NovellSAA Gateway" },
1128 { 0x0308, "COM/VERMED" },
1129 { 0x030a, "GalacticommWorldgroupServer" },
1130 { 0x030c, "IntelNetport2/HP JetDirect/HP Quicksilver" },
1131 { 0x0320, "AttachmateGateway" },
1132 { 0x0327, "MicrosoftDiagnostics" },
1133 { 0x0328, "WATCOM SQL Server" },
1134 { 0x0335, "MultiTechSystems MultisynchCommServer" },
1135 { 0x0343, "Xylogics RemoteAccessServer/LANModem" },
1136 { 0x0355, "ArcadaBackupExec" },
1137 { 0x0358, "MSLCD1" },
1138 { 0x0361, "NETINELO" },
1139 { 0x037e, "Powerchute UPS Monitoring" },
1140 { 0x037f, "ViruSafeNotify" },
1141 { 0x0386, "HP Bridge" },
1142 { 0x0387, "HP Hub" },
1143 { 0x0394, "NetWare SAA Gateway" },
1144 { 0x039b, "LotusNotes" },
1145 { 0x03b7, "CertusAntiVirus" },
1146 { 0x03c4, "ARCserve4.0" },
1147 { 0x03c7, "LANspool3.5" },
1148 { 0x03d7, "LexmarkPrinterServer" },
1149 { 0x03d8, "LexmarkXLE PrinterServer" },
1150 { 0x03dd, "BanyanENS NetwareClient" },
1151 { 0x03de, "GuptaSequelBaseServer/NetWareSQL" },
1152 { 0x03e1, "UnivelUnixware" },
1153 { 0x03e4, "UnivelUnixware" },
1154 { 0x03fc, "IntelNetport" },
1155 { 0x03fd, "PrintServerQueue" },
1156 { 0x040A, "ipnServer" },
1157 { 0x040D, "LVERRMAN" },
1158 { 0x040E, "LVLIC" },
1159 { 0x0414, "NET Silicon (DPI)/Kyocera" },
1160 { 0x0429, "SiteLockVirus" },
1161 { 0x0432, "UFHELPR???" },
1162 { 0x0433, "Synoptics281xAdvancedSNMPAgent" },
1163 { 0x0444, "MicrosoftNT SNA Server" },
1164 { 0x0448, "Oracle" },
1165 { 0x044c, "ARCserve5.01" },
1166 { 0x0457, "CanonGP55" },
1167 { 0x045a, "QMS Printers" },
1168 { 0x045b, "DellSCSI Array" },
1169 { 0x0491, "NetBlazerModems" },
1170 { 0x04ac, "OnTimeScheduler" },
1171 { 0x04b0, "CD-Net" },
1172 { 0x0513, "EmulexNQA" },
1173 { 0x0520, "SiteLockChecks" },
1174 { 0x0529, "SiteLockChecks" },
1175 { 0x052d, "CitrixOS2 AppServer" },
1176 { 0x0535, "Tektronix" },
1177 { 0x0536, "Milan" },
1178 { 0x055d, "Attachmate SNA gateway" },
1179 { 0x056b, "IBM8235 ModemServer" },
1180 { 0x056c, "ShivaLanRover/E PLUS" },
1181 { 0x056d, "ShivaLanRover/T PLUS" },
1182 { 0x0580, "McAfeeNetShield" },
1183 { 0x05B8, "NLM to workstation communication (Revelation Software)" },
1184 { 0x05BA, "CompatibleSystemsRouters" },
1185 { 0x05BE, "CheyenneHierarchicalStorageManager" },
1186 { 0x0606, "JCWatermarkImaging" },
1187 { 0x060c, "AXISNetworkPrinter" },
1188 { 0x0610, "AdaptecSCSIManagement" },
1189 { 0x0621, "IBM AntiVirus" },
1190 { 0x0640, "Windows95 RemoteRegistryService" },
1191 { 0x064e, "MicrosoftIIS" },
1192 { 0x067b, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1193 { 0x067c, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1194 { 0x076C, "Xerox" },
1195 { 0x079b, "ShivaLanRover/E 115" },
1196 { 0x079c, "ShivaLanRover/T 115" },
1197 { 0x07B4, "CubixWorldDesk" },
1198 { 0x07c2, "Quarterdeck IWare Connect V2.x NLM" },
1199 { 0x07c1, "Quarterdeck IWare Connect V3.x NLM" },
1200 { 0x0810, "ELAN License Server Demo" },
1201 { 0x0824, "ShivaLanRoverAccessSwitch/E" },
1202 { 0x086a, "ISSC Collector" },
1203 { 0x087f, "ISSC DAS AgentAIX" },
1204 { 0x0880, "Intel Netport PRO" },
1205 { 0x0881, "Intel Netport PRO" },
1206 { 0x0b29, "SiteLock" },
1207 { 0x0c29, "SiteLockApplications" },
1208 { 0x0c2c, "LicensingServer" },
1209 { 0x2101, "PerformanceTechnologyInstantInternet" },
1210 { 0x2380, "LAI SiteLock" },
1211 { 0x238c, "MeetingMaker" },
1212 { 0x4808, "SiteLockServer/SiteLockMetering" },
1213 { 0x5555, "SiteLockUser" },
1214 { 0x6312, "Tapeware" },
1215 { 0x6f00, "RabbitGateway" },
1216 { 0x7703, "MODEM" },
1217 { 0x8002, "NetPortPrinters" },
1218 { 0x8008, "WordPerfectNetworkVersion" },
1219 { 0x85BE, "Cisco EIGRP" },
1220 { 0x8888, "WordPerfectNetworkVersion/QuickNetworkManagement" },
1221 { 0x9000, "McAfeeNetShield" },
1222 { 0x9604, "CSA-NT_MON" },
1223 { 0xb6a8, "OceanIsleReachoutRemoteControl" },
1224 { 0xf11f, "SiteLockMetering" },
1225 { 0xf1ff, "SiteLock" },
1226 { 0xf503, "Microsoft SQL Server" },
1227 { 0xF905, "IBM TimeAndPlace" },
1228 { 0xfbfb, "TopCallIII FaxServer" },
1229 { 0xffff, "AnyService/Wildcard" },
1230 { 0, (char *)0 }
1231 };
1232
1233 static void
1234 init_ipxsaparray(netdissect_options *ndo)
1235 {
1236 int i;
1237 struct hnamemem *table;
1238
1239 for (i = 0; ipxsap_db[i].s != NULL; i++) {
1240 u_int j = htons(ipxsap_db[i].v) & (HASHNAMESIZE-1);
1241 table = &ipxsaptable[j];
1242 while (table->name)
1243 table = table->nxt;
1244 table->name = ipxsap_db[i].s;
1245 table->addr = htons(ipxsap_db[i].v);
1246 table->nxt = newhnamemem(ndo);
1247 }
1248 }
1249
1250 /*
1251 * Initialize the address to name translation machinery. We map all
1252 * non-local IP addresses to numeric addresses if ndo->ndo_fflag is true
1253 * (i.e., to prevent blocking on the nameserver). localnet is the IP address
1254 * of the local network. mask is its subnet mask.
1255 */
1256 void
1257 init_addrtoname(netdissect_options *ndo, uint32_t localnet, uint32_t mask)
1258 {
1259 if (ndo->ndo_fflag) {
1260 f_localnet = localnet;
1261 f_netmask = mask;
1262 }
1263 if (ndo->ndo_nflag)
1264 /*
1265 * Simplest way to suppress names.
1266 */
1267 return;
1268
1269 init_etherarray(ndo);
1270 init_servarray(ndo);
1271 init_eprotoarray(ndo);
1272 init_protoidarray(ndo);
1273 init_ipxsaparray(ndo);
1274 }
1275
1276 const char *
1277 dnaddr_string(netdissect_options *ndo, u_short dnaddr)
1278 {
1279 struct hnamemem *tp;
1280
1281 for (tp = &dnaddrtable[dnaddr & (HASHNAMESIZE-1)]; tp->nxt != NULL;
1282 tp = tp->nxt)
1283 if (tp->addr == dnaddr)
1284 return (tp->name);
1285
1286 tp->addr = dnaddr;
1287 tp->nxt = newhnamemem(ndo);
1288 tp->name = dnnum_string(ndo, dnaddr);
1289
1290 return(tp->name);
1291 }
1292
1293 /* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
1294 struct hnamemem *
1295 newhnamemem(netdissect_options *ndo)
1296 {
1297 struct hnamemem *p;
1298 static struct hnamemem *ptr = NULL;
1299 static u_int num = 0;
1300
1301 if (num == 0) {
1302 num = 64;
1303 ptr = (struct hnamemem *)calloc(num, sizeof (*ptr));
1304 if (ptr == NULL)
1305 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
1306 "%s: calloc", __func__);
1307 }
1308 --num;
1309 p = ptr++;
1310 return (p);
1311 }
1312
1313 /* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
1314 struct h6namemem *
1315 newh6namemem(netdissect_options *ndo)
1316 {
1317 struct h6namemem *p;
1318 static struct h6namemem *ptr = NULL;
1319 static u_int num = 0;
1320
1321 if (num == 0) {
1322 num = 64;
1323 ptr = (struct h6namemem *)calloc(num, sizeof (*ptr));
1324 if (ptr == NULL)
1325 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
1326 "%s: calloc", __func__);
1327 }
1328 --num;
1329 p = ptr++;
1330 return (p);
1331 }
1332
1333 /* Represent TCI part of the 802.1Q 4-octet tag as text. */
1334 const char *
1335 ieee8021q_tci_string(const uint16_t tci)
1336 {
1337 static char buf[128];
1338 snprintf(buf, sizeof(buf), "vlan %u, p %u%s",
1339 tci & 0xfff,
1340 tci >> 13,
1341 (tci & 0x1000) ? ", DEI" : "");
1342 return buf;
1343 }