forked from nperez-messagebird/sctp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sctp_SCTPListener.go
80 lines (63 loc) · 1.78 KB
/
sctp_SCTPListener.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package sctp
import (
"fmt"
"net"
)
type SCTPListener struct {
SCTPConn
socketMode SCTPSocketMode
}
func NewSCTPListener(laddr *SCTPAddr, init InitMsg, mode SCTPSocketMode, nonblocking bool) (*SCTPListener, error) {
if laddr == nil {
return nil, fmt.Errorf("Local SCTPAddr is required")
}
conn, err := NewSCTPConnection(laddr, laddr.AddressFamily, init, mode, nonblocking)
if err != nil {
return nil, err
}
ln := &SCTPListener{SCTPConn: *conn, socketMode: mode}
ln.socketMode = mode
if err := ln.Listen(); err != nil {
return nil, err
}
return ln, nil
}
// AcceptSCTP waits for and returns the next SCTP connection to the listener.
func (ln *SCTPListener) AcceptSCTP() (*SCTPConn, error) {
if ln.socketMode == OneToMany {
return nil, fmt.Errorf("Calling Accept on OneToMany socket is invalid")
}
fd, err := SCTPAccept(ln.FD())
if err != nil {
return nil, err
}
blocking, err := ln.GetNonblocking()
if err != nil {
return nil, err
}
conn := &SCTPConn{fd: int32(fd)}
err = conn.SetNonblocking(blocking)
if err != nil {
return nil, err
}
return conn, nil
}
// Accept waits for and returns the next connection connection to the listener.
func (ln *SCTPListener) Accept() (net.Conn, error) {
return ln.AcceptSCTP()
}
func (ln *SCTPListener) SCTPRead(b []byte) (int, *OOBMessage, int, error) {
if ln.socketMode == OneToOne {
return -1, nil, -1, fmt.Errorf("Invalid state: SCTPRead on OneToOne socket not allowed")
}
return ln.SCTPConn.SCTPRead(b)
}
func (ln *SCTPListener) SCTPWrite(b []byte, info *SndRcvInfo) (int, error) {
if ln.socketMode == OneToOne {
return -1, fmt.Errorf("Invalid state: SCTPWrite on OneToOne socket not allowed")
}
return ln.SCTPConn.SCTPWrite(b, info)
}
func (ln *SCTPListener) Addr() net.Addr {
return ln.SCTPConn.LocalAddr()
}