From 919dc45f2fe31d2b84feeb37f4086760111d4f54 Mon Sep 17 00:00:00 2001 From: Brad House Date: Thu, 16 Mar 2017 18:23:20 -0400 Subject: [PATCH] Portability updates for legacy systems. (#92) Socklen_t should not be used in code, instead ares_socklen_t should be used. Convert ssize_t to ares_ssize_t for portability since the public API now exposes this. --- CMakeLists.txt | 14 ++++++++++++++ ares.h | 6 +++--- ares_build.h.cmake | 2 ++ ares_build.h.dist | 13 +++++++++++++ ares_build.h.in | 6 ++++++ ares_config.h.cmake | 10 ---------- ares_expand_name.c | 2 +- ares_expand_string.c | 2 +- ares_init.c | 4 ++-- ares_nowarn.c | 12 ++++++------ ares_nowarn.h | 4 ++-- ares_process.c | 24 ++++++++++++------------ ares_set_socket_functions.3 | 12 ++++++------ ares_writev.c | 4 ++-- ares_writev.h | 2 +- config-dos.h | 5 ----- config-win32.h | 14 -------------- configure.ac | 8 ++++++-- inet_net_pton.c | 4 ++-- setup_once.h | 10 +++++----- 20 files changed, 84 insertions(+), 74 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cd9d9556..364b6c7c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -407,6 +407,20 @@ ELSE () Set (CARES_TYPEOF_ARES_SOCKLEN_T "int") ENDIF () +IF (HAVE_SSIZE_T) + Set (CARES_TYPEOF_ARES_SSIZE_T "ssize_t") +ELSE () + IF (WIN32) + IF ("${CMAKE_SIZEOF_VOID_P}" EQUAL "8") + Set (CARES_TYPEOF_ARES_SSIZE_T "__int64") + ELSE () + Set (CARES_TYPEOF_ARES_SSIZE_T "int") + ENDIF () + ELSE () + Set (CARES_TYPEOF_ARES_SSIZE_T "long") + ENDIF () +ENDIF () + IF (HAVE_FCNTL AND HAVE_O_NONBLOCK) SET (HAVE_FCNTL_O_NONBLOCK 1) ENDIF () diff --git a/ares.h b/ares.h index dcee80a6..a46c32eb 100644 --- a/ares.h +++ b/ares.h @@ -368,9 +368,9 @@ struct iovec; struct ares_socket_functions { ares_socket_t(*asocket)(int, int, int, void *); int(*aclose)(ares_socket_t, void *); - int(*aconnect)(ares_socket_t, const struct sockaddr *, socklen_t, void *); - ssize_t(*arecvfrom)(ares_socket_t, void *, size_t, int, struct sockaddr *, socklen_t *, void *); - ssize_t(*asendv)(ares_socket_t, const struct iovec *, int, void *); + int(*aconnect)(ares_socket_t, const struct sockaddr *, ares_socklen_t, void *); + ares_ssize_t(*arecvfrom)(ares_socket_t, void *, size_t, int, struct sockaddr *, ares_socklen_t *, void *); + ares_ssize_t(*asendv)(ares_socket_t, const struct iovec *, int, void *); }; CARES_EXTERN void ares_set_socket_functions(ares_channel channel, diff --git a/ares_build.h.cmake b/ares_build.h.cmake index e1e746dd..ac32d47f 100644 --- a/ares_build.h.cmake +++ b/ares_build.h.cmake @@ -2,6 +2,7 @@ #define __CARES_BUILD_H #define CARES_TYPEOF_ARES_SOCKLEN_T @CARES_TYPEOF_ARES_SOCKLEN_T@ +#define CARES_TYPEOF_ARES_SSIZE_T @CARES_TYPEOF_ARES_SSIZE_T@ /* Prefix names with CARES_ to make sure they don't conflict with other config.h * files. We need to include some dependent headers that may be system specific @@ -35,5 +36,6 @@ typedef CARES_TYPEOF_ARES_SOCKLEN_T ares_socklen_t; +typedef CARES_TYPEOF_ARES_SSIZE_T ares_ssize_t; #endif /* __CARES_BUILD_H */ diff --git a/ares_build.h.dist b/ares_build.h.dist index 9a865e34..f94bb296 100644 --- a/ares_build.h.dist +++ b/ares_build.h.dist @@ -191,4 +191,17 @@ typedef CARES_TYPEOF_ARES_SOCKLEN_T ares_socklen_t; #endif +/* Data type definition of ares_ssize_t. */ +#ifdef _WIN32 +# ifdef _WIN64 +# define CARES_TYPEOF_ARES_SSIZE_T __int64 +# else +# define CARES_TYPEOF_ARES_SSIZE_T long +# endif +#else +# define CARES_TYPEOF_ARES_SSIZE_T ssize_t; +#endif + +typedef CARES_TYPEOF_ARES_SSIZE_T ares_ssize_t; + #endif /* __CARES_BUILD_H */ diff --git a/ares_build.h.in b/ares_build.h.in index e5825235..24e3a989 100644 --- a/ares_build.h.in +++ b/ares_build.h.in @@ -91,4 +91,10 @@ /* Data type definition of ares_socklen_t. */ typedef CARES_TYPEOF_ARES_SOCKLEN_T ares_socklen_t; +/* Integral data type used for ares_ssize_t. */ +#undef CARES_TYPEOF_ARES_SSIZE_T + +/* Data type definition of ares_ssize_t. */ +typedef CARES_TYPEOF_ARES_SSIZE_T ares_ssize_t; + #endif /* __CARES_BUILD_H */ diff --git a/ares_config.h.cmake b/ares_config.h.cmake index acbef810..0cb2f6ae 100644 --- a/ares_config.h.cmake +++ b/ares_config.h.cmake @@ -421,13 +421,3 @@ /* Type to use in place of in_addr_t when system does not provide it. */ #undef in_addr_t -#cmakedefine HAVE_SSIZE_T - -/* Create our own ssize_t */ -#ifndef HAVE_SSIZE_T -# ifdef _WIN64 -typedef __int64 ssize_t; -# else -typedef long ssize_t; -# endif -#endif diff --git a/ares_expand_name.c b/ares_expand_name.c index 738be8dc..ec6adc85 100644 --- a/ares_expand_name.c +++ b/ares_expand_name.c @@ -66,7 +66,7 @@ int ares_expand_name(const unsigned char *encoded, const unsigned char *abuf, char *q; const unsigned char *p; union { - ssize_t sig; + ares_ssize_t sig; size_t uns; } nlen; diff --git a/ares_expand_string.c b/ares_expand_string.c index ed5476b9..d35df752 100644 --- a/ares_expand_string.c +++ b/ares_expand_string.c @@ -41,7 +41,7 @@ int ares_expand_string(const unsigned char *encoded, { unsigned char *q; union { - ssize_t sig; + ares_ssize_t sig; size_t uns; } elen; diff --git a/ares_init.c b/ares_init.c index 5591b157..971f8cf0 100644 --- a/ares_init.c +++ b/ares_init.c @@ -84,7 +84,7 @@ static int config_sortlist(struct apattern **sortlist, int *nsort, const char *str); static int sortlist_alloc(struct apattern **sortlist, int *nsort, struct apattern *pat); -static int ip_addr(const char *s, ssize_t len, struct in_addr *addr); +static int ip_addr(const char *s, ares_ssize_t len, struct in_addr *addr); static void natural_mask(struct apattern *pat); #if !defined(WIN32) && !defined(WATT32) && \ !defined(ANDROID) && !defined(__ANDROID__) && !defined(CARES_USE_LIBRESOLV) @@ -2163,7 +2163,7 @@ static char *try_config(char *s, const char *opt, char scc) } #endif /* !WIN32 & !WATT32 & !ANDROID & !__ANDROID__ */ -static int ip_addr(const char *ipbuf, ssize_t len, struct in_addr *addr) +static int ip_addr(const char *ipbuf, ares_ssize_t len, struct in_addr *addr) { /* Four octets and three periods yields at most 15 characters. */ diff --git a/ares_nowarn.c b/ares_nowarn.c index 7f9035c1..f63d9135 100644 --- a/ares_nowarn.c +++ b/ares_nowarn.c @@ -151,10 +151,10 @@ int aresx_sltosi(long slnum) } /* -** signed ssize_t to signed int +** signed ares_ssize_t to signed int */ -int aresx_sztosi(ssize_t sznum) +int aresx_sztosi(ares_ssize_t sznum) { #ifdef __INTEL_COMPILER # pragma warning(push) @@ -162,7 +162,7 @@ int aresx_sztosi(ssize_t sznum) #endif DEBUGASSERT(sznum >= 0); - return (int)(sznum & (ssize_t) CARES_MASK_SINT); + return (int)(sznum & (ares_ssize_t) CARES_MASK_SINT); #ifdef __INTEL_COMPILER # pragma warning(pop) @@ -170,10 +170,10 @@ int aresx_sztosi(ssize_t sznum) } /* -** signed ssize_t to unsigned int +** signed ares_ssize_t to unsigned int */ -unsigned int aresx_sztoui(ssize_t sznum) +unsigned int aresx_sztoui(ares_ssize_t sznum) { #ifdef __INTEL_COMPILER # pragma warning(push) @@ -181,7 +181,7 @@ unsigned int aresx_sztoui(ssize_t sznum) #endif DEBUGASSERT(sznum >= 0); - return (unsigned int)(sznum & (ssize_t) CARES_MASK_UINT); + return (unsigned int)(sznum & (ares_ssize_t) CARES_MASK_UINT); #ifdef __INTEL_COMPILER # pragma warning(pop) diff --git a/ares_nowarn.h b/ares_nowarn.h index 9b76d663..505e6220 100644 --- a/ares_nowarn.h +++ b/ares_nowarn.h @@ -25,9 +25,9 @@ short aresx_sitoss(int sinum); int aresx_sltosi(long slnum); -int aresx_sztosi(ssize_t sznum); +int aresx_sztosi(ares_ssize_t sznum); -unsigned int aresx_sztoui(ssize_t sznum); +unsigned int aresx_sztoui(ares_ssize_t sznum); unsigned short aresx_sitous(int sinum); diff --git a/ares_process.c b/ares_process.c index 2fc04bc8..459a642d 100644 --- a/ares_process.c +++ b/ares_process.c @@ -65,7 +65,7 @@ static void read_tcp_data(ares_channel channel, fd_set *read_fds, static void read_udp_packets(ares_channel channel, fd_set *read_fds, ares_socket_t read_fd, struct timeval *now); static void advance_tcp_send_queue(ares_channel channel, int whichserver, - ssize_t num_bytes); + ares_ssize_t num_bytes); static void process_timeouts(ares_channel channel, struct timeval *now); static void process_broken_connections(ares_channel channel, struct timeval *now); @@ -175,7 +175,7 @@ static int try_again(int errnum) return 0; } -static ssize_t socket_writev(ares_channel channel, ares_socket_t s, const struct iovec * vec, int len) +static ares_ssize_t socket_writev(ares_channel channel, ares_socket_t s, const struct iovec * vec, int len) { if (channel->sock_funcs) return channel->sock_funcs->asendv(s, vec, len, channel->sock_func_cb_data); @@ -183,7 +183,7 @@ static ssize_t socket_writev(ares_channel channel, ares_socket_t s, const struct return writev(s, vec, len); } -static ssize_t socket_write(ares_channel channel, ares_socket_t s, const void * data, size_t len) +static ares_ssize_t socket_write(ares_channel channel, ares_socket_t s, const void * data, size_t len) { if (channel->sock_funcs) { @@ -207,8 +207,8 @@ static void write_tcp_data(ares_channel channel, struct send_request *sendreq; struct iovec *vec; int i; - ssize_t scount; - ssize_t wcount; + ares_ssize_t scount; + ares_ssize_t wcount; size_t n; if(!write_fds && (write_fd == ARES_SOCKET_BAD)) @@ -291,7 +291,7 @@ static void write_tcp_data(ares_channel channel, /* Consume the given number of bytes from the head of the TCP send queue. */ static void advance_tcp_send_queue(ares_channel channel, int whichserver, - ssize_t num_bytes) + ares_ssize_t num_bytes) { struct send_request *sendreq; struct server_state *server = &channel->servers[whichserver]; @@ -319,13 +319,13 @@ static void advance_tcp_send_queue(ares_channel channel, int whichserver, } } -static ssize_t socket_recvfrom(ares_channel channel, +static ares_ssize_t socket_recvfrom(ares_channel channel, ares_socket_t s, void * data, size_t data_len, int flags, struct sockaddr *from, - socklen_t *from_len) + ares_socklen_t *from_len) { if (channel->sock_funcs) return channel->sock_funcs->arecvfrom(s, data, data_len, @@ -339,7 +339,7 @@ static ssize_t socket_recvfrom(ares_channel channel, #endif } -static ssize_t socket_recv(ares_channel channel, +static ares_ssize_t socket_recv(ares_channel channel, ares_socket_t s, void * data, size_t data_len) @@ -360,7 +360,7 @@ static void read_tcp_data(ares_channel channel, fd_set *read_fds, { struct server_state *server; int i; - ssize_t count; + ares_ssize_t count; if(!read_fds && (read_fd == ARES_SOCKET_BAD)) /* no possible action */ @@ -458,7 +458,7 @@ static void read_udp_packets(ares_channel channel, fd_set *read_fds, { struct server_state *server; int i; - ssize_t count; + ares_ssize_t count; unsigned char buf[MAXENDSSZ + 1]; #ifdef HAVE_RECVFROM ares_socklen_t fromlen; @@ -1024,7 +1024,7 @@ static ares_socket_t open_socket(ares_channel channel, int af, int type, int pro static int connect_socket(ares_channel channel, ares_socket_t sockfd, const struct sockaddr * addr, - socklen_t addrlen) + ares_socklen_t addrlen) { if (channel->sock_funcs != 0) return channel->sock_funcs->aconnect(sockfd, diff --git a/ares_set_socket_functions.3 b/ares_set_socket_functions.3 index f40bbb8e..6f6ccbb9 100644 --- a/ares_set_socket_functions.3 +++ b/ares_set_socket_functions.3 @@ -9,9 +9,9 @@ ares_set_socket_functions \- Set socket io callbacks .B struct ares_socket_functions { ares_socket_t(*\fIasocket\fP)(int, int, int, void *); int(*\fIaclose\fP)(ares_socket_t, void *); - int(*\fIaconnect\fP)(ares_socket_t, const struct sockaddr *, socklen_t, void *); - ssize_t(*\fIarecvfrom\fP)(ares_socket_t, void *, size_t, int, struct sockaddr *, socklen_t *, void *); - ssize_t(*\fIasendv\fP)(ares_socket_t, const struct iovec *, int, void *); + int(*\fIaconnect\fP)(ares_socket_t, const struct sockaddr *, ares_socklen_t, void *); + ares_ssize_t(*\fIarecvfrom\fP)(ares_socket_t, void *, size_t, int, struct sockaddr *, ares_socklen_t *, void *); + ares_ssize_t(*\fIasendv\fP)(ares_socket_t, const struct iovec *, int, void *); }; .PP @@ -60,21 +60,21 @@ Closes the socket endpoint indicated by \fIfd\fP. See .BR close(2) .TP 18 .B \fIaconnect\fP -.B int(*)(ares_socket_t \fIfd\fP, const struct sockaddr * \fIaddr\fP, socklen_t \fIaddr_len\fP, void * \fIuser_data\fP) +.B int(*)(ares_socket_t \fIfd\fP, const struct sockaddr * \fIaddr\fP, ares_socklen_t \fIaddr_len\fP, void * \fIuser_data\fP) .br Initiate a connection to the address indicated by \fIaddr\fP on a socket. See .BR connect(2) .TP 18 .B \fIarecvfrom\fP -.B ssize_t(*)(ares_socket_t \fIfd\fP, void * \fIbuffer\fP, size_t \fIbuf_size\fP, int \fIflags\fP, struct sockaddr * \fIaddr\fP, socklen_t * \fIaddr_len\fP, void * \fIuser_data\fP) +.B ares_ssize_t(*)(ares_socket_t \fIfd\fP, void * \fIbuffer\fP, size_t \fIbuf_size\fP, int \fIflags\fP, struct sockaddr * \fIaddr\fP, ares_socklen_t * \fIaddr_len\fP, void * \fIuser_data\fP) .br Receives data from remote socket endpoint, if available. If the \fIaddr\fP parameter is not NULL and the connection protocol provides the source address, the callback should fill this in. See .BR recvfrom(2) .TP 18 .B \fIasendv\fP -.B ssize_t(*)(ares_socket_t \fIfd\fP, const struct iovec * \fIdata\fP, int \fIlen\fP, void * \fIuser_data\fP) +.B ares_ssize_t(*)(ares_socket_t \fIfd\fP, const struct iovec * \fIdata\fP, int \fIlen\fP, void * \fIuser_data\fP) .br Send data, as provided by the iovec array \fIdata\fP, to the socket endpoint. See .BR writev(2), diff --git a/ares_writev.c b/ares_writev.c index 008efddc..e812c09e 100644 --- a/ares_writev.c +++ b/ares_writev.c @@ -25,12 +25,12 @@ #include "ares_private.h" #ifndef HAVE_WRITEV -ssize_t ares_writev(ares_socket_t s, const struct iovec *iov, int iovcnt) +ares_ssize_t ares_writev(ares_socket_t s, const struct iovec *iov, int iovcnt) { char *buffer, *bp; int i; size_t bytes = 0; - ssize_t result; + ares_ssize_t result; /* Validate iovcnt */ if (iovcnt <= 0) diff --git a/ares_writev.h b/ares_writev.h index 1a23a0f3..65cea870 100644 --- a/ares_writev.h +++ b/ares_writev.h @@ -29,7 +29,7 @@ struct iovec size_t iov_len; /* Length of data. */ }; -extern ssize_t ares_writev(ares_socket_t s, const struct iovec *iov, int iovcnt); +extern ares_ssize_t ares_writev(ares_socket_t s, const struct iovec *iov, int iovcnt); #endif diff --git a/config-dos.h b/config-dos.h index b895987d..114db511 100644 --- a/config-dos.h +++ b/config-dos.h @@ -60,11 +60,6 @@ #define BSD -#if defined(__HIGHC__) || \ - (defined(__GNUC__) && (__GNUC__ < 4)) - #define ssize_t int -#endif - /* Target HAVE_x section */ #if defined(DJGPP) diff --git a/config-win32.h b/config-win32.h index e1579b1e..8d704745 100644 --- a/config-win32.h +++ b/config-win32.h @@ -216,20 +216,6 @@ #define HAVE_BOOL_T #endif -/* Define if ssize_t is not an available 'typedefed' type. */ -#ifndef _SSIZE_T_DEFINED -# if (defined(__WATCOMC__) && (__WATCOMC__ >= 1240)) || \ - defined(__POCC__) || \ - defined(__MINGW32__) -# elif defined(_WIN64) -# define _SSIZE_T_DEFINED -# define ssize_t __int64 -# else -# define _SSIZE_T_DEFINED -# define ssize_t int -# endif -#endif - /* ---------------------------------------------------------------- */ /* TYPE SIZES */ /* ---------------------------------------------------------------- */ diff --git a/configure.ac b/configure.ac index bd8a1b8b..f78a0f78 100644 --- a/configure.ac +++ b/configure.ac @@ -557,8 +557,12 @@ fi # check for ssize_t -AC_CHECK_TYPE(ssize_t, , - AC_DEFINE(ssize_t, int, [the signed version of size_t])) +AC_CHECK_TYPE(ssize_t, [ CARES_TYPEOF_ARES_SSIZE_T=ssize_t ], + [ CARES_TYPEOF_ARES_SSIZE_T=int ]) + +AC_DEFINE_UNQUOTED([CARES_TYPEOF_ARES_SSIZE_T], ${CARES_TYPEOF_ARES_SSIZE_T}, + [the signed version of size_t]) + # check for bool type AC_CHECK_TYPE([bool],[ diff --git a/inet_net_pton.c b/inet_net_pton.c index b64fc5b1..af1a534a 100644 --- a/inet_net_pton.c +++ b/inet_net_pton.c @@ -357,8 +357,8 @@ inet_net_pton_ipv6(const char *src, unsigned char *dst, size_t size) * Since some memmove()'s erroneously fail to handle * overlapping regions, we'll do the shift by hand. */ - const ssize_t n = tp - colonp; - ssize_t i; + const ares_ssize_t n = tp - colonp; + ares_ssize_t i; if (tp == endp) goto enoent; diff --git a/setup_once.h b/setup_once.h index 25b144ae..a8cfe6be 100644 --- a/setup_once.h +++ b/setup_once.h @@ -131,7 +131,7 @@ struct timeval { #if defined(__minix) /* Minix doesn't support recv on TCP sockets */ -#define sread(x,y,z) (ssize_t)read((RECV_TYPE_ARG1)(x), \ +#define sread(x,y,z) (ares_ssize_t)read((RECV_TYPE_ARG1)(x), \ (RECV_TYPE_ARG2)(y), \ (RECV_TYPE_ARG3)(z)) @@ -167,7 +167,7 @@ struct timeval { Error Missing_definition_of_return_and_arguments_types_of_recv /* */ #else -#define sread(x,y,z) (ssize_t)recv((RECV_TYPE_ARG1)(x), \ +#define sread(x,y,z) (ares_ssize_t)recv((RECV_TYPE_ARG1)(x), \ (RECV_TYPE_ARG2)(y), \ (RECV_TYPE_ARG3)(z), \ (RECV_TYPE_ARG4)(0)) @@ -183,7 +183,7 @@ struct timeval { #if defined(__minix) /* Minix doesn't support send on TCP sockets */ -#define swrite(x,y,z) (ssize_t)write((SEND_TYPE_ARG1)(x), \ +#define swrite(x,y,z) (ares_ssize_t)write((SEND_TYPE_ARG1)(x), \ (SEND_TYPE_ARG2)(y), \ (SEND_TYPE_ARG3)(z)) @@ -198,7 +198,7 @@ struct timeval { Error Missing_definition_of_return_and_arguments_types_of_send /* */ #else -#define swrite(x,y,z) (ssize_t)send((SEND_TYPE_ARG1)(x), \ +#define swrite(x,y,z) (ares_ssize_t)send((SEND_TYPE_ARG1)(x), \ (SEND_TYPE_ARG2)(y), \ (SEND_TYPE_ARG3)(z), \ (SEND_TYPE_ARG4)(SEND_4TH_ARG)) @@ -228,7 +228,7 @@ struct timeval { Error Missing_definition_of_return_and_arguments_types_of_recvfrom /* */ #else -#define sreadfrom(s,b,bl,f,fl) (ssize_t)recvfrom((RECVFROM_TYPE_ARG1) (s), \ +#define sreadfrom(s,b,bl,f,fl) (ares_ssize_t)recvfrom((RECVFROM_TYPE_ARG1) (s), \ (RECVFROM_TYPE_ARG2 *)(b), \ (RECVFROM_TYPE_ARG3) (bl), \ (RECVFROM_TYPE_ARG4) (0), \