package handshake
import (
"encoding/binary"
"github.com/pion/dtls/v2/pkg/protocol"
"github.com/pion/dtls/v2/pkg/protocol/extension"
)
type MessageClientHello struct {
Version protocol .Version
Random Random
Cookie []byte
SessionID []byte
CipherSuiteIDs []uint16
CompressionMethods []*protocol .CompressionMethod
Extensions []extension .Extension
}
const handshakeMessageClientHelloVariableWidthStart = 34
func (m MessageClientHello ) Type () Type {
return TypeClientHello
}
func (m *MessageClientHello ) Marshal () ([]byte , error ) {
if len (m .Cookie ) > 255 {
return nil , errCookieTooLong
}
out := make ([]byte , handshakeMessageClientHelloVariableWidthStart )
out [0 ] = m .Version .Major
out [1 ] = m .Version .Minor
rand := m .Random .MarshalFixed ()
copy (out [2 :], rand [:])
out = append (out , byte (len (m .SessionID )))
out = append (out , m .SessionID ...)
out = append (out , byte (len (m .Cookie )))
out = append (out , m .Cookie ...)
out = append (out , encodeCipherSuiteIDs (m .CipherSuiteIDs )...)
out = append (out , protocol .EncodeCompressionMethods (m .CompressionMethods )...)
extensions , err := extension .Marshal (m .Extensions )
if err != nil {
return nil , err
}
return append (out , extensions ...), nil
}
func (m *MessageClientHello ) Unmarshal (data []byte ) error {
if len (data ) < 2 +RandomLength {
return errBufferTooSmall
}
m .Version .Major = data [0 ]
m .Version .Minor = data [1 ]
var random [RandomLength ]byte
copy (random [:], data [2 :])
m .Random .UnmarshalFixed (random )
currOffset := handshakeMessageClientHelloVariableWidthStart
currOffset ++
if len (data ) <= currOffset {
return errBufferTooSmall
}
n := int (data [currOffset -1 ])
if len (data ) <= currOffset +n {
return errBufferTooSmall
}
m .SessionID = append ([]byte {}, data [currOffset :currOffset +n ]...)
currOffset += len (m .SessionID )
currOffset ++
if len (data ) <= currOffset {
return errBufferTooSmall
}
n = int (data [currOffset -1 ])
if len (data ) <= currOffset +n {
return errBufferTooSmall
}
m .Cookie = append ([]byte {}, data [currOffset :currOffset +n ]...)
currOffset += len (m .Cookie )
if len (data ) < currOffset {
return errBufferTooSmall
}
cipherSuiteIDs , err := decodeCipherSuiteIDs (data [currOffset :])
if err != nil {
return err
}
m .CipherSuiteIDs = cipherSuiteIDs
if len (data ) < currOffset +2 {
return errBufferTooSmall
}
currOffset += int (binary .BigEndian .Uint16 (data [currOffset :])) + 2
if len (data ) < currOffset {
return errBufferTooSmall
}
compressionMethods , err := protocol .DecodeCompressionMethods (data [currOffset :])
if err != nil {
return err
}
m .CompressionMethods = compressionMethods
if len (data ) < currOffset {
return errBufferTooSmall
}
currOffset += int (data [currOffset ]) + 1
extensions , err := extension .Unmarshal (data [currOffset :])
if err != nil {
return err
}
m .Extensions = extensions
return nil
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .