ExtractDCIDAndSCID extracts both DCID and SCID from a Long Header packet. Returns (dcid, scid, error).
(packet []byte)
| 154 | // ExtractDCIDAndSCID extracts both DCID and SCID from a Long Header packet. |
| 155 | // Returns (dcid, scid, error). |
| 156 | func ExtractDCIDAndSCID(packet []byte) ([]byte, []byte, error) { |
| 157 | if len(packet) < 1 { |
| 158 | return nil, nil, errors.New("packet too short") |
| 159 | } |
| 160 | |
| 161 | // Short Header has no SCID |
| 162 | if packet[0]&0x80 == 0 { |
| 163 | return nil, nil, errors.New("short header") |
| 164 | } |
| 165 | |
| 166 | if len(packet) < 6 { |
| 167 | return nil, nil, errors.New("packet too short for long header") |
| 168 | } |
| 169 | |
| 170 | offset := 5 // Skip first byte + version (4 bytes) |
| 171 | dcidLen := int(packet[offset]) |
| 172 | offset++ |
| 173 | |
| 174 | if offset+dcidLen > len(packet) { |
| 175 | return nil, nil, errors.New("packet too short for DCID") |
| 176 | } |
| 177 | dcid := packet[offset : offset+dcidLen] |
| 178 | offset += dcidLen |
| 179 | |
| 180 | if offset >= len(packet) { |
| 181 | return nil, nil, errors.New("packet too short for SCID length") |
| 182 | } |
| 183 | |
| 184 | scidLen := int(packet[offset]) |
| 185 | offset++ |
| 186 | |
| 187 | if offset+scidLen > len(packet) { |
| 188 | return nil, nil, errors.New("packet too short for SCID") |
| 189 | } |
| 190 | scid := packet[offset : offset+scidLen] |
| 191 | |
| 192 | return dcid, scid, nil |
| 193 | } |
| 194 | |
| 195 | // ExtractAllSCIDs extracts SCIDs from all coalesced packets in a UDP datagram. |
| 196 | // QUIC allows multiple packets to be coalesced in a single datagram. |