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.
bool choice;
int i, err;
int replaywin_int;
bool choice;
int i, err;
int replaywin_int;
+ bool port_specified = false;
myself = new_node();
myself->connection = new_connection();
myself = new_node();
myself->connection = new_connection();
+ /* Read tinc.conf and our own host config file */
+
myself->name = name;
myself->connection->name = xstrdup(name);
xasprintf(&fname, "%s/hosts/%s", confbase, name);
myself->name = name;
myself->connection->name = xstrdup(name);
xasprintf(&fname, "%s/hosts/%s", confbase, name);
if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
myport = xstrdup("655");
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);
if(!atoi(myport)) {
struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
- if(listen_sockets)
- logger(LOG_NOTICE, "Ready");
- else {
logger(LOG_ERR, "Unable to create any listening socket!");
return false;
}
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");