socket: socket::set_sockaddr() for IPv4 addresses in IPv6 builds (#7196)

This commit is contained in:
David Woodhouse 2024-08-06 02:50:36 +01:00 committed by GitHub
parent 6b141102d6
commit 3ba9caa118
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -19,24 +19,22 @@ std::unique_ptr<Socket> socket_ip(int type, int protocol) {
socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const std::string &ip_address, uint16_t port) {
#if USE_NETWORK_IPV6
if (addrlen < sizeof(sockaddr_in6)) {
errno = EINVAL;
return 0;
}
auto *server = reinterpret_cast<sockaddr_in6 *>(addr);
memset(server, 0, sizeof(sockaddr_in6));
server->sin6_family = AF_INET6;
server->sin6_port = htons(port);
if (ip_address.find(':') != std::string::npos) {
if (addrlen < sizeof(sockaddr_in6)) {
errno = EINVAL;
return 0;
}
auto *server = reinterpret_cast<sockaddr_in6 *>(addr);
memset(server, 0, sizeof(sockaddr_in6));
server->sin6_family = AF_INET6;
server->sin6_port = htons(port);
if (ip_address.find('.') != std::string::npos) {
server->sin6_addr.un.u32_addr[3] = inet_addr(ip_address.c_str());
} else {
ip6_addr_t ip6;
inet6_aton(ip_address.c_str(), &ip6);
memcpy(server->sin6_addr.un.u32_addr, ip6.addr, sizeof(ip6.addr));
return sizeof(sockaddr_in6);
}
return sizeof(sockaddr_in6);
#else
#endif /* USE_NETWORK_IPV6 */
if (addrlen < sizeof(sockaddr_in)) {
errno = EINVAL;
return 0;
@ -47,7 +45,6 @@ socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const std::stri
server->sin_addr.s_addr = inet_addr(ip_address.c_str());
server->sin_port = htons(port);
return sizeof(sockaddr_in);
#endif /* USE_NETWORK_IPV6 */
}
socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port) {