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

win comp

parent 23742014
Loading
Loading
Loading
Loading
+4 −3
Changes for src/crypto/sha.cpp: 4 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -183,12 +183,13 @@ std::vector<uint8_t> netplus::sha384_hash(const std::vector<uint8_t>& input) {

    // Pre-processing (Padding)
    std::vector<uint8_t> msg = input;
    __uint128_t bitLen = (__uint128_t)input.size() * 8;
    uint64_t bitLen = (uint64_t)input.size() * 8;
    msg.push_back(0x80);
    while ((msg.size() + 16) % 128 != 0) msg.push_back(0x00);

    // Append length as 128-bit big-endian
    for (int i = 15; i >= 0; --i) msg.push_back((uint8_t)((bitLen >> (i * 8)) & 0xFF));
    // Append length as 128-bit big-endian (high 64 bits are 0 for practical sizes)
    for (int i = 7; i >= 0; --i) msg.push_back(0x00);
    for (int i = 7; i >= 0; --i) msg.push_back((uint8_t)((bitLen >> (i * 8)) & 0xFF));

    // Process in 1024-bit blocks
    for (size_t offset = 0; offset < msg.size(); offset += 128) {
+74 −97
Changes for src/quic.cpp: 74 added lines, 97 removed lines.
Original line number Diff line number Diff line
@@ -38,6 +38,8 @@
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <thread>
#include <chrono>

namespace netplus {

@@ -170,29 +172,12 @@ ssize_t quic::sendPacket(const uint8_t* data, size_t len) {
        return -1;
    }
    
    // If we have a peer address stored (child connection), use sendto
    if (_AddrLen > 0 && _Addr.ss_family != 0) {
        if (_Addr.ss_family == AF_INET) {
            struct sockaddr_in* addr_in = reinterpret_cast<struct sockaddr_in*>(&_Addr);
            char ip_str[INET_ADDRSTRLEN];
            inet_ntop(AF_INET, &addr_in->sin_addr, ip_str, INET_ADDRSTRLEN);
        }
        
        // Verify socket is still valid before sending
        int sock_val = 0;
        socklen_t sock_val_len = sizeof(sock_val);
        if (::getsockopt(_Socket, SOL_SOCKET, SO_ERROR, (void*)&sock_val, &sock_val_len) < 0) {
    try {
        buffer send_buf(reinterpret_cast<const char*>(data), len);
        return static_cast<ssize_t>(udp::sendData(send_buf, 0));
    } catch (...) {
        return -1;
    }
        
        ssize_t sent = ::sendto(_Socket, reinterpret_cast<const char*>(data), len, 0,
                        reinterpret_cast<const sockaddr*>(&_Addr), _AddrLen);
        if (sent < 0) {
        }
        return sent;
    }
    // Otherwise use send (for connected sockets or when no peer address)
    return ::send(_Socket, reinterpret_cast<const char*>(data), len, 0);
}

// ============================================================================
@@ -898,14 +883,17 @@ void quic::handshake_after_accept() {
    
    // Try to receive more data if handshake not complete
    if (!_handshake_complete && _Socket >= 0) {
        std::vector<uint8_t> recv_buf(65535);
        ssize_t recv_len = ::recv(_Socket, reinterpret_cast<char*>(recv_buf.data()),
                                   recv_buf.size(), MSG_DONTWAIT);
        
        buffer recv_buf(65535);
        try {
            size_t recv_len = udp::recvData(recv_buf, MSG_DONTWAIT);
            if (recv_len > 0) {
            processIncomingPacket(recv_buf.data(), static_cast<size_t>(recv_len));
        } else if (recv_len < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
        } else {
                processIncomingPacket(reinterpret_cast<const uint8_t*>(recv_buf.data.buf),
                                     recv_len);
            }
        } catch (NetException&) {
            // recvData throws on EAGAIN/EWOULDBLOCK or errors
        }
        {
            
            // If no data on child socket and parent exists, check if parent has data for us
            if (_parent != nullptr) {
@@ -953,23 +941,29 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) {
    
    // Receive UDP datagram
    ssize_t recv_len = ::recvfrom(_Socket, reinterpret_cast<char*>(dgram.data()), 
                                   dgram.size(), nonblock ? MSG_DONTWAIT : 0,
                                   static_cast<int>(dgram.size()), nonblock ? MSG_DONTWAIT : 0,
                                   reinterpret_cast<sockaddr*>(&peer_addr), &peer_len);
    
    
    if (recv_len < 0) {
#ifdef Windows
        int err = ::WSAGetLastError();
        if (err == WSAEWOULDBLOCK) {
            csock = nullptr;
            return;
        }
#else
        if (errno == EAGAIN || errno == EWOULDBLOCK) {
            // No data available in non-blocking mode
            csock = nullptr;
            return;
        }
        // Additional debugging for socket errors
        if (errno == EBADF) {
            csock = nullptr;
            return;
        }
#endif
        NetException e;
        e[NetException::Error] << "quic::accept recvfrom failed: " << strerror(errno) << " (errno=" << errno << ", FD=" << _Socket << ")";
        e[NetException::Error] << "quic::accept recvfrom failed";
        throw e;
    }
    
@@ -1602,7 +1596,11 @@ void quic::close() {
    // Close underlying UDP socket (only if we own it, not if we're a child)
    // Child connections share the parent's socket and should not close it
    if (_Socket >= 0 && _parent == nullptr) {
#ifdef Windows
        ::closesocket(_Socket);
#else
        ::close(_Socket);
#endif
        _Socket = -1;
    }
    
@@ -1830,7 +1828,11 @@ void quic::connect(const std::string& addr, int port, bool nonblock) {
            break;
        }
        
#ifdef Windows
        ::closesocket(_Socket);
#else
        ::close(_Socket);
#endif
        _Socket = -1;
    }
    
@@ -1878,12 +1880,9 @@ void quic::connect(const std::string& addr, int port, bool nonblock) {
    applyHeaderProtection(packet, _initial_hp_client, true);
    
    // Send Initial packet
    ssize_t sent = ::send(_Socket, reinterpret_cast<const char*>(packet.data()),
                          packet.size(), 0);
    if (sent < 0) {
        NetException e;
        e[NetException::Error] << "quic::connect: send failed: " << strerror(errno);
        throw e;
    {
        buffer send_buf(reinterpret_cast<const char*>(packet.data()), packet.size());
        udp::sendData(send_buf, 0);
    }
    
    // If blocking mode, wait for ServerHello and complete handshake
@@ -2131,24 +2130,18 @@ std::vector<uint8_t> quic::buildCryptoFrame(const std::vector<uint8_t>& data, ui

void quic::completeHandshake() {
    // Receive and process packets until handshake is complete
    std::vector<uint8_t> recv_buf(65535);
    
    int max_attempts = 100;  // Prevent infinite loop
    while (!_handshake_complete && max_attempts-- > 0) {
        ssize_t recv_len = ::recv(_Socket, reinterpret_cast<char*>(recv_buf.data()),
                                   recv_buf.size(), 0);
        
        if (recv_len < 0) {
            if (errno == EAGAIN || errno == EWOULDBLOCK) {
                continue;  // Retry
            }
            NetException e;
            e[NetException::Error] << "quic::completeHandshake: recv failed: " << strerror(errno);
            throw e;
        }
        
        try {
            buffer recv_buf_wrapper(65535);
            size_t recv_len = udp::recvData(recv_buf_wrapper, 0);
            if (recv_len > 0) {
            processIncomingPacket(recv_buf.data(), static_cast<size_t>(recv_len));
                processIncomingPacket(reinterpret_cast<const uint8_t*>(recv_buf_wrapper.data.buf),
                                     recv_len);
            }
        } catch (NetException&) {
            continue;  // EAGAIN or other transient error, retry
        }
    }
    
@@ -2204,21 +2197,15 @@ size_t quic::recvData(buffer& data, int flags) {
    }
    
    // No buffered data, try to receive from network
    std::vector<uint8_t> recv_buf(65535);
    int recv_flags = (flags & MSG_DONTWAIT) ? MSG_DONTWAIT : 0;
    
    ssize_t recv_len = ::recv(_Socket, reinterpret_cast<char*>(recv_buf.data()),
                               recv_buf.size(), recv_flags);
    
    if (recv_len < 0) {
        if (errno == EAGAIN || errno == EWOULDBLOCK) {
            NetException e;
            e[NetException::Note] << "quic::recvData would block";
            throw e;
        }
        NetException e;
        e[NetException::Error] << "quic::recvData: recv failed: " << strerror(errno);
        throw e;
    size_t recv_len;
    buffer recv_buf_wrapper(65535);
    try {
        recv_len = udp::recvData(recv_buf_wrapper, recv_flags);
    } catch (NetException&) {
        // EAGAIN/EWOULDBLOCK - propagate as-is
        throw;
    }
    
    if (recv_len == 0) {
@@ -2228,7 +2215,7 @@ size_t quic::recvData(buffer& data, int flags) {
    }
    
    // Process the received packet
    processIncomingPacket(recv_buf.data(), static_cast<size_t>(recv_len));
    processIncomingPacket(reinterpret_cast<const uint8_t*>(recv_buf_wrapper.data.buf), recv_len);
    
    // After processing, check again for buffered stream data
    it = _streams.find(default_stream);
@@ -2876,7 +2863,7 @@ void quic::sendStreamData(uint64_t stream_id, const std::vector<uint8_t>& data,
        ssize_t sent = sendPacket(packet.data(), packet.size());
        if (sent < 0) {
            // Retry once after brief pause (UDP send buffer may be full)
            usleep(100);
            std::this_thread::sleep_for(std::chrono::microseconds(100));
            sent = sendPacket(packet.data(), packet.size());
            if (sent < 0) {
                return;
@@ -2916,7 +2903,7 @@ void quic::sendMaxStreamData(uint64_t stream_id, uint64_t max_data) {
        std::cerr << "[QUIC-FC] sendMaxStreamData sid=" << stream_id
                  << " limit=" << max_data << " FAILED, retrying" << std::endl;
        // Flow control frames are critical — retry once
        usleep(100);
        std::this_thread::sleep_for(std::chrono::microseconds(100));
        sent = sendPacket(packet.data(), packet.size());
        if (sent < 0) {
            std::cerr << "[QUIC-FC] sendMaxStreamData RETRY FAILED" << std::endl;
@@ -2943,7 +2930,7 @@ void quic::sendMaxData(uint64_t max_data) {
        std::cerr << "[QUIC-FC] sendMaxData limit=" << max_data
                  << " FAILED, retrying" << std::endl;
        // Flow control frames are critical — retry once
        usleep(100);
        std::this_thread::sleep_for(std::chrono::microseconds(100));
        sent = sendPacket(packet.data(), packet.size());
        if (sent < 0) {
            std::cerr << "[QUIC-FC] sendMaxData RETRY FAILED" << std::endl;
@@ -3460,7 +3447,11 @@ size_t quic::sendData(buffer& data, int flags) {
    }
    
    // Get data pointer
#ifdef Windows
    const char* send_ptr = data.data.buf;
#else
    const char* send_ptr = data.ptr ? data.data.ptr : data.data.buf;
#endif
    size_t send_len = data.size;
    
    if (send_len == 0) {
@@ -3513,18 +3504,9 @@ size_t quic::sendData(buffer& data, int flags) {
    
    // Send packet
    int send_flags = (flags & MSG_DONTWAIT) ? MSG_DONTWAIT : 0;
    ssize_t sent = ::send(_Socket, reinterpret_cast<const char*>(packet.data()),
                          packet.size(), send_flags);
    
    if (sent < 0) {
        if (errno == EAGAIN || errno == EWOULDBLOCK) {
            NetException e;
            e[NetException::Note] << "quic::sendData would block";
            throw e;
        }
        NetException e;
        e[NetException::Error] << "quic::sendData: send failed: " << strerror(errno);
        throw e;
    {
        buffer send_buf(reinterpret_cast<const char*>(packet.data()), packet.size());
        udp::sendData(send_buf, send_flags);
    }
    
    // Update offsets
@@ -3579,10 +3561,10 @@ size_t quic::sendStreamData(uint64_t stream_id, const uint8_t* data, size_t len,
    const uint8_t* hp_key = _is_server ? _app_hp_server : _app_hp_client;
    applyHeaderProtection(packet, hp_key);
    
    ssize_t sent = ::send(_Socket, reinterpret_cast<const char*>(packet.data()),
                          packet.size(), 0);
    
    if (sent < 0) {
    try {
        buffer send_buf(reinterpret_cast<const char*>(packet.data()), packet.size());
        udp::sendData(send_buf, 0);
    } catch (...) {
        return 0;
    }
    
@@ -3722,16 +3704,13 @@ void quic::flush_out() {
    while (!_send_queue.empty()) {
        auto& packet = _send_queue.front();
        
        ssize_t sent = ::send(_Socket, reinterpret_cast<const char*>(packet.data()),
                              packet.size(), MSG_DONTWAIT);
        
        if (sent < 0) {
            if (errno == EAGAIN || errno == EWOULDBLOCK) {
                // Socket buffer full, stop trying
        try {
            buffer send_buf(reinterpret_cast<const char*>(packet.data()), packet.size());
            udp::sendData(send_buf, MSG_DONTWAIT);
        } catch (...) {
            // Socket buffer full or error - stop trying
            break;
        }
            // Error - discard packet and continue
        }
        
        _send_queue.pop_front();
    }
@@ -3798,8 +3777,7 @@ void quic::closeStream(uint64_t stream_id) {
        applyHeaderProtection(packet, hp_key);
        
        if (_Socket >= 0) {
            ::send(_Socket, reinterpret_cast<const char*>(packet.data()),
                   packet.size(), 0);
            sendPacket(packet.data(), packet.size());
        }
        
        stream.send_fin = true;
@@ -3841,8 +3819,7 @@ void quic::sendClientHello() {
    
    // Send Initial packet
    if (_Socket >= 0) {
        ::send(_Socket, reinterpret_cast<const char*>(packet.data()),
               packet.size(), 0);
        sendPacket(packet.data(), packet.size());
    }
}

+12 −0
Changes for src/socket.h: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -60,9 +60,17 @@
	#include <errno.h>
#else
	#define WIN32_LEAN_AND_MEAN
	#ifndef NOMINMAX
	#define NOMINMAX
	#endif
	#include <WinSock2.h>
	#include <ws2tcpip.h>
	#include <mswsock.h>
	#include <BaseTsd.h>
	typedef SSIZE_T ssize_t;
	#ifndef MSG_DONTWAIT
	#define MSG_DONTWAIT 0
	#endif
#endif

namespace netplus {
@@ -529,6 +537,10 @@ namespace netplus {
		// Socket interface overrides
		void flush_out() override;
		void accept(std::unique_ptr<socket>& csock, bool nonblock) override;
#ifdef Windows
		void accept(LPFN_ACCEPTEX lpfnAcceptEx, std::unique_ptr<socket>& csock) override;
		void prime_read() override;
#endif
		void connect(const std::string& addr, int port, bool nonblock = false) override;
		size_t sendData(buffer& data, int flags = 0) override;
		size_t recvData(buffer& data, int flags = 0) override;
+1 −1
Changes for test/https_test.cpp: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ enum H2Flags : uint8_t {

// HTTP/2 Error Codes
enum class H2Error : uint32_t {
    NO_ERROR            = 0x00,
    H2_NO_ERROR         = 0x00,
    PROTOCOL_ERROR      = 0x01,
    INTERNAL_ERROR      = 0x02,
    FLOW_CONTROL_ERROR  = 0x03,