Skip to content

Commit

Permalink
Make onion announce code work with vanilla nacl
Browse files Browse the repository at this point in the history
  • Loading branch information
JFreegman committed May 2, 2020
1 parent eaef66f commit 2914efb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
23 changes: 21 additions & 2 deletions toxcore/onion_announce.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ int create_announce_request(uint8_t *packet, uint16_t max_packet_length, const u
return ONION_ANNOUNCE_REQUEST_MIN_SIZE;
}

#ifndef VANILLA_NACL

// TODO: params - to struct
int create_gc_announce_request(uint8_t *packet, uint16_t max_packet_length, const uint8_t *dest_client_id,
const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *ping_id,
Expand Down Expand Up @@ -146,6 +148,7 @@ int create_gc_announce_request(uint8_t *packet, uint16_t max_packet_length, cons

return full_length;
}
#endif // VANILLA_NACL

/* Create an onion data request packet in packet of max_packet_length (recommended size ONION_MAX_PACKET_SIZE).
*
Expand Down Expand Up @@ -408,6 +411,10 @@ static int handle_gc_announce_request(Onion_Announce *onion_a, IP_Port source, c
return 1;
}

#ifdef VANILLA_NACL
return 1;
#endif

const uint8_t *packet_public_key = packet + 1 + CRYPTO_NONCE_SIZE;
uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE];
get_shared_key(onion_a->mono_time, &onion_a->shared_keys_recv, shared_key, dht_get_self_secret_key(onion_a->dht),
Expand Down Expand Up @@ -538,7 +545,11 @@ static int handle_announce_request(void *object, IP_Port source, const uint8_t *
Onion_Announce *onion_a = (Onion_Announce *)object;

if (length != ANNOUNCE_REQUEST_MIN_SIZE_RECV) {
#ifndef VANILLA_NACL
return handle_gc_announce_request(onion_a, source, packet, length);
#else
return 1;
#endif
}

const uint8_t *packet_public_key = packet + 1 + CRYPTO_NONCE_SIZE;
Expand Down Expand Up @@ -666,20 +677,28 @@ static int handle_data_request(void *object, IP_Port source, const uint8_t *pack

Onion_Announce *new_onion_announce(Mono_Time *mono_time, DHT *dht, GC_Announces_List *gc_announces_list)
{
if (dht == nullptr || gc_announces_list == nullptr) {
if (dht == nullptr) {
return nullptr;
}

#ifndef VANILLA_NACL

if (gc_announces_list == nullptr) {
return nullptr;
}

#endif

Onion_Announce *onion_a = (Onion_Announce *)calloc(1, sizeof(Onion_Announce));

if (onion_a == nullptr) {
return nullptr;
}

onion_a->gc_announces_list = gc_announces_list;
onion_a->mono_time = mono_time;
onion_a->dht = dht;
onion_a->net = dht_get_net(dht);
onion_a->gc_announces_list = gc_announces_list;
new_symmetric_key(onion_a->secret_bytes);

networking_registerhandler(onion_a->net, NET_PACKET_ANNOUNCE_REQUEST, &handle_announce_request, onion_a);
Expand Down
15 changes: 14 additions & 1 deletion toxcore/onion_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,15 @@ static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, IP_
onion_friend->temp_secret_key, ping_id, onion_friend->real_public_key,
zero_ping_id, sendback);
} else if (onion_friend->gc_data_length > 0) { // contact is a gc

#ifndef VANILLA_NACL
len = create_gc_announce_request(request, sizeof(request), dest_pubkey, onion_friend->temp_public_key,
onion_friend->temp_secret_key, ping_id, onion_friend->real_public_key,
zero_ping_id, sendback, onion_friend->gc_data,
onion_friend->gc_data_length);
#else
return -1;
#endif // VANILLA_NACL
} else {
return 0;
}
Expand Down Expand Up @@ -1791,10 +1796,18 @@ void do_onion_client(Onion_Client *onion_c)

Onion_Client *new_onion_client(Mono_Time *mono_time, Net_Crypto *c, GC_Session *gc_session)
{
if (!c || !gc_session) {
if (!c) {
return nullptr;
}

#ifndef VANILLA_NACL

if (gc_session == nullptr) {
return nullptr;
}

#endif

Onion_Client *onion_c = (Onion_Client *)calloc(1, sizeof(Onion_Client));

if (onion_c == nullptr) {
Expand Down

0 comments on commit 2914efb

Please sign in to comment.