Finding the right interface on FreeBSD

Gaël Roualland gael.roualland at oleane.net
Thu May 18 00:38:01 CEST 2006


Hello,

The code in tinc for BSD device has no way to handle the fact that
several tun devices are already used., since it always try to open the
specified device which might already be in use.

The following patch makes it try to open the various available tun
devices before giving up.

Hope that helps,

-- 
Gaël Roualland -+- gael.roualland at oleane.net
-------------- next part --------------
diff -ru tinc-1.0.4/src/bsd/device.c tinc-1.0.4.gr/src/bsd/device.c
--- tinc-1.0.4/src/bsd/device.c	Wed May  4 18:57:55 2005
+++ tinc-1.0.4.gr/src/bsd/device.c	Wed May 17 22:47:27 2006
@@ -38,6 +38,10 @@
 
 int device_fd = -1;
 char *device;
+
+static char device_buf[256];
+#define DEVICE_TRIES_MAX 255
+
 char *iface;
 char *device_info;
 static int device_total_in = 0;
@@ -60,8 +64,31 @@
 		iface = rindex(device, '/') ? rindex(device, '/') + 1 : device;
 
 	if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
-		logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
-		return false;
+		int device_nr;
+
+		/* if device terminates by a number and is of reasonable size,
+		   try any following numbered device */
+		if (strlen(device) > 1 && strlen(device) < sizeof(device_buf) - 3) {
+			char *nr = device + strlen(device) - 1;
+			while (nr > device && *nr >= '0' && *nr <= '9')
+				nr--;
+			if (nr > device && nr < device + strlen(device) - 1) {
+				nr++;
+				device_nr = atoi(nr);
+				*nr = 0;	
+		        	do {
+					device_nr++;
+					sprintf(device_buf, "%s%d", device, device_nr);
+					device_fd = open(device_buf, O_RDWR | O_NONBLOCK);
+		    		} while (device_fd < 0 && device_nr < DEVICE_TRIES_MAX);
+		    		if (device_fd >= 0)
+					device = device_buf;
+			}
+		} 
+		if (device_fd < 0) {
+			logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
+			return false;
+		}
 	}
 
 	if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {


More information about the tinc-devel mailing list