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