diff options
-rw-r--r-- | toxcore/network.c | 53 |
1 files changed, 23 insertions, 30 deletions
diff --git a/toxcore/network.c b/toxcore/network.c index e9c82c39..3aaeb075 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -423,7 +423,7 @@ uint16_t net_port(const Networking_Core *net) | |||
423 | */ | 423 | */ |
424 | int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint16_t length) | 424 | int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint16_t length) |
425 | { | 425 | { |
426 | if (net->family == 0) { /* Socket not initialized */ | 426 | if (net->family == TOX_AF_UNSPEC) { /* Socket not initialized */ |
427 | return -1; | 427 | return -1; |
428 | } | 428 | } |
429 | 429 | ||
@@ -436,42 +436,35 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 | |||
436 | 436 | ||
437 | size_t addrsize = 0; | 437 | size_t addrsize = 0; |
438 | 438 | ||
439 | if (ip_port.ip.family == TOX_AF_INET && net->family == TOX_AF_INET6) { | ||
440 | /* must convert to IPV4-in-IPV6 address */ | ||
441 | IP6 ip6; | ||
442 | |||
443 | /* there should be a macro for this in a standards compliant | ||
444 | * environment, not found */ | ||
445 | ip6.uint32[0] = 0; | ||
446 | ip6.uint32[1] = 0; | ||
447 | ip6.uint32[2] = net_htonl(0xFFFF); | ||
448 | ip6.uint32[3] = ip_port.ip.ip.v4.uint32; | ||
449 | |||
450 | ip_port.ip.family = TOX_AF_INET6; | ||
451 | ip_port.ip.ip.v6 = ip6; | ||
452 | } | ||
453 | |||
439 | if (ip_port.ip.family == TOX_AF_INET) { | 454 | if (ip_port.ip.family == TOX_AF_INET) { |
440 | if (net->family == TOX_AF_INET6) { | 455 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; |
441 | /* must convert to IPV4-in-IPV6 address */ | ||
442 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; | ||
443 | |||
444 | addrsize = sizeof(struct sockaddr_in6); | ||
445 | addr6->sin6_family = AF_INET6; | ||
446 | addr6->sin6_port = ip_port.port; | ||
447 | |||
448 | /* there should be a macro for this in a standards compliant | ||
449 | * environment, not found */ | ||
450 | IP6 ip6; | ||
451 | |||
452 | ip6.uint32[0] = 0; | ||
453 | ip6.uint32[1] = 0; | ||
454 | ip6.uint32[2] = net_htonl(0xFFFF); | ||
455 | ip6.uint32[3] = ip_port.ip.ip.v4.uint32; | ||
456 | fill_addr6(ip6, &addr6->sin6_addr); | ||
457 | |||
458 | addr6->sin6_flowinfo = 0; | ||
459 | addr6->sin6_scope_id = 0; | ||
460 | } else { | ||
461 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; | ||
462 | 456 | ||
463 | addrsize = sizeof(struct sockaddr_in); | 457 | addrsize = sizeof(struct sockaddr_in); |
464 | addr4->sin_family = AF_INET; | 458 | fill_addr4(ip_port.ip.ip.v4, &addr4->sin_addr); |
465 | fill_addr4(ip_port.ip.ip.v4, &addr4->sin_addr); | 459 | addr4->sin_family = AF_INET; |
466 | addr4->sin_port = ip_port.port; | 460 | addr4->sin_port = ip_port.port; |
467 | } | ||
468 | } else if (ip_port.ip.family == TOX_AF_INET6) { | 461 | } else if (ip_port.ip.family == TOX_AF_INET6) { |
469 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; | 462 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; |
470 | 463 | ||
471 | addrsize = sizeof(struct sockaddr_in6); | 464 | addrsize = sizeof(struct sockaddr_in6); |
465 | fill_addr6(ip_port.ip.ip.v6, &addr6->sin6_addr); | ||
472 | addr6->sin6_family = AF_INET6; | 466 | addr6->sin6_family = AF_INET6; |
473 | addr6->sin6_port = ip_port.port; | 467 | addr6->sin6_port = ip_port.port; |
474 | fill_addr6(ip_port.ip.ip.v6, &addr6->sin6_addr); | ||
475 | 468 | ||
476 | addr6->sin6_flowinfo = 0; | 469 | addr6->sin6_flowinfo = 0; |
477 | addr6->sin6_scope_id = 0; | 470 | addr6->sin6_scope_id = 0; |
@@ -480,7 +473,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 | |||
480 | return -1; | 473 | return -1; |
481 | } | 474 | } |
482 | 475 | ||
483 | int res = sendto(net->sock, (const char *) data, length, 0, (struct sockaddr *)&addr, addrsize); | 476 | const int res = sendto(net->sock, (const char *) data, length, 0, (struct sockaddr *)&addr, addrsize); |
484 | 477 | ||
485 | loglogdata(net->log, "O=>", data, length, ip_port, res); | 478 | loglogdata(net->log, "O=>", data, length, ip_port, res); |
486 | 479 | ||