From a7dbb50c23f447a23b543c92ec096ff178bc2de3 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Wed, 21 Mar 2012 13:20:15 +0100 Subject: [PATCH] Allow a port to be specified in BindToAddress statements. This can be used to let tinc listen on multiple ports for incoming connections. --- doc/tinc.conf.5.in | 15 ++++++++++++--- doc/tinc.texi | 6 +++++- src/net_setup.c | 15 ++++++++++++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/doc/tinc.conf.5.in b/doc/tinc.conf.5.in index a6ae4f5a..1d2f17f9 100644 --- a/doc/tinc.conf.5.in +++ b/doc/tinc.conf.5.in @@ -129,7 +129,7 @@ If is selected, then depending on the operating system both IPv4 and IPv6 or just IPv6 listening sockets will be created. -.It Va BindToAddress Li = Ar address Bq experimental +.It Va BindToAddress Li = Ar address Oo Ar port Oc Bq experimental If your computer has more than one IPv4 or IPv6 address, .Nm tinc will by default listen on all of them for incoming connections. @@ -137,7 +137,16 @@ Multiple .Va BindToAddress variables may be specified, in which case listening sockets for each specified address are made. - +.Pp +If no +.Ar port +is specified, the socket will be bound to the port specified by the +.Va Port +option, or to port 655 if neither is given. +To only bind to a specific port but not to a specific address, use +.Li * +for the +.Ar address . .Pp This option may not work on all platforms. @@ -452,7 +461,7 @@ Since host configuration files only contain public keys, no secrets are revealed by sending out this information. .Bl -tag -width indent -.It Va Address Li = Ar address Oo port Oc Bq recommended +.It Va Address Li = Ar address Oo Ar port Oc Bq recommended The IP address or hostname of this tinc daemon on the real network. This will only be used when trying to make an outgoing connection to this tinc daemon. Optionally, a port can be specified to use for this address. diff --git a/doc/tinc.texi b/doc/tinc.texi index aedde3f8..8bf0a6f5 100644 --- a/doc/tinc.texi +++ b/doc/tinc.texi @@ -756,12 +756,16 @@ If any is selected, then depending on the operating system both IPv4 and IPv6 or just IPv6 listening sockets will be created. @cindex BindToAddress -@item BindToAddress = <@var{address}> [experimental] +@item BindToAddress = <@var{address}> [<@var{port}>] [experimental] If your computer has more than one IPv4 or IPv6 address, tinc will by default listen on all of them for incoming connections. Multiple BindToAddress variables may be specified, in which case listening sockets for each specified address are made. +If no @var{port} is specified, the socket will be bound to the port specified by the Port option, +or to port 655 if neither is given. +To only bind to a specific port but not to a specific address, use "*" for the @var{address}. + This option may not work on all platforms. @cindex BindToInterface diff --git a/src/net_setup.c b/src/net_setup.c index 26845143..29d4952a 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -587,12 +587,25 @@ static bool setup_myself(void) { if(cfg) cfg = lookup_config_next(config_tree, cfg); + char *port = myport; + + if(address) { + char *space = strchr(address, ' '); + if(space) { + *space++ = 0; + port = space; + } + + if(!strcmp(address, "*")) + *address = 0; + } + hint.ai_family = addressfamily; hint.ai_socktype = SOCK_STREAM; hint.ai_protocol = IPPROTO_TCP; hint.ai_flags = AI_PASSIVE; - err = getaddrinfo(address, myport, &hint, &ai); + err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai); free(address); if(err || !ai) { -- 2.20.1