diff options
author | irungentoo <irungentoo@gmail.com> | 2014-01-05 19:22:38 -0500 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2014-01-05 19:22:38 -0500 |
commit | 6cd1e7fb707e20681e066b0a7e452e15c227c280 (patch) | |
tree | d36892dd4affaedbd47e90f5066b2dc6cda630da /toxcore | |
parent | 7e2d21271a549347575a38e5c3859d7374d496ed (diff) |
Tests added and some fixes for the onion part.
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/network.c | 1 | ||||
-rw-r--r-- | toxcore/onion.c | 8 | ||||
-rw-r--r-- | toxcore/onion.h | 2 | ||||
-rw-r--r-- | toxcore/onion_announce.c | 55 | ||||
-rw-r--r-- | toxcore/onion_announce.h | 23 |
5 files changed, 68 insertions, 21 deletions
diff --git a/toxcore/network.c b/toxcore/network.c index 35f7c51d..b534db9c 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -226,6 +226,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t le | |||
226 | */ | 226 | */ |
227 | static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t *length) | 227 | static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t *length) |
228 | { | 228 | { |
229 | memset(ip_port, 0, sizeof(IP_Port)); | ||
229 | struct sockaddr_storage addr; | 230 | struct sockaddr_storage addr; |
230 | #ifdef WIN32 | 231 | #ifdef WIN32 |
231 | int addrlen = sizeof(addr); | 232 | int addrlen = sizeof(addr); |
diff --git a/toxcore/onion.c b/toxcore/onion.c index 17443fe0..7ec6bb61 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c | |||
@@ -44,7 +44,7 @@ | |||
44 | * return -1 on failure. | 44 | * return -1 on failure. |
45 | * return 0 on success. | 45 | * return 0 on success. |
46 | */ | 46 | */ |
47 | int send_onion_packet(Onion *onion, Node_format *nodes, uint8_t *data, uint32_t length) | 47 | int send_onion_packet(DHT *dht, Node_format *nodes, uint8_t *data, uint32_t length) |
48 | { | 48 | { |
49 | if (1 + length + SEND_1 > MAX_ONION_SIZE || length == 0) | 49 | if (1 + length + SEND_1 > MAX_ONION_SIZE || length == 0) |
50 | return -1; | 50 | return -1; |
@@ -82,15 +82,15 @@ int send_onion_packet(Onion *onion, Node_format *nodes, uint8_t *data, uint32_t | |||
82 | uint8_t packet[1 + length + SEND_1]; | 82 | uint8_t packet[1 + length + SEND_1]; |
83 | packet[0] = NET_PACKET_ONION_SEND_INITIAL; | 83 | packet[0] = NET_PACKET_ONION_SEND_INITIAL; |
84 | memcpy(packet + 1, nonce, crypto_box_NONCEBYTES); | 84 | memcpy(packet + 1, nonce, crypto_box_NONCEBYTES); |
85 | memcpy(packet + 1 + crypto_box_NONCEBYTES, onion->dht->self_public_key, crypto_box_PUBLICKEYBYTES); | 85 | memcpy(packet + 1 + crypto_box_NONCEBYTES, dht->self_public_key, crypto_box_PUBLICKEYBYTES); |
86 | 86 | ||
87 | len = encrypt_data(nodes[0].client_id, onion->dht->self_secret_key, nonce, | 87 | len = encrypt_data(nodes[0].client_id, dht->self_secret_key, nonce, |
88 | step3, sizeof(step3), packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES); | 88 | step3, sizeof(step3), packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES); |
89 | 89 | ||
90 | if ((uint32_t)len != sizeof(IP_Port) + SEND_BASE * 2 + length + crypto_box_MACBYTES) | 90 | if ((uint32_t)len != sizeof(IP_Port) + SEND_BASE * 2 + length + crypto_box_MACBYTES) |
91 | return -1; | 91 | return -1; |
92 | 92 | ||
93 | if ((uint32_t)sendpacket(onion->net, nodes[0].ip_port, packet, sizeof(packet)) != sizeof(packet)) | 93 | if ((uint32_t)sendpacket(dht->c->lossless_udp->net, nodes[0].ip_port, packet, sizeof(packet)) != sizeof(packet)) |
94 | return -1; | 94 | return -1; |
95 | 95 | ||
96 | return 0; | 96 | return 0; |
diff --git a/toxcore/onion.h b/toxcore/onion.h index bb4687fe..b46dbdfe 100644 --- a/toxcore/onion.h +++ b/toxcore/onion.h | |||
@@ -49,7 +49,7 @@ typedef struct { | |||
49 | * return -1 on failure. | 49 | * return -1 on failure. |
50 | * return 0 on success. | 50 | * return 0 on success. |
51 | */ | 51 | */ |
52 | int send_onion_packet(Onion *onion, Node_format *nodes, uint8_t *data, uint32_t length); | 52 | int send_onion_packet(DHT *dht, Node_format *nodes, uint8_t *data, uint32_t length); |
53 | 53 | ||
54 | /* Create and send a onion response sent initially to dest with. | 54 | /* Create and send a onion response sent initially to dest with. |
55 | * | 55 | * |
diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c index 891c308e..8c314b18 100644 --- a/toxcore/onion_announce.c +++ b/toxcore/onion_announce.c | |||
@@ -30,22 +30,57 @@ | |||
30 | #define PING_ID_SIZE crypto_hash_sha256_BYTES | 30 | #define PING_ID_SIZE crypto_hash_sha256_BYTES |
31 | #define PING_ID_TIMEOUT 10 | 31 | #define PING_ID_TIMEOUT 10 |
32 | 32 | ||
33 | #define ANNOUNCE_REQUEST_SIZE (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES + ONION_RETURN_3) | 33 | #define ANNOUNCE_REQUEST_SIZE (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES) |
34 | #define ANNOUNCE_REQUEST_SIZE_RECV (ANNOUNCE_REQUEST_SIZE + ONION_RETURN_3) | ||
34 | #define ANNOUNCE_RESPONSE_MIN_SIZE (1 + crypto_box_NONCEBYTES + PING_ID_SIZE + crypto_box_MACBYTES) | 35 | #define ANNOUNCE_RESPONSE_MIN_SIZE (1 + crypto_box_NONCEBYTES + PING_ID_SIZE + crypto_box_MACBYTES) |
35 | #define ANNOUNCE_RESPONSE_MAX_SIZE (ANNOUNCE_RESPONSE_MIN_SIZE + sizeof(Node_format)*MAX_SENT_NODES) | 36 | #define ANNOUNCE_RESPONSE_MAX_SIZE (ANNOUNCE_RESPONSE_MIN_SIZE + sizeof(Node_format)*MAX_SENT_NODES) |
36 | 37 | ||
37 | #define DATA_REQUEST_MIN_SIZE (1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES + ONION_RETURN_3) | 38 | #define DATA_REQUEST_MIN_SIZE (1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES + ONION_RETURN_3) |
38 | 39 | ||
40 | /* Create and send an onion announce request packet. | ||
41 | * | ||
42 | * nodes is a list of 4 nodes, the packet will route through nodes 0, 1, 2 and the data | ||
43 | * with length length will arrive at 3. | ||
44 | * | ||
45 | * public_key and secret_key is the kepair which will be used to encrypt the request. | ||
46 | * ping_id is the ping id that will be sent in the request. | ||
47 | * client_id is the client id of the node we are searching for. | ||
48 | * | ||
49 | * return -1 on failure. | ||
50 | * return 0 on success. | ||
51 | */ | ||
52 | int send_announce_request(DHT *dht, Node_format *nodes, uint8_t *public_key, uint8_t *secret_key, uint8_t *ping_id, | ||
53 | uint8_t *client_id) | ||
54 | { | ||
55 | uint8_t plain[PING_ID_SIZE + crypto_box_PUBLICKEYBYTES]; | ||
56 | memcpy(plain, ping_id, PING_ID_SIZE); | ||
57 | memcpy(plain + PING_ID_SIZE, client_id, crypto_box_PUBLICKEYBYTES); | ||
58 | |||
59 | uint8_t packet[ANNOUNCE_REQUEST_SIZE]; | ||
60 | packet[0] = NET_PACKET_ANNOUNCE_REQUEST; | ||
61 | new_nonce(packet + 1); | ||
62 | |||
63 | int len = encrypt_data(nodes[3].client_id, secret_key, packet + 1, plain, sizeof(plain), | ||
64 | packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES); | ||
65 | |||
66 | if ((uint32_t)len + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES != ANNOUNCE_REQUEST_SIZE) | ||
67 | return -1; | ||
68 | |||
69 | memcpy(packet + 1 + crypto_box_NONCEBYTES, public_key, crypto_box_PUBLICKEYBYTES); | ||
70 | |||
71 | return send_onion_packet(dht, nodes, packet, sizeof(packet)); | ||
72 | } | ||
73 | |||
39 | /* Generate a ping_id and put it in ping_id */ | 74 | /* Generate a ping_id and put it in ping_id */ |
40 | static void generate_ping_id(Onion_Announce *onion_a, uint64_t time, uint8_t *public_key, uint8_t *ret, | 75 | static void generate_ping_id(Onion_Announce *onion_a, uint64_t time, uint8_t *public_key, IP_Port ret_ip_port, |
41 | uint8_t *ping_id) | 76 | uint8_t *ping_id) |
42 | { | 77 | { |
43 | time /= PING_ID_TIMEOUT; | 78 | time /= PING_ID_TIMEOUT; |
44 | uint8_t data[crypto_secretbox_KEYBYTES + sizeof(time) + crypto_box_PUBLICKEYBYTES + ONION_RETURN_3]; | 79 | uint8_t data[crypto_secretbox_KEYBYTES + sizeof(time) + crypto_box_PUBLICKEYBYTES + sizeof(ret_ip_port)]; |
45 | memcpy(data, onion_a->secret_bytes, crypto_secretbox_KEYBYTES); | 80 | memcpy(data, onion_a->secret_bytes, crypto_secretbox_KEYBYTES); |
46 | memcpy(data + crypto_secretbox_KEYBYTES, &time, sizeof(time)); | 81 | memcpy(data + crypto_secretbox_KEYBYTES, &time, sizeof(time)); |
47 | memcpy(data + crypto_secretbox_KEYBYTES + sizeof(time), public_key, crypto_box_PUBLICKEYBYTES); | 82 | memcpy(data + crypto_secretbox_KEYBYTES + sizeof(time), public_key, crypto_box_PUBLICKEYBYTES); |
48 | memcpy(data + crypto_secretbox_KEYBYTES + sizeof(time) + crypto_box_PUBLICKEYBYTES, ret, ONION_RETURN_3); | 83 | memcpy(data + crypto_secretbox_KEYBYTES + sizeof(time) + crypto_box_PUBLICKEYBYTES, &ret_ip_port, sizeof(ret_ip_port)); |
49 | crypto_hash_sha256(ping_id, data, sizeof(data)); | 84 | crypto_hash_sha256(ping_id, data, sizeof(data)); |
50 | } | 85 | } |
51 | 86 | ||
@@ -138,7 +173,7 @@ static int handle_announce_request(void *object, IP_Port source, uint8_t *packet | |||
138 | { | 173 | { |
139 | Onion_Announce *onion_a = object; | 174 | Onion_Announce *onion_a = object; |
140 | 175 | ||
141 | if (length != ANNOUNCE_REQUEST_SIZE) | 176 | if (length != ANNOUNCE_REQUEST_SIZE_RECV) |
142 | return 1; | 177 | return 1; |
143 | 178 | ||
144 | uint8_t plain[PING_ID_SIZE + crypto_box_PUBLICKEYBYTES]; | 179 | uint8_t plain[PING_ID_SIZE + crypto_box_PUBLICKEYBYTES]; |
@@ -150,18 +185,16 @@ static int handle_announce_request(void *object, IP_Port source, uint8_t *packet | |||
150 | return 1; | 185 | return 1; |
151 | 186 | ||
152 | uint8_t ping_id1[PING_ID_SIZE]; | 187 | uint8_t ping_id1[PING_ID_SIZE]; |
153 | generate_ping_id(onion_a, unix_time(), packet + 1 + crypto_box_NONCEBYTES, | 188 | generate_ping_id(onion_a, unix_time(), packet + 1 + crypto_box_NONCEBYTES, source, ping_id1); |
154 | packet + (ANNOUNCE_REQUEST_SIZE - ONION_RETURN_3), ping_id1); | ||
155 | 189 | ||
156 | uint8_t ping_id2[PING_ID_SIZE]; | 190 | uint8_t ping_id2[PING_ID_SIZE]; |
157 | generate_ping_id(onion_a, unix_time() + PING_ID_TIMEOUT, packet + 1 + crypto_box_NONCEBYTES, | 191 | generate_ping_id(onion_a, unix_time() + PING_ID_TIMEOUT, packet + 1 + crypto_box_NONCEBYTES, source, ping_id2); |
158 | packet + (ANNOUNCE_REQUEST_SIZE - ONION_RETURN_3), ping_id2); | ||
159 | 192 | ||
160 | int stored = 0; | 193 | int stored = 0; |
161 | 194 | ||
162 | if (memcmp(ping_id1, plain, PING_ID_SIZE) == 0 || memcmp(ping_id2, plain, PING_ID_SIZE) == 0) { | 195 | if (memcmp(ping_id1, plain, PING_ID_SIZE) == 0 || memcmp(ping_id2, plain, PING_ID_SIZE) == 0) { |
163 | stored = add_to_entries(onion_a, source, packet + 1 + crypto_box_NONCEBYTES, | 196 | stored = add_to_entries(onion_a, source, packet + 1 + crypto_box_NONCEBYTES, |
164 | packet + (ANNOUNCE_REQUEST_SIZE - ONION_RETURN_3)); | 197 | packet + (ANNOUNCE_REQUEST_SIZE_RECV - ONION_RETURN_3)); |
165 | } else { | 198 | } else { |
166 | stored = (in_entries(onion_a, plain + PING_ID_SIZE) != -1); | 199 | stored = (in_entries(onion_a, plain + PING_ID_SIZE) != -1); |
167 | } | 200 | } |
@@ -193,7 +226,7 @@ static int handle_announce_request(void *object, IP_Port source, uint8_t *packet | |||
193 | memcpy(data + 1, nonce, crypto_box_NONCEBYTES); | 226 | memcpy(data + 1, nonce, crypto_box_NONCEBYTES); |
194 | 227 | ||
195 | if (send_onion_response(onion_a->net, source, data, 1 + crypto_box_NONCEBYTES + len, | 228 | if (send_onion_response(onion_a->net, source, data, 1 + crypto_box_NONCEBYTES + len, |
196 | packet + (ANNOUNCE_REQUEST_SIZE - ONION_RETURN_3)) == -1) | 229 | packet + (ANNOUNCE_REQUEST_SIZE_RECV - ONION_RETURN_3)) == -1) |
197 | return 1; | 230 | return 1; |
198 | 231 | ||
199 | return 0; | 232 | return 0; |
diff --git a/toxcore/onion_announce.h b/toxcore/onion_announce.h index c8d9b442..66a8702d 100644 --- a/toxcore/onion_announce.h +++ b/toxcore/onion_announce.h | |||
@@ -29,10 +29,10 @@ | |||
29 | #define ONION_ANNOUNCE_TIMEOUT 300 | 29 | #define ONION_ANNOUNCE_TIMEOUT 300 |
30 | 30 | ||
31 | typedef struct { | 31 | typedef struct { |
32 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 32 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
33 | IP_Port ret_ip_port; | 33 | IP_Port ret_ip_port; |
34 | uint8_t ret[ONION_RETURN_3]; | 34 | uint8_t ret[ONION_RETURN_3]; |
35 | uint64_t time; | 35 | uint64_t time; |
36 | } Onion_Announce_Entry; | 36 | } Onion_Announce_Entry; |
37 | 37 | ||
38 | typedef struct { | 38 | typedef struct { |
@@ -43,7 +43,20 @@ typedef struct { | |||
43 | uint8_t secret_bytes[crypto_secretbox_KEYBYTES]; | 43 | uint8_t secret_bytes[crypto_secretbox_KEYBYTES]; |
44 | } Onion_Announce; | 44 | } Onion_Announce; |
45 | 45 | ||
46 | 46 | /* Create and send an onion announce request packet. | |
47 | * | ||
48 | * nodes is a list of 4 nodes, the packet will route through nodes 0, 1, 2 and the data | ||
49 | * with length length will arrive at 3. | ||
50 | * | ||
51 | * public_key and secret_key is the kepair which will be used to encrypt the request. | ||
52 | * ping_id is the ping id that will be sent in the request. | ||
53 | * client_id is the client id of the node we are searching for. | ||
54 | * | ||
55 | * return -1 on failure. | ||
56 | * return 0 on success. | ||
57 | */ | ||
58 | int send_announce_request(DHT *dht, Node_format *nodes, uint8_t *public_key, uint8_t *secret_key, uint8_t *ping_id, | ||
59 | uint8_t *client_id); | ||
47 | 60 | ||
48 | Onion_Announce *new_onion_announce(DHT *dht); | 61 | Onion_Announce *new_onion_announce(DHT *dht); |
49 | 62 | ||