Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tinygo support #242

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions net/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
44 changes: 1 addition & 43 deletions net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
76 changes: 76 additions & 0 deletions net/net_other.go
Original file line number Diff line number Diff line change
@@ -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
}

Check warning on line 16 in net/net_other.go

View check run for this annotation

Codecov / codecov/patch

net/net_other.go#L15-L16

Added lines #L15 - L16 were not covered by tests

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)

Check warning on line 24 in net/net_other.go

View check run for this annotation

Codecov / codecov/patch

net/net_other.go#L21-L24

Added lines #L21 - L24 were not covered by tests
case "unix":
return net.ResolveUnixAddr(network, host)
}

return nil, fmt.Errorf("network not supported: %s", network)

Check warning on line 29 in net/net_other.go

View check run for this annotation

Codecov / codecov/patch

net/net_other.go#L29

Added line #L29 was not covered by tests
}

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}

Check warning on line 55 in net/net_other.go

View check run for this annotation

Codecov / codecov/patch

net/net_other.go#L51-L55

Added lines #L51 - L55 were not covered by tests
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}

Check warning on line 70 in net/net_other.go

View check run for this annotation

Codecov / codecov/patch

net/net_other.go#L61-L70

Added lines #L61 - L70 were not covered by tests
}
}

func listenPacket(network, address string) (net.PacketConn, error) {
return net.ListenPacket(network, address)
}
84 changes: 84 additions & 0 deletions net/net_tinygo.go
Original file line number Diff line number Diff line change
@@ -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")
}