summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-04-27 20:37:59 -0400
committerirungentoo <irungentoo@gmail.com>2014-04-27 20:37:59 -0400
commit50cea69f5705a7a01e31417bae29feb920bfdd59 (patch)
tree7a9f98b29b070720871fa8d3740bd9d9924e3dee
parent82873d62c128cb04b4c89fd042042cb7b923e230 (diff)
Added raw UDP cookie request packet handler.
-rw-r--r--toxcore/net_crypto.c32
1 files changed, 25 insertions, 7 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 2136e09f..3d36e8e0 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -46,9 +46,6 @@ static uint8_t crypt_connection_id_not_valid(Net_Crypto *c, int crypt_connection
46 */ 46 */
47static int create_cookie_request(Net_Crypto *c, uint8_t *packet, uint8_t *dht_public_key, uint8_t *real_public_key) 47static int create_cookie_request(Net_Crypto *c, uint8_t *packet, uint8_t *dht_public_key, uint8_t *real_public_key)
48{ 48{
49 if (!c->dht)
50 return -1;
51
52 uint8_t plain[COOKIE_REQUEST_PLAIN_LENGTH]; 49 uint8_t plain[COOKIE_REQUEST_PLAIN_LENGTH];
53 50
54 memcpy(plain, c->self_public_key, crypto_box_PUBLICKEYBYTES); 51 memcpy(plain, c->self_public_key, crypto_box_PUBLICKEYBYTES);
@@ -123,7 +120,7 @@ static int open_cookie(uint8_t *bytes, uint8_t *cookie, uint8_t *encryption_key)
123} 120}
124 121
125 122
126/* Create a cookie request packet and put it in packet. 123/* Create a cookie response packet and put it in packet.
127 * request_plain must be COOKIE_REQUEST_PLAIN_LENGTH bytes. 124 * request_plain must be COOKIE_REQUEST_PLAIN_LENGTH bytes.
128 * packet must be of size COOKIE_RESPONSE_LENGTH or bigger. 125 * packet must be of size COOKIE_RESPONSE_LENGTH or bigger.
129 * 126 *
@@ -157,9 +154,6 @@ static int create_cookie_response(Net_Crypto *c, uint8_t *packet, uint8_t *reque
157static int handle_cookie_request(Net_Crypto *c, uint8_t *request_plain, uint8_t *shared_key, uint8_t *packet, 154static int handle_cookie_request(Net_Crypto *c, uint8_t *request_plain, uint8_t *shared_key, uint8_t *packet,
158 uint16_t length) 155 uint16_t length)
159{ 156{
160 if (!c->dht)
161 return -1;
162
163 if (length != COOKIE_REQUEST_LENGTH) 157 if (length != COOKIE_REQUEST_LENGTH)
164 return -1; 158 return -1;
165 159
@@ -174,6 +168,28 @@ static int handle_cookie_request(Net_Crypto *c, uint8_t *request_plain, uint8_t
174 return 0; 168 return 0;
175} 169}
176 170
171/* Handle the cookie request packet (for raw UDP)
172 */
173static int udp_handle_cookie_request(void *object, IP_Port source, uint8_t *packet, uint32_t length)
174{
175 Net_Crypto *c = c;
176 uint8_t request_plain[COOKIE_REQUEST_PLAIN_LENGTH];
177 uint8_t shared_key[crypto_box_BEFORENMBYTES];
178
179 if (handle_cookie_request(c, request_plain, shared_key, packet, length) != 0)
180 return 1;
181
182 uint8_t data[COOKIE_RESPONSE_LENGTH];
183
184 if (create_cookie_response(c, data, request_plain, shared_key) != sizeof(data))
185 return 1;
186
187 if ((uint32_t)sendpacket(c->dht->net, source, data, sizeof(data)) != sizeof(data))
188 return 1;
189
190 return 0;
191}
192
177/* return 0 if there is no received data in the buffer. 193/* return 0 if there is no received data in the buffer.
178 * return -1 if the packet was discarded. 194 * return -1 if the packet was discarded.
179 * return length of received data if successful. 195 * return length of received data if successful.
@@ -689,6 +705,8 @@ Net_Crypto *new_net_crypto(DHT *dht)
689 705
690 new_keys(temp); 706 new_keys(temp);
691 new_symmetric_key(temp->secret_symmetric_key); 707 new_symmetric_key(temp->secret_symmetric_key);
708
709 networking_registerhandler(dht->net, NET_PACKET_COOKIE_REQUEST, &udp_handle_cookie_request, temp);
692 return temp; 710 return temp;
693} 711}
694 712