Loading src/posix/tcp.cpp +60 −0 Changes for src/posix/tcp.cpp: 60 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -356,6 +356,30 @@ size_t tcp::recvData(buffer& data, int flags) { throw e; } namespace { // Binds `fd` to the numeric source address `src` if it is of `family`. Returns false on a // family mismatch (caller skips that destination) and throws if the bind itself fails. bool bindSourceAddress(int fd, const std::string& src, int family) { addrinfo hints{}; hints.ai_family = family; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE; addrinfo* res = nullptr; if (::getaddrinfo(src.c_str(), nullptr, &hints, &res) != 0 || !res) return false; int rc = ::bind(fd, res->ai_addr, res->ai_addrlen); int err = errno; ::freeaddrinfo(res); if (rc != 0) { netplus::NetException e; e[netplus::NetException::Error] << "tcp::connect: cannot bind source address " << src << ": " << std::strerror(err); throw e; } return true; } } // namespace void netplus::tcp::connect(const std::string& addr, int port, bool nonblock) { NetException exception; Loading Loading @@ -394,11 +418,29 @@ void netplus::tcp::connect(const std::string& addr, int port, bool nonblock) } } // a source address is bound once per socket: always start from a fresh one if (!_SourceAddress.empty() && _Socket >= 0) { ::close(_Socket); _Socket = -1; } if (_Socket < 0) { _Socket = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (_Socket < 0) continue; } if (!_SourceAddress.empty()) { bool bound = false; try { bound = bindSourceAddress(_Socket, _SourceAddress, rp->ai_family); } catch (const NetException& be) { exception[NetException::Error] << be.what() << "; "; } if (!bound) { ::close(_Socket); _Socket = -1; continue; } } // ------------------------------------------------------- // 2) always connect non-blocking internally, even when the Loading Loading @@ -545,11 +587,29 @@ void tcp::connectTimeout(const std::string& addr, int port, int timeout_ms) { } } // a source address is bound once per socket: always start from a fresh one if (!_SourceAddress.empty() && _Socket >= 0) { ::close(_Socket); _Socket = -1; } if (_Socket < 0) { _Socket = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (_Socket < 0) continue; } if (!_SourceAddress.empty()) { bool bound = false; try { bound = bindSourceAddress(_Socket, _SourceAddress, rp->ai_family); } catch (const NetException& be) { exception[NetException::Error] << be.what() << "; "; } if (!bound) { ::close(_Socket); _Socket = -1; continue; } } try { setNonBlock(); Loading src/socket.h +9 −0 Changes for src/socket.h: 9 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -384,11 +384,20 @@ namespace netplus { void connect(const std::string& addr, int port, bool nonblock = false) override; void connectTimeout(const std::string& addr, int port, int timeout_ms) override; // Local IP (numeric, v4 or v6) outgoing connections are bound to before // connect() -- for hosts with several addresses, e.g. behind 1:1/binat NAT // where only one maps to the public IP with the right reverse DNS. // Destination addresses of the other family are skipped. "" = any. // Must be set before connect()/connectTimeout(). void setSourceAddress(const std::string& ip) { _SourceAddress = ip; } const std::string& sourceAddress() const { return _SourceAddress; } void getAddress(std::string& addr) override; private: int _Maxconnections; std::string _UxPath; std::string _SourceAddress; }; // The real wire length of a peer address, by family — NOT Loading src/windows/tcp.cpp +42 −0 Changes for src/windows/tcp.cpp: 42 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -273,6 +273,30 @@ size_t tcp::recvData(buffer& data, int flags) { throw e; } namespace { // Binds `s` to the numeric source address `src` if it is of `family`. Returns false on a // family mismatch (caller skips that destination) and throws if the bind itself fails. bool bindSourceAddress(SOCKET s, const std::string& src, int family) { addrinfo hints{}; hints.ai_family = family; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE; addrinfo* res = nullptr; if (::getaddrinfo(src.c_str(), nullptr, &hints, &res) != 0 || !res) return false; int rc = ::bind(s, res->ai_addr, static_cast<int>(res->ai_addrlen)); int err = WSAGetLastError(); ::freeaddrinfo(res); if (rc == SOCKET_ERROR) { netplus::NetException e; e[netplus::NetException::Error] << "tcp::connect: cannot bind source address " << src << ": WSA error " << err; throw e; } return true; } } // namespace void netplus::tcp::connect(const std::string& addr, int port, bool nonblock) { NetException exception; Loading Loading @@ -311,11 +335,29 @@ void netplus::tcp::connect(const std::string& addr, int port, bool nonblock) } } // a source address is bound once per socket: always start from a fresh one if (!_SourceAddress.empty() && _Socket != INVALID_SOCKET) { ::closesocket(_Socket); _Socket = INVALID_SOCKET; } if (_Socket == INVALID_SOCKET) { _Socket = ::WSASocketA(rp->ai_family, rp->ai_socktype, rp->ai_protocol, nullptr, 0, WSA_FLAG_OVERLAPPED); if (_Socket == INVALID_SOCKET) continue; } if (!_SourceAddress.empty()) { bool bound = false; try { bound = bindSourceAddress(_Socket, _SourceAddress, rp->ai_family); } catch (const NetException& be) { exception[NetException::Error] << be.what() << "; "; } if (!bound) { ::closesocket(_Socket); _Socket = INVALID_SOCKET; continue; } } // ------------------------------------------------------- // 2) set nonblock BEFORE connect() if requested Loading test/CMakeLists.txt +8 −0 Changes for test/CMakeLists.txt: 8 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -328,3 +328,11 @@ add_test(NAME srtp_test COMMAND srtp_test) add_executable(dtls_harness dtls_harness.cpp) target_link_libraries(dtls_harness netplus-static) add_test(NAME dtls_openssl COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/dtls_openssl.sh $<TARGET_FILE:dtls_harness>) add_executable(source_address_test source_address_test.cpp) if(WIN32) target_link_libraries(source_address_test netplus-static ws2_32) else() target_link_libraries(source_address_test netplus-static) endif() add_test(NAME source_address_test COMMAND source_address_test) test/source_address_test.cpp 0 → 100644 +59 −0 Changes for test/source_address_test.cpp: 59 added lines, 0 removed lines. Original line number Diff line number Diff line // tcp::setSourceAddress(): the outgoing connection leaves from the given local IP. #include <atomic> #include <iostream> #include <memory> #include <string> #include <thread> #include "socket.h" #include "exception.h" #ifdef __linux__ #include <unistd.h> #endif int main() { int failures = 0; #ifdef __linux__ // only Linux routes all of 127/8 to lo; BSDs configure just 127.0.0.1 const int port = 39000 + (static_cast<int>(::getpid()) % 2000); netplus::tcp server("127.0.0.1", port, 4, 0); server.bind(); server.listen(); std::string peer; std::thread acceptor([&] { std::unique_ptr<netplus::socket> c; server.accept(c, false); if (c) c->getAddress(peer); }); try { netplus::tcp client; client.setSourceAddress("127.0.0.2"); client.connect("127.0.0.1", port); } catch (const std::exception &e) { std::cerr << "connect with source address failed: " << e.what() << std::endl; ++failures; } acceptor.join(); if (peer.rfind("127.0.0.2", 0) != 0) { std::cerr << "server saw peer '" << peer << "', expected 127.0.0.2" << std::endl; ++failures; } // an address of the other family only: no destination left -> connect fails bool threw = false; try { netplus::tcp client; client.setSourceAddress("::1"); client.connect("127.0.0.1", port); } catch (const std::exception &) { threw = true; } if (!threw) { std::cerr << "IPv6 source to an IPv4-only destination should fail" << std::endl; ++failures; } #endif std::cout << (failures ? "FAILED" : "OK") << std::endl; return failures ? 1 : 0; } Loading
src/posix/tcp.cpp +60 −0 Changes for src/posix/tcp.cpp: 60 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -356,6 +356,30 @@ size_t tcp::recvData(buffer& data, int flags) { throw e; } namespace { // Binds `fd` to the numeric source address `src` if it is of `family`. Returns false on a // family mismatch (caller skips that destination) and throws if the bind itself fails. bool bindSourceAddress(int fd, const std::string& src, int family) { addrinfo hints{}; hints.ai_family = family; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE; addrinfo* res = nullptr; if (::getaddrinfo(src.c_str(), nullptr, &hints, &res) != 0 || !res) return false; int rc = ::bind(fd, res->ai_addr, res->ai_addrlen); int err = errno; ::freeaddrinfo(res); if (rc != 0) { netplus::NetException e; e[netplus::NetException::Error] << "tcp::connect: cannot bind source address " << src << ": " << std::strerror(err); throw e; } return true; } } // namespace void netplus::tcp::connect(const std::string& addr, int port, bool nonblock) { NetException exception; Loading Loading @@ -394,11 +418,29 @@ void netplus::tcp::connect(const std::string& addr, int port, bool nonblock) } } // a source address is bound once per socket: always start from a fresh one if (!_SourceAddress.empty() && _Socket >= 0) { ::close(_Socket); _Socket = -1; } if (_Socket < 0) { _Socket = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (_Socket < 0) continue; } if (!_SourceAddress.empty()) { bool bound = false; try { bound = bindSourceAddress(_Socket, _SourceAddress, rp->ai_family); } catch (const NetException& be) { exception[NetException::Error] << be.what() << "; "; } if (!bound) { ::close(_Socket); _Socket = -1; continue; } } // ------------------------------------------------------- // 2) always connect non-blocking internally, even when the Loading Loading @@ -545,11 +587,29 @@ void tcp::connectTimeout(const std::string& addr, int port, int timeout_ms) { } } // a source address is bound once per socket: always start from a fresh one if (!_SourceAddress.empty() && _Socket >= 0) { ::close(_Socket); _Socket = -1; } if (_Socket < 0) { _Socket = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (_Socket < 0) continue; } if (!_SourceAddress.empty()) { bool bound = false; try { bound = bindSourceAddress(_Socket, _SourceAddress, rp->ai_family); } catch (const NetException& be) { exception[NetException::Error] << be.what() << "; "; } if (!bound) { ::close(_Socket); _Socket = -1; continue; } } try { setNonBlock(); Loading
src/socket.h +9 −0 Changes for src/socket.h: 9 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -384,11 +384,20 @@ namespace netplus { void connect(const std::string& addr, int port, bool nonblock = false) override; void connectTimeout(const std::string& addr, int port, int timeout_ms) override; // Local IP (numeric, v4 or v6) outgoing connections are bound to before // connect() -- for hosts with several addresses, e.g. behind 1:1/binat NAT // where only one maps to the public IP with the right reverse DNS. // Destination addresses of the other family are skipped. "" = any. // Must be set before connect()/connectTimeout(). void setSourceAddress(const std::string& ip) { _SourceAddress = ip; } const std::string& sourceAddress() const { return _SourceAddress; } void getAddress(std::string& addr) override; private: int _Maxconnections; std::string _UxPath; std::string _SourceAddress; }; // The real wire length of a peer address, by family — NOT Loading
src/windows/tcp.cpp +42 −0 Changes for src/windows/tcp.cpp: 42 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -273,6 +273,30 @@ size_t tcp::recvData(buffer& data, int flags) { throw e; } namespace { // Binds `s` to the numeric source address `src` if it is of `family`. Returns false on a // family mismatch (caller skips that destination) and throws if the bind itself fails. bool bindSourceAddress(SOCKET s, const std::string& src, int family) { addrinfo hints{}; hints.ai_family = family; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE; addrinfo* res = nullptr; if (::getaddrinfo(src.c_str(), nullptr, &hints, &res) != 0 || !res) return false; int rc = ::bind(s, res->ai_addr, static_cast<int>(res->ai_addrlen)); int err = WSAGetLastError(); ::freeaddrinfo(res); if (rc == SOCKET_ERROR) { netplus::NetException e; e[netplus::NetException::Error] << "tcp::connect: cannot bind source address " << src << ": WSA error " << err; throw e; } return true; } } // namespace void netplus::tcp::connect(const std::string& addr, int port, bool nonblock) { NetException exception; Loading Loading @@ -311,11 +335,29 @@ void netplus::tcp::connect(const std::string& addr, int port, bool nonblock) } } // a source address is bound once per socket: always start from a fresh one if (!_SourceAddress.empty() && _Socket != INVALID_SOCKET) { ::closesocket(_Socket); _Socket = INVALID_SOCKET; } if (_Socket == INVALID_SOCKET) { _Socket = ::WSASocketA(rp->ai_family, rp->ai_socktype, rp->ai_protocol, nullptr, 0, WSA_FLAG_OVERLAPPED); if (_Socket == INVALID_SOCKET) continue; } if (!_SourceAddress.empty()) { bool bound = false; try { bound = bindSourceAddress(_Socket, _SourceAddress, rp->ai_family); } catch (const NetException& be) { exception[NetException::Error] << be.what() << "; "; } if (!bound) { ::closesocket(_Socket); _Socket = INVALID_SOCKET; continue; } } // ------------------------------------------------------- // 2) set nonblock BEFORE connect() if requested Loading
test/CMakeLists.txt +8 −0 Changes for test/CMakeLists.txt: 8 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -328,3 +328,11 @@ add_test(NAME srtp_test COMMAND srtp_test) add_executable(dtls_harness dtls_harness.cpp) target_link_libraries(dtls_harness netplus-static) add_test(NAME dtls_openssl COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/dtls_openssl.sh $<TARGET_FILE:dtls_harness>) add_executable(source_address_test source_address_test.cpp) if(WIN32) target_link_libraries(source_address_test netplus-static ws2_32) else() target_link_libraries(source_address_test netplus-static) endif() add_test(NAME source_address_test COMMAND source_address_test)
test/source_address_test.cpp 0 → 100644 +59 −0 Changes for test/source_address_test.cpp: 59 added lines, 0 removed lines. Original line number Diff line number Diff line // tcp::setSourceAddress(): the outgoing connection leaves from the given local IP. #include <atomic> #include <iostream> #include <memory> #include <string> #include <thread> #include "socket.h" #include "exception.h" #ifdef __linux__ #include <unistd.h> #endif int main() { int failures = 0; #ifdef __linux__ // only Linux routes all of 127/8 to lo; BSDs configure just 127.0.0.1 const int port = 39000 + (static_cast<int>(::getpid()) % 2000); netplus::tcp server("127.0.0.1", port, 4, 0); server.bind(); server.listen(); std::string peer; std::thread acceptor([&] { std::unique_ptr<netplus::socket> c; server.accept(c, false); if (c) c->getAddress(peer); }); try { netplus::tcp client; client.setSourceAddress("127.0.0.2"); client.connect("127.0.0.1", port); } catch (const std::exception &e) { std::cerr << "connect with source address failed: " << e.what() << std::endl; ++failures; } acceptor.join(); if (peer.rfind("127.0.0.2", 0) != 0) { std::cerr << "server saw peer '" << peer << "', expected 127.0.0.2" << std::endl; ++failures; } // an address of the other family only: no destination left -> connect fails bool threw = false; try { netplus::tcp client; client.setSourceAddress("::1"); client.connect("127.0.0.1", port); } catch (const std::exception &) { threw = true; } if (!threw) { std::cerr << "IPv6 source to an IPv4-only destination should fail" << std::endl; ++failures; } #endif std::cout << (failures ? "FAILED" : "OK") << std::endl; return failures ? 1 : 0; }