Subject: Make UDP sockets non-blocking

Make UDP sockets non-blocking

From: William Ahern <william_at_25thandclement.com>
Date: 2005-06-02

I can't remember if I submitted this before. This patch makes UDP sockets
non-blocking. I've confirmed that at least on Linux 2.4 a read event can
come back from poll() on a valid SOCK_DGRAM socket but recv(2) will still
block. This patch doesn't ignore EAGAIN in read_udp_packets(), though maybe
it should.

--- c-ares1/ares_process.c 2005/06/01 23:33:19 7804
+++ c-ares1/ares_process.c 2005/06/01 23:36:55 7805
@@ -517,6 +517,7 @@
 static int open_udp_socket(ares_channel channel, struct server_state *server)
 {
   ares_socket_t s;
+ int flags;
   struct sockaddr_in sockin;
 
   /* Acquire a socket. */
@@ -524,6 +525,27 @@
   if (s == ARES_SOCKET_BAD)
     return -1;
 
+ /* Set the socket non-blocking. */
+
+#if defined(WIN32) || defined(WATT32)
+ flags = 1;
+ ioctlsocket(s, FIONBIO, &flags);
+#else
+ flags = fcntl(s, F_GETFL, 0);
+
+ if (flags == -1)
+ {
+ closesocket(s);
+ return -1;
+ }
+ flags |= O_NONBLOCK;
+ if (fcntl(s, F_SETFL, flags) == -1)
+ {
+ closesocket(s);
+ return -1;
+ }
+#endif
+
   /* Connect to the server. */
   memset(&sockin, 0, sizeof(sockin));
   sockin.sin_family = AF_INET;

_______________________________________________
http://cool.haxx.se/mailman/listinfo/c-ares
Received on Thu Jun 2 01:57:34 2005