refactor: add tr_address::toSockaddr() (#3870)

This commit is contained in:
Charles Kerr 2022-10-04 14:52:51 -05:00 committed by GitHub
parent b49129dfa6
commit fff4c87740
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 25 deletions

View File

@ -209,27 +209,25 @@ bool tr_address_from_sockaddr_storage(tr_address* setme_addr, tr_port* setme_por
return false; return false;
} }
static socklen_t setup_sockaddr(tr_address const* addr, tr_port port, struct sockaddr_storage* sockaddr) std::pair<sockaddr_storage, socklen_t> tr_address::toSockaddr(tr_port port) const noexcept
{ {
TR_ASSERT(tr_address_is_valid(addr)); auto ss = sockaddr_storage{};
if (addr->isIPv4()) if (isIPv4())
{ {
sockaddr_in sock4 = {}; auto* const ss4 = reinterpret_cast<sockaddr_in*>(&ss);
sock4.sin_family = AF_INET; ss4->sin_addr.s_addr = addr.addr4.s_addr;
sock4.sin_addr.s_addr = addr->addr.addr4.s_addr; ss4->sin_family = AF_INET;
sock4.sin_port = port.network(); ss4->sin_port = port.network();
memcpy(sockaddr, &sock4, sizeof(sock4)); return { ss, sizeof(sockaddr_in) };
return sizeof(struct sockaddr_in);
} }
sockaddr_in6 sock6 = {}; auto* const ss6 = reinterpret_cast<sockaddr_in6*>(&ss);
sock6.sin6_family = AF_INET6; ss6->sin6_addr = addr.addr6;
sock6.sin6_port = port.network(); ss6->sin6_family = AF_INET6;
sock6.sin6_flowinfo = 0; ss6->sin6_flowinfo = 0;
sock6.sin6_addr = addr->addr.addr6; ss6->sin6_port = port.network();
memcpy(sockaddr, &sock6, sizeof(sock6)); return { ss, sizeof(sockaddr_in6) };
return sizeof(struct sockaddr_in6);
} }
static tr_socket_t createSocket(tr_session* session, int domain, int type) static tr_socket_t createSocket(tr_session* session, int domain, int type)
@ -312,13 +310,11 @@ struct tr_peer_socket tr_netOpenPeerSocket(tr_session* session, tr_address const
} }
} }
auto sock = sockaddr_storage{}; auto const [sock, addrlen] = addr->toSockaddr(port);
socklen_t const addrlen = setup_sockaddr(addr, port, &sock);
// set source address // set source address
auto const [source_addr, is_default_addr] = session->publicAddress(addr->type); auto const [source_addr, is_default_addr] = session->publicAddress(addr->type);
auto source_sock = sockaddr_storage{}; auto const [source_sock, sourcelen] = source_addr.toSockaddr({});
socklen_t const sourcelen = setup_sockaddr(&source_addr, {}, &source_sock);
if (bind(s, (struct sockaddr*)&source_sock, sourcelen) == -1) if (bind(s, (struct sockaddr*)&source_sock, sourcelen) == -1)
{ {
@ -372,13 +368,12 @@ struct tr_peer_socket tr_netOpenPeerUTPSocket(
if (session->utp_context != nullptr && tr_address_is_valid_for_peers(addr, port)) if (session->utp_context != nullptr && tr_address_is_valid_for_peers(addr, port))
{ {
auto ss = sockaddr_storage{}; auto const [ss, sslen] = addr->toSockaddr(port);
socklen_t const sslen = setup_sockaddr(addr, port, &ss);
auto* const socket = utp_create_socket(session->utp_context); auto* const socket = utp_create_socket(session->utp_context);
if (socket != nullptr) if (socket != nullptr)
{ {
if (utp_connect(socket, reinterpret_cast<sockaddr*>(&ss), sslen) != -1) if (utp_connect(socket, reinterpret_cast<sockaddr const*>(&ss), sslen) != -1)
{ {
ret = tr_peer_socket_utp_create(socket); ret = tr_peer_socket_utp_create(socket);
} }
@ -420,7 +415,6 @@ static tr_socket_t tr_netBindTCPImpl(tr_address const* addr, tr_port port, bool
TR_ASSERT(tr_address_is_valid(addr)); TR_ASSERT(tr_address_is_valid(addr));
static auto constexpr Domains = std::array<int, NUM_TR_AF_INET_TYPES>{ AF_INET, AF_INET6 }; static auto constexpr Domains = std::array<int, NUM_TR_AF_INET_TYPES>{ AF_INET, AF_INET6 };
auto sock = sockaddr_storage{};
auto const fd = socket(Domains[addr->type], SOCK_STREAM, 0); auto const fd = socket(Domains[addr->type], SOCK_STREAM, 0);
if (fd == TR_BAD_SOCKET) if (fd == TR_BAD_SOCKET)
@ -453,7 +447,7 @@ static tr_socket_t tr_netBindTCPImpl(tr_address const* addr, tr_port port, bool
#endif #endif
int const addrlen = setup_sockaddr(addr, port, &sock); auto const [sock, addrlen] = addr->toSockaddr(port);
if (bind(fd, (struct sockaddr*)&sock, addrlen) == -1) if (bind(fd, (struct sockaddr*)&sock, addrlen) == -1)
{ {

View File

@ -191,6 +191,10 @@ struct tr_address
return this->compare(that) > 0; return this->compare(that) > 0;
} }
//
[[nodiscard]] std::pair<sockaddr_storage, socklen_t> toSockaddr(tr_port port) const noexcept;
tr_address_type type; tr_address_type type;
union union
{ {