Debian Codegraph @

stdnet/net.go

raw symbols SCIP
1 // Package stdnet implements the transport.Net interface
2 // using methods from Go's standard net package.
3 package stdnet
4
5 import (
6 "fmt"
7 "net"
8
9 "github.com/pion/transport/v2"
10 )
11
12 const (
13 lo0String = "lo0String"
14 udpString = "udp"
15 )
16
17 // Net is an implementation of the net.Net interface
18 // based on functions of the standard net package.
19 type Net struct {
20 interfaces []*transport.Interface
21 }
22
23 // NewNet creates a new StdNet instance.
24 func NewNet() (*Net, error) {
25 n := &Net{}
26
27 return n, n.UpdateInterfaces()
28 }
29
30 // Compile-time assertion
31 var _ transport.Net = &Net{}
32
33 // UpdateInterfaces updates the internal list of network interfaces
34 // and associated addresses.
35 func (n *Net) UpdateInterfaces() error {
36 ifs := []*transport.Interface{}
37
38 oifs, err := net.Interfaces()
39 if err != nil {
40 return err
41 }
42
43 for _, oif := range oifs {
44 ifc := transport.NewInterface(oif)
45
46 addrs, err := oif.Addrs()
47 if err != nil {
48 return err
49 }
50
51 for _, addr := range addrs {
52 ifc.AddAddress(addr)
53 }
54
55 ifs = append(ifs, ifc)
56 }
57
58 n.interfaces = ifs
59
60 return nil
61 }
62
63 // Interfaces returns a slice of interfaces which are available on the
64 // system
65 func (n *Net) Interfaces() ([]*transport.Interface, error) {
66 return n.interfaces, nil
67 }
68
69 // InterfaceByIndex returns the interface specified by index.
70 //
71 // On Solaris, it returns one of the logical network interfaces
72 // sharing the logical data link; for more precision use
73 // InterfaceByName.
74 func (n *Net) InterfaceByIndex(index int) (*transport.Interface, error) {
75 for _, ifc := range n.interfaces {
76 if ifc.Index == index {
77 return ifc, nil
78 }
79 }
80
81 return nil, fmt.Errorf("%w: index=%d", transport.ErrInterfaceNotFound, index)
82 }
83
84 // InterfaceByName returns the interface specified by name.
85 func (n *Net) InterfaceByName(name string) (*transport.Interface, error) {
86 for _, ifc := range n.interfaces {
87 if ifc.Name == name {
88 return ifc, nil
89 }
90 }
91
92 return nil, fmt.Errorf("%w: %s", transport.ErrInterfaceNotFound, name)
93 }
94
95 // ListenPacket announces on the local network address.
96 func (n *Net) ListenPacket(network string, address string) (net.PacketConn, error) {
97 return net.ListenPacket(network, address)
98 }
99
100 // ListenUDP acts like ListenPacket for UDP networks.
101 func (n *Net) ListenUDP(network string, locAddr *net.UDPAddr) (transport.UDPConn, error) {
102 return net.ListenUDP(network, locAddr)
103 }
104
105 // Dial connects to the address on the named network.
106 func (n *Net) Dial(network, address string) (net.Conn, error) {
107 return net.Dial(network, address)
108 }
109
110 // DialUDP acts like Dial for UDP networks.
111 func (n *Net) DialUDP(network string, laddr, raddr *net.UDPAddr) (transport.UDPConn, error) {
112 return net.DialUDP(network, laddr, raddr)
113 }
114
115 // ResolveIPAddr returns an address of IP end point.
116 func (n *Net) ResolveIPAddr(network, address string) (*net.IPAddr, error) {
117 return net.ResolveIPAddr(network, address)
118 }
119
120 // ResolveUDPAddr returns an address of UDP end point.
121 func (n *Net) ResolveUDPAddr(network, address string) (*net.UDPAddr, error) {
122 return net.ResolveUDPAddr(network, address)
123 }
124
125 // ResolveTCPAddr returns an address of TCP end point.
126 func (n *Net) ResolveTCPAddr(network, address string) (*net.TCPAddr, error) {
127 return net.ResolveTCPAddr(network, address)
128 }
129
130 // DialTCP acts like Dial for TCP networks.
131 func (n *Net) DialTCP(network string, laddr, raddr *net.TCPAddr) (transport.TCPConn, error) {
132 return net.DialTCP(network, laddr, raddr)
133 }
134
135 // ListenTCP acts like Listen for TCP networks.
136 func (n *Net) ListenTCP(network string, laddr *net.TCPAddr) (transport.TCPListener, error) {
137 l, err := net.ListenTCP(network, laddr)
138 if err != nil {
139 return nil, err
140 }
141
142 return tcpListener{l}, nil
143 }
144
145 type tcpListener struct {
146 *net.TCPListener
147 }
148
149 func (l tcpListener) AcceptTCP() (transport.TCPConn, error) {
150 return l.TCPListener.AcceptTCP()
151 }
152
153 type stdDialer struct {
154 *net.Dialer
155 }
156
157 func (d stdDialer) Dial(network, address string) (net.Conn, error) {
158 return d.Dialer.Dial(network, address)
159 }
160
161 // CreateDialer creates an instance of vnet.Dialer
162 func (n *Net) CreateDialer(d *net.Dialer) transport.Dialer {
163 return stdDialer{d}
164 }