Skip to content

Commit

Permalink
cleanup: Add Network object parameter for addr_resolve.
Browse files Browse the repository at this point in the history
This function doesn't use Network yet, but it will in the future, and
for now it's better to pass Network to all network I/O functions.
  • Loading branch information
iphydf committed Apr 1, 2022
1 parent e04484e commit d0ebc21
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 32 deletions.
11 changes: 7 additions & 4 deletions auto_tests/network_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ static void test_addr_resolv_localhost(void)
errno = 0;
#endif

const Network *ns = system_network();
ck_assert(ns != nullptr);

const char localhost[] = "localhost";

IP ip;
ip_init(&ip, 0); // ipv6enabled = 0

bool res = addr_resolve_or_parse_ip(localhost, &ip, nullptr);
bool res = addr_resolve_or_parse_ip(ns, localhost, &ip, nullptr);

int error = net_error();
char *strerror = net_new_strerror(error);
Expand All @@ -39,14 +42,14 @@ static void test_addr_resolv_localhost(void)
ip_ntoa(&ip, ip_str, sizeof(ip_str)));

ip_init(&ip, 1); // ipv6enabled = 1
res = addr_resolve_or_parse_ip(localhost, &ip, nullptr);
res = addr_resolve_or_parse_ip(ns, localhost, &ip, nullptr);

#if USE_IPV6

int localhost_split = 0;

if (!net_family_is_ipv6(ip.family)) {
res = addr_resolve_or_parse_ip("ip6-localhost", &ip, nullptr);
res = addr_resolve_or_parse_ip(ns, "ip6-localhost", &ip, nullptr);
localhost_split = 1;
}

Expand All @@ -72,7 +75,7 @@ static void test_addr_resolv_localhost(void)
ip.family = net_family_unspec;
IP extra;
ip_reset(&extra);
res = addr_resolve_or_parse_ip(localhost, &ip, &extra);
res = addr_resolve_or_parse_ip(ns, localhost, &ip, &extra);
error = net_error();
strerror = net_new_strerror(error);
ck_assert_msg(res, "Resolver failed: %d, %s", error, strerror);
Expand Down
17 changes: 9 additions & 8 deletions auto_tests/onion_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,16 @@ static void send_onion_packet(const Networking_Core *net, const Onion_Path *path
/** Initialize networking.
* Added for reverse compatibility with old new_networking calls.
*/
static Networking_Core *new_networking(const Logger *log, const IP *ip, uint16_t port)
static Networking_Core *new_networking(const Logger *log, const Network *ns, const IP *ip, uint16_t port)
{
const Network *ns = system_network();
return new_networking_ex(log, ns, ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM), nullptr);
}

static void test_basic(void)
{
uint32_t index[] = { 1, 2, 3 };
const Network *ns = system_network();

Logger *log1 = logger_new();
logger_callback_log(log1, (logger_cb *)print_debug_log, nullptr, &index[0]);
Logger *log2 = logger_new();
Expand All @@ -198,8 +199,8 @@ static void test_basic(void)
Mono_Time *mono_time2 = mono_time_new();

IP ip = get_loopback();
Onion *onion1 = new_onion(log1, mono_time1, new_dht(log1, mono_time1, new_networking(log1, &ip, 36567), true, false));
Onion *onion2 = new_onion(log2, mono_time2, new_dht(log2, mono_time2, new_networking(log2, &ip, 36568), true, false));
Onion *onion1 = new_onion(log1, mono_time1, new_dht(log1, ns, mono_time1, new_networking(log1, ns, &ip, 36567), true, false));
Onion *onion2 = new_onion(log2, mono_time2, new_dht(log2, ns, mono_time2, new_networking(log2, ns, &ip, 36568), true, false));
ck_assert_msg((onion1 != nullptr) && (onion2 != nullptr), "Onion failed initializing.");
networking_registerhandler(onion2->net, NET_PACKET_ANNOUNCE_REQUEST_OLD, &handle_test_1, onion2);

Expand Down Expand Up @@ -292,7 +293,7 @@ static void test_basic(void)

Mono_Time *mono_time3 = mono_time_new();

Onion *onion3 = new_onion(log3, mono_time3, new_dht(log3, mono_time3, new_networking(log3, &ip, 36569), true, false));
Onion *onion3 = new_onion(log3, mono_time3, new_dht(log3, ns, mono_time3, new_networking(log3, ns, &ip, 36569), true, false));
ck_assert_msg((onion3 != nullptr), "Onion failed initializing.");

random_nonce(nonce);
Expand Down Expand Up @@ -361,6 +362,7 @@ static Onions *new_onions(uint16_t port, uint32_t *index)
{
IP ip = get_loopback();
ip.ip.v6.uint8[15] = 1;
const Network *ns = system_network();
Onions *on = (Onions *)malloc(sizeof(Onions));

if (!on) {
Expand All @@ -384,7 +386,7 @@ static Onions *new_onions(uint16_t port, uint32_t *index)
return nullptr;
}

Networking_Core *net = new_networking(on->log, &ip, port);
Networking_Core *net = new_networking(on->log, ns, &ip, port);

if (!net) {
mono_time_free(on->mono_time);
Expand All @@ -393,7 +395,7 @@ static Onions *new_onions(uint16_t port, uint32_t *index)
return nullptr;
}

DHT *dht = new_dht(on->log, on->mono_time, net, true, false);
DHT *dht = new_dht(on->log, ns, on->mono_time, net, true, false);

if (!dht) {
kill_networking(net);
Expand Down Expand Up @@ -427,7 +429,6 @@ static Onions *new_onions(uint16_t port, uint32_t *index)
}

TCP_Proxy_Info inf = {{{{0}}}};
const Network *ns = system_network();
on->onion_c = new_onion_client(on->log, on->mono_time, new_net_crypto(on->log, on->mono_time, ns, dht, &inf));

if (!on->onion_c) {
Expand Down
2 changes: 1 addition & 1 deletion other/DHT_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ int main(int argc, char *argv[])
const uint16_t start_port = PORT;
const uint16_t end_port = start_port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM);
const Network *ns = system_network();
DHT *dht = new_dht(logger, mono_time, new_networking_ex(logger, ns, &ip, start_port, end_port, nullptr), true, true);
DHT *dht = new_dht(logger, ns, mono_time, new_networking_ex(logger, ns, &ip, start_port, end_port, nullptr), true, true);
Onion *onion = new_onion(logger, mono_time, dht);
const Onion_Announce *onion_a = new_onion_announce(logger, mono_time, dht);

Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
073710c9592554bbcb8bd094c9d64a987d52a2d1dedf965557b56fe848ddc14b /usr/local/bin/tox-bootstrapd
c40a0017ec22042f0be897893a774b89a6b6fbb621ac4d7f68416134fe4a78a2 /usr/local/bin/tox-bootstrapd
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/src/tox-bootstrapd.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ int main(int argc, char *argv[])

mono_time_update(mono_time);

DHT *const dht = new_dht(logger, mono_time, net, true, enable_lan_discovery);
DHT *const dht = new_dht(logger, ns, mono_time, net, true, enable_lan_discovery);

if (dht == nullptr) {
log_write(LOG_LEVEL_ERROR, "Couldn't initialize Tox DHT instance. Exiting.\n");
Expand Down
8 changes: 5 additions & 3 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ typedef struct Cryptopacket_Handler {

struct DHT {
const Logger *log;
const Network *ns;
Mono_Time *mono_time;
Networking_Core *net;

Expand Down Expand Up @@ -1910,7 +1911,7 @@ int dht_bootstrap_from_address(DHT *dht, const char *address, bool ipv6enabled,
ip_extra = &ip_port_v4.ip;
}

if (addr_resolve_or_parse_ip(address, &ip_port_v64.ip, ip_extra)) {
if (addr_resolve_or_parse_ip(dht->ns, address, &ip_port_v64.ip, ip_extra)) {
ip_port_v64.port = port;
dht_bootstrap(dht, &ip_port_v64, public_key);

Expand Down Expand Up @@ -2568,8 +2569,8 @@ static int handle_LANdiscovery(void *object, const IP_Port *source, const uint8_

/*----------------------------------------------------------------------------------*/

DHT *new_dht(const Logger *log, Mono_Time *mono_time, Networking_Core *net, bool hole_punching_enabled,
bool lan_discovery_enabled)
DHT *new_dht(const Logger *log, const Network *ns, Mono_Time *mono_time, Networking_Core *net,
bool hole_punching_enabled, bool lan_discovery_enabled)
{
if (net == nullptr) {
return nullptr;
Expand All @@ -2581,6 +2582,7 @@ DHT *new_dht(const Logger *log, Mono_Time *mono_time, Networking_Core *net, bool
return nullptr;
}

dht->ns = ns;
dht->mono_time = mono_time;
dht->cur_time = mono_time_get(mono_time);
dht->log = log;
Expand Down
4 changes: 2 additions & 2 deletions toxcore/DHT.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,8 @@ int dht_load(DHT *dht, const uint8_t *data, uint32_t length);

/** Initialize DHT. */
non_null()
DHT *new_dht(const Logger *log, Mono_Time *mono_time, Networking_Core *net, bool hole_punching_enabled,
bool lan_discovery_enabled);
DHT *new_dht(const Logger *log, const Network *ns, Mono_Time *mono_time, Networking_Core *net,
bool hole_punching_enabled, bool lan_discovery_enabled);

non_null()
void kill_dht(DHT *dht);
Expand Down
4 changes: 2 additions & 2 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -3196,7 +3196,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Network *ns, Messenger_Opti
}

if (options->udp_disabled) {
m->net = new_networking_no_udp(m->log);
m->net = new_networking_no_udp(m->log, m->ns);
} else {
IP ip;
ip_init(&ip, options->ipv6enabled);
Expand All @@ -3215,7 +3215,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Network *ns, Messenger_Opti
return nullptr;
}

m->dht = new_dht(m->log, m->mono_time, m->net, options->hole_punching_enabled, options->local_discovery_enabled);
m->dht = new_dht(m->log, m->ns, m->mono_time, m->net, options->hole_punching_enabled, options->local_discovery_enabled);

if (m->dht == nullptr) {
kill_networking(m->net);
Expand Down
11 changes: 6 additions & 5 deletions toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,7 @@ Networking_Core *new_networking_ex(
return nullptr;
}

Networking_Core *new_networking_no_udp(const Logger *log)
Networking_Core *new_networking_no_udp(const Logger *log, const Network *ns)
{
if (networking_at_startup() != 0) {
return nullptr;
Expand All @@ -1285,6 +1285,7 @@ Networking_Core *new_networking_no_udp(const Logger *log)
}

net->log = log;
net->ns = ns;

return net;
}
Expand Down Expand Up @@ -1536,8 +1537,8 @@ bool addr_parse_ip(const char *address, IP *to)
*
* @return 0 on failure, `TOX_ADDR_RESOLVE_*` on success.
*/
non_null(1, 2) nullable(3)
static int addr_resolve(const char *address, IP *to, IP *extra)
non_null(1, 2, 3) nullable(4)
static int addr_resolve(const Network *ns, const char *address, IP *to, IP *extra)
{
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
return 0;
Expand Down Expand Up @@ -1633,9 +1634,9 @@ static int addr_resolve(const char *address, IP *to, IP *extra)
#endif
}

bool addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra)
bool addr_resolve_or_parse_ip(const Network *ns, const char *address, IP *to, IP *extra)
{
if (addr_resolve(address, to, extra) == 0) {
if (addr_resolve(ns, address, to, extra) == 0) {
if (!addr_parse_ip(address, to)) {
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions toxcore/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@ void ipport_copy(IP_Port *target, const IP_Port *source);
*
* @return true on success, false on failure
*/
non_null(1, 2) nullable(3)
bool addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra);
non_null(1, 2, 3) nullable(4)
bool addr_resolve_or_parse_ip(const Network *ns, const char *address, IP *to, IP *extra);

/** @brief Function to receive data, ip and port of sender is put into ip_port.
* Packet data is put into data.
Expand Down Expand Up @@ -582,7 +582,7 @@ Networking_Core *new_networking_ex(
const Logger *log, const Network *ns, const IP *ip,
uint16_t port_from, uint16_t port_to, unsigned int *error);
non_null()
Networking_Core *new_networking_no_udp(const Logger *log);
Networking_Core *new_networking_no_udp(const Logger *log, const Network *ns);

/** Function to cleanup networking stuff (doesn't do much right now). */
non_null()
Expand Down
5 changes: 3 additions & 2 deletions toxcore/tox.c
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,8 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
}
}

tox_set_network(tox, nullptr);

if (m_options.proxy_info.proxy_type != TCP_PROXY_NONE) {
if (tox_options_get_proxy_port(opts) == 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_PORT);
Expand All @@ -536,7 +538,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)

const char *const proxy_host = tox_options_get_proxy_host(opts);

if (proxy_host == nullptr || !addr_resolve_or_parse_ip(proxy_host, &m_options.proxy_info.ip_port.ip, nullptr)) {
if (proxy_host == nullptr || !addr_resolve_or_parse_ip(&tox->ns, proxy_host, &m_options.proxy_info.ip_port.ip, nullptr)) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_HOST);
// TODO(irungentoo): TOX_ERR_NEW_PROXY_NOT_FOUND if domain.
tox_options_free(default_options);
Expand All @@ -548,7 +550,6 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
}

tox->mono_time = mono_time_new();
tox_set_network(tox, nullptr);

if (tox->mono_time == nullptr) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC);
Expand Down

0 comments on commit d0ebc21

Please sign in to comment.