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:
Moses Narrow
2026-06-23 18:19:40 -05:00
committed by Ron Evans
parent 7bf471a0a9
commit 2be2e34090
5 changed files with 105 additions and 53 deletions
+11
View File
@@ -10,6 +10,7 @@ import (
const (
_AF_INET = 0x2
_AF_INET6 = 0xa
_SOCK_STREAM = 0x1
_SOCK_DGRAM = 0x2
_SOL_SOCKET = 0x1
@@ -36,6 +37,16 @@ func useNetdev(dev netdever) {
netdev = dev
}
// socketFamily returns the address family (_AF_INET or _AF_INET6) to use for a
// socket targeting ip. A nil/zero-length IP (e.g. a wildcard listen address)
// defaults to IPv4.
func socketFamily(ip IP) int {
if len(ip) == 16 && ip.To4() == nil {
return _AF_INET6
}
return _AF_INET
}
// netdever is TinyGo's OSI L3/L4 network/transport layer interface. Network
// drivers implement the netdever interface, providing a common network L3/L4
// interface to TinyGo's "net" package. net.Conn implementations (TCPConn,