summaryrefslogtreecommitdiff
path: root/toxcore/onion_announce.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-08-11 18:20:13 -0400
committerirungentoo <irungentoo@gmail.com>2014-08-11 18:20:13 -0400
commit5a22fbf4bfe01990be0759fb29d8090cda1f8956 (patch)
tree9db0588fda8c631b2d03cbf66487c5fda9335f91 /toxcore/onion_announce.c
parentf83fcbb13c0ea9b23ded77ede487c9cd9171995f (diff)
Some code refactoring to make passing onion packets through TCP easier.
Diffstat (limited to 'toxcore/onion_announce.c')
-rw-r--r--toxcore/onion_announce.c76
1 files changed, 42 insertions, 34 deletions
diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c
index dff05135..f5c38eea 100644
--- a/toxcore/onion_announce.c
+++ b/toxcore/onion_announce.c
@@ -29,16 +29,14 @@
29 29
30#define PING_ID_TIMEOUT 20 30#define PING_ID_TIMEOUT 20
31 31
32#define ANNOUNCE_REQUEST_SIZE (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_MACBYTES) 32#define ANNOUNCE_REQUEST_SIZE_RECV (ONION_ANNOUNCE_REQUEST_SIZE + ONION_RETURN_3)
33#define ANNOUNCE_REQUEST_SIZE_RECV (ANNOUNCE_REQUEST_SIZE + ONION_RETURN_3)
34 33
35#define DATA_REQUEST_MIN_SIZE ONION_DATA_REQUEST_MIN_SIZE 34#define DATA_REQUEST_MIN_SIZE ONION_DATA_REQUEST_MIN_SIZE
36#define DATA_REQUEST_MIN_SIZE_RECV (DATA_REQUEST_MIN_SIZE + ONION_RETURN_3) 35#define DATA_REQUEST_MIN_SIZE_RECV (DATA_REQUEST_MIN_SIZE + ONION_RETURN_3)
37 36
38/* Create an onion announce request packet in packet of max_packet_length (recommended size ONION_MAX_PACKET_SIZE). 37/* Create an onion announce request packet in packet of max_packet_length (recommended size ONION_ANNOUNCE_REQUEST_SIZE).
39 *
40 * path is the path the request will take before it is sent to dest.
41 * 38 *
39 * dest_client_id is the public key of the node the packet will be sent to.
42 * public_key and secret_key is the kepair which will be used to encrypt the request. 40 * public_key and secret_key is the kepair which will be used to encrypt the request.
43 * ping_id is the ping id that will be sent in the request. 41 * ping_id is the ping id that will be sent in the request.
44 * client_id is the client id of the node we are searching for. 42 * client_id is the client id of the node we are searching for.
@@ -49,10 +47,13 @@
49 * return -1 on failure. 47 * return -1 on failure.
50 * return packet length on success. 48 * return packet length on success.
51 */ 49 */
52int create_announce_request(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, Node_format dest, 50int create_announce_request(uint8_t *packet, uint16_t max_packet_length, const uint8_t *dest_client_id,
53 const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *ping_id, const uint8_t *client_id, 51 const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *ping_id, const uint8_t *client_id,
54 const uint8_t *data_public_key, uint64_t sendback_data) 52 const uint8_t *data_public_key, uint64_t sendback_data)
55{ 53{
54 if (max_packet_length < ONION_ANNOUNCE_REQUEST_SIZE)
55 return -1;
56
56 uint8_t plain[ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES + 57 uint8_t plain[ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES +
57 ONION_ANNOUNCE_SENDBACK_DATA_LENGTH]; 58 ONION_ANNOUNCE_SENDBACK_DATA_LENGTH];
58 memcpy(plain, ping_id, ONION_PING_ID_SIZE); 59 memcpy(plain, ping_id, ONION_PING_ID_SIZE);
@@ -60,27 +61,23 @@ int create_announce_request(uint8_t *packet, uint16_t max_packet_length, const O
60 memcpy(plain + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES, data_public_key, crypto_box_PUBLICKEYBYTES); 61 memcpy(plain + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES, data_public_key, crypto_box_PUBLICKEYBYTES);
61 memcpy(plain + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES, &sendback_data, 62 memcpy(plain + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES, &sendback_data,
62 sizeof(sendback_data)); 63 sizeof(sendback_data));
63 uint8_t temp[ANNOUNCE_REQUEST_SIZE];
64 temp[0] = NET_PACKET_ANNOUNCE_REQUEST;
65 random_nonce(temp + 1);
66 64
67 int len = encrypt_data(dest.client_id, secret_key, temp + 1, plain, sizeof(plain), 65 packet[0] = NET_PACKET_ANNOUNCE_REQUEST;
68 temp + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES); 66 random_nonce(packet + 1);
67
68 int len = encrypt_data(dest_client_id, secret_key, packet + 1, plain, sizeof(plain),
69 packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES);
69 70
70 if ((uint32_t)len + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES != ANNOUNCE_REQUEST_SIZE) 71 if ((uint32_t)len + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES != ONION_ANNOUNCE_REQUEST_SIZE)
71 return -1; 72 return -1;
72 73
73 memcpy(temp + 1 + crypto_box_NONCEBYTES, public_key, crypto_box_PUBLICKEYBYTES); 74 memcpy(packet + 1 + crypto_box_NONCEBYTES, public_key, crypto_box_PUBLICKEYBYTES);
74 75
75 return create_onion_packet(packet, max_packet_length, path, dest.ip_port, temp, sizeof(temp)); 76 return ONION_ANNOUNCE_REQUEST_SIZE;
76} 77}
77 78
78/* Create an onion data request packet in packet of max_packet_length (recommended size ONION_MAX_PACKET_SIZE). 79/* Create an onion data request packet in packet of max_packet_length (recommended size ONION_MAX_PACKET_SIZE).
79 * 80 *
80 * path is the path the request will take before it is sent to dest.
81 * (if dest knows the person with the public_key they should
82 * send the packet to that person in the form of a response)
83 *
84 * public_key is the real public key of the node which we want to send the data of length length to. 81 * public_key is the real public key of the node which we want to send the data of length length to.
85 * encrypt_public_key is the public key used to encrypt the data packet. 82 * encrypt_public_key is the public key used to encrypt the data packet.
86 * 83 *
@@ -89,31 +86,33 @@ int create_announce_request(uint8_t *packet, uint16_t max_packet_length, const O
89 * return -1 on failure. 86 * return -1 on failure.
90 * return 0 on success. 87 * return 0 on success.
91 */ 88 */
92int create_data_request(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, IP_Port dest, 89int create_data_request(uint8_t *packet, uint16_t max_packet_length, const uint8_t *public_key,
93 const uint8_t *public_key, const uint8_t *encrypt_public_key, const uint8_t *nonce, const uint8_t *data, 90 const uint8_t *encrypt_public_key, const uint8_t *nonce, const uint8_t *data, uint16_t length)
94 uint16_t length)
95{ 91{
92 if (DATA_REQUEST_MIN_SIZE + length > max_packet_length)
93 return -1;
94
96 if ((unsigned int)DATA_REQUEST_MIN_SIZE + length > ONION_MAX_DATA_SIZE) 95 if ((unsigned int)DATA_REQUEST_MIN_SIZE + length > ONION_MAX_DATA_SIZE)
97 return -1; 96 return -1;
98 97
99 uint8_t temp[DATA_REQUEST_MIN_SIZE + length]; 98 packet[0] = NET_PACKET_ONION_DATA_REQUEST;
100 temp[0] = NET_PACKET_ONION_DATA_REQUEST; 99 memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES);
101 memcpy(temp + 1, public_key, crypto_box_PUBLICKEYBYTES); 100 memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES);
102 memcpy(temp + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES);
103 101
104 uint8_t random_public_key[crypto_box_PUBLICKEYBYTES]; 102 uint8_t random_public_key[crypto_box_PUBLICKEYBYTES];
105 uint8_t random_secret_key[crypto_box_SECRETKEYBYTES]; 103 uint8_t random_secret_key[crypto_box_SECRETKEYBYTES];
106 crypto_box_keypair(random_public_key, random_secret_key); 104 crypto_box_keypair(random_public_key, random_secret_key);
107 105
108 memcpy(temp + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, random_public_key, crypto_box_PUBLICKEYBYTES); 106 memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, random_public_key, crypto_box_PUBLICKEYBYTES);
109 107
110 int len = encrypt_data(encrypt_public_key, random_secret_key, temp + 1 + crypto_box_PUBLICKEYBYTES, 108 int len = encrypt_data(encrypt_public_key, random_secret_key, packet + 1 + crypto_box_PUBLICKEYBYTES, data, length,
111 data, length, temp + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES); 109 packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES);
112 110
113 if (1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + (uint32_t)len != sizeof(temp)) 111 if (1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES +
112 (uint32_t)len != DATA_REQUEST_MIN_SIZE + length)
114 return -1; 113 return -1;
115 114
116 return create_onion_packet(packet, max_packet_length, path, dest, temp, sizeof(temp)); 115 return DATA_REQUEST_MIN_SIZE + length;
117} 116}
118 117
119/* Create and send an onion announce request packet. 118/* Create and send an onion announce request packet.
@@ -134,10 +133,16 @@ int send_announce_request(Networking_Core *net, const Onion_Path *path, Node_for
134 const uint8_t *secret_key, const uint8_t *ping_id, const uint8_t *client_id, const uint8_t *data_public_key, 133 const uint8_t *secret_key, const uint8_t *ping_id, const uint8_t *client_id, const uint8_t *data_public_key,
135 uint64_t sendback_data) 134 uint64_t sendback_data)
136{ 135{
137 uint8_t packet[ONION_MAX_PACKET_SIZE]; 136 uint8_t request[ONION_ANNOUNCE_REQUEST_SIZE];
138 int len = create_announce_request(packet, sizeof(packet), path, dest, public_key, secret_key, ping_id, client_id, 137 int len = create_announce_request(request, sizeof(request), dest.client_id, public_key, secret_key, ping_id, client_id,
139 data_public_key, sendback_data); 138 data_public_key, sendback_data);
140 139
140 if (len != sizeof(request))
141 return -1;
142
143 uint8_t packet[ONION_MAX_PACKET_SIZE];
144 len = create_onion_packet(packet, sizeof(packet), path, dest.ip_port, request, sizeof(request));
145
141 if (len == -1) 146 if (len == -1)
142 return -1; 147 return -1;
143 148
@@ -164,12 +169,15 @@ int send_announce_request(Networking_Core *net, const Onion_Path *path, Node_for
164int send_data_request(Networking_Core *net, const Onion_Path *path, IP_Port dest, const uint8_t *public_key, 169int send_data_request(Networking_Core *net, const Onion_Path *path, IP_Port dest, const uint8_t *public_key,
165 const uint8_t *encrypt_public_key, const uint8_t *nonce, const uint8_t *data, uint16_t length) 170 const uint8_t *encrypt_public_key, const uint8_t *nonce, const uint8_t *data, uint16_t length)
166{ 171{
167 uint8_t packet[ONION_MAX_PACKET_SIZE]; 172 uint8_t request[ONION_MAX_DATA_SIZE];
168 int len = create_data_request(packet, sizeof(packet), path, dest, public_key, encrypt_public_key, nonce, data, length); 173 int len = create_data_request(request, sizeof(request), public_key, encrypt_public_key, nonce, data, length);
169 174
170 if (len == -1) 175 if (len == -1)
171 return -1; 176 return -1;
172 177
178 uint8_t packet[ONION_MAX_PACKET_SIZE];
179 len = create_onion_packet(packet, sizeof(packet), path, dest, request, len);
180
173 if (sendpacket(net, path->ip_port1, packet, len) != len) 181 if (sendpacket(net, path->ip_port1, packet, len) != len)
174 return -1; 182 return -1;
175 183