-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftypes.go
More file actions
407 lines (329 loc) · 12 KB
/
Copy pathftypes.go
File metadata and controls
407 lines (329 loc) · 12 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package gonnect
import (
"context"
"errors"
"io"
"net"
"net/netip"
"syscall"
"time"
)
var ErrUnsupported = errors.New("unsupported")
// Static type assertions
var (
_ Dial = (&net.Dialer{}).DialContext
_ Listen = (&net.ListenConfig{}).Listen
_ LookupIP = (&net.Resolver{}).LookupIP
_ LookupIPAddr = (&net.Resolver{}).LookupIPAddr
_ LookupNetIP = (&net.Resolver{}).LookupNetIP
_ LookupHost = (&net.Resolver{}).LookupHost
_ LookupAddr = (&net.Resolver{}).LookupAddr
_ LookupCNAME = (&net.Resolver{}).LookupCNAME
_ LookupPort = (&net.Resolver{}).LookupPort
_ LookupTXT = (&net.Resolver{}).LookupTXT
_ LookupMX = (&net.Resolver{}).LookupMX
_ LookupNS = (&net.Resolver{}).LookupNS
_ LookupSRV = (&net.Resolver{}).LookupSRV
_ TCPConn = &net.TCPConn{}
_ UDPConn = &net.UDPConn{}
_ TCPListener = &NetTCPListener{}
)
// PacketConn is an interface for UDP-like packet connections.
type PacketConn interface {
net.PacketConn
net.Conn
}
type ControlFlags int
const (
ControlDst ControlFlags = 1 << iota
ControlInterface
)
type ControlMessage struct {
Dst net.Addr
IfIndex int
IfName string
}
type MulticastOptions struct {
ReuseAddr bool
ReusePort bool
RecvAnyIf bool
ControlFlags ControlFlags
}
type MulticastPacketConn interface {
net.PacketConn
JoinGroup(iface NetworkInterface, group net.Addr) error
LeaveGroup(iface NetworkInterface, group net.Addr) error
SetControlMessage(flags ControlFlags, on bool) error
ReadFromControl(
b []byte,
) (n int, cm ControlMessage, from net.Addr, err error)
WriteToControl(b []byte, cm ControlMessage, dst net.Addr) (int, error)
}
type TCPConn interface {
// Read(b []byte) (n int, err error)
// Write(b []byte) (n int, err error)
// Close() error
// LocalAddr() Addr
// RemoteAddr() Addr
// SetDeadline(t time.Time) error
// SetReadDeadline(t time.Time) error
// SetWriteDeadline(t time.Time) error
net.Conn
// ReadFrom(r io.Reader) (int64, error)
io.ReaderFrom
// WriteTo(w io.Writer) (int64, error)
io.WriterTo
SetKeepAlive(keepalive bool) error
SetKeepAliveConfig(config net.KeepAliveConfig) error
SetKeepAlivePeriod(d time.Duration) error
SetLinger(sec int) error
SetNoDelay(noDelay bool) error
CloseRead() error
CloseWrite() error
}
type UDPConn interface {
// Read(b []byte) (n int, err error)
// Write(b []byte) (n int, err error)
// Close() error
// LocalAddr() Addr
// RemoteAddr() Addr
// SetDeadline(t time.Time) error
// SetReadDeadline(t time.Time) error
// SetWriteDeadline(t time.Time) error
net.Conn
// ReadFrom(p []byte) (n int, addr Addr, err error)
// WriteTo(p []byte, addr Addr) (n int, err error)
net.PacketConn
ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error)
ReadFromUDPAddrPort(b []byte) (n int, addr netip.AddrPort, err error)
WriteToUDP(b []byte, addr *net.UDPAddr) (int, error)
WriteToUDPAddrPort(b []byte, addr netip.AddrPort) (int, error)
ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *net.UDPAddr, err error)
ReadMsgUDPAddrPort(
b, oob []byte,
) (n, oobn, flags int, addr netip.AddrPort, err error)
WriteMsgUDP(b, oob []byte, addr *net.UDPAddr) (n, oobn int, err error)
WriteMsgUDPAddrPort(
b, oob []byte,
addr netip.AddrPort,
) (n, oobn int, err error)
}
type TCPListener interface {
// Accept() (Conn, error)
// Close() error
// Addr() Addr
net.Listener
AcceptTCP() (TCPConn, error)
SetDeadline(t time.Time) error
}
type NetTCPListener struct {
*net.TCPListener
}
func (l *NetTCPListener) AcceptTCP() (TCPConn, error) {
return l.TCPListener.AcceptTCP()
}
// Dial is a function type for establishing TCP-like connections.
// It matches the signature of net.Dialer.DialContext.
type Dial = func(ctx context.Context, network, address string) (net.Conn, error)
// DialTCP establishes a TCP connection similar to net.Dial for TCP networks.
type DialTCP = func(
ctx context.Context,
network, laddr, raddr string,
) (TCPConn, error)
// PacketDial is a function type for establishing UDP-like packet connections.
type PacketDial = func(ctx context.Context, network, address string) (PacketConn, error)
// DialUDP creates a UDP connection similar to net.Dial for UDP networks.
type DialUDP = func(
ctx context.Context,
network, laddr, raddr string,
) (UDPConn, error)
// Listen is a function type for creating TCP-like listeners.
type Listen = func(ctx context.Context, network, address string) (net.Listener, error)
// ListenTCP announces and returns a listener for TCP connections.
type ListenTCP = func(
ctx context.Context,
network, laddr string,
) (TCPListener, error)
// ListenUDP announces and returns a packet-oriented UDP connection.
type ListenUDP = func(
ctx context.Context,
network, laddr string,
) (UDPConn, error)
// PacketListen is a function type for creating UDP-like packet listeners.
type PacketListen = func(ctx context.Context, network, address string) (PacketConn, error)
// LookupIP looks up host. It returns a slice of that host's IPv4 and IPv6 addresses.
type LookupIP = func(ctx context.Context, network, address string) ([]net.IP, error)
// LookupIPAddr looks up host just like LookupIP but unlike it returns slice of net.IPAddr.
type LookupIPAddr = func(ctx context.Context, host string) ([]net.IPAddr, error)
// LookupNetIP looks up host just like LookupIP but unlike it returns slice of netip.Addr.
type LookupNetIP = func(ctx context.Context, network, host string) ([]netip.Addr, error)
// LookupHost looks up host just like LookupIP but unlike it returns slice of IP strings.
type LookupHost = func(ctx context.Context, host string) (addrs []string, err error)
// LookupAddr performs a reverse lookup for the given address,
// returning a list of names mapping to that address.
type LookupAddr = func(ctx context.Context, addr string) (names []string, err error)
// LookupCNAME returns the canonical name for the given host.
type LookupCNAME = func(ctx context.Context, host string) (cname string, err error)
// LookupPort looks up the port for the given network and service.
type LookupPort = func(ctx context.Context, network, service string) (port int, err error)
// LookupTXT returns the DNS TXT records for the given domain name.
// If a DNS TXT record holds multiple strings, they are concatenated as a single string.
type LookupTXT = func(ctx context.Context, name string) ([]string, error)
// LookupMX returns the DNS MX records for the given domain name sorted by preference.
type LookupMX = func(ctx context.Context, name string) ([]*net.MX, error)
// LookupNS returns the DNS NS records for the given domain name.
type LookupNS = func(ctx context.Context, name string) ([]*net.NS, error)
// LookupSRV tries to resolve an SRV query of the given service, protocol, and domain name.
// The proto is "tcp" or "udp".
type LookupSRV = func(ctx context.Context, service, proto, name string) (string, []*net.SRV, error)
type Resolver interface {
LookupIP(ctx context.Context, network, address string) ([]net.IP, error)
LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr, error)
LookupNetIP(ctx context.Context, network, host string) ([]netip.Addr, error)
LookupHost(ctx context.Context, host string) (addrs []string, err error)
LookupAddr(ctx context.Context, addr string) (names []string, err error)
LookupCNAME(ctx context.Context, host string) (cname string, err error)
LookupPort(
ctx context.Context,
network, service string,
) (port int, err error)
LookupNS(ctx context.Context, name string) ([]*net.NS, error)
LookupMX(
ctx context.Context,
name string,
) ([]*net.MX, error)
LookupSRV(
ctx context.Context,
service, proto, name string,
) (string, []*net.SRV, error)
LookupTXT(
ctx context.Context,
name string,
) ([]string, error)
}
// Network defines an abstraction over network providers.
type Network interface {
Resolver
// IsNative reports whether Network instance provides direct access to
// OS network stack or emulates it/adding mutation wrappers.
// Simple Network middlewares like logging ones should preserve IsNative status
// while complex ones (e.g. encrypting/proxyng/etc) should reports false.
// Some Network consumers may directly run low-level optimised dial/listen
// operations bypassing Network's methods calls if IsNative reports true.
IsNative() bool
Dial(
ctx context.Context,
network, address string,
) (net.Conn, error)
Listen(
ctx context.Context,
network, address string,
) (net.Listener, error)
PacketDial(ctx context.Context, network, address string) (PacketConn, error)
ListenPacket(
ctx context.Context,
network, address string,
) (PacketConn, error)
DialTCP(
ctx context.Context,
network, laddr, raddr string,
) (TCPConn, error)
ListenTCP(
ctx context.Context,
network, laddr string,
) (TCPListener, error)
DialUDP(
ctx context.Context,
network, laddr, raddr string,
) (UDPConn, error)
ListenUDP(
ctx context.Context,
network, laddr string,
) (UDPConn, error)
ListenPacketConfig(
ctx context.Context,
lc *ListenConfig,
network, address string,
) (PacketConn, error)
ListenUDPConfig(
ctx context.Context,
lc *ListenConfig,
network, laddr string,
) (UDPConn, error)
ListenMulticastUDP(
ctx context.Context,
network, address string,
opts MulticastOptions,
) (MulticastPacketConn, error)
// Interfaces returns the list of network interfaces known to this Network.
// Implementations may return an empty slice when the network is virtual,
// even though Dial/Listen/ListenPacket may still be usable. Multiple
// entries with the same Index or Name are allowed.
Interfaces() ([]NetworkInterface, error)
// InterfaceAddrs returns the addresses associated with interfaces known
// to this Network.
// Implementations may return an empty slice when the network is virtual,
// even though Dial/Listen/ListenPacket may still be usable.
// Duplicates are allowed.
InterfaceAddrs() ([]net.Addr, error)
// InterfaceMulticastAddrs returns the multicast addresses associated with
// interfaces known to this Network.
// Implementations may return an empty slice when the network is virtual,
// even though Dial/Listen/ListenPacket may still be usable.
// Duplicates are allowed.
InterfaceMulticastAddrs() ([]net.Addr, error)
// InterfacesByIndex returns all interfaces that match the provided index.
// Implementations may return an empty slice when the network is virtual,
// even though Dial/Listen/ListenPacket may still be usable.
// Multiple interfaces with the same index are allowed.
InterfacesByIndex(index int) ([]NetworkInterface, error)
// InterfacesByName returns all interfaces that match the provided name.
// Implementations may return an empty slice when the network is virtual,
// even though Dial/Listen/ListenPacket may still be usable.
// Multiple interfaces with the same name are allowed.
InterfacesByName(name string) ([]NetworkInterface, error)
}
type ListenConfig struct {
// Some virtual Network implementations may ignore Control function or
// call it with nil syscall.RawConn if they are not wrapping real syscalls.
Control func(network, address string, c syscall.RawConn) error
}
// Merge returns a new ListenConfig that applies this config's Control callback
// followed by other.Control. Neither input config is modified.
func (lc *ListenConfig) Merge(other *ListenConfig) *ListenConfig {
merged := &ListenConfig{}
if lc == nil && other == nil {
return merged
}
var controls []func(network, address string, c syscall.RawConn) error
if lc != nil && lc.Control != nil {
controls = append(controls, lc.Control)
}
if other != nil && other.Control != nil {
controls = append(controls, other.Control)
}
if len(controls) == 0 {
return merged
}
merged.Control = func(
network, address string,
c syscall.RawConn,
) error {
for _, control := range controls {
if err := control(network, address, c); err != nil {
return err
}
}
return nil
}
return merged
}
func (lc *ListenConfig) MergeNet(other *net.ListenConfig) *ListenConfig {
var otherConf *ListenConfig
if other != nil {
otherConf = &ListenConfig{
Control: other.Control,
}
}
return lc.Merge(otherConf)
}