Commit 97d177a9 authored by jan.koester's avatar jan.koester
Browse files

windows: skip repeated bind/listen, report Winsock error codes

tcp/udp::bind() and tcp::listen() on Windows had no _Bound/_Listening
guard, unlike POSIX. Callers that bind before the event loop (blogi)
got a second ::bind() from iocp event init, which fails with WSAEINVAL
on every port ("tcp::bind failed"). Also append WSAGetLastError() with
a short hint to bind/listen/socket-creation errors.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent 3bb42289
Loading
Loading
Loading
Loading
+30 −3
Changes for src/windows/tcp.cpp: 30 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -30,6 +30,27 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "eventapi.h"
#include <ws2tcpip.h>
#include <cstring>
#include <string>


namespace {
// readable form of a Winsock error for exception text: "10048 WSAEADDRINUSE (...)"
std::string wsaErrorText(int err) {
    const char* hint = nullptr;
    switch (err) {
    case WSAEADDRINUSE:    hint = "WSAEADDRINUSE: address/port already in use"; break;
    case WSAEACCES:        hint = "WSAEACCES: port reserved or access denied (see netsh int ipv4 show excludedportrange protocol=tcp)"; break;
    case WSAEADDRNOTAVAIL: hint = "WSAEADDRNOTAVAIL: address not available on this host"; break;
    case WSAEINVAL:        hint = "WSAEINVAL: socket already bound"; break;
    case WSAENOTSOCK:      hint = "WSAENOTSOCK: not a socket"; break;
    case WSANOTINITIALISED:hint = "WSANOTINITIALISED: WSAStartup not called"; break;
    default: break;
    }
    std::string s = std::to_string(err);
    if (hint) { s += " "; s += hint; }
    return s;
}
}

namespace netplus {

@@ -82,7 +103,7 @@ tcp::tcp(const std::string& addr, int port, int maxconnections, int sockopts)

    if (_Socket == INVALID_SOCKET) {
        NetException e;
        e[NetException::Critical] << "Failed to create TCP socket";
        e[NetException::Critical] << "Failed to create TCP socket: " << wsaErrorText(::WSAGetLastError());
        throw e;
    }

@@ -98,19 +119,25 @@ tcp::tcp(const std::string& uxsocket, int maxconnections, int sockopts)
}

void tcp::bind() {
    // already bound by the caller (blogi binds before the event loop, which binds again):
    // a second ::bind() on Windows fails with WSAEINVAL
    if (_Bound) return;
    if (::bind(_Socket, reinterpret_cast<sockaddr*>(&_Addr), _AddrLen) == SOCKET_ERROR) {
        NetException e;
        e[NetException::Error] << "tcp::bind failed";
        e[NetException::Error] << "tcp::bind failed: " << wsaErrorText(::WSAGetLastError());
        throw e;
    }
    _Bound = true;
}

void tcp::listen() {
    if (_Listening) return;
    if (::listen(_Socket, _Maxconnections) == SOCKET_ERROR) {
        NetException e;
        e[NetException::Error] << "tcp::listen failed";
        e[NetException::Error] << "tcp::listen failed: " << wsaErrorText(::WSAGetLastError());
        throw e;
    }
    _Listening = true;
}

void tcp::setNonBlock() {
+27 −2
Changes for src/windows/udp.cpp: 27 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -30,6 +30,27 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "eventapi.h"
#include <ws2tcpip.h>
#include <cstring>
#include <string>


namespace {
// readable form of a Winsock error for exception text: "10048 WSAEADDRINUSE (...)"
std::string wsaErrorText(int err) {
    const char* hint = nullptr;
    switch (err) {
    case WSAEADDRINUSE:    hint = "WSAEADDRINUSE: address/port already in use"; break;
    case WSAEACCES:        hint = "WSAEACCES: port reserved or access denied (see netsh int ipv4 show excludedportrange protocol=tcp)"; break;
    case WSAEADDRNOTAVAIL: hint = "WSAEADDRNOTAVAIL: address not available on this host"; break;
    case WSAEINVAL:        hint = "WSAEINVAL: socket already bound"; break;
    case WSAENOTSOCK:      hint = "WSAENOTSOCK: not a socket"; break;
    case WSANOTINITIALISED:hint = "WSANOTINITIALISED: WSAStartup not called"; break;
    default: break;
    }
    std::string s = std::to_string(err);
    if (hint) { s += " "; s += hint; }
    return s;
}
}

namespace netplus {

@@ -82,7 +103,7 @@ udp::udp(const std::string& addr, int port, int maxconnections, int sockopts)

    if (_Socket == INVALID_SOCKET) {
        NetException e;
        e[NetException::Critical] << "Failed to create UDP socket";
        e[NetException::Critical] << "Failed to create UDP socket: " << wsaErrorText(::WSAGetLastError());
        throw e;
    }

@@ -98,11 +119,15 @@ udp::udp(const std::string& uxsocket, int maxconnections, int sockopts)
}

void udp::bind() {
    // already bound by the caller (blogi binds before the event loop, which binds again):
    // a second ::bind() on Windows fails with WSAEINVAL
    if (_Bound) return;
    if (::bind(_Socket, reinterpret_cast<sockaddr*>(&_Addr), _AddrLen) == SOCKET_ERROR) {
        NetException e;
        e[NetException::Error] << "udp::bind failed";
        e[NetException::Error] << "udp::bind failed: " << wsaErrorText(::WSAGetLastError());
        throw e;
    }
    _Bound = true;
}

void udp::listen() {