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