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

test

parent af4cd7f2
Loading
Loading
Loading
Loading
+26 −9
Changes for src/quic.cpp: 26 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -2361,7 +2361,13 @@ void quic::processIncomingPacket(const uint8_t* data, size_t len) {
            // without enough trailing bytes to cover that inflated length,
            // past the buffer entirely.
            size_t packet_len = pkt_offset + payload_len - offset;
            processLongHeaderPacket(data + offset, packet_len);
            // pkt_offset (absolute) is exactly where the packet-number
            // field starts, i.e. this packet's pn_offset relative to
            // `data + offset` — hand it down so processInitialPacket/
            // processHandshakePacket/unprotectPacket don't each re-walk the
            // DCID/SCID/token/length fields just parsed above (F08).
            size_t pn_offset_hint = pkt_offset - offset;
            processLongHeaderPacket(data + offset, packet_len, pn_offset_hint);
            offset += packet_len;
        } else {
            // Short header packet - rest of datagram is one packet
@@ -2390,7 +2396,7 @@ void quic::processIncomingPacket(const uint8_t* data, size_t len) {
    }
}

void quic::processLongHeaderPacket(const uint8_t* data, size_t len) {
void quic::processLongHeaderPacket(const uint8_t* data, size_t len, size_t pn_offset_hint) {
    if (len < 7) return;

    uint8_t packet_type = (data[0] & 0x30) >> 4;
@@ -2399,13 +2405,13 @@ void quic::processLongHeaderPacket(const uint8_t* data, size_t len) {

    switch (packet_type) {
        case 0x00:  // Initial
            processInitialPacket(data, len);
            processInitialPacket(data, len, pn_offset_hint);
            break;
        case 0x01:  // 0-RTT
            // TODO: Implement 0-RTT handling
            break;
        case 0x02:  // Handshake
            processHandshakePacket(data, len);
            processHandshakePacket(data, len, pn_offset_hint);
            break;
        case 0x03:  // Retry
            processRetryPacket(data, len);
@@ -2483,11 +2489,22 @@ void quic::processShortHeaderPacket(const uint8_t* data, size_t len) {
    processApplicationPacket(data, len);
}

void quic::processInitialPacket(const uint8_t* data, size_t len) {
    // First, parse the header to get the actual packet length (for coalesced packets)
void quic::processInitialPacket(const uint8_t* data, size_t len, size_t pn_offset_hint) {
    if (len < 20) return;
    QUIC_DBG("processInitialPacket: len=%zu", len);

    // packet_len equals `len` whenever the caller already sliced `data` to
    // exactly one packet (true whenever it also supplied a valid hint —
    // see processIncomingPacket) — only the fallback path below needs to
    // (re)derive it from a `len` that may still cover trailing coalesced
    // bytes.
    size_t packet_len = len;

    if (pn_offset_hint == SIZE_MAX || pn_offset_hint > len) {
        // Fallback: caller couldn't determine the packet-number offset
        // itself (e.g. a truncated/malformed coalesced-packet edge case in
        // processIncomingPacket) — parse the header ourselves, identical to
        // the original unconditional logic.
        size_t offset = 5;  // After first byte + version
        if (offset >= len) return;
        uint8_t dcid_len = data[offset++];
@@ -2515,8 +2532,8 @@ void quic::processInitialPacket(const uint8_t* data, size_t len) {
        if (payload_len > len - offset) return;

        // Total packet length = header length (offset) + payload length
    size_t packet_len = offset + payload_len;
    
        packet_len = offset + payload_len;
    }

    // Decrypt and process Initial packet
    std::vector<uint8_t>& header = _recv_header_scratch;
@@ -2524,7 +2541,7 @@ void quic::processInitialPacket(const uint8_t* data, size_t len) {
    uint64_t pn;

    // Initial packets always use AES-128 - use actual packet_len, not full datagram len
    if (!unprotectPacket(data, packet_len, header, payload, pn, EncryptionLevel::Initial, _initial_keys.recv)) {
    if (!unprotectPacket(data, packet_len, header, payload, pn, EncryptionLevel::Initial, _initial_keys.recv, pn_offset_hint)) {
        QUIC_DBG("processInitialPacket: decrypt FAILED");
        return;
    }
+19 −4
Changes for src/socket.h: 19 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -815,11 +815,22 @@ namespace netplus {
		};

		// Packet processing
		//
		// `pn_offset_hint` (default SIZE_MAX = unknown): when the caller has
		// already parsed this long-header packet's DCID/SCID/token/length
		// fields once (processIncomingPacket does, to find packet
		// boundaries in a possibly-coalesced datagram), it passes the
		// resulting packet-number field offset here so processInitialPacket/
		// processHandshakePacket/unprotectPacket don't each walk the same
		// fields again from scratch. Falls back to the old self-contained
		// parse when omitted or out of range — used for the "incomplete/
		// malformed header" edge cases where processIncomingPacket itself
		// couldn't determine it.
		void processIncomingPacket(const uint8_t* data, size_t len);
		void processLongHeaderPacket(const uint8_t* data, size_t len);
		void processLongHeaderPacket(const uint8_t* data, size_t len, size_t pn_offset_hint = SIZE_MAX);
		void processShortHeaderPacket(const uint8_t* data, size_t len);
		void processInitialPacket(const uint8_t* data, size_t len);
		void processHandshakePacket(const uint8_t* data, size_t len);
		void processInitialPacket(const uint8_t* data, size_t len, size_t pn_offset_hint = SIZE_MAX);
		void processHandshakePacket(const uint8_t* data, size_t len, size_t pn_offset_hint = SIZE_MAX);
		void processApplicationPacket(const uint8_t* data, size_t len);
		// Client-side: handle an incoming Retry packet (RFC 9000 §8.1.2) —
		// verify its RFC 9001 §5.8 Integrity Tag, capture the token + new
@@ -923,11 +934,15 @@ namespace netplus {
		                        uint64_t packet_number,
		                        EncryptionLevel level, LevelKeyMaterial& mat,
		                        std::vector<uint8_t>& out);
		// `pn_offset_hint`: see processLongHeaderPacket()'s comment — reuses
		// an already-parsed long-header packet-number offset instead of
		// re-walking DCID/SCID/token/length fields when the caller has one.
		bool unprotectPacket(const uint8_t* data, size_t len,
		                     std::vector<uint8_t>& header,
		                     std::vector<uint8_t>& payload,
		                     uint64_t& packet_number,
		                     EncryptionLevel level, LevelKeyMaterial& mat);
		                     EncryptionLevel level, LevelKeyMaterial& mat,
		                     size_t pn_offset_hint = SIZE_MAX);

		// Header protection (send direction only — decrypt-side header
		// protection removal is handled inline inside unprotectPacket()).