net: support IPv6 in the host netdev and TCP/UDP plumbing#60
Open
0pcom wants to merge 2 commits into
Open
Conversation
On the native linux target the net package defaulted to a NOP netdev that returns "Netdev not set" for every operation, so net.Dial/Listen, DNS and anything built on them (net/http, tls) failed at runtime. Native linux does not override the syscall package, and the TinyGo compiler lowers syscall.Syscall/RawSyscall into real system calls, so the standard library socket functions work directly (musl's omitted network module is not needed). Register a default netdev implementing the netdever interface on top of syscall.Socket/Connect/Bind/Listen/Accept/Send/Recv/SetSockOpt, plus a small UDP DNS resolver (/etc/hosts, /etc/resolv.conf) for GetHostByName. Also restore the net.DNSError type used by resolution errors. Blocking sockets under the threads scheduler; IPv4 only. This avoids the internal/poll netpoller dependency that blocked the upstream-net approach. Refs tinygo-org#28
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Makes the TinyGo
netpackage address-family aware so IPv6 works on the nativeLinux host netdev, instead of being hard-coded to IPv4.
What changes
Shared
netplumbing:DialTCP,listenTCP, andDialUDPchooseAF_INET/AF_INET6from the targetaddress via a new
socketFamily()helper instead of always usingAF_INET."only ipv4 supported"guard inDialTCPbecomes a 4-or-16-byte lengthcheck;
DialUDPgets the same guard."tcp6"/"udp6"network names are accepted.Host netdev (
netdev_native.go):sockaddrFromPartsbuilds asyscall.SockaddrInet6for IPv6 addresses(
SockaddrInet4otherwise, with the zero address as the0.0.0.0wildcard).Acceptdecodes bothSockaddrInet4andSockaddrInet6.GetHostByNameand the/etc/hostslookup accept IPv6 addresses; the stubUDP resolver now queries
AAAAafterA.Behaviour / scope
/etc/hostslookup prefers a v4 match. Because the
netdever.GetHostByNameinterface has nofamily argument, the
"4"/"6"network suffix is advisory for host names — thisis documented on
Dial/Listen. Dialing a literal IPv6 address always uses IPv6.%zone) are not resolved to a scope id.Verification
Built and run with
tinygoonlinux/amd64:net.Listen("tcp", "[::1]:…")+Accept+net.Dialround-trips,RemoteAddr()reports the[::1]peer.net.ResolveTCPAddr("tcp", "ipv6.google.com:80")returns an IPv6 (AAAA) address(A-then-AAAA fallback), while a dual-stack host (
example.com) still resolves toIPv4.
http.Get) unchanged.