diff options
author | iphydf <iphydf@users.noreply.github.com> | 2017-01-05 16:47:48 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2017-01-06 13:53:49 +0000 |
commit | e4d81e1520f34644fc25ddbe3c869c2ee7766f47 (patch) | |
tree | 29e9fce14bcc3995075e0f394f3ff7dd30a5d022 | |
parent | 8ef1f35ca71e39d40bdae88243534eebc6fa6a26 (diff) |
Eliminate dead return statement.
Previously, the `ipv6` variable was initialised to `-1`, but that value
was never read. Either it was set to 0 or 1, or the function would return
before it was read. Thus, I've changed it to uninitialised `bool is_ipv4`
(inverted semantics to avoid negative conditions `if (!is_ipv6)`).
The `pack_ip_port` function is a bit unfortunate, but I'm not rewriting
it until we have a binary writer (I'd be rewriting it twice).
-rw-r--r-- | toxcore/DHT.c | 36 |
1 files changed, 14 insertions, 22 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 23faf57b..abe7b1ed 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -316,27 +316,27 @@ static int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port) | |||
316 | return -1; | 316 | return -1; |
317 | } | 317 | } |
318 | 318 | ||
319 | int ipv6 = -1; | 319 | bool is_ipv4; |
320 | uint8_t net_family; | 320 | uint8_t net_family; |
321 | 321 | ||
322 | if (ip_port->ip.family == AF_INET) { | 322 | if (ip_port->ip.family == AF_INET) { |
323 | // TODO(irungentoo): use functions to convert endianness | 323 | // TODO(irungentoo): use functions to convert endianness |
324 | ipv6 = 0; | 324 | is_ipv4 = true; |
325 | net_family = TOX_AF_INET; | 325 | net_family = TOX_AF_INET; |
326 | } else if (ip_port->ip.family == TCP_INET) { | 326 | } else if (ip_port->ip.family == TCP_INET) { |
327 | ipv6 = 0; | 327 | is_ipv4 = true; |
328 | net_family = TOX_TCP_INET; | 328 | net_family = TOX_TCP_INET; |
329 | } else if (ip_port->ip.family == AF_INET6) { | 329 | } else if (ip_port->ip.family == AF_INET6) { |
330 | ipv6 = 1; | 330 | is_ipv4 = false; |
331 | net_family = TOX_AF_INET6; | 331 | net_family = TOX_AF_INET6; |
332 | } else if (ip_port->ip.family == TCP_INET6) { | 332 | } else if (ip_port->ip.family == TCP_INET6) { |
333 | ipv6 = 1; | 333 | is_ipv4 = false; |
334 | net_family = TOX_TCP_INET6; | 334 | net_family = TOX_TCP_INET6; |
335 | } else { | 335 | } else { |
336 | return -1; | 336 | return -1; |
337 | } | 337 | } |
338 | 338 | ||
339 | if (ipv6 == 0) { | 339 | if (is_ipv4) { |
340 | uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t); | 340 | uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t); |
341 | 341 | ||
342 | if (size > length) { | 342 | if (size > length) { |
@@ -347,9 +347,7 @@ static int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port) | |||
347 | memcpy(data + 1, &ip_port->ip.ip4, SIZE_IP4); | 347 | memcpy(data + 1, &ip_port->ip.ip4, SIZE_IP4); |
348 | memcpy(data + 1 + SIZE_IP4, &ip_port->port, sizeof(uint16_t)); | 348 | memcpy(data + 1 + SIZE_IP4, &ip_port->port, sizeof(uint16_t)); |
349 | return size; | 349 | return size; |
350 | } | 350 | } else { |
351 | |||
352 | if (ipv6 == 1) { | ||
353 | uint32_t size = 1 + SIZE_IP6 + sizeof(uint16_t); | 351 | uint32_t size = 1 + SIZE_IP6 + sizeof(uint16_t); |
354 | 352 | ||
355 | if (size > length) { | 353 | if (size > length) { |
@@ -361,8 +359,6 @@ static int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port) | |||
361 | memcpy(data + 1 + SIZE_IP6, &ip_port->port, sizeof(uint16_t)); | 359 | memcpy(data + 1 + SIZE_IP6, &ip_port->port, sizeof(uint16_t)); |
362 | return size; | 360 | return size; |
363 | } | 361 | } |
364 | |||
365 | return -1; | ||
366 | } | 362 | } |
367 | 363 | ||
368 | static int DHT_create_packet(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], | 364 | static int DHT_create_packet(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], |
@@ -399,34 +395,34 @@ static int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length | |||
399 | return -1; | 395 | return -1; |
400 | } | 396 | } |
401 | 397 | ||
402 | int ipv6 = -1; | 398 | bool is_ipv4; |
403 | uint8_t host_family; | 399 | uint8_t host_family; |
404 | 400 | ||
405 | if (data[0] == TOX_AF_INET) { | 401 | if (data[0] == TOX_AF_INET) { |
406 | ipv6 = 0; | 402 | is_ipv4 = true; |
407 | host_family = AF_INET; | 403 | host_family = AF_INET; |
408 | } else if (data[0] == TOX_TCP_INET) { | 404 | } else if (data[0] == TOX_TCP_INET) { |
409 | if (!tcp_enabled) { | 405 | if (!tcp_enabled) { |
410 | return -1; | 406 | return -1; |
411 | } | 407 | } |
412 | 408 | ||
413 | ipv6 = 0; | 409 | is_ipv4 = true; |
414 | host_family = TCP_INET; | 410 | host_family = TCP_INET; |
415 | } else if (data[0] == TOX_AF_INET6) { | 411 | } else if (data[0] == TOX_AF_INET6) { |
416 | ipv6 = 1; | 412 | is_ipv4 = false; |
417 | host_family = AF_INET6; | 413 | host_family = AF_INET6; |
418 | } else if (data[0] == TOX_TCP_INET6) { | 414 | } else if (data[0] == TOX_TCP_INET6) { |
419 | if (!tcp_enabled) { | 415 | if (!tcp_enabled) { |
420 | return -1; | 416 | return -1; |
421 | } | 417 | } |
422 | 418 | ||
423 | ipv6 = 1; | 419 | is_ipv4 = false; |
424 | host_family = TCP_INET6; | 420 | host_family = TCP_INET6; |
425 | } else { | 421 | } else { |
426 | return -1; | 422 | return -1; |
427 | } | 423 | } |
428 | 424 | ||
429 | if (ipv6 == 0) { | 425 | if (is_ipv4) { |
430 | uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t); | 426 | uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t); |
431 | 427 | ||
432 | if (size > length) { | 428 | if (size > length) { |
@@ -437,9 +433,7 @@ static int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length | |||
437 | memcpy(&ip_port->ip.ip4, data + 1, SIZE_IP4); | 433 | memcpy(&ip_port->ip.ip4, data + 1, SIZE_IP4); |
438 | memcpy(&ip_port->port, data + 1 + SIZE_IP4, sizeof(uint16_t)); | 434 | memcpy(&ip_port->port, data + 1 + SIZE_IP4, sizeof(uint16_t)); |
439 | return size; | 435 | return size; |
440 | } | 436 | } else { |
441 | |||
442 | if (ipv6 == 1) { | ||
443 | uint32_t size = 1 + SIZE_IP6 + sizeof(uint16_t); | 437 | uint32_t size = 1 + SIZE_IP6 + sizeof(uint16_t); |
444 | 438 | ||
445 | if (size > length) { | 439 | if (size > length) { |
@@ -451,8 +445,6 @@ static int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length | |||
451 | memcpy(&ip_port->port, data + 1 + SIZE_IP6, sizeof(uint16_t)); | 445 | memcpy(&ip_port->port, data + 1 + SIZE_IP6, sizeof(uint16_t)); |
452 | return size; | 446 | return size; |
453 | } | 447 | } |
454 | |||
455 | return -1; | ||
456 | } | 448 | } |
457 | 449 | ||
458 | /* Pack number of nodes into data of maxlength length. | 450 | /* Pack number of nodes into data of maxlength length. |