status-go/vendor/github.com/pion/sctp/chunk_abort.go

94 lines
2.4 KiB
Go
Raw Normal View History

2022-03-10 09:44:48 +00:00
package sctp // nolint:dupl
import (
"errors"
"fmt"
)
/*
Abort represents an SCTP Chunk of type ABORT
The ABORT chunk is sent to the peer of an association to close the
association. The ABORT chunk may contain Cause Parameters to inform
the receiver about the reason of the abort. DATA chunks MUST NOT be
bundled with ABORT. Control chunks (except for INIT, INIT ACK, and
SHUTDOWN COMPLETE) MAY be bundled with an ABORT, but they MUST be
placed before the ABORT in the SCTP packet or they will be ignored by
the receiver.
2024-01-03 21:57:33 +00:00
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type = 6 |Reserved |T| Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| zero or more Error Causes |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2022-03-10 09:44:48 +00:00
*/
type chunkAbort struct {
chunkHeader
errorCauses []errorCause
}
2024-01-03 21:57:33 +00:00
// Abort chunk errors
2022-03-10 09:44:48 +00:00
var (
2024-01-03 21:57:33 +00:00
ErrChunkTypeNotAbort = errors.New("ChunkType is not of type ABORT")
ErrBuildAbortChunkFailed = errors.New("failed build Abort Chunk")
2022-03-10 09:44:48 +00:00
)
func (a *chunkAbort) unmarshal(raw []byte) error {
if err := a.chunkHeader.unmarshal(raw); err != nil {
return err
}
if a.typ != ctAbort {
2024-01-03 21:57:33 +00:00
return fmt.Errorf("%w: actually is %s", ErrChunkTypeNotAbort, a.typ.String())
2022-03-10 09:44:48 +00:00
}
offset := chunkHeaderSize
for {
if len(raw)-offset < 4 {
break
}
e, err := buildErrorCause(raw[offset:])
if err != nil {
2024-01-03 21:57:33 +00:00
return fmt.Errorf("%w: %v", ErrBuildAbortChunkFailed, err) //nolint:errorlint
2022-03-10 09:44:48 +00:00
}
offset += int(e.length())
a.errorCauses = append(a.errorCauses, e)
}
return nil
}
func (a *chunkAbort) marshal() ([]byte, error) {
a.chunkHeader.typ = ctAbort
a.flags = 0x00
a.raw = []byte{}
for _, ec := range a.errorCauses {
raw, err := ec.marshal()
if err != nil {
return nil, err
}
a.raw = append(a.raw, raw...)
}
return a.chunkHeader.marshal()
}
func (a *chunkAbort) check() (abort bool, err error) {
return false, nil
}
// String makes chunkAbort printable
func (a *chunkAbort) String() string {
res := a.chunkHeader.String()
for _, cause := range a.errorCauses {
res += fmt.Sprintf("\n - %s", cause)
}
return res
}