diff options
author | irungentoo <irungentoo@gmail.com> | 2014-06-18 19:44:49 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2014-06-18 19:44:49 -0400 |
commit | a82cbfff6a8d7f830857c01ceb84e762ec6e43b3 (patch) | |
tree | 3366b8219c035032fe07781f76e55914c095970c /toxcore/onion.c | |
parent | 881ccced56f486b41c3ad0ac0b8312b7ab43bc6b (diff) |
Preparation work to make onion packets go through TCP.
Diffstat (limited to 'toxcore/onion.c')
-rw-r--r-- | toxcore/onion.c | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/toxcore/onion.c b/toxcore/onion.c index 194178f3..ac654689 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c | |||
@@ -84,17 +84,19 @@ int create_onion_path(DHT *dht, Onion_Path *new_path, Node_format *nodes) | |||
84 | return 0; | 84 | return 0; |
85 | } | 85 | } |
86 | 86 | ||
87 | /* Create and send a onion packet. | 87 | /* Create a onion packet. |
88 | * | 88 | * |
89 | * Use Onion_Path path to send data of length to dest. | 89 | * Use Onion_Path path to create packet for data of length to dest. |
90 | * Maximum length of data is ONION_MAX_DATA_SIZE. | 90 | * Maximum length of data is ONION_MAX_DATA_SIZE. |
91 | * packet should be at least ONION_MAX_PACKET_SIZE big. | ||
91 | * | 92 | * |
92 | * return -1 on failure. | 93 | * return -1 on failure. |
93 | * return 0 on success. | 94 | * return length of created packet on success. |
94 | */ | 95 | */ |
95 | int send_onion_packet(Networking_Core *net, Onion_Path *path, IP_Port dest, uint8_t *data, uint32_t length) | 96 | int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, Onion_Path *path, IP_Port dest, uint8_t *data, |
97 | uint32_t length) | ||
96 | { | 98 | { |
97 | if (1 + length + SEND_1 > ONION_MAX_PACKET_SIZE || length == 0) | 99 | if (1 + length + SEND_1 > max_packet_length || length == 0) |
98 | return -1; | 100 | return -1; |
99 | 101 | ||
100 | to_net_family(&dest.ip); | 102 | to_net_family(&dest.ip); |
@@ -126,7 +128,6 @@ int send_onion_packet(Networking_Core *net, Onion_Path *path, IP_Port dest, uint | |||
126 | if ((uint32_t)len != SIZE_IPPORT + SEND_BASE + length + crypto_box_MACBYTES) | 128 | if ((uint32_t)len != SIZE_IPPORT + SEND_BASE + length + crypto_box_MACBYTES) |
127 | return -1; | 129 | return -1; |
128 | 130 | ||
129 | uint8_t packet[1 + length + SEND_1]; | ||
130 | packet[0] = NET_PACKET_ONION_SEND_INITIAL; | 131 | packet[0] = NET_PACKET_ONION_SEND_INITIAL; |
131 | memcpy(packet + 1, nonce, crypto_box_NONCEBYTES); | 132 | memcpy(packet + 1, nonce, crypto_box_NONCEBYTES); |
132 | memcpy(packet + 1 + crypto_box_NONCEBYTES, path->public_key1, crypto_box_PUBLICKEYBYTES); | 133 | memcpy(packet + 1 + crypto_box_NONCEBYTES, path->public_key1, crypto_box_PUBLICKEYBYTES); |
@@ -137,7 +138,26 @@ int send_onion_packet(Networking_Core *net, Onion_Path *path, IP_Port dest, uint | |||
137 | if ((uint32_t)len != SIZE_IPPORT + SEND_BASE * 2 + length + crypto_box_MACBYTES) | 138 | if ((uint32_t)len != SIZE_IPPORT + SEND_BASE * 2 + length + crypto_box_MACBYTES) |
138 | return -1; | 139 | return -1; |
139 | 140 | ||
140 | if ((uint32_t)sendpacket(net, path->ip_port1, packet, sizeof(packet)) != sizeof(packet)) | 141 | return 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + len; |
142 | } | ||
143 | |||
144 | /* Create and send a onion packet. | ||
145 | * | ||
146 | * Use Onion_Path path to send data of length to dest. | ||
147 | * Maximum length of data is ONION_MAX_DATA_SIZE. | ||
148 | * | ||
149 | * return -1 on failure. | ||
150 | * return 0 on success. | ||
151 | */ | ||
152 | int send_onion_packet(Networking_Core *net, Onion_Path *path, IP_Port dest, uint8_t *data, uint32_t length) | ||
153 | { | ||
154 | uint8_t packet[ONION_MAX_PACKET_SIZE]; | ||
155 | int len = create_onion_packet(packet, sizeof(packet), path, dest, data, length); | ||
156 | |||
157 | if (len == -1) | ||
158 | return -1; | ||
159 | |||
160 | if (sendpacket(net, path->ip_port1, packet, len) != len) | ||
141 | return -1; | 161 | return -1; |
142 | 162 | ||
143 | return 0; | 163 | return 0; |