diff options
author | irungentoo <irungentoo@gmail.com> | 2013-12-30 12:20:30 -0500 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-12-30 12:20:30 -0500 |
commit | 9c9e3b0e453f9a399876e72ae55c4e14439d91d0 (patch) | |
tree | 28e7b54b8c0f43417d8a47575c7f0d6c96b8290f /toxcore | |
parent | 346dc5265e5269fc4a01cd9cbfc718ef1e8c0a69 (diff) |
Added a function to create and send a onion packet.
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/onion.c | 60 | ||||
-rw-r--r-- | toxcore/onion.h | 9 |
2 files changed, 69 insertions, 0 deletions
diff --git a/toxcore/onion.c b/toxcore/onion.c index c4497078..2786bcce 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c | |||
@@ -33,6 +33,66 @@ | |||
33 | #define SEND_2 (crypto_box_NONCEBYTES + SEND_BASE*2 + RETURN_1) | 33 | #define SEND_2 (crypto_box_NONCEBYTES + SEND_BASE*2 + RETURN_1) |
34 | #define SEND_1 (crypto_box_NONCEBYTES + SEND_BASE*3) | 34 | #define SEND_1 (crypto_box_NONCEBYTES + SEND_BASE*3) |
35 | 35 | ||
36 | /* Create and send a onion packet. | ||
37 | * | ||
38 | * nodes is a list of 4 nodes, the packet will route through nodes 0, 1, 2 and the data | ||
39 | * with length length will arrive at 3. | ||
40 | * | ||
41 | * return -1 on failure. | ||
42 | * return 0 on success. | ||
43 | */ | ||
44 | int send_onion_packet(Onion *onion, Node_format *nodes, uint8_t *data, uint32_t length) | ||
45 | { | ||
46 | if (1 + length + SEND_1 > MAX_ONION_SIZE || length == 0) | ||
47 | return -1; | ||
48 | |||
49 | uint8_t step1[sizeof(IP_Port) + length]; | ||
50 | memcpy(step1, &nodes[3].ip_port, sizeof(IP_Port)); | ||
51 | memcpy(step1 + sizeof(IP_Port), data, length); | ||
52 | |||
53 | uint8_t nonce[crypto_box_NONCEBYTES]; | ||
54 | new_nonce(nonce); | ||
55 | uint8_t random_public_key[crypto_box_PUBLICKEYBYTES]; | ||
56 | uint8_t random_secret_key[crypto_box_SECRETKEYBYTES]; | ||
57 | crypto_box_keypair(random_public_key, random_secret_key); | ||
58 | |||
59 | uint8_t step2[sizeof(IP_Port) + SEND_BASE + length]; | ||
60 | memcpy(step2, &nodes[2].ip_port, sizeof(IP_Port)); | ||
61 | memcpy(step2 + sizeof(IP_Port), random_public_key, crypto_box_PUBLICKEYBYTES); | ||
62 | |||
63 | int len = encrypt_data(nodes[2].client_id, random_secret_key, nonce, | ||
64 | step1, sizeof(step1), step2 + sizeof(IP_Port) + crypto_box_PUBLICKEYBYTES); | ||
65 | |||
66 | if ((uint32_t)len != sizeof(IP_Port) + length + crypto_box_MACBYTES) | ||
67 | return -1; | ||
68 | |||
69 | crypto_box_keypair(random_public_key, random_secret_key); | ||
70 | uint8_t step3[sizeof(IP_Port) + SEND_BASE * 2 + length]; | ||
71 | memcpy(step3, &nodes[1].ip_port, sizeof(IP_Port)); | ||
72 | memcpy(step3 + sizeof(IP_Port), random_public_key, crypto_box_PUBLICKEYBYTES); | ||
73 | len = encrypt_data(nodes[1].client_id, random_secret_key, nonce, | ||
74 | step2, sizeof(step2), step3 + sizeof(IP_Port) + crypto_box_PUBLICKEYBYTES); | ||
75 | |||
76 | if ((uint32_t)len != sizeof(IP_Port) + SEND_BASE + length + crypto_box_MACBYTES) | ||
77 | return -1; | ||
78 | |||
79 | uint8_t packet[1 + length + SEND_1]; | ||
80 | packet[0] = NET_PACKET_ONION_SEND_INITIAL; | ||
81 | memcpy(packet + 1, nonce, crypto_box_NONCEBYTES); | ||
82 | memcpy(packet + 1 + crypto_box_NONCEBYTES, onion->dht->self_public_key, crypto_box_PUBLICKEYBYTES); | ||
83 | |||
84 | len = encrypt_data(nodes[0].client_id, onion->dht->self_secret_key, nonce, | ||
85 | step3, sizeof(step3), packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES); | ||
86 | |||
87 | if ((uint32_t)len != sizeof(IP_Port) + SEND_BASE * 2 + length + crypto_box_MACBYTES) | ||
88 | return -1; | ||
89 | |||
90 | if ((uint32_t)sendpacket(onion->net, nodes[0].ip_port, packet, sizeof(packet)) != sizeof(packet)) | ||
91 | return -1; | ||
92 | |||
93 | return 0; | ||
94 | } | ||
95 | |||
36 | static int handle_send_initial(void *object, IP_Port source, uint8_t *packet, uint32_t length) | 96 | static int handle_send_initial(void *object, IP_Port source, uint8_t *packet, uint32_t length) |
37 | { | 97 | { |
38 | Onion *onion = object; | 98 | Onion *onion = object; |
diff --git a/toxcore/onion.h b/toxcore/onion.h index 97ac3021..637692eb 100644 --- a/toxcore/onion.h +++ b/toxcore/onion.h | |||
@@ -28,6 +28,15 @@ typedef struct { | |||
28 | uint8_t secret_symmetric_key[crypto_secretbox_KEYBYTES]; | 28 | uint8_t secret_symmetric_key[crypto_secretbox_KEYBYTES]; |
29 | } Onion; | 29 | } Onion; |
30 | 30 | ||
31 | /* Create and send a onion packet. | ||
32 | * | ||
33 | * nodes is a list of 4 nodes, the packet will route through nodes 0, 1, 2 and the data | ||
34 | * with length length will arrive at 3. | ||
35 | * | ||
36 | * return -1 on failure. | ||
37 | * return 0 on success. | ||
38 | */ | ||
39 | int send_onion_packet(Onion *onion, Node_format *nodes, uint8_t *data, uint32_t length); | ||
31 | 40 | ||
32 | Onion *new_onion(DHT *dht); | 41 | Onion *new_onion(DHT *dht); |
33 | 42 | ||