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