Skip to content

Commit 8784f3e

Browse files
committed
Added DialAssociation and ListenAssociation
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
1 parent 6f6d053 commit 8784f3e

File tree

8 files changed

+91
-95
lines changed

8 files changed

+91
-95
lines changed

association.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,19 @@ func Server(config Config) (*Association, error) {
212212
}
213213
}
214214

215+
// DialAssociation connects to the given network address and establishes a
216+
// SCTP association on top. The net.Conn in the config is ignored.
217+
func DialAssociation(network string, raddr *net.UDPAddr, config Config) (*Association, error) {
218+
pConn, err := net.DialUDP(network, nil, raddr)
219+
if err != nil {
220+
return nil, err
221+
}
222+
223+
config.NetConn = pConn
224+
225+
return Client(config)
226+
}
227+
215228
// Client opens a SCTP stream over a conn
216229
func Client(config Config) (*Association, error) {
217230
a := createAssociation(config)

examples/ping-pong/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
all: ping pong
22

3-
ping: ping.go conn.go
3+
ping: ping.go
44
go build -o $@
55

6-
pong: pong.go conn.go
6+
pong: pong.go
77
go build -o $@ -tags $@

examples/ping-pong/conn.go

Lines changed: 0 additions & 72 deletions
This file was deleted.

examples/ping-pong/ping.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,13 @@ import (
1212
)
1313

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

2218
config := sctp.Config{
23-
NetConn: conn,
2419
LoggerFactory: logging.NewDefaultLoggerFactory(),
2520
}
26-
a, err := sctp.Client(config)
21+
a, err := sctp.DialAssociation("udp", addr, config)
2722
if err != nil {
2823
log.Fatal(err)
2924
}

examples/ping-pong/pong.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,27 @@ import (
1313
)
1414

1515
func main() {
16-
addr := net.UDPAddr{
17-
IP: net.IPv4(127, 0, 0, 1),
18-
Port: 5678,
19-
}
16+
addr := &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 5678}
2017

21-
conn, err := net.ListenUDP("udp", &addr)
18+
config := sctp.Config{
19+
LoggerFactory: logging.NewDefaultLoggerFactory(),
20+
}
21+
l, err := sctp.ListenAssociation("udp", addr, config)
2222
if err != nil {
2323
log.Fatal(err)
2424
}
25-
defer conn.Close()
26-
fmt.Println("created a udp listener")
25+
defer l.Close()
26+
fmt.Println("created a listener")
2727

28-
config := sctp.Config{
29-
NetConn: &disconnectedPacketConn{pConn: conn},
30-
LoggerFactory: logging.NewDefaultLoggerFactory(),
31-
}
32-
a, err := sctp.Server(config)
28+
// Note: You should accept all incoming associations in a loop.
29+
a, err := l.Accept()
3330
if err != nil {
3431
log.Fatal(err)
3532
}
3633
defer a.Close()
37-
fmt.Println("created a server")
34+
fmt.Println("accepted an association")
3835

36+
// Note: You should accept all incoming streams in a loop.
3937
stream, err := a.AcceptStream()
4038
if err != nil {
4139
log.Fatal(err)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ require (
55
github.com/kr/pretty v0.1.0 // indirect
66
github.com/pion/logging v0.2.2
77
github.com/pion/transport v0.10.0
8+
github.com/pion/udp v0.1.0
89
github.com/pkg/errors v0.9.1
910
github.com/stretchr/testify v1.5.1
1011
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ github.com/pion/logging v0.2.2 h1:M9+AIj/+pxNsDfAT64+MAVgJO0rsyLnoJKCqf//DoeY=
1111
github.com/pion/logging v0.2.2/go.mod h1:k0/tDVsRCX2Mb2ZEmTqNa7CWsQPc+YYCB7Q+5pahoms=
1212
github.com/pion/transport v0.10.0 h1:9M12BSneJm6ggGhJyWpDveFOstJsTiQjkLf4M44rm80=
1313
github.com/pion/transport v0.10.0/go.mod h1:BnHnUipd0rZQyTVB2SBGojFHT9CBt5C5TcsJSQGkvSE=
14+
github.com/pion/udp v0.1.0 h1:uGxQsNyrqG3GLINv36Ff60covYmfrLoxzwnCsIYspXI=
15+
github.com/pion/udp v0.1.0/go.mod h1:BPELIjbwE9PRbd/zxI/KYBnbo7B6+oA6YuEaNE8lths=
1416
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
1517
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1618
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

listener.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package sctp
2+
3+
import (
4+
"net"
5+
6+
"github.com/pion/udp"
7+
)
8+
9+
// ListenAssociation creates a SCTP association listener
10+
func ListenAssociation(network string, laddr *net.UDPAddr, config Config) (*AssociationListener, error) {
11+
lc := udp.ListenConfig{}
12+
parent, err := lc.Listen(network, laddr)
13+
if err != nil {
14+
return nil, err
15+
}
16+
return &AssociationListener{
17+
config: config,
18+
parent: parent,
19+
}, nil
20+
}
21+
22+
// NewAssociationListener creates a SCTP association listener
23+
// which accepts connections from an inner Listener.
24+
// The net.Conn in the config is ignored.
25+
func NewAssociationListener(inner net.Listener, config Config) (*AssociationListener, error) {
26+
return &AssociationListener{
27+
config: config,
28+
parent: inner,
29+
}, nil
30+
}
31+
32+
// AssociationListener represents a SCTP association listener
33+
type AssociationListener struct {
34+
config Config
35+
parent net.Listener
36+
}
37+
38+
// Accept waits for and returns the next association to the listener.
39+
// You have to either close or read on all connection that are created.
40+
func (l *AssociationListener) Accept() (*Association, error) {
41+
c, err := l.parent.Accept()
42+
if err != nil {
43+
return nil, err
44+
}
45+
l.config.NetConn = c
46+
return Server(l.config)
47+
}
48+
49+
// Close closes the listener.
50+
// Any blocked Accept operations will be unblocked and return errors.
51+
// Already Accepted connections are not closed.
52+
func (l *AssociationListener) Close() error {
53+
return l.parent.Close()
54+
}
55+
56+
// Addr returns the listener's network address.
57+
func (l *AssociationListener) Addr() net.Addr {
58+
return l.parent.Addr()
59+
}

0 commit comments

Comments
 (0)