/* Add a TCP relay tied to a connection. * * return 0 on success. * return -1 on failure. */ int add_tcp_number_relay_connection(TCP_Connections *tcp_c, int connections_number, unsigned int tcp_connections_number) { TCP_Connection_to *con_to = get_connection(tcp_c, connections_number); if (!con_to) { return -1; } TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); if (!tcp_con) { return -1; } if (con_to->status != TCP_CONN_SLEEPING && tcp_con->status == TCP_CONN_SLEEPING) { tcp_con->unsleep = 1; } if (add_tcp_connection_to_conn(con_to, tcp_connections_number) == -1) { return -1; } if (tcp_con->status == TCP_CONN_CONNECTED) { if (send_tcp_relay_routing_request(tcp_c, tcp_connections_number, con_to->public_key) == 0) { tcp_con->connected_time = unix_time(); } } return 0; }
/* Add a TCP relay tied to a connection. * * return 0 on success. * return -1 on failure. */ int add_tcp_relay_connection(TCP_Connections *tcp_c, int connections_number, IP_Port ip_port, const uint8_t *relay_pk) { TCP_Connection_to *con_to = get_connection(tcp_c, connections_number); if (!con_to) return -1; int tcp_connections_number = find_tcp_connection_relay(tcp_c, relay_pk); if (tcp_connections_number != -1) { if (add_tcp_connection_to_conn(con_to, tcp_connections_number) == -1) return -1; TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); if (!tcp_con) return -1; ++tcp_con->lock_count; if (tcp_con->status == TCP_CONN_CONNECTED) { send_tcp_relay_routing_request(tcp_c, tcp_connections_number, con_to->public_key); } return 0; } else { int tcp_connections_number = add_tcp_relay(tcp_c, ip_port, relay_pk); if (add_tcp_connection_to_conn(con_to, tcp_connections_number) == -1) { return -1; } TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); if (!tcp_con) return -1; ++tcp_con->lock_count; return 0; } }
static int tcp_relay_on_online(TCP_Connections *tcp_c, int tcp_connections_number) { TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); if (!tcp_con) { return -1; } unsigned int i, sent = 0; for (i = 0; i < tcp_c->connections_length; ++i) { TCP_Connection_to *con_to = get_connection(tcp_c, i); if (con_to) { if (tcp_connection_in_conn(con_to, tcp_connections_number)) { if (send_tcp_relay_routing_request(tcp_c, tcp_connections_number, con_to->public_key) == 0) { ++sent; } } } } tcp_relay_set_callbacks(tcp_c, tcp_connections_number); tcp_con->status = TCP_CONN_CONNECTED; /* If this connection isn't used by any connection, we don't need to wait for them to come online. */ if (sent) { tcp_con->connected_time = unix_time(); } else { tcp_con->connected_time = 0; } if (tcp_c->onion_status && tcp_c->onion_num_conns < NUM_ONION_TCP_CONNECTIONS) { tcp_con->onion = 1; ++tcp_c->onion_num_conns; } return 0; }
/* Add a TCP relay tied to a connection. * * return 0 on success. * return -1 on failure. */ int add_tcp_number_relay_connection(TCP_Connections *tcp_c, int connections_number, unsigned int tcp_connections_number) { TCP_Connection_to *con_to = get_connection(tcp_c, connections_number); if (!con_to) return -1; TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); if (!tcp_con) return -1; if (add_tcp_connection_to_conn(con_to, tcp_connections_number) == -1) return -1; ++tcp_con->lock_count; if (tcp_con->status == TCP_CONN_CONNECTED) { send_tcp_relay_routing_request(tcp_c, tcp_connections_number, con_to->public_key); } return 0; }
static int tcp_relay_on_online(TCP_Connections *tcp_c, int tcp_connections_number) { TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); if (!tcp_con) return -1; unsigned int i; for (i = 0; i < tcp_c->connections_length; ++i) { TCP_Connection_to *con_to = get_connection(tcp_c, i); if (con_to) { if (tcp_connection_in_conn(con_to, tcp_connections_number)) { send_tcp_relay_routing_request(tcp_c, tcp_connections_number, con_to->public_key); } } } tcp_relay_set_callbacks(tcp_c, tcp_connections_number); tcp_con->status = TCP_CONN_CONNECTED; return 0; }