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