From: Guus Sliepen <guus@tinc-vpn.org>
Date: Fri, 17 Feb 2012 15:25:00 +0000 (+0100)
Subject: Allow multiple BindToAddress statements.
X-Git-Tag: release-1.0.17~23
X-Git-Url: https://tinc-vpn.org/git/browse?a=commitdiff_plain;h=9f6a96af3939bd2de410ce346a8c8fbcf93e7c9b;p=tinc

Allow multiple BindToAddress statements.
---

diff --git a/doc/tinc.conf.5.in b/doc/tinc.conf.5.in
index dd74acfa..6853bae4 100644
--- a/doc/tinc.conf.5.in
+++ b/doc/tinc.conf.5.in
@@ -133,7 +133,10 @@ IPv6 listening sockets will be created.
 If your computer has more than one IPv4 or IPv6 address,
 .Nm tinc
 will by default listen on all of them for incoming connections.
-It is possible to bind only to a single address with this variable.
+Multiple
+.Va BindToAddress
+variables may be specified,
+in which case listening sockets for each specified address are made.
 
 .Pp
 This option may not work on all platforms.
diff --git a/doc/tinc.texi b/doc/tinc.texi
index b7d646d5..817c2ded 100644
--- a/doc/tinc.texi
+++ b/doc/tinc.texi
@@ -759,7 +759,8 @@ both IPv4 and IPv6 or just IPv6 listening sockets will be created.
 @item BindToAddress = <@var{address}> [experimental]
 If your computer has more than one IPv4 or IPv6 address, tinc
 will by default listen on all of them for incoming connections.
-It is possible to bind only to a single address with this variable.
+Multiple BindToAddress variables may be specified,
+in which case listening sockets for each specified address are made.
 
 This option may not work on all platforms.
 
diff --git a/src/net_setup.c b/src/net_setup.c
index 9cf24bf8..279feaef 100644
--- a/src/net_setup.c
+++ b/src/net_setup.c
@@ -582,47 +582,54 @@ static bool setup_myself(void) {
 
 	/* Open sockets */
 
-	get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
+	cfg = lookup_config(config_tree, "BindToAddress");
 
-	hint.ai_family = addressfamily;
-	hint.ai_socktype = SOCK_STREAM;
-	hint.ai_protocol = IPPROTO_TCP;
-	hint.ai_flags = AI_PASSIVE;
+	do {
+		get_config_string(cfg, &address);
+		if(cfg)
+			cfg = lookup_config_next(config_tree, cfg);
 
-	err = getaddrinfo(address, myport, &hint, &ai);
+		hint.ai_family = addressfamily;
+		hint.ai_socktype = SOCK_STREAM;
+		hint.ai_protocol = IPPROTO_TCP;
+		hint.ai_flags = AI_PASSIVE;
 
-	if(err || !ai) {
-		logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
-			   gai_strerror(err));
-		return false;
-	}
+		err = getaddrinfo(address, myport, &hint, &ai);
+		free(address);
 
-	listen_sockets = 0;
+		if(err || !ai) {
+			logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
+				   gai_strerror(err));
+			return false;
+		}
 
-	for(aip = ai; aip; aip = aip->ai_next) {
-		listen_socket[listen_sockets].tcp =
-			setup_listen_socket((sockaddr_t *) aip->ai_addr);
+		listen_sockets = 0;
 
-		if(listen_socket[listen_sockets].tcp < 0)
-			continue;
+		for(aip = ai; aip; aip = aip->ai_next) {
+			listen_socket[listen_sockets].tcp =
+				setup_listen_socket((sockaddr_t *) aip->ai_addr);
 
-		listen_socket[listen_sockets].udp =
-			setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
+			if(listen_socket[listen_sockets].tcp < 0)
+				continue;
 
-		if(listen_socket[listen_sockets].udp < 0)
-			continue;
+			listen_socket[listen_sockets].udp =
+				setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
 
-		ifdebug(CONNECTIONS) {
-			hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
-			logger(LOG_NOTICE, "Listening on %s", hostname);
-			free(hostname);
-		}
+			if(listen_socket[listen_sockets].udp < 0)
+				continue;
 
-		memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
-		listen_sockets++;
-	}
+			ifdebug(CONNECTIONS) {
+				hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
+				logger(LOG_NOTICE, "Listening on %s", hostname);
+				free(hostname);
+			}
+
+			memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
+			listen_sockets++;
+		}
 
-	freeaddrinfo(ai);
+		freeaddrinfo(ai);
+	} while(cfg);
 
 	if(listen_sockets)
 		logger(LOG_NOTICE, "Ready");