123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785 |
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
- #ifdef DECNETLIB
- #include <sys/types.h>
- #include <netdnet/dnetdb.h>
- #endif
- #ifdef _WIN32
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #ifdef INET6
-
- #include <wspiapi.h>
- #endif
- #else
- #include <sys/param.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/time.h>
- #include <netinet/in.h>
- #ifdef HAVE_ETHER_HOSTTON
- #if defined(NET_ETHERNET_H_DECLARES_ETHER_HOSTTON)
-
- #include <net/ethernet.h>
- #elif defined(NETINET_ETHER_H_DECLARES_ETHER_HOSTTON)
-
- #include <netinet/ether.h>
- #elif defined(SYS_ETHERNET_H_DECLARES_ETHER_HOSTTON)
-
- #include <sys/ethernet.h>
- #elif defined(ARPA_INET_H_DECLARES_ETHER_HOSTTON)
-
- #include <arpa/inet.h>
- #elif defined(NETINET_IF_ETHER_H_DECLARES_ETHER_HOSTTON)
-
- #define NEED_NETINET_IF_ETHER_H
- #else
-
- #ifdef HAVE_STRUCT_ETHER_ADDR
- #define NEED_NETINET_IF_ETHER_H
- #else
- struct ether_addr {
- unsigned char ether_addr_octet[6];
- };
- #endif
- #endif
- #ifdef NEED_NETINET_IF_ETHER_H
- #include <net/if.h> /* Needed on some platforms */
- #include <netinet/in.h> /* Needed on some platforms */
- #include <netinet/if_ether.h>
- #endif
- #ifndef HAVE_DECL_ETHER_HOSTTON
-
- extern int ether_hostton(const char *, struct ether_addr *);
- #endif
- #endif
- #include <arpa/inet.h>
- #include <netdb.h>
- #endif
- #include <ctype.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include "pcap-int.h"
- #include "gencode.h"
- #include <pcap/namedb.h>
- #include "nametoaddr.h"
- #ifdef HAVE_OS_PROTO_H
- #include "os-proto.h"
- #endif
- #ifndef NTOHL
- #define NTOHL(x) (x) = ntohl(x)
- #define NTOHS(x) (x) = ntohs(x)
- #endif
- bpf_u_int32 **
- pcap_nametoaddr(const char *name)
- {
- #ifndef h_addr
- static bpf_u_int32 *hlist[2];
- #endif
- bpf_u_int32 **p;
- struct hostent *hp;
- if ((hp = gethostbyname(name)) != NULL) {
- #ifndef h_addr
- hlist[0] = (bpf_u_int32 *)hp->h_addr;
- NTOHL(hp->h_addr);
- return hlist;
- #else
- for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
- NTOHL(**p);
- return (bpf_u_int32 **)hp->h_addr_list;
- #endif
- }
- else
- return 0;
- }
- struct addrinfo *
- pcap_nametoaddrinfo(const char *name)
- {
- struct addrinfo hints, *res;
- int error;
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = PF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- error = getaddrinfo(name, NULL, &hints, &res);
- if (error)
- return NULL;
- else
- return res;
- }
- bpf_u_int32
- pcap_nametonetaddr(const char *name)
- {
- #ifdef _WIN32
-
- return 0;
- #else
-
- struct netent *np;
- #if defined(HAVE_LINUX_GETNETBYNAME_R)
-
- struct netent result_buf;
- char buf[1024];
- int h_errnoval;
- int err;
- err = getnetbyname_r(name, &result_buf, buf, sizeof buf, &np,
- &h_errnoval);
- if (err != 0) {
-
- return 0;
- }
- #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
-
- struct netent result_buf;
- char buf[1024];
- np = getnetbyname_r(name, &result_buf, buf, (int)sizeof buf);
- #elif defined(HAVE_AIX_GETNETBYNAME_R)
-
- struct netent result_buf;
- struct netent_data net_data;
- if (getnetbyname_r(name, &result_buf, &net_data) == -1)
- np = NULL;
- else
- np = &result_buf;
- #else
-
- np = getnetbyname(name);
- #endif
- if (np != NULL)
- return np->n_net;
- else
- return 0;
- #endif
- }
- int
- pcap_nametoport(const char *name, int *port, int *proto)
- {
- struct addrinfo hints, *res, *ai;
- int error;
- struct sockaddr_in *in4;
- #ifdef INET6
- struct sockaddr_in6 *in6;
- #endif
- int tcp_port = -1;
- int udp_port = -1;
-
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = PF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- error = getaddrinfo(NULL, name, &hints, &res);
- if (error != 0) {
- if (error != EAI_NONAME &&
- error != EAI_SERVICE) {
-
- return 0;
- }
- } else {
-
- for (ai = res; ai != NULL; ai = ai->ai_next) {
-
- if (ai->ai_addr != NULL) {
-
- if (ai->ai_addr->sa_family == AF_INET) {
- in4 = (struct sockaddr_in *)ai->ai_addr;
- tcp_port = ntohs(in4->sin_port);
- break;
- }
- #ifdef INET6
- if (ai->ai_addr->sa_family == AF_INET6) {
- in6 = (struct sockaddr_in6 *)ai->ai_addr;
- tcp_port = ntohs(in6->sin6_port);
- break;
- }
- #endif
- }
- }
- freeaddrinfo(res);
- }
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = PF_UNSPEC;
- hints.ai_socktype = SOCK_DGRAM;
- hints.ai_protocol = IPPROTO_UDP;
- error = getaddrinfo(NULL, name, &hints, &res);
- if (error != 0) {
- if (error != EAI_NONAME &&
- error != EAI_SERVICE) {
-
- return 0;
- }
- } else {
-
- for (ai = res; ai != NULL; ai = ai->ai_next) {
-
- if (ai->ai_addr != NULL) {
-
- if (ai->ai_addr->sa_family == AF_INET) {
- in4 = (struct sockaddr_in *)ai->ai_addr;
- udp_port = ntohs(in4->sin_port);
- break;
- }
- #ifdef INET6
- if (ai->ai_addr->sa_family == AF_INET6) {
- in6 = (struct sockaddr_in6 *)ai->ai_addr;
- udp_port = ntohs(in6->sin6_port);
- break;
- }
- #endif
- }
- }
- freeaddrinfo(res);
- }
-
- if (tcp_port >= 0) {
- *port = tcp_port;
- *proto = IPPROTO_TCP;
- if (udp_port >= 0) {
- if (udp_port == tcp_port)
- *proto = PROTO_UNDEF;
- #ifdef notdef
- else
-
- warning("ambiguous port %s in /etc/services",
- name);
- #endif
- }
- return 1;
- }
- if (udp_port >= 0) {
- *port = udp_port;
- *proto = IPPROTO_UDP;
- return 1;
- }
- #if defined(ultrix) || defined(__osf__)
-
- if (strcmp(name, "nfs") == 0) {
- *port = 2049;
- *proto = PROTO_UNDEF;
- return 1;
- }
- #endif
- return 0;
- }
- int
- pcap_nametoportrange(const char *name, int *port1, int *port2, int *proto)
- {
- u_int p1, p2;
- char *off, *cpy;
- int save_proto;
- if (sscanf(name, "%d-%d", &p1, &p2) != 2) {
- if ((cpy = strdup(name)) == NULL)
- return 0;
- if ((off = strchr(cpy, '-')) == NULL) {
- free(cpy);
- return 0;
- }
- *off = '\0';
- if (pcap_nametoport(cpy, port1, proto) == 0) {
- free(cpy);
- return 0;
- }
- save_proto = *proto;
- if (pcap_nametoport(off + 1, port2, proto) == 0) {
- free(cpy);
- return 0;
- }
- free(cpy);
- if (*proto != save_proto)
- *proto = PROTO_UNDEF;
- } else {
- *port1 = p1;
- *port2 = p2;
- *proto = PROTO_UNDEF;
- }
- return 1;
- }
- int
- pcap_nametoproto(const char *str)
- {
- struct protoent *p;
- #if defined(HAVE_LINUX_GETNETBYNAME_R)
-
- struct protoent result_buf;
- char buf[1024];
- int err;
- err = getprotobyname_r(str, &result_buf, buf, sizeof buf, &p);
- if (err != 0) {
-
- return 0;
- }
- #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
-
- struct protoent result_buf;
- char buf[1024];
- p = getprotobyname_r(str, &result_buf, buf, (int)sizeof buf);
- #elif defined(HAVE_AIX_GETNETBYNAME_R)
-
- struct protoent result_buf;
- struct protoent_data proto_data;
- if (getprotobyname_r(str, &result_buf, &proto_data) == -1)
- p = NULL;
- else
- p = &result_buf;
- #else
-
- p = getprotobyname(str);
- #endif
- if (p != 0)
- return p->p_proto;
- else
- return PROTO_UNDEF;
- }
- #include "ethertype.h"
- struct eproto {
- const char *s;
- u_short p;
- };
- PCAP_API struct eproto eproto_db[];
- PCAP_API_DEF struct eproto eproto_db[] = {
- { "pup", ETHERTYPE_PUP },
- { "xns", ETHERTYPE_NS },
- { "ip", ETHERTYPE_IP },
- #ifdef INET6
- { "ip6", ETHERTYPE_IPV6 },
- #endif
- { "arp", ETHERTYPE_ARP },
- { "rarp", ETHERTYPE_REVARP },
- { "sprite", ETHERTYPE_SPRITE },
- { "mopdl", ETHERTYPE_MOPDL },
- { "moprc", ETHERTYPE_MOPRC },
- { "decnet", ETHERTYPE_DN },
- { "lat", ETHERTYPE_LAT },
- { "sca", ETHERTYPE_SCA },
- { "lanbridge", ETHERTYPE_LANBRIDGE },
- { "vexp", ETHERTYPE_VEXP },
- { "vprod", ETHERTYPE_VPROD },
- { "atalk", ETHERTYPE_ATALK },
- { "atalkarp", ETHERTYPE_AARP },
- { "loopback", ETHERTYPE_LOOPBACK },
- { "decdts", ETHERTYPE_DECDTS },
- { "decdns", ETHERTYPE_DECDNS },
- { (char *)0, 0 }
- };
- int
- pcap_nametoeproto(const char *s)
- {
- struct eproto *p = eproto_db;
- while (p->s != 0) {
- if (strcmp(p->s, s) == 0)
- return p->p;
- p += 1;
- }
- return PROTO_UNDEF;
- }
- #include "llc.h"
- static struct eproto llc_db[] = {
- { "iso", LLCSAP_ISONS },
- { "stp", LLCSAP_8021D },
- { "ipx", LLCSAP_IPX },
- { "netbeui", LLCSAP_NETBEUI },
- { (char *)0, 0 }
- };
- int
- pcap_nametollc(const char *s)
- {
- struct eproto *p = llc_db;
- while (p->s != 0) {
- if (strcmp(p->s, s) == 0)
- return p->p;
- p += 1;
- }
- return PROTO_UNDEF;
- }
- static inline u_char
- xdtoi(u_char c)
- {
- if (isdigit(c))
- return (u_char)(c - '0');
- else if (islower(c))
- return (u_char)(c - 'a' + 10);
- else
- return (u_char)(c - 'A' + 10);
- }
- int
- __pcap_atoin(const char *s, bpf_u_int32 *addr)
- {
- u_int n;
- int len;
- *addr = 0;
- len = 0;
- for (;;) {
- n = 0;
- while (*s && *s != '.')
- n = n * 10 + *s++ - '0';
- *addr <<= 8;
- *addr |= n & 0xff;
- len += 8;
- if (*s == '\0')
- return len;
- ++s;
- }
-
- }
- int
- __pcap_atodn(const char *s, bpf_u_int32 *addr)
- {
- #define AREASHIFT 10
- #define AREAMASK 0176000
- #define NODEMASK 01777
- u_int node, area;
- if (sscanf(s, "%d.%d", &area, &node) != 2)
- return(0);
- *addr = (area << AREASHIFT) & AREAMASK;
- *addr |= (node & NODEMASK);
- return(32);
- }
- u_char *
- pcap_ether_aton(const char *s)
- {
- register u_char *ep, *e;
- register u_char d;
- e = ep = (u_char *)malloc(6);
- if (e == NULL)
- return (NULL);
- while (*s) {
- if (*s == ':' || *s == '.' || *s == '-')
- s += 1;
- d = xdtoi(*s++);
- if (isxdigit((unsigned char)*s)) {
- d <<= 4;
- d |= xdtoi(*s++);
- }
- *ep++ = d;
- }
- return (e);
- }
- #ifndef HAVE_ETHER_HOSTTON
- u_char *
- pcap_ether_hostton(const char *name)
- {
- register struct pcap_etherent *ep;
- register u_char *ap;
- static FILE *fp = NULL;
- static int init = 0;
- if (!init) {
- fp = fopen(PCAP_ETHERS_FILE, "r");
- ++init;
- if (fp == NULL)
- return (NULL);
- } else if (fp == NULL)
- return (NULL);
- else
- rewind(fp);
- while ((ep = pcap_next_etherent(fp)) != NULL) {
- if (strcmp(ep->name, name) == 0) {
- ap = (u_char *)malloc(6);
- if (ap != NULL) {
- memcpy(ap, ep->addr, 6);
- return (ap);
- }
- break;
- }
- }
- return (NULL);
- }
- #else
- u_char *
- pcap_ether_hostton(const char *name)
- {
- register u_char *ap;
- u_char a[6];
- ap = NULL;
- if (ether_hostton(name, (struct ether_addr *)a) == 0) {
- ap = (u_char *)malloc(6);
- if (ap != NULL)
- memcpy((char *)ap, (char *)a, 6);
- }
- return (ap);
- }
- #endif
- int
- #ifdef DECNETLIB
- __pcap_nametodnaddr(const char *name, u_short *res)
- {
- struct nodeent *getnodebyname();
- struct nodeent *nep;
- nep = getnodebyname(name);
- if (nep == ((struct nodeent *)0))
- return(0);
- memcpy((char *)res, (char *)nep->n_addr, sizeof(unsigned short));
- return(1);
- #else
- __pcap_nametodnaddr(const char *name _U_, u_short *res _U_)
- {
- return(0);
- #endif
- }
|