diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index 291d65894..50d473a9d 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -31,6 +31,10 @@ #include "../toxcore/friend_requests.h" #include "../toxcore/util.h" +#ifdef TCP_RELAY_ENABLED +#include "../toxcore/TCP_server.h" +#endif + #include "../testing/misc_tools.c" #ifdef DHT_NODE_EXTRA_PACKETS @@ -129,6 +133,18 @@ int main(int argc, char *argv[]) printf("Public key: "); uint32_t i; +#ifdef TCP_RELAY_ENABLED +#define NUM_PORTS 3 + uint16_t ports[NUM_PORTS] = {443, 3389, PORT}; + TCP_Server *tcp_s = new_TCP_server(ipv6enabled, NUM_PORTS, ports, dht->self_public_key, dht->self_secret_key, onion); + + if (tcp_s == NULL) { + printf("TCP server failed to initialize.\n"); + exit(1); + } + +#endif + FILE *file; file = fopen("PUBLIC_ID.txt", "w"); @@ -177,6 +193,9 @@ int main(int argc, char *argv[]) last_LANdiscovery = unix_time(); } +#ifdef TCP_RELAY_ENABLED + do_TCP_server(tcp_s); +#endif networking_poll(dht->c->lossless_udp->net); c_sleep(1); diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 18f63c5f6..b88770189 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -171,7 +171,7 @@ static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, uint8_ * return 0 if could not send packet. * return -1 on failure (connection must be killed). */ -static int send_routing_request(TCP_Client_Connection *con, uint8_t *public_key) +int send_routing_request(TCP_Client_Connection *con, uint8_t *public_key) { uint8_t packet[1 + crypto_box_PUBLICKEYBYTES]; packet[0] = TCP_PACKET_ROUTING_REQUEST; @@ -179,6 +179,28 @@ static int send_routing_request(TCP_Client_Connection *con, uint8_t *public_key) return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); } +void routing_response_handler(TCP_Client_Connection *con, int (*response_callback)(void *object, uint8_t connection_id, + uint8_t *public_key), void *object) +{ + con->response_callback = response_callback; + con->response_callback_object = object; +} + +void routing_status_handler(TCP_Client_Connection *con, int (*status_callback)(void *object, uint8_t connection_id, + uint8_t status), void *object) +{ + con->status_callback = status_callback; + con->status_callback_object = object; +} + +void routing_data_handler(TCP_Client_Connection *con, int (*data_callback)(void *object, uint8_t connection_id, + uint8_t *data, uint16_t length), void *object) +{ + con->data_callback = data_callback; + con->data_callback_object = object; +} + + /* return 1 on success. * return 0 if could not send packet. * return -1 on failure (connection must be killed). @@ -290,19 +312,68 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, uint8_t *public_key, */ static int handle_TCP_packet(TCP_Client_Connection *conn, uint8_t *data, uint16_t length) { - if (length == 0) + if (length <= 1) return -1; switch (data[0]) { case TCP_PACKET_ROUTING_RESPONSE: { + if (length != 1 + 1 + crypto_box_PUBLICKEYBYTES) + return -1; + + if (data[1] < NUM_RESERVED_PORTS) + return 0; + + uint8_t con_id = data[1] - NUM_RESERVED_PORTS; + + if (conn->connections[con_id].status != 0) + return -1; + + conn->connections[con_id].status = 1; + memcpy(conn->connections[con_id].public_key, data + 2, crypto_box_PUBLICKEYBYTES); + + if (conn->response_callback) + conn->response_callback(conn->response_callback_object, con_id, conn->connections[con_id].public_key); + return 0; } case TCP_PACKET_CONNECTION_NOTIFICATION: { + if (length != 1 + 1) + return -1; + + if (data[1] < NUM_RESERVED_PORTS) + return -1; + + uint8_t con_id = data[1] - NUM_RESERVED_PORTS; + + if (conn->connections[con_id].status != 1) + return -1; + + conn->connections[con_id].status = 2; + + if (conn->status_callback) + conn->status_callback(conn->status_callback_object, con_id, conn->connections[con_id].status); + return 0; } case TCP_PACKET_DISCONNECT_NOTIFICATION: { + if (length != 1 + 1) + return -1; + + if (data[1] < NUM_RESERVED_PORTS) + return -1; + + uint8_t con_id = data[1] - NUM_RESERVED_PORTS; + + if (conn->connections[con_id].status != 2) + return -1; + + conn->connections[con_id].status = 1; + + if (conn->status_callback) + conn->status_callback(conn->status_callback_object, con_id, conn->connections[con_id].status); + return 0; } @@ -344,7 +415,9 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, uint8_t *data, uint16_ return -1; uint8_t con_id = data[0] - NUM_RESERVED_PORTS; - //TODO + + if (conn->data_callback) + conn->data_callback(conn->data_callback_object, con_id, data + 1, length - 1); } } diff --git a/toxcore/TCP_client.h b/toxcore/TCP_client.h index e096ae620..66084a642 100644 --- a/toxcore/TCP_client.h +++ b/toxcore/TCP_client.h @@ -60,6 +60,12 @@ typedef struct { uint8_t status; /* 0 if not used, 1 if other is offline, 2 if other is online. */ uint8_t public_key[crypto_box_PUBLICKEYBYTES]; } connections[NUM_CLIENT_CONNECTIONS]; + int (*response_callback)(void *object, uint8_t connection_id, uint8_t *public_key); + void *response_callback_object; + int (*status_callback)(void *object, uint8_t connection_id, uint8_t status); + void *status_callback_object; + int (*data_callback)(void *object, uint8_t connection_id, uint8_t *data, uint16_t length); + void *data_callback_object; int (*onion_callback)(void *object, uint8_t *data, uint16_t length); void *onion_callback_object; @@ -86,5 +92,16 @@ int send_onion_request(TCP_Client_Connection *con, uint8_t *data, uint16_t lengt void onion_response_handler(TCP_Client_Connection *con, int (*onion_callback)(void *object, uint8_t *data, uint16_t length), void *object); +/* return 1 on success. + * return 0 if could not send packet. + * return -1 on failure (connection must be killed). + */ +int send_routing_request(TCP_Client_Connection *con, uint8_t *public_key); +void routing_response_handler(TCP_Client_Connection *con, int (*response_callback)(void *object, uint8_t connection_id, + uint8_t *public_key), void *object); +void routing_status_handler(TCP_Client_Connection *con, int (*status_callback)(void *object, uint8_t connection_id, + uint8_t status), void *object); +void routing_data_handler(TCP_Client_Connection *con, int (*data_callback)(void *object, uint8_t connection_id, + uint8_t *data, uint16_t length), void *object); #endif