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