diff options
author | irungentoo <irungentoo@gmail.com> | 2014-04-13 20:40:48 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2014-04-13 20:40:48 -0400 |
commit | b16af69d9252faf730d12d3a452bcd76b2cc686f (patch) | |
tree | b09fdfb9ab7efdf807dbdce704e5c5844b1eb594 /toxcore | |
parent | ecf0ff3e7f4f5517ae1b66f01aec3325638f7761 (diff) |
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.)
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/TCP_client.c | 79 | ||||
-rw-r--r-- | toxcore/TCP_client.h | 17 |
2 files changed, 93 insertions, 3 deletions
diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 18f63c5f..b8877018 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_ | |||
171 | * return 0 if could not send packet. | 171 | * return 0 if could not send packet. |
172 | * return -1 on failure (connection must be killed). | 172 | * return -1 on failure (connection must be killed). |
173 | */ | 173 | */ |
174 | static int send_routing_request(TCP_Client_Connection *con, uint8_t *public_key) | 174 | int send_routing_request(TCP_Client_Connection *con, uint8_t *public_key) |
175 | { | 175 | { |
176 | uint8_t packet[1 + crypto_box_PUBLICKEYBYTES]; | 176 | uint8_t packet[1 + crypto_box_PUBLICKEYBYTES]; |
177 | packet[0] = TCP_PACKET_ROUTING_REQUEST; | 177 | packet[0] = TCP_PACKET_ROUTING_REQUEST; |
@@ -179,6 +179,28 @@ static int send_routing_request(TCP_Client_Connection *con, uint8_t *public_key) | |||
179 | return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); | 179 | return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); |
180 | } | 180 | } |
181 | 181 | ||
182 | void routing_response_handler(TCP_Client_Connection *con, int (*response_callback)(void *object, uint8_t connection_id, | ||
183 | uint8_t *public_key), void *object) | ||
184 | { | ||
185 | con->response_callback = response_callback; | ||
186 | con->response_callback_object = object; | ||
187 | } | ||
188 | |||
189 | void routing_status_handler(TCP_Client_Connection *con, int (*status_callback)(void *object, uint8_t connection_id, | ||
190 | uint8_t status), void *object) | ||
191 | { | ||
192 | con->status_callback = status_callback; | ||
193 | con->status_callback_object = object; | ||
194 | } | ||
195 | |||
196 | void routing_data_handler(TCP_Client_Connection *con, int (*data_callback)(void *object, uint8_t connection_id, | ||
197 | uint8_t *data, uint16_t length), void *object) | ||
198 | { | ||
199 | con->data_callback = data_callback; | ||
200 | con->data_callback_object = object; | ||
201 | } | ||
202 | |||
203 | |||
182 | /* return 1 on success. | 204 | /* return 1 on success. |
183 | * return 0 if could not send packet. | 205 | * return 0 if could not send packet. |
184 | * return -1 on failure (connection must be killed). | 206 | * 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, | |||
290 | */ | 312 | */ |
291 | static int handle_TCP_packet(TCP_Client_Connection *conn, uint8_t *data, uint16_t length) | 313 | static int handle_TCP_packet(TCP_Client_Connection *conn, uint8_t *data, uint16_t length) |
292 | { | 314 | { |
293 | if (length == 0) | 315 | if (length <= 1) |
294 | return -1; | 316 | return -1; |
295 | 317 | ||
296 | switch (data[0]) { | 318 | switch (data[0]) { |
297 | case TCP_PACKET_ROUTING_RESPONSE: { | 319 | case TCP_PACKET_ROUTING_RESPONSE: { |
320 | if (length != 1 + 1 + crypto_box_PUBLICKEYBYTES) | ||
321 | return -1; | ||
322 | |||
323 | if (data[1] < NUM_RESERVED_PORTS) | ||
324 | return 0; | ||
325 | |||
326 | uint8_t con_id = data[1] - NUM_RESERVED_PORTS; | ||
327 | |||
328 | if (conn->connections[con_id].status != 0) | ||
329 | return -1; | ||
330 | |||
331 | conn->connections[con_id].status = 1; | ||
332 | memcpy(conn->connections[con_id].public_key, data + 2, crypto_box_PUBLICKEYBYTES); | ||
333 | |||
334 | if (conn->response_callback) | ||
335 | conn->response_callback(conn->response_callback_object, con_id, conn->connections[con_id].public_key); | ||
336 | |||
298 | return 0; | 337 | return 0; |
299 | } | 338 | } |
300 | 339 | ||
301 | case TCP_PACKET_CONNECTION_NOTIFICATION: { | 340 | case TCP_PACKET_CONNECTION_NOTIFICATION: { |
341 | if (length != 1 + 1) | ||
342 | return -1; | ||
343 | |||
344 | if (data[1] < NUM_RESERVED_PORTS) | ||
345 | return -1; | ||
346 | |||
347 | uint8_t con_id = data[1] - NUM_RESERVED_PORTS; | ||
348 | |||
349 | if (conn->connections[con_id].status != 1) | ||
350 | return -1; | ||
351 | |||
352 | conn->connections[con_id].status = 2; | ||
353 | |||
354 | if (conn->status_callback) | ||
355 | conn->status_callback(conn->status_callback_object, con_id, conn->connections[con_id].status); | ||
356 | |||
302 | return 0; | 357 | return 0; |
303 | } | 358 | } |
304 | 359 | ||
305 | case TCP_PACKET_DISCONNECT_NOTIFICATION: { | 360 | case TCP_PACKET_DISCONNECT_NOTIFICATION: { |
361 | if (length != 1 + 1) | ||
362 | return -1; | ||
363 | |||
364 | if (data[1] < NUM_RESERVED_PORTS) | ||
365 | return -1; | ||
366 | |||
367 | uint8_t con_id = data[1] - NUM_RESERVED_PORTS; | ||
368 | |||
369 | if (conn->connections[con_id].status != 2) | ||
370 | return -1; | ||
371 | |||
372 | conn->connections[con_id].status = 1; | ||
373 | |||
374 | if (conn->status_callback) | ||
375 | conn->status_callback(conn->status_callback_object, con_id, conn->connections[con_id].status); | ||
376 | |||
306 | return 0; | 377 | return 0; |
307 | } | 378 | } |
308 | 379 | ||
@@ -344,7 +415,9 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, uint8_t *data, uint16_ | |||
344 | return -1; | 415 | return -1; |
345 | 416 | ||
346 | uint8_t con_id = data[0] - NUM_RESERVED_PORTS; | 417 | uint8_t con_id = data[0] - NUM_RESERVED_PORTS; |
347 | //TODO | 418 | |
419 | if (conn->data_callback) | ||
420 | conn->data_callback(conn->data_callback_object, con_id, data + 1, length - 1); | ||
348 | } | 421 | } |
349 | } | 422 | } |
350 | 423 | ||
diff --git a/toxcore/TCP_client.h b/toxcore/TCP_client.h index e096ae62..66084a64 100644 --- a/toxcore/TCP_client.h +++ b/toxcore/TCP_client.h | |||
@@ -60,6 +60,12 @@ typedef struct { | |||
60 | uint8_t status; /* 0 if not used, 1 if other is offline, 2 if other is online. */ | 60 | uint8_t status; /* 0 if not used, 1 if other is offline, 2 if other is online. */ |
61 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 61 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
62 | } connections[NUM_CLIENT_CONNECTIONS]; | 62 | } connections[NUM_CLIENT_CONNECTIONS]; |
63 | int (*response_callback)(void *object, uint8_t connection_id, uint8_t *public_key); | ||
64 | void *response_callback_object; | ||
65 | int (*status_callback)(void *object, uint8_t connection_id, uint8_t status); | ||
66 | void *status_callback_object; | ||
67 | int (*data_callback)(void *object, uint8_t connection_id, uint8_t *data, uint16_t length); | ||
68 | void *data_callback_object; | ||
63 | 69 | ||
64 | int (*onion_callback)(void *object, uint8_t *data, uint16_t length); | 70 | int (*onion_callback)(void *object, uint8_t *data, uint16_t length); |
65 | void *onion_callback_object; | 71 | void *onion_callback_object; |
@@ -86,5 +92,16 @@ int send_onion_request(TCP_Client_Connection *con, uint8_t *data, uint16_t lengt | |||
86 | void onion_response_handler(TCP_Client_Connection *con, int (*onion_callback)(void *object, uint8_t *data, | 92 | void onion_response_handler(TCP_Client_Connection *con, int (*onion_callback)(void *object, uint8_t *data, |
87 | uint16_t length), void *object); | 93 | uint16_t length), void *object); |
88 | 94 | ||
95 | /* return 1 on success. | ||
96 | * return 0 if could not send packet. | ||
97 | * return -1 on failure (connection must be killed). | ||
98 | */ | ||
99 | int send_routing_request(TCP_Client_Connection *con, uint8_t *public_key); | ||
100 | void routing_response_handler(TCP_Client_Connection *con, int (*response_callback)(void *object, uint8_t connection_id, | ||
101 | uint8_t *public_key), void *object); | ||
102 | void routing_status_handler(TCP_Client_Connection *con, int (*status_callback)(void *object, uint8_t connection_id, | ||
103 | uint8_t status), void *object); | ||
104 | void routing_data_handler(TCP_Client_Connection *con, int (*data_callback)(void *object, uint8_t connection_id, | ||
105 | uint8_t *data, uint16_t length), void *object); | ||
89 | 106 | ||
90 | #endif | 107 | #endif |