check for FD_SETSIZE
authorChristian Grothoff <christian@grothoff.org>
Thu, 8 Oct 2009 08:52:48 +0000 (08:52 +0000)
committerChristian Grothoff <christian@grothoff.org>
Thu, 8 Oct 2009 08:52:48 +0000 (08:52 +0000)
src/util/network.c

index 91d4066b2a2429c02afadff61a166279da380f15..8bdace580b7c740b6a34cfb3dc1e873d7dd4aac7 100644 (file)
 \r
 #define DEBUG_SOCK GNUNET_NO\r
 \r
+#ifndef INVALID_SOCKET\r
+#define INVALID_SOCKET -1\r
+#endif\r
+\r
 struct GNUNET_NETWORK_Handle\r
 {\r
   int fd;\r
@@ -68,9 +72,22 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
 \r
   ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));\r
   ret->fd = accept (desc->fd, address, address_len);\r
+  if (ret->fd == INVALID_SOCKET)\r
+    {\r
 #ifdef MINGW\r
-  if (INVALID_SOCKET == ret->fd)\r
-    SetErrnoFromWinsockError (WSAGetLastError ());\r
+      SetErrnoFromWinsockError (WSAGetLastError ());\r
+#endif\r
+      GNUNET_free (ret);\r
+      return NULL;\r
+    }\r
+#ifndef MINGW\r
+  if (ret->fd >= FD_SETSIZE)\r
+    {\r
+      close (desc->fd);\r
+      GNUNET_free (ret);\r
+      errno = EMFILE;\r
+      return NULL;\r
+    }\r
 #endif\r
   return ret;\r
 }\r
@@ -347,17 +364,23 @@ GNUNET_NETWORK_socket_socket (int domain, int type, int protocol)
 \r
   ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));\r
   ret->fd = socket (domain, type, protocol);\r
-#ifdef MINGW\r
   if (INVALID_SOCKET == ret->fd)\r
-    SetErrnoFromWinsockError (WSAGetLastError ());\r
+    {\r
+#ifdef MINGW\r
+      SetErrnoFromWinsockError (WSAGetLastError ());\r
 #endif\r
-\r
-  if (ret->fd < 0)\r
+      GNUNET_free (ret);\r
+      return NULL;\r
+    }\r
+#ifndef MINGW\r
+  if (ret->fd >= FD_SETSIZE)\r
     {\r
+      close (ret->fd);\r
       GNUNET_free (ret);\r
-      ret = NULL;\r
+      errno = EMFILE;\r
+      return NULL;\r
     }\r
-\r
+#endif\r
   return ret;\r
 }\r
 \r