/* Returns a new TCP_Connections object associated with the secret_key. * * In order for others to connect to this instance new_tcp_connection_to() must be called with the * public_key associated with secret_key. * * Returns NULL on failure. */ TCP_Connections *new_tcp_connections(const uint8_t *secret_key, TCP_Proxy_Info *proxy_info) { if (secret_key == nullptr) { return nullptr; } TCP_Connections *temp = (TCP_Connections *)calloc(1, sizeof(TCP_Connections)); if (temp == nullptr) { return nullptr; } memcpy(temp->self_secret_key, secret_key, CRYPTO_SECRET_KEY_SIZE); crypto_derive_public_key(temp->self_public_key, temp->self_secret_key); temp->proxy_info = *proxy_info; return temp; }
TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, const uint8_t *secret_key, Onion *onion) { if (num_sockets == 0 || ports == NULL) { return NULL; } if (networking_at_startup() != 0) { return NULL; } TCP_Server *temp = (TCP_Server *)calloc(1, sizeof(TCP_Server)); if (temp == NULL) { return NULL; } temp->socks_listening = (Socket *)calloc(num_sockets, sizeof(Socket)); if (temp->socks_listening == NULL) { free(temp); return NULL; } #ifdef TCP_SERVER_USE_EPOLL temp->efd = epoll_create(8); if (temp->efd == -1) { free(temp->socks_listening); free(temp); return NULL; } #endif uint8_t family; if (ipv6_enabled) { family = AF_INET6; } else { family = AF_INET; } uint32_t i; #ifdef TCP_SERVER_USE_EPOLL struct epoll_event ev; #endif for (i = 0; i < num_sockets; ++i) { Socket sock = new_listening_TCP_socket(family, ports[i]); if (sock_valid(sock)) { #ifdef TCP_SERVER_USE_EPOLL ev.events = EPOLLIN | EPOLLET; ev.data.u64 = sock | ((uint64_t)TCP_SOCKET_LISTENING << 32); if (epoll_ctl(temp->efd, EPOLL_CTL_ADD, sock, &ev) == -1) { continue; } #endif temp->socks_listening[temp->num_listening_socks] = sock; ++temp->num_listening_socks; } } if (temp->num_listening_socks == 0) { free(temp->socks_listening); free(temp); return NULL; } if (onion) { temp->onion = onion; set_callback_handle_recv_1(onion, &handle_onion_recv_1, temp); } memcpy(temp->secret_key, secret_key, CRYPTO_SECRET_KEY_SIZE); crypto_derive_public_key(temp->public_key, temp->secret_key); bs_list_init(&temp->accepted_key_list, CRYPTO_PUBLIC_KEY_SIZE, 8); return temp; }