diff options
author | irungentoo <irungentoo@gmail.com> | 2014-08-13 06:41:35 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2014-08-13 06:41:35 -0400 |
commit | 70d338703dd95f66f8f3e11abe67379551e106eb (patch) | |
tree | e21ec1e36bdee46847a3d0e42f79fde49561caa9 | |
parent | 19b07f7e80097f1a65c1e358140cd441d6ba67b6 (diff) |
Added function to create onion packets to send via TCP.
-rw-r--r-- | toxcore/onion.c | 48 | ||||
-rw-r--r-- | toxcore/onion.h | 13 |
2 files changed, 61 insertions, 0 deletions
diff --git a/toxcore/onion.c b/toxcore/onion.c index 33bb54ca..938b861e 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c | |||
@@ -141,6 +141,54 @@ int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, const Onion | |||
141 | return 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + len; | 141 | return 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + len; |
142 | } | 142 | } |
143 | 143 | ||
144 | /* Create a onion packet to be sent over tcp. | ||
145 | * | ||
146 | * Use Onion_Path path to create packet for data of length to dest. | ||
147 | * Maximum length of data is ONION_MAX_DATA_SIZE. | ||
148 | * packet should be at least ONION_MAX_PACKET_SIZE big. | ||
149 | * | ||
150 | * return -1 on failure. | ||
151 | * return length of created packet on success. | ||
152 | */ | ||
153 | int create_onion_packet_tcp(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, IP_Port dest, | ||
154 | const uint8_t *data, uint32_t length) | ||
155 | { | ||
156 | if (crypto_box_NONCEBYTES + SIZE_IPPORT + SEND_BASE * 2 + length > max_packet_length || length == 0) | ||
157 | return -1; | ||
158 | |||
159 | to_net_family(&dest.ip); | ||
160 | uint8_t step1[SIZE_IPPORT + length]; | ||
161 | |||
162 | |||
163 | ipport_pack(step1, &dest); | ||
164 | memcpy(step1 + SIZE_IPPORT, data, length); | ||
165 | |||
166 | uint8_t nonce[crypto_box_NONCEBYTES]; | ||
167 | random_nonce(nonce); | ||
168 | |||
169 | uint8_t step2[SIZE_IPPORT + SEND_BASE + length]; | ||
170 | ipport_pack(step2, &path->ip_port3); | ||
171 | memcpy(step2 + SIZE_IPPORT, path->public_key3, crypto_box_PUBLICKEYBYTES); | ||
172 | |||
173 | int len = encrypt_data_symmetric(path->shared_key3, nonce, step1, sizeof(step1), | ||
174 | step2 + SIZE_IPPORT + crypto_box_PUBLICKEYBYTES); | ||
175 | |||
176 | if ((uint32_t)len != SIZE_IPPORT + length + crypto_box_MACBYTES) | ||
177 | return -1; | ||
178 | |||
179 | ipport_pack(packet + crypto_box_NONCEBYTES, &path->ip_port2); | ||
180 | memcpy(packet + crypto_box_NONCEBYTES + SIZE_IPPORT, path->public_key2, crypto_box_PUBLICKEYBYTES); | ||
181 | len = encrypt_data_symmetric(path->shared_key2, nonce, step2, sizeof(step2), | ||
182 | packet + crypto_box_NONCEBYTES + SIZE_IPPORT + crypto_box_PUBLICKEYBYTES); | ||
183 | |||
184 | if ((uint32_t)len != SIZE_IPPORT + SEND_BASE + length + crypto_box_MACBYTES) | ||
185 | return -1; | ||
186 | |||
187 | memcpy(packet, nonce, crypto_box_NONCEBYTES); | ||
188 | |||
189 | return crypto_box_NONCEBYTES + SIZE_IPPORT + crypto_box_PUBLICKEYBYTES + len; | ||
190 | } | ||
191 | |||
144 | /* Create and send a onion packet. | 192 | /* Create and send a onion packet. |
145 | * | 193 | * |
146 | * Use Onion_Path path to send data of length to dest. | 194 | * Use Onion_Path path to send data of length to dest. |
diff --git a/toxcore/onion.h b/toxcore/onion.h index 5dedd60d..e8e7042c 100644 --- a/toxcore/onion.h +++ b/toxcore/onion.h | |||
@@ -92,6 +92,19 @@ int create_onion_path(const DHT *dht, Onion_Path *new_path, const Node_format *n | |||
92 | int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, IP_Port dest, | 92 | int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, IP_Port dest, |
93 | const uint8_t *data, uint32_t length); | 93 | const uint8_t *data, uint32_t length); |
94 | 94 | ||
95 | |||
96 | /* Create a onion packet to be sent over tcp. | ||
97 | * | ||
98 | * Use Onion_Path path to create packet for data of length to dest. | ||
99 | * Maximum length of data is ONION_MAX_DATA_SIZE. | ||
100 | * packet should be at least ONION_MAX_PACKET_SIZE big. | ||
101 | * | ||
102 | * return -1 on failure. | ||
103 | * return length of created packet on success. | ||
104 | */ | ||
105 | int create_onion_packet_tcp(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, IP_Port dest, | ||
106 | const uint8_t *data, uint32_t length); | ||
107 | |||
95 | /* Create and send a onion packet. | 108 | /* Create and send a onion packet. |
96 | * | 109 | * |
97 | * Use Onion_Path path to send data of length to dest. | 110 | * Use Onion_Path path to send data of length to dest. |