+// Get the port that `from_fd` is listening on, and assign it to
+// `sa` if `sa` has a dynamically allocated (zero) port.
+static bool assign_static_port(sockaddr_t *sa, int from_fd) {
+ // We cannot get a port from a bad FD. Bail out.
+ if(from_fd <= 0) {
+ return false;
+ }
+
+ int port = get_bound_port(from_fd);
+
+ if(!port) {
+ return false;
+ }
+
+ // If the port is non-zero, don't reassign it as it's already static.
+ switch(sa->sa.sa_family) {
+ case AF_INET:
+ if(!sa->in.sin_port) {
+ sa->in.sin_port = htons(port);
+ }
+
+ return true;
+
+ case AF_INET6:
+ if(!sa->in6.sin6_port) {
+ sa->in6.sin6_port = htons(port);
+ }
+
+ return true;
+
+ default:
+ logger(DEBUG_ALWAYS, LOG_ERR, "Unknown address family 0x%x", sa->sa.sa_family);
+ return false;
+ }
+}
+
+typedef int (*bind_fn_t)(const sockaddr_t *);
+
+static int bind_reusing_port(const sockaddr_t *sa, int from_fd, bind_fn_t setup) {
+ sockaddr_t reuse_sa;
+ memcpy(&reuse_sa, sa, SALEN(sa->sa));
+
+ int fd = -1;
+
+ // Check if the address we've been passed here is using port 0.
+ // If it is, try to get an actual port from an already bound socket, and reuse it here.
+ if(assign_static_port(&reuse_sa, from_fd)) {
+ fd = setup(&reuse_sa);
+ }
+
+ // If we're binding to a hardcoded non-zero port, or no socket is listening yet,
+ // or binding failed, try the original address.
+ if(fd < 0) {
+ fd = setup(sa);
+ }
+
+ return fd;
+}
+
+/*
+ Add listening sockets.
+*/
+static bool add_listen_address(char *address, bool bindto) {
+ char *port = myport.tcp;
+
+ if(address) {
+ char *space = strchr(address, ' ');
+
+ if(space) {
+ *space++ = 0;
+ port = space;
+ }
+
+ if(!strcmp(address, "*")) {
+ *address = 0;
+ }
+ }
+
+ struct addrinfo *ai, hint = {0};
+
+ hint.ai_family = addressfamily;
+
+ hint.ai_socktype = SOCK_STREAM;
+
+ hint.ai_protocol = IPPROTO_TCP;
+
+ hint.ai_flags = AI_PASSIVE;
+
+#if HAVE_DECL_RES_INIT
+ res_init();
+
+#endif
+ int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
+
+ free(address);
+
+ if(err || !ai) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
+ return false;
+ }
+
+ for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
+ // Ignore duplicate addresses
+ bool found = false;
+
+ for(int i = 0; i < listen_sockets; i++)
+ if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
+ found = true;
+ break;
+ }
+
+ if(found) {
+ continue;
+ }
+
+ if(listen_sockets >= MAXSOCKETS) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
+ freeaddrinfo(ai);
+ return false;
+ }
+
+ const sockaddr_t *sa = (sockaddr_t *) aip->ai_addr;
+ int from_fd = listen_socket[0].tcp.fd;
+
+ // If we're binding to a dynamically allocated (zero) port, try to get the actual
+ // port of the first TCP socket, and use it for this one. If that succeeds, our
+ // tincd instance will use the same port for all addresses it listens on.
+ int tcp_fd = bind_reusing_port(sa, from_fd, setup_listen_socket);
+
+ if(tcp_fd < 0) {
+ continue;
+ }
+
+ // If we just successfully bound the first socket, use it for the UDP procedure below.
+ // Otherwise, keep using the socket we've obtained from listen_socket[0].
+ if(!from_fd) {
+ from_fd = tcp_fd;
+ }
+
+ int udp_fd = bind_reusing_port(sa, from_fd, setup_vpn_in_socket);
+
+ if(udp_fd < 0) {
+ closesocket(tcp_fd);
+ continue;
+ }
+
+ listen_socket_t *sock = &listen_socket[listen_sockets];
+ io_add(&sock->tcp, handle_new_meta_connection, sock, tcp_fd, IO_READ);
+ io_add(&sock->udp, handle_incoming_vpn_data, sock, udp_fd, IO_READ);
+
+ if(debug_level >= DEBUG_CONNECTIONS) {
+ int tcp_port = get_bound_port(tcp_fd);
+ char *hostname = NULL;
+ sockaddr2str(sa, &hostname, NULL);
+ logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s port %d", hostname, tcp_port);
+ free(hostname);
+ }
+
+ sock->bindto = bindto;
+ memcpy(&sock->sa, aip->ai_addr, aip->ai_addrlen);
+ listen_sockets++;
+ }
+
+ freeaddrinfo(ai);
+ return true;
+}
+
+void device_enable(void) {
+ if(devops.enable) {
+ devops.enable();
+ }
+
+ /* Run tinc-up script to further initialize the tap interface */
+
+ environment_t env;
+ environment_init(&env);
+ execute_script("tinc-up", &env);
+ environment_exit(&env);
+}
+
+void device_disable(void) {
+ environment_t env;
+ environment_init(&env);
+ execute_script("tinc-down", &env);
+ environment_exit(&env);
+
+ if(devops.disable) {
+ devops.disable();
+ }
+}
+