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

Additional socket checks for socket inet implementations #35674

Merged
merged 7 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
67 changes: 32 additions & 35 deletions src/inet/TCPEndPointImplSockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ namespace Inet {

CHIP_ERROR TCPEndPointImplSockets::BindImpl(IPAddressType addrType, const IPAddress & addr, uint16_t port, bool reuseAddr)
{
CHIP_ERROR res = GetSocket(addrType);
ReturnErrorOnFailure(GetSocket(addrType));

// need to be in a connected state to be able to bind
VerifyOrReturnError(mSocket >= 0, CHIP_ERROR_INCORRECT_STATE);
andy31415 marked this conversation as resolved.
Show resolved Hide resolved

if (res == CHIP_NO_ERROR && reuseAddr)
if (reuseAddr)
{
int n = 1;
setsockopt(mSocket, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n));
Expand All @@ -94,47 +97,41 @@ CHIP_ERROR TCPEndPointImplSockets::BindImpl(IPAddressType addrType, const IPAddr
#endif // defined(SO_REUSEPORT)
}

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

if (addrType == IPAddressType::kIPv6)
{
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;
if (addrType == IPAddressType::kIPv6)
{
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);
}
sockaddrsize = sizeof(sa.in6);
}
#if INET_CONFIG_ENABLE_IPV4
else if (addrType == IPAddressType::kIPv4)
{
sa.in.sin_family = AF_INET;
sa.in.sin_port = htons(port);
sa.in.sin_addr = addr.ToIPv4();
else if (addrType == IPAddressType::kIPv4)
{
sa.in.sin_family = AF_INET;
sa.in.sin_port = htons(port);
sa.in.sin_addr = addr.ToIPv4();

sockaddrsize = sizeof(sa.in);
}
sockaddrsize = sizeof(sa.in);
}
#endif // INET_CONFIG_ENABLE_IPV4
else
{
res = INET_ERROR_WRONG_ADDRESS_TYPE;
}
else
{
return INET_ERROR_WRONG_ADDRESS_TYPE;
}

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

return res;
return CHIP_NO_ERROR;
}

CHIP_ERROR TCPEndPointImplSockets::ListenImpl(uint16_t backlog)
Expand Down
7 changes: 7 additions & 0 deletions src/inet/UDPEndPointImplSockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ namespace {

CHIP_ERROR IPv6Bind(int socket, const IPAddress & address, uint16_t port, InterfaceId interface)
{
VerifyOrReturnValue(socket >= 0, CHIP_ERROR_INVALID_ARGUMENT);
andy31415 marked this conversation as resolved.
Show resolved Hide resolved

struct sockaddr_in6 sa;
memset(&sa, 0, sizeof(sa));
sa.sin6_family = AF_INET6;
Expand Down Expand Up @@ -132,6 +134,8 @@ CHIP_ERROR IPv6Bind(int socket, const IPAddress & address, uint16_t port, Interf
#if INET_CONFIG_ENABLE_IPV4
CHIP_ERROR IPv4Bind(int socket, const IPAddress & address, uint16_t port)
{
VerifyOrReturnValue(socket >= 0, CHIP_ERROR_INVALID_ARGUMENT);

struct sockaddr_in sa;
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
Expand Down Expand Up @@ -286,6 +290,9 @@ CHIP_ERROR UDPEndPointImplSockets::SendMsgImpl(const IPPacketInfo * aPktInfo, Sy
// destination address.
ReturnErrorOnFailure(GetSocket(aPktInfo->DestAddress.Type()));

// have to have a valid socket to send something with. Generally GetSocket ensures that
VerifyOrReturnValue(mSocket >= 0, CHIP_ERROR_INCORRECT_STATE);

// Ensure the destination address type is compatible with the endpoint address type.
VerifyOrReturnError(mAddrType == aPktInfo->DestAddress.Type(), CHIP_ERROR_INVALID_ARGUMENT);

Expand Down
Loading