Commit 2ab05905 authored by jan.koester's avatar jan.koester
Browse files

test

parent 9c1f774c
Loading
Loading
Loading
Loading
+43 −33
Changes for src/event/epoll.cpp: 43 added lines, 33 removed lines.
Original line number Diff line number Diff line
@@ -230,54 +230,56 @@ namespace netplus {

        void ReadEventHandler(int pos, const int tid, ULONG_PTR args) {
            con* rcon = reinterpret_cast<con*>(_Events[pos].data.ptr);
            buffer buf(BLOCKSIZE);
            if (!rcon || !rcon->csock) return;

            if (!rcon || !rcon->csock) {
                return;
            }
            std::lock_guard<std::mutex> lk(rcon->event_mutex);
            if (!_Events[pos].data.ptr || !rcon->csock) return;

            std::lock_guard<std::mutex> lock_guard(rcon->event_mutex);
            try {
                bool gotAny = false;

            if (!_Events[pos].data.ptr || !rcon->csock) { return; }
                for (;;) {
                    buffer buf(BLOCKSIZE);

            try{
                    size_t r = rcon->csock->recvData(buf, BLOCKSIZE); // kann Note werfen

                size_t rcvsize = rcon->csock->recvData(buf, BLOCKSIZE);
                    if (r == 0) {
                        break;
                    }

                    gotAny = true;
                    rcon->RecvData.append(buf.data.buf, r);
                    rcon->lasteventime = time(nullptr);

                if(rcvsize!=0){
                    rcon->RecvData.append(buf.data.buf,rcvsize);
                    if (r < (size_t)BLOCKSIZE) break;
                }

                if (gotAny) {
                    evconnection->RequestEvent(*rcon, tid, args);
                }

                if (!rcon->SendData.empty()) {
                    setpollEvents(pos, EPOLLOUT | EPOLLONESHOT);
                    return;
                }

                } else {
                    setpollEvents(pos, EPOLLIN | EPOLLONESHOT);
                }
                return;

            } catch (NetException& e) {
                if (e.getErrorType() == NetException::Note) {
                    setpollEvents(pos, EPOLLIN | EPOLLONESHOT);
                    return;
                }
                throw e;
                throw;
            }
        }
        };

       void WriteEventHandler(int pos, const int tid, ULONG_PTR args) {
            con* wcon = reinterpret_cast<con*>(_Events[pos].data.ptr);
            if (!wcon || !wcon->csock) return;

            if(!wcon ||  !wcon->csock){
                    return;
            }

            std::lock_guard<std::mutex> lock_guard(wcon->event_mutex);

            if (!_Events[pos].data.ptr || !wcon->csock) { return; }
            std::lock_guard<std::mutex> lk(wcon->event_mutex);
            if (!_Events[pos].data.ptr || !wcon->csock) return;

            try {
                evconnection->ResponseEvent(*wcon, tid, args);
@@ -287,31 +289,39 @@ namespace netplus {
                    return;
                }

                size_t ssize = BLOCKSIZE < wcon->SendData.size() ? BLOCKSIZE : wcon->SendData.size();
                while (!wcon->SendData.empty()) {
                    size_t toSend = std::min((size_t)BLOCKSIZE, wcon->SendData.size());

                buffer buf(wcon->SendData.data(),ssize);
                size_t sended = wcon->csock->sendData(buf);
                    buffer out(wcon->SendData.data(), toSend);

                if (sended > ssize) {
                    NetException e;
                    e[NetException::Note] << "WriteEventHandler: Fatal socket error detected. Closing connection.";
                    throw e;
                    size_t sent = wcon->csock->sendData(out);

                    if (sent == 0) {
                        setpollEvents(pos, EPOLLOUT | EPOLLONESHOT);
                        return;
                    }

                wcon->SendData.erase(wcon->SendData.begin(), wcon->SendData.begin() + sended);
                    wcon->SendData.erase(wcon->SendData.begin(),
                                        wcon->SendData.begin() + sent);

                    wcon->lasteventime = time(nullptr);

                setpollEvents(pos,EPOLLOUT | EPOLLONESHOT);
                    if (wcon->SendData.empty()) {
                        evconnection->ResponseEvent(*wcon, tid, args);
                    }
                }

                setpollEvents(pos, EPOLLIN | EPOLLONESHOT);
                return;

            } catch (NetException& e) {
                if (e.getErrorType() == NetException::Note) {
                    setpollEvents(pos, EPOLLOUT | EPOLLONESHOT);
                    return;
                }else{
                }
                throw e;
            }
        }
        };

        void CloseEventHandler(int pos, const int tid, ULONG_PTR args) {
            std::lock_guard<std::mutex> global_lock(POLL_HANDLER_MUTEX);
+7 −5
Changes for src/posix/tcp.cpp: 7 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -196,16 +196,18 @@ size_t netplus::tcp::sendData(buffer &data,int flags){

        if (errno == EAGAIN || errno == EWOULDBLOCK)
            etype = NetException::Note;
        else if (errno == EPIPE || errno == ECONNRESET)
            etype = NetException::Error;

        NetException ex;
        char errstr[512];
        strerror_r_netplus(errno, errstr, 512);

        exception[etype] << "Socket senddata failed on Socket: " << _Socket
                                       << " ErrorMsg: " <<  errstr;
        throw exception;
        ex[etype] << "Socket senddata failed on Socket: " << _Socket << " ErrorMsg: " << errstr;
        throw ex;
    }

    return (size_t) rval != 0 ? rval : data.size;

    return (size_t)rval;
}


+4 −0
Changes for src/socket.h: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -236,6 +236,10 @@ namespace netplus {
        uint64_t _send_seq = 0;
        uint64_t _recv_seq = 0;

        std::vector<uint8_t> _send_record;
        size_t _send_off = 0;


        std::vector<uint8_t> _handshake_transcript;

        std::vector<uint8_t> _sha1_hash(const std::vector<uint8_t>& input);
+59 −44
Changes for src/ssl.cpp: 59 added lines, 44 removed lines.
Original line number Diff line number Diff line
@@ -842,80 +842,95 @@ void netplus::ssl::connect(std::unique_ptr<socket> &csock) {
}

size_t netplus::ssl::sendData(buffer &data, int flags) {
    if (!_handshakeDone) return tcp::sendData(data, flags);
    auto _flushSendRecord = [&](int flags) {
        if (_send_record.empty()) return size_t(0);

    // 1) Record type: caller should set this correctly.
    // If your stack always calls sendData() for application data, keep 0x17.
    // For Finished (handshake) you should call a dedicated send that uses 0x16.
    uint8_t recordType = 0x17; // Application Data by default
        if (_send_off >= _send_record.size()) {
            _send_record.clear();
            _send_off = 0;
            return size_t(0);
        }

        const char* p = (const char*)_send_record.data() + _send_off;
        size_t left   = _send_record.size() - _send_off;

        buffer out(p, left); // ptr-buffer
        size_t n = tcp::sendData(out, flags); // kann NetException::Note werfen

        _send_off += n;

        if (_send_off == _send_record.size()) {
            // Record vollständig übertragen -> erst jetzt Sequenznummer erhöhen
            _send_seq++;
            _send_record.clear();
            _send_off = 0;
        }
        return n;
    };

    if (!_handshakeDone)
        return tcp::sendData(data, flags);

    // 1) Falls noch ein TLS-Record pending ist: nur flushen, KEINEN neuen bauen
    if (!_send_record.empty()) {
        _flushSendRecord(flags);   // bei EAGAIN => NetException::Note nach oben
        return 0;                  // keine neuen App-Bytes konsumiert
    }

    // If you DO want to support sending encrypted handshake records through this method,
    // pass the desired type in another parameter or set it via a member before calling.
    // (Don't try to infer it from the first byte; that breaks for HTTP/PNG etc.)
    // 2) RecordType NICHT raten – du nutzt hier AppData fest
    const uint8_t recordType = 0x17;

    // 2) Plaintext content
    // 3) Content (Application Bytes)
    std::vector<uint8_t> content(
        (const uint8_t*)data.data.buf,
        (const uint8_t*)data.data.buf + data.size
    );

    // 3) Compute HMAC over (seq_num || type || version || length || content)
    // Using server_write_MAC_key for sending
    // 4) MAC über seq + header + content
    std::vector<uint8_t> mac = _calculateHMAC(content, recordType, _send_seq, _mac_key);

    // 4) Build TLSInnerPlaintext = content || mac || padding
    // 5) inner = content || mac || padding
    std::vector<uint8_t> inner;
    inner.reserve(content.size() + mac.size() + 32);
    inner.insert(inner.end(), content.begin(), content.end());
    inner.insert(inner.end(), mac.begin(), mac.end());

    // 5) TLS 1.2 CBC padding: padLen = bytes_of_padding_excluding_last_byte
    // total padding bytes = padLen + 1, all bytes have value padLen
    constexpr size_t block = 16;
    size_t rem = inner.size() % block;
    size_t padBytes = (rem == 0) ? block : (block - rem); // must add at least 1 byte, even if aligned
    size_t padBytes = (rem == 0) ? block : (block - rem); // mind. 1 Block
    uint8_t padLen = (uint8_t)(padBytes - 1);
    inner.insert(inner.end(), padBytes, padLen);

    for (size_t i = 0; i < padBytes; ++i) inner.push_back(padLen);

    // Sanity: must be multiple of 16 for raw CBC
    if (inner.empty() || (inner.size() % block) != 0) {
        NetException e;
        e[NetException::Error] << "ssl::sendData: internal error (CBC plaintext not block-aligned)";
        throw e;
    }

    // 6) Explicit IV (TLS 1.1+ for CBC; TLS 1.2 uses explicit IV)
    // 6) Explicit IV
    std::vector<uint8_t> iv(16);
    {
        std::random_device rd;
        for (auto &b : iv) b = (uint8_t)(rd() & 0xFF);
    }
    { std::random_device rd; for (auto &b : iv) b = (uint8_t)(rd() & 0xFF); }

    // 7) Encrypt (CBC raw)
    // 7) Encrypt (raw CBC: plaintext MUSS block-aligned sein -> ist es)
    std::vector<uint8_t> ciphertext = _aes->encryptCBC(inner, iv);

    // 8) Build TLS record header + fragment
    // fragment = IV || ciphertext
    // 8) Build TLS record: header + IV + ciphertext
    uint16_t fragLen = (uint16_t)(iv.size() + ciphertext.size());

    std::vector<uint8_t> record;
    record.reserve(5 + fragLen);
    record.push_back(recordType);
    record.push_back(0x03); record.push_back(0x03);      // TLS 1.2
    record.push_back((fragLen >> 8) & 0xFF);
    record.push_back(fragLen & 0xFF);
    _send_record.clear();
    _send_record.reserve(5 + fragLen);
    _send_record.push_back(recordType);
    _send_record.push_back(0x03); _send_record.push_back(0x03);
    _send_record.push_back((fragLen >> 8) & 0xFF);
    _send_record.push_back(fragLen & 0xFF);
    _send_record.insert(_send_record.end(), iv.begin(), iv.end());
    _send_record.insert(_send_record.end(), ciphertext.begin(), ciphertext.end());

    record.insert(record.end(), iv.begin(), iv.end());
    record.insert(record.end(), ciphertext.begin(), ciphertext.end());
    _send_off = 0;

    // 9) Increment sequence number AFTER using it
    _send_seq++;
    // 9) Direkt versuchen zu senden (kann partial werden)
    _flushSendRecord(flags);

    buffer out((const char*)record.data(), record.size());
    return tcp::sendData(out, flags);
    // WICHTIG für deinen WriteEventHandler:
    // Wir haben die App-Bytes in einen TLS-Record übernommen => App-Bytes sind "konsumiert".
    return data.size;
}


size_t netplus::ssl::recvData(buffer &data, int flags) {
    if (!_handshakeDone) return tcp::recvData(data, flags);