github.com/tumi8/quic-go@v0.37.4-tum/qlog/packet_header.go (about)

     1  package qlog
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/tumi8/quic-go/noninternal/protocol"
     7  	"github.com/tumi8/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
   114  type packetHeaderWithType struct {
   115  	PacketType logging.PacketType
   116  }
   117  
   118  func (h packetHeaderWithType) IsNil() bool { return false }
   119  func (h packetHeaderWithType) MarshalJSONObject(enc *gojay.Encoder) {
   120  	enc.StringKey("packet_type", packetType(h.PacketType).String())
   121  }
   122  
   123  // a minimal header that only outputs the packet type
   124  type packetHeaderWithTypeAndPacketNumber struct {
   125  	PacketType   logging.PacketType
   126  	PacketNumber logging.PacketNumber
   127  }
   128  
   129  func (h packetHeaderWithTypeAndPacketNumber) IsNil() bool { return false }
   130  func (h packetHeaderWithTypeAndPacketNumber) MarshalJSONObject(enc *gojay.Encoder) {
   131  	enc.StringKey("packet_type", packetType(h.PacketType).String())
   132  	enc.Int64Key("packet_number", int64(h.PacketNumber))
   133  }
   134  
   135  type shortHeader struct {
   136  	DestConnectionID logging.ConnectionID
   137  	PacketNumber     logging.PacketNumber
   138  	KeyPhaseBit      logging.KeyPhaseBit
   139  }
   140  
   141  func transformShortHeader(hdr *logging.ShortHeader) *shortHeader {
   142  	return &shortHeader{
   143  		DestConnectionID: hdr.DestConnectionID,
   144  		PacketNumber:     hdr.PacketNumber,
   145  		KeyPhaseBit:      hdr.KeyPhase,
   146  	}
   147  }
   148  
   149  func (h shortHeader) IsNil() bool { return false }
   150  func (h shortHeader) MarshalJSONObject(enc *gojay.Encoder) {
   151  	enc.StringKey("packet_type", packetType(logging.PacketType1RTT).String())
   152  	if h.DestConnectionID.Len() > 0 {
   153  		enc.StringKey("dcid", h.DestConnectionID.String())
   154  	}
   155  	enc.Int64Key("packet_number", int64(h.PacketNumber))
   156  	enc.StringKey("key_phase_bit", h.KeyPhaseBit.String())
   157  }