diff options
author | irungentoo <irungentoo@gmail.com> | 2015-01-01 21:30:42 -0500 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2015-01-01 21:30:42 -0500 |
commit | 93a998d15a983614de680dfe1f690f59f777698f (patch) | |
tree | 2b3f719aacc73266ec133a816d3f7668888f1c2d /toxcore/onion.c | |
parent | 3328127f186377018c5ccadc8b33a66661f423dd (diff) |
Code cleanups.
Moved some functions to onion.c.
Fixed possible portability issues.
Diffstat (limited to 'toxcore/onion.c')
-rw-r--r-- | toxcore/onion.c | 72 |
1 files changed, 52 insertions, 20 deletions
diff --git a/toxcore/onion.c b/toxcore/onion.c index b444e02a..05dbaae6 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c | |||
@@ -45,6 +45,58 @@ static void change_symmetric_key(Onion *onion) | |||
45 | } | 45 | } |
46 | } | 46 | } |
47 | 47 | ||
48 | /* packing and unpacking functions */ | ||
49 | static void ip_pack(uint8_t *data, IP source) | ||
50 | { | ||
51 | to_net_family(&source); | ||
52 | |||
53 | data[0] = source.family; | ||
54 | |||
55 | if (source.family == TOX_AF_INET || source.family == TOX_TCP_INET) { | ||
56 | memset(data + 1, 0, SIZE_IP6); | ||
57 | memcpy(data + 1, source.ip4.uint8, SIZE_IP4); | ||
58 | } else { | ||
59 | memcpy(data + 1, source.ip6.uint8, SIZE_IP6); | ||
60 | } | ||
61 | } | ||
62 | |||
63 | /* return 0 on success, -1 on failure. */ | ||
64 | static int ip_unpack(IP *target, const uint8_t *data, unsigned int data_size) | ||
65 | { | ||
66 | if (data_size < (1 + SIZE_IP6)) | ||
67 | return -1; | ||
68 | |||
69 | target->family = data[0]; | ||
70 | |||
71 | if (target->family == TOX_AF_INET || target->family == TOX_TCP_INET) { | ||
72 | memcpy(target->ip4.uint8, data + 1, SIZE_IP4); | ||
73 | } else { | ||
74 | memcpy(target->ip6.uint8, data + 1, SIZE_IP6); | ||
75 | } | ||
76 | |||
77 | return to_host_family(target); | ||
78 | } | ||
79 | |||
80 | static void ipport_pack(uint8_t *data, const IP_Port *source) | ||
81 | { | ||
82 | ip_pack(data, source->ip); | ||
83 | memcpy(data + SIZE_IP, &source->port, SIZE_PORT); | ||
84 | } | ||
85 | |||
86 | /* return 0 on success, -1 on failure. */ | ||
87 | static int ipport_unpack(IP_Port *target, const uint8_t *data, unsigned int data_size) | ||
88 | { | ||
89 | if (data_size < (SIZE_IP + SIZE_PORT)) | ||
90 | return -1; | ||
91 | |||
92 | if (ip_unpack(&target->ip, data, data_size) == -1) | ||
93 | return -1; | ||
94 | |||
95 | memcpy(&target->port, data + SIZE_IP, SIZE_PORT); | ||
96 | return 0; | ||
97 | } | ||
98 | |||
99 | |||
48 | /* Create a new onion path. | 100 | /* Create a new onion path. |
49 | * | 101 | * |
50 | * Create a new onion path out of nodes (nodes is a list of 3 nodes) | 102 | * Create a new onion path out of nodes (nodes is a list of 3 nodes) |
@@ -81,10 +133,6 @@ int create_onion_path(const DHT *dht, Onion_Path *new_path, const Node_format *n | |||
81 | memcpy(new_path->node_public_key2, nodes[1].client_id, crypto_box_PUBLICKEYBYTES); | 133 | memcpy(new_path->node_public_key2, nodes[1].client_id, crypto_box_PUBLICKEYBYTES); |
82 | memcpy(new_path->node_public_key3, nodes[2].client_id, crypto_box_PUBLICKEYBYTES); | 134 | memcpy(new_path->node_public_key3, nodes[2].client_id, crypto_box_PUBLICKEYBYTES); |
83 | 135 | ||
84 | /* to_net_family(&new_path->ip_port1.ip); */ | ||
85 | to_net_family(&new_path->ip_port2.ip); | ||
86 | to_net_family(&new_path->ip_port3.ip); | ||
87 | |||
88 | return 0; | 136 | return 0; |
89 | } | 137 | } |
90 | 138 | ||
@@ -102,9 +150,6 @@ int onion_path_to_nodes(Node_format *nodes, unsigned int num_nodes, const Onion_ | |||
102 | nodes[1].ip_port = path->ip_port2; | 150 | nodes[1].ip_port = path->ip_port2; |
103 | nodes[2].ip_port = path->ip_port3; | 151 | nodes[2].ip_port = path->ip_port3; |
104 | 152 | ||
105 | to_host_family(&nodes[1].ip_port.ip); | ||
106 | to_host_family(&nodes[2].ip_port.ip); | ||
107 | |||
108 | memcpy(nodes[0].client_id, path->node_public_key1, crypto_box_PUBLICKEYBYTES); | 153 | memcpy(nodes[0].client_id, path->node_public_key1, crypto_box_PUBLICKEYBYTES); |
109 | memcpy(nodes[1].client_id, path->node_public_key2, crypto_box_PUBLICKEYBYTES); | 154 | memcpy(nodes[1].client_id, path->node_public_key2, crypto_box_PUBLICKEYBYTES); |
110 | memcpy(nodes[2].client_id, path->node_public_key3, crypto_box_PUBLICKEYBYTES); | 155 | memcpy(nodes[2].client_id, path->node_public_key3, crypto_box_PUBLICKEYBYTES); |
@@ -126,10 +171,8 @@ int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, const Onion | |||
126 | if (1 + length + SEND_1 > max_packet_length || length == 0) | 171 | if (1 + length + SEND_1 > max_packet_length || length == 0) |
127 | return -1; | 172 | return -1; |
128 | 173 | ||
129 | to_net_family(&dest.ip); | ||
130 | uint8_t step1[SIZE_IPPORT + length]; | 174 | uint8_t step1[SIZE_IPPORT + length]; |
131 | 175 | ||
132 | |||
133 | ipport_pack(step1, &dest); | 176 | ipport_pack(step1, &dest); |
134 | memcpy(step1 + SIZE_IPPORT, data, length); | 177 | memcpy(step1 + SIZE_IPPORT, data, length); |
135 | 178 | ||
@@ -183,10 +226,8 @@ int create_onion_packet_tcp(uint8_t *packet, uint16_t max_packet_length, const O | |||
183 | if (crypto_box_NONCEBYTES + SIZE_IPPORT + SEND_BASE * 2 + length > max_packet_length || length == 0) | 226 | if (crypto_box_NONCEBYTES + SIZE_IPPORT + SEND_BASE * 2 + length > max_packet_length || length == 0) |
184 | return -1; | 227 | return -1; |
185 | 228 | ||
186 | to_net_family(&dest.ip); | ||
187 | uint8_t step1[SIZE_IPPORT + length]; | 229 | uint8_t step1[SIZE_IPPORT + length]; |
188 | 230 | ||
189 | |||
190 | ipport_pack(step1, &dest); | 231 | ipport_pack(step1, &dest); |
191 | memcpy(step1 + SIZE_IPPORT, data, length); | 232 | memcpy(step1 + SIZE_IPPORT, data, length); |
192 | 233 | ||
@@ -297,9 +338,6 @@ int onion_send_1(const Onion *onion, const uint8_t *plain, uint16_t len, IP_Port | |||
297 | if (ipport_unpack(&send_to, plain, len) == -1) | 338 | if (ipport_unpack(&send_to, plain, len) == -1) |
298 | return 1; | 339 | return 1; |
299 | 340 | ||
300 | if (to_host_family(&send_to.ip) == -1) | ||
301 | return 1; | ||
302 | |||
303 | uint8_t ip_port[SIZE_IPPORT]; | 341 | uint8_t ip_port[SIZE_IPPORT]; |
304 | ipport_pack(ip_port, &source); | 342 | ipport_pack(ip_port, &source); |
305 | 343 | ||
@@ -350,9 +388,6 @@ static int handle_send_1(void *object, IP_Port source, const uint8_t *packet, ui | |||
350 | if (ipport_unpack(&send_to, plain, len) == -1) | 388 | if (ipport_unpack(&send_to, plain, len) == -1) |
351 | return 1; | 389 | return 1; |
352 | 390 | ||
353 | if (to_host_family(&send_to.ip) == -1) | ||
354 | return 1; | ||
355 | |||
356 | uint8_t data[ONION_MAX_PACKET_SIZE]; | 391 | uint8_t data[ONION_MAX_PACKET_SIZE]; |
357 | data[0] = NET_PACKET_ONION_SEND_2; | 392 | data[0] = NET_PACKET_ONION_SEND_2; |
358 | memcpy(data + 1, packet + 1, crypto_box_NONCEBYTES); | 393 | memcpy(data + 1, packet + 1, crypto_box_NONCEBYTES); |
@@ -403,9 +438,6 @@ static int handle_send_2(void *object, IP_Port source, const uint8_t *packet, ui | |||
403 | if (ipport_unpack(&send_to, plain, len) == -1) | 438 | if (ipport_unpack(&send_to, plain, len) == -1) |
404 | return 1; | 439 | return 1; |
405 | 440 | ||
406 | if (to_host_family(&send_to.ip) == -1) | ||
407 | return 1; | ||
408 | |||
409 | uint8_t data[ONION_MAX_PACKET_SIZE]; | 441 | uint8_t data[ONION_MAX_PACKET_SIZE]; |
410 | memcpy(data, plain + SIZE_IPPORT, len - SIZE_IPPORT); | 442 | memcpy(data, plain + SIZE_IPPORT, len - SIZE_IPPORT); |
411 | uint16_t data_len = (len - SIZE_IPPORT); | 443 | uint16_t data_len = (len - SIZE_IPPORT); |