summaryrefslogtreecommitdiff
path: root/toxcore/TCP_server.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/TCP_server.c')
-rw-r--r--toxcore/TCP_server.c113
1 files changed, 108 insertions, 5 deletions
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c
index df56f490..15212d84 100644
--- a/toxcore/TCP_server.c
+++ b/toxcore/TCP_server.c
@@ -115,12 +115,17 @@ static int bind_to_port(sock_t sock, int family, uint16_t port)
115 */ 115 */
116static uint16_t read_length(sock_t sock) 116static uint16_t read_length(sock_t sock)
117{ 117{
118 int count; 118#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
119 unsigned long count = 0;
120 ioctlsocket(sock, FIONREAD, &count);
121#else
122 int count = 0;
119 ioctl(sock, FIONREAD, &count); 123 ioctl(sock, FIONREAD, &count);
124#endif
120 125
121 if ((unsigned int)count >= sizeof(uint16_t)) { 126 if ((unsigned int)count >= sizeof(uint16_t)) {
122 uint16_t length; 127 uint16_t length;
123 int len = recv(sock, &length, sizeof(uint16_t), 0); 128 int len = recv(sock, (uint8_t *)&length, sizeof(uint16_t), 0);
124 129
125 if (len != sizeof(uint16_t)) { 130 if (len != sizeof(uint16_t)) {
126 fprintf(stderr, "FAIL recv packet\n"); 131 fprintf(stderr, "FAIL recv packet\n");
@@ -144,8 +149,13 @@ static uint16_t read_length(sock_t sock)
144 */ 149 */
145static int read_TCP_packet(sock_t sock, uint8_t *data, uint16_t length) 150static int read_TCP_packet(sock_t sock, uint8_t *data, uint16_t length)
146{ 151{
147 int count; 152#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
153 unsigned long count = 0;
154 ioctlsocket(sock, FIONREAD, &count);
155#else
156 int count = 0;
148 ioctl(sock, FIONREAD, &count); 157 ioctl(sock, FIONREAD, &count);
158#endif
149 159
150 if (count >= length) { 160 if (count >= length) {
151 int len = recv(sock, data, length, 0); 161 int len = recv(sock, data, length, 0);
@@ -155,12 +165,82 @@ static int read_TCP_packet(sock_t sock, uint8_t *data, uint16_t length)
155 return -1; 165 return -1;
156 } 166 }
157 167
158 return length; 168 return len;
159 } 169 }
160 170
161 return -1; 171 return -1;
162} 172}
163 173
174/* return length of recieved packet on success.
175 * return 0 if could not read any packet.
176 * return -1 on failure (connection must be killed).
177 */
178static int read_packet_TCP_secure_connection(TCP_Secure_Connection *con, uint8_t *data, uint16_t max_len)
179{
180 if (con->next_packet_length == 0) {
181 uint16_t len = read_length(con->sock);
182
183 if (len == (uint16_t)~0)
184 return -1;
185
186 if (len == 0)
187 return 0;
188
189 con->next_packet_length = len;
190 }
191
192 if (max_len + crypto_box_MACBYTES < con->next_packet_length)
193 return -1;
194
195 uint8_t data_encrypted[con->next_packet_length];
196 int len_packet = read_TCP_packet(con->sock, data_encrypted, con->next_packet_length);
197
198 if (len_packet != con->next_packet_length)
199 return 0;
200
201 con->next_packet_length = 0;
202
203 int len = decrypt_data_fast(con->shared_key, con->recv_nonce, data_encrypted, len_packet, data);
204
205 if (len + crypto_box_MACBYTES != len_packet)
206 return -1;
207
208 increment_nonce(con->recv_nonce);
209
210 return len;
211}
212
213/* return 1 on success.
214 * return 0 if could not send packet.
215 * return -1 on failure (connection must be killed).
216 */
217static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, uint8_t *data, uint16_t length)
218{
219 if (length + crypto_box_MACBYTES > MAX_PACKET_SIZE)
220 return -1;
221
222 uint8_t packet[sizeof(uint16_t) + length + crypto_box_MACBYTES];
223
224 length = htons(length);
225 memcpy(packet, &length, sizeof(uint16_t));
226 uint32_t len = encrypt_data_fast(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t));
227
228 if (len != (sizeof(packet) - sizeof(uint16_t)))
229 return -1;
230
231 increment_nonce(con->sent_nonce);
232
233 len = send(con->sock, packet, sizeof(packet), 0);
234
235 if (len == sizeof(packet))
236 return 1;
237
238 if (len <= 0)
239 return 0;
240
241 return -1;
242}
243
164/* Kill a TCP_Secure_Connection 244/* Kill a TCP_Secure_Connection
165 */ 245 */
166static void kill_TCP_connection(TCP_Secure_Connection *con) 246static void kill_TCP_connection(TCP_Secure_Connection *con)
@@ -228,6 +308,13 @@ static int read_connection_handshake(TCP_Secure_Connection *con, uint8_t *self_s
228 return 0; 308 return 0;
229} 309}
230 310
311
312static int confirm_TCP_connection(TCP_Secure_Connection *con, uint8_t *data, uint16_t length)
313{
314
315 return 0;
316}
317
231/* return 1 on success 318/* return 1 on success
232 * return 0 on failure 319 * return 0 on failure
233 */ 320 */
@@ -368,8 +455,24 @@ static void do_TCP_unconfirmed(TCP_Server *TCP_server)
368 uint32_t i; 455 uint32_t i;
369 456
370 for (i = 0; i < MAX_INCOMMING_CONNECTIONS; ++i) { 457 for (i = 0; i < MAX_INCOMMING_CONNECTIONS; ++i) {
371 if (TCP_server->incomming_connection_queue[i].status != TCP_STATUS_CONNECTED) 458 TCP_Secure_Connection *conn = &TCP_server->unconfirmed_connection_queue[i];
459
460 if (conn->status != TCP_STATUS_UNCONFIRMED)
372 continue; 461 continue;
462
463 uint8_t packet[MAX_PACKET_SIZE];
464 int len = read_packet_TCP_secure_connection(conn, packet, sizeof(packet));
465
466 if (len == 0) {
467 continue;
468 } else if (len == -1) {
469 kill_TCP_connection(conn);
470 continue;
471 } else {
472 //TODO
473 confirm_TCP_connection(conn, packet, len);
474 kill_TCP_connection(conn);
475 }
373 } 476 }
374} 477}
375void do_TCP_server(TCP_Server *TCP_server) 478void do_TCP_server(TCP_Server *TCP_server)