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