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