package handshake
import (
"encoding/binary"
"github.com/pion/dtls/v2/pkg/protocol"
"github.com/pion/dtls/v2/pkg/protocol/extension"
)
type MessageServerHello struct {
Version protocol .Version
Random Random
SessionID []byte
CipherSuiteID *uint16
CompressionMethod *protocol .CompressionMethod
Extensions []extension .Extension
}
const messageServerHelloVariableWidthStart = 2 + RandomLength
func (m MessageServerHello ) Type () Type {
return TypeServerHello
}
func (m *MessageServerHello ) Marshal () ([]byte , error ) {
if m .CipherSuiteID == nil {
return nil , errCipherSuiteUnset
} else if m .CompressionMethod == nil {
return nil , errCompressionMethodUnset
}
out := make ([]byte , messageServerHelloVariableWidthStart )
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 {0x00 , 0x00 }...)
binary .BigEndian .PutUint16 (out [len (out )-2 :], *m .CipherSuiteID )
out = append (out , byte (m .CompressionMethod .ID ))
extensions , err := extension .Marshal (m .Extensions )
if err != nil {
return nil , err
}
return append (out , extensions ...), nil
}
func (m *MessageServerHello ) 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 := messageServerHelloVariableWidthStart
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 )
if len (data ) < currOffset +2 {
return errBufferTooSmall
}
m .CipherSuiteID = new (uint16 )
*m .CipherSuiteID = binary .BigEndian .Uint16 (data [currOffset :])
currOffset += 2
if len (data ) <= currOffset {
return errBufferTooSmall
}
if compressionMethod , ok := protocol .CompressionMethods ()[protocol .CompressionMethodID (data [currOffset ])]; ok {
m .CompressionMethod = compressionMethod
currOffset ++
} else {
return errInvalidCompressionMethod
}
if len (data ) <= currOffset {
m .Extensions = []extension .Extension {}
return nil
}
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 .