Code cleanups.

Moved some functions to onion.c.

Fixed possible portability issues.
This commit is contained in:
irungentoo 2015-01-01 21:30:42 -05:00
parent 3328127f18
commit 93a998d15a
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
4 changed files with 53 additions and 67 deletions

View File

@ -43,6 +43,7 @@ int crypto_cmp(const uint8_t *mem1, const uint8_t *mem2, size_t length)
unsigned int check = 0; unsigned int check = 0;
size_t i; size_t i;
for (i = 0; i < length; ++i) { for (i = 0; i < length; ++i) {
check |= mem1[i] ^ mem2[i]; check |= mem1[i] ^ mem2[i];
} }

View File

@ -756,41 +756,6 @@ void ipport_copy(IP_Port *target, const IP_Port *source)
memcpy(target, source, sizeof(IP_Port)); memcpy(target, source, sizeof(IP_Port));
}; };
/* packing and unpacking functions */
void ip_pack(uint8_t *data, const IP *source)
{
data[0] = source->family;
memcpy(data + 1, &source->ip6, SIZE_IP6);
}
/* return 0 on success, -1 on failure. */
int ip_unpack(IP *target, const uint8_t *data, unsigned int data_size)
{
if (data_size < (1 + SIZE_IP6))
return -1;
target->family = data[0];
memcpy(&target->ip6, data + 1, SIZE_IP6);
return 0;
}
void ipport_pack(uint8_t *data, const IP_Port *source)
{
ip_pack(data, &source->ip);
memcpy(data + SIZE_IP, &source->port, SIZE_PORT);
}
/* return 0 on success, -1 on failure. */
int ipport_unpack(IP_Port *target, const uint8_t *data, unsigned int data_size)
{
if (data_size < (SIZE_IP + SIZE_PORT))
return -1;
ip_unpack(&target->ip, data, data_size);
memcpy(&target->port, data + SIZE_IP, SIZE_PORT);
return 0;
}
/* ip_ntoa /* ip_ntoa
* converts ip into a string * converts ip into a string
* uses a static buffer, so mustn't used multiple times in the same output * uses a static buffer, so mustn't used multiple times in the same output

View File

@ -246,18 +246,6 @@ void ip_copy(IP *target, const IP *source);
/* copies an ip_port structure */ /* copies an ip_port structure */
void ipport_copy(IP_Port *target, const IP_Port *source); void ipport_copy(IP_Port *target, const IP_Port *source);
/* packs IP into data, writes SIZE_IP bytes to data */
void ip_pack(uint8_t *data, const IP *source);
/* unpacks IP from data, reads SIZE_IP bytes from data
return 0 on success, -1 on failure. */
int ip_unpack(IP *target, const uint8_t *data, unsigned int data_size);
/* packs IP_Port into data, writes SIZE_IPPORT bytes to data */
void ipport_pack(uint8_t *data, const IP_Port *source);
/* unpacks IP_Port from data, reads SIZE_IPPORT bytes to data
return 0 on success, -1 on failure. */
int ipport_unpack(IP_Port *target, const uint8_t *data, unsigned int data_size);
/* /*
* addr_resolve(): * addr_resolve():
* uses getaddrinfo to resolve an address into an IP address * uses getaddrinfo to resolve an address into an IP address

View File

@ -45,6 +45,58 @@ static void change_symmetric_key(Onion *onion)
} }
} }
/* packing and unpacking functions */
static void ip_pack(uint8_t *data, IP source)
{
to_net_family(&source);
data[0] = source.family;
if (source.family == TOX_AF_INET || source.family == TOX_TCP_INET) {
memset(data + 1, 0, SIZE_IP6);
memcpy(data + 1, source.ip4.uint8, SIZE_IP4);
} else {
memcpy(data + 1, source.ip6.uint8, SIZE_IP6);
}
}
/* return 0 on success, -1 on failure. */
static int ip_unpack(IP *target, const uint8_t *data, unsigned int data_size)
{
if (data_size < (1 + SIZE_IP6))
return -1;
target->family = data[0];
if (target->family == TOX_AF_INET || target->family == TOX_TCP_INET) {
memcpy(target->ip4.uint8, data + 1, SIZE_IP4);
} else {
memcpy(target->ip6.uint8, data + 1, SIZE_IP6);
}
return to_host_family(target);
}
static void ipport_pack(uint8_t *data, const IP_Port *source)
{
ip_pack(data, source->ip);
memcpy(data + SIZE_IP, &source->port, SIZE_PORT);
}
/* return 0 on success, -1 on failure. */
static int ipport_unpack(IP_Port *target, const uint8_t *data, unsigned int data_size)
{
if (data_size < (SIZE_IP + SIZE_PORT))
return -1;
if (ip_unpack(&target->ip, data, data_size) == -1)
return -1;
memcpy(&target->port, data + SIZE_IP, SIZE_PORT);
return 0;
}
/* Create a new onion path. /* Create a new onion path.
* *
* Create a new onion path out of nodes (nodes is a list of 3 nodes) * 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
memcpy(new_path->node_public_key2, nodes[1].client_id, crypto_box_PUBLICKEYBYTES); memcpy(new_path->node_public_key2, nodes[1].client_id, crypto_box_PUBLICKEYBYTES);
memcpy(new_path->node_public_key3, nodes[2].client_id, crypto_box_PUBLICKEYBYTES); memcpy(new_path->node_public_key3, nodes[2].client_id, crypto_box_PUBLICKEYBYTES);
/* to_net_family(&new_path->ip_port1.ip); */
to_net_family(&new_path->ip_port2.ip);
to_net_family(&new_path->ip_port3.ip);
return 0; return 0;
} }
@ -102,9 +150,6 @@ int onion_path_to_nodes(Node_format *nodes, unsigned int num_nodes, const Onion_
nodes[1].ip_port = path->ip_port2; nodes[1].ip_port = path->ip_port2;
nodes[2].ip_port = path->ip_port3; nodes[2].ip_port = path->ip_port3;
to_host_family(&nodes[1].ip_port.ip);
to_host_family(&nodes[2].ip_port.ip);
memcpy(nodes[0].client_id, path->node_public_key1, crypto_box_PUBLICKEYBYTES); memcpy(nodes[0].client_id, path->node_public_key1, crypto_box_PUBLICKEYBYTES);
memcpy(nodes[1].client_id, path->node_public_key2, crypto_box_PUBLICKEYBYTES); memcpy(nodes[1].client_id, path->node_public_key2, crypto_box_PUBLICKEYBYTES);
memcpy(nodes[2].client_id, path->node_public_key3, crypto_box_PUBLICKEYBYTES); 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
if (1 + length + SEND_1 > max_packet_length || length == 0) if (1 + length + SEND_1 > max_packet_length || length == 0)
return -1; return -1;
to_net_family(&dest.ip);
uint8_t step1[SIZE_IPPORT + length]; uint8_t step1[SIZE_IPPORT + length];
ipport_pack(step1, &dest); ipport_pack(step1, &dest);
memcpy(step1 + SIZE_IPPORT, data, length); memcpy(step1 + SIZE_IPPORT, data, length);
@ -183,10 +226,8 @@ int create_onion_packet_tcp(uint8_t *packet, uint16_t max_packet_length, const O
if (crypto_box_NONCEBYTES + SIZE_IPPORT + SEND_BASE * 2 + length > max_packet_length || length == 0) if (crypto_box_NONCEBYTES + SIZE_IPPORT + SEND_BASE * 2 + length > max_packet_length || length == 0)
return -1; return -1;
to_net_family(&dest.ip);
uint8_t step1[SIZE_IPPORT + length]; uint8_t step1[SIZE_IPPORT + length];
ipport_pack(step1, &dest); ipport_pack(step1, &dest);
memcpy(step1 + SIZE_IPPORT, data, length); memcpy(step1 + SIZE_IPPORT, data, length);
@ -297,9 +338,6 @@ int onion_send_1(const Onion *onion, const uint8_t *plain, uint16_t len, IP_Port
if (ipport_unpack(&send_to, plain, len) == -1) if (ipport_unpack(&send_to, plain, len) == -1)
return 1; return 1;
if (to_host_family(&send_to.ip) == -1)
return 1;
uint8_t ip_port[SIZE_IPPORT]; uint8_t ip_port[SIZE_IPPORT];
ipport_pack(ip_port, &source); ipport_pack(ip_port, &source);
@ -350,9 +388,6 @@ static int handle_send_1(void *object, IP_Port source, const uint8_t *packet, ui
if (ipport_unpack(&send_to, plain, len) == -1) if (ipport_unpack(&send_to, plain, len) == -1)
return 1; return 1;
if (to_host_family(&send_to.ip) == -1)
return 1;
uint8_t data[ONION_MAX_PACKET_SIZE]; uint8_t data[ONION_MAX_PACKET_SIZE];
data[0] = NET_PACKET_ONION_SEND_2; data[0] = NET_PACKET_ONION_SEND_2;
memcpy(data + 1, packet + 1, crypto_box_NONCEBYTES); 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
if (ipport_unpack(&send_to, plain, len) == -1) if (ipport_unpack(&send_to, plain, len) == -1)
return 1; return 1;
if (to_host_family(&send_to.ip) == -1)
return 1;
uint8_t data[ONION_MAX_PACKET_SIZE]; uint8_t data[ONION_MAX_PACKET_SIZE];
memcpy(data, plain + SIZE_IPPORT, len - SIZE_IPPORT); memcpy(data, plain + SIZE_IPPORT, len - SIZE_IPPORT);
uint16_t data_len = (len - SIZE_IPPORT); uint16_t data_len = (len - SIZE_IPPORT);