addrtoname.c (127675) | addrtoname.c (146778) |
---|---|
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 --- 7 unchanged lines hidden (view full) --- 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 * | 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 --- 7 unchanged lines hidden (view full) --- 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 127675 2004-03-31 14:57:24Z bms $ | 24 * $FreeBSD: head/contrib/tcpdump/addrtoname.c 146778 2005-05-29 19:09:28Z sam $ |
25 */ 26#ifndef lint 27static const char rcsid[] _U_ = | 25 */ 26#ifndef lint 27static const char rcsid[] _U_ = |
28 "@(#) $Header: /tcpdump/master/tcpdump/addrtoname.c,v 1.96.2.6 2004/03/24 04:14:31 guy Exp $ (LBL)"; | 28 "@(#) $Header: /tcpdump/master/tcpdump/addrtoname.c,v 1.108 2005/03/27 22:38:09 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 */ | 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 HAVE_NETINET_ETHER_H 45#include <netinet/ether.h> /* ether_ntohost on linux */ 46#endif /* HAVE_NETINET_ETHER_H */ | 44#ifdef NETINET_ETHER_H_DECLARES_ETHER_NTOHOST 45#include 46#endif /* NETINET_ETHER_H_DECLARES_ETHER_NTOHOST */ |
47#endif /* USE_ETHER_NTOHOST */ 48 | 47#endif /* USE_ETHER_NTOHOST */ 48 |
49#if !defined(HAVE_DECL_ETHER_NTOHOST) || !HAVE_DECL_ETHER_NTOHOST 50extern int ether_ntohost(char *, const struct ether_addr *); 51#endif 52 |
|
49#include <pcap.h> 50#include <pcap-namedb.h> 51#include <signal.h> 52#include <stdio.h> 53#include <string.h> 54#include <stdlib.h> 55 56#include "interface.h" --- 43 unchanged lines hidden (view full) --- 100 switch (type) { 101 case AF_INET: 102 return gethostbyaddr(addr, len, type); 103 break; 104 case AF_INET6: 105 memset(&addr6, 0, sizeof(addr6)); 106 addr6.sin6_family = AF_INET6; 107 memcpy(&addr6.sin6_addr, addr, len); | 53#include <pcap.h> 54#include <pcap-namedb.h> 55#include <signal.h> 56#include <stdio.h> 57#include <string.h> 58#include <stdlib.h> 59 60#include "interface.h" --- 43 unchanged lines hidden (view full) --- 104 switch (type) { 105 case AF_INET: 106 return gethostbyaddr(addr, len, type); 107 break; 108 case AF_INET6: 109 memset(&addr6, 0, sizeof(addr6)); 110 addr6.sin6_family = AF_INET6; 111 memcpy(&addr6.sin6_addr, addr, len); |
108#ifdef __MINGW32__ 109 /* MinGW doesn't provide getnameinfo */ 110 return NULL; 111#else | |
112 if (getnameinfo((struct sockaddr *)&addr6, sizeof(addr6), | 112 if (getnameinfo((struct sockaddr *)&addr6, sizeof(addr6), |
113 hname, sizeof(hname), NULL, 0, 0)) { 114 return NULL; | 113 hname, sizeof(hname), NULL, 0, 0)) { 114 return NULL; |
115 } else { 116 strcpy(host.h_name, hname); 117 return &host; 118 } | 115 } else { 116 strcpy(host.h_name, hname); 117 return &host; 118 } |
119#endif /* __MINGW32__ */ | |
120 break; 121 default: 122 return NULL; 123 } 124} 125#define gethostbyaddr win32_gethostbyaddr | 119 break; 120 default: 121 return NULL; 122 } 123} 124#define gethostbyaddr win32_gethostbyaddr |
126#endif /* INET6 & WIN32*/ | 125#endif /* INET6 & WIN32 */ |
127 128#ifdef INET6 129struct h6namemem { 130 struct in6_addr addr; 131 char *name; 132 struct h6namemem *nxt; 133}; 134 --- 326 unchanged lines hidden (view full) --- 461 char buf[sizeof("00:00:00:00:00:00")]; 462 463 tp = lookup_emem(ep); 464 if (tp->e_name) 465 return (tp->e_name); 466#ifdef USE_ETHER_NTOHOST 467 if (!nflag) { 468 char buf2[128]; | 126 127#ifdef INET6 128struct h6namemem { 129 struct in6_addr addr; 130 char *name; 131 struct h6namemem *nxt; 132}; 133 --- 326 unchanged lines hidden (view full) --- 460 char buf[sizeof("00:00:00:00:00:00")]; 461 462 tp = lookup_emem(ep); 463 if (tp->e_name) 464 return (tp->e_name); 465#ifdef USE_ETHER_NTOHOST 466 if (!nflag) { 467 char buf2[128]; |
469 if (ether_ntohost(buf2, (const struct ether_addr *)ep) == 0) { | 468 469 /* 470 * We don't cast it to "const struct ether_addr *" 471 * because some systems don't modify the Ethernet 472 * address but fail to declare the second argument 473 * as a "const" pointer. 474 */ 475 if (ether_ntohost(buf2, (struct ether_addr *)ep) == 0) { |
470 tp->e_name = strdup(buf2); 471 return (tp->e_name); 472 } 473 } 474#endif 475 cp = buf; | 476 tp->e_name = strdup(buf2); 477 return (tp->e_name); 478 } 479 } 480#endif 481 cp = buf; |
476 *cp++ = hex[*ep >> 4 ]; | 482 *cp++ = hex[*ep >> 4 ]; |
477 *cp++ = hex[*ep++ & 0xf]; 478 for (i = 5; (int)--i >= 0;) { 479 *cp++ = ':'; | 483 *cp++ = hex[*ep++ & 0xf]; 484 for (i = 5; (int)--i >= 0;) { 485 *cp++ = ':'; |
480 *cp++ = hex[*ep >> 4 ]; | 486 *cp++ = hex[*ep >> 4 ]; |
481 *cp++ = hex[*ep++ & 0xf]; 482 } 483 *cp = '\0'; 484 tp->e_name = strdup(buf); 485 return (tp->e_name); 486} 487 488const char * 489linkaddr_string(const u_char *ep, const unsigned int len) 490{ | 487 *cp++ = hex[*ep++ & 0xf]; 488 } 489 *cp = '\0'; 490 tp->e_name = strdup(buf); 491 return (tp->e_name); 492} 493 494const char * 495linkaddr_string(const u_char *ep, const unsigned int len) 496{ |
491 register u_int i, j; | 497 register u_int i; |
492 register char *cp; 493 register struct enamemem *tp; 494 495 if (len == 6) /* XXX not totally correct... */ 496 return etheraddr_string(ep); 497 498 tp = lookup_bytestring(ep, len); 499 if (tp->e_name) 500 return (tp->e_name); 501 502 tp->e_name = cp = (char *)malloc(len*3); 503 if (tp->e_name == NULL) 504 error("linkaddr_string: malloc"); | 498 register char *cp; 499 register struct enamemem *tp; 500 501 if (len == 6) /* XXX not totally correct... */ 502 return etheraddr_string(ep); 503 504 tp = lookup_bytestring(ep, len); 505 if (tp->e_name) 506 return (tp->e_name); 507 508 tp->e_name = cp = (char *)malloc(len*3); 509 if (tp->e_name == NULL) 510 error("linkaddr_string: malloc"); |
505 if ((j = *ep >> 4) != 0) 506 *cp++ = hex[j]; | 511 *cp++ = hex[*ep >> 4]; |
507 *cp++ = hex[*ep++ & 0xf]; 508 for (i = len-1; i > 0 ; --i) { 509 *cp++ = ':'; | 512 *cp++ = hex[*ep++ & 0xf]; 513 for (i = len-1; i > 0 ; --i) { 514 *cp++ = ':'; |
510 if ((j = *ep >> 4) != 0) 511 *cp++ = hex[j]; | 515 *cp++ = hex[*ep >> 4]; |
512 *cp++ = hex[*ep++ & 0xf]; 513 } 514 *cp = '\0'; 515 return (tp->e_name); 516} 517 518const char * 519etherproto_string(u_short port) --- 62 unchanged lines hidden (view full) --- 582 tp->addr = i; 583 tp->nxt = newhnamemem(); 584 585 snprintf(buf, sizeof(buf), "sap %02x", sap & 0xff); 586 tp->name = strdup(buf); 587 return (tp->name); 588} 589 | 516 *cp++ = hex[*ep++ & 0xf]; 517 } 518 *cp = '\0'; 519 return (tp->e_name); 520} 521 522const char * 523etherproto_string(u_short port) --- 62 unchanged lines hidden (view full) --- 586 tp->addr = i; 587 tp->nxt = newhnamemem(); 588 589 snprintf(buf, sizeof(buf), "sap %02x", sap & 0xff); 590 tp->name = strdup(buf); 591 return (tp->name); 592} 593 |
594#define ISONSAP_MAX_LENGTH 20 |
|
590const char * | 595const char * |
591isonsap_string(const u_char *nsap) | 596isonsap_string(const u_char *nsap, register u_int nsap_length) |
592{ | 597{ |
593 register u_int i, nlen = nsap[0]; | 598 register u_int nsap_idx; |
594 register char *cp; 595 register struct enamemem *tp; 596 | 599 register char *cp; 600 register struct enamemem *tp; 601 |
602 if (nsap_length < 1 || nsap_length > ISONSAP_MAX_LENGTH) 603 error("isonsap_string: illegal length"); 604 |
|
597 tp = lookup_nsap(nsap); 598 if (tp->e_name) 599 return tp->e_name; 600 | 605 tp = lookup_nsap(nsap); 606 if (tp->e_name) 607 return tp->e_name; 608 |
601 tp->e_name = cp = (char *)malloc(nlen * 2 + 2 + (nlen>>1)); | 609 tp->e_name = cp = (char *)malloc(sizeof("xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx")); |
602 if (cp == NULL) 603 error("isonsap_string: malloc"); 604 | 610 if (cp == NULL) 611 error("isonsap_string: malloc"); 612 |
605 nsap++; 606 for (i = 0; i < nlen; i++) { | 613 for (nsap_idx = 0; nsap_idx < nsap_length; nsap_idx++) { |
607 *cp++ = hex[*nsap >> 4]; 608 *cp++ = hex[*nsap++ & 0xf]; | 614 *cp++ = hex[*nsap >> 4]; 615 *cp++ = hex[*nsap++ & 0xf]; |
609 if (((i & 1) == 0) && (i + 1 < nlen)) 610 *cp++ = '.'; | 616 if (((nsap_idx & 1) == 0) && 617 (nsap_idx + 1 < nsap_length)) { 618 *cp++ = '.'; 619 } |
611 } 612 *cp = '\0'; 613 return (tp->e_name); 614} 615 616const char * 617tcpport_string(u_short port) 618{ --- 84 unchanged lines hidden (view full) --- 703 } else 704 table->name = strdup(sv->s_name); 705 table->addr = port; 706 table->nxt = newhnamemem(); 707 } 708 endservent(); 709} 710 | 620 } 621 *cp = '\0'; 622 return (tp->e_name); 623} 624 625const char * 626tcpport_string(u_short port) 627{ --- 84 unchanged lines hidden (view full) --- 712 } else 713 table->name = strdup(sv->s_name); 714 table->addr = port; 715 table->nxt = newhnamemem(); 716 } 717 endservent(); 718} 719 |
711/*XXX from libbpfc.a */ 712#ifndef WIN32 713extern struct eproto { | 720/* in libpcap.a (nametoaddr.c) */ 721#if defined(WIN32) && !defined(USE_STATIC_LIBPCAP) 722__declspec(dllimport) |
714#else | 723#else |
715__declspec( dllimport) struct eproto { | 724extern |
716#endif | 725#endif |
717 char *s; | 726const struct eproto { 727 const char *s; |
718 u_short p; 719} eproto_db[]; 720 721static void 722init_eprotoarray(void) 723{ 724 register int i; 725 register struct hnamemem *table; --- 101 unchanged lines hidden (view full) --- 827 /* Hardwire some ethernet names */ 828 for (el = etherlist; el->name != NULL; ++el) { 829 tp = lookup_emem(el->addr); 830 /* Don't override existing name */ 831 if (tp->e_name != NULL) 832 continue; 833 834#ifdef USE_ETHER_NTOHOST | 728 u_short p; 729} eproto_db[]; 730 731static void 732init_eprotoarray(void) 733{ 734 register int i; 735 register struct hnamemem *table; --- 101 unchanged lines hidden (view full) --- 837 /* Hardwire some ethernet names */ 838 for (el = etherlist; el->name != NULL; ++el) { 839 tp = lookup_emem(el->addr); 840 /* Don't override existing name */ 841 if (tp->e_name != NULL) 842 continue; 843 844#ifdef USE_ETHER_NTOHOST |
835 /* Use yp/nis version of name if available */ 836 if (ether_ntohost(name, (const struct ether_addr *)el->addr) == 0) { 837 tp->e_name = strdup(name); | 845 /* 846 * Use YP/NIS version of name if available. 847 * 848 * We don't cast it to "const struct ether_addr *" 849 * because some systems don't modify the Ethernet 850 * address but fail to declare the second argument 851 * as a "const" pointer. 852 */ 853 if (ether_ntohost(name, (struct ether_addr *)el->addr) == 0) { 854 tp->e_name = strdup(name); |
838 continue; 839 } 840#endif 841 tp->e_name = el->name; 842 } 843} 844 845static struct tok llcsap_db[] = { --- 353 unchanged lines hidden --- | 855 continue; 856 } 857#endif 858 tp->e_name = el->name; 859 } 860} 861 862static struct tok llcsap_db[] = { --- 353 unchanged lines hidden --- |