diff --git a/configure.ac b/configure.ac index ca8c6d81..49fa72d5 100644 --- a/configure.ac +++ b/configure.ac @@ -589,7 +589,7 @@ case "$enable_ed25519" in no) ;; *) - if test $USE_NSS = "no" -a $USE_NETTLE = "no"; then + if test "$USE_NSS" = "no" -a "$USE_NETTLE" = "no"; then AC_CHECK_DECLS([NID_ED25519], [ AC_DEFINE_UNQUOTED([USE_ED25519], [1], [Define this to enable ED25519 support.]) use_ed25519="yes" @@ -1274,7 +1274,7 @@ CFLAGS="$CFLAGS $LIBBSD_CFLAGS" ],[ AC_MSG_WARN([libbsd not found or usable; using embedded code instead]) ]) -AC_CHECK_DECLS([strlcpy,arc4random,arc4random_uniform]) +AC_CHECK_DECLS([inet_pton,inet_ntop,strlcpy,arc4random,arc4random_uniform]) AC_REPLACE_FUNCS(inet_pton) AC_REPLACE_FUNCS(inet_ntop) AC_REPLACE_FUNCS(strlcpy) @@ -1449,11 +1449,11 @@ void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*); unsigned char *SHA512(void* data, unsigned int data_len, unsigned char *digest); #endif /* COMPAT_SHA512 */ -#ifndef HAVE_INET_PTON +#ifndef HAVE_DECL_INET_PTON int inet_pton(int af, const char* src, void* dst); #endif /* HAVE_INET_PTON */ -#ifndef HAVE_INET_NTOP +#ifndef HAVE_DECL_INET_NTOP const char *inet_ntop(int af, const void *src, char *dst, size_t size); #endif diff --git a/src/anchor.c b/src/anchor.c index d37cdec4..a5a31b2e 100644 --- a/src/anchor.c +++ b/src/anchor.c @@ -50,6 +50,7 @@ #include "gldns/keyraw.h" #include "general.h" #include "util-internal.h" +#include "platform.h" /* get key usage out of its extension, returns 0 if no key_usage extension */ static unsigned long @@ -1198,7 +1199,7 @@ static void tas_read_cb(void *userarg) return; } } - } else if (_getdns_EWOULDBLOCK) + } else if (_getdns_socketerror() == _getdns_EWOULDBLOCK) return; DEBUG_ANCHOR("Read error: %d %s\n", (int)n, strerror(errno)); @@ -1248,7 +1249,7 @@ static void tas_write_cb(void *userarg) tas_read_cb, NULL, tas_timeout_cb)); return; - } else if (_getdns_EWOULDBLOCK || _getdns_EINPROGRESS) + } else if (_getdns_socketerror() == _getdns_EWOULDBLOCK || _getdns_socketerror() == _getdns_EINPROGRESS) return; DEBUG_ANCHOR("Write error: %s\n", strerror(errno)); @@ -1348,8 +1349,8 @@ static void tas_connect(getdns_context *context, tas_connection *a) addr.sin6_scope_id = 0; r = connect(a->fd, (struct sockaddr *)&addr, sizeof(addr)); } - if (r == 0 || (r == -1 && (_getdns_EINPROGRESS || - _getdns_EWOULDBLOCK))) { + if (r == 0 || (r == -1 && (_getdns_socketerror() == _getdns_EINPROGRESS || + _getdns_socketerror() == _getdns_EWOULDBLOCK))) { char tas_hostname[256]; const char *path = "", *fmt; getdns_return_t R; diff --git a/src/context.c b/src/context.c index ada3e7bd..3456bfa0 100644 --- a/src/context.c +++ b/src/context.c @@ -80,6 +80,7 @@ typedef unsigned short in_port_t; #include "context.h" #include "types-internal.h" #include "util-internal.h" +#include "platform.h" #include "dnssec.h" #include "stub.h" #include "list.h" @@ -704,11 +705,7 @@ _getdns_upstreams_dereference(getdns_upstreams *upstreams) } if (upstream->fd != -1) { -#ifdef USE_WINSOCK - closesocket(upstream->fd); -#else - close(upstream->fd); -#endif + _getdns_closesocket(upstream->fd); } while (pin) { sha256_pin_t *nextpin = pin->next; @@ -809,11 +806,7 @@ _getdns_upstream_reset(getdns_upstream *upstream) upstream->tls_obj = NULL; } if (upstream->fd != -1) { -#ifdef USE_WINSOCK - closesocket(upstream->fd); -#else - close(upstream->fd); -#endif + _getdns_closesocket(upstream->fd); upstream->fd = -1; } /* Set connection ready for use again*/ diff --git a/src/extension/poll_eventloop.c b/src/extension/poll_eventloop.c index b4f204b1..7d6bb99c 100644 --- a/src/extension/poll_eventloop.c +++ b/src/extension/poll_eventloop.c @@ -27,13 +27,8 @@ #include "config.h" -#ifdef HAVE_SYS_POLL_H -#include -#else -#ifndef USE_WINSOCK -#include -#endif -#endif +#include "util-internal.h" +#include "platform.h" #ifdef HAVE_SYS_RESOURCE_H #include #endif @@ -411,10 +406,8 @@ poll_eventloop_run_once(getdns_eventloop *loop, int blocking) { Sleep(poll_timeout); } else - if (WSAPoll(poll_loop->pfds, poll_loop->fd_events_free, poll_timeout) < 0) { -#else - if (poll(poll_loop->pfds, poll_loop->fd_events_free, poll_timeout) < 0) { #endif + if (_getdns_poll(poll_loop->pfds, poll_loop->fd_events_free, poll_timeout) < 0) { perror("poll() failed"); exit(EXIT_FAILURE); } diff --git a/src/mdns.c b/src/mdns.c index 073c48cb..30bdab6e 100644 --- a/src/mdns.c +++ b/src/mdns.c @@ -26,28 +26,11 @@ #include "gldns/pkthdr.h" #include "gldns/rrdef.h" #include "util-internal.h" +#include "platform.h" #include "mdns.h" #ifdef HAVE_MDNS_SUPPORT -#ifdef USE_WINSOCK -typedef u_short sa_family_t; -#define _getdns_EWOULDBLOCK (WSAGetLastError() == WSATRY_AGAIN ||\ - WSAGetLastError() == WSAEWOULDBLOCK) -#define _getdns_EINPROGRESS (WSAGetLastError() == WSAEINPROGRESS) -#else -#define _getdns_EWOULDBLOCK (errno == EAGAIN || errno == EWOULDBLOCK) -#define _getdns_EINPROGRESS (errno == EINPROGRESS) -#define SOCKADDR struct sockaddr -#define SOCKADDR_IN struct sockaddr_in -#define SOCKADDR_IN6 struct sockaddr_in6 -#define SOCKET int -#define IP_MREQ struct ip_mreq -#define IPV6_MREQ struct ipv6_mreq -#define BOOL int -#define TRUE 1 -#endif - /* Define IPV6_ADD_MEMBERSHIP for FreeBSD and Mac OS X */ #ifndef IPV6_ADD_MEMBERSHIP #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP @@ -1148,7 +1131,8 @@ mdns_udp_multicast_read_cb(void *userarg) sizeof(cnx->response), 0, NULL, NULL); - if (read == -1 && _getdns_EWOULDBLOCK) + if (read == -1 && (_getdns_socketerror() == _getdns_EWOULDBLOCK || + _getdns_socketerror() == _getdns_ECONNRESET)) return; /* TODO: this will stop the receive loop! */ if (read >= GLDNS_HEADER_SIZE) @@ -1353,11 +1337,7 @@ static int mdns_open_ipv4_multicast(SOCKADDR_STORAGE* mcast_dest, int* mcast_des if (ret != 0 && fd4 != -1) { -#ifdef USE_WINSOCK - closesocket(fd4); -#else - close(fd4); -#endif + _getdns_closesocket(fd4); fd4 = -1; } @@ -1428,11 +1408,7 @@ static int mdns_open_ipv6_multicast(SOCKADDR_STORAGE* mcast_dest, int* mcast_des if (ret != 0 && fd6 != -1) { -#ifdef USE_WINSOCK - closesocket(fd6); -#else - close(fd6); -#endif + _getdns_closesocket(fd6); fd6 = -1; } @@ -1514,11 +1490,7 @@ static getdns_return_t mdns_delayed_network_init(struct getdns_context *context) GETDNS_CLEAR_EVENT(context->extension , &context->mdns_connection[i].event); -#ifdef USE_WINSOCK - closesocket(context->mdns_connection[i].fd); -#else - close(context->mdns_connection[i].fd); -#endif + _getdns_closesocket(context->mdns_connection[i].fd); } } @@ -1657,11 +1629,7 @@ void _getdns_mdns_context_destroy(struct getdns_context *context) /* suppress the receive event */ GETDNS_CLEAR_EVENT(context->extension, &context->mdns_connection[i].event); /* close the socket */ -#ifdef USE_WINSOCK - closesocket(context->mdns_connection[i].fd); -#else - close(context->mdns_connection[i].fd); -#endif + _getdns_closesocket(context->mdns_connection[i].fd); } GETDNS_FREE(context->mf, context->mdns_connection); @@ -1686,11 +1654,7 @@ _getdns_cancel_mdns_request(getdns_network_req *netreq) { mdns_cleanup(netreq); if (netreq->fd >= 0) { -#ifdef USE_WINSOCK - closesocket(netreq->fd); -#else - close(netreq->fd); -#endif + _getdns_closesocket(netreq->fd); } } @@ -1706,11 +1670,7 @@ mdns_timeout_cb(void *userarg) /* Check the required cleanup */ mdns_cleanup(netreq); if (netreq->fd >= 0) -#ifdef USE_WINSOCK - closesocket(netreq->fd); -#else - close(netreq->fd); -#endif + _getdns_closesocket(netreq->fd); _getdns_netreq_change_state(netreq, NET_REQ_TIMED_OUT); if (netreq->owner->user_callback) { netreq->debug_end_time = _getdns_get_time_as_uintt64(); @@ -1745,7 +1705,8 @@ mdns_udp_read_cb(void *userarg) * i.e. overflow */ 0, NULL, NULL); - if (read == -1 && _getdns_EWOULDBLOCK) + if (read == -1 && (_getdns_socketerror() == _getdns_EWOULDBLOCK || + _getdns_socketerror() == _getdns_ECONNRESET)) return; if (read < GLDNS_HEADER_SIZE) @@ -1759,11 +1720,7 @@ mdns_udp_read_cb(void *userarg) // TODO: check that the source address originates from the local network. // TODO: check TTL = 255 -#ifdef USE_WINSOCK - closesocket(netreq->fd); -#else - close(netreq->fd); -#endif + _getdns_closesocket(netreq->fd); /* * TODO: how to handle an MDNS response with TC bit set? * Ignore it for now, as we do not support any kind of TCP fallback @@ -1814,11 +1771,7 @@ mdns_udp_write_cb(void *userarg) netreq->fd, (const void *)netreq->query, pkt_len, 0, (struct sockaddr *)&mdns_mcast_v4, sizeof(mdns_mcast_v4))) { -#ifdef USE_WINSOCK - closesocket(netreq->fd); -#else - close(netreq->fd); -#endif + _getdns_closesocket(netreq->fd); return; } GETDNS_SCHEDULE_EVENT( diff --git a/src/mdns.h b/src/mdns.h index b7c7d20c..79ec05fe 100644 --- a/src/mdns.h +++ b/src/mdns.h @@ -24,13 +24,11 @@ #ifdef HAVE_MDNS_SUPPORT #include "getdns/getdns.h" #include "types-internal.h" +#include "util-internal.h" +#include "platform.h" #include "util/lruhash.h" #include "config.h" -#ifndef USE_WINSOCK -#define SOCKADDR_STORAGE struct sockaddr_storage -#endif - getdns_return_t _getdns_submit_mdns_request(getdns_network_req *netreq); diff --git a/src/platform.h b/src/platform.h new file mode 100644 index 00000000..0cd4c9f6 --- /dev/null +++ b/src/platform.h @@ -0,0 +1,81 @@ +/** + * + * /brief general functions with platform-dependent implementations + * + */ + +/* + * Copyright (c) 2017, NLnet Labs, Sinodun + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the names of the copyright holders nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Verisign, Inc. BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef PLATFORM_H +#define PLATFORM_H + +#include "config.h" + +#ifdef USE_WINSOCK +typedef u_short sa_family_t; +#define _getdns_EAGAIN (WSATRY_AGAIN) +#define _getdns_EWOULDBLOCK (WSAEWOULDBLOCK) +#define _getdns_EINPROGRESS (WSAEINPROGRESS) +#define _getdns_EMFILE (WSAEMFILE) +#define _getdns_ECONNRESET (WSAECONNRESET) + +#define _getdns_closesocket(fd) closesocket(fd) +#define _getdns_poll(fdarray, nsockets, timer) WSAPoll(fdarray, nsockets, timer) +#define _getdns_socketerror() (WSAGetLastError()) + +#else /* USE_WINSOCK */ + +#ifdef HAVE_SYS_POLL_H +# include +#else +# include +#endif + +#define _getdns_EAGAIN (EAGAIN) +#define _getdns_EWOULDBLOCK (EWOULDBLOCK) +#define _getdns_EINPROGRESS (EINPROGRESS) +#define _getdns_EMFILE (EMFILE) +#define _getdns_ECONNRESET (ECONNRESET) + +#define SOCKADDR struct sockaddr +#define SOCKADDR_IN struct sockaddr_in +#define SOCKADDR_IN6 struct sockaddr_in6 +#define SOCKADDR_STORAGE struct sockaddr_storage +#define SOCKET int + +#define IP_MREQ struct ip_mreq +#define IPV6_MREQ struct ipv6_mreq +#define BOOL int +#define TRUE 1 + +#define _getdns_closesocket(fd) close(fd) +#define _getdns_poll(fdarray, nsockets, timer) poll(fdarray, nsockets, timer) +#define _getdns_socketerror() (errno) +#endif + +#endif diff --git a/src/server.c b/src/server.c index ab986a41..f29cb603 100644 --- a/src/server.c +++ b/src/server.c @@ -39,6 +39,8 @@ #include "types-internal.h" #include "debug.h" #include "util/rbtree.h" +#include "util-internal.h" +#include "platform.h" #include "server.h" #define DNS_REQUEST_SZ 4096 @@ -135,11 +137,7 @@ static void tcp_connection_destroy(tcp_connection *conn) loop->vmt->clear(loop, &conn->event); if (conn->fd >= 0) -#ifdef USE_WINSOCK - (void) closesocket(conn->fd); -#else - (void) close(conn->fd); -#endif + (void) _getdns_closesocket(conn->fd); GETDNS_FREE(*mf, conn->read_buf); for (cur = conn->to_write; cur; cur = next) { @@ -285,11 +283,7 @@ getdns_reply( (struct sockaddr *)&conn->remote_in, conn->addrlen) == -1) { /* IO error, cleanup this listener */ loop->vmt->clear(loop, &conn->l->event); -#ifdef USE_WINSOCK - closesocket(conn->l->fd); -#else - close(conn->l->fd); -#endif + _getdns_closesocket(conn->l->fd); conn->l->fd = -1; } /* Unlink this connection */ @@ -483,11 +477,7 @@ static void tcp_accept_cb(void *userarg) &conn->super.remote_in, &conn->super.addrlen)) == -1) { /* IO error, cleanup this listener */ loop->vmt->clear(loop, &l->event); -#ifdef USE_WINSOCK - closesocket(l->fd); -#else - close(l->fd); -#endif + _getdns_closesocket(l->fd); l->fd = -1; GETDNS_FREE(*mf, conn); return; @@ -555,13 +545,17 @@ static void udp_read_cb(void *userarg) conn->addrlen = sizeof(conn->remote_in); if ((len = recvfrom(l->fd, (void *)buf, sizeof(buf), 0, (struct sockaddr *)&conn->remote_in, &conn->addrlen)) == -1) { + if (_getdns_socketerror() == _getdns_ECONNRESET) { + /* + * WINSOCK gives ECONNRESET on ICMP Port Unreachable + * being received. Ignore it. + * */ + GETDNS_FREE(*mf, conn); + return; + } /* IO error, cleanup this listener. */ loop->vmt->clear(loop, &l->event); -#ifdef USE_WINSOCK - closesocket(l->fd); -#else - close(l->fd); -#endif + _getdns_closesocket(l->fd); l->fd = -1; #if 0 && defined(SERVER_DEBUG) && SERVER_DEBUG @@ -710,11 +704,7 @@ static void remove_listeners(listen_set *set) continue; loop->vmt->clear(loop, &l->event); -#ifdef USE_WINSOCK - closesocket(l->fd); -#else - close(l->fd); -#endif + _getdns_closesocket(l->fd); l->fd = -1; if (l->transport != GETDNS_TRANSPORT_TCP) diff --git a/src/stub.c b/src/stub.c index 1d16c4bb..ebcb2527 100644 --- a/src/stub.c +++ b/src/stub.c @@ -38,17 +38,6 @@ */ #define INTERCEPT_COM_DS 0 -#ifdef USE_POLL_DEFAULT_EVENTLOOP -# ifdef HAVE_SYS_POLL_H -# include -# else -#ifdef USE_WINSOCK -#define poll(fdarray, nbsockets, timer) WSAPoll(fdarray, nbsockets, timer) -#else -# include -#endif -# endif -#endif #include "debug.h" #include #include @@ -63,6 +52,7 @@ #include "rr-iter.h" #include "context.h" #include "util-internal.h" +#include "platform.h" #include "general.h" #include "pubkey-pinning.h" @@ -426,13 +416,10 @@ tcp_connect(getdns_upstream *upstream, getdns_transport_list_t transport) #endif if (connect(fd, (struct sockaddr *)&upstream->addr, upstream->addr_len) == -1) { - if (_getdns_EINPROGRESS || _getdns_EWOULDBLOCK) + if (_getdns_socketerror() == _getdns_EINPROGRESS || + _getdns_socketerror() == _getdns_EWOULDBLOCK) return fd; -#ifdef USE_WINSOCK - closesocket(fd); -#else - close(fd); -#endif + _getdns_closesocket(fd); return -1; } return fd; @@ -443,22 +430,13 @@ tcp_connected(getdns_upstream *upstream) { int error = 0; socklen_t len = (socklen_t)sizeof(error); getsockopt(upstream->fd, SOL_SOCKET, SO_ERROR, (void*)&error, &len); -#ifdef USE_WINSOCK - if (error == WSAEINPROGRESS) + if (error == _getdns_EINPROGRESS) return STUB_TCP_AGAIN; - else if (error == WSAEWOULDBLOCK) - return STUB_TCP_WOULDBLOCK; - else if (error != 0) - return STUB_SETUP_ERROR; -#else - if (error == EINPROGRESS) - return STUB_TCP_AGAIN; - else if (error == EWOULDBLOCK || error == EAGAIN) + else if (error == _getdns_EWOULDBLOCK || error == _getdns_EAGAIN) return STUB_TCP_WOULDBLOCK; else if (error != 0) { return STUB_SETUP_ERROR; } -#endif if (upstream->transport == GETDNS_TRANSPORT_TCP && upstream->queries_sent == 0) { upstream->conn_state = GETDNS_CONN_OPEN; @@ -570,11 +548,7 @@ _getdns_cancel_stub_request(getdns_network_req *netreq) STUB_DEBUG_CLEANUP, __FUNC__, (void*)netreq); stub_cleanup(netreq); if (netreq->fd >= 0) { -#ifdef USE_WINSOCK - closesocket(netreq->fd); -#else - close(netreq->fd); -#endif + _getdns_closesocket(netreq->fd); netreq->fd = -1; } } @@ -589,11 +563,7 @@ stub_timeout_cb(void *userarg) _getdns_netreq_change_state(netreq, NET_REQ_TIMED_OUT); /* Handle upstream*/ if (netreq->fd >= 0) { -#ifdef USE_WINSOCK - closesocket(netreq->fd); -#else - close(netreq->fd); -#endif + _getdns_closesocket(netreq->fd); netreq->fd = -1; netreq->upstream->udp_timeouts++; if (netreq->upstream->udp_timeouts % 100 == 0) @@ -650,7 +620,7 @@ upstream_setup_timeout_cb(void *userarg) #ifdef USE_POLL_DEFAULT_EVENTLOOP fds.fd = upstream->fd; fds.events = POLLOUT; - ret = poll(&fds, 1, 0); + ret = _getdns_poll(&fds, 1, 0); #else FD_ZERO(&fds); FD_SET((int)(upstream->fd), &fds); @@ -690,7 +660,7 @@ stub_tcp_read(int fd, getdns_tcp_state *tcp, struct mem_funcs *mf) } read = recv(fd, (void *)tcp->read_pos, tcp->to_read, 0); if (read < 0) { - if (_getdns_EWOULDBLOCK) + if (_getdns_socketerror() == _getdns_EWOULDBLOCK) return STUB_TCP_WOULDBLOCK; else return STUB_TCP_ERROR; @@ -807,11 +777,11 @@ stub_tcp_write(int fd, getdns_tcp_state *tcp, getdns_network_req *netreq) (struct sockaddr *)&(netreq->upstream->addr), netreq->upstream->addr_len); #endif - if ((written < 0 && (_getdns_EWOULDBLOCK || + if ((written < 0 && (_getdns_socketerror() == _getdns_EWOULDBLOCK || /* Add the error case where the connection is in progress which is when a cookie is not available (e.g. when doing the first request to an upstream). We must let the handshake complete since non-blocking. */ - _getdns_EINPROGRESS)) || + _getdns_socketerror() == _getdns_EINPROGRESS)) || (size_t)written < pkt_len + 2) { /* We couldn't write the whole packet. @@ -839,15 +809,10 @@ stub_tcp_write(int fd, getdns_tcp_state *tcp, getdns_network_req *netreq) /* Coming back from an earlier unfinished write or handshake. * Try to send remaining data */ -#ifdef USE_WINSOCK written = send(fd, (void *)(tcp->write_buf + tcp->written), tcp->write_buf_len - tcp->written, 0); -#else - written = write(fd, tcp->write_buf + tcp->written, - tcp->write_buf_len - tcp->written); -#endif if (written == -1) { - if (_getdns_EWOULDBLOCK) + if (_getdns_socketerror() == _getdns_EWOULDBLOCK) return STUB_TCP_WOULDBLOCK; else { DEBUG_STUB("%s %-35s: MSG: %p error while writing to TCP socket:" @@ -1386,7 +1351,8 @@ stub_udp_read_cb(void *userarg) * i.e. overflow */ 0, NULL, NULL); - if (read == -1 && _getdns_EWOULDBLOCK) + if (read == -1 && (_getdns_socketerror() == _getdns_EWOULDBLOCK || + _getdns_socketerror() == _getdns_ECONNRESET)) return; /* Try again later */ if (read == -1) { @@ -1398,11 +1364,7 @@ stub_udp_read_cb(void *userarg) _getdns_netreq_change_state(netreq, NET_REQ_ERRORED); /* Handle upstream*/ if (netreq->fd >= 0) { -#ifdef USE_WINSOCK - closesocket(netreq->fd); -#else - close(netreq->fd); -#endif + _getdns_closesocket(netreq->fd); netreq->fd = -1; stub_next_upstream(netreq); } @@ -1422,11 +1384,7 @@ stub_udp_read_cb(void *userarg) GETDNS_CLEAR_EVENT(dnsreq->loop, &netreq->event); -#ifdef USE_WINSOCK - closesocket(netreq->fd); -#else - close(netreq->fd); -#endif + _getdns_closesocket(netreq->fd); netreq->fd = -1; while (GLDNS_TC_WIRE(netreq->response)) { DEBUG_STUB("%s %-35s: MSG: %p TC bit set in response \n", STUB_DEBUG_READ, @@ -1519,11 +1477,7 @@ stub_udp_write_cb(void *userarg) _getdns_netreq_change_state(netreq, NET_REQ_ERRORED); /* Handle upstream*/ if (netreq->fd >= 0) { -#ifdef USE_WINSOCK - closesocket(netreq->fd); -#else - close(netreq->fd); -#endif + _getdns_closesocket(netreq->fd); netreq->fd = -1; stub_next_upstream(netreq); } @@ -2055,11 +2009,7 @@ upstream_connect(getdns_upstream *upstream, getdns_transport_list_t transport, if (upstream->tls_obj == NULL) { upstream_failed(upstream, 1); _getdns_upstream_reset(upstream); -#ifdef USE_WINSOCK - closesocket(fd); -#else - close(fd); -#endif + _getdns_closesocket(fd); return -1; } upstream->tls_hs_state = GETDNS_HS_WRITE; @@ -2128,7 +2078,7 @@ upstream_find_for_netreq(getdns_network_req *netreq) continue; if (fd == -1) { - if (_getdns_EMFILE) + if (_getdns_socketerror() == _getdns_EMFILE) return STUB_TRY_AGAIN_LATER; return -1; } diff --git a/src/util-internal.h b/src/util-internal.h index 1ef007bc..3d768de1 100644 --- a/src/util-internal.h +++ b/src/util-internal.h @@ -218,18 +218,5 @@ INLINE uint64_t _getdns_ms_until_expiry2(uint64_t expires, uint64_t *now_ms) return *now_ms >= expires ? 0 : expires - *now_ms; } - -#ifdef USE_WINSOCK -typedef u_short sa_family_t; -#define _getdns_EWOULDBLOCK (WSAGetLastError() == WSATRY_AGAIN ||\ - WSAGetLastError() == WSAEWOULDBLOCK) -#define _getdns_EINPROGRESS (WSAGetLastError() == WSAEINPROGRESS) -#define _getdns_EMFILE (WSAGetLastError() == WSAEMFILE) -#else -#define _getdns_EWOULDBLOCK (errno == EAGAIN || errno == EWOULDBLOCK) -#define _getdns_EINPROGRESS (errno == EINPROGRESS) -#define _getdns_EMFILE (errno == EMFILE) -#endif - #endif /* util-internal.h */