From c5eb7568405a3a9256386d524459320ae3ada6f2 Mon Sep 17 00:00:00 2001 From: Logan oos Even <46396513+Logan007@users.noreply.github.com> Date: Mon, 30 Aug 2021 16:03:11 +0545 Subject: [PATCH] de-nested one if block --- src/n2n.c | 77 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 39 insertions(+), 38 deletions(-) diff --git a/src/n2n.c b/src/n2n.c index c00372a..058e352 100644 --- a/src/n2n.c +++ b/src/n2n.c @@ -427,48 +427,49 @@ uint8_t resolve_check (n2n_resolve_parameter_t *param, uint8_t requires_resoluti uint8_t ret = requires_resolution; /* if trylock fails, it still requires resolution */ -#ifdef HAVE_PTHREAD - if(param != NULL) { - n2n_resolve_ip_sock_t *entry, *tmp_entry; - n2n_sock_str_t sock_buf; - - // check_interval and last_check do not need to be guarded by the mutex because - // their values get changed and evaluated only here - - if((now - param->last_checked > param->check_interval) || (requires_resolution)) { - // try to lock access - if(pthread_mutex_trylock(¶m->access) == 0) { - // any changes? - if(param->changed) { - // reset flag - param->changed = 0; - // unselectively copy all socks (even those with error code, that would be the old one because - // sockets do not get overwritten in case of error in resolve_thread) from list to supernode list - HASH_ITER(hh, param->list, entry, tmp_entry) { - memcpy(entry->org_sock, &entry->sock, sizeof(n2n_sock_t)); - traceEvent(TRACE_INFO, "resolve_check renews ip address of supernode '%s' to %s", - entry->org_ip, sock_to_cstr(sock_buf, &(entry->sock))); - } - } +#ifdef HAVE_PTHREAD + n2n_resolve_ip_sock_t *entry, *tmp_entry; + n2n_sock_str_t sock_buf; + + if(NULL == param) + return ret; + + // check_interval and last_check do not need to be guarded by the mutex because + // their values get changed and evaluated only here + + if((now - param->last_checked > param->check_interval) || (requires_resolution)) { + // try to lock access + if(pthread_mutex_trylock(¶m->access) == 0) { + // any changes? + if(param->changed) { + // reset flag + param->changed = 0; + // unselectively copy all socks (even those with error code, that would be the old one because + // sockets do not get overwritten in case of error in resolve_thread) from list to supernode list + HASH_ITER(hh, param->list, entry, tmp_entry) { + memcpy(entry->org_sock, &entry->sock, sizeof(n2n_sock_t)); + traceEvent(TRACE_INFO, "resolve_check renews ip address of supernode '%s' to %s", + entry->org_ip, sock_to_cstr(sock_buf, &(entry->sock))); + } + } - // let the resolver thread know eventual difficulties in reaching the supernode - if(requires_resolution) { - param->request = 1; - ret = 0; - } + // let the resolver thread know eventual difficulties in reaching the supernode + if(requires_resolution) { + param->request = 1; + ret = 0; + } - param->last_checked = now; + param->last_checked = now; - // next appointment - if(param->request) - // earlier if resolver still working on fulfilling a request - param->check_interval = N2N_RESOLVE_CHECK_INTERVAL / 10; - else - param->check_interval = N2N_RESOLVE_CHECK_INTERVAL; + // next appointment + if(param->request) + // earlier if resolver still working on fulfilling a request + param->check_interval = N2N_RESOLVE_CHECK_INTERVAL / 10; + else + param->check_interval = N2N_RESOLVE_CHECK_INTERVAL; - // unlock access - pthread_mutex_unlock(¶m->access); - } + // unlock access + pthread_mutex_unlock(¶m->access); } } #endif