forked from nperez-messagebird/sctp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sctp_SCTPAddr.go
177 lines (152 loc) · 3.67 KB
/
sctp_SCTPAddr.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package sctp
import (
"bytes"
"fmt"
"net"
"strconv"
"strings"
syscall "golang.org/x/sys/unix"
)
type SCTPAddr struct {
IPAddrs []net.IPAddr
Port int
AddressFamily SCTPAddressFamily
}
func ResolveSCTPAddr(addressFamily SCTPAddressFamily, addrs string) (*SCTPAddr, error) {
elems := strings.Split(addrs, "/")
if len(elems) == 0 {
return nil, fmt.Errorf("invalid input: %s", addrs)
}
lastE := elems[len(elems)-1]
ipaddrs := make([]net.IPAddr, 0, len(elems))
addr, port, err := net.SplitHostPort(lastE)
if err != nil {
return nil, fmt.Errorf("invalid input: Missing port: %s", addrs)
} else {
if port == "" {
return nil, fmt.Errorf("invalid input: Missing port: %s", addrs)
}
}
iPort, err := strconv.Atoi(port)
if err != nil {
return nil, fmt.Errorf("invalid input: Non-integer port: %s", addrs)
}
elems[len(elems)-1] = addr
for _, e := range elems {
family := addressFamily.String()
if !strings.Contains(e, ":") && addressFamily == SCTP6 {
family = SCTP4.String()
}
ipa, err := net.ResolveIPAddr(family, e)
if err != nil {
return nil, err
}
if ipa.IP != nil {
if ipa.IP.To4() == nil {
if addressFamily == SCTP4 {
return nil, fmt.Errorf("IPv6 address detected but addressFamily is IPv4")
}
}
ipaddrs = append(ipaddrs, net.IPAddr{IP: ipa.IP, Zone: ipa.Zone})
} else {
var ip net.IPAddr
switch addressFamily {
case SCTP4:
ip = net.IPAddr{IP: net.IPv4zero, Zone: ""}
case SCTP6:
ip = net.IPAddr{IP: net.IPv6zero, Zone: ""}
default:
return nil, fmt.Errorf("Unknown addressFamily: %s", addressFamily)
}
ipaddrs = append(ipaddrs, ip)
}
}
return &SCTPAddr{
IPAddrs: ipaddrs,
Port: iPort,
AddressFamily: addressFamily,
}, nil
}
func (a *SCTPAddr) Network() string { return "sctp" }
func (a *SCTPAddr) ToRawSockAddrBuf() []byte {
p := htons(uint16(a.Port))
if len(a.IPAddrs) == 0 { // if a.IPAddrs list is empty - fall back to IPv4 zero addr
s := syscall.RawSockaddrInet4{
Family: syscall.AF_INET,
Port: p,
}
copy(s.Addr[:], net.IPv4zero)
return toBuf(s)
}
buf := []byte{}
for _, ip := range a.IPAddrs {
ipBytes := ip.IP
if len(ipBytes) == 0 {
ipBytes = net.IPv4zero
}
if ip4 := ipBytes.To4(); ip4 != nil {
s := syscall.RawSockaddrInet4{
Family: syscall.AF_INET,
Port: p,
}
copy(s.Addr[:], ip4)
buf = append(buf, toBuf(s)...)
} else {
var scopeid uint32
ifi, err := net.InterfaceByName(ip.Zone)
if err == nil {
scopeid = uint32(ifi.Index)
}
s := syscall.RawSockaddrInet6{
Family: syscall.AF_INET6,
Port: p,
Scope_id: scopeid,
}
copy(s.Addr[:], ipBytes)
buf = append(buf, toBuf(s)...)
}
}
return buf
}
func (a *SCTPAddr) String() string {
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 {
if n == len(a.IPAddrs)-1 {
b.WriteRune('[')
b.WriteString(i.String())
b.WriteRune(']')
} else {
b.WriteString(i.String())
}
}
if n < len(a.IPAddrs)-1 {
b.WriteRune('/')
}
}
b.WriteRune(':')
b.WriteString(strconv.Itoa(a.Port))
return b.String()
}
func (a *SCTPAddr) isWildcard() bool {
if a == nil {
return true
}
if 0 == len(a.IPAddrs) {
return true
}
return a.IPAddrs[0].IP.IsUnspecified()
}
func SCTPAddrFamily(laddr *SCTPAddr, raddr *SCTPAddr) (family int, ipv6only bool) {
if laddr != nil && raddr != nil {
if laddr.AddressFamily == raddr.AddressFamily {
return laddr.AddressFamily.ToSyscall(), (laddr.AddressFamily == SCTP6)
}
if supportsIPv4map() || !supportsIPv4() {
return SCTP6.ToSyscall(), false
}
}
return SCTP4.ToSyscall(), false
}