Commit b5a76f9a authored by jan.koester's avatar jan.koester
Browse files

Revert tcp::setSourceAddress()

Not needed (proxyplus gets postfix-style proxy_interfaces instead), and the
new tcp member was an ABI break for every libnetplus dependent.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent 0fa567e2
Loading
Loading
Loading
Loading
+0 −60
Changes for src/posix/tcp.cpp: 0 added lines, 60 removed lines.
Original line number Diff line number Diff line
@@ -356,30 +356,6 @@ 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;
@@ -418,29 +394,11 @@ 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
@@ -587,29 +545,11 @@ 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();
+0 −9
Changes for src/socket.h: 0 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -384,20 +384,11 @@ 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
+0 −42
Changes for src/windows/tcp.cpp: 0 added lines, 42 removed lines.
Original line number Diff line number Diff line
@@ -273,30 +273,6 @@ 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;
@@ -335,29 +311,11 @@ 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
+0 −8
Changes for test/CMakeLists.txt: 0 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -328,11 +328,3 @@ 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

deleted100644 → 0
+0 −59
Changes for test/source_address_test.cpp: 0 added lines, 59 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;
}