-
Notifications
You must be signed in to change notification settings - Fork 1
/
inet_unix.go
339 lines (307 loc) · 8.38 KB
/
inet_unix.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// ©Hayabusa Cloud Co., Ltd. 2022. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//go:build unix
package sox
import (
"encoding/binary"
"golang.org/x/sys/unix"
"net/netip"
"unsafe"
)
func inetAddrFromAddrPort(addrPort netip.AddrPort) Sockaddr {
if addrPort.Addr().Is4() {
return &SockaddrInet4{
Port: int(addrPort.Port()),
Addr: addrPort.Addr().As4(),
}
} else if addrPort.Addr().Is6() {
return &SockaddrInet6{
Port: int(addrPort.Port()),
Addr: addrPort.Addr().As16(),
}
}
return &SockaddrInet6{}
}
func unixAddrFromSockaddr(sa Sockaddr, proto UnderlyingProtocol) *UnixAddr {
switch proto {
case UnderlyingProtocolStream:
return &UnixAddr{Name: sa.(*SockaddrUnix).Name, Net: "unix"}
case UnderlyingProtocolDgram:
return &UnixAddr{Name: sa.(*SockaddrUnix).Name, Net: "unixgram"}
case UnderlyingProtocolSeqPacket:
return &UnixAddr{Name: sa.(*SockaddrUnix).Name, Net: "unixpacket"}
}
return nil
}
func addrPortFromSockaddr(addr Sockaddr) netip.AddrPort {
if addr == nil {
return netip.AddrPort{}
}
switch addr.(type) {
case *SockaddrInet4:
a := addr.(*SockaddrInet4)
return netip.AddrPortFrom(netip.AddrFrom4(a.Addr), uint16(a.Port))
case *SockaddrInet6:
a := addr.(*SockaddrInet6)
return netip.AddrPortFrom(netip.AddrFrom16(a.Addr), uint16(a.Port))
}
return netip.AddrPort{}
}
func inet4AddrToSockaddr(addr Addr) *SockaddrInet4 {
switch a := addr.(type) {
case *IPAddr:
return ip4AddrToSockaddr(a)
case *TCPAddr:
return tcp4AddrToSockaddr(a)
case *UDPAddr:
return udp4AddrToSockaddr(a)
case *SCTPAddr:
return sctp4AddrToSockaddr(a)
default:
return nil
}
}
func inet6AddrToSockaddr(addr Addr) *SockaddrInet6 {
switch a := addr.(type) {
case *IPAddr:
return ip6AddrToSockaddr(a)
case *TCPAddr:
return tcp6AddrToSockaddr(a)
case *UDPAddr:
return udp6AddrToSockaddr(a)
case *SCTPAddr:
return sctp6AddrToSockaddr(a)
default:
return nil
}
}
func ip4AddrToSockaddr(addr *IPAddr) *SockaddrInet4 {
ip := addr.IP
if ip == nil {
ip = IPV4zero
}
return &SockaddrInet4{
Addr: IP4AddressToBytes(addr.IP),
}
}
func ip4AddrPortToSockaddr(addr *IPAddr, port int) *SockaddrInet4 {
ip := addr.IP
if ip == nil {
ip = IPV4zero
}
return &SockaddrInet4{
Port: port,
Addr: IP4AddressToBytes(ip),
}
}
func ip6AddrToSockaddr(addr *IPAddr) *SockaddrInet6 {
ip := addr.IP
if ip == nil {
ip = IPV6unspecified
}
return &SockaddrInet6{
ZoneId: uint32(ip6ZoneID(addr.Zone)),
Addr: IP6AddressToBytes(ip),
}
}
func ip6AddrPortToSockaddr(addr *IPAddr, port int) *SockaddrInet6 {
ip := addr.IP
if ip == nil {
ip = IPV6unspecified
}
return &SockaddrInet6{
Port: port,
ZoneId: uint32(ip6ZoneID(addr.Zone)),
Addr: IP6AddressToBytes(ip),
}
}
func ipAddrToSockaddr(network string, addr *IPAddr) Sockaddr {
if networkIPFamily(network, addr.IP) == NetworkIPv4 {
return ip4AddrToSockaddr(addr)
}
return ip6AddrToSockaddr(addr)
}
func ipAddrPortToSockaddr(network string, addr *IPAddr, port int) Sockaddr {
if networkIPFamily(network, addr.IP) == NetworkIPv4 {
return ip4AddrPortToSockaddr(addr, port)
}
return ip6AddrPortToSockaddr(addr, port)
}
func tcp4AddrToSockaddr(addr *TCPAddr) *SockaddrInet4 {
ip := addr.IP
if ip == nil {
ip = IPV4zero
}
return &SockaddrInet4{
Port: addr.Port,
Addr: IP4AddressToBytes(ip),
}
}
func tcp6AddrToSockaddr(addr *TCPAddr) *SockaddrInet6 {
ip := addr.IP
if ip == nil {
ip = IPV6unspecified
}
return &SockaddrInet6{
Port: addr.Port,
ZoneId: uint32(ip6ZoneID(addr.Zone)),
Addr: IP6AddressToBytes(ip),
}
}
func tcpAddrToSockaddr(network string, addr *TCPAddr) Sockaddr {
return ipAddrPortToSockaddr(network, IPAddrFromTCPAddr(addr), addr.Port)
}
func udp4AddrToSockaddr(addr *UDPAddr) *SockaddrInet4 {
ip := addr.IP
if ip == nil {
ip = IPV4zero
}
return &SockaddrInet4{
Port: addr.Port,
Addr: IP4AddressToBytes(ip),
}
}
func udp6AddrToSockaddr(addr *UDPAddr) *SockaddrInet6 {
ip := addr.IP
if ip == nil {
ip = IPV6unspecified
}
return &SockaddrInet6{
Port: addr.Port,
ZoneId: uint32(ip6ZoneID(addr.Zone)),
Addr: IP6AddressToBytes(ip),
}
}
func udpAddrToSockaddr(network string, addr *UDPAddr) Sockaddr {
return ipAddrPortToSockaddr(network, IPAddrFromUDPAddr(addr), addr.Port)
}
func sctp4AddrToSockaddr(addr *SCTPAddr) *SockaddrInet4 {
ip := addr.IP
if ip == nil {
ip = IPV4zero
}
return &SockaddrInet4{
Port: addr.Port,
Addr: IP4AddressToBytes(ip),
}
}
func sctp6AddrToSockaddr(addr *SCTPAddr) *SockaddrInet6 {
ip := addr.IP
if ip == nil {
ip = IPV6unspecified
}
return &SockaddrInet6{
Port: addr.Port,
ZoneId: uint32(ip6ZoneID(addr.Zone)),
Addr: IP6AddressToBytes(ip),
}
}
func sctpAddrToSockaddr(network string, addr *SCTPAddr) Sockaddr {
return ipAddrPortToSockaddr(network, IPAddrFromSCTPAddr(addr), addr.Port)
}
func inetAddrToSockaddr(addr Addr) Sockaddr {
switch addr := addr.(type) {
case *IPAddr:
return ipAddrToSockaddr("ip", addr)
case *TCPAddr:
return tcpAddrToSockaddr("tcp", addr)
case *UDPAddr:
return udpAddrToSockaddr("udp", addr)
case *SCTPAddr:
return sctpAddrToSockaddr("sctp", addr)
default:
panic("addr must be an internet address")
}
}
func sockaddr(sa Sockaddr) (ptr unsafe.Pointer, n int, err error) {
switch sa.(type) {
case *SockaddrInet4:
return inet4Sockaddr(sa.(*SockaddrInet4))
case *SockaddrInet6:
return inet6Sockaddr(sa.(*SockaddrInet6))
case *SockaddrUnix:
if len(sa.(*SockaddrUnix).Name) >= unix.SizeofSockaddrUnix-unix.SizeofShort {
return unsafe.Pointer(uintptr(0)), 0, ErrInvalidParam
}
return unixSockaddr(sa.(*SockaddrUnix))
default:
panic("unimplemented sockaddr type")
}
}
func inet4Sockaddr(sa *SockaddrInet4) (ptr unsafe.Pointer, n int, err error) {
rawSa := &unix.RawSockaddrInet4{
Family: unix.AF_INET,
Addr: sa.Addr,
}
b := unsafe.Slice((*byte)(unsafe.Pointer(rawSa)), unix.SizeofSockaddrInet4)
portOffset, portSize := unsafe.Sizeof(uint16(0)), unsafe.Sizeof(uint16(0))
binary.BigEndian.PutUint16(b[portOffset:portOffset+portSize], uint16(sa.Port))
return unsafe.Pointer(unsafe.SliceData(b)), unix.SizeofSockaddrInet4, nil
}
func inet6Sockaddr(sa *SockaddrInet6) (ptr unsafe.Pointer, n int, err error) {
rawSa := &unix.RawSockaddrInet6{
Family: unix.AF_INET6,
Addr: sa.Addr,
Scope_id: sa.ZoneId,
}
b := unsafe.Slice((*byte)(unsafe.Pointer(rawSa)), unix.SizeofSockaddrInet6)
portOffset, portSize := unsafe.Sizeof(uint16(0)), unsafe.Sizeof(uint16(0))
binary.BigEndian.PutUint16(b[portOffset:portOffset+portSize], uint16(sa.Port))
return unsafe.Pointer(unsafe.SliceData(b)), unix.SizeofSockaddrInet6, nil
}
func sockaddrData(sa Sockaddr) (data []byte, err error) {
switch sa.(type) {
case *SockaddrInet4:
return inet4SockaddrData(sa.(*SockaddrInet4))
case *SockaddrInet6:
return inet6SockaddrData(sa.(*SockaddrInet6))
case *SockaddrUnix:
if len(sa.(*SockaddrUnix).Name) >= unix.SizeofSockaddrUnix-unix.SizeofShort {
return nil, ErrInvalidParam
}
return unixSockaddrData(sa.(*SockaddrUnix))
default:
panic("unimplemented sockaddr type")
}
}
func inet4SockaddrData(sa *SockaddrInet4) (data []byte, err error) {
rawSa := &unix.RawSockaddrInet4{
Family: unix.AF_INET,
Port: uint16(sa.Port),
Addr: sa.Addr,
}
b := unsafe.Slice((*byte)(unsafe.Pointer(rawSa)), unix.SizeofSockaddrInet4)
return b[:unix.SizeofSockaddrInet4], nil
}
func inet6SockaddrData(sa *SockaddrInet6) (data []byte, err error) {
rawSa := &unix.RawSockaddrInet6{
Family: unix.AF_INET6,
Port: uint16(sa.Port),
Addr: sa.Addr,
Scope_id: sa.ZoneId,
}
b := unsafe.Slice((*byte)(unsafe.Pointer(rawSa)), unix.SizeofSockaddrInet6)
return b[:unix.SizeofSockaddrInet6], nil
}
func inet4SockaddrFromData(data []byte) *SockaddrInet4 {
raw := (*unix.RawSockaddrInet4)(unsafe.Pointer(unsafe.SliceData(data)))
sa := SockaddrInet4{
Port: int(raw.Port),
Addr: raw.Addr,
}
fRawPtr := unsafe.Add(unsafe.Pointer(&sa), unsafe.Sizeof(sa.Port)+unsafe.Sizeof(sa.Addr))
*(*unix.RawSockaddrInet4)(fRawPtr) = *raw
return &sa
}
func inet6SockaddrFromData(data []byte) *SockaddrInet6 {
raw := (*unix.RawSockaddrInet6)(unsafe.Pointer(unsafe.SliceData(data)))
sa := SockaddrInet6{
Port: int(raw.Port),
ZoneId: raw.Scope_id,
Addr: raw.Addr,
}
fRawPtr := unsafe.Add(unsafe.Pointer(&sa), unsafe.Sizeof(sa.Port)+unsafe.Sizeof(sa.ZoneId)+unsafe.Sizeof(sa.Addr))
*(*unix.RawSockaddrInet6)(fRawPtr) = *raw
return &sa
}