Skip to content

Commit

Permalink
Added DialAssociation and ListenAssociation
Browse files Browse the repository at this point in the history
These are helpers to easily run SCTP over UDP. Example adapted.
I added the Association postfix to keep space for a potential
Dial/Listen implementation that directly gives you a Stream.

Relates to #74
  • Loading branch information
backkem committed Jun 21, 2020
1 parent 6f6d053 commit 8784f3e
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 95 deletions.
13 changes: 13 additions & 0 deletions association.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,19 @@ func Server(config Config) (*Association, error) {
}
}

// DialAssociation connects to the given network address and establishes a
// SCTP association on top. The net.Conn in the config is ignored.
func DialAssociation(network string, raddr *net.UDPAddr, config Config) (*Association, error) {
pConn, err := net.DialUDP(network, nil, raddr)
if err != nil {
return nil, err
}

config.NetConn = pConn

return Client(config)
}

// Client opens a SCTP stream over a conn
func Client(config Config) (*Association, error) {
a := createAssociation(config)
Expand Down
4 changes: 2 additions & 2 deletions examples/ping-pong/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
all: ping pong

ping: ping.go conn.go
ping: ping.go
go build -o $@

pong: pong.go conn.go
pong: pong.go
go build -o $@ -tags $@
72 changes: 0 additions & 72 deletions examples/ping-pong/conn.go

This file was deleted.

11 changes: 3 additions & 8 deletions examples/ping-pong/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,13 @@ import (
)

func main() {
conn, err := net.Dial("udp", "127.0.0.1:5678")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
fmt.Println("dialed udp ponger")
// Prepare the IP to connect to
addr := &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 5678}

config := sctp.Config{
NetConn: conn,
LoggerFactory: logging.NewDefaultLoggerFactory(),
}
a, err := sctp.Client(config)
a, err := sctp.DialAssociation("udp", addr, config)
if err != nil {
log.Fatal(err)
}
Expand Down
24 changes: 11 additions & 13 deletions examples/ping-pong/pong.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,27 @@ import (
)

func main() {
addr := net.UDPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 5678,
}
addr := &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 5678}

conn, err := net.ListenUDP("udp", &addr)
config := sctp.Config{
LoggerFactory: logging.NewDefaultLoggerFactory(),
}
l, err := sctp.ListenAssociation("udp", addr, config)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
fmt.Println("created a udp listener")
defer l.Close()
fmt.Println("created a listener")

config := sctp.Config{
NetConn: &disconnectedPacketConn{pConn: conn},
LoggerFactory: logging.NewDefaultLoggerFactory(),
}
a, err := sctp.Server(config)
// Note: You should accept all incoming associations in a loop.
a, err := l.Accept()
if err != nil {
log.Fatal(err)
}
defer a.Close()
fmt.Println("created a server")
fmt.Println("accepted an association")

// Note: You should accept all incoming streams in a loop.
stream, err := a.AcceptStream()
if err != nil {
log.Fatal(err)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require (
github.com/kr/pretty v0.1.0 // indirect
github.com/pion/logging v0.2.2
github.com/pion/transport v0.10.0
github.com/pion/udp v0.1.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.5.1
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ github.com/pion/logging v0.2.2 h1:M9+AIj/+pxNsDfAT64+MAVgJO0rsyLnoJKCqf//DoeY=
github.com/pion/logging v0.2.2/go.mod h1:k0/tDVsRCX2Mb2ZEmTqNa7CWsQPc+YYCB7Q+5pahoms=
github.com/pion/transport v0.10.0 h1:9M12BSneJm6ggGhJyWpDveFOstJsTiQjkLf4M44rm80=
github.com/pion/transport v0.10.0/go.mod h1:BnHnUipd0rZQyTVB2SBGojFHT9CBt5C5TcsJSQGkvSE=
github.com/pion/udp v0.1.0 h1:uGxQsNyrqG3GLINv36Ff60covYmfrLoxzwnCsIYspXI=
github.com/pion/udp v0.1.0/go.mod h1:BPELIjbwE9PRbd/zxI/KYBnbo7B6+oA6YuEaNE8lths=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
59 changes: 59 additions & 0 deletions listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package sctp

import (
"net"

"github.com/pion/udp"
)

// ListenAssociation creates a SCTP association listener
func ListenAssociation(network string, laddr *net.UDPAddr, config Config) (*AssociationListener, error) {
lc := udp.ListenConfig{}
parent, err := lc.Listen(network, laddr)
if err != nil {
return nil, err
}
return &AssociationListener{
config: config,
parent: parent,
}, nil
}

// NewAssociationListener creates a SCTP association listener
// which accepts connections from an inner Listener.
// The net.Conn in the config is ignored.
func NewAssociationListener(inner net.Listener, config Config) (*AssociationListener, error) {
return &AssociationListener{
config: config,
parent: inner,
}, nil
}

// AssociationListener represents a SCTP association listener
type AssociationListener struct {
config Config
parent net.Listener
}

// Accept waits for and returns the next association to the listener.
// You have to either close or read on all connection that are created.
func (l *AssociationListener) Accept() (*Association, error) {
c, err := l.parent.Accept()
if err != nil {
return nil, err
}
l.config.NetConn = c
return Server(l.config)
}

// Close closes the listener.
// Any blocked Accept operations will be unblocked and return errors.
// Already Accepted connections are not closed.
func (l *AssociationListener) Close() error {
return l.parent.Close()
}

// Addr returns the listener's network address.
func (l *AssociationListener) Addr() net.Addr {
return l.parent.Addr()
}

0 comments on commit 8784f3e

Please sign in to comment.