- for(aip = ai; aip; aip = aip->ai_next) {
- if(listen_sockets >= MAXSOCKETS) {
- logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
- return false;
- }
-
- listen_socket[listen_sockets].tcp =
- setup_listen_socket((sockaddr_t *) aip->ai_addr);
-
- if(listen_socket[listen_sockets].tcp < 0)
- continue;
-
- listen_socket[listen_sockets].udp =
- setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
-
- if(listen_socket[listen_sockets].udp < 0) {
- close(listen_socket[listen_sockets].tcp);
- continue;
- }
-
- event_set(&listen_socket[listen_sockets].ev_tcp,
- listen_socket[listen_sockets].tcp,
- EV_READ|EV_PERSIST,
- handle_new_meta_connection, NULL);
- if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
- logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
- abort();
- }
-
- event_set(&listen_socket[listen_sockets].ev_udp,
- listen_socket[listen_sockets].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((sockaddr_t *) aip->ai_addr);
- logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
- free(hostname);
- }
-
- memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
- listen_sockets++;
- }
-
- freeaddrinfo(ai);
- } while(cfg);
- }
-
- if(listen_sockets)
- logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready");
- else {
- logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
- return false;
- }
-
- last_config_check = time(NULL);