From 2a07dacae7443ac5a437a82c33d9e9d8d2acbfbd Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Thu, 5 Dec 2013 14:30:41 +0100 Subject: [PATCH] If no Port is specified, set myport to actual port of first listening socket. If the Port statement is not used, there are two other ways to let tinc listen on a non-default port: either by specifying one or more BindToAddress statements including port numbers, or by starting it from systemd with socket activation. Tinc announces its own port to other nodes, but before it only announced what was set using the Port statement. --- src/net_setup.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/net_setup.c b/src/net_setup.c index e1d9f275..249724d3 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -340,6 +340,7 @@ static bool setup_myself(void) { bool choice; int i, err; int replaywin_int; + bool port_specified = false; myself = new_node(); myself->connection = new_connection(); @@ -355,6 +356,8 @@ static bool setup_myself(void) { return false; } + /* Read tinc.conf and our own host config file */ + myself->name = name; myself->connection->name = xstrdup(name); xasprintf(&fname, "%s/hosts/%s", confbase, name); @@ -367,6 +370,10 @@ static bool setup_myself(void) { if(!get_config_string(lookup_config(config_tree, "Port"), &myport)) myport = xstrdup("655"); + else + port_specified = true; + + /* Ensure myport is numeric */ if(!atoi(myport)) { struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM); @@ -812,13 +819,27 @@ static bool setup_myself(void) { } while(cfg); } - if(listen_sockets) - logger(LOG_NOTICE, "Ready"); - else { + if(!listen_sockets) { logger(LOG_ERR, "Unable to create any listening socket!"); return false; } + /* If no Port option was specified, set myport to the port used by the first listening socket. */ + + if(!port_specified) { + sockaddr_t sa; + socklen_t salen = sizeof sa; + if(!getsockname(listen_socket[0].udp, &sa.sa, &salen)) { + free(myport); + sockaddr2str(&sa, NULL, &myport); + if(!myport) + myport = xstrdup("655"); + } + } + + /* Done. */ + + logger(LOG_NOTICE, "Ready"); return true; } -- 2.20.1