summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-03-19 20:19:51 -0400
committerirungentoo <irungentoo@gmail.com>2014-03-19 20:19:51 -0400
commitd0136733665e79484fc42e72d792a3c1f9383824 (patch)
tree5a35aa16354921152bc5e85c1202ce40f1d65fe7 /toxcore
parent91838f8c2d3f3bb30f23d20aeba4691ce797fb45 (diff)
A bit more code written for TCP servers.
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/TCP_server.c110
-rw-r--r--toxcore/TCP_server.h23
2 files changed, 128 insertions, 5 deletions
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c
index 15212d84..2c43ff14 100644
--- a/toxcore/TCP_server.c
+++ b/toxcore/TCP_server.c
@@ -109,6 +109,83 @@ static int bind_to_port(sock_t sock, int family, uint16_t port)
109 return (bind(sock, (struct sockaddr *)&addr, addrsize) == 0); 109 return (bind(sock, (struct sockaddr *)&addr, addrsize) == 0);
110} 110}
111 111
112/* Set the size of the connection list to numfriends.
113 *
114 * return -1 if realloc fails.
115 * return 0 if it succeeds.
116 */
117static int realloc_connection(TCP_Server *TCP_server, uint32_t num)
118{
119 if (num == 0) {
120 free(TCP_server->accepted_connection_array);
121 TCP_server->accepted_connection_array = NULL;
122 TCP_server->size_accepted_connections = 0;
123 return 0;
124 }
125
126 TCP_Secure_Connection *new_connections = realloc(TCP_server->accepted_connection_array,
127 num * sizeof(TCP_Secure_Connection));
128
129 if (new_connections == NULL)
130 return -1;
131
132 TCP_server->accepted_connection_array = new_connections;
133 TCP_server->size_accepted_connections = num;
134 return 0;
135}
136
137/* Add accepted TCP connection to the list.
138 *
139 * return index on success
140 * return -1 on failure
141 */
142static int add_accepted(TCP_Server *TCP_server, TCP_Secure_Connection *con)
143{
144 int index = -1;
145
146 if (TCP_server->size_accepted_connections == TCP_server->num_accepted_connections) {
147 if (realloc_connection(TCP_server, TCP_server->size_accepted_connections + 4) == -1)
148 return -1;
149
150 index = TCP_server->num_accepted_connections;
151 } else {
152 uint32_t i;
153
154 for (i = TCP_server->size_accepted_connections; i != 0; --i) {
155 if (TCP_server->accepted_connection_array[i - 1].status == TCP_STATUS_NO_STATUS) {
156 index = i - 1;
157 break;
158 }
159 }
160 }
161
162 if (index == -1) {
163 fprintf(stderr, "FAIL index is -1\n");
164 return -1;
165 }
166
167 memcpy(&TCP_server->accepted_connection_array[index], con, sizeof(TCP_Secure_Connection));
168 ++TCP_server->num_accepted_connections;
169 return index;
170}
171
172static int del_accepted(TCP_Server *TCP_server, int index)
173{
174 if ((uint32_t)index >= TCP_server->size_accepted_connections)
175 return -1;
176
177 if (TCP_server->accepted_connection_array[index].status == TCP_STATUS_NO_STATUS)
178 return -1;
179
180 memset(&TCP_server->accepted_connection_array[index], 0, sizeof(TCP_Secure_Connection));
181 --TCP_server->num_accepted_connections;
182
183 if (TCP_server->num_accepted_connections == 0)
184 realloc_connection(TCP_server, 0);
185
186 return 0;
187}
188
112/* return length on success 189/* return length on success
113 * return 0 if nothing has been read from socket. 190 * return 0 if nothing has been read from socket.
114 * return ~0 on failure. 191 * return ~0 on failure.
@@ -308,10 +385,25 @@ static int read_connection_handshake(TCP_Secure_Connection *con, uint8_t *self_s
308 return 0; 385 return 0;
309} 386}
310 387
388/* return 0 on success
389 * return -1 on failure
390 */
391static int handle_TCP_packet(TCP_Secure_Connection *con, uint8_t *data, uint16_t length)
392{
393
394 return 0;
395}
396
311 397
312static int confirm_TCP_connection(TCP_Secure_Connection *con, uint8_t *data, uint16_t length) 398static int confirm_TCP_connection(TCP_Server *TCP_server, TCP_Secure_Connection *con, uint8_t *data, uint16_t length)
313{ 399{
400 int index = add_accepted(TCP_server, con);
314 401
402 if (index == -1)
403 return -1;
404
405 //TODO
406 //handle_TCP_packet(TCP_Secure_Connection *con, data, length);
315 return 0; 407 return 0;
316} 408}
317 409
@@ -469,16 +561,26 @@ static void do_TCP_unconfirmed(TCP_Server *TCP_server)
469 kill_TCP_connection(conn); 561 kill_TCP_connection(conn);
470 continue; 562 continue;
471 } else { 563 } else {
472 //TODO 564 if (confirm_TCP_connection(TCP_server, conn, packet, len) == -1) {
473 confirm_TCP_connection(conn, packet, len); 565 kill_TCP_connection(conn);
474 kill_TCP_connection(conn); 566 } else {
567 memset(conn, 0, sizeof(TCP_Secure_Connection));
568 }
475 } 569 }
476 } 570 }
477} 571}
572
573static void do_TCP_confirmed(TCP_Server *TCP_server)
574{
575 uint32_t i;
576
577}
578
478void do_TCP_server(TCP_Server *TCP_server) 579void do_TCP_server(TCP_Server *TCP_server)
479{ 580{
480 do_TCP_accept_new(TCP_server); 581 do_TCP_accept_new(TCP_server);
481 do_TCP_incomming(TCP_server); 582 do_TCP_incomming(TCP_server);
583 do_TCP_confirmed(TCP_server);
482} 584}
483 585
484void kill_TCP_server(TCP_Server *TCP_server) 586void kill_TCP_server(TCP_Server *TCP_server)
diff --git a/toxcore/TCP_server.h b/toxcore/TCP_server.h
index 89ccb50f..504ffd77 100644
--- a/toxcore/TCP_server.h
+++ b/toxcore/TCP_server.h
@@ -32,6 +32,18 @@
32#define TCP_SERVER_HANDSHAKE_SIZE (crypto_box_NONCEBYTES + TCP_HANDSHAKE_PLAIN_SIZE + crypto_box_MACBYTES) 32#define TCP_SERVER_HANDSHAKE_SIZE (crypto_box_NONCEBYTES + TCP_HANDSHAKE_PLAIN_SIZE + crypto_box_MACBYTES)
33#define TCP_CLIENT_HANDSHAKE_SIZE (crypto_box_PUBLICKEYBYTES + TCP_SERVER_HANDSHAKE_SIZE) 33#define TCP_CLIENT_HANDSHAKE_SIZE (crypto_box_PUBLICKEYBYTES + TCP_SERVER_HANDSHAKE_SIZE)
34 34
35#define NUM_RESERVED_PORTS 16
36#define NUM_CLIENT_CONNECTIONS (256 - NUM_RESERVED_PORTS)
37
38#define TCP_PACKET_ROUTING_REQUEST 0
39#define TCP_PACKET_ROUTING_RESPONSE 1
40#define TCP_PACKET_CONNECTION_NOTIFICATION 2
41#define TCP_PACKET_DISCONNECT_NOTIFICATION 3
42#define TCP_PACKET_ONION_REQUEST 8
43#define TCP_PACKET_ONION_RESPONSE 9
44
45#define ARRAY_ENTRY_SIZE 6
46
35enum { 47enum {
36 TCP_STATUS_NO_STATUS, 48 TCP_STATUS_NO_STATUS,
37 TCP_STATUS_CONNECTED, 49 TCP_STATUS_CONNECTED,
@@ -39,7 +51,7 @@ enum {
39 TCP_STATUS_CONFIRMED, 51 TCP_STATUS_CONFIRMED,
40}; 52};
41 53
42typedef struct { 54typedef struct TCP_Secure_Connection {
43 uint8_t status; 55 uint8_t status;
44 sock_t sock; 56 sock_t sock;
45 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 57 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
@@ -47,8 +59,13 @@ typedef struct {
47 uint8_t sent_nonce[crypto_box_NONCEBYTES]; /* Nonce of sent packets. */ 59 uint8_t sent_nonce[crypto_box_NONCEBYTES]; /* Nonce of sent packets. */
48 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 60 uint8_t shared_key[crypto_box_BEFORENMBYTES];
49 uint16_t next_packet_length; 61 uint16_t next_packet_length;
62 struct {
63 struct TCP_Secure_Connection *connection;
64 uint8_t other_id;
65 } connections[NUM_CLIENT_CONNECTIONS];
50} TCP_Secure_Connection; 66} TCP_Secure_Connection;
51 67
68
52typedef struct { 69typedef struct {
53 sock_t *socks_listening; 70 sock_t *socks_listening;
54 unsigned int num_listening_socks; 71 unsigned int num_listening_socks;
@@ -59,6 +76,10 @@ typedef struct {
59 uint16_t incomming_connection_queue_index; 76 uint16_t incomming_connection_queue_index;
60 TCP_Secure_Connection unconfirmed_connection_queue[MAX_INCOMMING_CONNECTIONS]; 77 TCP_Secure_Connection unconfirmed_connection_queue[MAX_INCOMMING_CONNECTIONS];
61 uint16_t unconfirmed_connection_queue_index; 78 uint16_t unconfirmed_connection_queue_index;
79
80 TCP_Secure_Connection *accepted_connection_array;
81 uint32_t size_accepted_connections;
82 uint32_t num_accepted_connections;
62} TCP_Server; 83} TCP_Server;
63 84
64/* Create new TCP server instance. 85/* Create new TCP server instance.