From 6f0354cc6755f298ac7b99b3f4004b088aa4087b Mon Sep 17 00:00:00 2001 From: Christian Stewart Date: Sat, 16 Mar 2024 13:34:49 -0700 Subject: [PATCH] feat: add tinygo support Add a separate build-tagged file for tinygo and not-tinygo (other). Stub functions not available in tinygo. Fixes build with: tinygo build -tags "tinygo purego" -target wasm -no-debug -opt=2 ./ Signed-off-by: Christian Stewart --- net/convert.go | 20 ----------- net/net.go | 44 +------------------------ net/net_other.go | 76 ++++++++++++++++++++++++++++++++++++++++++ net/net_tinygo.go | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 161 insertions(+), 63 deletions(-) create mode 100644 net/net_other.go create mode 100644 net/net_tinygo.go 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") +}