Simplify sendpacket function, deduplicate some logic.

This commit is contained in:
iphydf 2018-02-27 04:44:18 +00:00
parent acb25e66eb
commit 78d5b74dce
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9

View File

@ -423,7 +423,7 @@ uint16_t net_port(const Networking_Core *net)
*/ */
int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint16_t length) int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint16_t length)
{ {
if (net->family == 0) { /* Socket not initialized */ if (net->family == TOX_AF_UNSPEC) { /* Socket not initialized */
return -1; return -1;
} }
@ -436,42 +436,35 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1
size_t addrsize = 0; size_t addrsize = 0;
if (ip_port.ip.family == TOX_AF_INET) { if (ip_port.ip.family == TOX_AF_INET && net->family == TOX_AF_INET6) {
if (net->family == TOX_AF_INET6) {
/* must convert to IPV4-in-IPV6 address */ /* must convert to IPV4-in-IPV6 address */
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; IP6 ip6;
addrsize = sizeof(struct sockaddr_in6);
addr6->sin6_family = AF_INET6;
addr6->sin6_port = ip_port.port;
/* there should be a macro for this in a standards compliant /* there should be a macro for this in a standards compliant
* environment, not found */ * environment, not found */
IP6 ip6;
ip6.uint32[0] = 0; ip6.uint32[0] = 0;
ip6.uint32[1] = 0; ip6.uint32[1] = 0;
ip6.uint32[2] = net_htonl(0xFFFF); ip6.uint32[2] = net_htonl(0xFFFF);
ip6.uint32[3] = ip_port.ip.ip.v4.uint32; ip6.uint32[3] = ip_port.ip.ip.v4.uint32;
fill_addr6(ip6, &addr6->sin6_addr);
addr6->sin6_flowinfo = 0; ip_port.ip.family = TOX_AF_INET6;
addr6->sin6_scope_id = 0; ip_port.ip.ip.v6 = ip6;
} else { }
if (ip_port.ip.family == TOX_AF_INET) {
struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr;
addrsize = sizeof(struct sockaddr_in); addrsize = sizeof(struct sockaddr_in);
addr4->sin_family = AF_INET;
fill_addr4(ip_port.ip.ip.v4, &addr4->sin_addr); fill_addr4(ip_port.ip.ip.v4, &addr4->sin_addr);
addr4->sin_family = AF_INET;
addr4->sin_port = ip_port.port; addr4->sin_port = ip_port.port;
}
} else if (ip_port.ip.family == TOX_AF_INET6) { } else if (ip_port.ip.family == TOX_AF_INET6) {
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr;
addrsize = sizeof(struct sockaddr_in6); addrsize = sizeof(struct sockaddr_in6);
fill_addr6(ip_port.ip.ip.v6, &addr6->sin6_addr);
addr6->sin6_family = AF_INET6; addr6->sin6_family = AF_INET6;
addr6->sin6_port = ip_port.port; addr6->sin6_port = ip_port.port;
fill_addr6(ip_port.ip.ip.v6, &addr6->sin6_addr);
addr6->sin6_flowinfo = 0; addr6->sin6_flowinfo = 0;
addr6->sin6_scope_id = 0; addr6->sin6_scope_id = 0;
@ -480,7 +473,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1
return -1; return -1;
} }
int res = sendto(net->sock, (const char *) data, length, 0, (struct sockaddr *)&addr, addrsize); const int res = sendto(net->sock, (const char *) data, length, 0, (struct sockaddr *)&addr, addrsize);
loglogdata(net->log, "O=>", data, length, ip_port, res); loglogdata(net->log, "O=>", data, length, ip_port, res);