Skip to content

Commit

Permalink
Add SCTP status test
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeyanev committed Dec 16, 2024
1 parent 26befb3 commit 0741d1d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
24 changes: 7 additions & 17 deletions addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
package sctp

import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"golang.org/x/sys/unix"
"net"
"strconv"
"strings"
"unsafe"
)
Expand All @@ -31,23 +29,15 @@ func (a *SCTPAddr) String() string {
if a == nil {
return "<nil>"
}

var b bytes.Buffer
for n, i := range a.IPAddrs {
if i.IP.To4() != nil {
b.WriteString(i.String())
} else if i.IP.To16() != nil {
b.WriteRune('[')
b.WriteString(i.String())
b.WriteRune(']')
}
if n < len(a.IPAddrs)-1 {
b.WriteRune('/')
var ipStrings []string
for _, ipAddr := range a.IPAddrs {
if ipAddr.IP.To4() != nil {
ipStrings = append(ipStrings, ipAddr.String())
} else if ipAddr.IP.To16() != nil {
ipStrings = append(ipStrings, "["+ipAddr.String()+"]")
}
}
b.WriteRune(':')
b.WriteString(strconv.Itoa(a.Port))
return b.String()
return strings.Join(ipStrings, "/") + fmt.Sprintf(":%d", a.Port)
}

// Wildcard addresses cannot be used in combination with non-wildcard addresses.
Expand Down
7 changes: 3 additions & 4 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type conn struct {
// Since os.File is used for integration with the poller, os.ErrClosed
// should be checked instead of net.ErrClosed.
//
// If specific SCTP features are needed, use the ReadMsg() functions.
// If specific SCTP features are needed, use SCTPConn.ReadMsg.
func (c *conn) Read(b []byte) (int, error) {
if !c.ok() {
return 0, unix.EINVAL
Expand Down Expand Up @@ -65,7 +65,7 @@ func (c *conn) Read(b []byte) (int, error) {
// Write return errors from `os` package so check for os.ErrClosed instead
// of net.ErrClosed.
//
// If specific SCTP features are needed, use the ReadMsg() functions.
// If specific SCTP features are needed, use SCTPConn.WriteMsg.
func (c *conn) Write(b []byte) (int, error) {
if !c.ok() {
return 0, unix.EINVAL
Expand Down Expand Up @@ -114,8 +114,7 @@ func (c *conn) SetWriteDeadline(t time.Time) error {
// By default, a graceful close is performed and
// a SHUTDOWN message is sent to the peer.
// If different behaviour is desired (i.e. immediate
// close and sending ABORT chunk), use the SCTPConn.SetLinger
// function.
// close and sending ABORT chunk), use SCTPConn.SetLinger
// Also, different close behaviour can be achieved by using
// the SndInfo flags appropriately.
func (c *conn) Close() error {
Expand Down
1 change: 0 additions & 1 deletion sctp.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ const (
SCTP_INACTIVE = 0
)

// TODO: Get Assoc Status (SCTP_STATUS)
// TODO: Add heartbeat management (SCTP_PEER_ADDR_PARAMS)
// TODO: Cookie (SCTP_ASSOCINFO)
// TODO: Auth and SPA (WriteMsgSpa, WriteMsgSpaExt, Read equivalents)
50 changes: 49 additions & 1 deletion sctp_conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"testing"
)

// Test AssocChange even
func TestPartialRead(t *testing.T) {
ln1, err := Listen("sctp4", "127.0.0.1:0")
if err != nil {
Expand Down Expand Up @@ -119,6 +118,55 @@ func TestPartialRead(t *testing.T) {
}
}
}
func TestSCTPStatus(t *testing.T) {
ln1, err := Listen("sctp4", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
closeChan := make(chan struct{})
errorChan := make(chan error)
go func() {
c, err1 := ln1.Accept()
if err1 != nil {
errorChan <- err1
return
}
defer func(c net.Conn) {
c.Close()
close(closeChan)
}(c)

c1 := c.(*SCTPConn)
sctpStatus, err1 := c1.Status()
if err1 != nil {
errorChan <- err1
}
// remote ostreams are 11, ours in are 10, remote Instreams are 9, ours out are 10
if sctpStatus.InStreams != 10 || sctpStatus.OutStreams != 9 {
errorChan <- errors.New("expected 10 inStreams and 9 outStreams")
}
}()
d := Dialer{
InitOptions: InitOptions{
NumOstreams: 11,
MaxInstreams: 9,
},
}
c, err := d.Dial("sctp4", ln1.Addr().String())
if err != nil {
log.Fatal(err)
}

select {
case <-closeChan:
c.Close()
case err := <-errorChan:
c.Close()
if err != io.EOF {
t.Fatal(err)
}
}
}

func TestHtonui32Ntohui32(t *testing.T) {
var a uint32 = 0x01020304
Expand Down

0 comments on commit 0741d1d

Please sign in to comment.