+ for(i = 0; i < listen_sockets; i++) {
+ salen = sizeof sa;
+ if(getsockname(i + 3, &sa.sa, &salen) < 0) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno));
+ return false;
+ }
+
+ listen_socket[i].tcp = i + 3;
+
+#ifdef FD_CLOEXEC
+ fcntl(i + 3, F_SETFD, FD_CLOEXEC);
+#endif
+
+ listen_socket[i].udp = setup_vpn_in_socket(&sa);
+ if(listen_socket[i].udp < 0)
+ return false;
+
+ event_set(&listen_socket[i].ev_tcp, listen_socket[i].tcp, EV_READ|EV_PERSIST, handle_new_meta_connection, NULL);
+ if(event_add(&listen_socket[i].ev_tcp, NULL) < 0) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
+ abort();
+ }
+
+ event_set(&listen_socket[i].ev_udp, listen_socket[i].udp, EV_READ|EV_PERSIST, handle_incoming_vpn_data, (void *)(intptr_t)listen_sockets);
+ if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
+ abort();
+ }
+
+ if(debug_level >= DEBUG_CONNECTIONS) {
+ hostname = sockaddr2hostname(&sa);
+ logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
+ free(hostname);
+ }
+
+ memcpy(&listen_socket[i].sa, &sa, salen);
+ }
+ } else {
+ listen_sockets = 0;
+ cfg = lookup_config(config_tree, "BindToAddress");
+
+ do {
+ get_config_string(cfg, &address);
+ 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 && *address ? address : NULL, port, &hint, &ai);
+ free(address);