From a9fbdaf46b23db5c598bf33d6bc5c4555b06e674 Mon Sep 17 00:00:00 2001 From: iphydf Date: Wed, 31 Aug 2016 20:40:20 +0100 Subject: [PATCH] Do not use `else` after `return`. http://llvm.org/docs/CodingStandards.html#use-early-exits-and-continue-to-simplify-code --- auto_tests/assoc_test.c | 1 - auto_tests/crypto_test.c | 1 - auto_tests/dht_test.c | 10 +- auto_tests/tox_test.c | 45 ++++----- auto_tests/toxav_basic_test.c | 1 - testing/DHT_test.c | 1 - testing/Messenger_test.c | 1 - testing/av_test.c | 1 - testing/nTox.c | 78 +++++++-------- testing/tox_sync.c | 2 - toxav/audio.c | 12 +-- toxav/group.c | 1 - toxav/msi.c | 9 +- toxav/rtp.c | 164 ++++++++++++++++---------------- toxav/toxav.c | 2 - toxcore/DHT.c | 68 +++++++------ toxcore/LAN_discovery.c | 1 - toxcore/Messenger.c | 55 +++++------ toxcore/TCP_client.c | 6 +- toxcore/TCP_connection.c | 97 ++++++++++--------- toxcore/TCP_server.c | 21 ++-- toxcore/assoc.c | 18 ++-- toxcore/friend_connection.c | 27 +++--- toxcore/group.c | 41 ++++---- toxcore/list.c | 8 +- toxcore/net_crypto.c | 56 +++++------ toxcore/network.c | 12 ++- toxcore/onion.c | 6 +- toxcore/onion_client.c | 20 ++-- toxcore/tox.c | 51 +++++----- toxencryptsave/toxencryptsave.c | 4 +- 31 files changed, 411 insertions(+), 409 deletions(-) diff --git a/auto_tests/assoc_test.c b/auto_tests/assoc_test.c index e9f5207f0f..79a97fd5bf 100644 --- a/auto_tests/assoc_test.c +++ b/auto_tests/assoc_test.c @@ -101,7 +101,6 @@ START_TEST(test_fillup) if (j % 16 == 0) { memcpy(entries[j].id, id, crypto_box_PUBLICKEYBYTES - 30); memcpy(&closest[j / 16], &entries[j], sizeof(struct entry)); - } uint8_t res = Assoc_add_entry(assoc, entries[j].id, &entries[j].ippts_send, &entries[j].ipp_recv, 1); diff --git a/auto_tests/crypto_test.c b/auto_tests/crypto_test.c index eeb995675b..30cc2178b6 100644 --- a/auto_tests/crypto_test.c +++ b/auto_tests/crypto_test.c @@ -131,7 +131,6 @@ START_TEST(test_fast_known) ck_assert_msg(memcmp(test_m, m, sizeof(m)) == 0, "decrypted text doesn't match test vector"); ck_assert_msg(mlen == sizeof(m) / sizeof(unsigned char), "wrong plaintext length"); - } END_TEST diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c index 92209e7ae2..7aab9573f5 100644 --- a/auto_tests/dht_test.c +++ b/auto_tests/dht_test.c @@ -404,10 +404,10 @@ void test_add_to_list(uint8_t cmp_list[][crypto_box_PUBLICKEYBYTES + 1], unsigne memcpy(cmp_list[i], pk, crypto_box_PUBLICKEYBYTES); cmp_list[i][crypto_box_PUBLICKEYBYTES] = 1; return; - } else { - if (memcmp(cmp_list[i], pk, crypto_box_PUBLICKEYBYTES) == 0) { - return; - } + } + + if (memcmp(cmp_list[i], pk, crypto_box_PUBLICKEYBYTES) == 0) { + return; } } @@ -557,8 +557,6 @@ END_TEST void ip_callback(void *data, int32_t number, IP_Port ip_port) { - - } diff --git a/auto_tests/tox_test.c b/auto_tests/tox_test.c index b2dc0c17f6..9af7ee41cf 100644 --- a/auto_tests/tox_test.c +++ b/auto_tests/tox_test.c @@ -614,13 +614,12 @@ START_TEST(test_few_clients) tox_iterate(tox2, &to_compare); tox_iterate(tox3, &to_compare); - if (typing_changes == 2) { break; - } else { - ck_assert_msg(typing_changes == 0, "Typing fail"); } + ck_assert_msg(typing_changes == 0, "Typing fail"); + c_sleep(50); } @@ -635,10 +634,10 @@ START_TEST(test_few_clients) if (typing_changes == 1) { break; - } else { - ck_assert_msg(typing_changes == 0, "Typing fail"); } + ck_assert_msg(typing_changes == 0, "Typing fail"); + c_sleep(50); } @@ -663,10 +662,10 @@ START_TEST(test_few_clients) if (custom_packet == 1) { break; - } else { - ck_assert_msg(custom_packet == 0, "Lossless packet fail"); } + ck_assert_msg(custom_packet == 0, "Lossless packet fail"); + c_sleep(50); } @@ -686,10 +685,10 @@ START_TEST(test_few_clients) if (custom_packet == 1) { break; - } else { - ck_assert_msg(custom_packet == 0, "lossy packet fail"); } + ck_assert_msg(custom_packet == 0, "lossy packet fail"); + c_sleep(50); } @@ -725,11 +724,11 @@ START_TEST(test_few_clients) if (sendf_ok && file_recv && totalf_size == file_size && size_recv == file_size && sending_pos == size_recv && file_accepted == 1) { break; - } else { - ck_abort_msg("Something went wrong in file transfer %u %u %u %u %u %u %llu %llu %llu", sendf_ok, file_recv, - totalf_size == file_size, size_recv == file_size, sending_pos == size_recv, file_accepted == 1, totalf_size, size_recv, - sending_pos); } + + ck_abort_msg("Something went wrong in file transfer %u %u %u %u %u %u %llu %llu %llu", sendf_ok, file_recv, + totalf_size == file_size, size_recv == file_size, sending_pos == size_recv, file_accepted == 1, totalf_size, size_recv, + sending_pos); } uint32_t tox1_interval = tox_iteration_interval(tox1); @@ -772,12 +771,12 @@ START_TEST(test_few_clients) if (sendf_ok && file_recv && m_send_reached && totalf_size == file_size && size_recv == max_sending && sending_pos == size_recv && file_accepted == 1) { break; - } else { - ck_abort_msg("Something went wrong in file transfer %u %u %u %u %u %u %u %llu %llu %llu %llu", sendf_ok, file_recv, - m_send_reached, totalf_size == file_size, size_recv == max_sending, sending_pos == size_recv, file_accepted == 1, - totalf_size, file_size, - size_recv, sending_pos); } + + ck_abort_msg("Something went wrong in file transfer %u %u %u %u %u %u %u %llu %llu %llu %llu", sendf_ok, file_recv, + m_send_reached, totalf_size == file_size, size_recv == max_sending, sending_pos == size_recv, file_accepted == 1, + totalf_size, file_size, + size_recv, sending_pos); } uint32_t tox1_interval = tox_iteration_interval(tox1); @@ -806,7 +805,6 @@ START_TEST(test_few_clients) ck_assert_msg(tox_file_get_file_id(tox2, 0, fnum, file_cmp_id, &gfierr), "tox_file_get_file_id failed"); ck_assert_msg(gfierr == TOX_ERR_FILE_GET_OK, "wrong error"); - while (1) { tox_iterate(tox1, &to_compare); tox_iterate(tox2, &to_compare); @@ -816,11 +814,11 @@ START_TEST(test_few_clients) if (sendf_ok && file_recv && totalf_size == file_size && size_recv == file_size && sending_pos == size_recv && file_accepted == 1) { break; - } else { - ck_abort_msg("Something went wrong in file transfer %u %u %u %u %u %u %llu %llu %llu", sendf_ok, file_recv, - totalf_size == file_size, size_recv == file_size, sending_pos == size_recv, file_accepted == 1, totalf_size, size_recv, - sending_pos); } + + ck_abort_msg("Something went wrong in file transfer %u %u %u %u %u %u %llu %llu %llu", sendf_ok, file_recv, + totalf_size == file_size, size_recv == file_size, sending_pos == size_recv, file_accepted == 1, totalf_size, size_recv, + sending_pos); } uint32_t tox1_interval = tox_iteration_interval(tox1); @@ -1013,7 +1011,6 @@ START_TEST(test_many_clients_tcp) ++counter; } } - } if (counter == NUM_FRIENDS * 2) { diff --git a/auto_tests/toxav_basic_test.c b/auto_tests/toxav_basic_test.c index 971c1d8316..747c3aa130 100644 --- a/auto_tests/toxav_basic_test.c +++ b/auto_tests/toxav_basic_test.c @@ -49,7 +49,6 @@ typedef struct { bool incoming; uint32_t state; - } CallControl; diff --git a/testing/DHT_test.c b/testing/DHT_test.c index e782d76521..f6a2c4bc1b 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c @@ -98,7 +98,6 @@ void print_assoc(IPPTsPng *assoc, uint8_t ours) printf("Timestamp: %llu\n", (long long unsigned int) assoc->ret_timestamp); print_hardening(&assoc->hardening); - } void print_clientlist(DHT *dht) diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index 790439e726..edfe4fb108 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c @@ -146,7 +146,6 @@ int main(int argc, char *argv[]) read = fread(buffer, 1, 128000, file); printf("Messenger loaded: %i\n", messenger_load(m, buffer, read)); fclose(file); - } m_callback_friendrequest(m, print_request, NULL); diff --git a/testing/av_test.c b/testing/av_test.c index 2c8281f435..cac5abd5ef 100644 --- a/testing/av_test.c +++ b/testing/av_test.c @@ -76,7 +76,6 @@ typedef struct { uint32_t state; pthread_mutex_t arb_mutex[1]; RingBuffer *arb; /* Audio ring buffer */ - } CallControl; struct toxav_thread_data { diff --git a/testing/nTox.c b/testing/nTox.c index d55d76538f..689d200264 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -510,7 +510,9 @@ void line_eval(Tox *m, char *line) new_lines_mark(help_friend1, 1); new_lines_mark(help_friend2, 1); return; - } else if (line[3] == 'g') { + } + + if (line[3] == 'g') { new_lines_mark(help_group, 1); return; } @@ -779,56 +781,56 @@ void wrap_bars(char output[STRING_LENGTH_WRAPPED], char input[STRING_LENGTH], si } continue; - } else { - /* at the limit */ - if (bar_avail > nl_got) { - /* overwrite */ - memcpy(output + bar_avail - 1, wrap_cont_str, wrap_cont_len); - nl_got = bar_avail; - - ipos--; - continue; - } + } - if (space_avail > nl_got) { - if (opos + wrap_cont_len - 1 >= STRING_LENGTH_WRAPPED) { - opos = STRING_LENGTH_WRAPPED - 1; - break; - } + /* at the limit */ + if (bar_avail > nl_got) { + /* overwrite */ + memcpy(output + bar_avail - 1, wrap_cont_str, wrap_cont_len); + nl_got = bar_avail; - /* move forward by 2 characters */ - memmove(output + space_avail + 3, output + space_avail + 1, opos - (space_avail + 1)); - memcpy(output + space_avail, wrap_cont_str, wrap_cont_len); - nl_got = space_avail + 1; + ipos--; + continue; + } - opos += 2; - ipos--; - continue; + if (space_avail > nl_got) { + if (opos + wrap_cont_len - 1 >= STRING_LENGTH_WRAPPED) { + opos = STRING_LENGTH_WRAPPED - 1; + break; } - char c = input[ipos]; + /* move forward by 2 characters */ + memmove(output + space_avail + 3, output + space_avail + 1, opos - (space_avail + 1)); + memcpy(output + space_avail, wrap_cont_str, wrap_cont_len); + nl_got = space_avail + 1; - if ((c == '|') || (c == ' ') || (c == '\n')) { - if (opos + wrap_cont_len >= STRING_LENGTH_WRAPPED) { - opos = STRING_LENGTH_WRAPPED - 1; - break; - } - - memcpy(output + opos, wrap_cont_str, wrap_cont_len); - - nl_got = opos; - opos += wrap_cont_len; - } + opos += 2; + ipos--; + continue; + } - output[opos++] = input[ipos]; + char c = input[ipos]; - if (opos >= STRING_LENGTH_WRAPPED) { + if ((c == '|') || (c == ' ') || (c == '\n')) { + if (opos + wrap_cont_len >= STRING_LENGTH_WRAPPED) { opos = STRING_LENGTH_WRAPPED - 1; break; } - continue; + memcpy(output + opos, wrap_cont_str, wrap_cont_len); + + nl_got = opos; + opos += wrap_cont_len; } + + output[opos++] = input[ipos]; + + if (opos >= STRING_LENGTH_WRAPPED) { + opos = STRING_LENGTH_WRAPPED - 1; + break; + } + + continue; } if (opos >= STRING_LENGTH_WRAPPED) { diff --git a/testing/tox_sync.c b/testing/tox_sync.c index 8fa1162527..f30ce05927 100644 --- a/testing/tox_sync.c +++ b/testing/tox_sync.c @@ -173,7 +173,6 @@ void file_request_accept(Tox *tox, uint32_t friend_number, uint32_t file_number, if (tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_RESUME, 0)) { printf("Accepted file transfer. (file: %s)\n", fullpath); } - } void file_print_control(Tox *tox, uint32_t friend_number, uint32_t file_number, TOX_FILE_CONTROL control, @@ -323,7 +322,6 @@ int main(int argc, char *argv[]) } closedir(d); - } else { printf("\nFailed to open directory.\n"); return 1; diff --git a/toxav/audio.c b/toxav/audio.c index eaa1f6d088..c0bfa60818 100644 --- a/toxav/audio.c +++ b/toxav/audio.c @@ -413,13 +413,13 @@ bool reconfigure_audio_encoder(Logger *log, OpusEncoder **e, int32_t new_br, int *e = new_encoder; } else if (*old_br == new_br) { return true; /* Nothing changed */ - } else { - int status = opus_encoder_ctl(*e, OPUS_SET_BITRATE(new_br)); + } - if (status != OPUS_OK) { - LOGGER_ERROR(log, "Error while setting encoder ctl: %s", opus_strerror(status)); - return false; - } + int status = opus_encoder_ctl(*e, OPUS_SET_BITRATE(new_br)); + + if (status != OPUS_OK) { + LOGGER_ERROR(log, "Error while setting encoder ctl: %s", opus_strerror(status)); + return false; } *old_br = new_br; diff --git a/toxav/group.c b/toxav/group.c index dea10af701..969250f877 100644 --- a/toxav/group.c +++ b/toxav/group.c @@ -370,7 +370,6 @@ static int decode_audio_packet(Group_AV *group_av, Group_Peer_AV *peer_av, int g if (out_audio_samples <= 0) { return -1; } - } if (out_audio) { diff --git a/toxav/msi.c b/toxav/msi.c index 3becac7e90..a62bdb35aa 100644 --- a/toxav/msi.c +++ b/toxav/msi.c @@ -46,7 +46,6 @@ typedef enum { IDRequest = 1, IDError, IDCapabilities, - } MSIHeaderID; @@ -538,7 +537,6 @@ MSICall *new_call (MSISession *session, uint32_t friend_number) } session->calls_tail = session->calls_head = friend_number; - } else if (session->calls_tail < friend_number) { /* Appending */ void *tmp = realloc(session->calls, sizeof(MSICall *) * (friend_number + 1)); @@ -560,7 +558,6 @@ MSICall *new_call (MSISession *session, uint32_t friend_number) session->calls[session->calls_tail]->next = rc; session->calls_tail = friend_number; - } else if (session->calls_head > friend_number) { /* Inserting at front */ rc->next = session->calls[session->calls_head]; session->calls[session->calls_head]->prev = rc; @@ -736,7 +733,6 @@ void handle_push (MSICall *call, const MSIMessage *msg) if (invoke_callback(call, msi_OnStart) == -1) { goto FAILURE; } - } break; @@ -767,7 +763,6 @@ void handle_pop (MSICall *call, const MSIMessage *msg) LOGGER_WARNING(call->session->messenger->log, "Friend detected an error: %d", msg->error.value); call->error = msg->error.value; invoke_callback(call, msi_OnError); - } else { switch (call->state) { case msi_CallInactive: { @@ -812,10 +807,10 @@ void handle_msi_packet (Messenger *m, uint32_t friend_number, const uint8_t *dat LOGGER_WARNING(m->log, "Error parsing message"); send_error(m, friend_number, msi_EInvalidMessage); return; - } else { - LOGGER_DEBUG(m->log, "Successfully parsed message"); } + LOGGER_DEBUG(m->log, "Successfully parsed message"); + pthread_mutex_lock(session->mutex); MSICall *call = get_call(session, friend_number); diff --git a/toxav/rtp.c b/toxav/rtp.c index 1556d23dc2..38e64dd750 100644 --- a/toxav/rtp.c +++ b/toxav/rtp.c @@ -268,12 +268,12 @@ int handle_rtp_packet (Messenger *m, uint32_t friendnumber, const uint8_t *data, */ if (chloss(session, header)) { return 0; - } else { - /* Message is not late; pick up the latest parameters */ - session->rsequnum = ntohs(header->sequnum); - session->rtimestamp = ntohl(header->timestamp); } + /* Message is not late; pick up the latest parameters */ + session->rsequnum = ntohs(header->sequnum); + session->rtimestamp = ntohl(header->timestamp); + bwc_add_recv(session->bwc, length); /* Invoke processing of active multiparted message */ @@ -296,69 +296,43 @@ int handle_rtp_packet (Messenger *m, uint32_t friendnumber, const uint8_t *data, } return session->mcb (session->cs, new_message(length, data, length)); - } else { - /* The message is sent in multiple parts */ - - if (session->mp) { - /* There are 2 possible situations in this case: - * 1) being that we got the part of already processing message. - * 2) being that we got the part of a new/old message. - * - * We handle them differently as we only allow a single multiparted - * processing message - */ - - if (session->mp->header.sequnum == ntohs(header->sequnum) && - session->mp->header.timestamp == ntohl(header->timestamp)) { - /* First case */ - - /* Make sure we have enough allocated memory */ - if (session->mp->header.tlen - session->mp->len < length - sizeof(struct RTPHeader) || - session->mp->header.tlen <= ntohs(header->cpart)) { - /* There happened to be some corruption on the stream; - * continue wihtout this part - */ - return 0; - } - - memcpy(session->mp->data + ntohs(header->cpart), data + sizeof(struct RTPHeader), - length - sizeof(struct RTPHeader)); + } - session->mp->len += length - sizeof(struct RTPHeader); + /* The message is sent in multiple parts */ - bwc_add_recv(session->bwc, length); + if (session->mp) { + /* There are 2 possible situations in this case: + * 1) being that we got the part of already processing message. + * 2) being that we got the part of a new/old message. + * + * We handle them differently as we only allow a single multiparted + * processing message + */ - if (session->mp->len == session->mp->header.tlen) { - /* Received a full message; now push it for the further - * processing. - */ - if (session->mcb) { - session->mcb (session->cs, session->mp); - } else { - free(session->mp); - } + if (session->mp->header.sequnum == ntohs(header->sequnum) && + session->mp->header.timestamp == ntohl(header->timestamp)) { + /* First case */ - session->mp = NULL; - } - } else { - /* Second case */ + /* Make sure we have enough allocated memory */ + if (session->mp->header.tlen - session->mp->len < length - sizeof(struct RTPHeader) || + session->mp->header.tlen <= ntohs(header->cpart)) { + /* There happened to be some corruption on the stream; + * continue wihtout this part + */ + return 0; + } - if (session->mp->header.timestamp > ntohl(header->timestamp)) { - /* The received message part is from the old message; - * discard it. - */ - return 0; - } + memcpy(session->mp->data + ntohs(header->cpart), data + sizeof(struct RTPHeader), + length - sizeof(struct RTPHeader)); - /* Measure missing parts of the old message */ - bwc_add_lost(session->bwc, - (session->mp->header.tlen - session->mp->len) + + session->mp->len += length - sizeof(struct RTPHeader); - /* Must account sizes of rtp headers too */ - ((session->mp->header.tlen - session->mp->len) / - MAX_CRYPTO_DATA_SIZE) * sizeof(struct RTPHeader) ); + bwc_add_recv(session->bwc, length); - /* Push the previous message for processing */ + if (session->mp->len == session->mp->header.tlen) { + /* Received a full message; now push it for the further + * processing. + */ if (session->mcb) { session->mcb (session->cs, session->mp); } else { @@ -366,40 +340,66 @@ int handle_rtp_packet (Messenger *m, uint32_t friendnumber, const uint8_t *data, } session->mp = NULL; - goto NEW_MULTIPARTED; } } else { - /* In this case threat the message as if it was received in order - */ - - /* This is also a point for new multiparted messages */ -NEW_MULTIPARTED: + /* Second case */ - /* Only allow messages which have arrived in order; - * drop late messages - */ - if (chloss(session, header)) { + if (session->mp->header.timestamp > ntohl(header->timestamp)) { + /* The received message part is from the old message; + * discard it. + */ return 0; - } else { - /* Message is not late; pick up the latest parameters */ - session->rsequnum = ntohs(header->sequnum); - session->rtimestamp = ntohl(header->timestamp); } - bwc_add_recv(session->bwc, length); + /* Measure missing parts of the old message */ + bwc_add_lost(session->bwc, + (session->mp->header.tlen - session->mp->len) + + + /* Must account sizes of rtp headers too */ + ((session->mp->header.tlen - session->mp->len) / + MAX_CRYPTO_DATA_SIZE) * sizeof(struct RTPHeader) ); - /* Again, only store message if handler is present - */ + /* Push the previous message for processing */ if (session->mcb) { - session->mp = new_message(ntohs(header->tlen) + sizeof(struct RTPHeader), data, length); + session->mcb (session->cs, session->mp); + } else { + free(session->mp); + } - /* Reposition data if necessary */ - if (ntohs(header->cpart)) { - ; - } + session->mp = NULL; + goto NEW_MULTIPARTED; + } + } else { + /* In this case threat the message as if it was received in order + */ + + /* This is also a point for new multiparted messages */ +NEW_MULTIPARTED: + + /* Only allow messages which have arrived in order; + * drop late messages + */ + if (chloss(session, header)) { + return 0; + } + + /* Message is not late; pick up the latest parameters */ + session->rsequnum = ntohs(header->sequnum); + session->rtimestamp = ntohl(header->timestamp); - memmove(session->mp->data + ntohs(header->cpart), session->mp->data, session->mp->len); + bwc_add_recv(session->bwc, length); + + /* Again, only store message if handler is present + */ + if (session->mcb) { + session->mp = new_message(ntohs(header->tlen) + sizeof(struct RTPHeader), data, length); + + /* Reposition data if necessary */ + if (ntohs(header->cpart)) { + ; } + + memmove(session->mp->data + ntohs(header->cpart), session->mp->data, session->mp->len); } } diff --git a/toxav/toxav.c b/toxav/toxav.c index 126e29a77e..749519a270 100644 --- a/toxav/toxav.c +++ b/toxav/toxav.c @@ -1087,7 +1087,6 @@ ToxAVCall *call_new(ToxAV *av, uint32_t friend_number, TOXAV_ERR_CALL *error) } av->calls_tail = av->calls_head = friend_number; - } else if (av->calls_tail < friend_number) { /* Appending */ void *tmp = realloc(av->calls, sizeof(ToxAVCall *) * (friend_number + 1)); @@ -1111,7 +1110,6 @@ ToxAVCall *call_new(ToxAV *av, uint32_t friend_number, TOXAV_ERR_CALL *error) av->calls[av->calls_tail]->next = call; av->calls_tail = friend_number; - } else if (av->calls_head > friend_number) { /* Inserting at front */ call->next = av->calls[av->calls_head]; av->calls[av->calls_head]->prev = call; diff --git a/toxcore/DHT.c b/toxcore/DHT.c index c9fa99392f..2fd18c49ab 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -195,12 +195,14 @@ int to_host_family(IP *ip) if (ip->family == TOX_AF_INET) { ip->family = AF_INET; return 0; - } else if (ip->family == TOX_AF_INET6) { + } + + if (ip->family == TOX_AF_INET6) { ip->family = AF_INET6; return 0; - } else { - return -1; } + + return -1; } #define PACKED_NODE_SIZE_IP4 (1 + SIZE_IP4 + sizeof(uint16_t) + crypto_box_PUBLICKEYBYTES) @@ -213,15 +215,21 @@ int packed_node_size(uint8_t ip_family) { if (ip_family == AF_INET) { return PACKED_NODE_SIZE_IP4; - } else if (ip_family == TCP_INET) { + } + + if (ip_family == TCP_INET) { return PACKED_NODE_SIZE_IP4; - } else if (ip_family == AF_INET6) { + } + + if (ip_family == AF_INET6) { return PACKED_NODE_SIZE_IP6; - } else if (ip_family == TCP_INET6) { + } + + if (ip_family == TCP_INET6) { return PACKED_NODE_SIZE_IP6; - } else { - return -1; } + + return -1; } @@ -434,7 +442,9 @@ static int client_or_ip_port_in_list(Logger *log, Client_data *list, uint16_t le /* kill the other address, if it was set */ memset(&list[i].assoc6, 0, sizeof(list[i].assoc6)); return 1; - } else if ((ip_port.ip.family == AF_INET6) && ipport_equal(&list[i].assoc6.ip_port, &ip_port)) { + } + + if ((ip_port.ip.family == AF_INET6) && ipport_equal(&list[i].assoc6.ip_port, &ip_port)) { /* Initialize client timestamp. */ list[i].assoc6.timestamp = temp_time; memcpy(list[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); @@ -739,9 +749,9 @@ static unsigned int store_node_ok(const Client_data *client, const uint8_t *publ if ((is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) && is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) || (id_closest(comp_public_key, client->public_key, public_key) == 2)) { return 1; - } else { - return 0; } + + return 0; } static void sort_client_list(Client_data *list, unsigned int length, const uint8_t *comp_public_key) @@ -1677,9 +1687,9 @@ int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enable } return 1; - } else { - return 0; } + + return 0; } /* Send the given packet to node with public_key @@ -1696,11 +1706,13 @@ int route_packet(const DHT *dht, const uint8_t *public_key, const uint8_t *packe if (ip_isset(&client->assoc6.ip_port.ip)) { return sendpacket(dht->net, client->assoc6.ip_port, packet, length); - } else if (ip_isset(&client->assoc4.ip_port.ip)) { + } + + if (ip_isset(&client->assoc4.ip_port.ip)) { return sendpacket(dht->net, client->assoc4.ip_port, packet, length); - } else { - break; } + + break; } } @@ -1955,7 +1967,9 @@ static int handle_NATping(void *object, IP_Port source, const uint8_t *source_pu send_NATping(dht, source_pubkey, ping_id, NAT_PING_RESPONSE); friend->nat.recvNATping_timestamp = unix_time(); return 0; - } else if (packet[0] == NAT_PING_RESPONSE) { + } + + if (packet[0] == NAT_PING_RESPONSE) { if (friend->nat.NATping_id == ping_id) { friend->nat.NATping_id = random_64b(); friend->nat.hole_punching = 1; @@ -2186,7 +2200,9 @@ static IPPTsPng *get_closelist_IPPTsPng(DHT *dht, const uint8_t *public_key, sa_ if (sa_family == AF_INET) { return &dht->close_clientlist[i].assoc4; - } else if (sa_family == AF_INET6) { + } + + if (sa_family == AF_INET6) { return &dht->close_clientlist[i].assoc6; } } @@ -2319,9 +2335,9 @@ Node_format random_node(DHT *dht, sa_family_t sa_family) if (num_nodes == 0) { return nodes_list[0]; - } else { - return nodes_list[rand() % num_nodes]; } + + return nodes_list[rand() % num_nodes]; } /* Put up to max_num nodes in nodes from the closelist. @@ -2488,13 +2504,13 @@ static int cryptopacket_handle(void *object, IP_Port source, const uint8_t *pack return dht->cryptopackethandlers[number].function(dht->cryptopackethandlers[number].object, source, public_key, data, len); + } - } else { /* If request is not for us, try routing it. */ - int retval = route_packet(dht, packet + 1, packet, length); + /* If request is not for us, try routing it. */ + int retval = route_packet(dht, packet + 1, packet, length); - if ((unsigned int)retval == length) { - return 0; - } + if ((unsigned int)retval == length) { + return 0; } } @@ -2747,7 +2763,6 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le } else { dht->loaded_num_nodes = 0; } - } /* localize declarations */ break; @@ -2824,7 +2839,6 @@ int DHT_non_lan_connected(const DHT *dht) if (!is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT) && LAN_ip(client->assoc6.ip_port.ip) == -1) { return 1; } - } return 0; diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index ab28f6126f..4edc960a7c 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -299,7 +299,6 @@ int LAN_ip(IP ip) if ((ip4.uint8[0] == 100) && ((ip4.uint8[1] & 0xC0) == 0x40)) { return 0; } - } else if (ip.family == AF_INET6) { /* autogenerated for each interface: FE80::* (up to FEBF::*) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 81e17b7ba4..99281c7332 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -455,16 +455,16 @@ int m_get_friend_connectionstatus(const Messenger *m, int32_t friendnumber) if (direct_connected) { return CONNECTION_UDP; - } else { - if (num_online_relays) { - return CONNECTION_TCP; - } else { - return CONNECTION_UNKNOWN; - } } - } else { - return CONNECTION_NONE; + + if (num_online_relays) { + return CONNECTION_TCP; + } + + return CONNECTION_UNKNOWN; } + + return CONNECTION_NONE; } int m_friend_exists(const Messenger *m, int32_t friendnumber) @@ -906,9 +906,9 @@ static void check_friend_tcp_udp(Messenger *m, int32_t friendnumber) if (ret == CONNECTION_UNKNOWN) { if (last_connection_udp_tcp == CONNECTION_UDP) { return; - } else { - ret = CONNECTION_TCP; } + + ret = CONNECTION_TCP; } if (last_connection_udp_tcp != ret) { @@ -1276,9 +1276,9 @@ int file_control(const Messenger *m, int32_t friendnumber, uint32_t filenumber, if (!(ft->paused & FILE_PAUSE_US)) { if (ft->paused & FILE_PAUSE_OTHER) { return -6; - } else { - return -7; } + + return -7; } } else { if (ft->status != FILESTATUS_NOT_ACCEPTED) { @@ -1481,7 +1481,6 @@ int file_data(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uin } return -6; - } /* Give the number of bytes left to be sent/received. @@ -1504,14 +1503,14 @@ uint64_t file_dataremaining(const Messenger *m, int32_t friendnumber, uint8_t fi return m->friendlist[friendnumber].file_sending[filenumber].size - m->friendlist[friendnumber].file_sending[filenumber].transferred; - } else { - if (m->friendlist[friendnumber].file_receiving[filenumber].status == FILESTATUS_NONE) { - return 0; - } + } - return m->friendlist[friendnumber].file_receiving[filenumber].size - - m->friendlist[friendnumber].file_receiving[filenumber].transferred; + if (m->friendlist[friendnumber].file_receiving[filenumber].status == FILESTATUS_NONE) { + return 0; } + + return m->friendlist[friendnumber].file_receiving[filenumber].size - + m->friendlist[friendnumber].file_receiving[filenumber].transferred; } static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber) @@ -1593,7 +1592,6 @@ static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber) } --free_slots; - } if (num == 0) { @@ -1686,7 +1684,6 @@ static int handle_filecontrol(Messenger *m, int32_t friendnumber, uint8_t receiv if (receive_send) { --m->friendlist[friendnumber].num_sending_files; } - } else if (control_type == FILECONTROL_SEEK) { uint64_t position; @@ -1816,9 +1813,9 @@ int send_custom_lossy_packet(const Messenger *m, int32_t friendnumber, const uin if (send_lossy_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, m->friendlist[friendnumber].friendcon_id), data, length) == -1) { return -5; - } else { - return 0; } + + return 0; } static int handle_custom_lossless_packet(void *object, int friend_num, const uint8_t *packet, uint16_t length) @@ -1876,9 +1873,9 @@ int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const if (write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, m->friendlist[friendnumber].friendcon_id), data, length, 1) == -1) { return -5; - } else { - return 0; } + + return 0; } /* Function to filter out some friend requests*/ @@ -2462,9 +2459,9 @@ uint32_t messenger_run_interval(const Messenger *m) if (crypto_interval > MIN_RUN_INTERVAL) { return MIN_RUN_INTERVAL; - } else { - return crypto_interval; } + + return crypto_interval; } /* The main loop that needs to be run at least 20 times per second. */ @@ -2954,9 +2951,9 @@ int messenger_load(Messenger *m, const uint8_t *data, uint32_t length) if (!data32[0] && (data32[1] == MESSENGER_STATE_COOKIE_GLOBAL)) { return load_state(messenger_load_state_callback, m, data + cookie_len, length - cookie_len, MESSENGER_STATE_COOKIE_TYPE); - } else { - return -1; } + + return -1; } /* Return the number of friends in the instance m. diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 641760ae76..15e26107b3 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -202,7 +202,6 @@ static int proxy_socks5_read_connection_response(TCP_Client_Connection *TCP_conn if (data[0] == 5 && data[1] == 0) { return 1; } - } else { uint8_t data[4 + sizeof(IP6) + sizeof(uint16_t)]; int ret = read_TCP_packet(TCP_conn->sock, data, sizeof(data)); @@ -364,7 +363,6 @@ static void wipe_priority_list(TCP_Client_Connection *con) p = p->next; free(pp); } - } /* return 1 on success. @@ -828,9 +826,9 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, const uint8_t *data, u } return 0; - } else { - return -1; } + + return -1; } case TCP_PACKET_OOB_RECV: { diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index 786cb16430..922bf0df79 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -256,7 +256,9 @@ int send_packet_tcp_connection(TCP_Connections *tcp_c, int connections_number, c if (ret == 1) { return 0; - } else if (!limit_reached) { + } + + if (!limit_reached) { ret = 0; /* Send oob packets to all relays tied to the connection. */ @@ -280,12 +282,12 @@ int send_packet_tcp_connection(TCP_Connections *tcp_c, int connections_number, c if (ret >= 1) { return 0; - } else { - return -1; } - } else { + return -1; } + + return -1; } /* Return a random TCP connection number for use in send_tcp_onion_request. @@ -527,7 +529,7 @@ int set_tcp_connection_to_status(TCP_Connections *tcp_c, int connections_number, } if (status) { - /* Conection is unsleeping. */ + /* Connection is unsleeping. */ if (con_to->status != TCP_CONN_SLEEPING) { return -1; } @@ -551,32 +553,32 @@ int set_tcp_connection_to_status(TCP_Connections *tcp_c, int connections_number, con_to->status = TCP_CONN_VALID; return 0; - } else { - /* Conection is going to sleep. */ - if (con_to->status != TCP_CONN_VALID) { - return -1; - } + } - unsigned int i; + /* Connection is going to sleep. */ + if (con_to->status != TCP_CONN_VALID) { + return -1; + } - for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) { - if (con_to->connections[i].tcp_connection) { - unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1; - TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); + unsigned int i; - if (!tcp_con) { - continue; - } + for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) { + if (con_to->connections[i].tcp_connection) { + unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1; + TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); - if (con_to->connections[i].status == TCP_CONNECTIONS_STATUS_ONLINE) { - ++tcp_con->sleep_count; - } + if (!tcp_con) { + continue; } - } - con_to->status = TCP_CONN_SLEEPING; - return 0; + if (con_to->connections[i].status == TCP_CONNECTIONS_STATUS_ONLINE) { + ++tcp_con->sleep_count; + } + } } + + con_to->status = TCP_CONN_SLEEPING; + return 0; } static _Bool tcp_connection_in_conn(TCP_Connection_to *con_to, unsigned int tcp_connections_number) @@ -980,10 +982,10 @@ static int tcp_oob_callback(void *object, const uint8_t *public_key, const uint8 if (con_to && tcp_connection_in_conn(con_to, tcp_connections_number)) { return tcp_data_callback(object, connections_number, 0, data, length, userdata); - } else { - if (tcp_c->tcp_oob_callback) { - tcp_c->tcp_oob_callback(tcp_c->tcp_oob_callback_object, public_key, tcp_connections_number, data, length); - } + } + + if (tcp_c->tcp_oob_callback) { + tcp_c->tcp_oob_callback(tcp_c->tcp_oob_callback_object, public_key, tcp_connections_number, data, length); } return 0; @@ -1086,7 +1088,6 @@ static int add_tcp_relay_instance(TCP_Connections *tcp_c, IP_Port ip_port, const TCP_con *tcp_con = &tcp_c->tcp_connections[tcp_connections_number]; - tcp_con->connection = new_TCP_connection(ip_port, relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info); @@ -1174,25 +1175,25 @@ int add_tcp_relay_connection(TCP_Connections *tcp_c, int connections_number, IP_ if (tcp_connections_number != -1) { return add_tcp_number_relay_connection(tcp_c, connections_number, tcp_connections_number); - } else { - if (online_tcp_connection_from_conn(con_to) >= RECOMMENDED_FRIEND_TCP_CONNECTIONS) { - return -1; - } + } - int tcp_connections_number = add_tcp_relay_instance(tcp_c, ip_port, relay_pk); + if (online_tcp_connection_from_conn(con_to) >= RECOMMENDED_FRIEND_TCP_CONNECTIONS) { + return -1; + } - TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); + tcp_connections_number = add_tcp_relay_instance(tcp_c, ip_port, relay_pk); - if (!tcp_con) { - return -1; - } + TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); - if (add_tcp_connection_to_conn(con_to, tcp_connections_number) == -1) { - return -1; - } + if (!tcp_con) { + return -1; + } - return 0; + if (add_tcp_connection_to_conn(con_to, tcp_connections_number) == -1) { + return -1; } + + return 0; } /* return number of online tcp relays tied to the connection on success. @@ -1406,12 +1407,12 @@ static void kill_nonused_tcp(TCP_Connections *tcp_c) if (num_online <= RECOMMENDED_FRIEND_TCP_CONNECTIONS) { return; - } else { - unsigned int n = num_online - RECOMMENDED_FRIEND_TCP_CONNECTIONS; + } - if (n < num_kill) { - num_kill = n; - } + unsigned int n = num_online - RECOMMENDED_FRIEND_TCP_CONNECTIONS; + + if (n < num_kill) { + num_kill = n; } for (i = 0; i < num_kill; ++i) { @@ -1437,5 +1438,3 @@ void kill_tcp_connections(TCP_Connections *tcp_c) free(tcp_c->connections); free(tcp_c); } - - diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index d689e81739..81f884d8b8 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -325,7 +325,6 @@ static int send_pending_data_nonpriority(TCP_Secure_Connection *con) con->last_packet_sent += len; return -1; - } /* return 0 if pending data was sent completely @@ -620,9 +619,9 @@ static int handle_TCP_routing_req(TCP_Server *TCP_server, uint32_t con_id, const if (public_key_cmp(public_key, con->connections[i].public_key) == 0) { if (send_routing_response(con, i + NUM_RESERVED_PORTS, public_key) == -1) { return -1; - } else { - return 0; } + + return 0; } } else if (index == (uint32_t)~0) { index = i; @@ -737,9 +736,9 @@ static int rm_connection_index(TCP_Server *TCP_server, TCP_Secure_Connection *co con->connections[con_number].other_id = 0; con->connections[con_number].status = 0; return 0; - } else { - return -1; } + + return -1; } static int handle_onion_recv_1(void *object, IP_Port dest, const uint8_t *data, uint16_t length) @@ -830,9 +829,9 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint } return 0; - } else { - return -1; } + + return -1; } case TCP_PACKET_OOB_SEND: { @@ -1132,12 +1131,14 @@ static int do_unconfirmed(TCP_Server *TCP_server, uint32_t i) if (len == 0) { return -1; - } else if (len == -1) { + } + + if (len == -1) { kill_TCP_connection(conn); return -1; - } else { - return confirm_TCP_connection(TCP_server, conn, packet, len); } + + return confirm_TCP_connection(TCP_server, conn, packet, len); } static void do_confirmed_recv(TCP_Server *TCP_server, uint32_t i) diff --git a/toxcore/assoc.c b/toxcore/assoc.c index ced1f65a78..9060c3d4f9 100644 --- a/toxcore/assoc.c +++ b/toxcore/assoc.c @@ -272,11 +272,13 @@ static IP_Port *entry_heard_get(Client_entry *entry, const IP_Port *ipp) { if (ipp->ip.family == AF_INET) { return &entry->assoc_heard4; - } else if (ipp->ip.family == AF_INET6) { + } + + if (ipp->ip.family == AF_INET6) { return &entry->assoc_heard6; - } else { - return NULL; } + + return NULL; } /* store a "heard" entry @@ -629,13 +631,13 @@ uint8_t Assoc_add_entry(Assoc *assoc, const uint8_t *id, const IPPTs *ippts_send if (!candidates_search(assoc, id, hash, &cnd_entry)) { if (candidates_create_new(assoc, hash, id, used, ippts_send, ipp_recv)) { return 1; - } else { - return 0; } - } else { - candidates_update_assoc(assoc, cnd_entry, used, ippts_send, ipp_recv); - return 2; + + return 0; } + + candidates_update_assoc(assoc, cnd_entry, used, ippts_send, ipp_recv); + return 2; } /*****************************************************************************/ diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index 31a7d18786..0779fb20ca 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -269,9 +269,9 @@ static int tcp_relay_node_callback(void *object, uint32_t number, IP_Port ip_por if (friend_con->crypt_connection_id != -1) { return friend_add_tcp_relay(fr_c, number, ip_port, public_key); - } else { - return add_tcp_relay(fr_c->net_crypto, ip_port, public_key); } + + return add_tcp_relay(fr_c->net_crypto, ip_port, public_key); } static int friend_new_connection(Friend_Connections *fr_c, int friendcon_id); @@ -411,10 +411,14 @@ static int handle_packet(void *object, int number, uint8_t *data, uint16_t lengt } return 0; - } else if (data[0] == PACKET_ID_ALIVE) { + } + + if (data[0] == PACKET_ID_ALIVE) { friend_con->ping_lastrecv = unix_time(); return 0; - } else if (data[0] == PACKET_ID_SHARE_RELAYS) { + } + + if (data[0] == PACKET_ID_SHARE_RELAYS) { Node_format nodes[MAX_SHARED_RELAYS]; int n; @@ -795,16 +799,16 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3 if (friend_con->status == FRIENDCONN_STATUS_CONNECTED) { packet[0] = PACKET_ID_FRIEND_REQUESTS; return write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, packet, sizeof(packet), 0) != -1; - } else { - packet[0] = CRYPTO_PACKET_FRIEND_REQ; - int num = send_onion_data(fr_c->onion_c, friend_con->onion_friendnum, packet, sizeof(packet)); + } - if (num <= 0) { - return -1; - } + packet[0] = CRYPTO_PACKET_FRIEND_REQ; + int num = send_onion_data(fr_c->onion_c, friend_con->onion_friendnum, packet, sizeof(packet)); - return num; + if (num <= 0) { + return -1; } + + return num; } /* Create new friend_connections instance. */ @@ -867,7 +871,6 @@ void do_friend_connections(Friend_Connections *fr_c) connect_to_saved_tcp_relays(fr_c, i, (MAX_FRIEND_TCP_CONNECTIONS / 2)); /* Only fill it half up. */ } } - } else if (friend_con->status == FRIENDCONN_STATUS_CONNECTED) { if (friend_con->ping_lastsent + FRIEND_PING_INTERVAL < temp_time) { send_ping(fr_c, i); diff --git a/toxcore/group.c b/toxcore/group.c index 6d5c6ad44e..db114529f1 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -320,7 +320,6 @@ static unsigned int pk_in_closest_peers(Group_c *g, uint8_t *real_pk) if (public_key_cmp(g->closest_peers[i].real_pk, real_pk) == 0) { return 1; } - } return 0; @@ -983,10 +982,10 @@ int invite_friend(Group_Chats *g_c, int32_t friendnumber, int groupnumber) if (send_group_invite_packet(g_c->m, friendnumber, invite, sizeof(invite))) { return 0; - } else { - wipe_group_chat(g_c, groupnumber); - return -1; } + + wipe_group_chat(g_c, groupnumber); + return -1; } static unsigned int send_peer_query(Group_Chats *g_c, int friendcon_id, uint16_t group_num); @@ -1051,10 +1050,10 @@ int join_groupchat(Group_Chats *g_c, int32_t friendnumber, uint8_t expected_type send_peer_query(g_c, friendcon_id, other_groupnum); return groupnumber; - } else { - g->status = GROUPCHAT_STATUS_NONE; - return -1; } + + g->status = GROUPCHAT_STATUS_NONE; + return -1; } /* Set the callback for group invites. @@ -1193,9 +1192,9 @@ int group_ping_send(const Group_Chats *g_c, int groupnumber) { if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_PING_ID, 0, 0)) { return 0; - } else { - return -1; } + + return -1; } #define GROUP_MESSAGE_NEW_PEER_ID 16 @@ -1216,9 +1215,9 @@ int group_new_peer_send(const Group_Chats *g_c, int groupnumber, uint16_t peer_n if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_NEW_PEER_ID, packet, sizeof(packet))) { return 0; - } else { - return -1; } + + return -1; } #define GROUP_MESSAGE_KILL_PEER_ID 17 @@ -1237,9 +1236,9 @@ static int group_kill_peer_send(const Group_Chats *g_c, int groupnumber, uint16_ if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_KILL_PEER_ID, packet, sizeof(packet))) { return 0; - } else { - return -1; } + + return -1; } #define GROUP_MESSAGE_NAME_ID 48 @@ -1256,9 +1255,9 @@ static int group_name_send(const Group_Chats *g_c, int groupnumber, const uint8_ if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_NAME_ID, nick, nick_len)) { return 0; - } else { - return -1; } + + return -1; } #define GROUP_MESSAGE_TITLE_ID 49 @@ -1293,9 +1292,9 @@ int group_title_send(const Group_Chats *g_c, int groupnumber, const uint8_t *tit if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_TITLE_ID, title, title_len)) { return 0; - } else { - return -1; } + + return -1; } /* Get group title from groupnumber and put it in title. @@ -1878,9 +1877,9 @@ int group_message_send(const Group_Chats *g_c, int groupnumber, const uint8_t *m { if (send_message_group(g_c, groupnumber, PACKET_ID_MESSAGE, message, length)) { return 0; - } else { - return -1; } + + return -1; } /* send a group action @@ -1891,9 +1890,9 @@ int group_action_send(const Group_Chats *g_c, int groupnumber, const uint8_t *ac { if (send_message_group(g_c, groupnumber, PACKET_ID_ACTION, action, length)) { return 0; - } else { - return -1; } + + return -1; } /* High level function to send custom lossy packets. diff --git a/toxcore/list.c b/toxcore/list.c index b97727f8f5..305e563c63 100644 --- a/toxcore/list.c +++ b/toxcore/list.c @@ -119,18 +119,18 @@ static int resize(BS_LIST *list, uint32_t new_size) if (!p) { return 0; - } else { - list->data = p; } + list->data = p; + p = realloc(list->ids, sizeof(int) * new_size); if (!p) { return 0; - } else { - list->ids = p; } + list->ids = p; + return 1; } diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 63043494b7..e2d7e4942b 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -482,13 +482,17 @@ IP_Port return_ip_port_connection(Net_Crypto *c, int crypt_connection_id) if (v4 && LAN_ip(conn->ip_portv4.ip) == 0) { return conn->ip_portv4; - } else if (v6 && conn->ip_portv6.ip.family == AF_INET6) { + } + + if (v6 && conn->ip_portv6.ip.family == AF_INET6) { return conn->ip_portv6; - } else if (conn->ip_portv4.ip.family == AF_INET) { + } + + if (conn->ip_portv4.ip.family == AF_INET) { return conn->ip_portv4; - } else { - return empty; } + + return empty; } /* Sends a packet to the peer using the fastest route. @@ -519,10 +523,10 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t if ((uint32_t)sendpacket(c->dht->net, ip_port, data, length) == length) { pthread_mutex_unlock(&conn->mutex); return 0; - } else { - pthread_mutex_unlock(&conn->mutex); - return -1; } + + pthread_mutex_unlock(&conn->mutex); + return -1; } //TODO: a better way of sending packets directly to confirm the others ip. @@ -775,7 +779,6 @@ static int generate_request_packet(uint8_t *data, uint16_t length, const Packets if (length <= cur_len) { return cur_len; } - } else if (n == 255) { data[cur_len] = 0; n = 0; @@ -1135,7 +1138,9 @@ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint32 if (ret == -1) { return -1; - } else if (ret == 0) { + } + + if (ret == 0) { continue; } @@ -1391,10 +1396,10 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con if (requested == -1) { return -1; - } else { - //TODO? } + // else { /* TODO? */ } + set_buffer_end(&conn->recv_array, num); } else if (real_data[0] >= CRYPTO_RESERVED_PACKETS && real_data[0] < PACKET_ID_LOSSY_RANGE_START) { Packet_Data dt; @@ -1405,7 +1410,6 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con return -1; } - while (1) { pthread_mutex_lock(&conn->mutex); int ret = read_data_beg_buffer(&conn->recv_array, &dt); @@ -1439,7 +1443,6 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con conn->connection_lossy_data_callback(conn->connection_lossy_data_callback_object, conn->connection_lossy_data_callback_id, real_data, real_length); } - } else { return -1; } @@ -1525,7 +1528,6 @@ static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, cons conn->dht_pk_callback(conn->dht_pk_callback_object, conn->dht_pk_callback_number, dht_public_key); } } - } else { return -1; } @@ -1536,11 +1538,9 @@ static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, cons case NET_PACKET_CRYPTO_DATA: { if (conn->status == CRYPTO_CONN_NOT_CONFIRMED || conn->status == CRYPTO_CONN_ESTABLISHED) { return handle_data_packet_helper(c, crypt_connection_id, packet, length, udp, userdata); - } else { - return -1; } - return 0; + return -1; } default: { @@ -1698,7 +1698,9 @@ static int crypto_connection_add_source(Net_Crypto *c, int crypt_connection_id, } return 0; - } else if (source.ip.family == TCP_FAMILY) { + } + + if (source.ip.family == TCP_FAMILY) { if (add_tcp_number_relay_connection(c->tcp_c, conn->connection_number_tcp, source.ip.ip6.uint32[0]) == 0) { return 1; } @@ -1974,7 +1976,9 @@ static int tcp_oob_callback(void *object, const uint8_t *public_key, unsigned in if (data[0] == NET_PACKET_COOKIE_REQUEST) { return tcp_oob_handle_cookie_request(c, tcp_connections_number, public_key, data, length); - } else if (data[0] == NET_PACKET_CRYPTO_HS) { + } + + if (data[0] == NET_PACKET_CRYPTO_HS) { IP_Port source; source.port = 0; source.ip.family = TCP_FAMILY; @@ -1985,9 +1989,9 @@ static int tcp_oob_callback(void *object, const uint8_t *public_key, unsigned in } return 0; - } else { - return -1; } + + return -1; } /* Add a tcp relay, associating it to a crypt_connection_id. @@ -2305,7 +2309,6 @@ static void send_crypto_packets(Net_Crypto *c) if (send_request_packet(c, i) == 0) { conn->last_request_packet_sent = temp_time; } - } if (conn->status == CRYPTO_CONN_ESTABLISHED) { @@ -2433,7 +2436,6 @@ static void send_crypto_packets(Net_Crypto *c) conn->packet_send_rate_requested = conn->packet_send_rate; } } - } if (conn->last_packets_left_set == 0 || conn->last_packets_left_requested_set == 0) { @@ -2541,9 +2543,9 @@ uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connecti if (conn->packets_left < max_packets) { return conn->packets_left; - } else { - return max_packets; } + + return max_packets; } /* Sends a lossless cryptopacket. @@ -2617,9 +2619,9 @@ int cryptpacket_received(Net_Crypto *c, int crypt_connection_id, uint32_t packet if (num < num1) { return 0; - } else { - return -1; } + + return -1; } /* return -1 on failure. diff --git a/toxcore/network.c b/toxcore/network.c index fb491f4b30..05867adcfc 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -753,11 +753,13 @@ int ip_equal(const IP *a, const IP *b) if (a->family == b->family) { if (a->family == AF_INET) { return (a->ip4.in_addr.s_addr == b->ip4.in_addr.s_addr); - } else if (a->family == AF_INET6) { + } + + if (a->family == AF_INET6) { return a->ip6.uint64[0] == b->ip6.uint64[0] && a->ip6.uint64[1] == b->ip6.uint64[1]; - } else { - return 0; } + + return 0; } /* different family: check on the IPv6 one if it is the IPv4 one embedded */ @@ -921,7 +923,9 @@ int ip_parse_addr(const IP *ip, char *address, size_t length) if (ip->family == AF_INET) { struct in_addr *addr = (struct in_addr *)&ip->ip4; return inet_ntop(ip->family, addr, address, length) != NULL; - } else if (ip->family == AF_INET6) { + } + + if (ip->family == AF_INET6) { struct in6_addr *addr = (struct in6_addr *)&ip->ip6; return inet_ntop(ip->family, addr, address, length) != NULL; } diff --git a/toxcore/onion.c b/toxcore/onion.c index d351278000..0f0abf2654 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c @@ -77,10 +77,10 @@ static int ip_unpack(IP *target, const uint8_t *data, unsigned int data_size, _B if (!disable_family_check) { return to_host_family(target); - } else { - to_host_family(target); - return 0; } + + to_host_family(target); + return 0; } static void ipport_pack(uint8_t *data, const IP_Port *source) diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 9d57324023..851a0ee1c0 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -341,7 +341,9 @@ static int send_onion_packet_tcp_udp(const Onion_Client *onion_c, const Onion_Pa } return 0; - } else if (path->ip_port1.ip.family == TCP_FAMILY) { + } + + if (path->ip_port1.ip.family == TCP_FAMILY) { uint8_t packet[ONION_MAX_PACKET_SIZE]; int len = create_onion_packet_tcp(packet, sizeof(packet), path, dest, data, length); @@ -350,9 +352,9 @@ static int send_onion_packet_tcp_udp(const Onion_Client *onion_c, const Onion_Pa } return send_tcp_onion_request(onion_c->c, path->ip_port1.ip.ip4.uint32, packet, len); - } else { - return -1; } + + return -1; } /* Creates a sendback for use in an announce request. @@ -453,7 +455,6 @@ static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, IP_ if (num == 0) { len = create_announce_request(request, sizeof(request), dest_pubkey, onion_c->c->self_public_key, onion_c->c->self_secret_key, ping_id, onion_c->c->self_public_key, onion_c->temp_public_key, sendback); - } else { len = create_announce_request(request, sizeof(request), dest_pubkey, onion_c->friends_list[num - 1].temp_public_key, onion_c->friends_list[num - 1].temp_secret_key, ping_id, onion_c->friends_list[num - 1].real_public_key, zero_ping_id, @@ -520,7 +521,6 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t if (is_stored == 1 && public_key_cmp(pingid_or_key, onion_c->temp_public_key) != 0) { is_stored = 0; } - } else { if (is_stored >= 2) { return -1; @@ -841,7 +841,9 @@ static int handle_tcp_onion(void *object, const uint8_t *data, uint16_t length, if (data[0] == NET_PACKET_ANNOUNCE_RESPONSE) { return handle_announce_response(object, ip_port, data, length, userdata); - } else if (data[0] == NET_PACKET_ONION_DATA_RESPONSE) { + } + + if (data[0] == NET_PACKET_ONION_DATA_RESPONSE) { return handle_data_response(object, ip_port, data, length, userdata); } @@ -1418,7 +1420,6 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum) onion_c->friends_list[friendnum].last_dht_pk_dht_sent = unix_time(); } } - } } @@ -1541,9 +1542,9 @@ unsigned int onion_connection_status(const Onion_Client *onion_c) if (onion_c->onion_connected >= ONION_CONNECTION_SECONDS) { if (onion_c->UDP_connected) { return 2; - } else { - return 1; } + + return 1; } return 0; @@ -1566,7 +1567,6 @@ void do_onion_client(Onion_Client *onion_c) if (onion_c->onion_connected < ONION_CONNECTION_SECONDS * 2) { ++onion_c->onion_connected; } - } else { populate_path_nodes_tcp(onion_c); diff --git a/toxcore/tox.c b/toxcore/tox.c index 94cb7261e8..40bfaf702a 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -379,10 +379,10 @@ bool tox_bootstrap(Tox *tox, const char *address, uint16_t port, const uint8_t * if (count) { SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); return 1; - } else { - SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); - return 0; } + + SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); + return 0; } bool tox_add_tcp_relay(Tox *tox, const char *address, uint16_t port, const uint8_t *public_key, @@ -436,10 +436,10 @@ bool tox_add_tcp_relay(Tox *tox, const char *address, uint16_t port, const uint8 if (count) { SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); return 1; - } else { - SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); - return 0; } + + SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); + return 0; } TOX_CONNECTION tox_self_get_connection_status(const Tox *tox) @@ -450,11 +450,13 @@ TOX_CONNECTION tox_self_get_connection_status(const Tox *tox) if (ret == 2) { return TOX_CONNECTION_UDP; - } else if (ret == 1) { + } + + if (ret == 1) { return TOX_CONNECTION_TCP; - } else { - return TOX_CONNECTION_NONE; } + + return TOX_CONNECTION_NONE; } @@ -529,10 +531,10 @@ bool tox_self_set_name(Tox *tox, const uint8_t *name, size_t length, TOX_ERR_SET send_name_all_groups(m->group_chat_object); SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_OK); return 1; - } else { - SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_TOO_LONG); - return 0; } + + SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_TOO_LONG); + return 0; } size_t tox_self_get_name_size(const Tox *tox) @@ -561,10 +563,10 @@ bool tox_self_set_status_message(Tox *tox, const uint8_t *status_message, size_t if (m_set_statusmessage(m, status_message, length) == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_OK); return 1; - } else { - SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_TOO_LONG); - return 0; } + + SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_TOO_LONG); + return 0; } size_t tox_self_get_status_message_size(const Tox *tox) @@ -623,7 +625,6 @@ static void set_friend_error(int32_t ret, TOX_ERR_FRIEND_ADD *error) case FAERR_NOMEM: SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_ADD_MALLOC); break; - } } @@ -1095,7 +1096,9 @@ bool tox_file_get_file_id(const Tox *tox, uint32_t friend_number, uint32_t file_ if (ret == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_GET_OK); return 1; - } else if (ret == -1) { + } + + if (ret == -1) { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_GET_FRIEND_NOT_FOUND); } else { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_GET_NOT_FOUND); @@ -1268,9 +1271,9 @@ bool tox_friend_send_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_ if (ret == 0) { return 1; - } else { - return 0; } + + return 0; } void tox_callback_friend_lossy_packet(Tox *tox, tox_friend_lossy_packet_cb *callback, void *user_data) @@ -1300,9 +1303,9 @@ bool tox_friend_send_lossless_packet(Tox *tox, uint32_t friend_number, const uin if (ret == 0) { return 1; - } else { - return 0; } + + return 0; } void tox_callback_friend_lossless_packet(Tox *tox, tox_friend_lossless_packet_cb *callback, void *user_data) @@ -1340,8 +1343,8 @@ uint16_t tox_self_get_tcp_port(const Tox *tox, TOX_ERR_GET_PORT *error) if (m->tcp_server) { SET_ERROR_PARAMETER(error, TOX_ERR_GET_PORT_OK); return m->options.tcp_server_port; - } else { - SET_ERROR_PARAMETER(error, TOX_ERR_GET_PORT_NOT_BOUND); - return 0; } + + SET_ERROR_PARAMETER(error, TOX_ERR_GET_PORT_NOT_BOUND); + return 0; } diff --git a/toxencryptsave/toxencryptsave.c b/toxencryptsave/toxencryptsave.c index bcbe9518af..69e5c0f37a 100644 --- a/toxencryptsave/toxencryptsave.c +++ b/toxencryptsave/toxencryptsave.c @@ -315,7 +315,7 @@ bool tox_is_data_encrypted(const uint8_t *data) { if (memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0) { return 1; - } else { - return 0; } + + return 0; }