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