mirror of
https://github.com/tinygo-org/net.git
synced 2026-09-10 06:29:27 +00:00
net: support IPv6 in the host netdev and TCP/UDP plumbing
Make the net package address-family aware instead of IPv4-only: DialTCP, listenTCP and DialUDP now choose AF_INET or AF_INET6 from the target address (socketFamily), the "only ipv4 supported" guard is replaced by a 4-or-16 byte check, and "tcp6"/"udp6" network names are accepted. In the host netdev, sockaddrFromParts builds a SockaddrInet6 for IPv6 addresses (SockaddrInet4 otherwise), Accept decodes both families, GetHostByName and the /etc/hosts lookup accept IPv6, and the stub resolver now queries AAAA after A. Name resolution still prefers IPv4, so the "4"/"6" suffix is advisory for host names; this is documented on Dial/Listen. Link-local IPv6 zones are not mapped to a scope id. Verified on linux/amd64 with tinygo: IPv6 loopback Listen/Accept/Dial over [::1], AAAA fallback for an IPv6-only host name, and IPv4 behaviour unchanged.
This commit is contained in:
+6
-6
@@ -94,7 +94,7 @@ func (a *TCPAddr) opAddr() Addr {
|
||||
func ResolveTCPAddr(network, address string) (*TCPAddr, error) {
|
||||
|
||||
switch network {
|
||||
case "tcp", "tcp4":
|
||||
case "tcp", "tcp4", "tcp6":
|
||||
default:
|
||||
return nil, fmt.Errorf("Network '%s' not supported", network)
|
||||
}
|
||||
@@ -161,7 +161,7 @@ type TCPConn struct {
|
||||
func DialTCP(network string, laddr, raddr *TCPAddr) (*TCPConn, error) {
|
||||
|
||||
switch network {
|
||||
case "tcp", "tcp4":
|
||||
case "tcp", "tcp4", "tcp6":
|
||||
default:
|
||||
return nil, errors.New("Network not supported: '" + network + "'")
|
||||
}
|
||||
@@ -174,11 +174,11 @@ func DialTCP(network string, laddr, raddr *TCPAddr) (*TCPConn, error) {
|
||||
|
||||
if raddr.IP.IsUnspecified() {
|
||||
return nil, errors.New("Sorry, localhost isn't available on Tinygo")
|
||||
} else if len(raddr.IP) != 4 {
|
||||
return nil, errors.New("only ipv4 supported")
|
||||
} else if len(raddr.IP) != 4 && len(raddr.IP) != 16 {
|
||||
return nil, errors.New("invalid IP address")
|
||||
}
|
||||
|
||||
fd, err := netdev.Socket(_AF_INET, _SOCK_STREAM, _IPPROTO_TCP)
|
||||
fd, err := netdev.Socket(socketFamily(raddr.IP), _SOCK_STREAM, _IPPROTO_TCP)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -379,7 +379,7 @@ func (l *listener) Addr() Addr {
|
||||
}
|
||||
|
||||
func listenTCP(laddr *TCPAddr) (Listener, error) {
|
||||
fd, err := netdev.Socket(_AF_INET, _SOCK_STREAM, _IPPROTO_TCP)
|
||||
fd, err := netdev.Socket(socketFamily(laddr.IP), _SOCK_STREAM, _IPPROTO_TCP)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user