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