github.com/apernet/quic-go@v0.43.1-0.20240515053213-5e9e635fd9f0/qlog/packet_header.go (about)

     1  package qlog
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/apernet/quic-go/internal/protocol"
     7  	"github.com/apernet/quic-go/logging"
     8  
     9  	"github.com/francoispqt/gojay"
    10  )
    11  
    12  func getPacketTypeFromEncryptionLevel(encLevel protocol.EncryptionLevel) logging.PacketType {
    13  	switch encLevel {
    14  	case protocol.EncryptionInitial:
    15  		return logging.PacketTypeInitial
    16  	case protocol.EncryptionHandshake:
    17  		return logging.PacketTypeHandshake
    18  	case protocol.Encryption0RTT:
    19  		return logging.PacketType0RTT
    20  	case protocol.Encryption1RTT:
    21  		return logging.PacketType1RTT
    22  	default:
    23  		panic("unknown encryption level")
    24  	}
    25  }
    26  
    27  type token struct {
    28  	Raw []byte
    29  }
    30  
    31  var _ gojay.MarshalerJSONObject = &token{}
    32  
    33  func (t token) IsNil() bool { return false }
    34  func (t token) MarshalJSONObject(enc *gojay.Encoder) {
    35  	enc.StringKey("data", fmt.Sprintf("%x", t.Raw))
    36  }
    37  
    38  // PacketHeader is a QUIC packet header.
    39  // TODO: make this a long header
    40  type packetHeader struct {
    41  	PacketType logging.PacketType
    42  
    43  	KeyPhaseBit  logging.KeyPhaseBit
    44  	PacketNumber logging.PacketNumber
    45  
    46  	Version          logging.VersionNumber
    47  	SrcConnectionID  logging.ConnectionID
    48  	DestConnectionID logging.ConnectionID
    49  
    50  	Token *token
    51  }
    52  
    53  func transformHeader(hdr *logging.Header) *packetHeader {
    54  	h := &packetHeader{
    55  		PacketType:       logging.PacketTypeFromHeader(hdr),
    56  		SrcConnectionID:  hdr.SrcConnectionID,
    57  		DestConnectionID: hdr.DestConnectionID,
    58  		Version:          hdr.Version,
    59  	}
    60  	if len(hdr.Token) > 0 {
    61  		h.Token = &token{Raw: hdr.Token}
    62  	}
    63  	return h
    64  }
    65  
    66  func transformLongHeader(hdr *logging.ExtendedHeader) *packetHeader {
    67  	h := transformHeader(&hdr.Header)
    68  	h.PacketNumber = hdr.PacketNumber
    69  	h.KeyPhaseBit = hdr.KeyPhase
    70  	return h
    71  }
    72  
    73  func (h packetHeader) MarshalJSONObject(enc *gojay.Encoder) {
    74  	enc.StringKey("packet_type", packetType(h.PacketType).String())
    75  	if h.PacketType != logging.PacketTypeRetry {
    76  		enc.Int64Key("packet_number", int64(h.PacketNumber))
    77  	}
    78  	if h.Version != 0 {
    79  		enc.StringKey("version", versionNumber(h.Version).String())
    80  	}
    81  	if h.PacketType != logging.PacketType1RTT {
    82  		enc.IntKey("scil", h.SrcConnectionID.Len())
    83  		if h.SrcConnectionID.Len() > 0 {
    84  			enc.StringKey("scid", h.SrcConnectionID.String())
    85  		}
    86  	}
    87  	enc.IntKey("dcil", h.DestConnectionID.Len())
    88  	if h.DestConnectionID.Len() > 0 {
    89  		enc.StringKey("dcid", h.DestConnectionID.String())
    90  	}
    91  	if h.KeyPhaseBit == logging.KeyPhaseZero || h.KeyPhaseBit == logging.KeyPhaseOne {
    92  		enc.StringKey("key_phase_bit", h.KeyPhaseBit.String())
    93  	}
    94  	if h.Token != nil {
    95  		enc.ObjectKey("token", h.Token)
    96  	}
    97  }
    98  
    99  type packetHeaderVersionNegotiation struct {
   100  	SrcConnectionID  logging.ArbitraryLenConnectionID
   101  	DestConnectionID logging.ArbitraryLenConnectionID
   102  }
   103  
   104  func (h packetHeaderVersionNegotiation) IsNil() bool { return false }
   105  func (h packetHeaderVersionNegotiation) MarshalJSONObject(enc *gojay.Encoder) {
   106  	enc.StringKey("packet_type", "version_negotiation")
   107  	enc.IntKey("scil", h.SrcConnectionID.Len())
   108  	enc.StringKey("scid", h.SrcConnectionID.String())
   109  	enc.IntKey("dcil", h.DestConnectionID.Len())
   110  	enc.StringKey("dcid", h.DestConnectionID.String())
   111  }
   112  
   113  // a minimal header that only outputs the packet type, and potentially a packet number
   114  type packetHeaderWithType struct {
   115  	PacketType   logging.PacketType
   116  	PacketNumber logging.PacketNumber
   117  }
   118  
   119  func (h packetHeaderWithType) IsNil() bool { return false }
   120  func (h packetHeaderWithType) MarshalJSONObject(enc *gojay.Encoder) {
   121  	enc.StringKey("packet_type", packetType(h.PacketType).String())
   122  	if h.PacketNumber != protocol.InvalidPacketNumber {
   123  		enc.Int64Key("packet_number", int64(h.PacketNumber))
   124  	}
   125  }
   126  
   127  // a minimal header that only outputs the packet type
   128  type packetHeaderWithTypeAndPacketNumber struct {
   129  	PacketType   logging.PacketType
   130  	PacketNumber logging.PacketNumber
   131  }
   132  
   133  func (h packetHeaderWithTypeAndPacketNumber) IsNil() bool { return false }
   134  func (h packetHeaderWithTypeAndPacketNumber) MarshalJSONObject(enc *gojay.Encoder) {
   135  	enc.StringKey("packet_type", packetType(h.PacketType).String())
   136  	enc.Int64Key("packet_number", int64(h.PacketNumber))
   137  }
   138  
   139  type shortHeader struct {
   140  	DestConnectionID logging.ConnectionID
   141  	PacketNumber     logging.PacketNumber
   142  	KeyPhaseBit      logging.KeyPhaseBit
   143  }
   144  
   145  func transformShortHeader(hdr *logging.ShortHeader) *shortHeader {
   146  	return &shortHeader{
   147  		DestConnectionID: hdr.DestConnectionID,
   148  		PacketNumber:     hdr.PacketNumber,
   149  		KeyPhaseBit:      hdr.KeyPhase,
   150  	}
   151  }
   152  
   153  func (h shortHeader) IsNil() bool { return false }
   154  func (h shortHeader) MarshalJSONObject(enc *gojay.Encoder) {
   155  	enc.StringKey("packet_type", packetType(logging.PacketType1RTT).String())
   156  	if h.DestConnectionID.Len() > 0 {
   157  		enc.StringKey("dcid", h.DestConnectionID.String())
   158  	}
   159  	enc.Int64Key("packet_number", int64(h.PacketNumber))
   160  	enc.StringKey("key_phase_bit", h.KeyPhaseBit.String())
   161  }