package sctp
import (
"encoding/binary"
"errors"
"fmt"
)
type chunkForwardTSN struct {
chunkHeader
newCumulativeTSN uint32
streams []chunkForwardTSNStream
}
const (
newCumulativeTSNLength = 4
forwardTSNStreamLength = 4
)
var (
ErrMarshalStreamFailed = errors .New ("failed to marshal stream" )
ErrChunkTooShort = errors .New ("chunk too short" )
)
func (c *chunkForwardTSN ) unmarshal (raw []byte ) error {
if err := c .chunkHeader .unmarshal (raw ); err != nil {
return err
}
if len (c .raw ) < newCumulativeTSNLength {
return ErrChunkTooShort
}
c .newCumulativeTSN = binary .BigEndian .Uint32 (c .raw [0 :])
offset := newCumulativeTSNLength
remaining := len (c .raw ) - offset
for remaining > 0 {
s := chunkForwardTSNStream {}
if err := s .unmarshal (c .raw [offset :]); err != nil {
return fmt .Errorf ("%w: %v" , ErrMarshalStreamFailed , err )
}
c .streams = append (c .streams , s )
offset += s .length ()
remaining -= s .length ()
}
return nil
}
func (c *chunkForwardTSN ) marshal () ([]byte , error ) {
out := make ([]byte , newCumulativeTSNLength )
binary .BigEndian .PutUint32 (out [0 :], c .newCumulativeTSN )
for _ , s := range c .streams {
b , err := s .marshal ()
if err != nil {
return nil , fmt .Errorf ("%w: %v" , ErrMarshalStreamFailed , err )
}
out = append (out , b ...)
}
c .typ = ctForwardTSN
c .raw = out
return c .chunkHeader .marshal ()
}
func (c *chunkForwardTSN ) check () (abort bool , err error ) {
return true , nil
}
func (c *chunkForwardTSN ) String () string {
res := fmt .Sprintf ("New Cumulative TSN: %d\n" , c .newCumulativeTSN )
for _ , s := range c .streams {
res += fmt .Sprintf (" - si=%d, ssn=%d\n" , s .identifier , s .sequence )
}
return res
}
type chunkForwardTSNStream struct {
identifier uint16
sequence uint16
}
func (s *chunkForwardTSNStream ) length () int {
return forwardTSNStreamLength
}
func (s *chunkForwardTSNStream ) unmarshal (raw []byte ) error {
if len (raw ) < forwardTSNStreamLength {
return ErrChunkTooShort
}
s .identifier = binary .BigEndian .Uint16 (raw [0 :])
s .sequence = binary .BigEndian .Uint16 (raw [2 :])
return nil
}
func (s *chunkForwardTSNStream ) marshal () ([]byte , error ) {
out := make ([]byte , forwardTSNStreamLength )
binary .BigEndian .PutUint16 (out [0 :], s .identifier )
binary .BigEndian .PutUint16 (out [2 :], s .sequence )
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 .