From 124de13caa57b99b9e184c18410690fd2e210759 Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Wed, 15 Oct 2014 23:28:59 +0200 Subject: [PATCH] Initialize udp socket nonblocking --- configure | 46 ++++++++++++++++++++++++++++++++++++++++++++++ configure.ac | 14 ++++++++++++++ src/config.h.in | 6 ++++++ src/stub.c | 24 +++++++++++++++++++++++- 4 files changed, 89 insertions(+), 1 deletion(-) diff --git a/configure b/configure index c633b567..f820560c 100755 --- a/configure +++ b/configure @@ -11176,6 +11176,52 @@ if test "x$ac_cv_type_u_char" = xyes; then : fi +for ac_func in fcntl +do : + ac_fn_c_check_func "$LINENO" "fcntl" "ac_cv_func_fcntl" +if test "x$ac_cv_func_fcntl" = xyes; then : + cat >>confdefs.h <<_ACEOF +#define HAVE_FCNTL 1 +_ACEOF + +fi +done + +# check ioctlsocket +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for ioctlsocket" >&5 +$as_echo_n "checking for ioctlsocket... " >&6; } +cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + +#ifdef HAVE_WINSOCK2_H +#include +#endif + +int +main () +{ + + (void)ioctlsocket(0, 0, NULL); + + ; + return 0; +} +_ACEOF +if ac_fn_c_try_link "$LINENO"; then : + +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 +$as_echo "yes" >&6; } + +$as_echo "#define HAVE_IOCTLSOCKET 1" >>confdefs.h + + +else + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } +fi +rm -f core conftest.err conftest.$ac_objext \ + conftest$ac_exeext conftest.$ac_ext + getdns_LIBS=$LIBS getdns_LDFLAGS=$LDFLAGS diff --git a/configure.ac b/configure.ac index 9bc9bca5..c72791fa 100644 --- a/configure.ac +++ b/configure.ac @@ -189,6 +189,20 @@ AC_TYPE_UINT64_T AC_TYPE_UINT8_T AC_CHECK_TYPE([u_char]) +AC_CHECK_FUNCS([fcntl]) +# check ioctlsocket +AC_MSG_CHECKING(for ioctlsocket) +AC_LINK_IFELSE([AC_LANG_PROGRAM([ +#ifdef HAVE_WINSOCK2_H +#include +#endif +], [ + (void)ioctlsocket(0, 0, NULL); +])], [ +AC_MSG_RESULT(yes) +AC_DEFINE(HAVE_IOCTLSOCKET, 1, [if the function 'ioctlsocket' is available]) +],[AC_MSG_RESULT(no)]) + getdns_LIBS=$LIBS getdns_LDFLAGS=$LDFLAGS diff --git a/src/config.h.in b/src/config.h.in index 43ff20d0..d25ff728 100644 --- a/src/config.h.in +++ b/src/config.h.in @@ -30,9 +30,15 @@ /* Define to 1 if you have the header file. */ #undef HAVE_EV_H +/* Define to 1 if you have the `fcntl' function. */ +#undef HAVE_FCNTL + /* Define to 1 if you have the header file. */ #undef HAVE_INTTYPES_H +/* if the function 'ioctlsocket' is available */ +#undef HAVE_IOCTLSOCKET + /* Define to 1 if you have the header file. */ #undef HAVE_LIBEV_EV_H diff --git a/src/stub.c b/src/stub.c index 07b1850b..fed751e0 100644 --- a/src/stub.c +++ b/src/stub.c @@ -32,6 +32,7 @@ */ #include "config.h" +#include #include "stub.h" #include "gldns/rrdef.h" #include "gldns/str2wire.h" @@ -42,7 +43,6 @@ #include "util-internal.h" #include "general.h" - static int getdns_make_query_pkt_buf(getdns_context *context, const char *name, uint16_t request_type, getdns_dict *extensions, uint8_t* buf, size_t* olen) @@ -243,6 +243,26 @@ getdns_get_query_pkt_size(getdns_context *context, ; } +/** best effort to set nonblocking */ +static void +getdns_sock_nonblock(int sockfd) +{ +#ifdef HAVE_FCNTL + int flag; + if((flag = fcntl(sockfd, F_GETFL)) != -1) { + flag |= O_NONBLOCK; + if(fcntl(sockfd, F_SETFL, flag) == -1) { + /* ignore error, continue blockingly */ + } + } +#elif defined(HAVE_IOCTLSOCKET) + unsigned long on = 1; + if(ioctlsocket(sockfd, FIONBIO, &on) != 0) { + /* ignore error, continue blockingly */ + } +#endif +} + static void stub_resolve_timeout_cb(void *userarg) { @@ -327,6 +347,8 @@ priv_getdns_submit_stub_request(getdns_network_req *netreq) upstream->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) goto error; + getdns_sock_nonblock(netreq->udp_fd); + if (pkt_len != sendto(netreq->udp_fd, pkt, pkt_len, 0, (struct sockaddr *)&upstream->addr, upstream->addr_len)) { close(netreq->udp_fd);