Blame nametoaddr.c

Packit Service c0f7c7
/*
Packit Service c0f7c7
 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
Packit Service c0f7c7
 *	The Regents of the University of California.  All rights reserved.
Packit Service c0f7c7
 *
Packit Service c0f7c7
 * Redistribution and use in source and binary forms, with or without
Packit Service c0f7c7
 * modification, are permitted provided that: (1) source code distributions
Packit Service c0f7c7
 * retain the above copyright notice and this paragraph in its entirety, (2)
Packit Service c0f7c7
 * distributions including binary code include the above copyright notice and
Packit Service c0f7c7
 * this paragraph in its entirety in the documentation or other materials
Packit Service c0f7c7
 * provided with the distribution, and (3) all advertising materials mentioning
Packit Service c0f7c7
 * features or use of this software display the following acknowledgement:
Packit Service c0f7c7
 * ``This product includes software developed by the University of California,
Packit Service c0f7c7
 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
Packit Service c0f7c7
 * the University nor the names of its contributors may be used to endorse
Packit Service c0f7c7
 * or promote products derived from this software without specific prior
Packit Service c0f7c7
 * written permission.
Packit Service c0f7c7
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
Packit Service c0f7c7
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
Packit Service c0f7c7
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Packit Service c0f7c7
 *
Packit Service c0f7c7
 * Name to id translation routines used by the scanner.
Packit Service c0f7c7
 * These functions are not time critical.
Packit Service c0f7c7
 */
Packit Service c0f7c7
Packit Service c0f7c7
#ifdef HAVE_CONFIG_H
Packit Service c0f7c7
#include <config.h>
Packit Service c0f7c7
#endif
Packit Service c0f7c7
Packit Service c0f7c7
#ifdef DECNETLIB
Packit Service c0f7c7
#include <sys/types.h>
Packit Service c0f7c7
#include <netdnet/dnetdb.h>
Packit Service c0f7c7
#endif
Packit Service c0f7c7
Packit Service c0f7c7
#ifdef _WIN32
Packit Service c0f7c7
  #include <winsock2.h>
Packit Service c0f7c7
  #include <ws2tcpip.h>
Packit Service c0f7c7
Packit Service c0f7c7
  #ifdef INET6
Packit Service c0f7c7
    /*
Packit Service c0f7c7
     * To quote the MSDN page for getaddrinfo() at
Packit Service c0f7c7
     *
Packit Service c0f7c7
     *    https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
Packit Service c0f7c7
     *
Packit Service c0f7c7
     * "Support for getaddrinfo on Windows 2000 and older versions
Packit Service c0f7c7
     * The getaddrinfo function was added to the Ws2_32.dll on Windows XP and
Packit Service c0f7c7
     * later. To execute an application that uses this function on earlier
Packit Service c0f7c7
     * versions of Windows, then you need to include the Ws2tcpip.h and
Packit Service c0f7c7
     * Wspiapi.h files. When the Wspiapi.h include file is added, the
Packit Service c0f7c7
     * getaddrinfo function is defined to the WspiapiGetAddrInfo inline
Packit Service c0f7c7
     * function in the Wspiapi.h file. At runtime, the WspiapiGetAddrInfo
Packit Service c0f7c7
     * function is implemented in such a way that if the Ws2_32.dll or the
Packit Service c0f7c7
     * Wship6.dll (the file containing getaddrinfo in the IPv6 Technology
Packit Service c0f7c7
     * Preview for Windows 2000) does not include getaddrinfo, then a
Packit Service c0f7c7
     * version of getaddrinfo is implemented inline based on code in the
Packit Service c0f7c7
     * Wspiapi.h header file. This inline code will be used on older Windows
Packit Service c0f7c7
     * platforms that do not natively support the getaddrinfo function."
Packit Service c0f7c7
     *
Packit Service c0f7c7
     * We use getaddrinfo(), so we include Wspiapi.h here.
Packit Service c0f7c7
     */
Packit Service c0f7c7
    #include <wspiapi.h>
Packit Service c0f7c7
  #endif /* INET6 */
Packit Service c0f7c7
#else /* _WIN32 */
Packit Service c0f7c7
  #include <sys/param.h>
Packit Service c0f7c7
  #include <sys/types.h>
Packit Service c0f7c7
  #include <sys/socket.h>
Packit Service c0f7c7
  #include <sys/time.h>
Packit Service c0f7c7
Packit Service c0f7c7
  #include <netinet/in.h>
Packit Service c0f7c7
Packit Service c0f7c7
  #ifdef HAVE_ETHER_HOSTTON
Packit Service c0f7c7
    #if defined(NET_ETHERNET_H_DECLARES_ETHER_HOSTTON)
Packit Service c0f7c7
      /*
Packit Service c0f7c7
       * OK, just include <net/ethernet.h>.
Packit Service c0f7c7
       */
Packit Service c0f7c7
      #include <net/ethernet.h>
Packit Service c0f7c7
    #elif defined(NETINET_ETHER_H_DECLARES_ETHER_HOSTTON)
Packit Service c0f7c7
      /*
Packit Service c0f7c7
       * OK, just include <netinet/ether.h>
Packit Service c0f7c7
       */
Packit Service c0f7c7
      #include <netinet/ether.h>
Packit Service c0f7c7
    #elif defined(SYS_ETHERNET_H_DECLARES_ETHER_HOSTTON)
Packit Service c0f7c7
      /*
Packit Service c0f7c7
       * OK, just include <sys/ethernet.h>
Packit Service c0f7c7
       */
Packit Service c0f7c7
      #include <sys/ethernet.h>
Packit Service c0f7c7
    #elif defined(ARPA_INET_H_DECLARES_ETHER_HOSTTON)
Packit Service c0f7c7
      /*
Packit Service c0f7c7
       * OK, just include <arpa/inet.h>
Packit Service c0f7c7
       */
Packit Service c0f7c7
      #include <arpa/inet.h>
Packit Service c0f7c7
    #elif defined(NETINET_IF_ETHER_H_DECLARES_ETHER_HOSTTON)
Packit Service c0f7c7
      /*
Packit Service c0f7c7
       * OK, include <netinet/if_ether.h>, after all the other stuff we
Packit Service c0f7c7
       * need to include or define for its benefit.
Packit Service c0f7c7
       */
Packit Service c0f7c7
      #define NEED_NETINET_IF_ETHER_H
Packit Service c0f7c7
    #else
Packit Service c0f7c7
      /*
Packit Service c0f7c7
       * We'll have to declare it ourselves.
Packit Service c0f7c7
       * If <netinet/if_ether.h> defines struct ether_addr, include
Packit Service c0f7c7
       * it.  Otherwise, define it ourselves.
Packit Service c0f7c7
       */
Packit Service c0f7c7
      #ifdef HAVE_STRUCT_ETHER_ADDR
Packit Service c0f7c7
        #define NEED_NETINET_IF_ETHER_H
Packit Service c0f7c7
      #else /* HAVE_STRUCT_ETHER_ADDR */
Packit Service c0f7c7
	struct ether_addr {
Packit Service c0f7c7
		unsigned char ether_addr_octet[6];
Packit Service c0f7c7
	};
Packit Service c0f7c7
      #endif /* HAVE_STRUCT_ETHER_ADDR */
Packit Service c0f7c7
    #endif /* what declares ether_hostton() */
Packit Service c0f7c7
Packit Service c0f7c7
    #ifdef NEED_NETINET_IF_ETHER_H
Packit Service c0f7c7
      #include <net/if.h>	/* Needed on some platforms */
Packit Service c0f7c7
      #include <netinet/in.h>	/* Needed on some platforms */
Packit Service c0f7c7
      #include <netinet/if_ether.h>
Packit Service c0f7c7
    #endif /* NEED_NETINET_IF_ETHER_H */
Packit Service c0f7c7
Packit Service c0f7c7
    #ifndef HAVE_DECL_ETHER_HOSTTON
Packit Service c0f7c7
      /*
Packit Service c0f7c7
       * No header declares it, so declare it ourselves.
Packit Service c0f7c7
       */
Packit Service c0f7c7
      extern int ether_hostton(const char *, struct ether_addr *);
Packit Service c0f7c7
    #endif /* !defined(HAVE_DECL_ETHER_HOSTTON) */
Packit Service c0f7c7
  #endif /* HAVE_ETHER_HOSTTON */
Packit Service c0f7c7
Packit Service c0f7c7
  #include <arpa/inet.h>
Packit Service c0f7c7
  #include <netdb.h>
Packit Service c0f7c7
#endif /* _WIN32 */
Packit Service c0f7c7
Packit Service c0f7c7
#include <ctype.h>
Packit Service c0f7c7
#include <errno.h>
Packit Service c0f7c7
#include <stdlib.h>
Packit Service c0f7c7
#include <string.h>
Packit Service c0f7c7
#include <stdio.h>
Packit Service c0f7c7
Packit Service c0f7c7
#include "pcap-int.h"
Packit Service c0f7c7
Packit Service c0f7c7
#include "gencode.h"
Packit Service c0f7c7
#include <pcap/namedb.h>
Packit Service c0f7c7
#include "nametoaddr.h"
Packit Service c0f7c7
Packit Service c0f7c7
#ifdef HAVE_OS_PROTO_H
Packit Service c0f7c7
#include "os-proto.h"
Packit Service c0f7c7
#endif
Packit Service c0f7c7
Packit Service c0f7c7
#ifndef NTOHL
Packit Service c0f7c7
#define NTOHL(x) (x) = ntohl(x)
Packit Service c0f7c7
#define NTOHS(x) (x) = ntohs(x)
Packit Service c0f7c7
#endif
Packit Service c0f7c7
Packit Service c0f7c7
/*
Packit Service c0f7c7
 *  Convert host name to internet address.
Packit Service c0f7c7
 *  Return 0 upon failure.
Packit Service c0f7c7
 *  XXX - not thread-safe; don't use it inside libpcap.
Packit Service c0f7c7
 */
Packit Service c0f7c7
bpf_u_int32 **
Packit Service c0f7c7
pcap_nametoaddr(const char *name)
Packit Service c0f7c7
{
Packit Service c0f7c7
#ifndef h_addr
Packit Service c0f7c7
	static bpf_u_int32 *hlist[2];
Packit Service c0f7c7
#endif
Packit Service c0f7c7
	bpf_u_int32 **p;
Packit Service c0f7c7
	struct hostent *hp;
Packit Service c0f7c7
Packit Service c0f7c7
	if ((hp = gethostbyname(name)) != NULL) {
Packit Service c0f7c7
#ifndef h_addr
Packit Service c0f7c7
		hlist[0] = (bpf_u_int32 *)hp->h_addr;
Packit Service c0f7c7
		NTOHL(hp->h_addr);
Packit Service c0f7c7
		return hlist;
Packit Service c0f7c7
#else
Packit Service c0f7c7
		for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
Packit Service c0f7c7
			NTOHL(**p);
Packit Service c0f7c7
		return (bpf_u_int32 **)hp->h_addr_list;
Packit Service c0f7c7
#endif
Packit Service c0f7c7
	}
Packit Service c0f7c7
	else
Packit Service c0f7c7
		return 0;
Packit Service c0f7c7
}
Packit Service c0f7c7
Packit Service c0f7c7
struct addrinfo *
Packit Service c0f7c7
pcap_nametoaddrinfo(const char *name)
Packit Service c0f7c7
{
Packit Service c0f7c7
	struct addrinfo hints, *res;
Packit Service c0f7c7
	int error;
Packit Service c0f7c7
Packit Service c0f7c7
	memset(&hints, 0, sizeof(hints));
Packit Service c0f7c7
	hints.ai_family = PF_UNSPEC;
Packit Service c0f7c7
	hints.ai_socktype = SOCK_STREAM;	/*not really*/
Packit Service c0f7c7
	hints.ai_protocol = IPPROTO_TCP;	/*not really*/
Packit Service c0f7c7
	error = getaddrinfo(name, NULL, &hints, &res;;
Packit Service c0f7c7
	if (error)
Packit Service c0f7c7
		return NULL;
Packit Service c0f7c7
	else
Packit Service c0f7c7
		return res;
Packit Service c0f7c7
}
Packit Service c0f7c7
Packit Service c0f7c7
/*
Packit Service c0f7c7
 *  Convert net name to internet address.
Packit Service c0f7c7
 *  Return 0 upon failure.
Packit Service c0f7c7
 *  XXX - not guaranteed to be thread-safe!  See below for platforms
Packit Service c0f7c7
 *  on which it is thread-safe and on which it isn't.
Packit Service c0f7c7
 */
Packit Service c0f7c7
bpf_u_int32
Packit Service c0f7c7
pcap_nametonetaddr(const char *name)
Packit Service c0f7c7
{
Packit Service c0f7c7
#ifdef _WIN32
Packit Service c0f7c7
	/*
Packit Service c0f7c7
	 * There's no "getnetbyname()" on Windows.
Packit Service c0f7c7
	 *
Packit Service c0f7c7
	 * XXX - I guess we could use the BSD code to read
Packit Service c0f7c7
	 * C:\Windows\System32\drivers\etc/networks, assuming
Packit Service c0f7c7
	 * that's its home on all the versions of Windows
Packit Service c0f7c7
	 * we use, but that file probably just has the loopback
Packit Service c0f7c7
	 * network on 127/24 on 99 44/100% of Windows machines.
Packit Service c0f7c7
	 *
Packit Service c0f7c7
	 * (Heck, these days it probably just has that on 99 44/100%
Packit Service c0f7c7
	 * of *UN*X* machines.)
Packit Service c0f7c7
	 */
Packit Service c0f7c7
	return 0;
Packit Service c0f7c7
#else
Packit Service c0f7c7
	/*
Packit Service c0f7c7
	 * UN*X.
Packit Service c0f7c7
	 */
Packit Service c0f7c7
	struct netent *np;
Packit Service c0f7c7
  #if defined(HAVE_LINUX_GETNETBYNAME_R)
Packit Service c0f7c7
	/*
Packit Service c0f7c7
	 * We have Linux's reentrant getnetbyname_r().
Packit Service c0f7c7
	 */
Packit Service c0f7c7
	struct netent result_buf;
Packit Service c0f7c7
	char buf[1024];	/* arbitrary size */
Packit Service c0f7c7
	int h_errnoval;
Packit Service c0f7c7
	int err;
Packit Service c0f7c7
Packit Service c0f7c7
	/*
Packit Service c0f7c7
	 * Apparently, the man page at
Packit Service c0f7c7
	 *
Packit Service c0f7c7
	 *    http://man7.org/linux/man-pages/man3/getnetbyname_r.3.html
Packit Service c0f7c7
	 *
Packit Service c0f7c7
	 * lies when it says
Packit Service c0f7c7
	 *
Packit Service c0f7c7
	 *    If the function call successfully obtains a network record,
Packit Service c0f7c7
	 *    then *result is set pointing to result_buf; otherwise, *result
Packit Service c0f7c7
	 *    is set to NULL.
Packit Service c0f7c7
	 *
Packit Service c0f7c7
	 * and, in fact, at least in some versions of GNU libc, it does
Packit Service c0f7c7
	 * *not* always get set if getnetbyname_r() succeeds.
Packit Service c0f7c7
	 */
Packit Service c0f7c7
	np = NULL;
Packit Service c0f7c7
 	err = getnetbyname_r(name, &result_buf, buf, sizeof buf, &np,
Packit Service c0f7c7
	    &h_errnoval);
Packit Service c0f7c7
	if (err != 0) {
Packit Service c0f7c7
		/*
Packit Service c0f7c7
		 * XXX - dynamically allocate the buffer, and make it
Packit Service c0f7c7
		 * bigger if we get ERANGE back?
Packit Service c0f7c7
		 */
Packit Service c0f7c7
		return 0;
Packit Service c0f7c7
	}
Packit Service c0f7c7
  #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
Packit Service c0f7c7
	/*
Packit Service c0f7c7
	 * We have Solaris's and IRIX's reentrant getnetbyname_r().
Packit Service c0f7c7
	 */
Packit Service c0f7c7
	struct netent result_buf;
Packit Service c0f7c7
	char buf[1024];	/* arbitrary size */
Packit Service c0f7c7
Packit Service c0f7c7
	np = getnetbyname_r(name, &result_buf, buf, (int)sizeof buf);
Packit Service c0f7c7
  #elif defined(HAVE_AIX_GETNETBYNAME_R)
Packit Service c0f7c7
	/*
Packit Service c0f7c7
	 * We have AIX's reentrant getnetbyname_r().
Packit Service c0f7c7
	 */
Packit Service c0f7c7
	struct netent result_buf;
Packit Service c0f7c7
	struct netent_data net_data;
Packit Service c0f7c7
Packit Service c0f7c7
	if (getnetbyname_r(name, &result_buf, &net_data) == -1)
Packit Service c0f7c7
		np = NULL;
Packit Service c0f7c7
	else
Packit Service c0f7c7
		np = &result_buf;
Packit Service c0f7c7
  #else
Packit Service c0f7c7
 	/*
Packit Service c0f7c7
 	 * We don't have any getnetbyname_r(); either we have a
Packit Service c0f7c7
 	 * getnetbyname() that uses thread-specific data, in which
Packit Service c0f7c7
 	 * case we're thread-safe (sufficiently recent FreeBSD,
Packit Service c0f7c7
 	 * sufficiently recent Darwin-based OS, sufficiently recent
Packit Service c0f7c7
 	 * HP-UX, sufficiently recent Tru64 UNIX), or we have the
Packit Service c0f7c7
 	 * traditional getnetbyname() (everything else, including
Packit Service c0f7c7
 	 * current NetBSD and OpenBSD), in which case we're not
Packit Service c0f7c7
 	 * thread-safe.
Packit Service c0f7c7
 	 */
Packit Service c0f7c7
	np = getnetbyname(name);
Packit Service c0f7c7
  #endif
Packit Service c0f7c7
	if (np != NULL)
Packit Service c0f7c7
		return np->n_net;
Packit Service c0f7c7
	else
Packit Service c0f7c7
		return 0;
Packit Service c0f7c7
#endif /* _WIN32 */
Packit Service c0f7c7
}
Packit Service c0f7c7
Packit Service c0f7c7
/*
Packit Service c0f7c7
 * Convert a port name to its port and protocol numbers.
Packit Service c0f7c7
 * We assume only TCP or UDP.
Packit Service c0f7c7
 * Return 0 upon failure.
Packit Service c0f7c7
 */
Packit Service c0f7c7
int
Packit Service c0f7c7
pcap_nametoport(const char *name, int *port, int *proto)
Packit Service c0f7c7
{
Packit Service c0f7c7
	struct addrinfo hints, *res, *ai;
Packit Service c0f7c7
	int error;
Packit Service c0f7c7
	struct sockaddr_in *in4;
Packit Service c0f7c7
#ifdef INET6
Packit Service c0f7c7
	struct sockaddr_in6 *in6;
Packit Service c0f7c7
#endif
Packit Service c0f7c7
	int tcp_port = -1;
Packit Service c0f7c7
	int udp_port = -1;
Packit Service c0f7c7
Packit Service c0f7c7
	/*
Packit Service c0f7c7
	 * We check for both TCP and UDP in case there are
Packit Service c0f7c7
	 * ambiguous entries.
Packit Service c0f7c7
	 */
Packit Service c0f7c7
	memset(&hints, 0, sizeof(hints));
Packit Service c0f7c7
	hints.ai_family = PF_UNSPEC;
Packit Service c0f7c7
	hints.ai_socktype = SOCK_STREAM;
Packit Service c0f7c7
	hints.ai_protocol = IPPROTO_TCP;
Packit Service c0f7c7
	error = getaddrinfo(NULL, name, &hints, &res;;
Packit Service c0f7c7
	if (error != 0) {
Packit Service c0f7c7
		if (error != EAI_NONAME &&
Packit Service c0f7c7
		    error != EAI_SERVICE) {
Packit Service c0f7c7
			/*
Packit Service c0f7c7
			 * This is a real error, not just "there's
Packit Service c0f7c7
			 * no such service name".
Packit Service c0f7c7
			 * XXX - this doesn't return an error string.
Packit Service c0f7c7
			 */
Packit Service c0f7c7
			return 0;
Packit Service c0f7c7
		}
Packit Service c0f7c7
	} else {
Packit Service c0f7c7
		/*
Packit Service c0f7c7
		 * OK, we found it.  Did it find anything?
Packit Service c0f7c7
		 */
Packit Service c0f7c7
		for (ai = res; ai != NULL; ai = ai->ai_next) {
Packit Service c0f7c7
			/*
Packit Service c0f7c7
			 * Does it have an address?
Packit Service c0f7c7
			 */
Packit Service c0f7c7
			if (ai->ai_addr != NULL) {
Packit Service c0f7c7
				/*
Packit Service c0f7c7
				 * Yes.  Get a port number; we're done.
Packit Service c0f7c7
				 */
Packit Service c0f7c7
				if (ai->ai_addr->sa_family == AF_INET) {
Packit Service c0f7c7
					in4 = (struct sockaddr_in *)ai->ai_addr;
Packit Service c0f7c7
					tcp_port = ntohs(in4->sin_port);
Packit Service c0f7c7
					break;
Packit Service c0f7c7
				}
Packit Service c0f7c7
#ifdef INET6
Packit Service c0f7c7
				if (ai->ai_addr->sa_family == AF_INET6) {
Packit Service c0f7c7
					in6 = (struct sockaddr_in6 *)ai->ai_addr;
Packit Service c0f7c7
					tcp_port = ntohs(in6->sin6_port);
Packit Service c0f7c7
					break;
Packit Service c0f7c7
				}
Packit Service c0f7c7
#endif
Packit Service c0f7c7
			}
Packit Service c0f7c7
		}
Packit Service c0f7c7
		freeaddrinfo(res);
Packit Service c0f7c7
	}
Packit Service c0f7c7
Packit Service c0f7c7
	memset(&hints, 0, sizeof(hints));
Packit Service c0f7c7
	hints.ai_family = PF_UNSPEC;
Packit Service c0f7c7
	hints.ai_socktype = SOCK_DGRAM;
Packit Service c0f7c7
	hints.ai_protocol = IPPROTO_UDP;
Packit Service c0f7c7
	error = getaddrinfo(NULL, name, &hints, &res;;
Packit Service c0f7c7
	if (error != 0) {
Packit Service c0f7c7
		if (error != EAI_NONAME &&
Packit Service c0f7c7
		    error != EAI_SERVICE) {
Packit Service c0f7c7
			/*
Packit Service c0f7c7
			 * This is a real error, not just "there's
Packit Service c0f7c7
			 * no such service name".
Packit Service c0f7c7
			 * XXX - this doesn't return an error string.
Packit Service c0f7c7
			 */
Packit Service c0f7c7
			return 0;
Packit Service c0f7c7
		}
Packit Service c0f7c7
	} else {
Packit Service c0f7c7
		/*
Packit Service c0f7c7
		 * OK, we found it.  Did it find anything?
Packit Service c0f7c7
		 */
Packit Service c0f7c7
		for (ai = res; ai != NULL; ai = ai->ai_next) {
Packit Service c0f7c7
			/*
Packit Service c0f7c7
			 * Does it have an address?
Packit Service c0f7c7
			 */
Packit Service c0f7c7
			if (ai->ai_addr != NULL) {
Packit Service c0f7c7
				/*
Packit Service c0f7c7
				 * Yes.  Get a port number; we're done.
Packit Service c0f7c7
				 */
Packit Service c0f7c7
				if (ai->ai_addr->sa_family == AF_INET) {
Packit Service c0f7c7
					in4 = (struct sockaddr_in *)ai->ai_addr;
Packit Service c0f7c7
					udp_port = ntohs(in4->sin_port);
Packit Service c0f7c7
					break;
Packit Service c0f7c7
				}
Packit Service c0f7c7
#ifdef INET6
Packit Service c0f7c7
				if (ai->ai_addr->sa_family == AF_INET6) {
Packit Service c0f7c7
					in6 = (struct sockaddr_in6 *)ai->ai_addr;
Packit Service c0f7c7
					udp_port = ntohs(in6->sin6_port);
Packit Service c0f7c7
					break;
Packit Service c0f7c7
				}
Packit Service c0f7c7
#endif
Packit Service c0f7c7
			}
Packit Service c0f7c7
		}
Packit Service c0f7c7
		freeaddrinfo(res);
Packit Service c0f7c7
	}
Packit Service c0f7c7
Packit Service c0f7c7
	/*
Packit Service c0f7c7
	 * We need to check /etc/services for ambiguous entries.
Packit Service c0f7c7
	 * If we find an ambiguous entry, and it has the
Packit Service c0f7c7
	 * same port number, change the proto to PROTO_UNDEF
Packit Service c0f7c7
	 * so both TCP and UDP will be checked.
Packit Service c0f7c7
	 */
Packit Service c0f7c7
	if (tcp_port >= 0) {
Packit Service c0f7c7
		*port = tcp_port;
Packit Service c0f7c7
		*proto = IPPROTO_TCP;
Packit Service c0f7c7
		if (udp_port >= 0) {
Packit Service c0f7c7
			if (udp_port == tcp_port)
Packit Service c0f7c7
				*proto = PROTO_UNDEF;
Packit Service c0f7c7
#ifdef notdef
Packit Service c0f7c7
			else
Packit Service c0f7c7
				/* Can't handle ambiguous names that refer
Packit Service c0f7c7
				   to different port numbers. */
Packit Service c0f7c7
				warning("ambiguous port %s in /etc/services",
Packit Service c0f7c7
					name);
Packit Service c0f7c7
#endif
Packit Service c0f7c7
		}
Packit Service c0f7c7
		return 1;
Packit Service c0f7c7
	}
Packit Service c0f7c7
	if (udp_port >= 0) {
Packit Service c0f7c7
		*port = udp_port;
Packit Service c0f7c7
		*proto = IPPROTO_UDP;
Packit Service c0f7c7
		return 1;
Packit Service c0f7c7
	}
Packit Service c0f7c7
#if defined(ultrix) || defined(__osf__)
Packit Service c0f7c7
	/* Special hack in case NFS isn't in /etc/services */
Packit Service c0f7c7
	if (strcmp(name, "nfs") == 0) {
Packit Service c0f7c7
		*port = 2049;
Packit Service c0f7c7
		*proto = PROTO_UNDEF;
Packit Service c0f7c7
		return 1;
Packit Service c0f7c7
	}
Packit Service c0f7c7
#endif
Packit Service c0f7c7
	return 0;
Packit Service c0f7c7
}
Packit Service c0f7c7
Packit Service c0f7c7
/*
Packit Service c0f7c7
 * Convert a string in the form PPP-PPP, where correspond to ports, to
Packit Service c0f7c7
 * a starting and ending port in a port range.
Packit Service c0f7c7
 * Return 0 on failure.
Packit Service c0f7c7
 */
Packit Service c0f7c7
int
Packit Service c0f7c7
pcap_nametoportrange(const char *name, int *port1, int *port2, int *proto)
Packit Service c0f7c7
{
Packit Service c0f7c7
	u_int p1, p2;
Packit Service c0f7c7
	char *off, *cpy;
Packit Service c0f7c7
	int save_proto;
Packit Service c0f7c7
Packit Service c0f7c7
	if (sscanf(name, "%d-%d", &p1, &p2) != 2) {
Packit Service c0f7c7
		if ((cpy = strdup(name)) == NULL)
Packit Service c0f7c7
			return 0;
Packit Service c0f7c7
Packit Service c0f7c7
		if ((off = strchr(cpy, '-')) == NULL) {
Packit Service c0f7c7
			free(cpy);
Packit Service c0f7c7
			return 0;
Packit Service c0f7c7
		}
Packit Service c0f7c7
Packit Service c0f7c7
		*off = '\0';
Packit Service c0f7c7
Packit Service c0f7c7
		if (pcap_nametoport(cpy, port1, proto) == 0) {
Packit Service c0f7c7
			free(cpy);
Packit Service c0f7c7
			return 0;
Packit Service c0f7c7
		}
Packit Service c0f7c7
		save_proto = *proto;
Packit Service c0f7c7
Packit Service c0f7c7
		if (pcap_nametoport(off + 1, port2, proto) == 0) {
Packit Service c0f7c7
			free(cpy);
Packit Service c0f7c7
			return 0;
Packit Service c0f7c7
		}
Packit Service c0f7c7
		free(cpy);
Packit Service c0f7c7
Packit Service c0f7c7
		if (*proto != save_proto)
Packit Service c0f7c7
			*proto = PROTO_UNDEF;
Packit Service c0f7c7
	} else {
Packit Service c0f7c7
		*port1 = p1;
Packit Service c0f7c7
		*port2 = p2;
Packit Service c0f7c7
		*proto = PROTO_UNDEF;
Packit Service c0f7c7
	}
Packit Service c0f7c7
Packit Service c0f7c7
	return 1;
Packit Service c0f7c7
}
Packit Service c0f7c7
Packit Service c0f7c7
/*
Packit Service c0f7c7
 * XXX - not guaranteed to be thread-safe!  See below for platforms
Packit Service c0f7c7
 * on which it is thread-safe and on which it isn't.
Packit Service c0f7c7
 */
Packit Service c0f7c7
int
Packit Service c0f7c7
pcap_nametoproto(const char *str)
Packit Service c0f7c7
{
Packit Service c0f7c7
	struct protoent *p;
Packit Service c0f7c7
  #if defined(HAVE_LINUX_GETNETBYNAME_R)
Packit Service c0f7c7
	/*
Packit Service c0f7c7
	 * We have Linux's reentrant getprotobyname_r().
Packit Service c0f7c7
	 */
Packit Service c0f7c7
	struct protoent result_buf;
Packit Service c0f7c7
	char buf[1024];	/* arbitrary size */
Packit Service c0f7c7
	int err;
Packit Service c0f7c7
Packit Service c0f7c7
	err = getprotobyname_r(str, &result_buf, buf, sizeof buf, &p);
Packit Service c0f7c7
	if (err != 0) {
Packit Service c0f7c7
		/*
Packit Service c0f7c7
		 * XXX - dynamically allocate the buffer, and make it
Packit Service c0f7c7
		 * bigger if we get ERANGE back?
Packit Service c0f7c7
		 */
Packit Service c0f7c7
		return 0;
Packit Service c0f7c7
	}
Packit Service c0f7c7
  #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
Packit Service c0f7c7
	/*
Packit Service c0f7c7
	 * We have Solaris's and IRIX's reentrant getprotobyname_r().
Packit Service c0f7c7
	 */
Packit Service c0f7c7
	struct protoent result_buf;
Packit Service c0f7c7
	char buf[1024];	/* arbitrary size */
Packit Service c0f7c7
Packit Service c0f7c7
	p = getprotobyname_r(str, &result_buf, buf, (int)sizeof buf);
Packit Service c0f7c7
  #elif defined(HAVE_AIX_GETNETBYNAME_R)
Packit Service c0f7c7
	/*
Packit Service c0f7c7
	 * We have AIX's reentrant getprotobyname_r().
Packit Service c0f7c7
	 */
Packit Service c0f7c7
	struct protoent result_buf;
Packit Service c0f7c7
	struct protoent_data proto_data;
Packit Service c0f7c7
Packit Service c0f7c7
	if (getprotobyname_r(str, &result_buf, &proto_data) == -1)
Packit Service c0f7c7
		p = NULL;
Packit Service c0f7c7
	else
Packit Service c0f7c7
		p = &result_buf;
Packit Service c0f7c7
  #else
Packit Service c0f7c7
 	/*
Packit Service c0f7c7
 	 * We don't have any getprotobyname_r(); either we have a
Packit Service c0f7c7
 	 * getprotobyname() that uses thread-specific data, in which
Packit Service c0f7c7
 	 * case we're thread-safe (sufficiently recent FreeBSD,
Packit Service c0f7c7
 	 * sufficiently recent Darwin-based OS, sufficiently recent
Packit Service c0f7c7
 	 * HP-UX, sufficiently recent Tru64 UNIX, Windows), or we have
Packit Service c0f7c7
	 * the traditional getprotobyname() (everything else, including
Packit Service c0f7c7
 	 * current NetBSD and OpenBSD), in which case we're not
Packit Service c0f7c7
 	 * thread-safe.
Packit Service c0f7c7
 	 */
Packit Service c0f7c7
	p = getprotobyname(str);
Packit Service c0f7c7
  #endif
Packit Service c0f7c7
	if (p != 0)
Packit Service c0f7c7
		return p->p_proto;
Packit Service c0f7c7
	else
Packit Service c0f7c7
		return PROTO_UNDEF;
Packit Service c0f7c7
}
Packit Service c0f7c7
Packit Service c0f7c7
#include "ethertype.h"
Packit Service c0f7c7
Packit Service c0f7c7
struct eproto {
Packit Service c0f7c7
	const char *s;
Packit Service c0f7c7
	u_short p;
Packit Service c0f7c7
};
Packit Service c0f7c7
Packit Service c0f7c7
/*
Packit Service c0f7c7
 * Static data base of ether protocol types.
Packit Service c0f7c7
 * tcpdump used to import this, and it's declared as an export on
Packit Service c0f7c7
 * Debian, at least, so make it a public symbol, even though we
Packit Service c0f7c7
 * don't officially export it by declaring it in a header file.
Packit Service c0f7c7
 * (Programs *should* do this themselves, as tcpdump now does.)
Packit Service c0f7c7
 *
Packit Service c0f7c7
 * We declare it here, right before defining it, to squelch any
Packit Service c0f7c7
 * warnings we might get from compilers about the lack of a
Packit Service c0f7c7
 * declaration.
Packit Service c0f7c7
 */
Packit Service c0f7c7
PCAP_API struct eproto eproto_db[];
Packit Service c0f7c7
PCAP_API_DEF struct eproto eproto_db[] = {
Packit Service c0f7c7
	{ "pup", ETHERTYPE_PUP },
Packit Service c0f7c7
	{ "xns", ETHERTYPE_NS },
Packit Service c0f7c7
	{ "ip", ETHERTYPE_IP },
Packit Service c0f7c7
#ifdef INET6
Packit Service c0f7c7
	{ "ip6", ETHERTYPE_IPV6 },
Packit Service c0f7c7
#endif
Packit Service c0f7c7
	{ "arp", ETHERTYPE_ARP },
Packit Service c0f7c7
	{ "rarp", ETHERTYPE_REVARP },
Packit Service c0f7c7
	{ "sprite", ETHERTYPE_SPRITE },
Packit Service c0f7c7
	{ "mopdl", ETHERTYPE_MOPDL },
Packit Service c0f7c7
	{ "moprc", ETHERTYPE_MOPRC },
Packit Service c0f7c7
	{ "decnet", ETHERTYPE_DN },
Packit Service c0f7c7
	{ "lat", ETHERTYPE_LAT },
Packit Service c0f7c7
	{ "sca", ETHERTYPE_SCA },
Packit Service c0f7c7
	{ "lanbridge", ETHERTYPE_LANBRIDGE },
Packit Service c0f7c7
	{ "vexp", ETHERTYPE_VEXP },
Packit Service c0f7c7
	{ "vprod", ETHERTYPE_VPROD },
Packit Service c0f7c7
	{ "atalk", ETHERTYPE_ATALK },
Packit Service c0f7c7
	{ "atalkarp", ETHERTYPE_AARP },
Packit Service c0f7c7
	{ "loopback", ETHERTYPE_LOOPBACK },
Packit Service c0f7c7
	{ "decdts", ETHERTYPE_DECDTS },
Packit Service c0f7c7
	{ "decdns", ETHERTYPE_DECDNS },
Packit Service c0f7c7
	{ (char *)0, 0 }
Packit Service c0f7c7
};
Packit Service c0f7c7
Packit Service c0f7c7
int
Packit Service c0f7c7
pcap_nametoeproto(const char *s)
Packit Service c0f7c7
{
Packit Service c0f7c7
	struct eproto *p = eproto_db;
Packit Service c0f7c7
Packit Service c0f7c7
	while (p->s != 0) {
Packit Service c0f7c7
		if (strcmp(p->s, s) == 0)
Packit Service c0f7c7
			return p->p;
Packit Service c0f7c7
		p += 1;
Packit Service c0f7c7
	}
Packit Service c0f7c7
	return PROTO_UNDEF;
Packit Service c0f7c7
}
Packit Service c0f7c7
Packit Service c0f7c7
#include "llc.h"
Packit Service c0f7c7
Packit Service c0f7c7
/* Static data base of LLC values. */
Packit Service c0f7c7
static struct eproto llc_db[] = {
Packit Service c0f7c7
	{ "iso", LLCSAP_ISONS },
Packit Service c0f7c7
	{ "stp", LLCSAP_8021D },
Packit Service c0f7c7
	{ "ipx", LLCSAP_IPX },
Packit Service c0f7c7
	{ "netbeui", LLCSAP_NETBEUI },
Packit Service c0f7c7
	{ (char *)0, 0 }
Packit Service c0f7c7
};
Packit Service c0f7c7
Packit Service c0f7c7
int
Packit Service c0f7c7
pcap_nametollc(const char *s)
Packit Service c0f7c7
{
Packit Service c0f7c7
	struct eproto *p = llc_db;
Packit Service c0f7c7
Packit Service c0f7c7
	while (p->s != 0) {
Packit Service c0f7c7
		if (strcmp(p->s, s) == 0)
Packit Service c0f7c7
			return p->p;
Packit Service c0f7c7
		p += 1;
Packit Service c0f7c7
	}
Packit Service c0f7c7
	return PROTO_UNDEF;
Packit Service c0f7c7
}
Packit Service c0f7c7
Packit Service c0f7c7
/* Hex digit to 8-bit unsigned integer. */
Packit Service c0f7c7
static inline u_char
Packit Service c0f7c7
xdtoi(u_char c)
Packit Service c0f7c7
{
Packit Service c0f7c7
	if (isdigit(c))
Packit Service c0f7c7
		return (u_char)(c - '0');
Packit Service c0f7c7
	else if (islower(c))
Packit Service c0f7c7
		return (u_char)(c - 'a' + 10);
Packit Service c0f7c7
	else
Packit Service c0f7c7
		return (u_char)(c - 'A' + 10);
Packit Service c0f7c7
}
Packit Service c0f7c7
Packit Service c0f7c7
int
Packit Service c0f7c7
__pcap_atoin(const char *s, bpf_u_int32 *addr)
Packit Service c0f7c7
{
Packit Service c0f7c7
	u_int n;
Packit Service c0f7c7
	int len;
Packit Service c0f7c7
Packit Service c0f7c7
	*addr = 0;
Packit Service c0f7c7
	len = 0;
Packit Service c0f7c7
	for (;;) {
Packit Service c0f7c7
		n = 0;
rpm-build 974280
		while (*s && *s != '.') {
rpm-build 974280
			if (n > 25) {
rpm-build 974280
				/* The result will be > 255 */
rpm-build 974280
				return -1;
rpm-build 974280
			}
Packit Service c0f7c7
			n = n * 10 + *s++ - '0';
rpm-build 974280
		}
rpm-build 974280
		if (n > 255)
rpm-build 974280
			return -1;
Packit Service c0f7c7
		*addr <<= 8;
Packit Service c0f7c7
		*addr |= n & 0xff;
Packit Service c0f7c7
		len += 8;
Packit Service c0f7c7
		if (*s == '\0')
Packit Service c0f7c7
			return len;
Packit Service c0f7c7
		++s;
Packit Service c0f7c7
	}
Packit Service c0f7c7
	/* NOTREACHED */
Packit Service c0f7c7
}
Packit Service c0f7c7
Packit Service c0f7c7
int
Packit Service c0f7c7
__pcap_atodn(const char *s, bpf_u_int32 *addr)
Packit Service c0f7c7
{
Packit Service c0f7c7
#define AREASHIFT 10
Packit Service c0f7c7
#define AREAMASK 0176000
Packit Service c0f7c7
#define NODEMASK 01777
Packit Service c0f7c7
Packit Service c0f7c7
	u_int node, area;
Packit Service c0f7c7
Packit Service c0f7c7
	if (sscanf(s, "%d.%d", &area, &node) != 2)
Packit Service c0f7c7
		return(0);
Packit Service c0f7c7
Packit Service c0f7c7
	*addr = (area << AREASHIFT) & AREAMASK;
Packit Service c0f7c7
	*addr |= (node & NODEMASK);
Packit Service c0f7c7
Packit Service c0f7c7
	return(32);
Packit Service c0f7c7
}
Packit Service c0f7c7
Packit Service c0f7c7
/*
Packit Service c0f7c7
 * Convert 's', which can have the one of the forms:
Packit Service c0f7c7
 *
Packit Service c0f7c7
 *	"xx:xx:xx:xx:xx:xx"
Packit Service c0f7c7
 *	"xx.xx.xx.xx.xx.xx"
Packit Service c0f7c7
 *	"xx-xx-xx-xx-xx-xx"
Packit Service c0f7c7
 *	"xxxx.xxxx.xxxx"
Packit Service c0f7c7
 *	"xxxxxxxxxxxx"
Packit Service c0f7c7
 *
Packit Service c0f7c7
 * (or various mixes of ':', '.', and '-') into a new
Packit Service c0f7c7
 * ethernet address.  Assumes 's' is well formed.
Packit Service c0f7c7
 */
Packit Service c0f7c7
u_char *
Packit Service c0f7c7
pcap_ether_aton(const char *s)
Packit Service c0f7c7
{
Packit Service c0f7c7
	register u_char *ep, *e;
Packit Service c0f7c7
	register u_char d;
Packit Service c0f7c7
Packit Service c0f7c7
	e = ep = (u_char *)malloc(6);
Packit Service c0f7c7
	if (e == NULL)
Packit Service c0f7c7
		return (NULL);
Packit Service c0f7c7
Packit Service c0f7c7
	while (*s) {
Packit Service c0f7c7
		if (*s == ':' || *s == '.' || *s == '-')
Packit Service c0f7c7
			s += 1;
Packit Service c0f7c7
		d = xdtoi(*s++);
Packit Service c0f7c7
		if (isxdigit((unsigned char)*s)) {
Packit Service c0f7c7
			d <<= 4;
Packit Service c0f7c7
			d |= xdtoi(*s++);
Packit Service c0f7c7
		}
Packit Service c0f7c7
		*ep++ = d;
Packit Service c0f7c7
	}
Packit Service c0f7c7
Packit Service c0f7c7
	return (e);
Packit Service c0f7c7
}
Packit Service c0f7c7
Packit Service c0f7c7
#ifndef HAVE_ETHER_HOSTTON
Packit Service c0f7c7
/*
Packit Service c0f7c7
 * Roll our own.
Packit Service c0f7c7
 * XXX - not thread-safe, because pcap_next_etherent() isn't thread-
Packit Service c0f7c7
 * safe!  Needs a mutex or a thread-safe pcap_next_etherent().
Packit Service c0f7c7
 */
Packit Service c0f7c7
u_char *
Packit Service c0f7c7
pcap_ether_hostton(const char *name)
Packit Service c0f7c7
{
Packit Service c0f7c7
	register struct pcap_etherent *ep;
Packit Service c0f7c7
	register u_char *ap;
Packit Service c0f7c7
	static FILE *fp = NULL;
Packit Service c0f7c7
	static int init = 0;
Packit Service c0f7c7
Packit Service c0f7c7
	if (!init) {
Packit Service c0f7c7
		fp = fopen(PCAP_ETHERS_FILE, "r");
Packit Service c0f7c7
		++init;
Packit Service c0f7c7
		if (fp == NULL)
Packit Service c0f7c7
			return (NULL);
Packit Service c0f7c7
	} else if (fp == NULL)
Packit Service c0f7c7
		return (NULL);
Packit Service c0f7c7
	else
Packit Service c0f7c7
		rewind(fp);
Packit Service c0f7c7
Packit Service c0f7c7
	while ((ep = pcap_next_etherent(fp)) != NULL) {
Packit Service c0f7c7
		if (strcmp(ep->name, name) == 0) {
Packit Service c0f7c7
			ap = (u_char *)malloc(6);
Packit Service c0f7c7
			if (ap != NULL) {
Packit Service c0f7c7
				memcpy(ap, ep->addr, 6);
Packit Service c0f7c7
				return (ap);
Packit Service c0f7c7
			}
Packit Service c0f7c7
			break;
Packit Service c0f7c7
		}
Packit Service c0f7c7
	}
Packit Service c0f7c7
	return (NULL);
Packit Service c0f7c7
}
Packit Service c0f7c7
#else
Packit Service c0f7c7
/*
Packit Service c0f7c7
 * Use the OS-supplied routine.
Packit Service c0f7c7
 * This *should* be thread-safe; the API doesn't have a static buffer.
Packit Service c0f7c7
 */
Packit Service c0f7c7
u_char *
Packit Service c0f7c7
pcap_ether_hostton(const char *name)
Packit Service c0f7c7
{
Packit Service c0f7c7
	register u_char *ap;
Packit Service c0f7c7
	u_char a[6];
Packit Service c0f7c7
Packit Service c0f7c7
	ap = NULL;
Packit Service c0f7c7
	if (ether_hostton(name, (struct ether_addr *)a) == 0) {
Packit Service c0f7c7
		ap = (u_char *)malloc(6);
Packit Service c0f7c7
		if (ap != NULL)
Packit Service c0f7c7
			memcpy((char *)ap, (char *)a, 6);
Packit Service c0f7c7
	}
Packit Service c0f7c7
	return (ap);
Packit Service c0f7c7
}
Packit Service c0f7c7
#endif
Packit Service c0f7c7
Packit Service c0f7c7
/*
Packit Service c0f7c7
 * XXX - not guaranteed to be thread-safe!
Packit Service c0f7c7
 */
Packit Service c0f7c7
int
Packit Service c0f7c7
#ifdef	DECNETLIB
Packit Service c0f7c7
__pcap_nametodnaddr(const char *name, u_short *res)
Packit Service c0f7c7
{
Packit Service c0f7c7
	struct nodeent *getnodebyname();
Packit Service c0f7c7
	struct nodeent *nep;
Packit Service c0f7c7
Packit Service c0f7c7
	nep = getnodebyname(name);
Packit Service c0f7c7
	if (nep == ((struct nodeent *)0))
Packit Service c0f7c7
		return(0);
Packit Service c0f7c7
Packit Service c0f7c7
	memcpy((char *)res, (char *)nep->n_addr, sizeof(unsigned short));
Packit Service c0f7c7
	return(1);
Packit Service c0f7c7
#else
Packit Service c0f7c7
__pcap_nametodnaddr(const char *name _U_, u_short *res _U_)
Packit Service c0f7c7
{
Packit Service c0f7c7
	return(0);
Packit Service c0f7c7
#endif
Packit Service c0f7c7
}