// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

// Package stdnet implements the transport.Net interface // using methods from Go's standard net package.
package stdnet import ( ) const ( lo0String = "lo0String" udpString = "udp" ) // Net is an implementation of the net.Net interface // based on functions of the standard net package. type Net struct { interfaces []*transport.Interface } // NewNet creates a new StdNet instance. func () (*Net, error) { := &Net{} return , .UpdateInterfaces() } // Compile-time assertion var _ transport.Net = &Net{} // UpdateInterfaces updates the internal list of network interfaces // and associated addresses. func ( *Net) () error { := []*transport.Interface{} , := anet.Interfaces() if != nil { return } for := range { := transport.NewInterface([]) , := anet.InterfaceAddrsByInterface(&[]) if != nil { return } for , := range { .AddAddress() } = append(, ) } .interfaces = return nil } // Interfaces returns a slice of interfaces which are available on the // system func ( *Net) () ([]*transport.Interface, error) { return .interfaces, nil } // InterfaceByIndex returns the interface specified by index. // // On Solaris, it returns one of the logical network interfaces // sharing the logical data link; for more precision use // InterfaceByName. func ( *Net) ( int) (*transport.Interface, error) { for , := range .interfaces { if .Index == { return , nil } } return nil, fmt.Errorf("%w: index=%d", transport.ErrInterfaceNotFound, ) } // InterfaceByName returns the interface specified by name. func ( *Net) ( string) (*transport.Interface, error) { for , := range .interfaces { if .Name == { return , nil } } return nil, fmt.Errorf("%w: %s", transport.ErrInterfaceNotFound, ) } // ListenPacket announces on the local network address. func ( *Net) ( string, string) (net.PacketConn, error) { return net.ListenPacket(, ) } // ListenUDP acts like ListenPacket for UDP networks. func ( *Net) ( string, *net.UDPAddr) (transport.UDPConn, error) { return net.ListenUDP(, ) } // Dial connects to the address on the named network. func ( *Net) (, string) (net.Conn, error) { return net.Dial(, ) } // DialUDP acts like Dial for UDP networks. func ( *Net) ( string, , *net.UDPAddr) (transport.UDPConn, error) { return net.DialUDP(, , ) } // ResolveIPAddr returns an address of IP end point. func ( *Net) (, string) (*net.IPAddr, error) { return net.ResolveIPAddr(, ) } // ResolveUDPAddr returns an address of UDP end point. func ( *Net) (, string) (*net.UDPAddr, error) { return net.ResolveUDPAddr(, ) } // ResolveTCPAddr returns an address of TCP end point. func ( *Net) (, string) (*net.TCPAddr, error) { return net.ResolveTCPAddr(, ) } // DialTCP acts like Dial for TCP networks. func ( *Net) ( string, , *net.TCPAddr) (transport.TCPConn, error) { return net.DialTCP(, , ) } // ListenTCP acts like Listen for TCP networks. func ( *Net) ( string, *net.TCPAddr) (transport.TCPListener, error) { , := net.ListenTCP(, ) if != nil { return nil, } return tcpListener{}, nil } type tcpListener struct { *net.TCPListener } func ( tcpListener) () (transport.TCPConn, error) { return .TCPListener.AcceptTCP() } type stdDialer struct { *net.Dialer } func ( stdDialer) (, string) (net.Conn, error) { return .Dialer.Dial(, ) } // CreateDialer creates an instance of vnet.Dialer func ( *Net) ( *net.Dialer) transport.Dialer { return stdDialer{} }