Skip to content

Commit

Permalink
Make slightly better use of SockAddr in TCP code. (#26483)
Browse files Browse the repository at this point in the history
Since we have a union for our type-punning, we don't need to do
reinterpret_cast.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Dec 6, 2023
1 parent 6f76934 commit 1421794
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions src/inet/TCPEndPointImplSockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,40 +96,42 @@ CHIP_ERROR TCPEndPointImplSockets::BindImpl(IPAddressType addrType, const IPAddr

if (res == CHIP_NO_ERROR)
{
SockAddr sa;
memset(&sa, 0, sizeof(sa));
socklen_t sockaddrsize = 0;

if (addrType == IPAddressType::kIPv6)
{
struct sockaddr_in6 sa;
memset(&sa, 0, sizeof(sa));
sa.sin6_family = AF_INET6;
sa.sin6_port = htons(port);
sa.sin6_flowinfo = 0;
sa.sin6_addr = addr.ToIPv6();
sa.sin6_scope_id = 0;

if (bind(mSocket, reinterpret_cast<const sockaddr *>(&sa), static_cast<unsigned>(sizeof(sa))) != 0)
{
res = CHIP_ERROR_POSIX(errno);
}
sa.in6.sin6_family = AF_INET6;
sa.in6.sin6_port = htons(port);
sa.in6.sin6_flowinfo = 0;
sa.in6.sin6_addr = addr.ToIPv6();
sa.in6.sin6_scope_id = 0;

sockaddrsize = sizeof(sa.in6);
}
#if INET_CONFIG_ENABLE_IPV4
else if (addrType == IPAddressType::kIPv4)
{
struct sockaddr_in sa;
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
sa.sin_addr = addr.ToIPv4();
sa.in.sin_family = AF_INET;
sa.in.sin_port = htons(port);
sa.in.sin_addr = addr.ToIPv4();

if (bind(mSocket, reinterpret_cast<const sockaddr *>(&sa), static_cast<unsigned>(sizeof(sa))) != 0)
{
res = CHIP_ERROR_POSIX(errno);
}
sockaddrsize = sizeof(sa.in);
}
#endif // INET_CONFIG_ENABLE_IPV4
else
{
res = INET_ERROR_WRONG_ADDRESS_TYPE;
}

if (res == CHIP_NO_ERROR)
{
if (bind(mSocket, &sa.any, sockaddrsize) != 0)
{
res = CHIP_ERROR_POSIX(errno);
}
}
}

return res;
Expand Down Expand Up @@ -218,8 +220,7 @@ CHIP_ERROR TCPEndPointImplSockets::ConnectImpl(const IPAddress & addr, uint16_t
int flags = fcntl(mSocket, F_GETFL, 0);
fcntl(mSocket, F_SETFL, flags | O_NONBLOCK);

socklen_t sockaddrsize = 0;
const sockaddr * sockaddrptr = nullptr;
socklen_t sockaddrsize = 0;

SockAddr sa;
memset(&sa, 0, sizeof(sa));
Expand All @@ -232,7 +233,6 @@ CHIP_ERROR TCPEndPointImplSockets::ConnectImpl(const IPAddress & addr, uint16_t
sa.in6.sin6_addr = addr.ToIPv6();
sa.in6.sin6_scope_id = intfId.GetPlatformInterface();
sockaddrsize = sizeof(sockaddr_in6);
sockaddrptr = reinterpret_cast<const sockaddr *>(&sa.in6);
}
#if INET_CONFIG_ENABLE_IPV4
else if (addrType == IPAddressType::kIPv4)
Expand All @@ -241,15 +241,14 @@ CHIP_ERROR TCPEndPointImplSockets::ConnectImpl(const IPAddress & addr, uint16_t
sa.in.sin_port = htons(port);
sa.in.sin_addr = addr.ToIPv4();
sockaddrsize = sizeof(sockaddr_in);
sockaddrptr = reinterpret_cast<const sockaddr *>(&sa.in);
}
#endif // INET_CONFIG_ENABLE_IPV4
else
{
return INET_ERROR_WRONG_ADDRESS_TYPE;
}

int conRes = connect(mSocket, sockaddrptr, sockaddrsize);
int conRes = connect(mSocket, &sa.any, sockaddrsize);

if (conRes == -1 && errno != EINPROGRESS)
{
Expand Down

0 comments on commit 1421794

Please sign in to comment.