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