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