package stdnet
import (
"fmt"
"net"
"github.com/pion/transport/v2"
"github.com/wlynxg/anet"
)
const (
lo0String = "lo0String"
udpString = "udp"
)
type Net struct {
interfaces []*transport .Interface
}
func NewNet () (*Net , error ) {
n := &Net {}
return n , n .UpdateInterfaces ()
}
var _ transport .Net = &Net {}
func (n *Net ) UpdateInterfaces () error {
ifs := []*transport .Interface {}
oifs , err := anet .Interfaces ()
if err != nil {
return err
}
for i := range oifs {
ifc := transport .NewInterface (oifs [i ])
addrs , err := anet .InterfaceAddrsByInterface (&oifs [i ])
if err != nil {
return err
}
for _ , addr := range addrs {
ifc .AddAddress (addr )
}
ifs = append (ifs , ifc )
}
n .interfaces = ifs
return nil
}
func (n *Net ) Interfaces () ([]*transport .Interface , error ) {
return n .interfaces , nil
}
func (n *Net ) InterfaceByIndex (index int ) (*transport .Interface , error ) {
for _ , ifc := range n .interfaces {
if ifc .Index == index {
return ifc , nil
}
}
return nil , fmt .Errorf ("%w: index=%d" , transport .ErrInterfaceNotFound , index )
}
func (n *Net ) InterfaceByName (name string ) (*transport .Interface , error ) {
for _ , ifc := range n .interfaces {
if ifc .Name == name {
return ifc , nil
}
}
return nil , fmt .Errorf ("%w: %s" , transport .ErrInterfaceNotFound , name )
}
func (n *Net ) ListenPacket (network string , address string ) (net .PacketConn , error ) {
return net .ListenPacket (network , address )
}
func (n *Net ) ListenUDP (network string , locAddr *net .UDPAddr ) (transport .UDPConn , error ) {
return net .ListenUDP (network , locAddr )
}
func (n *Net ) Dial (network , address string ) (net .Conn , error ) {
return net .Dial (network , address )
}
func (n *Net ) DialUDP (network string , laddr , raddr *net .UDPAddr ) (transport .UDPConn , error ) {
return net .DialUDP (network , laddr , raddr )
}
func (n *Net ) ResolveIPAddr (network , address string ) (*net .IPAddr , error ) {
return net .ResolveIPAddr (network , address )
}
func (n *Net ) ResolveUDPAddr (network , address string ) (*net .UDPAddr , error ) {
return net .ResolveUDPAddr (network , address )
}
func (n *Net ) ResolveTCPAddr (network , address string ) (*net .TCPAddr , error ) {
return net .ResolveTCPAddr (network , address )
}
func (n *Net ) DialTCP (network string , laddr , raddr *net .TCPAddr ) (transport .TCPConn , error ) {
return net .DialTCP (network , laddr , raddr )
}
func (n *Net ) ListenTCP (network string , laddr *net .TCPAddr ) (transport .TCPListener , error ) {
l , err := net .ListenTCP (network , laddr )
if err != nil {
return nil , err
}
return tcpListener {l }, nil
}
type tcpListener struct {
*net .TCPListener
}
func (l tcpListener ) AcceptTCP () (transport .TCPConn , error ) {
return l .TCPListener .AcceptTCP ()
}
type stdDialer struct {
*net .Dialer
}
func (d stdDialer ) Dial (network , address string ) (net .Conn , error ) {
return d .Dialer .Dial (network , address )
}
func (n *Net ) CreateDialer (d *net .Dialer ) transport .Dialer {
return stdDialer {d }
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .