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