diff --git a/net/convert.go b/net/convert.go index 4603fa2..3a4ac7a 100644 --- a/net/convert.go +++ b/net/convert.go @@ -79,26 +79,6 @@ func MultiaddrToIPNet(m ma.Multiaddr) (*net.IPNet, error) { return ipnet, err } -func parseBasicNetMaddr(maddr ma.Multiaddr) (net.Addr, error) { - network, host, err := DialArgs(maddr) - if err != nil { - return nil, err - } - - switch network { - case "tcp", "tcp4", "tcp6": - return net.ResolveTCPAddr(network, host) - case "udp", "udp4", "udp6": - return net.ResolveUDPAddr(network, host) - case "ip", "ip4", "ip6": - return net.ResolveIPAddr(network, host) - case "unix": - return net.ResolveUnixAddr(network, host) - } - - return nil, fmt.Errorf("network not supported: %s", network) -} - func FromIPAndZone(ip net.IP, zone string) (ma.Multiaddr, error) { switch { case ip.To4() != nil: diff --git a/net/net.go b/net/net.go index 10fcff7..7863a04 100644 --- a/net/net.go +++ b/net/net.go @@ -34,48 +34,6 @@ type halfOpen interface { CloseWrite() error } -func wrap(nconn net.Conn, laddr, raddr ma.Multiaddr) Conn { - endpts := maEndpoints{ - laddr: laddr, - raddr: raddr, - } - // This sucks. However, it's the only way to reliably expose the - // underlying methods. This way, users that need access to, e.g., - // CloseRead and CloseWrite, can do so via type assertions. - switch nconn := nconn.(type) { - case *net.TCPConn: - return &struct { - *net.TCPConn - maEndpoints - }{nconn, endpts} - case *net.UDPConn: - return &struct { - *net.UDPConn - maEndpoints - }{nconn, endpts} - case *net.IPConn: - return &struct { - *net.IPConn - maEndpoints - }{nconn, endpts} - case *net.UnixConn: - return &struct { - *net.UnixConn - maEndpoints - }{nconn, endpts} - case halfOpen: - return &struct { - halfOpen - maEndpoints - }{nconn, endpts} - default: - return &struct { - net.Conn - maEndpoints - }{nconn, endpts} - } -} - // WrapNetConn wraps a net.Conn object with a Multiaddr friendly Conn. // // This function does it's best to avoid "hiding" methods exposed by the wrapped @@ -362,7 +320,7 @@ func ListenPacket(laddr ma.Multiaddr) (PacketConn, error) { return nil, err } - pc, err := net.ListenPacket(lnet, lnaddr) + pc, err := listenPacket(lnet, lnaddr) if err != nil { return nil, err } diff --git a/net/net_other.go b/net/net_other.go new file mode 100644 index 0000000..7f5bb3f --- /dev/null +++ b/net/net_other.go @@ -0,0 +1,76 @@ +//go:build !tinygo + +package manet + +import ( + "fmt" + "net" + + ma "github.com/multiformats/go-multiaddr" +) + +func parseBasicNetMaddr(maddr ma.Multiaddr) (net.Addr, error) { + network, host, err := DialArgs(maddr) + if err != nil { + return nil, err + } + + switch network { + case "tcp", "tcp4", "tcp6": + return net.ResolveTCPAddr(network, host) + case "udp", "udp4", "udp6": + return net.ResolveUDPAddr(network, host) + case "ip", "ip4", "ip6": + return net.ResolveIPAddr(network, host) + case "unix": + return net.ResolveUnixAddr(network, host) + } + + return nil, fmt.Errorf("network not supported: %s", network) +} + +func wrap(nconn net.Conn, laddr, raddr ma.Multiaddr) Conn { + endpts := maEndpoints{ + laddr: laddr, + raddr: raddr, + } + // This sucks. However, it's the only way to reliably expose the + // underlying methods. This way, users that need access to, e.g., + // CloseRead and CloseWrite, can do so via type assertions. + switch nconn := nconn.(type) { + case *net.TCPConn: + return &struct { + *net.TCPConn + maEndpoints + }{nconn, endpts} + case *net.UDPConn: + return &struct { + *net.UDPConn + maEndpoints + }{nconn, endpts} + case *net.IPConn: + return &struct { + *net.IPConn + maEndpoints + }{nconn, endpts} + case *net.UnixConn: + return &struct { + *net.UnixConn + maEndpoints + }{nconn, endpts} + case halfOpen: + return &struct { + halfOpen + maEndpoints + }{nconn, endpts} + default: + return &struct { + net.Conn + maEndpoints + }{nconn, endpts} + } +} + +func listenPacket(network, address string) (net.PacketConn, error) { + return net.ListenPacket(network, address) +} diff --git a/net/net_tinygo.go b/net/net_tinygo.go new file mode 100644 index 0000000..d7169be --- /dev/null +++ b/net/net_tinygo.go @@ -0,0 +1,84 @@ +//go:build tinygo + +package manet + +import ( + "errors" + "fmt" + "net" + + ma "github.com/multiformats/go-multiaddr" +) + +func parseBasicNetMaddr(maddr ma.Multiaddr) (net.Addr, error) { + network, host, err := DialArgs(maddr) + if err != nil { + return nil, err + } + + switch network { + case "tcp", "tcp4", "tcp6": + return net.ResolveTCPAddr(network, host) + case "udp", "udp4", "udp6": + return net.ResolveUDPAddr(network, host) + /* + https://github.com/tinygo-org/net/issues/23 + case "ip", "ip4", "ip6": + return net.ResolveIPAddr(network, host) + case "unix": + return net.ResolveUnixAddr(network, host) + */ + } + + return nil, fmt.Errorf("network not supported: %s", network) +} + +func wrap(nconn net.Conn, laddr, raddr ma.Multiaddr) Conn { + endpts := maEndpoints{ + laddr: laddr, + raddr: raddr, + } + // This sucks. However, it's the only way to reliably expose the + // underlying methods. This way, users that need access to, e.g., + // CloseRead and CloseWrite, can do so via type assertions. + switch nconn := nconn.(type) { + case *net.TCPConn: + return &struct { + *net.TCPConn + maEndpoints + }{nconn, endpts} + case *net.UDPConn: + return &struct { + *net.UDPConn + maEndpoints + }{nconn, endpts} + /* + case *net.IPConn: + return &struct { + *net.IPConn + maEndpoints + }{nconn, endpts} + */ + /* + case *net.UnixConn: + return &struct { + *net.UnixConn + maEndpoints + }{nconn, endpts} + */ + case halfOpen: + return &struct { + halfOpen + maEndpoints + }{nconn, endpts} + default: + return &struct { + net.Conn + maEndpoints + }{nconn, endpts} + } +} + +func listenPacket(network, address string) (net.PacketConn, error) { + return nil, errors.New("tinygo: net.ListenPacket is not implemented") +}