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