summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/TCP_server.c113
-rw-r--r--toxcore/net_crypto.c2
-rw-r--r--toxcore/net_crypto.h3
-rw-r--r--toxcore/network.c4
-rw-r--r--toxcore/network.h4
5 files changed, 116 insertions, 10 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)
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index d2dbc90f..17d2e8ff 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -159,7 +159,7 @@ int decrypt_data_symmetric(uint8_t *secret_key, uint8_t *nonce, uint8_t *encrypt
159} 159}
160 160
161/* Increment the given nonce by 1. */ 161/* Increment the given nonce by 1. */
162static void increment_nonce(uint8_t *nonce) 162void increment_nonce(uint8_t *nonce)
163{ 163{
164 uint32_t i; 164 uint32_t i;
165 165
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index 74c3326a..da776527 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -132,6 +132,9 @@ int encrypt_data_symmetric(uint8_t *secret_key, uint8_t *nonce, uint8_t *plain,
132 */ 132 */
133int decrypt_data_symmetric(uint8_t *secret_key, uint8_t *nonce, uint8_t *encrypted, uint32_t length, uint8_t *plain); 133int decrypt_data_symmetric(uint8_t *secret_key, uint8_t *nonce, uint8_t *encrypted, uint32_t length, uint8_t *plain);
134 134
135/* Increment the given nonce by 1. */
136void increment_nonce(uint8_t *nonce);
137
135/* Fill the given nonce with random bytes. */ 138/* Fill the given nonce with random bytes. */
136void random_nonce(uint8_t *nonce); 139void random_nonce(uint8_t *nonce);
137 140
diff --git a/toxcore/network.c b/toxcore/network.c
index d7ea2bf0..64f0af2f 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -36,10 +36,6 @@
36#include "network.h" 36#include "network.h"
37#include "util.h" 37#include "util.h"
38 38
39#ifndef IPV6_V6ONLY
40#define IPV6_V6ONLY 27
41#endif
42
43#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 39#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
44 40
45static const char *inet_ntop(sa_family_t family, void *addr, char *buf, size_t bufsize) 41static const char *inet_ntop(sa_family_t family, void *addr, char *buf, size_t bufsize)
diff --git a/toxcore/network.h b/toxcore/network.h
index b0d306e4..5e434c1a 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -44,6 +44,10 @@
44#include <windows.h> 44#include <windows.h>
45#include <ws2tcpip.h> 45#include <ws2tcpip.h>
46 46
47#ifndef IPV6_V6ONLY
48#define IPV6_V6ONLY 27
49#endif
50
47typedef unsigned int sock_t; 51typedef unsigned int sock_t;
48/* sa_family_t is the sockaddr_in / sockaddr_in6 family field */ 52/* sa_family_t is the sockaddr_in / sockaddr_in6 family field */
49typedef short sa_family_t; 53typedef short sa_family_t;