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