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

fixed connect windows

parent 6786e2a5
Loading
Loading
Loading
Loading

server_err.txt

0 → 100644
+20867 −0

File added.

Preview size limit exceeded, changes collapsed.

server_err2.txt

0 → 100644
+0 −0

Empty file added.

server_out.txt

0 → 100644
+2 −0
Original line number Diff line number Diff line
Certificate validity: OK.
/
+55 −15
Original line number Diff line number Diff line
@@ -361,24 +361,52 @@ netplus::ssl::ssl(std::shared_ptr<netplus::x509cert> cert, int sock) :
std::vector<uint8_t> netplus::ssl::readTlsRecordAsync()
{
    // Caller must hold con::event_mutex to protect _rx_tcp_buf
#if SSL_DEBUG
    std::cerr << "[SSL] readTlsRecordAsync: entry _rx_tcp_buf.size()=" << _rx_tcp_buf.size() << std::endl;
#endif
    
    // ----------------------------------------------------------------
    // For blocking client mode, try to read data from socket if buffer is empty
    // ----------------------------------------------------------------
    auto tryReadFromSocket = [this]() -> bool {
        // Only do blocking reads for client-side connections
        if (!_is_client) return false;
        
        buffer tmp(16384);
        try {
            size_t n = tcp::recvData(tmp, 0);
            if (n > 0) {
                _rx_tcp_buf.insert(_rx_tcp_buf.end(), 
                    (uint8_t*)tmp.data.buf, 
                    (uint8_t*)tmp.data.buf + n);
                return true;
            }
        } catch (NetException& e) {
            if (e.getErrorType() == NetException::Note) {
                return false; // would block - no data available
            }
            throw;
        }
        return false;
    };

    // ----------------------------------------------------------------
    // 1) ensure we have at least TLS record header (5 bytes)
    // On Windows/IOCP: data arrives via pushReceivedData(), just check buffer
    // On Linux/epoll: would need to call tcp::recvData() - but that's handled
    // by the event loop before calling ssl::recvData()
    // For blocking client: try to read from socket
    // ----------------------------------------------------------------
    if (_rx_tcp_buf.size() < 5) {
#if SSL_DEBUG
        std::cerr << "[SSL] readTlsRecordAsync: need header, only have " << _rx_tcp_buf.size() << " bytes" << std::endl;
#endif
    while (_rx_tcp_buf.size() < 5) {
        if (_is_client) {
            if (!tryReadFromSocket()) {
                NetException n;
                n[NetException::Note] << "ssl: record incomplete (need header)";
                throw n;
            }
        } else {
            NetException n;
            n[NetException::Note] << "ssl: record incomplete (need header)";
            throw n;
        }
    }

    // ------------------------------------------------------------
    // 2) parse record length
@@ -392,8 +420,11 @@ std::vector<uint8_t> netplus::ssl::readTlsRecordAsync()
              
    // ------------------------------------------------------------
    // 3) ensure full record is present
    // For blocking client: try to read more data from socket
    // ------------------------------------------------------------
    if (_rx_tcp_buf.size() < total) {
    while (_rx_tcp_buf.size() < total) {
        if (_is_client) {
            if (!tryReadFromSocket()) {
#if SSL_DEBUG
                std::cerr << "[SSL] readTlsRecordAsync: need " << total << " bytes, only have " << _rx_tcp_buf.size() << std::endl;
#endif
@@ -401,6 +432,15 @@ std::vector<uint8_t> netplus::ssl::readTlsRecordAsync()
                n[NetException::Note] << "ssl: record incomplete";
                throw n;
            }
        } else {
#if SSL_DEBUG
            std::cerr << "[SSL] readTlsRecordAsync: need " << total << " bytes, only have " << _rx_tcp_buf.size() << std::endl;
#endif
            NetException n;
            n[NetException::Note] << "ssl: record incomplete";
            throw n;
        }
    }

    // ------------------------------------------------------------
    // 4) extract full record
+36 −6
Original line number Diff line number Diff line
@@ -201,14 +201,44 @@ size_t tcp::recvData(buffer& data, int flags) {
        return r;
    }

    // No data available - throw Note to let IOCP handler post the recv
    // This prevents race conditions where we post a recv while IOCP is already processing one
    std::cerr << "[TCP] recvData: no data, throwing Note" << std::endl;
    // Use WSARecv for blocking receive on client sockets
    WSABUF wsaBuf;
    wsaBuf.buf = data.data.buf;
    wsaBuf.len = (ULONG)data.size;
    
    DWORD bytesRecv = 0;
    DWORD dwFlags = (DWORD)flags;
    
    int ret = ::WSARecv(_Socket, &wsaBuf, 1, &bytesRecv, &dwFlags, nullptr, nullptr);
    
    if (ret == 0) {
        // Success
        if (bytesRecv > 0) {
            std::cerr << "[TCP] recvData: WSARecv returned " << bytesRecv << " bytes" << std::endl;
            return (size_t)bytesRecv;
        }
        // bytesRecv == 0 means connection closed
        std::cerr << "[TCP] recvData: connection closed" << std::endl;
        NetException e;
        e[NetException::Error] << "tcp::recvData: connection closed";
        throw e;
    }
    
    int err = ::WSAGetLastError();
    if (err == WSAEWOULDBLOCK) {
        // Non-blocking socket with no data - throw Note
        std::cerr << "[TCP] recvData: WSAEWOULDBLOCK, throwing Note" << std::endl;
        NetException e;
        e[NetException::Note] << "tcp::recvData: no data available";
        throw e;
    }
    
    std::cerr << "[TCP] recvData: WSARecv error " << err << std::endl;
    NetException e;
    e[NetException::Error] << "tcp::recvData: WSARecv failed with error " << err;
    throw e;
}

void netplus::tcp::connect(const std::string& addr, int port, bool nonblock)
{
    NetException exception;
@@ -267,9 +297,9 @@ void netplus::tcp::connect(const std::string& addr, int port, bool nonblock)
        }

        // -------------------------------------------------------
        // 3) connect()
        // 3) connect() - use WSAConnect for IOCP socket compatibility
        // -------------------------------------------------------
        if (::connect(_Socket, rp->ai_addr, (int)rp->ai_addrlen) == 0) {
        if (::WSAConnect(_Socket, rp->ai_addr, (int)rp->ai_addrlen, nullptr, nullptr, nullptr, nullptr) == 0) {
            // connected immediately
            setAddrFromAI(rp);