11077d0bdSPeter Avalos /* 21077d0bdSPeter Avalos * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998 31077d0bdSPeter Avalos * The Regents of the University of California. All rights reserved. 41077d0bdSPeter Avalos * 51077d0bdSPeter Avalos * Redistribution and use in source and binary forms, with or without 61077d0bdSPeter Avalos * modification, are permitted provided that: (1) source code distributions 71077d0bdSPeter Avalos * retain the above copyright notice and this paragraph in its entirety, (2) 81077d0bdSPeter Avalos * distributions including binary code include the above copyright notice and 91077d0bdSPeter Avalos * this paragraph in its entirety in the documentation or other materials 101077d0bdSPeter Avalos * provided with the distribution, and (3) all advertising materials mentioning 111077d0bdSPeter Avalos * features or use of this software display the following acknowledgement: 121077d0bdSPeter Avalos * ``This product includes software developed by the University of California, 131077d0bdSPeter Avalos * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 141077d0bdSPeter Avalos * the University nor the names of its contributors may be used to endorse 151077d0bdSPeter Avalos * or promote products derived from this software without specific prior 161077d0bdSPeter Avalos * written permission. 171077d0bdSPeter Avalos * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 181077d0bdSPeter Avalos * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 191077d0bdSPeter Avalos * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 201077d0bdSPeter Avalos * 211077d0bdSPeter Avalos * Name to id translation routines used by the scanner. 221077d0bdSPeter Avalos * These functions are not time critical. 231077d0bdSPeter Avalos */ 241077d0bdSPeter Avalos 251077d0bdSPeter Avalos #ifndef lint 261077d0bdSPeter Avalos static const char rcsid[] _U_ = 27*a85e14b0SPeter Avalos "@(#) $Header: /tcpdump/master/libpcap/nametoaddr.c,v 1.83 2008-02-06 10:21:30 guy Exp $ (LBL)"; 281077d0bdSPeter Avalos #endif 291077d0bdSPeter Avalos 301077d0bdSPeter Avalos #ifdef HAVE_CONFIG_H 311077d0bdSPeter Avalos #include "config.h" 321077d0bdSPeter Avalos #endif 331077d0bdSPeter Avalos 34*a85e14b0SPeter Avalos #ifdef DECNETLIB 35*a85e14b0SPeter Avalos #include <sys/types.h> 36*a85e14b0SPeter Avalos #include <netdnet/dnetdb.h> 37*a85e14b0SPeter Avalos #endif 38*a85e14b0SPeter Avalos 391077d0bdSPeter Avalos #ifdef WIN32 401077d0bdSPeter Avalos #include <pcap-stdinc.h> 411077d0bdSPeter Avalos 421077d0bdSPeter Avalos #else /* WIN32 */ 431077d0bdSPeter Avalos 441077d0bdSPeter Avalos #include <sys/param.h> 451077d0bdSPeter Avalos #include <sys/types.h> /* concession to AIX */ 461077d0bdSPeter Avalos #include <sys/socket.h> 471077d0bdSPeter Avalos #include <sys/time.h> 481077d0bdSPeter Avalos 491077d0bdSPeter Avalos #include <netinet/in.h> 501077d0bdSPeter Avalos #endif /* WIN32 */ 511077d0bdSPeter Avalos 521077d0bdSPeter Avalos #ifndef WIN32 531077d0bdSPeter Avalos #ifdef HAVE_ETHER_HOSTTON 541077d0bdSPeter Avalos /* 551077d0bdSPeter Avalos * XXX - do we need any of this if <netinet/if_ether.h> doesn't declare 561077d0bdSPeter Avalos * ether_hostton()? 571077d0bdSPeter Avalos */ 581077d0bdSPeter Avalos #ifdef HAVE_NETINET_IF_ETHER_H 591077d0bdSPeter Avalos struct mbuf; /* Squelch compiler warnings on some platforms for */ 601077d0bdSPeter Avalos struct rtentry; /* declarations in <net/if.h> */ 611077d0bdSPeter Avalos #include <net/if.h> /* for "struct ifnet" in "struct arpcom" on Solaris */ 621077d0bdSPeter Avalos #include <netinet/if_ether.h> 631077d0bdSPeter Avalos #endif /* HAVE_NETINET_IF_ETHER_H */ 641077d0bdSPeter Avalos #ifdef NETINET_ETHER_H_DECLARES_ETHER_HOSTTON 651077d0bdSPeter Avalos #include <netinet/ether.h> 661077d0bdSPeter Avalos #endif /* NETINET_ETHER_H_DECLARES_ETHER_HOSTTON */ 671077d0bdSPeter Avalos #endif /* HAVE_ETHER_HOSTTON */ 681077d0bdSPeter Avalos #include <arpa/inet.h> 691077d0bdSPeter Avalos #include <netdb.h> 701077d0bdSPeter Avalos #endif /* WIN32 */ 711077d0bdSPeter Avalos 721077d0bdSPeter Avalos #include <ctype.h> 731077d0bdSPeter Avalos #include <errno.h> 741077d0bdSPeter Avalos #include <stdlib.h> 751077d0bdSPeter Avalos #include <string.h> 761077d0bdSPeter Avalos #include <stdio.h> 771077d0bdSPeter Avalos 781077d0bdSPeter Avalos #include "pcap-int.h" 791077d0bdSPeter Avalos 801077d0bdSPeter Avalos #include "gencode.h" 81de0d3203SPeter Avalos #include <pcap/namedb.h> 821077d0bdSPeter Avalos 831077d0bdSPeter Avalos #ifdef HAVE_OS_PROTO_H 841077d0bdSPeter Avalos #include "os-proto.h" 851077d0bdSPeter Avalos #endif 861077d0bdSPeter Avalos 871077d0bdSPeter Avalos #ifndef NTOHL 881077d0bdSPeter Avalos #define NTOHL(x) (x) = ntohl(x) 891077d0bdSPeter Avalos #define NTOHS(x) (x) = ntohs(x) 901077d0bdSPeter Avalos #endif 911077d0bdSPeter Avalos 921077d0bdSPeter Avalos static inline int xdtoi(int); 931077d0bdSPeter Avalos 941077d0bdSPeter Avalos /* 951077d0bdSPeter Avalos * Convert host name to internet address. 961077d0bdSPeter Avalos * Return 0 upon failure. 971077d0bdSPeter Avalos */ 981077d0bdSPeter Avalos bpf_u_int32 ** 991077d0bdSPeter Avalos pcap_nametoaddr(const char *name) 1001077d0bdSPeter Avalos { 1011077d0bdSPeter Avalos #ifndef h_addr 1021077d0bdSPeter Avalos static bpf_u_int32 *hlist[2]; 1031077d0bdSPeter Avalos #endif 1041077d0bdSPeter Avalos bpf_u_int32 **p; 1051077d0bdSPeter Avalos struct hostent *hp; 1061077d0bdSPeter Avalos 1071077d0bdSPeter Avalos if ((hp = gethostbyname(name)) != NULL) { 1081077d0bdSPeter Avalos #ifndef h_addr 1091077d0bdSPeter Avalos hlist[0] = (bpf_u_int32 *)hp->h_addr; 1101077d0bdSPeter Avalos NTOHL(hp->h_addr); 1111077d0bdSPeter Avalos return hlist; 1121077d0bdSPeter Avalos #else 1131077d0bdSPeter Avalos for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p) 1141077d0bdSPeter Avalos NTOHL(**p); 1151077d0bdSPeter Avalos return (bpf_u_int32 **)hp->h_addr_list; 1161077d0bdSPeter Avalos #endif 1171077d0bdSPeter Avalos } 1181077d0bdSPeter Avalos else 1191077d0bdSPeter Avalos return 0; 1201077d0bdSPeter Avalos } 1211077d0bdSPeter Avalos 1221077d0bdSPeter Avalos #ifdef INET6 1231077d0bdSPeter Avalos struct addrinfo * 1241077d0bdSPeter Avalos pcap_nametoaddrinfo(const char *name) 1251077d0bdSPeter Avalos { 1261077d0bdSPeter Avalos struct addrinfo hints, *res; 1271077d0bdSPeter Avalos int error; 1281077d0bdSPeter Avalos 1291077d0bdSPeter Avalos memset(&hints, 0, sizeof(hints)); 1301077d0bdSPeter Avalos hints.ai_family = PF_UNSPEC; 1311077d0bdSPeter Avalos hints.ai_socktype = SOCK_STREAM; /*not really*/ 1321077d0bdSPeter Avalos hints.ai_protocol = IPPROTO_TCP; /*not really*/ 1331077d0bdSPeter Avalos error = getaddrinfo(name, NULL, &hints, &res); 1341077d0bdSPeter Avalos if (error) 1351077d0bdSPeter Avalos return NULL; 1361077d0bdSPeter Avalos else 1371077d0bdSPeter Avalos return res; 1381077d0bdSPeter Avalos } 1391077d0bdSPeter Avalos #endif /*INET6*/ 1401077d0bdSPeter Avalos 1411077d0bdSPeter Avalos /* 1421077d0bdSPeter Avalos * Convert net name to internet address. 1431077d0bdSPeter Avalos * Return 0 upon failure. 1441077d0bdSPeter Avalos */ 1451077d0bdSPeter Avalos bpf_u_int32 1461077d0bdSPeter Avalos pcap_nametonetaddr(const char *name) 1471077d0bdSPeter Avalos { 1481077d0bdSPeter Avalos #ifndef WIN32 1491077d0bdSPeter Avalos struct netent *np; 1501077d0bdSPeter Avalos 1511077d0bdSPeter Avalos if ((np = getnetbyname(name)) != NULL) 1521077d0bdSPeter Avalos return np->n_net; 1531077d0bdSPeter Avalos else 1541077d0bdSPeter Avalos return 0; 1551077d0bdSPeter Avalos #else 1561077d0bdSPeter Avalos /* 1571077d0bdSPeter Avalos * There's no "getnetbyname()" on Windows. 1581077d0bdSPeter Avalos */ 1591077d0bdSPeter Avalos return 0; 1601077d0bdSPeter Avalos #endif 1611077d0bdSPeter Avalos } 1621077d0bdSPeter Avalos 1631077d0bdSPeter Avalos /* 1641077d0bdSPeter Avalos * Convert a port name to its port and protocol numbers. 1651077d0bdSPeter Avalos * We assume only TCP or UDP. 1661077d0bdSPeter Avalos * Return 0 upon failure. 1671077d0bdSPeter Avalos */ 1681077d0bdSPeter Avalos int 1691077d0bdSPeter Avalos pcap_nametoport(const char *name, int *port, int *proto) 1701077d0bdSPeter Avalos { 1711077d0bdSPeter Avalos struct servent *sp; 1721077d0bdSPeter Avalos int tcp_port = -1; 1731077d0bdSPeter Avalos int udp_port = -1; 1741077d0bdSPeter Avalos 1751077d0bdSPeter Avalos /* 1761077d0bdSPeter Avalos * We need to check /etc/services for ambiguous entries. 1771077d0bdSPeter Avalos * If we find the ambiguous entry, and it has the 1781077d0bdSPeter Avalos * same port number, change the proto to PROTO_UNDEF 1791077d0bdSPeter Avalos * so both TCP and UDP will be checked. 1801077d0bdSPeter Avalos */ 1811077d0bdSPeter Avalos sp = getservbyname(name, "tcp"); 1821077d0bdSPeter Avalos if (sp != NULL) tcp_port = ntohs(sp->s_port); 1831077d0bdSPeter Avalos sp = getservbyname(name, "udp"); 1841077d0bdSPeter Avalos if (sp != NULL) udp_port = ntohs(sp->s_port); 1851077d0bdSPeter Avalos if (tcp_port >= 0) { 1861077d0bdSPeter Avalos *port = tcp_port; 1871077d0bdSPeter Avalos *proto = IPPROTO_TCP; 1881077d0bdSPeter Avalos if (udp_port >= 0) { 1891077d0bdSPeter Avalos if (udp_port == tcp_port) 1901077d0bdSPeter Avalos *proto = PROTO_UNDEF; 1911077d0bdSPeter Avalos #ifdef notdef 1921077d0bdSPeter Avalos else 1931077d0bdSPeter Avalos /* Can't handle ambiguous names that refer 1941077d0bdSPeter Avalos to different port numbers. */ 1951077d0bdSPeter Avalos warning("ambiguous port %s in /etc/services", 1961077d0bdSPeter Avalos name); 1971077d0bdSPeter Avalos #endif 1981077d0bdSPeter Avalos } 1991077d0bdSPeter Avalos return 1; 2001077d0bdSPeter Avalos } 2011077d0bdSPeter Avalos if (udp_port >= 0) { 2021077d0bdSPeter Avalos *port = udp_port; 2031077d0bdSPeter Avalos *proto = IPPROTO_UDP; 2041077d0bdSPeter Avalos return 1; 2051077d0bdSPeter Avalos } 2061077d0bdSPeter Avalos #if defined(ultrix) || defined(__osf__) 2071077d0bdSPeter Avalos /* Special hack in case NFS isn't in /etc/services */ 2081077d0bdSPeter Avalos if (strcmp(name, "nfs") == 0) { 2091077d0bdSPeter Avalos *port = 2049; 2101077d0bdSPeter Avalos *proto = PROTO_UNDEF; 2111077d0bdSPeter Avalos return 1; 2121077d0bdSPeter Avalos } 2131077d0bdSPeter Avalos #endif 2141077d0bdSPeter Avalos return 0; 2151077d0bdSPeter Avalos } 2161077d0bdSPeter Avalos 2171077d0bdSPeter Avalos /* 2181077d0bdSPeter Avalos * Convert a string in the form PPP-PPP, where correspond to ports, to 2191077d0bdSPeter Avalos * a starting and ending port in a port range. 2201077d0bdSPeter Avalos * Return 0 on failure. 2211077d0bdSPeter Avalos */ 2221077d0bdSPeter Avalos int 2231077d0bdSPeter Avalos pcap_nametoportrange(const char *name, int *port1, int *port2, int *proto) 2241077d0bdSPeter Avalos { 2251077d0bdSPeter Avalos u_int p1, p2; 2261077d0bdSPeter Avalos char *off, *cpy; 2271077d0bdSPeter Avalos int save_proto; 2281077d0bdSPeter Avalos 2291077d0bdSPeter Avalos if (sscanf(name, "%d-%d", &p1, &p2) != 2) { 2301077d0bdSPeter Avalos if ((cpy = strdup(name)) == NULL) 2311077d0bdSPeter Avalos return 0; 2321077d0bdSPeter Avalos 2331077d0bdSPeter Avalos if ((off = strchr(cpy, '-')) == NULL) { 2341077d0bdSPeter Avalos free(cpy); 2351077d0bdSPeter Avalos return 0; 2361077d0bdSPeter Avalos } 2371077d0bdSPeter Avalos 2381077d0bdSPeter Avalos *off = '\0'; 2391077d0bdSPeter Avalos 2401077d0bdSPeter Avalos if (pcap_nametoport(cpy, port1, proto) == 0) { 2411077d0bdSPeter Avalos free(cpy); 2421077d0bdSPeter Avalos return 0; 2431077d0bdSPeter Avalos } 2441077d0bdSPeter Avalos save_proto = *proto; 2451077d0bdSPeter Avalos 2461077d0bdSPeter Avalos if (pcap_nametoport(off + 1, port2, proto) == 0) { 2471077d0bdSPeter Avalos free(cpy); 2481077d0bdSPeter Avalos return 0; 2491077d0bdSPeter Avalos } 2501077d0bdSPeter Avalos 2511077d0bdSPeter Avalos if (*proto != save_proto) 2521077d0bdSPeter Avalos *proto = PROTO_UNDEF; 2531077d0bdSPeter Avalos } else { 2541077d0bdSPeter Avalos *port1 = p1; 2551077d0bdSPeter Avalos *port2 = p2; 2561077d0bdSPeter Avalos *proto = PROTO_UNDEF; 2571077d0bdSPeter Avalos } 2581077d0bdSPeter Avalos 2591077d0bdSPeter Avalos return 1; 2601077d0bdSPeter Avalos } 2611077d0bdSPeter Avalos 2621077d0bdSPeter Avalos int 2631077d0bdSPeter Avalos pcap_nametoproto(const char *str) 2641077d0bdSPeter Avalos { 2651077d0bdSPeter Avalos struct protoent *p; 2661077d0bdSPeter Avalos 2671077d0bdSPeter Avalos p = getprotobyname(str); 2681077d0bdSPeter Avalos if (p != 0) 2691077d0bdSPeter Avalos return p->p_proto; 2701077d0bdSPeter Avalos else 2711077d0bdSPeter Avalos return PROTO_UNDEF; 2721077d0bdSPeter Avalos } 2731077d0bdSPeter Avalos 2741077d0bdSPeter Avalos #include "ethertype.h" 2751077d0bdSPeter Avalos 2761077d0bdSPeter Avalos struct eproto { 2771077d0bdSPeter Avalos const char *s; 2781077d0bdSPeter Avalos u_short p; 2791077d0bdSPeter Avalos }; 2801077d0bdSPeter Avalos 2811077d0bdSPeter Avalos /* Static data base of ether protocol types. */ 2821077d0bdSPeter Avalos struct eproto eproto_db[] = { 2831077d0bdSPeter Avalos { "pup", ETHERTYPE_PUP }, 2841077d0bdSPeter Avalos { "xns", ETHERTYPE_NS }, 2851077d0bdSPeter Avalos { "ip", ETHERTYPE_IP }, 2861077d0bdSPeter Avalos #ifdef INET6 2871077d0bdSPeter Avalos { "ip6", ETHERTYPE_IPV6 }, 2881077d0bdSPeter Avalos #endif 2891077d0bdSPeter Avalos { "arp", ETHERTYPE_ARP }, 2901077d0bdSPeter Avalos { "rarp", ETHERTYPE_REVARP }, 2911077d0bdSPeter Avalos { "sprite", ETHERTYPE_SPRITE }, 2921077d0bdSPeter Avalos { "mopdl", ETHERTYPE_MOPDL }, 2931077d0bdSPeter Avalos { "moprc", ETHERTYPE_MOPRC }, 2941077d0bdSPeter Avalos { "decnet", ETHERTYPE_DN }, 2951077d0bdSPeter Avalos { "lat", ETHERTYPE_LAT }, 2961077d0bdSPeter Avalos { "sca", ETHERTYPE_SCA }, 2971077d0bdSPeter Avalos { "lanbridge", ETHERTYPE_LANBRIDGE }, 2981077d0bdSPeter Avalos { "vexp", ETHERTYPE_VEXP }, 2991077d0bdSPeter Avalos { "vprod", ETHERTYPE_VPROD }, 3001077d0bdSPeter Avalos { "atalk", ETHERTYPE_ATALK }, 3011077d0bdSPeter Avalos { "atalkarp", ETHERTYPE_AARP }, 3021077d0bdSPeter Avalos { "loopback", ETHERTYPE_LOOPBACK }, 3031077d0bdSPeter Avalos { "decdts", ETHERTYPE_DECDTS }, 3041077d0bdSPeter Avalos { "decdns", ETHERTYPE_DECDNS }, 3051077d0bdSPeter Avalos { (char *)0, 0 } 3061077d0bdSPeter Avalos }; 3071077d0bdSPeter Avalos 3081077d0bdSPeter Avalos int 3091077d0bdSPeter Avalos pcap_nametoeproto(const char *s) 3101077d0bdSPeter Avalos { 3111077d0bdSPeter Avalos struct eproto *p = eproto_db; 3121077d0bdSPeter Avalos 3131077d0bdSPeter Avalos while (p->s != 0) { 3141077d0bdSPeter Avalos if (strcmp(p->s, s) == 0) 3151077d0bdSPeter Avalos return p->p; 3161077d0bdSPeter Avalos p += 1; 3171077d0bdSPeter Avalos } 3181077d0bdSPeter Avalos return PROTO_UNDEF; 3191077d0bdSPeter Avalos } 3201077d0bdSPeter Avalos 3211077d0bdSPeter Avalos #include "llc.h" 3221077d0bdSPeter Avalos 3231077d0bdSPeter Avalos /* Static data base of LLC values. */ 3241077d0bdSPeter Avalos static struct eproto llc_db[] = { 3251077d0bdSPeter Avalos { "iso", LLCSAP_ISONS }, 3261077d0bdSPeter Avalos { "stp", LLCSAP_8021D }, 3271077d0bdSPeter Avalos { "ipx", LLCSAP_IPX }, 3281077d0bdSPeter Avalos { "netbeui", LLCSAP_NETBEUI }, 3291077d0bdSPeter Avalos { (char *)0, 0 } 3301077d0bdSPeter Avalos }; 3311077d0bdSPeter Avalos 3321077d0bdSPeter Avalos int 3331077d0bdSPeter Avalos pcap_nametollc(const char *s) 3341077d0bdSPeter Avalos { 3351077d0bdSPeter Avalos struct eproto *p = llc_db; 3361077d0bdSPeter Avalos 3371077d0bdSPeter Avalos while (p->s != 0) { 3381077d0bdSPeter Avalos if (strcmp(p->s, s) == 0) 3391077d0bdSPeter Avalos return p->p; 3401077d0bdSPeter Avalos p += 1; 3411077d0bdSPeter Avalos } 3421077d0bdSPeter Avalos return PROTO_UNDEF; 3431077d0bdSPeter Avalos } 3441077d0bdSPeter Avalos 3451077d0bdSPeter Avalos /* Hex digit to integer. */ 3461077d0bdSPeter Avalos static inline int 3471077d0bdSPeter Avalos xdtoi(c) 3481077d0bdSPeter Avalos register int c; 3491077d0bdSPeter Avalos { 3501077d0bdSPeter Avalos if (isdigit(c)) 3511077d0bdSPeter Avalos return c - '0'; 3521077d0bdSPeter Avalos else if (islower(c)) 3531077d0bdSPeter Avalos return c - 'a' + 10; 3541077d0bdSPeter Avalos else 3551077d0bdSPeter Avalos return c - 'A' + 10; 3561077d0bdSPeter Avalos } 3571077d0bdSPeter Avalos 3581077d0bdSPeter Avalos int 3591077d0bdSPeter Avalos __pcap_atoin(const char *s, bpf_u_int32 *addr) 3601077d0bdSPeter Avalos { 3611077d0bdSPeter Avalos u_int n; 3621077d0bdSPeter Avalos int len; 3631077d0bdSPeter Avalos 3641077d0bdSPeter Avalos *addr = 0; 3651077d0bdSPeter Avalos len = 0; 3661077d0bdSPeter Avalos while (1) { 3671077d0bdSPeter Avalos n = 0; 3681077d0bdSPeter Avalos while (*s && *s != '.') 3691077d0bdSPeter Avalos n = n * 10 + *s++ - '0'; 3701077d0bdSPeter Avalos *addr <<= 8; 3711077d0bdSPeter Avalos *addr |= n & 0xff; 3721077d0bdSPeter Avalos len += 8; 3731077d0bdSPeter Avalos if (*s == '\0') 3741077d0bdSPeter Avalos return len; 3751077d0bdSPeter Avalos ++s; 3761077d0bdSPeter Avalos } 3771077d0bdSPeter Avalos /* NOTREACHED */ 3781077d0bdSPeter Avalos } 3791077d0bdSPeter Avalos 3801077d0bdSPeter Avalos int 3811077d0bdSPeter Avalos __pcap_atodn(const char *s, bpf_u_int32 *addr) 3821077d0bdSPeter Avalos { 3831077d0bdSPeter Avalos #define AREASHIFT 10 3841077d0bdSPeter Avalos #define AREAMASK 0176000 3851077d0bdSPeter Avalos #define NODEMASK 01777 3861077d0bdSPeter Avalos 3871077d0bdSPeter Avalos u_int node, area; 3881077d0bdSPeter Avalos 3891077d0bdSPeter Avalos if (sscanf(s, "%d.%d", &area, &node) != 2) 3901077d0bdSPeter Avalos bpf_error("malformed decnet address '%s'", s); 3911077d0bdSPeter Avalos 3921077d0bdSPeter Avalos *addr = (area << AREASHIFT) & AREAMASK; 3931077d0bdSPeter Avalos *addr |= (node & NODEMASK); 3941077d0bdSPeter Avalos 3951077d0bdSPeter Avalos return(32); 3961077d0bdSPeter Avalos } 3971077d0bdSPeter Avalos 3981077d0bdSPeter Avalos /* 399de0d3203SPeter Avalos * Convert 's', which can have the one of the forms: 400de0d3203SPeter Avalos * 401de0d3203SPeter Avalos * "xx:xx:xx:xx:xx:xx" 402de0d3203SPeter Avalos * "xx.xx.xx.xx.xx.xx" 403de0d3203SPeter Avalos * "xx-xx-xx-xx-xx-xx" 404de0d3203SPeter Avalos * "xxxx.xxxx.xxxx" 405de0d3203SPeter Avalos * "xxxxxxxxxxxx" 406de0d3203SPeter Avalos * 407de0d3203SPeter Avalos * (or various mixes of ':', '.', and '-') into a new 4081077d0bdSPeter Avalos * ethernet address. Assumes 's' is well formed. 4091077d0bdSPeter Avalos */ 4101077d0bdSPeter Avalos u_char * 4111077d0bdSPeter Avalos pcap_ether_aton(const char *s) 4121077d0bdSPeter Avalos { 4131077d0bdSPeter Avalos register u_char *ep, *e; 4141077d0bdSPeter Avalos register u_int d; 4151077d0bdSPeter Avalos 4161077d0bdSPeter Avalos e = ep = (u_char *)malloc(6); 4171077d0bdSPeter Avalos 4181077d0bdSPeter Avalos while (*s) { 419de0d3203SPeter Avalos if (*s == ':' || *s == '.' || *s == '-') 4201077d0bdSPeter Avalos s += 1; 4211077d0bdSPeter Avalos d = xdtoi(*s++); 4221077d0bdSPeter Avalos if (isxdigit((unsigned char)*s)) { 4231077d0bdSPeter Avalos d <<= 4; 4241077d0bdSPeter Avalos d |= xdtoi(*s++); 4251077d0bdSPeter Avalos } 4261077d0bdSPeter Avalos *ep++ = d; 4271077d0bdSPeter Avalos } 4281077d0bdSPeter Avalos 4291077d0bdSPeter Avalos return (e); 4301077d0bdSPeter Avalos } 4311077d0bdSPeter Avalos 4321077d0bdSPeter Avalos #ifndef HAVE_ETHER_HOSTTON 4331077d0bdSPeter Avalos /* Roll our own */ 4341077d0bdSPeter Avalos u_char * 4351077d0bdSPeter Avalos pcap_ether_hostton(const char *name) 4361077d0bdSPeter Avalos { 4371077d0bdSPeter Avalos register struct pcap_etherent *ep; 4381077d0bdSPeter Avalos register u_char *ap; 4391077d0bdSPeter Avalos static FILE *fp = NULL; 4401077d0bdSPeter Avalos static int init = 0; 4411077d0bdSPeter Avalos 4421077d0bdSPeter Avalos if (!init) { 4431077d0bdSPeter Avalos fp = fopen(PCAP_ETHERS_FILE, "r"); 4441077d0bdSPeter Avalos ++init; 4451077d0bdSPeter Avalos if (fp == NULL) 4461077d0bdSPeter Avalos return (NULL); 4471077d0bdSPeter Avalos } else if (fp == NULL) 4481077d0bdSPeter Avalos return (NULL); 4491077d0bdSPeter Avalos else 4501077d0bdSPeter Avalos rewind(fp); 4511077d0bdSPeter Avalos 4521077d0bdSPeter Avalos while ((ep = pcap_next_etherent(fp)) != NULL) { 4531077d0bdSPeter Avalos if (strcmp(ep->name, name) == 0) { 4541077d0bdSPeter Avalos ap = (u_char *)malloc(6); 4551077d0bdSPeter Avalos if (ap != NULL) { 4561077d0bdSPeter Avalos memcpy(ap, ep->addr, 6); 4571077d0bdSPeter Avalos return (ap); 4581077d0bdSPeter Avalos } 4591077d0bdSPeter Avalos break; 4601077d0bdSPeter Avalos } 4611077d0bdSPeter Avalos } 4621077d0bdSPeter Avalos return (NULL); 4631077d0bdSPeter Avalos } 4641077d0bdSPeter Avalos #else 4651077d0bdSPeter Avalos 4661077d0bdSPeter Avalos #if !defined(HAVE_DECL_ETHER_HOSTTON) || !HAVE_DECL_ETHER_HOSTTON 4671077d0bdSPeter Avalos #ifndef HAVE_STRUCT_ETHER_ADDR 4681077d0bdSPeter Avalos struct ether_addr { 4691077d0bdSPeter Avalos unsigned char ether_addr_octet[6]; 4701077d0bdSPeter Avalos }; 4711077d0bdSPeter Avalos #endif 4721077d0bdSPeter Avalos extern int ether_hostton(const char *, struct ether_addr *); 4731077d0bdSPeter Avalos #endif 4741077d0bdSPeter Avalos 4751077d0bdSPeter Avalos /* Use the os supplied routines */ 4761077d0bdSPeter Avalos u_char * 4771077d0bdSPeter Avalos pcap_ether_hostton(const char *name) 4781077d0bdSPeter Avalos { 4791077d0bdSPeter Avalos register u_char *ap; 4801077d0bdSPeter Avalos u_char a[6]; 4811077d0bdSPeter Avalos 4821077d0bdSPeter Avalos ap = NULL; 4831077d0bdSPeter Avalos if (ether_hostton(name, (struct ether_addr *)a) == 0) { 4841077d0bdSPeter Avalos ap = (u_char *)malloc(6); 4851077d0bdSPeter Avalos if (ap != NULL) 4861077d0bdSPeter Avalos memcpy((char *)ap, (char *)a, 6); 4871077d0bdSPeter Avalos } 4881077d0bdSPeter Avalos return (ap); 4891077d0bdSPeter Avalos } 4901077d0bdSPeter Avalos #endif 4911077d0bdSPeter Avalos 4921077d0bdSPeter Avalos u_short 4931077d0bdSPeter Avalos __pcap_nametodnaddr(const char *name) 4941077d0bdSPeter Avalos { 4951077d0bdSPeter Avalos #ifdef DECNETLIB 4961077d0bdSPeter Avalos struct nodeent *getnodebyname(); 4971077d0bdSPeter Avalos struct nodeent *nep; 4981077d0bdSPeter Avalos unsigned short res; 4991077d0bdSPeter Avalos 5001077d0bdSPeter Avalos nep = getnodebyname(name); 5011077d0bdSPeter Avalos if (nep == ((struct nodeent *)0)) 5021077d0bdSPeter Avalos bpf_error("unknown decnet host name '%s'\n", name); 5031077d0bdSPeter Avalos 5041077d0bdSPeter Avalos memcpy((char *)&res, (char *)nep->n_addr, sizeof(unsigned short)); 5051077d0bdSPeter Avalos return(res); 5061077d0bdSPeter Avalos #else 5071077d0bdSPeter Avalos bpf_error("decnet name support not included, '%s' cannot be translated\n", 5081077d0bdSPeter Avalos name); 5091077d0bdSPeter Avalos return(0); 5101077d0bdSPeter Avalos #endif 5111077d0bdSPeter Avalos } 512