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