Blame sockutils.c

Packit 209cc3
/*
Packit 209cc3
 * Copyright (c) 2002 - 2003
Packit 209cc3
 * NetGroup, Politecnico di Torino (Italy)
Packit 209cc3
 * All rights reserved.
Packit 209cc3
 *
Packit 209cc3
 * Redistribution and use in source and binary forms, with or without
Packit 209cc3
 * modification, are permitted provided that the following conditions
Packit 209cc3
 * are met:
Packit 209cc3
 *
Packit 209cc3
 * 1. Redistributions of source code must retain the above copyright
Packit 209cc3
 * notice, this list of conditions and the following disclaimer.
Packit 209cc3
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 209cc3
 * notice, this list of conditions and the following disclaimer in the
Packit 209cc3
 * documentation and/or other materials provided with the distribution.
Packit 209cc3
 * 3. Neither the name of the Politecnico di Torino nor the names of its
Packit 209cc3
 * contributors may be used to endorse or promote products derived from
Packit 209cc3
 * this software without specific prior written permission.
Packit 209cc3
 *
Packit 209cc3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 209cc3
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 209cc3
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 209cc3
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 209cc3
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 209cc3
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 209cc3
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 209cc3
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 209cc3
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 209cc3
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 209cc3
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 209cc3
 *
Packit 209cc3
 */
Packit 209cc3
Packit 209cc3
#ifdef HAVE_CONFIG_H
Packit 209cc3
#include <config.h>
Packit 209cc3
#endif
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * \file sockutils.c
Packit 209cc3
 *
Packit 209cc3
 * The goal of this file is to provide a common set of primitives for socket
Packit 209cc3
 * manipulation.
Packit 209cc3
 *
Packit 209cc3
 * Although the socket interface defined in the RFC 2553 (and its updates)
Packit 209cc3
 * is excellent, there are still differences between the behavior of those
Packit 209cc3
 * routines on UN*X and Windows, and between UN*Xes.
Packit 209cc3
 *
Packit 209cc3
 * These calls provide an interface similar to the socket interface, but
Packit 209cc3
 * that hides the differences between operating systems.  It does not
Packit 209cc3
 * attempt to significantly improve on the socket interface in other
Packit 209cc3
 * ways.
Packit 209cc3
 */
Packit 209cc3
Packit 209cc3
#include "ftmacros.h"
Packit 209cc3
Packit 209cc3
#include <string.h>
Packit 209cc3
#include <errno.h>	/* for the errno variable */
Packit 209cc3
#include <stdio.h>	/* for the stderr file */
Packit 209cc3
#include <stdlib.h>	/* for malloc() and free() */
Packit 209cc3
#ifdef HAVE_LIMITS_H
Packit 209cc3
#include <limits.h>
Packit 209cc3
#else
Packit 209cc3
#define INT_MAX		2147483647
Packit 209cc3
#endif
Packit 209cc3
Packit 209cc3
#include "pcap-int.h"
Packit 209cc3
Packit 209cc3
#include "sockutils.h"
Packit 209cc3
#include "portability.h"
Packit 209cc3
Packit 209cc3
#ifdef _WIN32
Packit 209cc3
  /*
Packit 209cc3
   * Winsock initialization.
Packit 209cc3
   *
Packit 209cc3
   * Ask for WinSock 2.2.
Packit 209cc3
   */
Packit 209cc3
  #define WINSOCK_MAJOR_VERSION 2
Packit 209cc3
  #define WINSOCK_MINOR_VERSION 2
Packit 209cc3
Packit 209cc3
  static int sockcount = 0;	/*!< Variable that allows calling the WSAStartup() only one time */
Packit 209cc3
#endif
Packit 209cc3
Packit 209cc3
/* Some minor differences between UNIX and Win32 */
Packit 209cc3
#ifdef _WIN32
Packit 209cc3
  #define SHUT_WR SD_SEND	/* The control code for shutdown() is different in Win32 */
Packit 209cc3
#endif
Packit 209cc3
Packit 209cc3
/* Size of the buffer that has to keep error messages */
Packit 209cc3
#define SOCK_ERRBUF_SIZE 1024
Packit 209cc3
Packit 209cc3
/* Constants; used in order to keep strings here */
Packit 209cc3
#define SOCKET_NO_NAME_AVAILABLE "No name available"
Packit 209cc3
#define SOCKET_NO_PORT_AVAILABLE "No port available"
Packit 209cc3
#define SOCKET_NAME_NULL_DAD "Null address (possibly DAD Phase)"
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * On UN*X, send() and recv() return ssize_t.
Packit 209cc3
 *
Packit 209cc3
 * On Windows, send() and recv() return an int.
Packit 209cc3
 *
Packit 209cc3
 *   Wth MSVC, there *is* no ssize_t.
Packit 209cc3
 *
Packit 209cc3
 *   With MinGW, there is an ssize_t type; it is either an int (32 bit)
Packit 209cc3
 *   or a long long (64 bit).
Packit 209cc3
 *
Packit 209cc3
 * So, on Windows, if we don't have ssize_t defined, define it as an
Packit 209cc3
 * int, so we can use it, on all platforms, as the type of variables
Packit 209cc3
 * that hold the return values from send() and recv().
Packit 209cc3
 */
Packit 209cc3
#if defined(_WIN32) && !defined(_SSIZE_T_DEFINED)
Packit 209cc3
typedef int ssize_t;
Packit 209cc3
#endif
Packit 209cc3
Packit 209cc3
/****************************************************
Packit 209cc3
 *                                                  *
Packit 209cc3
 * Locally defined functions                        *
Packit 209cc3
 *                                                  *
Packit 209cc3
 ****************************************************/
Packit 209cc3
Packit 209cc3
static int sock_ismcastaddr(const struct sockaddr *saddr);
Packit 209cc3
Packit 209cc3
/****************************************************
Packit 209cc3
 *                                                  *
Packit 209cc3
 * Function bodies                                  *
Packit 209cc3
 *                                                  *
Packit 209cc3
 ****************************************************/
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * Format an error message given an errno value (UN*X) or a WinSock error
Packit 209cc3
 * (Windows).
Packit 209cc3
 */
Packit 209cc3
void sock_fmterror(const char *caller, int errcode, char *errbuf, int errbuflen)
Packit 209cc3
{
Packit 209cc3
	if (errbuf == NULL)
Packit 209cc3
		return;
Packit 209cc3
Packit 209cc3
#ifdef _WIN32
Packit 209cc3
	pcap_fmt_errmsg_for_win32_err(errbuf, errbuflen, errcode,
Packit 209cc3
	    "%s", caller);
Packit 209cc3
#else
Packit 209cc3
	pcap_fmt_errmsg_for_errno(errbuf, errbuflen, errcode,
Packit 209cc3
	    "%s", caller);
Packit 209cc3
#endif
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * \brief It retrieves the error message after an error occurred in the socket interface.
Packit 209cc3
 *
Packit 209cc3
 * This function is defined because of the different way errors are returned in UNIX
Packit 209cc3
 * and Win32. This function provides a consistent way to retrieve the error message
Packit 209cc3
 * (after a socket error occurred) on all the platforms.
Packit 209cc3
 *
Packit 209cc3
 * \param caller: a pointer to a user-allocated string which contains a message that has
Packit 209cc3
 * to be printed *before* the true error message. It could be, for example, 'this error
Packit 209cc3
 * comes from the recv() call at line 31'.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
Packit 209cc3
 * error message. This buffer has to be at least 'errbuflen' in length.
Packit 209cc3
 * It can be NULL; in this case the error cannot be printed.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
Packit 209cc3
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
Packit 209cc3
 *
Packit 209cc3
 * \return No return values. The error message is returned in the 'string' parameter.
Packit 209cc3
 */
Packit 209cc3
void sock_geterror(const char *caller, char *errbuf, int errbuflen)
Packit 209cc3
{
Packit 209cc3
#ifdef _WIN32
Packit 209cc3
	sock_fmterror(caller, GetLastError(), errbuf, errbuflen);
Packit 209cc3
#else
Packit 209cc3
	sock_fmterror(caller, errno, errbuf, errbuflen);
Packit 209cc3
#endif
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * \brief This function initializes the socket mechanism if it hasn't
Packit 209cc3
 * already been initialized or reinitializes it after it has been
Packit 209cc3
 * cleaned up.
Packit 209cc3
 *
Packit 209cc3
 * On UN*Xes, it doesn't need to do anything; on Windows, it needs to
Packit 209cc3
 * initialize Winsock.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuf: a pointer to an user-allocated buffer that will contain
Packit 209cc3
 * the complete error message. This buffer has to be at least 'errbuflen'
Packit 209cc3
 * in length. It can be NULL; in this case no error message is supplied.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuflen: length of the buffer that will contains the error.
Packit 209cc3
 * The error message cannot be larger than 'errbuflen - 1' because the
Packit 209cc3
 * last char is reserved for the string terminator.
Packit 209cc3
 *
Packit 209cc3
 * \return '0' if everything is fine, '-1' if some errors occurred. The
Packit 209cc3
 * error message is returned in the buffer pointed to by 'errbuf' variable.
Packit 209cc3
 */
Packit 209cc3
#ifdef _WIN32
Packit 209cc3
int sock_init(char *errbuf, int errbuflen)
Packit 209cc3
{
Packit 209cc3
	if (sockcount == 0)
Packit 209cc3
	{
Packit 209cc3
		WSADATA wsaData;			/* helper variable needed to initialize Winsock */
Packit 209cc3
Packit 209cc3
		if (WSAStartup(MAKEWORD(WINSOCK_MAJOR_VERSION,
Packit 209cc3
		    WINSOCK_MINOR_VERSION), &wsaData) != 0)
Packit 209cc3
		{
Packit 209cc3
			if (errbuf)
Packit 209cc3
				pcap_snprintf(errbuf, errbuflen, "Failed to initialize Winsock\n");
Packit 209cc3
Packit 209cc3
			WSACleanup();
Packit 209cc3
Packit 209cc3
			return -1;
Packit 209cc3
		}
Packit 209cc3
	}
Packit 209cc3
Packit 209cc3
	sockcount++;
Packit 209cc3
	return 0;
Packit 209cc3
}
Packit 209cc3
#else
Packit 209cc3
int sock_init(char *errbuf _U_, int errbuflen _U_)
Packit 209cc3
{
Packit 209cc3
	/*
Packit 209cc3
	 * Nothing to do on UN*Xes.
Packit 209cc3
	 */
Packit 209cc3
	return 0;
Packit 209cc3
}
Packit 209cc3
#endif
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * \brief This function cleans up the socket mechanism if we have no
Packit 209cc3
 * sockets left open.
Packit 209cc3
 *
Packit 209cc3
 * On UN*Xes, it doesn't need to do anything; on Windows, it needs
Packit 209cc3
 * to clean up Winsock.
Packit 209cc3
 *
Packit 209cc3
 * \return No error values.
Packit 209cc3
 */
Packit 209cc3
void sock_cleanup(void)
Packit 209cc3
{
Packit 209cc3
#ifdef _WIN32
Packit 209cc3
	sockcount--;
Packit 209cc3
Packit 209cc3
	if (sockcount == 0)
Packit 209cc3
		WSACleanup();
Packit 209cc3
#endif
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * \brief It checks if the sockaddr variable contains a multicast address.
Packit 209cc3
 *
Packit 209cc3
 * \return '0' if the address is multicast, '-1' if it is not.
Packit 209cc3
 */
Packit 209cc3
static int sock_ismcastaddr(const struct sockaddr *saddr)
Packit 209cc3
{
Packit 209cc3
	if (saddr->sa_family == PF_INET)
Packit 209cc3
	{
Packit 209cc3
		struct sockaddr_in *saddr4 = (struct sockaddr_in *) saddr;
Packit 209cc3
		if (IN_MULTICAST(ntohl(saddr4->sin_addr.s_addr))) return 0;
Packit 209cc3
		else return -1;
Packit 209cc3
	}
Packit 209cc3
	else
Packit 209cc3
	{
Packit 209cc3
		struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *) saddr;
Packit 209cc3
		if (IN6_IS_ADDR_MULTICAST(&saddr6->sin6_addr)) return 0;
Packit 209cc3
		else return -1;
Packit 209cc3
	}
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * \brief It initializes a network connection both from the client and the server side.
Packit 209cc3
 *
Packit 209cc3
 * In case of a client socket, this function calls socket() and connect().
Packit 209cc3
 * In the meanwhile, it checks for any socket error.
Packit 209cc3
 * If an error occurs, it writes the error message into 'errbuf'.
Packit 209cc3
 *
Packit 209cc3
 * In case of a server socket, the function calls socket(), bind() and listen().
Packit 209cc3
 *
Packit 209cc3
 * This function is usually preceeded by the sock_initaddress().
Packit 209cc3
 *
Packit 209cc3
 * \param addrinfo: pointer to an addrinfo variable which will be used to
Packit 209cc3
 * open the socket and such. This variable is the one returned by the previous call to
Packit 209cc3
 * sock_initaddress().
Packit 209cc3
 *
Packit 209cc3
 * \param server: '1' if this is a server socket, '0' otherwise.
Packit 209cc3
 *
Packit 209cc3
 * \param nconn: number of the connections that are allowed to wait into the listen() call.
Packit 209cc3
 * This value has no meanings in case of a client socket.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
Packit 209cc3
 * error message. This buffer has to be at least 'errbuflen' in length.
Packit 209cc3
 * It can be NULL; in this case the error cannot be printed.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
Packit 209cc3
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
Packit 209cc3
 *
Packit 209cc3
 * \return the socket that has been opened (that has to be used in the following sockets calls)
Packit 209cc3
 * if everything is fine, INVALID_SOCKET if some errors occurred. The error message is returned
Packit 209cc3
 * in the 'errbuf' variable.
Packit 209cc3
 */
Packit 209cc3
SOCKET sock_open(struct addrinfo *addrinfo, int server, int nconn, char *errbuf, int errbuflen)
Packit 209cc3
{
Packit 209cc3
	SOCKET sock;
Packit 209cc3
#if defined(SO_NOSIGPIPE) || defined(IPV6_V6ONLY) || defined(IPV6_BINDV6ONLY)
Packit 209cc3
	int on = 1;
Packit 209cc3
#endif
Packit 209cc3
Packit 209cc3
	sock = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol);
Packit 209cc3
	if (sock == INVALID_SOCKET)
Packit 209cc3
	{
Packit 209cc3
		sock_geterror("socket()", errbuf, errbuflen);
Packit 209cc3
		return INVALID_SOCKET;
Packit 209cc3
	}
Packit 209cc3
Packit 209cc3
	/*
Packit 209cc3
	 * Disable SIGPIPE, if we have SO_NOSIGPIPE.  We don't want to
Packit 209cc3
	 * have to deal with signals if the peer closes the connection,
Packit 209cc3
	 * especially in client programs, which may not even be aware that
Packit 209cc3
	 * they're sending to sockets.
Packit 209cc3
	 */
Packit 209cc3
#ifdef SO_NOSIGPIPE
Packit 209cc3
	if (setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (char *)&on,
Packit 209cc3
	    sizeof (int)) == -1)
Packit 209cc3
	{
Packit 209cc3
		sock_geterror("setsockopt(SO_NOSIGPIPE)", errbuf, errbuflen);
Packit 209cc3
		closesocket(sock);
Packit 209cc3
		return INVALID_SOCKET;
Packit 209cc3
	}
Packit 209cc3
#endif
Packit 209cc3
Packit 209cc3
	/* This is a server socket */
Packit 209cc3
	if (server)
Packit 209cc3
	{
Packit 209cc3
		/*
Packit 209cc3
		 * Allow a new server to bind the socket after the old one
Packit 209cc3
		 * exited, even if lingering sockets are still present.
Packit 209cc3
		 *
Packit 209cc3
		 * Don't treat an error as a failure.
Packit 209cc3
		 */
Packit 209cc3
		int optval = 1;
Packit 209cc3
		(void)setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
Packit 209cc3
		    (char *)&optval, sizeof (optval));
Packit 209cc3
Packit 209cc3
#if defined(IPV6_V6ONLY) || defined(IPV6_BINDV6ONLY)
Packit 209cc3
		/*
Packit 209cc3
		 * Force the use of IPv6-only addresses.
Packit 209cc3
		 *
Packit 209cc3
		 * RFC 3493 indicates that you can support IPv4 on an
Packit 209cc3
		 * IPv6 socket:
Packit 209cc3
		 *
Packit 209cc3
		 *    https://tools.ietf.org/html/rfc3493#section-3.7
Packit 209cc3
		 *
Packit 209cc3
		 * and that this is the default behavior.  This means
Packit 209cc3
		 * that if we first create an IPv6 socket bound to the
Packit 209cc3
		 * "any" address, it is, in effect, also bound to the
Packit 209cc3
		 * IPv4 "any" address, so when we create an IPv4 socket
Packit 209cc3
		 * and try to bind it to the IPv4 "any" address, it gets
Packit 209cc3
		 * EADDRINUSE.
Packit 209cc3
		 *
Packit 209cc3
		 * Not all network stacks support IPv4 on IPv6 sockets;
Packit 209cc3
		 * pre-NT 6 Windows stacks don't support it, and the
Packit 209cc3
		 * OpenBSD stack doesn't support it for security reasons
Packit 209cc3
		 * (see the OpenBSD inet6(4) man page).  Therefore, we
Packit 209cc3
		 * don't want to rely on this behavior.
Packit 209cc3
		 *
Packit 209cc3
		 * So we try to disable it, using either the IPV6_V6ONLY
Packit 209cc3
		 * option from RFC 3493:
Packit 209cc3
		 *
Packit 209cc3
		 *    https://tools.ietf.org/html/rfc3493#section-5.3
Packit 209cc3
		 *
Packit 209cc3
		 * or the IPV6_BINDV6ONLY option from older UN*Xes.
Packit 209cc3
		 */
Packit 209cc3
#ifndef IPV6_V6ONLY
Packit 209cc3
  /* For older systems */
Packit 209cc3
  #define IPV6_V6ONLY IPV6_BINDV6ONLY
Packit 209cc3
#endif /* IPV6_V6ONLY */
Packit 209cc3
		if (addrinfo->ai_family == PF_INET6)
Packit 209cc3
		{
Packit 209cc3
			if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
Packit 209cc3
			    (char *)&on, sizeof (int)) == -1)
Packit 209cc3
			{
Packit 209cc3
				if (errbuf)
Packit 209cc3
					pcap_snprintf(errbuf, errbuflen, "setsockopt(IPV6_V6ONLY)");
Packit 209cc3
				closesocket(sock);
Packit 209cc3
				return INVALID_SOCKET;
Packit 209cc3
			}
Packit 209cc3
		}
Packit 209cc3
#endif /* defined(IPV6_V6ONLY) || defined(IPV6_BINDV6ONLY) */
Packit 209cc3
Packit 209cc3
		/* WARNING: if the address is a mcast one, I should place the proper Win32 code here */
Packit 209cc3
		if (bind(sock, addrinfo->ai_addr, (int) addrinfo->ai_addrlen) != 0)
Packit 209cc3
		{
Packit 209cc3
			sock_geterror("bind()", errbuf, errbuflen);
Packit 209cc3
			closesocket(sock);
Packit 209cc3
			return INVALID_SOCKET;
Packit 209cc3
		}
Packit 209cc3
Packit 209cc3
		if (addrinfo->ai_socktype == SOCK_STREAM)
Packit 209cc3
			if (listen(sock, nconn) == -1)
Packit 209cc3
			{
Packit 209cc3
				sock_geterror("listen()", errbuf, errbuflen);
Packit 209cc3
				closesocket(sock);
Packit 209cc3
				return INVALID_SOCKET;
Packit 209cc3
			}
Packit 209cc3
Packit 209cc3
		/* server side ended */
Packit 209cc3
		return sock;
Packit 209cc3
	}
Packit 209cc3
	else	/* we're the client */
Packit 209cc3
	{
Packit 209cc3
		struct addrinfo *tempaddrinfo;
Packit 209cc3
		char *errbufptr;
Packit 209cc3
		size_t bufspaceleft;
Packit 209cc3
Packit 209cc3
		tempaddrinfo = addrinfo;
Packit 209cc3
		errbufptr = errbuf;
Packit 209cc3
		bufspaceleft = errbuflen;
Packit 209cc3
		*errbufptr = 0;
Packit 209cc3
Packit 209cc3
		/*
Packit 209cc3
		 * We have to loop though all the addinfo returned.
Packit 209cc3
		 * For instance, we can have both IPv6 and IPv4 addresses, but the service we're trying
Packit 209cc3
		 * to connect to is unavailable in IPv6, so we have to try in IPv4 as well
Packit 209cc3
		 */
Packit 209cc3
		while (tempaddrinfo)
Packit 209cc3
		{
Packit 209cc3
Packit 209cc3
			if (connect(sock, tempaddrinfo->ai_addr, (int) tempaddrinfo->ai_addrlen) == -1)
Packit 209cc3
			{
Packit 209cc3
				size_t msglen;
Packit 209cc3
				char TmpBuffer[100];
Packit 209cc3
				char SocketErrorMessage[SOCK_ERRBUF_SIZE];
Packit 209cc3
Packit 209cc3
				/*
Packit 209cc3
				 * We have to retrieve the error message before any other socket call completes, otherwise
Packit 209cc3
				 * the error message is lost
Packit 209cc3
				 */
Packit 209cc3
				sock_geterror("Connect to socket failed",
Packit 209cc3
				    SocketErrorMessage, sizeof(SocketErrorMessage));
Packit 209cc3
Packit 209cc3
				/* Returns the numeric address of the host that triggered the error */
Packit 209cc3
				sock_getascii_addrport((struct sockaddr_storage *) tempaddrinfo->ai_addr, TmpBuffer, sizeof(TmpBuffer), NULL, 0, NI_NUMERICHOST, TmpBuffer, sizeof(TmpBuffer));
Packit 209cc3
Packit 209cc3
				pcap_snprintf(errbufptr, bufspaceleft,
Packit 209cc3
				    "Is the server properly installed on %s?  %s", TmpBuffer, SocketErrorMessage);
Packit 209cc3
Packit 209cc3
				/* In case more then one 'connect' fails, we manage to keep all the error messages */
Packit 209cc3
				msglen = strlen(errbufptr);
Packit 209cc3
Packit 209cc3
				errbufptr[msglen] = ' ';
Packit 209cc3
				errbufptr[msglen + 1] = 0;
Packit 209cc3
Packit 209cc3
				bufspaceleft = bufspaceleft - (msglen + 1);
Packit 209cc3
				errbufptr += (msglen + 1);
Packit 209cc3
Packit 209cc3
				tempaddrinfo = tempaddrinfo->ai_next;
Packit 209cc3
			}
Packit 209cc3
			else
Packit 209cc3
				break;
Packit 209cc3
		}
Packit 209cc3
Packit 209cc3
		/*
Packit 209cc3
		 * Check how we exit from the previous loop
Packit 209cc3
		 * If tempaddrinfo is equal to NULL, it means that all the connect() failed.
Packit 209cc3
		 */
Packit 209cc3
		if (tempaddrinfo == NULL)
Packit 209cc3
		{
Packit 209cc3
			closesocket(sock);
Packit 209cc3
			return INVALID_SOCKET;
Packit 209cc3
		}
Packit 209cc3
		else
Packit 209cc3
			return sock;
Packit 209cc3
	}
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * \brief Closes the present (TCP and UDP) socket connection.
Packit 209cc3
 *
Packit 209cc3
 * This function sends a shutdown() on the socket in order to disable send() calls
Packit 209cc3
 * (while recv() ones are still allowed). Then, it closes the socket.
Packit 209cc3
 *
Packit 209cc3
 * \param sock: the socket identifier of the connection that has to be closed.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
Packit 209cc3
 * error message. This buffer has to be at least 'errbuflen' in length.
Packit 209cc3
 * It can be NULL; in this case the error cannot be printed.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
Packit 209cc3
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
Packit 209cc3
 *
Packit 209cc3
 * \return '0' if everything is fine, '-1' if some errors occurred. The error message is returned
Packit 209cc3
 * in the 'errbuf' variable.
Packit 209cc3
 */
Packit 209cc3
int sock_close(SOCKET sock, char *errbuf, int errbuflen)
Packit 209cc3
{
Packit 209cc3
	/*
Packit 209cc3
	 * SHUT_WR: subsequent calls to the send function are disallowed.
Packit 209cc3
	 * For TCP sockets, a FIN will be sent after all data is sent and
Packit 209cc3
	 * acknowledged by the Server.
Packit 209cc3
	 */
Packit 209cc3
	if (shutdown(sock, SHUT_WR))
Packit 209cc3
	{
Packit 209cc3
		sock_geterror("shutdown()", errbuf, errbuflen);
Packit 209cc3
		/* close the socket anyway */
Packit 209cc3
		closesocket(sock);
Packit 209cc3
		return -1;
Packit 209cc3
	}
Packit 209cc3
Packit 209cc3
	closesocket(sock);
Packit 209cc3
	return 0;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * gai_errstring() has some problems:
Packit 209cc3
 *
Packit 209cc3
 * 1) on Windows, Microsoft explicitly says it's not thread-safe;
Packit 209cc3
 * 2) on UN*X, the Single UNIX Specification doesn't say it *is*
Packit 209cc3
 *    thread-safe, so an implementation might use a static buffer
Packit 209cc3
 *    for unknown error codes;
Packit 209cc3
 * 3) the error message for the most likely error, EAI_NONAME, is
Packit 209cc3
 *    truly horrible on several platforms ("nodename nor servname
Packit 209cc3
 *    provided, or not known"?  It's typically going to be "not
Packit 209cc3
 *    known", not "oopsie, I passed null pointers for the host name
Packit 209cc3
 *    and service name", not to mention they forgot the "neither");
Packit 209cc3
 *
Packit 209cc3
 * so we roll our own.
Packit 209cc3
 */
Packit 209cc3
static void
Packit 209cc3
get_gai_errstring(char *errbuf, int errbuflen, const char *prefix, int err,
Packit 209cc3
    const char *hostname, const char *portname)
Packit 209cc3
{
Packit 209cc3
	char hostport[PCAP_ERRBUF_SIZE];
Packit 209cc3
Packit 209cc3
	if (hostname != NULL && portname != NULL)
Packit 209cc3
		pcap_snprintf(hostport, PCAP_ERRBUF_SIZE, "%s:%s",
Packit 209cc3
		    hostname, portname);
Packit 209cc3
	else if (hostname != NULL)
Packit 209cc3
		pcap_snprintf(hostport, PCAP_ERRBUF_SIZE, "%s",
Packit 209cc3
		    hostname);
Packit 209cc3
	else if (portname != NULL)
Packit 209cc3
		pcap_snprintf(hostport, PCAP_ERRBUF_SIZE, ":%s",
Packit 209cc3
		    portname);
Packit 209cc3
	else
Packit 209cc3
		pcap_snprintf(hostport, PCAP_ERRBUF_SIZE, "<no host or port!>");
Packit 209cc3
	switch (err)
Packit 209cc3
	{
Packit 209cc3
#ifdef EAI_ADDRFAMILY
Packit 209cc3
		case EAI_ADDRFAMILY:
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen,
Packit 209cc3
			    "%sAddress family for %s not supported",
Packit 209cc3
			    prefix, hostport);
Packit 209cc3
			break;
Packit 209cc3
#endif
Packit 209cc3
Packit 209cc3
		case EAI_AGAIN:
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen,
Packit 209cc3
			    "%s%s could not be resolved at this time",
Packit 209cc3
			    prefix, hostport);
Packit 209cc3
			break;
Packit 209cc3
Packit 209cc3
		case EAI_BADFLAGS:
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen,
Packit 209cc3
			    "%sThe ai_flags parameter for looking up %s had an invalid value",
Packit 209cc3
			    prefix, hostport);
Packit 209cc3
			break;
Packit 209cc3
Packit 209cc3
		case EAI_FAIL:
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen,
Packit 209cc3
			    "%sA non-recoverable error occurred when attempting to resolve %s",
Packit 209cc3
			    prefix, hostport);
Packit 209cc3
			break;
Packit 209cc3
Packit 209cc3
		case EAI_FAMILY:
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen,
Packit 209cc3
			    "%sThe address family for looking up %s was not recognized",
Packit 209cc3
			    prefix, hostport);
Packit 209cc3
			break;
Packit 209cc3
Packit 209cc3
		case EAI_MEMORY:
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen,
Packit 209cc3
			    "%sOut of memory trying to allocate storage when looking up %s",
Packit 209cc3
			    prefix, hostport);
Packit 209cc3
			break;
Packit 209cc3
Packit 209cc3
		/*
Packit 209cc3
		 * RFC 2553 had both EAI_NODATA and EAI_NONAME.
Packit 209cc3
		 *
Packit 209cc3
		 * RFC 3493 has only EAI_NONAME.
Packit 209cc3
		 *
Packit 209cc3
		 * Some implementations define EAI_NODATA and EAI_NONAME
Packit 209cc3
		 * to the same value, others don't.  If EAI_NODATA is
Packit 209cc3
		 * defined and isn't the same as EAI_NONAME, we handle
Packit 209cc3
		 * EAI_NODATA.
Packit 209cc3
		 */
Packit 209cc3
#if defined(EAI_NODATA) && EAI_NODATA != EAI_NONAME
Packit 209cc3
		case EAI_NODATA:
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen,
Packit 209cc3
			    "%sNo address associated with %s",
Packit 209cc3
			    prefix, hostport);
Packit 209cc3
			break;
Packit 209cc3
#endif
Packit 209cc3
Packit 209cc3
		case EAI_NONAME:
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen,
Packit 209cc3
			    "%sThe host name %s couldn't be resolved",
Packit 209cc3
			    prefix, hostport);
Packit 209cc3
			break;
Packit 209cc3
Packit 209cc3
		case EAI_SERVICE:
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen,
Packit 209cc3
			    "%sThe service value specified when looking up %s as not recognized for the socket type",
Packit 209cc3
			    prefix, hostport);
Packit 209cc3
			break;
Packit 209cc3
Packit 209cc3
		case EAI_SOCKTYPE:
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen,
Packit 209cc3
			    "%sThe socket type specified when looking up %s as not recognized",
Packit 209cc3
			    prefix, hostport);
Packit 209cc3
			break;
Packit 209cc3
Packit 209cc3
#ifdef EAI_SYSTEM
Packit 209cc3
		case EAI_SYSTEM:
Packit 209cc3
			/*
Packit 209cc3
			 * Assumed to be UN*X.
Packit 209cc3
			 */
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen,
Packit 209cc3
			    "%sAn error occurred when looking up %s: %s",
Packit 209cc3
			    prefix, hostport, pcap_strerror(errno));
Packit 209cc3
			break;
Packit 209cc3
#endif
Packit 209cc3
Packit 209cc3
#ifdef EAI_BADHINTS
Packit 209cc3
		case EAI_BADHINTS:
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen,
Packit 209cc3
			    "%sInvalid value for hints when looking up %s",
Packit 209cc3
			    prefix, hostport);
Packit 209cc3
			break;
Packit 209cc3
#endif
Packit 209cc3
Packit 209cc3
#ifdef EAI_PROTOCOL
Packit 209cc3
		case EAI_PROTOCOL:
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen,
Packit 209cc3
			    "%sResolved protocol when looking up %s is unknown",
Packit 209cc3
			    prefix, hostport);
Packit 209cc3
			break;
Packit 209cc3
#endif
Packit 209cc3
Packit 209cc3
#ifdef EAI_OVERFLOW
Packit 209cc3
		case EAI_OVERFLOW:
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen,
Packit 209cc3
			    "%sArgument buffer overflow when looking up %s",
Packit 209cc3
			    prefix, hostport);
Packit 209cc3
			break;
Packit 209cc3
#endif
Packit 209cc3
Packit 209cc3
		default:
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen,
Packit 209cc3
			    "%sgetaddrinfo() error %d when looking up %s",
Packit 209cc3
			    prefix, err, hostport);
Packit 209cc3
			break;
Packit 209cc3
	}
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * \brief Checks that the address, port and flags given are valids and it returns an 'addrinfo' structure.
Packit 209cc3
 *
Packit 209cc3
 * This function basically calls the getaddrinfo() calls, and it performs a set of sanity checks
Packit 209cc3
 * to control that everything is fine (e.g. a TCP socket cannot have a mcast address, and such).
Packit 209cc3
 * If an error occurs, it writes the error message into 'errbuf'.
Packit 209cc3
 *
Packit 209cc3
 * \param host: a pointer to a string identifying the host. It can be
Packit 209cc3
 * a host name, a numeric literal address, or NULL or "" (useful
Packit 209cc3
 * in case of a server socket which has to bind to all addresses).
Packit 209cc3
 *
Packit 209cc3
 * \param port: a pointer to a user-allocated buffer containing the network port to use.
Packit 209cc3
 *
Packit 209cc3
 * \param hints: an addrinfo variable (passed by reference) containing the flags needed to create the
Packit 209cc3
 * addrinfo structure appropriately.
Packit 209cc3
 *
Packit 209cc3
 * \param addrinfo: it represents the true returning value. This is a pointer to an addrinfo variable
Packit 209cc3
 * (passed by reference), which will be allocated by this function and returned back to the caller.
Packit 209cc3
 * This variable will be used in the next sockets calls.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
Packit 209cc3
 * error message. This buffer has to be at least 'errbuflen' in length.
Packit 209cc3
 * It can be NULL; in this case the error cannot be printed.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
Packit 209cc3
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
Packit 209cc3
 *
Packit 209cc3
 * \return '0' if everything is fine, '-1' if some errors occurred. The error message is returned
Packit 209cc3
 * in the 'errbuf' variable. The addrinfo variable that has to be used in the following sockets calls is
Packit 209cc3
 * returned into the addrinfo parameter.
Packit 209cc3
 *
Packit 209cc3
 * \warning The 'addrinfo' variable has to be deleted by the programmer by calling freeaddrinfo() when
Packit 209cc3
 * it is no longer needed.
Packit 209cc3
 *
Packit 209cc3
 * \warning This function requires the 'hints' variable as parameter. The semantic of this variable is the same
Packit 209cc3
 * of the one of the corresponding variable used into the standard getaddrinfo() socket function. We suggest
Packit 209cc3
 * the programmer to look at that function in order to set the 'hints' variable appropriately.
Packit 209cc3
 */
Packit 209cc3
int sock_initaddress(const char *host, const char *port,
Packit 209cc3
    struct addrinfo *hints, struct addrinfo **addrinfo, char *errbuf, int errbuflen)
Packit 209cc3
{
Packit 209cc3
	int retval;
Packit 209cc3
Packit 209cc3
	retval = getaddrinfo(host, port, hints, addrinfo);
Packit 209cc3
	if (retval != 0)
Packit 209cc3
	{
Packit 209cc3
		if (errbuf)
Packit 209cc3
		{
Packit 209cc3
			get_gai_errstring(errbuf, errbuflen, "", retval,
Packit 209cc3
			    host, port);
Packit 209cc3
		}
Packit 209cc3
		return -1;
Packit 209cc3
	}
Packit 209cc3
	/*
Packit 209cc3
	 * \warning SOCKET: I should check all the accept() in order to bind to all addresses in case
Packit 209cc3
	 * addrinfo has more han one pointers
Packit 209cc3
	 */
Packit 209cc3
Packit 209cc3
	/*
Packit 209cc3
	 * This software only supports PF_INET and PF_INET6.
Packit 209cc3
	 *
Packit 209cc3
	 * XXX - should we just check that at least *one* address is
Packit 209cc3
	 * either PF_INET or PF_INET6, and, when using the list,
Packit 209cc3
	 * ignore all addresses that are neither?  (What, no IPX
Packit 209cc3
	 * support? :-))
Packit 209cc3
	 */
Packit 209cc3
	if (((*addrinfo)->ai_family != PF_INET) &&
Packit 209cc3
	    ((*addrinfo)->ai_family != PF_INET6))
Packit 209cc3
	{
Packit 209cc3
		if (errbuf)
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen, "getaddrinfo(): socket type not supported");
Packit 209cc3
		freeaddrinfo(*addrinfo);
Packit 209cc3
		*addrinfo = NULL;
Packit 209cc3
		return -1;
Packit 209cc3
	}
Packit 209cc3
Packit 209cc3
	/*
Packit 209cc3
	 * You can't do multicast (or broadcast) TCP.
Packit 209cc3
	 */
Packit 209cc3
	if (((*addrinfo)->ai_socktype == SOCK_STREAM) &&
Packit 209cc3
	    (sock_ismcastaddr((*addrinfo)->ai_addr) == 0))
Packit 209cc3
	{
Packit 209cc3
		if (errbuf)
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen, "getaddrinfo(): multicast addresses are not valid when using TCP streams");
Packit 209cc3
		freeaddrinfo(*addrinfo);
Packit 209cc3
		*addrinfo = NULL;
Packit 209cc3
		return -1;
Packit 209cc3
	}
Packit 209cc3
Packit 209cc3
	return 0;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * \brief It sends the amount of data contained into 'buffer' on the given socket.
Packit 209cc3
 *
Packit 209cc3
 * This function basically calls the send() socket function and it checks that all
Packit 209cc3
 * the data specified in 'buffer' (of size 'size') will be sent. If an error occurs,
Packit 209cc3
 * it writes the error message into 'errbuf'.
Packit 209cc3
 * In case the socket buffer does not have enough space, it loops until all data
Packit 209cc3
 * has been sent.
Packit 209cc3
 *
Packit 209cc3
 * \param socket: the connected socket currently opened.
Packit 209cc3
 *
Packit 209cc3
 * \param buffer: a char pointer to a user-allocated buffer in which data is contained.
Packit 209cc3
 *
Packit 209cc3
 * \param size: number of bytes that have to be sent.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
Packit 209cc3
 * error message. This buffer has to be at least 'errbuflen' in length.
Packit 209cc3
 * It can be NULL; in this case the error cannot be printed.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
Packit 209cc3
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
Packit 209cc3
 *
Packit 209cc3
 * \return '0' if everything is fine, '-1' if an error other than
Packit 209cc3
 * "connection reset" or "peer has closed the receive side" occurred,
Packit 209cc3
 * '-2' if we got one of those errors.
Packit 209cc3
 * For errors, an error message is returned in the 'errbuf' variable.
Packit 209cc3
 */
Packit 209cc3
int sock_send(SOCKET sock, const char *buffer, size_t size,
Packit 209cc3
    char *errbuf, int errbuflen)
Packit 209cc3
{
Packit 209cc3
	int remaining;
Packit 209cc3
	ssize_t nsent;
Packit 209cc3
Packit 209cc3
	if (size > INT_MAX)
Packit 209cc3
	{
Packit 209cc3
		if (errbuf)
Packit 209cc3
		{
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen,
Packit 209cc3
			    "Can't send more than %u bytes with sock_send",
Packit 209cc3
			    INT_MAX);
Packit 209cc3
		}
Packit 209cc3
		return -1;
Packit 209cc3
	}
Packit 209cc3
	remaining = (int)size;
Packit 209cc3
Packit 209cc3
	do {
Packit 209cc3
#ifdef MSG_NOSIGNAL
Packit 209cc3
		/*
Packit 209cc3
		 * Send with MSG_NOSIGNAL, so that we don't get SIGPIPE
Packit 209cc3
		 * on errors on stream-oriented sockets when the other
Packit 209cc3
		 * end breaks the connection.
Packit 209cc3
		 * The EPIPE error is still returned.
Packit 209cc3
		 */
Packit 209cc3
		nsent = send(sock, buffer, remaining, MSG_NOSIGNAL);
Packit 209cc3
#else
Packit 209cc3
		nsent = send(sock, buffer, remaining, 0);
Packit 209cc3
#endif
Packit 209cc3
Packit 209cc3
		if (nsent == -1)
Packit 209cc3
		{
Packit 209cc3
			/*
Packit 209cc3
			 * If the client closed the connection out from
Packit 209cc3
			 * under us, there's no need to log that as an
Packit 209cc3
			 * error.
Packit 209cc3
			 */
Packit 209cc3
			int errcode;
Packit 209cc3
Packit 209cc3
#ifdef _WIN32
Packit 209cc3
			errcode = GetLastError();
Packit 209cc3
			if (errcode == WSAECONNRESET ||
Packit 209cc3
			    errcode == WSAECONNABORTED)
Packit 209cc3
			{
Packit 209cc3
				/*
Packit 209cc3
				 * WSAECONNABORTED appears to be the error
Packit 209cc3
				 * returned in Winsock when you try to send
Packit 209cc3
				 * on a connection where the peer has closed
Packit 209cc3
				 * the receive side.
Packit 209cc3
				 */
Packit 209cc3
				return -2;
Packit 209cc3
			}
Packit 209cc3
			sock_fmterror("send()", errcode, errbuf, errbuflen);
Packit 209cc3
#else
Packit 209cc3
			errcode = errno;
Packit 209cc3
			if (errcode == ECONNRESET || errcode == EPIPE)
Packit 209cc3
			{
Packit 209cc3
				/*
Packit 209cc3
				 * EPIPE is what's returned on UN*X when
Packit 209cc3
				 * you try to send on a connection when
Packit 209cc3
				 * the peer has closed the receive side.
Packit 209cc3
				 */
Packit 209cc3
				return -2;
Packit 209cc3
			}
Packit 209cc3
			sock_fmterror("send()", errcode, errbuf, errbuflen);
Packit 209cc3
#endif
Packit 209cc3
			return -1;
Packit 209cc3
		}
Packit 209cc3
Packit 209cc3
		remaining -= nsent;
Packit 209cc3
		buffer += nsent;
Packit 209cc3
	} while (remaining != 0);
Packit 209cc3
Packit 209cc3
	return 0;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * \brief It copies the amount of data contained into 'buffer' into 'tempbuf'.
Packit 209cc3
 * and it checks for buffer overflows.
Packit 209cc3
 *
Packit 209cc3
 * This function basically copies 'size' bytes of data contained into 'buffer'
Packit 209cc3
 * into 'tempbuf', starting at offset 'offset'. Before that, it checks that the
Packit 209cc3
 * resulting buffer will not be larger	than 'totsize'. Finally, it updates
Packit 209cc3
 * the 'offset' variable in order to point to the first empty location of the buffer.
Packit 209cc3
 *
Packit 209cc3
 * In case the function is called with 'checkonly' equal to 1, it does not copy
Packit 209cc3
 * the data into the buffer. It only checks for buffer overflows and it updates the
Packit 209cc3
 * 'offset' variable. This mode can be useful when the buffer already contains the
Packit 209cc3
 * data (maybe because the producer writes directly into the target buffer), so
Packit 209cc3
 * only the buffer overflow check has to be made.
Packit 209cc3
 * In this case, both 'buffer' and 'tempbuf' can be NULL values.
Packit 209cc3
 *
Packit 209cc3
 * This function is useful in case the userland application does not know immediately
Packit 209cc3
 * all the data it has to write into the socket. This function provides a way to create
Packit 209cc3
 * the "stream" step by step, appending the new data to the old one. Then, when all the
Packit 209cc3
 * data has been bufferized, the application can call the sock_send() function.
Packit 209cc3
 *
Packit 209cc3
 * \param buffer: a char pointer to a user-allocated buffer that keeps the data
Packit 209cc3
 * that has to be copied.
Packit 209cc3
 *
Packit 209cc3
 * \param size: number of bytes that have to be copied.
Packit 209cc3
 *
Packit 209cc3
 * \param tempbuf: user-allocated buffer (of size 'totsize') in which data
Packit 209cc3
 * has to be copied.
Packit 209cc3
 *
Packit 209cc3
 * \param offset: an index into 'tempbuf' which keeps the location of its first
Packit 209cc3
 * empty location.
Packit 209cc3
 *
Packit 209cc3
 * \param totsize: total size of the buffer in which data is being copied.
Packit 209cc3
 *
Packit 209cc3
 * \param checkonly: '1' if we do not want to copy data into the buffer and we
Packit 209cc3
 * want just do a buffer ovreflow control, '0' if data has to be copied as well.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
Packit 209cc3
 * error message. This buffer has to be at least 'errbuflen' in length.
Packit 209cc3
 * It can be NULL; in this case the error cannot be printed.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
Packit 209cc3
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
Packit 209cc3
 *
Packit 209cc3
 * \return '0' if everything is fine, '-1' if some errors occurred. The error message
Packit 209cc3
 * is returned in the 'errbuf' variable. When the function returns, 'tempbuf' will
Packit 209cc3
 * have the new string appended, and 'offset' will keep the length of that buffer.
Packit 209cc3
 * In case of 'checkonly == 1', data is not copied, but 'offset' is updated in any case.
Packit 209cc3
 *
Packit 209cc3
 * \warning This function assumes that the buffer in which data has to be stored is
Packit 209cc3
 * large 'totbuf' bytes.
Packit 209cc3
 *
Packit 209cc3
 * \warning In case of 'checkonly', be carefully to call this function *before* copying
Packit 209cc3
 * the data into the buffer. Otherwise, the control about the buffer overflow is useless.
Packit 209cc3
 */
Packit 209cc3
int sock_bufferize(const char *buffer, int size, char *tempbuf, int *offset, int totsize, int checkonly, char *errbuf, int errbuflen)
Packit 209cc3
{
Packit 209cc3
	if ((*offset + size) > totsize)
Packit 209cc3
	{
Packit 209cc3
		if (errbuf)
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen, "Not enough space in the temporary send buffer.");
Packit 209cc3
		return -1;
Packit 209cc3
	}
Packit 209cc3
Packit 209cc3
	if (!checkonly)
Packit 209cc3
		memcpy(tempbuf + (*offset), buffer, size);
Packit 209cc3
Packit 209cc3
	(*offset) += size;
Packit 209cc3
Packit 209cc3
	return 0;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * \brief It waits on a connected socket and it manages to receive data.
Packit 209cc3
 *
Packit 209cc3
 * This function basically calls the recv() socket function and it checks that no
Packit 209cc3
 * error occurred. If that happens, it writes the error message into 'errbuf'.
Packit 209cc3
 *
Packit 209cc3
 * This function changes its behavior according to the 'receiveall' flag: if we
Packit 209cc3
 * want to receive exactly 'size' byte, it loops on the recv()	until all the requested
Packit 209cc3
 * data is arrived. Otherwise, it returns the data currently available.
Packit 209cc3
 *
Packit 209cc3
 * In case the socket does not have enough data available, it cycles on the recv()
Packit 209cc3
 * until the requested data (of size 'size') is arrived.
Packit 209cc3
 * In this case, it blocks until the number of bytes read is equal to 'size'.
Packit 209cc3
 *
Packit 209cc3
 * \param sock: the connected socket currently opened.
Packit 209cc3
 *
Packit 209cc3
 * \param buffer: a char pointer to a user-allocated buffer in which data has to be stored
Packit 209cc3
 *
Packit 209cc3
 * \param size: size of the allocated buffer. WARNING: this indicates the number of bytes
Packit 209cc3
 * that we are expecting to be read.
Packit 209cc3
 *
Packit 209cc3
 * \param flags:
Packit 209cc3
 *
Packit 209cc3
 *   SOCK_RECEIVALL_XXX:
Packit 209cc3
 *
Packit 209cc3
 * 	if SOCK_RECEIVEALL_NO, return as soon as some data is ready
Packit 209cc3
 *	if SOCK_RECEIVALL_YES, wait until 'size' data has been
Packit 209cc3
 *	    received (in case the socket does not have enough data available).
Packit 209cc3
 *
Packit 209cc3
 *   SOCK_EOF_XXX:
Packit 209cc3
 *
Packit 209cc3
 *	if SOCK_EOF_ISNT_ERROR, if the first read returns 0, just return 0,
Packit 209cc3
 *	    and return an error on any subsequent read that returns 0;
Packit 209cc3
 *	if SOCK_EOF_IS_ERROR, if any read returns 0, return an error.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
Packit 209cc3
 * error message. This buffer has to be at least 'errbuflen' in length.
Packit 209cc3
 * It can be NULL; in this case the error cannot be printed.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
Packit 209cc3
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
Packit 209cc3
 *
Packit 209cc3
 * \return the number of bytes read if everything is fine, '-1' if some errors occurred.
Packit 209cc3
 * The error message is returned in the 'errbuf' variable.
Packit 209cc3
 */
Packit 209cc3
Packit 209cc3
int sock_recv(SOCKET sock, void *buffer, size_t size, int flags,
Packit 209cc3
    char *errbuf, int errbuflen)
Packit 209cc3
{
Packit 209cc3
	char *bufp = buffer;
Packit 209cc3
	int remaining;
Packit 209cc3
	ssize_t nread;
Packit 209cc3
Packit 209cc3
	if (size == 0)
Packit 209cc3
	{
Packit 209cc3
		return 0;
Packit 209cc3
	}
Packit 209cc3
	if (size > INT_MAX)
Packit 209cc3
	{
Packit 209cc3
		if (errbuf)
Packit 209cc3
		{
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen,
Packit 209cc3
			    "Can't read more than %u bytes with sock_recv",
Packit 209cc3
			    INT_MAX);
Packit 209cc3
		}
Packit 209cc3
		return -1;
Packit 209cc3
	}
Packit 209cc3
Packit 209cc3
	bufp = (char *) buffer;
Packit 209cc3
	remaining = (int) size;
Packit 209cc3
Packit 209cc3
	/*
Packit 209cc3
	 * We don't use MSG_WAITALL because it's not supported in
Packit 209cc3
	 * Win32.
Packit 209cc3
	 */
Packit 209cc3
	for (;;) {
Packit 209cc3
		nread = recv(sock, bufp, remaining, 0);
Packit 209cc3
Packit 209cc3
		if (nread == -1)
Packit 209cc3
		{
Packit 209cc3
#ifndef _WIN32
Packit 209cc3
			if (errno == EINTR)
Packit 209cc3
				return -3;
Packit 209cc3
#endif
Packit 209cc3
			sock_geterror("recv()", errbuf, errbuflen);
Packit 209cc3
			return -1;
Packit 209cc3
		}
Packit 209cc3
Packit 209cc3
		if (nread == 0)
Packit 209cc3
		{
Packit 209cc3
			if ((flags & SOCK_EOF_IS_ERROR) ||
Packit 209cc3
			    (remaining != (int) size))
Packit 209cc3
			{
Packit 209cc3
				/*
Packit 209cc3
				 * Either we've already read some data,
Packit 209cc3
				 * or we're always supposed to return
Packit 209cc3
				 * an error on EOF.
Packit 209cc3
				 */
Packit 209cc3
				if (errbuf)
Packit 209cc3
				{
Packit 209cc3
					pcap_snprintf(errbuf, errbuflen,
Packit 209cc3
					    "The other host terminated the connection.");
Packit 209cc3
				}
Packit 209cc3
				return -1;
Packit 209cc3
			}
Packit 209cc3
			else
Packit 209cc3
				return 0;
Packit 209cc3
		}
Packit 209cc3
Packit 209cc3
		/*
Packit 209cc3
		 * Do we want to read the amount requested, or just return
Packit 209cc3
		 * what we got?
Packit 209cc3
		 */
Packit 209cc3
		if (!(flags & SOCK_RECEIVEALL_YES))
Packit 209cc3
		{
Packit 209cc3
			/*
Packit 209cc3
			 * Just return what we got.
Packit 209cc3
			 */
Packit 209cc3
			return (int) nread;
Packit 209cc3
		}
Packit 209cc3
Packit 209cc3
		bufp += nread;
Packit 209cc3
		remaining -= nread;
Packit 209cc3
Packit 209cc3
		if (remaining == 0)
Packit 209cc3
			return (int) size;
Packit 209cc3
	}
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * Receives a datagram from a socket.
Packit 209cc3
 *
Packit 209cc3
 * Returns the size of the datagram on success or -1 on error.
Packit 209cc3
 */
Packit 209cc3
int sock_recv_dgram(SOCKET sock, void *buffer, size_t size,
Packit 209cc3
    char *errbuf, int errbuflen)
Packit 209cc3
{
Packit 209cc3
	ssize_t nread;
Packit 209cc3
#ifndef _WIN32
Packit 209cc3
	struct msghdr message;
Packit 209cc3
	struct iovec iov;
Packit 209cc3
#endif
Packit 209cc3
Packit 209cc3
	if (size == 0)
Packit 209cc3
	{
Packit 209cc3
		return 0;
Packit 209cc3
	}
Packit 209cc3
	if (size > INT_MAX)
Packit 209cc3
	{
Packit 209cc3
		if (errbuf)
Packit 209cc3
		{
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen,
Packit 209cc3
			    "Can't read more than %u bytes with sock_recv_dgram",
Packit 209cc3
			    INT_MAX);
Packit 209cc3
		}
Packit 209cc3
		return -1;
Packit 209cc3
	}
Packit 209cc3
Packit 209cc3
	/*
Packit 209cc3
	 * This should be a datagram socket, so we should get the
Packit 209cc3
	 * entire datagram in one recv() or recvmsg() call, and
Packit 209cc3
	 * don't need to loop.
Packit 209cc3
	 */
Packit 209cc3
#ifdef _WIN32
Packit 209cc3
	nread = recv(sock, buffer, size, 0);
Packit 209cc3
	if (nread == SOCKET_ERROR)
Packit 209cc3
	{
Packit 209cc3
		/*
Packit 209cc3
		 * To quote the MSDN documentation for recv(),
Packit 209cc3
		 * "If the datagram or message is larger than
Packit 209cc3
		 * the buffer specified, the buffer is filled
Packit 209cc3
		 * with the first part of the datagram, and recv
Packit 209cc3
		 * generates the error WSAEMSGSIZE. For unreliable
Packit 209cc3
		 * protocols (for example, UDP) the excess data is
Packit 209cc3
		 * lost..."
Packit 209cc3
		 *
Packit 209cc3
		 * So if the message is bigger than the buffer
Packit 209cc3
		 * supplied to us, the excess data is discarded,
Packit 209cc3
		 * and we'll report an error.
Packit 209cc3
		 */
Packit 209cc3
		sock_geterror("recv()", errbuf, errbuflen);
Packit 209cc3
		return -1;
Packit 209cc3
	}
Packit 209cc3
#else /* _WIN32 */
Packit 209cc3
	/*
Packit 209cc3
	 * The Single UNIX Specification says that a recv() on
Packit 209cc3
	 * a socket for a message-oriented protocol will discard
Packit 209cc3
	 * the excess data.  It does *not* indicate that the
Packit 209cc3
	 * receive will fail with, for example, EMSGSIZE.
Packit 209cc3
	 *
Packit 209cc3
	 * Therefore, we use recvmsg(), which appears to be
Packit 209cc3
	 * the only way to get a "message truncated" indication
Packit 209cc3
	 * when receiving a message for a message-oriented
Packit 209cc3
	 * protocol.
Packit 209cc3
	 */
Packit 209cc3
	message.msg_name = NULL;	/* we don't care who it's from */
Packit 209cc3
	message.msg_namelen = 0;
Packit 209cc3
	iov.iov_base = buffer;
Packit 209cc3
	iov.iov_len = size;
Packit 209cc3
	message.msg_iov = &iov;
Packit 209cc3
	message.msg_iovlen = 1;
Packit 209cc3
#ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
Packit 209cc3
	message.msg_control = NULL;	/* we don't care about control information */
Packit 209cc3
	message.msg_controllen = 0;
Packit 209cc3
#endif
Packit 209cc3
#ifdef HAVE_STRUCT_MSGHDR_MSG_FLAGS
Packit 209cc3
	message.msg_flags = 0;
Packit 209cc3
#endif
Packit 209cc3
	nread = recvmsg(sock, &message, 0);
Packit 209cc3
	if (nread == -1)
Packit 209cc3
	{
Packit 209cc3
		if (errno == EINTR)
Packit 209cc3
			return -3;
Packit 209cc3
		sock_geterror("recv()", errbuf, errbuflen);
Packit 209cc3
		return -1;
Packit 209cc3
	}
Packit 209cc3
#ifdef HAVE_STRUCT_MSGHDR_MSG_FLAGS
Packit 209cc3
	/*
Packit 209cc3
	 * XXX - Solaris supports this, but only if you ask for the
Packit 209cc3
	 * X/Open version of recvmsg(); should we use that, or will
Packit 209cc3
	 * that cause other problems?
Packit 209cc3
	 */
Packit 209cc3
	if (message.msg_flags & MSG_TRUNC)
Packit 209cc3
	{
Packit 209cc3
		/*
Packit 209cc3
		 * Message was bigger than the specified buffer size.
Packit 209cc3
		 *
Packit 209cc3
		 * Report this as an error, as the Microsoft documentation
Packit 209cc3
		 * implies we'd do in a similar case on Windows.
Packit 209cc3
		 */
Packit 209cc3
		pcap_snprintf(errbuf, errbuflen, "recv(): Message too long");
Packit 209cc3
		return -1;
Packit 209cc3
	}
Packit 209cc3
#endif /* HAVE_STRUCT_MSGHDR_MSG_FLAGS */
Packit 209cc3
#endif /* _WIN32 */
Packit 209cc3
Packit 209cc3
	/*
Packit 209cc3
	 * The size we're reading fits in an int, so the return value
Packit 209cc3
	 * will fit in an int.
Packit 209cc3
	 */
Packit 209cc3
	return (int)nread;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * \brief It discards N bytes that are currently waiting to be read on the current socket.
Packit 209cc3
 *
Packit 209cc3
 * This function is useful in case we receive a message we cannot understand (e.g.
Packit 209cc3
 * wrong version number when receiving a network packet), so that we have to discard all
Packit 209cc3
 * data before reading a new message.
Packit 209cc3
 *
Packit 209cc3
 * This function will read 'size' bytes from the socket and discard them.
Packit 209cc3
 * It defines an internal buffer in which data will be copied; however, in case
Packit 209cc3
 * this buffer is not large enough, it will cycle in order to read everything as well.
Packit 209cc3
 *
Packit 209cc3
 * \param sock: the connected socket currently opened.
Packit 209cc3
 *
Packit 209cc3
 * \param size: number of bytes that have to be discarded.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
Packit 209cc3
 * error message. This buffer has to be at least 'errbuflen' in length.
Packit 209cc3
 * It can be NULL; in this case the error cannot be printed.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
Packit 209cc3
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
Packit 209cc3
 *
Packit 209cc3
 * \return '0' if everything is fine, '-1' if some errors occurred.
Packit 209cc3
 * The error message is returned in the 'errbuf' variable.
Packit 209cc3
 */
Packit 209cc3
int sock_discard(SOCKET sock, int size, char *errbuf, int errbuflen)
Packit 209cc3
{
Packit 209cc3
#define TEMP_BUF_SIZE 32768
Packit 209cc3
Packit 209cc3
	char buffer[TEMP_BUF_SIZE];		/* network buffer, to be used when the message is discarded */
Packit 209cc3
Packit 209cc3
	/*
Packit 209cc3
	 * A static allocation avoids the need of a 'malloc()' each time we want to discard a message
Packit 209cc3
	 * Our feeling is that a buffer if 32KB is enough for most of the application;
Packit 209cc3
	 * in case this is not enough, the "while" loop discards the message by calling the
Packit 209cc3
	 * sockrecv() several times.
Packit 209cc3
	 * We do not want to create a bigger variable because this causes the program to exit on
Packit 209cc3
	 * some platforms (e.g. BSD)
Packit 209cc3
	 */
Packit 209cc3
	while (size > TEMP_BUF_SIZE)
Packit 209cc3
	{
Packit 209cc3
		if (sock_recv(sock, buffer, TEMP_BUF_SIZE, SOCK_RECEIVEALL_YES, errbuf, errbuflen) == -1)
Packit 209cc3
			return -1;
Packit 209cc3
Packit 209cc3
		size -= TEMP_BUF_SIZE;
Packit 209cc3
	}
Packit 209cc3
Packit 209cc3
	/*
Packit 209cc3
	 * If there is still data to be discarded
Packit 209cc3
	 * In this case, the data can fit into the temporary buffer
Packit 209cc3
	 */
Packit 209cc3
	if (size)
Packit 209cc3
	{
Packit 209cc3
		if (sock_recv(sock, buffer, size, SOCK_RECEIVEALL_YES, errbuf, errbuflen) == -1)
Packit 209cc3
			return -1;
Packit 209cc3
	}
Packit 209cc3
Packit 209cc3
	return 0;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * \brief Checks that one host (identified by the sockaddr_storage structure) belongs to an 'allowed list'.
Packit 209cc3
 *
Packit 209cc3
 * This function is useful after an accept() call in order to check if the connecting
Packit 209cc3
 * host is allowed to connect to me. To do that, we have a buffer that keeps the list of the
Packit 209cc3
 * allowed host; this function checks the sockaddr_storage structure of the connecting host
Packit 209cc3
 * against this host list, and it returns '0' is the host is included in this list.
Packit 209cc3
 *
Packit 209cc3
 * \param hostlist: pointer to a string that contains the list of the allowed host.
Packit 209cc3
 *
Packit 209cc3
 * \param sep: a string that keeps the separators used between the hosts (for example the
Packit 209cc3
 * space character) in the host list.
Packit 209cc3
 *
Packit 209cc3
 * \param from: a sockaddr_storage structure, as it is returned by the accept() call.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
Packit 209cc3
 * error message. This buffer has to be at least 'errbuflen' in length.
Packit 209cc3
 * It can be NULL; in this case the error cannot be printed.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
Packit 209cc3
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
Packit 209cc3
 *
Packit 209cc3
 * \return It returns:
Packit 209cc3
 * - '1' if the host list is empty
Packit 209cc3
 * - '0' if the host belongs to the host list (and therefore it is allowed to connect)
Packit 209cc3
 * - '-1' in case the host does not belong to the host list (and therefore it is not allowed to connect
Packit 209cc3
 * - '-2' in case or error. The error message is returned in the 'errbuf' variable.
Packit 209cc3
 */
Packit 209cc3
int sock_check_hostlist(char *hostlist, const char *sep, struct sockaddr_storage *from, char *errbuf, int errbuflen)
Packit 209cc3
{
Packit 209cc3
	/* checks if the connecting host is among the ones allowed */
Packit 209cc3
	if ((hostlist) && (hostlist[0]))
Packit 209cc3
	{
Packit 209cc3
		char *token;					/* temp, needed to separate items into the hostlist */
Packit 209cc3
		struct addrinfo *addrinfo, *ai_next;
Packit 209cc3
		char *temphostlist;
Packit 209cc3
		char *lasts;
Packit 209cc3
		int getaddrinfo_failed = 0;
Packit 209cc3
Packit 209cc3
		/*
Packit 209cc3
		 * The problem is that strtok modifies the original variable by putting '0' at the end of each token
Packit 209cc3
		 * So, we have to create a new temporary string in which the original content is kept
Packit 209cc3
		 */
Packit 209cc3
		temphostlist = strdup(hostlist);
Packit 209cc3
		if (temphostlist == NULL)
Packit 209cc3
		{
Packit 209cc3
			sock_geterror("sock_check_hostlist(), malloc() failed", errbuf, errbuflen);
Packit 209cc3
			return -2;
Packit 209cc3
		}
Packit 209cc3
Packit 209cc3
		token = pcap_strtok_r(temphostlist, sep, &lasts);
Packit 209cc3
Packit 209cc3
		/* it avoids a warning in the compilation ('addrinfo used but not initialized') */
Packit 209cc3
		addrinfo = NULL;
Packit 209cc3
Packit 209cc3
		while (token != NULL)
Packit 209cc3
		{
Packit 209cc3
			struct addrinfo hints;
Packit 209cc3
			int retval;
Packit 209cc3
Packit 209cc3
			addrinfo = NULL;
Packit 209cc3
			memset(&hints, 0, sizeof(struct addrinfo));
Packit 209cc3
			hints.ai_family = PF_UNSPEC;
Packit 209cc3
			hints.ai_socktype = SOCK_STREAM;
Packit 209cc3
Packit 209cc3
			retval = getaddrinfo(token, NULL, &hints, &addrinfo);
Packit 209cc3
			if (retval != 0)
Packit 209cc3
			{
Packit 209cc3
				if (errbuf)
Packit 209cc3
					get_gai_errstring(errbuf, errbuflen,
Packit 209cc3
					    "Allowed host list error: ",
Packit 209cc3
					    retval, token, NULL);
Packit 209cc3
Packit 209cc3
				/*
Packit 209cc3
				 * Note that at least one call to getaddrinfo()
Packit 209cc3
				 * failed.
Packit 209cc3
				 */
Packit 209cc3
				getaddrinfo_failed = 1;
Packit 209cc3
Packit 209cc3
				/* Get next token */
Packit 209cc3
				token = pcap_strtok_r(NULL, sep, &lasts);
Packit 209cc3
				continue;
Packit 209cc3
			}
Packit 209cc3
Packit 209cc3
			/* ai_next is required to preserve the content of addrinfo, in order to deallocate it properly */
Packit 209cc3
			ai_next = addrinfo;
Packit 209cc3
			while (ai_next)
Packit 209cc3
			{
Packit 209cc3
				if (sock_cmpaddr(from, (struct sockaddr_storage *) ai_next->ai_addr) == 0)
Packit 209cc3
				{
Packit 209cc3
					free(temphostlist);
Packit 209cc3
					freeaddrinfo(addrinfo);
Packit 209cc3
					return 0;
Packit 209cc3
				}
Packit 209cc3
Packit 209cc3
				/*
Packit 209cc3
				 * If we are here, it means that the current address does not matches
Packit 209cc3
				 * Let's try with the next one in the header chain
Packit 209cc3
				 */
Packit 209cc3
				ai_next = ai_next->ai_next;
Packit 209cc3
			}
Packit 209cc3
Packit 209cc3
			freeaddrinfo(addrinfo);
Packit 209cc3
			addrinfo = NULL;
Packit 209cc3
Packit 209cc3
			/* Get next token */
Packit 209cc3
			token = pcap_strtok_r(NULL, sep, &lasts);
Packit 209cc3
		}
Packit 209cc3
Packit 209cc3
		if (addrinfo)
Packit 209cc3
		{
Packit 209cc3
			freeaddrinfo(addrinfo);
Packit 209cc3
			addrinfo = NULL;
Packit 209cc3
		}
Packit 209cc3
Packit 209cc3
		free(temphostlist);
Packit 209cc3
Packit 209cc3
		if (getaddrinfo_failed) {
Packit 209cc3
			/*
Packit 209cc3
			 * At least one getaddrinfo() call failed;
Packit 209cc3
			 * treat that as an error, so rpcapd knows
Packit 209cc3
			 * that it should log it locally as well
Packit 209cc3
			 * as telling the client about it.
Packit 209cc3
			 */
Packit 209cc3
			return -2;
Packit 209cc3
		} else {
Packit 209cc3
			/*
Packit 209cc3
			 * All getaddrinfo() calls succeeded, but
Packit 209cc3
			 * the host wasn't in the list.
Packit 209cc3
			 */
Packit 209cc3
			if (errbuf)
Packit 209cc3
				pcap_snprintf(errbuf, errbuflen, "The host is not in the allowed host list. Connection refused.");
Packit 209cc3
			return -1;
Packit 209cc3
		}
Packit 209cc3
	}
Packit 209cc3
Packit 209cc3
	/* No hostlist, so we have to return 'empty list' */
Packit 209cc3
	return 1;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * \brief Compares two addresses contained into two sockaddr_storage structures.
Packit 209cc3
 *
Packit 209cc3
 * This function is useful to compare two addresses, given their internal representation,
Packit 209cc3
 * i.e. an sockaddr_storage structure.
Packit 209cc3
 *
Packit 209cc3
 * The two structures do not need to be sockaddr_storage; you can have both 'sockaddr_in' and
Packit 209cc3
 * sockaddr_in6, properly acsted in order to be compliant to the function interface.
Packit 209cc3
 *
Packit 209cc3
 * This function will return '0' if the two addresses matches, '-1' if not.
Packit 209cc3
 *
Packit 209cc3
 * \param first: a sockaddr_storage structure, (for example the one that is returned by an
Packit 209cc3
 * accept() call), containing the first address to compare.
Packit 209cc3
 *
Packit 209cc3
 * \param second: a sockaddr_storage structure containing the second address to compare.
Packit 209cc3
 *
Packit 209cc3
 * \return '0' if the addresses are equal, '-1' if they are different.
Packit 209cc3
 */
Packit 209cc3
int sock_cmpaddr(struct sockaddr_storage *first, struct sockaddr_storage *second)
Packit 209cc3
{
Packit 209cc3
	if (first->ss_family == second->ss_family)
Packit 209cc3
	{
Packit 209cc3
		if (first->ss_family == AF_INET)
Packit 209cc3
		{
Packit 209cc3
			if (memcmp(&(((struct sockaddr_in *) first)->sin_addr),
Packit 209cc3
				&(((struct sockaddr_in *) second)->sin_addr),
Packit 209cc3
				sizeof(struct in_addr)) == 0)
Packit 209cc3
				return 0;
Packit 209cc3
		}
Packit 209cc3
		else /* address family is AF_INET6 */
Packit 209cc3
		{
Packit 209cc3
			if (memcmp(&(((struct sockaddr_in6 *) first)->sin6_addr),
Packit 209cc3
				&(((struct sockaddr_in6 *) second)->sin6_addr),
Packit 209cc3
				sizeof(struct in6_addr)) == 0)
Packit 209cc3
				return 0;
Packit 209cc3
		}
Packit 209cc3
	}
Packit 209cc3
Packit 209cc3
	return -1;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * \brief It gets the address/port the system picked for this socket (on connected sockets).
Packit 209cc3
 *
Packit 209cc3
 * It is used to return the address and port the server picked for our socket on the local machine.
Packit 209cc3
 * It works only on:
Packit 209cc3
 * - connected sockets
Packit 209cc3
 * - server sockets
Packit 209cc3
 *
Packit 209cc3
 * On unconnected client sockets it does not work because the system dynamically chooses a port
Packit 209cc3
 * only when the socket calls a send() call.
Packit 209cc3
 *
Packit 209cc3
 * \param sock: the connected socket currently opened.
Packit 209cc3
 *
Packit 209cc3
 * \param address: it contains the address that will be returned by the function. This buffer
Packit 209cc3
 * must be properly allocated by the user. The address can be either literal or numeric depending
Packit 209cc3
 * on the value of 'Flags'.
Packit 209cc3
 *
Packit 209cc3
 * \param addrlen: the length of the 'address' buffer.
Packit 209cc3
 *
Packit 209cc3
 * \param port: it contains the port that will be returned by the function. This buffer
Packit 209cc3
 * must be properly allocated by the user.
Packit 209cc3
 *
Packit 209cc3
 * \param portlen: the length of the 'port' buffer.
Packit 209cc3
 *
Packit 209cc3
 * \param flags: a set of flags (the ones defined into the getnameinfo() standard socket function)
Packit 209cc3
 * that determine if the resulting address must be in numeric / literal form, and so on.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
Packit 209cc3
 * error message. This buffer has to be at least 'errbuflen' in length.
Packit 209cc3
 * It can be NULL; in this case the error cannot be printed.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
Packit 209cc3
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
Packit 209cc3
 *
Packit 209cc3
 * \return It returns '-1' if this function succeeds, '0' otherwise.
Packit 209cc3
 * The address and port corresponding are returned back in the buffers 'address' and 'port'.
Packit 209cc3
 * In any case, the returned strings are '0' terminated.
Packit 209cc3
 *
Packit 209cc3
 * \warning If the socket is using a connectionless protocol, the address may not be available
Packit 209cc3
 * until I/O occurs on the socket.
Packit 209cc3
 */
Packit 209cc3
int sock_getmyinfo(SOCKET sock, char *address, int addrlen, char *port, int portlen, int flags, char *errbuf, int errbuflen)
Packit 209cc3
{
Packit 209cc3
	struct sockaddr_storage mysockaddr;
Packit 209cc3
	socklen_t sockaddrlen;
Packit 209cc3
Packit 209cc3
Packit 209cc3
	sockaddrlen = sizeof(struct sockaddr_storage);
Packit 209cc3
Packit 209cc3
	if (getsockname(sock, (struct sockaddr *) &mysockaddr, &sockaddrlen) == -1)
Packit 209cc3
	{
Packit 209cc3
		sock_geterror("getsockname()", errbuf, errbuflen);
Packit 209cc3
		return 0;
Packit 209cc3
	}
Packit 209cc3
Packit 209cc3
	/* Returns the numeric address of the host that triggered the error */
Packit 209cc3
	return sock_getascii_addrport(&mysockaddr, address, addrlen, port, portlen, flags, errbuf, errbuflen);
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * \brief It retrieves two strings containing the address and the port of a given 'sockaddr' variable.
Packit 209cc3
 *
Packit 209cc3
 * This function is basically an extended version of the inet_ntop(), which does not exist in
Packit 209cc3
 * Winsock because the same result can be obtained by using the getnameinfo().
Packit 209cc3
 * However, differently from inet_ntop(), this function is able to return also literal names
Packit 209cc3
 * (e.g. 'localhost') dependently from the 'Flags' parameter.
Packit 209cc3
 *
Packit 209cc3
 * The function accepts a sockaddr_storage variable (which can be returned by several functions
Packit 209cc3
 * like bind(), connect(), accept(), and more) and it transforms its content into a 'human'
Packit 209cc3
 * form. So, for instance, it is able to translate an hex address (stored in binary form) into
Packit 209cc3
 * a standard IPv6 address like "::1".
Packit 209cc3
 *
Packit 209cc3
 * The behavior of this function depends on the parameters we have in the 'Flags' variable, which
Packit 209cc3
 * are the ones allowed in the standard getnameinfo() socket function.
Packit 209cc3
 *
Packit 209cc3
 * \param sockaddr: a 'sockaddr_in' or 'sockaddr_in6' structure containing the address that
Packit 209cc3
 * need to be translated from network form into the presentation form. This structure must be
Packit 209cc3
 * zero-ed prior using it, and the address family field must be filled with the proper value.
Packit 209cc3
 * The user must cast any 'sockaddr_in' or 'sockaddr_in6' structures to 'sockaddr_storage' before
Packit 209cc3
 * calling this function.
Packit 209cc3
 *
Packit 209cc3
 * \param address: it contains the address that will be returned by the function. This buffer
Packit 209cc3
 * must be properly allocated by the user. The address can be either literal or numeric depending
Packit 209cc3
 * on the value of 'Flags'.
Packit 209cc3
 *
Packit 209cc3
 * \param addrlen: the length of the 'address' buffer.
Packit 209cc3
 *
Packit 209cc3
 * \param port: it contains the port that will be returned by the function. This buffer
Packit 209cc3
 * must be properly allocated by the user.
Packit 209cc3
 *
Packit 209cc3
 * \param portlen: the length of the 'port' buffer.
Packit 209cc3
 *
Packit 209cc3
 * \param flags: a set of flags (the ones defined into the getnameinfo() standard socket function)
Packit 209cc3
 * that determine if the resulting address must be in numeric / literal form, and so on.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
Packit 209cc3
 * error message. This buffer has to be at least 'errbuflen' in length.
Packit 209cc3
 * It can be NULL; in this case the error cannot be printed.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
Packit 209cc3
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
Packit 209cc3
 *
Packit 209cc3
 * \return It returns '-1' if this function succeeds, '0' otherwise.
Packit 209cc3
 * The address and port corresponding to the given SockAddr are returned back in the buffers 'address'
Packit 209cc3
 * and 'port'.
Packit 209cc3
 * In any case, the returned strings are '0' terminated.
Packit 209cc3
 */
Packit 209cc3
int sock_getascii_addrport(const struct sockaddr_storage *sockaddr, char *address, int addrlen, char *port, int portlen, int flags, char *errbuf, int errbuflen)
Packit 209cc3
{
Packit 209cc3
	socklen_t sockaddrlen;
Packit 209cc3
	int retval;					/* Variable that keeps the return value; */
Packit 209cc3
Packit 209cc3
	retval = -1;
Packit 209cc3
Packit 209cc3
#ifdef _WIN32
Packit 209cc3
	if (sockaddr->ss_family == AF_INET)
Packit 209cc3
		sockaddrlen = sizeof(struct sockaddr_in);
Packit 209cc3
	else
Packit 209cc3
		sockaddrlen = sizeof(struct sockaddr_in6);
Packit 209cc3
#else
Packit 209cc3
	sockaddrlen = sizeof(struct sockaddr_storage);
Packit 209cc3
#endif
Packit 209cc3
Packit 209cc3
	if ((flags & NI_NUMERICHOST) == 0)	/* Check that we want literal names */
Packit 209cc3
	{
Packit 209cc3
		if ((sockaddr->ss_family == AF_INET6) &&
Packit 209cc3
			(memcmp(&((struct sockaddr_in6 *) sockaddr)->sin6_addr, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", sizeof(struct in6_addr)) == 0))
Packit 209cc3
		{
Packit 209cc3
			if (address)
Packit 209cc3
				pcap_strlcpy(address, SOCKET_NAME_NULL_DAD, addrlen);
Packit 209cc3
			return retval;
Packit 209cc3
		}
Packit 209cc3
	}
Packit 209cc3
Packit 209cc3
	if (getnameinfo((struct sockaddr *) sockaddr, sockaddrlen, address, addrlen, port, portlen, flags) != 0)
Packit 209cc3
	{
Packit 209cc3
		/* If the user wants to receive an error message */
Packit 209cc3
		if (errbuf)
Packit 209cc3
		{
Packit 209cc3
			sock_geterror("getnameinfo()", errbuf, errbuflen);
Packit 209cc3
			errbuf[errbuflen - 1] = 0;
Packit 209cc3
		}
Packit 209cc3
Packit 209cc3
		if (address)
Packit 209cc3
		{
Packit 209cc3
			pcap_strlcpy(address, SOCKET_NO_NAME_AVAILABLE, addrlen);
Packit 209cc3
			address[addrlen - 1] = 0;
Packit 209cc3
		}
Packit 209cc3
Packit 209cc3
		if (port)
Packit 209cc3
		{
Packit 209cc3
			pcap_strlcpy(port, SOCKET_NO_PORT_AVAILABLE, portlen);
Packit 209cc3
			port[portlen - 1] = 0;
Packit 209cc3
		}
Packit 209cc3
Packit 209cc3
		retval = 0;
Packit 209cc3
	}
Packit 209cc3
Packit 209cc3
	return retval;
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * \brief It translates an address from the 'presentation' form into the 'network' form.
Packit 209cc3
 *
Packit 209cc3
 * This function basically replaces inet_pton(), which does not exist in Winsock because
Packit 209cc3
 * the same result can be obtained by using the getaddrinfo().
Packit 209cc3
 * An additional advantage is that 'Address' can be both a numeric address (e.g. '127.0.0.1',
Packit 209cc3
 * like in inet_pton() ) and a literal name (e.g. 'localhost').
Packit 209cc3
 *
Packit 209cc3
 * This function does the reverse job of sock_getascii_addrport().
Packit 209cc3
 *
Packit 209cc3
 * \param address: a zero-terminated string which contains the name you have to
Packit 209cc3
 * translate. The name can be either literal (e.g. 'localhost') or numeric (e.g. '::1').
Packit 209cc3
 *
Packit 209cc3
 * \param sockaddr: a user-allocated sockaddr_storage structure which will contains the
Packit 209cc3
 * 'network' form of the requested address.
Packit 209cc3
 *
Packit 209cc3
 * \param addr_family: a constant which can assume the following values:
Packit 209cc3
 * - 'AF_INET' if we want to ping an IPv4 host
Packit 209cc3
 * - 'AF_INET6' if we want to ping an IPv6 host
Packit 209cc3
 * - 'AF_UNSPEC' if we do not have preferences about the protocol used to ping the host
Packit 209cc3
 *
Packit 209cc3
 * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
Packit 209cc3
 * error message. This buffer has to be at least 'errbuflen' in length.
Packit 209cc3
 * It can be NULL; in this case the error cannot be printed.
Packit 209cc3
 *
Packit 209cc3
 * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
Packit 209cc3
 * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
Packit 209cc3
 *
Packit 209cc3
 * \return '-1' if the translation succeeded, '-2' if there was some non critical error, '0'
Packit 209cc3
 * otherwise. In case it fails, the content of the SockAddr variable remains unchanged.
Packit 209cc3
 * A 'non critical error' can occur in case the 'Address' is a literal name, which can be mapped
Packit 209cc3
 * to several network addresses (e.g. 'foo.bar.com' => '10.2.2.2' and '10.2.2.3'). In this case
Packit 209cc3
 * the content of the SockAddr parameter will be the address corresponding to the first mapping.
Packit 209cc3
 *
Packit 209cc3
 * \warning The sockaddr_storage structure MUST be allocated by the user.
Packit 209cc3
 */
Packit 209cc3
int sock_present2network(const char *address, struct sockaddr_storage *sockaddr, int addr_family, char *errbuf, int errbuflen)
Packit 209cc3
{
Packit 209cc3
	int retval;
Packit 209cc3
	struct addrinfo *addrinfo;
Packit 209cc3
	struct addrinfo hints;
Packit 209cc3
Packit 209cc3
	memset(&hints, 0, sizeof(hints));
Packit 209cc3
Packit 209cc3
	hints.ai_family = addr_family;
Packit 209cc3
Packit 209cc3
	if ((retval = sock_initaddress(address, "22222" /* fake port */, &hints, &addrinfo, errbuf, errbuflen)) == -1)
Packit 209cc3
		return 0;
Packit 209cc3
Packit 209cc3
	if (addrinfo->ai_family == PF_INET)
Packit 209cc3
		memcpy(sockaddr, addrinfo->ai_addr, sizeof(struct sockaddr_in));
Packit 209cc3
	else
Packit 209cc3
		memcpy(sockaddr, addrinfo->ai_addr, sizeof(struct sockaddr_in6));
Packit 209cc3
Packit 209cc3
	if (addrinfo->ai_next != NULL)
Packit 209cc3
	{
Packit 209cc3
		freeaddrinfo(addrinfo);
Packit 209cc3
Packit 209cc3
		if (errbuf)
Packit 209cc3
			pcap_snprintf(errbuf, errbuflen, "More than one socket requested; using the first one returned");
Packit 209cc3
		return -2;
Packit 209cc3
	}
Packit 209cc3
Packit 209cc3
	freeaddrinfo(addrinfo);
Packit 209cc3
	return -1;
Packit 209cc3
}