Debian Codegraph @

net.go

raw symbols SCIP
1 // Package transport implements various networking related
2 // functions used throughout the Pion modules.
3 package transport
4
5 import (
6 "errors"
7 "io"
8 "net"
9 "time"
10 )
11
12 var (
13 // ErrNoAddressAssigned ...
14 ErrNoAddressAssigned = errors.New("no address assigned")
15 // ErrNotSupported ...
16 ErrNotSupported = errors.New("not supported yey")
17 // ErrInterfaceNotFound ...
18 ErrInterfaceNotFound = errors.New("interface not found")
19 // ErrNotUDPAddress ...
20 ErrNotUDPAddress = errors.New("not a UDP address")
21 )
22
23 // Net is an interface providing common networking functions which are
24 // similar to the functions provided by standard net package.
25 type Net interface {
26 // ListenPacket announces on the local network address.
27 //
28 // The network must be "udp", "udp4", "udp6", "unixgram", or an IP
29 // transport. The IP transports are "ip", "ip4", or "ip6" followed by
30 // a colon and a literal protocol number or a protocol name, as in
31 // "ip:1" or "ip:icmp".
32 //
33 // For UDP and IP networks, if the host in the address parameter is
34 // empty or a literal unspecified IP address, ListenPacket listens on
35 // all available IP addresses of the local system except multicast IP
36 // addresses.
37 // To only use IPv4, use network "udp4" or "ip4:proto".
38 // The address can use a host name, but this is not recommended,
39 // because it will create a listener for at most one of the host's IP
40 // addresses.
41 // If the port in the address parameter is empty or "0", as in
42 // "127.0.0.1:" or "[::1]:0", a port number is automatically chosen.
43 // The LocalAddr method of PacketConn can be used to discover the
44 // chosen port.
45 //
46 // See func Dial for a description of the network and address
47 // parameters.
48 //
49 // ListenPacket uses context.Background internally; to specify the context, use
50 // ListenConfig.ListenPacket.
51 ListenPacket(network string, address string) (net.PacketConn, error)
52
53 // ListenUDP acts like ListenPacket for UDP networks.
54 //
55 // The network must be a UDP network name; see func Dial for details.
56 //
57 // If the IP field of laddr is nil or an unspecified IP address,
58 // ListenUDP listens on all available IP addresses of the local system
59 // except multicast IP addresses.
60 // If the Port field of laddr is 0, a port number is automatically
61 // chosen.
62 ListenUDP(network string, locAddr *net.UDPAddr) (UDPConn, error)
63
64 // ListenTCP acts like Listen for TCP networks.
65 //
66 // The network must be a TCP network name; see func Dial for details.
67 //
68 // If the IP field of laddr is nil or an unspecified IP address,
69 // ListenTCP listens on all available unicast and anycast IP addresses
70 // of the local system.
71 // If the Port field of laddr is 0, a port number is automatically
72 // chosen.
73 ListenTCP(network string, laddr *net.TCPAddr) (TCPListener, error)
74
75 // Dial connects to the address on the named network.
76 //
77 // Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only),
78 // "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4"
79 // (IPv4-only), "ip6" (IPv6-only), "unix", "unixgram" and
80 // "unixpacket".
81 //
82 // For TCP and UDP networks, the address has the form "host:port".
83 // The host must be a literal IP address, or a host name that can be
84 // resolved to IP addresses.
85 // The port must be a literal port number or a service name.
86 // If the host is a literal IPv6 address it must be enclosed in square
87 // brackets, as in "[2001:db8::1]:80" or "[fe80::1%zone]:80".
88 // The zone specifies the scope of the literal IPv6 address as defined
89 // in RFC 4007.
90 // The functions JoinHostPort and SplitHostPort manipulate a pair of
91 // host and port in this form.
92 // When using TCP, and the host resolves to multiple IP addresses,
93 // Dial will try each IP address in order until one succeeds.
94 //
95 // Examples:
96 //
97 // Dial("tcp", "golang.org:http")
98 // Dial("tcp", "192.0.2.1:http")
99 // Dial("tcp", "198.51.100.1:80")
100 // Dial("udp", "[2001:db8::1]:domain")
101 // Dial("udp", "[fe80::1%lo0]:53")
102 // Dial("tcp", ":80")
103 //
104 // For IP networks, the network must be "ip", "ip4" or "ip6" followed
105 // by a colon and a literal protocol number or a protocol name, and
106 // the address has the form "host". The host must be a literal IP
107 // address or a literal IPv6 address with zone.
108 // It depends on each operating system how the operating system
109 // behaves with a non-well known protocol number such as "0" or "255".
110 //
111 // Examples:
112 //
113 // Dial("ip4:1", "192.0.2.1")
114 // Dial("ip6:ipv6-icmp", "2001:db8::1")
115 // Dial("ip6:58", "fe80::1%lo0")
116 //
117 // For TCP, UDP and IP networks, if the host is empty or a literal
118 // unspecified IP address, as in ":80", "0.0.0.0:80" or "[::]:80" for
119 // TCP and UDP, "", "0.0.0.0" or "::" for IP, the local system is
120 // assumed.
121 //
122 // For Unix networks, the address must be a file system path.
123 Dial(network, address string) (net.Conn, error)
124
125 // DialUDP acts like Dial for UDP networks.
126 //
127 // The network must be a UDP network name; see func Dial for details.
128 //
129 // If laddr is nil, a local address is automatically chosen.
130 // If the IP field of raddr is nil or an unspecified IP address, the
131 // local system is assumed.
132 DialUDP(network string, laddr, raddr *net.UDPAddr) (UDPConn, error)
133
134 // DialTCP acts like Dial for TCP networks.
135 //
136 // The network must be a TCP network name; see func Dial for details.
137 //
138 // If laddr is nil, a local address is automatically chosen.
139 // If the IP field of raddr is nil or an unspecified IP address, the
140 // local system is assumed.
141 DialTCP(network string, laddr, raddr *net.TCPAddr) (TCPConn, error)
142
143 // ResolveIPAddr returns an address of IP end point.
144 //
145 // The network must be an IP network name.
146 //
147 // If the host in the address parameter is not a literal IP address,
148 // ResolveIPAddr resolves the address to an address of IP end point.
149 // Otherwise, it parses the address as a literal IP address.
150 // The address parameter can use a host name, but this is not
151 // recommended, because it will return at most one of the host name's
152 // IP addresses.
153 //
154 // See func Dial for a description of the network and address
155 // parameters.
156 ResolveIPAddr(network, address string) (*net.IPAddr, error)
157
158 // ResolveUDPAddr returns an address of UDP end point.
159 //
160 // The network must be a UDP network name.
161 //
162 // If the host in the address parameter is not a literal IP address or
163 // the port is not a literal port number, ResolveUDPAddr resolves the
164 // address to an address of UDP end point.
165 // Otherwise, it parses the address as a pair of literal IP address
166 // and port number.
167 // The address parameter can use a host name, but this is not
168 // recommended, because it will return at most one of the host name's
169 // IP addresses.
170 //
171 // See func Dial for a description of the network and address
172 // parameters.
173 ResolveUDPAddr(network, address string) (*net.UDPAddr, error)
174
175 // ResolveTCPAddr returns an address of TCP end point.
176 //
177 // The network must be a TCP network name.
178 //
179 // If the host in the address parameter is not a literal IP address or
180 // the port is not a literal port number, ResolveTCPAddr resolves the
181 // address to an address of TCP end point.
182 // Otherwise, it parses the address as a pair of literal IP address
183 // and port number.
184 // The address parameter can use a host name, but this is not
185 // recommended, because it will return at most one of the host name's
186 // IP addresses.
187 //
188 // See func Dial for a description of the network and address
189 // parameters.
190 ResolveTCPAddr(network, address string) (*net.TCPAddr, error)
191
192 // Interfaces returns a list of the system's network interfaces.
193 Interfaces() ([]*Interface, error)
194
195 // InterfaceByIndex returns the interface specified by index.
196 //
197 // On Solaris, it returns one of the logical network interfaces
198 // sharing the logical data link; for more precision use
199 // InterfaceByName.
200 InterfaceByIndex(index int) (*Interface, error)
201
202 // InterfaceByName returns the interface specified by name.
203 InterfaceByName(name string) (*Interface, error)
204
205 // The following functions are extensions to Go's standard net package
206
207 CreateDialer(dialer *net.Dialer) Dialer
208 }
209
210 // Dialer is identical to net.Dialer excepts that its methods
211 // (Dial, DialContext) are overridden to use the Net interface.
212 // Use vnet.CreateDialer() to create an instance of this Dialer.
213 type Dialer interface {
214 Dial(network, address string) (net.Conn, error)
215 }
216
217 // UDPConn is packet-oriented connection for UDP.
218 type UDPConn interface {
219 // Close closes the connection.
220 // Any blocked Read or Write operations will be unblocked and return errors.
221 Close() error
222
223 // LocalAddr returns the local network address, if known.
224 LocalAddr() net.Addr
225
226 // RemoteAddr returns the remote network address, if known.
227 RemoteAddr() net.Addr
228
229 // SetDeadline sets the read and write deadlines associated
230 // with the connection. It is equivalent to calling both
231 // SetReadDeadline and SetWriteDeadline.
232 //
233 // A deadline is an absolute time after which I/O operations
234 // fail instead of blocking. The deadline applies to all future
235 // and pending I/O, not just the immediately following call to
236 // Read or Write. After a deadline has been exceeded, the
237 // connection can be refreshed by setting a deadline in the future.
238 //
239 // If the deadline is exceeded a call to Read or Write or to other
240 // I/O methods will return an error that wraps os.ErrDeadlineExceeded.
241 // This can be tested using errors.Is(err, os.ErrDeadlineExceeded).
242 // The error's Timeout method will return true, but note that there
243 // are other possible errors for which the Timeout method will
244 // return true even if the deadline has not been exceeded.
245 //
246 // An idle timeout can be implemented by repeatedly extending
247 // the deadline after successful Read or Write calls.
248 //
249 // A zero value for t means I/O operations will not time out.
250 SetDeadline(t time.Time) error
251
252 // SetReadDeadline sets the deadline for future Read calls
253 // and any currently-blocked Read call.
254 // A zero value for t means Read will not time out.
255 SetReadDeadline(t time.Time) error
256
257 // SetWriteDeadline sets the deadline for future Write calls
258 // and any currently-blocked Write call.
259 // Even if write times out, it may return n > 0, indicating that
260 // some of the data was successfully written.
261 // A zero value for t means Write will not time out.
262 SetWriteDeadline(t time.Time) error
263
264 // SetReadBuffer sets the size of the operating system's
265 // receive buffer associated with the connection.
266 SetReadBuffer(bytes int) error
267
268 // SetWriteBuffer sets the size of the operating system's
269 // transmit buffer associated with the connection.
270 SetWriteBuffer(bytes int) error
271
272 // Read reads data from the connection.
273 // Read can be made to time out and return an error after a fixed
274 // time limit; see SetDeadline and SetReadDeadline.
275 Read(b []byte) (n int, err error)
276
277 // ReadFrom reads a packet from the connection,
278 // copying the payload into p. It returns the number of
279 // bytes copied into p and the return address that
280 // was on the packet.
281 // It returns the number of bytes read (0 <= n <= len(p))
282 // and any error encountered. Callers should always process
283 // the n > 0 bytes returned before considering the error err.
284 // ReadFrom can be made to time out and return an error after a
285 // fixed time limit; see SetDeadline and SetReadDeadline.
286 ReadFrom(p []byte) (n int, addr net.Addr, err error)
287
288 // ReadFromUDP acts like ReadFrom but returns a UDPAddr.
289 ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error)
290
291 // ReadMsgUDP reads a message from c, copying the payload into b and
292 // the associated out-of-band data into oob. It returns the number of
293 // bytes copied into b, the number of bytes copied into oob, the flags
294 // that were set on the message and the source address of the message.
295 //
296 // The packages golang.org/x/net/ipv4 and golang.org/x/net/ipv6 can be
297 // used to manipulate IP-level socket options in oob.
298 ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *net.UDPAddr, err error)
299
300 // Write writes data to the connection.
301 // Write can be made to time out and return an error after a fixed
302 // time limit; see SetDeadline and SetWriteDeadline.
303 Write(b []byte) (n int, err error)
304
305 // WriteTo writes a packet with payload p to addr.
306 // WriteTo can be made to time out and return an Error after a
307 // fixed time limit; see SetDeadline and SetWriteDeadline.
308 // On packet-oriented connections, write timeouts are rare.
309 WriteTo(p []byte, addr net.Addr) (n int, err error)
310
311 // WriteToUDP acts like WriteTo but takes a UDPAddr.
312 WriteToUDP(b []byte, addr *net.UDPAddr) (int, error)
313
314 // WriteMsgUDP writes a message to addr via c if c isn't connected, or
315 // to c's remote address if c is connected (in which case addr must be
316 // nil). The payload is copied from b and the associated out-of-band
317 // data is copied from oob. It returns the number of payload and
318 // out-of-band bytes written.
319 //
320 // The packages golang.org/x/net/ipv4 and golang.org/x/net/ipv6 can be
321 // used to manipulate IP-level socket options in oob.
322 WriteMsgUDP(b, oob []byte, addr *net.UDPAddr) (n, oobn int, err error)
323 }
324
325 // TCPConn is an interface for TCP network connections.
326 type TCPConn interface {
327 net.Conn
328
329 // CloseRead shuts down the reading side of the TCP connection.
330 // Most callers should just use Close.
331 CloseRead() error
332
333 // CloseWrite shuts down the writing side of the TCP connection.
334 // Most callers should just use Close.
335 CloseWrite() error
336
337 // ReadFrom implements the io.ReaderFrom ReadFrom method.
338 ReadFrom(r io.Reader) (int64, error)
339
340 // SetLinger sets the behavior of Close on a connection which still
341 // has data waiting to be sent or to be acknowledged.
342 //
343 // If sec < 0 (the default), the operating system finishes sending the
344 // data in the background.
345 //
346 // If sec == 0, the operating system discards any unsent or
347 // unacknowledged data.
348 //
349 // If sec > 0, the data is sent in the background as with sec < 0. On
350 // some operating systems after sec seconds have elapsed any remaining
351 // unsent data may be discarded.
352 SetLinger(sec int) error
353
354 // SetKeepAlive sets whether the operating system should send
355 // keep-alive messages on the connection.
356 SetKeepAlive(keepalive bool) error
357
358 // SetKeepAlivePeriod sets period between keep-alives.
359 SetKeepAlivePeriod(d time.Duration) error
360
361 // SetNoDelay controls whether the operating system should delay
362 // packet transmission in hopes of sending fewer packets (Nagle's
363 // algorithm). The default is true (no delay), meaning that data is
364 // sent as soon as possible after a Write.
365 SetNoDelay(noDelay bool) error
366
367 // SetWriteBuffer sets the size of the operating system's
368 // transmit buffer associated with the connection.
369 SetWriteBuffer(bytes int) error
370
371 // SetReadBuffer sets the size of the operating system's
372 // receive buffer associated with the connection.
373 SetReadBuffer(bytes int) error
374 }
375
376 // TCPListener is a TCP network listener. Clients should typically
377 // use variables of type Listener instead of assuming TCP.
378 type TCPListener interface {
379 net.Listener
380
381 // AcceptTCP accepts the next incoming call and returns the new
382 // connection.
383 AcceptTCP() (TCPConn, error)
384
385 // SetDeadline sets the deadline associated with the listener.
386 // A zero time value disables the deadline.
387 SetDeadline(t time.Time) error
388 }
389
390 // Interface wraps a standard net.Interfaces and its assigned addresses
391 type Interface struct {
392 net.Interface
393 addrs []net.Addr
394 }
395
396 // NewInterface creates a new interface based of a standard net.Interface
397 func NewInterface(ifc net.Interface) *Interface {
398 return &Interface{
399 Interface: ifc,
400 addrs: nil,
401 }
402 }
403
404 // AddAddress adds a new address to the interface
405 func (ifc *Interface) AddAddress(addr net.Addr) {
406 ifc.addrs = append(ifc.addrs, addr)
407 }
408
409 // Addrs returns a slice of configured addresses on the interface
410 func (ifc *Interface) Addrs() ([]net.Addr, error) {
411 if len(ifc.addrs) == 0 {
412 return nil, ErrNoAddressAssigned
413 }
414 return ifc.addrs, nil
415 }