socket leakage patch

Jamie Briggs jamie.briggs at computalog.com
Wed Nov 8 18:32:24 CET 2000


We have been fighting with intermittent connections here and have noticed that
tinc seems to use up its supply of file descriptors.

After a whole bunch of 

Nov  8 03:51:23 tserver tinc.calgary[23909]: Could not set up a meta connection.
Nov  8 03:51:23 tserver tinc.calgary[23909]: Still failed to connect to other. Will retry
Nov  8 03:51:33 tserver tinc.calgary[23909]: 10.38.9.1:8193: Connection refused
Nov  8 03:51:33 tserver tinc.calgary[23909]: Could not set up a meta connection.
Nov  8 03:51:33 tserver tinc.calgary[23909]: Still failed to connect to other. Will retry
Nov  8 03:51:48 tserver tinc.calgary[23909]: 10.38.9.1:8193: Connection refused

We started getting

Nov  8 07:38:35 tserver tinc.calgary[23909]: Creating socket failed: Too many open files
Nov  8 07:38:35 tserver tinc.calgary[23909]: Could not set up a meta connection.
Nov  8 07:38:35 tserver tinc.calgary[23909]: Still failed to connect to other. Will retry
Nov  8 07:42:40 tserver tinc.calgary[23909]: Creating socket failed: Too many open files
Nov  8 07:42:40 tserver tinc.calgary[23909]: Could not set up a meta connection.
Nov  8 07:42:40 tserver tinc.calgary[23909]: Still failed to connect to other. Will retry

Looking at the source I decided to try the attached patch.  Our vpn has just 
been restarted with the patch installed and it seems to work but we will have
to wait until the next batch of connection/routing problems to see whether it
fixes the problem.

I guess my question is:  is this a reasonable approach to fixing the problem?
I am familiar with network programming but haven't done enough of it to
be sure that I haven't overlooked something.

- Jamie
-------------- next part --------------
diff -uNr tinc-1.0pre2.org/src/net.c tinc-1.0pre2/src/net.c
--- tinc-1.0pre2.org/src/net.c	Wed May 31 12:23:05 2000
+++ tinc-1.0pre2/src/net.c	Wed Nov  8 09:36:05 2000
@@ -357,6 +357,7 @@
 
   if(setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)))
     {
+      close(nfd);
       syslog(LOG_ERR, _("setsockopt: %m"));
       return -1;
     }
@@ -364,6 +365,7 @@
   flags = fcntl(nfd, F_GETFL);
   if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
     {
+      close(nfd);
       syslog(LOG_ERR, _("fcntl: %m"));
       return -1;
     }
@@ -375,12 +377,14 @@
 
   if(bind(nfd, (struct sockaddr *)&a, sizeof(struct sockaddr)))
     {
+      close(nfd);
       syslog(LOG_ERR, _("Can't bind to port %hd/tcp: %m"), port);
       return -1;
     }
 
   if(listen(nfd, 3))
     {
+      close(nfd);
       syslog(LOG_ERR, _("listen: %m"));
       return -1;
     }
@@ -406,6 +410,7 @@
 
   if(setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)))
     {
+      close(nfd);
       syslog(LOG_ERR, _("setsockopt: %m"));
       return -1;
     }
@@ -413,6 +418,7 @@
   flags = fcntl(nfd, F_GETFL);
   if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
     {
+      close(nfd);
       syslog(LOG_ERR, _("fcntl: %m"));
       return -1;
     }
@@ -424,6 +430,7 @@
 
   if(bind(nfd, (struct sockaddr *)&a, sizeof(struct sockaddr)))
     {
+      close(nfd);
       syslog(LOG_ERR, _("Can't bind to port %hd/udp: %m"), port);
       return -1;
     }
@@ -458,6 +465,7 @@
 
   if(connect(cl->meta_socket, (struct sockaddr *)&a, sizeof(a)) == -1)
     {
+      close(cl->meta_socket);
       syslog(LOG_ERR, _(IP_ADDR_S ":%d: %m"), IP_ADDR_V(cl->real_ip), cl->port);
       return -1;
     }
@@ -465,6 +473,7 @@
   flags = fcntl(cl->meta_socket, F_GETFL);
   if(fcntl(cl->meta_socket, F_SETFL, flags | O_NONBLOCK) < 0)
     {
+      close(cl->meta_socket);
       syslog(LOG_ERR, _("fcntl: %m"));
       return -1;
     }


More information about the Tinc mailing list