-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #152.
- Loading branch information
Showing
10 changed files
with
744 additions
and
27 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package sctp | ||
|
||
import ( | ||
"encoding/binary" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
/* | ||
chunkShutdown represents an SCTP Chunk of type chunkShutdown | ||
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 = 7 | Chunk Flags | Length = 8 | | ||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| Cumulative TSN Ack | | ||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
*/ | ||
type chunkShutdown struct { | ||
chunkHeader | ||
cumulativeTSNAck uint32 | ||
} | ||
|
||
const ( | ||
cumulativeTSNAckLength = 4 | ||
) | ||
|
||
var ( | ||
errInvalidChunkSize = errors.New("invalid chunk size") | ||
errChunkTypeNotShutdown = errors.New("ChunkType is not of type SHUTDOWN") | ||
) | ||
|
||
func (c *chunkShutdown) unmarshal(raw []byte) error { | ||
if err := c.chunkHeader.unmarshal(raw); err != nil { | ||
return err | ||
} | ||
|
||
if c.typ != ctShutdown { | ||
return fmt.Errorf("%w: actually is %s", errChunkTypeNotShutdown, c.typ.String()) | ||
} | ||
|
||
if len(c.raw) != cumulativeTSNAckLength { | ||
return errInvalidChunkSize | ||
} | ||
|
||
c.cumulativeTSNAck = binary.BigEndian.Uint32(c.raw[0:]) | ||
|
||
return nil | ||
} | ||
|
||
func (c *chunkShutdown) marshal() ([]byte, error) { | ||
out := make([]byte, cumulativeTSNAckLength) | ||
binary.BigEndian.PutUint32(out[0:], c.cumulativeTSNAck) | ||
|
||
c.typ = ctShutdown | ||
c.raw = out | ||
return c.chunkHeader.marshal() | ||
} | ||
|
||
func (c *chunkShutdown) check() (abort bool, err error) { | ||
return false, nil | ||
} | ||
|
||
// String makes chunkShutdown printable | ||
func (c *chunkShutdown) String() string { | ||
return c.chunkHeader.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package sctp | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
/* | ||
chunkShutdownAck represents an SCTP Chunk of type chunkShutdownAck | ||
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 = 8 | Chunk Flags | Length = 4 | | ||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
*/ | ||
type chunkShutdownAck struct { | ||
chunkHeader | ||
} | ||
|
||
var errChunkTypeNotShutdownAck = errors.New("ChunkType is not of type SHUTDOWN-ACK") | ||
|
||
func (c *chunkShutdownAck) unmarshal(raw []byte) error { | ||
if err := c.chunkHeader.unmarshal(raw); err != nil { | ||
return err | ||
} | ||
|
||
if c.typ != ctShutdownAck { | ||
return fmt.Errorf("%w: actually is %s", errChunkTypeNotShutdownAck, c.typ.String()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *chunkShutdownAck) marshal() ([]byte, error) { | ||
c.typ = ctShutdownAck | ||
return c.chunkHeader.marshal() | ||
} | ||
|
||
func (c *chunkShutdownAck) check() (abort bool, err error) { | ||
return false, nil | ||
} | ||
|
||
// String makes chunkShutdownAck printable | ||
func (c *chunkShutdownAck) String() string { | ||
return c.chunkHeader.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package sctp | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestChunkShutdownAck_Success(t *testing.T) { | ||
tt := []struct { | ||
binary []byte | ||
}{ | ||
{[]byte{0x08, 0x00, 0x00, 0x04}}, | ||
} | ||
|
||
for i, tc := range tt { | ||
actual := &chunkShutdownAck{} | ||
err := actual.unmarshal(tc.binary) | ||
if err != nil { | ||
t.Fatalf("failed to unmarshal #%d: %v", i, err) | ||
} | ||
|
||
b, err := actual.marshal() | ||
if err != nil { | ||
t.Fatalf("failed to marshal: %v", err) | ||
} | ||
assert.Equal(t, tc.binary, b, "test %d not equal", i) | ||
} | ||
} | ||
|
||
func TestChunkShutdownAck_Failure(t *testing.T) { | ||
tt := []struct { | ||
name string | ||
binary []byte | ||
}{ | ||
{"length too short", []byte{0x08, 0x00, 0x00}}, | ||
{"length too long", []byte{0x08, 0x00, 0x00, 0x04, 0x12}}, | ||
{"invalid type", []byte{0x0f, 0x00, 0x00, 0x04}}, | ||
} | ||
|
||
for i, tc := range tt { | ||
actual := &chunkShutdownAck{} | ||
err := actual.unmarshal(tc.binary) | ||
if err == nil { | ||
t.Errorf("expected unmarshal #%d: '%s' to fail.", i, tc.name) | ||
} | ||
} | ||
} |
Oops, something went wrong.