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