diff --git a/src/anchor.c b/src/anchor.c index 6872ab18..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 diff --git a/src/context.c b/src/context.c index 9b43ff76..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" diff --git a/src/extension/poll_eventloop.c b/src/extension/poll_eventloop.c index b0793e7c..7d6bb99c 100644 --- a/src/extension/poll_eventloop.c +++ b/src/extension/poll_eventloop.c @@ -28,6 +28,7 @@ #include "config.h" #include "util-internal.h" +#include "platform.h" #ifdef HAVE_SYS_RESOURCE_H #include #endif @@ -406,7 +407,7 @@ poll_eventloop_run_once(getdns_eventloop *loop, int blocking) Sleep(poll_timeout); } else #endif - if (poll(poll_loop->pfds, poll_loop->fd_events_free, poll_timeout) < 0) { + 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 7578a262..30bdab6e 100644 --- a/src/mdns.c +++ b/src/mdns.c @@ -26,6 +26,7 @@ #include "gldns/pkthdr.h" #include "gldns/rrdef.h" #include "util-internal.h" +#include "platform.h" #include "mdns.h" #ifdef HAVE_MDNS_SUPPORT diff --git a/src/mdns.h b/src/mdns.h index e53f79a1..79ec05fe 100644 --- a/src/mdns.h +++ b/src/mdns.h @@ -25,6 +25,7 @@ #include "getdns/getdns.h" #include "types-internal.h" #include "util-internal.h" +#include "platform.h" #include "util/lruhash.h" #include "config.h" 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 319991da..f29cb603 100644 --- a/src/server.c +++ b/src/server.c @@ -40,6 +40,7 @@ #include "debug.h" #include "util/rbtree.h" #include "util-internal.h" +#include "platform.h" #include "server.h" #define DNS_REQUEST_SZ 4096 diff --git a/src/stub.c b/src/stub.c index 5d472f72..ebcb2527 100644 --- a/src/stub.c +++ b/src/stub.c @@ -52,6 +52,7 @@ #include "rr-iter.h" #include "context.h" #include "util-internal.h" +#include "platform.h" #include "general.h" #include "pubkey-pinning.h" diff --git a/src/util-internal.h b/src/util-internal.h index faab07b0..3d768de1 100644 --- a/src/util-internal.h +++ b/src/util-internal.h @@ -218,46 +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_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 -#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 /* util-internal.h */