Initialize udp socket nonblocking

This commit is contained in:
Willem Toorop 2014-10-15 23:28:59 +02:00
parent a0cb4e1774
commit 124de13caa
4 changed files with 89 additions and 1 deletions

46
configure vendored
View File

@ -11176,6 +11176,52 @@ if test "x$ac_cv_type_u_char" = xyes; then :
fi 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 <winsock2.h>
#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_LIBS=$LIBS
getdns_LDFLAGS=$LDFLAGS getdns_LDFLAGS=$LDFLAGS

View File

@ -189,6 +189,20 @@ AC_TYPE_UINT64_T
AC_TYPE_UINT8_T AC_TYPE_UINT8_T
AC_CHECK_TYPE([u_char]) 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 <winsock2.h>
#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_LIBS=$LIBS
getdns_LDFLAGS=$LDFLAGS getdns_LDFLAGS=$LDFLAGS

View File

@ -30,9 +30,15 @@
/* Define to 1 if you have the <ev.h> header file. */ /* Define to 1 if you have the <ev.h> header file. */
#undef HAVE_EV_H #undef HAVE_EV_H
/* Define to 1 if you have the `fcntl' function. */
#undef HAVE_FCNTL
/* Define to 1 if you have the <inttypes.h> header file. */ /* Define to 1 if you have the <inttypes.h> header file. */
#undef HAVE_INTTYPES_H #undef HAVE_INTTYPES_H
/* if the function 'ioctlsocket' is available */
#undef HAVE_IOCTLSOCKET
/* Define to 1 if you have the <libev/ev.h> header file. */ /* Define to 1 if you have the <libev/ev.h> header file. */
#undef HAVE_LIBEV_EV_H #undef HAVE_LIBEV_EV_H

View File

@ -32,6 +32,7 @@
*/ */
#include "config.h" #include "config.h"
#include <fcntl.h>
#include "stub.h" #include "stub.h"
#include "gldns/rrdef.h" #include "gldns/rrdef.h"
#include "gldns/str2wire.h" #include "gldns/str2wire.h"
@ -42,7 +43,6 @@
#include "util-internal.h" #include "util-internal.h"
#include "general.h" #include "general.h"
static int static int
getdns_make_query_pkt_buf(getdns_context *context, const char *name, getdns_make_query_pkt_buf(getdns_context *context, const char *name,
uint16_t request_type, getdns_dict *extensions, uint8_t* buf, size_t* olen) 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 static void
stub_resolve_timeout_cb(void *userarg) 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) upstream->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1)
goto error; goto error;
getdns_sock_nonblock(netreq->udp_fd);
if (pkt_len != sendto(netreq->udp_fd, pkt, pkt_len, 0, if (pkt_len != sendto(netreq->udp_fd, pkt, pkt_len, 0,
(struct sockaddr *)&upstream->addr, upstream->addr_len)) { (struct sockaddr *)&upstream->addr, upstream->addr_len)) {
close(netreq->udp_fd); close(netreq->udp_fd);