Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make slightly better use of SockAddr in TCP code. #26483

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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