TUN-5299: Send/receive QUIC datagram from edge and proxy to origin as UDP

This commit is contained in:
cthuang
2021-11-14 11:18:05 +00:00
committed by Arég Harutyunyan
parent fc2333c934
commit dd32dc1364
5 changed files with 220 additions and 20 deletions

38
quic/datagram.go Normal file
View File

@@ -0,0 +1,38 @@
package quic
import (
"fmt"
"github.com/google/uuid"
)
const (
sessionIDLen = len(uuid.UUID{})
MaxDatagramFrameSize = 1220
)
// Each QUIC datagram should be suffixed with session ID.
// ExtractSessionID extracts the session ID and a slice with only the payload
func ExtractSessionID(b []byte) (uuid.UUID, []byte, error) {
msgLen := len(b)
if msgLen < sessionIDLen {
return uuid.Nil, nil, fmt.Errorf("session ID has %d bytes, but data only has %d", sessionIDLen, len(b))
}
// Parse last 16 bytess as UUID and remove it from slice
sessionID, err := uuid.FromBytes(b[len(b)-sessionIDLen:])
if err != nil {
return uuid.Nil, nil, err
}
b = b[:len(b)-sessionIDLen]
return sessionID, b, nil
}
// SuffixSessionID appends the session ID at the end of the payload. Suffix is more performant than prefix because
// the payload slice might already have enough capacity to append the session ID at the end
func SuffixSessionID(sessionID uuid.UUID, b []byte) ([]byte, error) {
if len(b)+len(sessionID) > MaxDatagramFrameSize {
return nil, fmt.Errorf("datagram size exceed %d", MaxDatagramFrameSize)
}
b = append(b, sessionID[:]...)
return b, nil
}