bloomberg / bloomberg/ntf-core
`ntsi::StreamSocket::bind` parameter `reuseAddress` is inconsistent on different platforms
- Dominant language
- C++
- Stars
- 99
- Forks
- 33
- PR merge metrics
- No merged PRs in 30d
Description
The behavior of the method seems to be different on AIX and Linux:
```C++
auto err = socket1->bind(ntsa::Endpoint(ntsa::Ipv4Endpoint("127.0.0.1", 0)), true);
assert(!err);
ntsa::Endpoint endpoint;
socket->sourceEndpoint(&endpoint);
err = socket2->bind(ntsa::Endpoint(ntsa::Ipv4Endpoint("127.0.0.1", 0)), true);
assert(!err);
// on AIX the second bind succeeds, while on Linux this fails.
```
I attached the relevant code in ntsu_socketoptionutil.cpp below. I think SO_REUSEPORT is required for the above code to work on Linux.
I understand making the behavior consistent on all support platform might be very difficult or impossible, so it's not a straight forward fix.
```C++
ntsa::Error SocketOptionUtil::setReuseAddress(ntsa::Handle socket,
bool reuseAddress)
{
{
int optionValue = static_cast(reuseAddress);
int rc = setsockopt(socket,
SOL_SOCKET,
SO_REUSEADDR,
reinterpret_cast(&optionValue),
sizeof(optionValue));
if (rc != 0) {
return ntsa::Error(errno);
}
}
#if defined(BSLS_PLATFORM_OS_AIX) || defined(BSLS_PLATFORM_OS_DARWIN) || \
defined(BSLS_PLATFORM_OS_FREEBSD)
{
int optionValue = static_cast(reuseAddress);
int rc = setsockopt(socket,
SOL_SOCKET,
SO_REUSEPORT,
reinterpret_cast(&optionValue),
sizeof(optionValue));
if (rc != 0) {
return ntsa::Error(errno);
}
}
#endif
return ntsa::Error();
}
```
Contributor guide
Research direction
Start in ntsu_socketoptionutil.cpp at SocketOptionUtil::setReuseAddress and compare the SO_REUSEADDR and SO_REUSEPORT handling across AIX and Linux. Investigate the platform socket semantics and any existing socket-option coverage. Done means the supported behavior is made consistent where possible, or the platform-dependent behavior and limitations are clearly documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- networking
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100