[or-cvs] If we are using select, make sure we stay within FD_SETSIZE.

Nick Mathewson nickm at seul.org
Wed Dec 1 03:16:02 UTC 2004


Update of /home/or/cvsroot/tor/src/or
In directory moria.mit.edu:/tmp/cvs-serv13318/src/or

Modified Files:
	connection.c 
Log Message:
If we are using select, make sure we stay within FD_SETSIZE.

Index: connection.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/connection.c,v
retrieving revision 1.304
retrieving revision 1.305
diff -u -d -r1.304 -r1.305
--- connection.c	29 Nov 2004 22:25:29 -0000	1.304
+++ connection.c	1 Dec 2004 03:15:59 -0000	1.305
@@ -363,6 +363,10 @@
   if (s < 0) {
     log_fn(LOG_WARN,"Socket creation failed.");
     return -1;
+  } else if (!SOCKET_IS_POLLABLE(s)) {
+    log_fn(LOG_WARN,"Too many connections; can't create pollable listener.");
+    tor_close_socket(s);
+    return -1;
   }
 
   setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*) &one, sizeof(one));
@@ -410,8 +414,16 @@
   int remotelen = sizeof(struct sockaddr_in);
 
   news = accept(conn->s,(struct sockaddr *)&remote,&remotelen);
-  if (news == -1) { /* accept() error */
-    int e = tor_socket_errno(conn->s);
+  if (!SOCKET_IS_POLLABLE(news)) {
+    /* accept() error, or two many conns to poll */
+    int e;
+    if (news>=0) {
+      /* Too many conns to poll. */
+      log_fn(LOG_WARN,"Too many connections; couldn't accept connection.");
+      tor_close_socket(news);
+      return 0;
+    }
+    e = tor_socket_errno(conn->s);
     if (ERRNO_IS_ACCEPT_EAGAIN(e)) {
       return 0; /* he hung up before we could accept(). that's fine. */
     } else if (ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e)) {
@@ -505,11 +517,16 @@
   struct sockaddr_in dest_addr;
   or_options_t *options = get_options();
 
-  s=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
+  s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
   if (s < 0) {
     log_fn(LOG_WARN,"Error creating network socket: %s",
            tor_socket_strerror(tor_socket_errno(-1)));
     return -1;
+  } else if (!SOCKET_IS_POLLABLE(s)) {
+    log_fn(LOG_WARN,
+      "Too many connections; can't create pollable connection to %s", address);
+    tor_close_socket(s);
+    return -1;
   }
 
   if (options->OutboundBindAddress) {



More information about the tor-commits mailing list