mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
TCP_client pretty much done?
Now next step is integrating it in tox. Added TCP server functionality to bootstrap server (define TCP_RELAY_ENABLED to enable it.)
This commit is contained in:
parent
ecf0ff3e7f
commit
b16af69d92
|
@ -31,6 +31,10 @@
|
||||||
#include "../toxcore/friend_requests.h"
|
#include "../toxcore/friend_requests.h"
|
||||||
#include "../toxcore/util.h"
|
#include "../toxcore/util.h"
|
||||||
|
|
||||||
|
#ifdef TCP_RELAY_ENABLED
|
||||||
|
#include "../toxcore/TCP_server.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "../testing/misc_tools.c"
|
#include "../testing/misc_tools.c"
|
||||||
|
|
||||||
#ifdef DHT_NODE_EXTRA_PACKETS
|
#ifdef DHT_NODE_EXTRA_PACKETS
|
||||||
|
@ -129,6 +133,18 @@ int main(int argc, char *argv[])
|
||||||
printf("Public key: ");
|
printf("Public key: ");
|
||||||
uint32_t i;
|
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 *file;
|
||||||
file = fopen("PUBLIC_ID.txt", "w");
|
file = fopen("PUBLIC_ID.txt", "w");
|
||||||
|
|
||||||
|
@ -177,6 +193,9 @@ int main(int argc, char *argv[])
|
||||||
last_LANdiscovery = unix_time();
|
last_LANdiscovery = unix_time();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef TCP_RELAY_ENABLED
|
||||||
|
do_TCP_server(tcp_s);
|
||||||
|
#endif
|
||||||
networking_poll(dht->c->lossless_udp->net);
|
networking_poll(dht->c->lossless_udp->net);
|
||||||
|
|
||||||
c_sleep(1);
|
c_sleep(1);
|
||||||
|
|
|
@ -171,7 +171,7 @@ static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, uint8_
|
||||||
* return 0 if could not send packet.
|
* return 0 if could not send packet.
|
||||||
* return -1 on failure (connection must be killed).
|
* 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];
|
uint8_t packet[1 + crypto_box_PUBLICKEYBYTES];
|
||||||
packet[0] = TCP_PACKET_ROUTING_REQUEST;
|
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));
|
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 1 on success.
|
||||||
* return 0 if could not send packet.
|
* return 0 if could not send packet.
|
||||||
* return -1 on failure (connection must be killed).
|
* 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)
|
static int handle_TCP_packet(TCP_Client_Connection *conn, uint8_t *data, uint16_t length)
|
||||||
{
|
{
|
||||||
if (length == 0)
|
if (length <= 1)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
switch (data[0]) {
|
switch (data[0]) {
|
||||||
case TCP_PACKET_ROUTING_RESPONSE: {
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
case TCP_PACKET_CONNECTION_NOTIFICATION: {
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
case TCP_PACKET_DISCONNECT_NOTIFICATION: {
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -344,7 +415,9 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, uint8_t *data, uint16_
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
uint8_t con_id = data[0] - NUM_RESERVED_PORTS;
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 status; /* 0 if not used, 1 if other is offline, 2 if other is online. */
|
||||||
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
|
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
|
||||||
} connections[NUM_CLIENT_CONNECTIONS];
|
} 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);
|
int (*onion_callback)(void *object, uint8_t *data, uint16_t length);
|
||||||
void *onion_callback_object;
|
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,
|
void onion_response_handler(TCP_Client_Connection *con, int (*onion_callback)(void *object, uint8_t *data,
|
||||||
uint16_t length), void *object);
|
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
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user