summaryrefslogtreecommitdiff
path: root/toxcore/DHT.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2016-08-31 20:40:20 +0100
committeriphydf <iphydf@users.noreply.github.com>2016-09-02 11:02:56 +0100
commita9fbdaf46b23db5c598bf33d6bc5c4555b06e674 (patch)
treee7894501bd010d9904fe0069fc1b8121d2da4040 /toxcore/DHT.c
parent6f42eadc54e81be50b7a817c72b0cf4d7ec5feb4 (diff)
Do not use `else` after `return`.
http://llvm.org/docs/CodingStandards.html#use-early-exits-and-continue-to-simplify-code
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r--toxcore/DHT.c68
1 files changed, 41 insertions, 27 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index c9fa9939..2fd18c49 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -195,12 +195,14 @@ int to_host_family(IP *ip)
195 if (ip->family == TOX_AF_INET) { 195 if (ip->family == TOX_AF_INET) {
196 ip->family = AF_INET; 196 ip->family = AF_INET;
197 return 0; 197 return 0;
198 } else if (ip->family == TOX_AF_INET6) { 198 }
199
200 if (ip->family == TOX_AF_INET6) {
199 ip->family = AF_INET6; 201 ip->family = AF_INET6;
200 return 0; 202 return 0;
201 } else {
202 return -1;
203 } 203 }
204
205 return -1;
204} 206}
205 207
206#define PACKED_NODE_SIZE_IP4 (1 + SIZE_IP4 + sizeof(uint16_t) + crypto_box_PUBLICKEYBYTES) 208#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)
213{ 215{
214 if (ip_family == AF_INET) { 216 if (ip_family == AF_INET) {
215 return PACKED_NODE_SIZE_IP4; 217 return PACKED_NODE_SIZE_IP4;
216 } else if (ip_family == TCP_INET) { 218 }
219
220 if (ip_family == TCP_INET) {
217 return PACKED_NODE_SIZE_IP4; 221 return PACKED_NODE_SIZE_IP4;
218 } else if (ip_family == AF_INET6) { 222 }
223
224 if (ip_family == AF_INET6) {
219 return PACKED_NODE_SIZE_IP6; 225 return PACKED_NODE_SIZE_IP6;
220 } else if (ip_family == TCP_INET6) { 226 }
227
228 if (ip_family == TCP_INET6) {
221 return PACKED_NODE_SIZE_IP6; 229 return PACKED_NODE_SIZE_IP6;
222 } else {
223 return -1;
224 } 230 }
231
232 return -1;
225} 233}
226 234
227 235
@@ -434,7 +442,9 @@ static int client_or_ip_port_in_list(Logger *log, Client_data *list, uint16_t le
434 /* kill the other address, if it was set */ 442 /* kill the other address, if it was set */
435 memset(&list[i].assoc6, 0, sizeof(list[i].assoc6)); 443 memset(&list[i].assoc6, 0, sizeof(list[i].assoc6));
436 return 1; 444 return 1;
437 } else if ((ip_port.ip.family == AF_INET6) && ipport_equal(&list[i].assoc6.ip_port, &ip_port)) { 445 }
446
447 if ((ip_port.ip.family == AF_INET6) && ipport_equal(&list[i].assoc6.ip_port, &ip_port)) {
438 /* Initialize client timestamp. */ 448 /* Initialize client timestamp. */
439 list[i].assoc6.timestamp = temp_time; 449 list[i].assoc6.timestamp = temp_time;
440 memcpy(list[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); 450 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
739 if ((is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) && is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) 749 if ((is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) && is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT))
740 || (id_closest(comp_public_key, client->public_key, public_key) == 2)) { 750 || (id_closest(comp_public_key, client->public_key, public_key) == 2)) {
741 return 1; 751 return 1;
742 } else {
743 return 0;
744 } 752 }
753
754 return 0;
745} 755}
746 756
747static void sort_client_list(Client_data *list, unsigned int length, const uint8_t *comp_public_key) 757static 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
1677 } 1687 }
1678 1688
1679 return 1; 1689 return 1;
1680 } else {
1681 return 0;
1682 } 1690 }
1691
1692 return 0;
1683} 1693}
1684 1694
1685/* Send the given packet to node with public_key 1695/* 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
1696 1706
1697 if (ip_isset(&client->assoc6.ip_port.ip)) { 1707 if (ip_isset(&client->assoc6.ip_port.ip)) {
1698 return sendpacket(dht->net, client->assoc6.ip_port, packet, length); 1708 return sendpacket(dht->net, client->assoc6.ip_port, packet, length);
1699 } else if (ip_isset(&client->assoc4.ip_port.ip)) { 1709 }
1710
1711 if (ip_isset(&client->assoc4.ip_port.ip)) {
1700 return sendpacket(dht->net, client->assoc4.ip_port, packet, length); 1712 return sendpacket(dht->net, client->assoc4.ip_port, packet, length);
1701 } else {
1702 break;
1703 } 1713 }
1714
1715 break;
1704 } 1716 }
1705 } 1717 }
1706 1718
@@ -1955,7 +1967,9 @@ static int handle_NATping(void *object, IP_Port source, const uint8_t *source_pu
1955 send_NATping(dht, source_pubkey, ping_id, NAT_PING_RESPONSE); 1967 send_NATping(dht, source_pubkey, ping_id, NAT_PING_RESPONSE);
1956 friend->nat.recvNATping_timestamp = unix_time(); 1968 friend->nat.recvNATping_timestamp = unix_time();
1957 return 0; 1969 return 0;
1958 } else if (packet[0] == NAT_PING_RESPONSE) { 1970 }
1971
1972 if (packet[0] == NAT_PING_RESPONSE) {
1959 if (friend->nat.NATping_id == ping_id) { 1973 if (friend->nat.NATping_id == ping_id) {
1960 friend->nat.NATping_id = random_64b(); 1974 friend->nat.NATping_id = random_64b();
1961 friend->nat.hole_punching = 1; 1975 friend->nat.hole_punching = 1;
@@ -2186,7 +2200,9 @@ static IPPTsPng *get_closelist_IPPTsPng(DHT *dht, const uint8_t *public_key, sa_
2186 2200
2187 if (sa_family == AF_INET) { 2201 if (sa_family == AF_INET) {
2188 return &dht->close_clientlist[i].assoc4; 2202 return &dht->close_clientlist[i].assoc4;
2189 } else if (sa_family == AF_INET6) { 2203 }
2204
2205 if (sa_family == AF_INET6) {
2190 return &dht->close_clientlist[i].assoc6; 2206 return &dht->close_clientlist[i].assoc6;
2191 } 2207 }
2192 } 2208 }
@@ -2319,9 +2335,9 @@ Node_format random_node(DHT *dht, sa_family_t sa_family)
2319 2335
2320 if (num_nodes == 0) { 2336 if (num_nodes == 0) {
2321 return nodes_list[0]; 2337 return nodes_list[0];
2322 } else {
2323 return nodes_list[rand() % num_nodes];
2324 } 2338 }
2339
2340 return nodes_list[rand() % num_nodes];
2325} 2341}
2326 2342
2327/* Put up to max_num nodes in nodes from the closelist. 2343/* 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
2488 2504
2489 return dht->cryptopackethandlers[number].function(dht->cryptopackethandlers[number].object, source, public_key, 2505 return dht->cryptopackethandlers[number].function(dht->cryptopackethandlers[number].object, source, public_key,
2490 data, len); 2506 data, len);
2507 }
2491 2508
2492 } else { /* If request is not for us, try routing it. */ 2509 /* If request is not for us, try routing it. */
2493 int retval = route_packet(dht, packet + 1, packet, length); 2510 int retval = route_packet(dht, packet + 1, packet, length);
2494 2511
2495 if ((unsigned int)retval == length) { 2512 if ((unsigned int)retval == length) {
2496 return 0; 2513 return 0;
2497 }
2498 } 2514 }
2499 } 2515 }
2500 2516
@@ -2747,7 +2763,6 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le
2747 } else { 2763 } else {
2748 dht->loaded_num_nodes = 0; 2764 dht->loaded_num_nodes = 0;
2749 } 2765 }
2750
2751 } /* localize declarations */ 2766 } /* localize declarations */
2752 2767
2753 break; 2768 break;
@@ -2824,7 +2839,6 @@ int DHT_non_lan_connected(const DHT *dht)
2824 if (!is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT) && LAN_ip(client->assoc6.ip_port.ip) == -1) { 2839 if (!is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT) && LAN_ip(client->assoc6.ip_port.ip) == -1) {
2825 return 1; 2840 return 1;
2826 } 2841 }
2827
2828 } 2842 }
2829 2843
2830 return 0; 2844 return 0;