package recordlayer
import (
"encoding/binary"
"github.com/pion/dtls/v2/pkg/protocol"
"github.com/pion/dtls/v2/pkg/protocol/alert"
"github.com/pion/dtls/v2/pkg/protocol/handshake"
)
type RecordLayer struct {
Header Header
Content protocol .Content
}
func (r *RecordLayer ) Marshal () ([]byte , error ) {
contentRaw , err := r .Content .Marshal ()
if err != nil {
return nil , err
}
r .Header .ContentLen = uint16 (len (contentRaw ))
r .Header .ContentType = r .Content .ContentType ()
headerRaw , err := r .Header .Marshal ()
if err != nil {
return nil , err
}
return append (headerRaw , contentRaw ...), nil
}
func (r *RecordLayer ) Unmarshal (data []byte ) error {
if len (data ) < HeaderSize {
return errBufferTooSmall
}
if err := r .Header .Unmarshal (data ); err != nil {
return err
}
switch protocol .ContentType (data [0 ]) {
case protocol .ContentTypeChangeCipherSpec :
r .Content = &protocol .ChangeCipherSpec {}
case protocol .ContentTypeAlert :
r .Content = &alert .Alert {}
case protocol .ContentTypeHandshake :
r .Content = &handshake .Handshake {}
case protocol .ContentTypeApplicationData :
r .Content = &protocol .ApplicationData {}
default :
return errInvalidContentType
}
return r .Content .Unmarshal (data [HeaderSize :])
}
func UnpackDatagram (buf []byte ) ([][]byte , error ) {
out := [][]byte {}
for offset := 0 ; len (buf ) != offset ; {
if len (buf )-offset <= HeaderSize {
return nil , ErrInvalidPacketLength
}
pktLen := (HeaderSize + int (binary .BigEndian .Uint16 (buf [offset +11 :])))
if offset +pktLen > len (buf ) {
return nil , ErrInvalidPacketLength
}
out = append (out , buf [offset :offset +pktLen ])
offset += pktLen
}
return out , 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 .