aboutsummaryrefslogtreecommitdiff
path: root/sockutils.c
diff options
context:
space:
mode:
authorJoseph Mingrone <jrm@FreeBSD.org>2024-09-05 19:46:35 +0000
committerJoseph Mingrone <jrm@FreeBSD.org>2024-09-05 19:46:35 +0000
commit025be3f592b920ff0a3e602e5aa8b60a34e5e617 (patch)
tree4f33277584e4a38c8bad5c2788584a00cf830ea4 /sockutils.c
parent6b96668d5b49eea57b7551349eca70928cc199ce (diff)
Diffstat (limited to 'sockutils.c')
-rw-r--r--sockutils.c123
1 files changed, 64 insertions, 59 deletions
diff --git a/sockutils.c b/sockutils.c
index 1c07f76fd1c0..e1cc4e3066b4 100644
--- a/sockutils.c
+++ b/sockutils.c
@@ -30,9 +30,7 @@
*
*/
-#ifdef HAVE_CONFIG_H
#include <config.h>
-#endif
/*
* \file sockutils.c
@@ -163,10 +161,10 @@ void sock_vfmterrmsg(char *errbuf, size_t errbuflen, int errcode,
return;
#ifdef _WIN32
- pcap_vfmt_errmsg_for_win32_err(errbuf, errbuflen, errcode,
+ pcapint_vfmt_errmsg_for_win32_err(errbuf, errbuflen, errcode,
fmt, ap);
#else
- pcap_vfmt_errmsg_for_errno(errbuf, errbuflen, errcode,
+ pcapint_vfmt_errmsg_for_errno(errbuf, errbuflen, errcode,
fmt, ap);
#endif
}
@@ -199,7 +197,7 @@ void sock_geterrmsg(char *errbuf, size_t errbuflen, const char *fmt, ...)
* These are sorted by how likely they are to be the "underlying" problem,
* so that lower-rated errors for a given address in a given family
* should not overwrite higher-rated errors for another address in that
- * family, and higher-rated errors should overwrit elower-rated errors.
+ * family, and higher-rated errors should overwrite lower-rated errors.
*/
typedef enum {
SOCK_CONNERR, /* connection error */
@@ -230,7 +228,7 @@ static sock_errtype sock_geterrtype(int errcode)
* and we're trying the wrong address family.
*
* These overwrite all other errors, as they indicate
- * that, even if somethng else went wrong in another
+ * that, even if something else went wrong in another
* attempt, this probably wouldn't work even if the
* other problems were fixed.
*/
@@ -334,9 +332,6 @@ int sock_init(char *errbuf, int errbuflen)
{
if (errbuf)
snprintf(errbuf, errbuflen, "Failed to initialize Winsock\n");
-
- WSACleanup();
-
return -1;
}
}
@@ -432,10 +427,10 @@ static int compare_addrs_to_try_by_status(const void *a, const void *b)
return addr_a->errtype - addr_b->errtype;
}
-static SOCKET sock_create_socket(struct addrinfo *addrinfo, char *errbuf,
+static PCAP_SOCKET sock_create_socket(struct addrinfo *addrinfo, char *errbuf,
int errbuflen)
{
- SOCKET sock;
+ PCAP_SOCKET sock;
#ifdef SO_NOSIGPIPE
int on = 1;
#endif
@@ -501,9 +496,10 @@ static SOCKET sock_create_socket(struct addrinfo *addrinfo, char *errbuf,
* if everything is fine, INVALID_SOCKET if some errors occurred. The error message is returned
* in the 'errbuf' variable.
*/
-SOCKET sock_open(const char *host, struct addrinfo *addrinfo, int server, int nconn, char *errbuf, int errbuflen)
+PCAP_SOCKET sock_open(const char *host, struct addrinfo *addrinfo,
+ int server, int nconn, char *errbuf, int errbuflen)
{
- SOCKET sock;
+ PCAP_SOCKET sock;
/* This is a server socket */
if (server)
@@ -872,7 +868,7 @@ SOCKET sock_open(const char *host, struct addrinfo *addrinfo, int server, int nc
* \return '0' if everything is fine, '-1' if some errors occurred. The error message is returned
* in the 'errbuf' variable.
*/
-int sock_close(SOCKET sock, char *errbuf, int errbuflen)
+int sock_close(PCAP_SOCKET sock, char *errbuf, int errbuflen)
{
/*
* SHUT_WR: subsequent calls to the send function are disallowed.
@@ -881,7 +877,7 @@ int sock_close(SOCKET sock, char *errbuf, int errbuflen)
*/
if (shutdown(sock, SHUT_WR))
{
- sock_geterrmsg(errbuf, errbuflen, "shutdown() feiled");
+ sock_geterrmsg(errbuf, errbuflen, "shutdown() failed");
/* close the socket anyway */
closesocket(sock);
return -1;
@@ -1004,7 +1000,7 @@ get_gai_errstring(char *errbuf, int errbuflen, const char *prefix, int err,
/*
* Assumed to be UN*X.
*/
- pcap_fmt_errmsg_for_errno(errbuf, errbuflen, errno,
+ pcapint_fmt_errmsg_for_errno(errbuf, errbuflen, errno,
"%sAn error occurred when looking up %s",
prefix, hostport);
break;
@@ -1043,7 +1039,7 @@ get_gai_errstring(char *errbuf, int errbuflen, const char *prefix, int err,
}
/*
- * \brief Checks that the address, port and flags given are valids and it returns an 'addrinfo' structure.
+ * \brief Checks that the address, port and flags given are valid and it returns an 'addrinfo' structure.
*
* This function basically calls the getaddrinfo() calls, and it performs a set of sanity checks
* to control that everything is fine (e.g. a TCP socket cannot have a mcast address, and such).
@@ -1069,20 +1065,21 @@ get_gai_errstring(char *errbuf, int errbuflen, const char *prefix, int err,
* \param errbuflen: length of the buffer that will contains the error. The error message cannot be
* larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
*
- * \return '0' if everything is fine, '-1' if some errors occurred. The error message is returned
- * in the 'errbuf' variable. The addrinfo variable that has to be used in the following sockets calls is
- * returned into the addrinfo parameter.
+ * \return a pointer to the first element in a list of addrinfo structures
+ * if everything is fine, NULL if some errors occurred. The error message
+ * is returned in the 'errbuf' variable.
*
- * \warning The 'addrinfo' variable has to be deleted by the programmer by calling freeaddrinfo() when
- * it is no longer needed.
+ * \warning The list of addrinfo structures returned has to be deleted by
+ * the programmer by calling freeaddrinfo() when it is no longer needed.
*
* \warning This function requires the 'hints' variable as parameter. The semantic of this variable is the same
* of the one of the corresponding variable used into the standard getaddrinfo() socket function. We suggest
* the programmer to look at that function in order to set the 'hints' variable appropriately.
*/
-int sock_initaddress(const char *host, const char *port,
- struct addrinfo *hints, struct addrinfo **addrinfo, char *errbuf, int errbuflen)
+struct addrinfo *sock_initaddress(const char *host, const char *port,
+ struct addrinfo *hints, char *errbuf, int errbuflen)
{
+ struct addrinfo *addrinfo;
int retval;
/*
@@ -1094,9 +1091,13 @@ int sock_initaddress(const char *host, const char *port,
* as those messages won't talk about a problem with the port if
* no port was specified.
*/
- retval = getaddrinfo(host, port == NULL ? "0" : port, hints, addrinfo);
+ retval = getaddrinfo(host, port == NULL ? "0" : port, hints, &addrinfo);
if (retval != 0)
{
+ /*
+ * That call failed.
+ * Determine whether the problem is that the host is bad.
+ */
if (errbuf)
{
if (host != NULL && port != NULL) {
@@ -1108,7 +1109,7 @@ int sock_initaddress(const char *host, const char *port,
int try_retval;
try_retval = getaddrinfo(host, NULL, hints,
- addrinfo);
+ &addrinfo);
if (try_retval == 0) {
/*
* Worked with just the host,
@@ -1117,14 +1118,16 @@ int sock_initaddress(const char *host, const char *port,
*
* Free up the address info first.
*/
- freeaddrinfo(*addrinfo);
+ freeaddrinfo(addrinfo);
get_gai_errstring(errbuf, errbuflen,
"", retval, NULL, port);
} else {
/*
* Didn't work with just the host,
* so assume the problem is
- * with the host.
+ * with the host; we assume
+ * the original error indicates
+ * the underlying problem.
*/
get_gai_errstring(errbuf, errbuflen,
"", retval, host, NULL);
@@ -1132,13 +1135,14 @@ int sock_initaddress(const char *host, const char *port,
} else {
/*
* Either the host or port was null, so
- * there's nothing to determine.
+ * there's nothing to determine; report
+ * the error from the original call.
*/
get_gai_errstring(errbuf, errbuflen, "",
retval, host, port);
}
}
- return -1;
+ return NULL;
}
/*
* \warning SOCKET: I should check all the accept() in order to bind to all addresses in case
@@ -1153,30 +1157,28 @@ int sock_initaddress(const char *host, const char *port,
* ignore all addresses that are neither? (What, no IPX
* support? :-))
*/
- if (((*addrinfo)->ai_family != PF_INET) &&
- ((*addrinfo)->ai_family != PF_INET6))
+ if ((addrinfo->ai_family != PF_INET) &&
+ (addrinfo->ai_family != PF_INET6))
{
if (errbuf)
snprintf(errbuf, errbuflen, "getaddrinfo(): socket type not supported");
- freeaddrinfo(*addrinfo);
- *addrinfo = NULL;
- return -1;
+ freeaddrinfo(addrinfo);
+ return NULL;
}
/*
* You can't do multicast (or broadcast) TCP.
*/
- if (((*addrinfo)->ai_socktype == SOCK_STREAM) &&
- (sock_ismcastaddr((*addrinfo)->ai_addr) == 0))
+ if ((addrinfo->ai_socktype == SOCK_STREAM) &&
+ (sock_ismcastaddr(addrinfo->ai_addr) == 0))
{
if (errbuf)
snprintf(errbuf, errbuflen, "getaddrinfo(): multicast addresses are not valid when using TCP streams");
- freeaddrinfo(*addrinfo);
- *addrinfo = NULL;
- return -1;
+ freeaddrinfo(addrinfo);
+ return NULL;
}
- return 0;
+ return addrinfo;
}
/*
@@ -1206,8 +1208,8 @@ int sock_initaddress(const char *host, const char *port,
* '-2' if we got one of those errors.
* For errors, an error message is returned in the 'errbuf' variable.
*/
-int sock_send(SOCKET sock, SSL *ssl _U_NOSSL_, const char *buffer, size_t size,
- char *errbuf, int errbuflen)
+int sock_send(PCAP_SOCKET sock, SSL *ssl _U_NOSSL_, const char *buffer,
+ size_t size, char *errbuf, int errbuflen)
{
int remaining;
ssize_t nsent;
@@ -1327,7 +1329,7 @@ int sock_send(SOCKET sock, SSL *ssl _U_NOSSL_, const char *buffer, size_t size,
* \param totsize: total size of the buffer into which data is being copied.
*
* \param checkonly: '1' if we do not want to copy data into the buffer and we
- * want just do a buffer ovreflow control, '0' if data has to be copied as well.
+ * want just do a buffer overflow control, '0' if data has to be copied as well.
*
* \param errbuf: a pointer to an user-allocated buffer that will contain the complete
* error message. This buffer has to be at least 'errbuflen' in length.
@@ -1410,7 +1412,7 @@ int sock_bufferize(const void *data, int size, char *outbuf, int *offset, int to
* The error message is returned in the 'errbuf' variable.
*/
-int sock_recv(SOCKET sock, SSL *ssl _U_NOSSL_, void *buffer, size_t size,
+int sock_recv(PCAP_SOCKET sock, SSL *ssl _U_NOSSL_, void *buffer, size_t size,
int flags, char *errbuf, int errbuflen)
{
int recv_flags = 0;
@@ -1517,8 +1519,8 @@ int sock_recv(SOCKET sock, SSL *ssl _U_NOSSL_, void *buffer, size_t size,
*
* Returns the size of the datagram on success or -1 on error.
*/
-int sock_recv_dgram(SOCKET sock, SSL *ssl _U_NOSSL_, void *buffer, size_t size,
- char *errbuf, int errbuflen)
+int sock_recv_dgram(PCAP_SOCKET sock, SSL *ssl _U_NOSSL_, void *buffer,
+ size_t size, char *errbuf, int errbuflen)
{
ssize_t nread;
#ifndef _WIN32
@@ -1665,7 +1667,8 @@ int sock_recv_dgram(SOCKET sock, SSL *ssl _U_NOSSL_, void *buffer, size_t size,
* \return '0' if everything is fine, '-1' if some errors occurred.
* The error message is returned in the 'errbuf' variable.
*/
-int sock_discard(SOCKET sock, SSL *ssl, int size, char *errbuf, int errbuflen)
+int sock_discard(PCAP_SOCKET sock, SSL *ssl, int size, char *errbuf,
+ int errbuflen)
{
#define TEMP_BUF_SIZE 32768
@@ -1728,7 +1731,7 @@ int sock_discard(SOCKET sock, SSL *ssl, int size, char *errbuf, int errbuflen)
* - '-1' in case the host does not belong to the host list (and therefore it is not allowed to connect
* - '-2' in case or error. The error message is returned in the 'errbuf' variable.
*/
-int sock_check_hostlist(char *hostlist, const char *sep, struct sockaddr_storage *from, char *errbuf, int errbuflen)
+int sock_check_hostlist(const char *hostlist, const char *sep, struct sockaddr_storage *from, char *errbuf, int errbuflen)
{
/* checks if the connecting host is among the ones allowed */
if ((hostlist) && (hostlist[0]))
@@ -1751,7 +1754,7 @@ int sock_check_hostlist(char *hostlist, const char *sep, struct sockaddr_storage
return -2;
}
- token = pcap_strtok_r(temphostlist, sep, &lasts);
+ token = pcapint_strtok_r(temphostlist, sep, &lasts);
/* it avoids a warning in the compilation ('addrinfo used but not initialized') */
addrinfo = NULL;
@@ -1781,7 +1784,7 @@ int sock_check_hostlist(char *hostlist, const char *sep, struct sockaddr_storage
getaddrinfo_failed = 1;
/* Get next token */
- token = pcap_strtok_r(NULL, sep, &lasts);
+ token = pcapint_strtok_r(NULL, sep, &lasts);
continue;
}
@@ -1807,7 +1810,7 @@ int sock_check_hostlist(char *hostlist, const char *sep, struct sockaddr_storage
addrinfo = NULL;
/* Get next token */
- token = pcap_strtok_r(NULL, sep, &lasts);
+ token = pcapint_strtok_r(NULL, sep, &lasts);
}
if (addrinfo)
@@ -1848,7 +1851,7 @@ int sock_check_hostlist(char *hostlist, const char *sep, struct sockaddr_storage
* i.e. an sockaddr_storage structure.
*
* The two structures do not need to be sockaddr_storage; you can have both 'sockaddr_in' and
- * sockaddr_in6, properly acsted in order to be compliant to the function interface.
+ * sockaddr_in6, properly casted in order to be compliant to the function interface.
*
* This function will return '0' if the two addresses matches, '-1' if not.
*
@@ -1923,7 +1926,8 @@ int sock_cmpaddr(struct sockaddr_storage *first, struct sockaddr_storage *second
* \warning If the socket is using a connectionless protocol, the address may not be available
* until I/O occurs on the socket.
*/
-int sock_getmyinfo(SOCKET sock, char *address, int addrlen, char *port, int portlen, int flags, char *errbuf, int errbuflen)
+int sock_getmyinfo(PCAP_SOCKET sock, char *address, int addrlen, char *port,
+ int portlen, int flags, char *errbuf, int errbuflen)
{
struct sockaddr_storage mysockaddr;
socklen_t sockaddrlen;
@@ -2011,7 +2015,7 @@ int sock_getascii_addrport(const struct sockaddr_storage *sockaddr, char *addres
(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))
{
if (address)
- pcap_strlcpy(address, SOCKET_NAME_NULL_DAD, addrlen);
+ pcapint_strlcpy(address, SOCKET_NAME_NULL_DAD, addrlen);
return retval;
}
}
@@ -2028,13 +2032,13 @@ int sock_getascii_addrport(const struct sockaddr_storage *sockaddr, char *addres
if (address)
{
- pcap_strlcpy(address, SOCKET_NO_NAME_AVAILABLE, addrlen);
+ pcapint_strlcpy(address, SOCKET_NO_NAME_AVAILABLE, addrlen);
address[addrlen - 1] = 0;
}
if (port)
{
- pcap_strlcpy(port, SOCKET_NO_PORT_AVAILABLE, portlen);
+ pcapint_strlcpy(port, SOCKET_NO_PORT_AVAILABLE, portlen);
port[portlen - 1] = 0;
}
@@ -2082,7 +2086,6 @@ int sock_getascii_addrport(const struct sockaddr_storage *sockaddr, char *addres
*/
int sock_present2network(const char *address, struct sockaddr_storage *sockaddr, int addr_family, char *errbuf, int errbuflen)
{
- int retval;
struct addrinfo *addrinfo;
struct addrinfo hints;
@@ -2090,7 +2093,9 @@ int sock_present2network(const char *address, struct sockaddr_storage *sockaddr,
hints.ai_family = addr_family;
- if ((retval = sock_initaddress(address, "22222" /* fake port */, &hints, &addrinfo, errbuf, errbuflen)) == -1)
+ addrinfo = sock_initaddress(address, "22222" /* fake port */, &hints,
+ errbuf, errbuflen);
+ if (addrinfo == NULL)
return 0;
if (addrinfo->ai_family == PF_INET)