67 Commits

Author SHA1 Message Date
Moses Narrow 70037cf71f net/http: fix nil didTimeout deref when a client dial fails
Client.do calls didTimeout() on the error path (line 461), and the contract is
that send() returns a non-nil didTimeout whenever err != nil. TinyGo dropped
setRequestCancel but left the roundTrip error path returning the nil named
return value, so any http.Client{Timeout: ...} request whose dial failed (e.g.
connection refused) panicked with a nil func-value dereference instead of
returning the error. Return alwaysFalse on those error paths, matching the
other error returns in send().

Verified: http.Client{Timeout}.Get to a refused port now returns
"connection refused" instead of panicking.
2026-08-28 09:38:19 +02:00
Patricio Whittingslow e75c5f0526 interface: add stubs for wasm builds 2026-08-27 19:23:54 +02:00
Moses Narrow 902c8d1203 net/http: add ErrUseLastResponse, Client.CloseIdleConnections, Transport.Proxy and ProxyURL (#70)
* http: add ErrUseLastResponse and Client.CloseIdleConnections

Both are API-compatibility fills. ErrUseLastResponse is a sentinel a
CheckRedirect func returns to stop following redirects; nothing here has
to act on it beyond existing. CloseIdleConnections delegates to the
Transport when it has the method, which is what net/http does.

Programs that reference either currently fail to compile against this
package for want of a name, which is the whole cost.
2026-08-27 09:08:14 +02:00
deadprogram 888be68522 Replace upgrade.sh with Go-based upgrade tool in tools/upgrade/
Rewrite the bash upgrade script as a standalone Go program for better
portability and maintainability. The tool preserves all existing behavior:
three-category file classification, dry-run mode, Go source resolution
(gvm/GOROOT/download), 3-way merge via diff3, and colored output.

Build: cd tools/upgrade && go build -o upgrade .
Run:   ./tools/upgrade/upgrade --dry-run

Signed-off-by: deadprogram <ron@hybridgroup.com>
2026-08-24 15:36:10 +02:00
Moses Narrow 1638a87512 net/http: add Transport.TLSHandshakeTimeout field for source compat
The TinyGo net/http Transport was missing TLSHandshakeTimeout, so callers that
set it (e.g. skywire's skysocks HTTPS-over-mesh client) fail to compile. Add the
field; the TinyGo transport does the handshake inline so it is advisory, but it
lets standard net/http source build unchanged under TinyGo.
2026-08-21 13:54:42 +02:00
Moses Narrow 2be2e34090 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.
2026-08-20 22:57:28 +02:00
Moses Narrow 7bf471a0a9 net: add default host netdev backed by raw Linux syscalls
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/net#28
2026-08-20 22:57:28 +02:00
Pat Whittingslow d5da3ddeef net: add UDP listen, resolver, DNSError, and TCP listener APIs for wasm (#63)
* net: add UDP listen, resolver, DNSError, and TCP listener APIs for wasm

TinyGo's net package was missing several symbols that upstream Go's
js/wasm net declares, blocking builds that pull pion/transport, pion/dtls,
and similar (via netbird's WASM client). Add them, backed by the existing
netdev abstraction so they compile for all targets and no-op cleanly under
nopNetdev (matching Go's js runtime behavior):

- DNSError type (dnserror.go), copied from Go 1.26.2.
- Resolver + DefaultResolver with LookupHost/LookupIP/LookupIPAddr/
  LookupNetIP/LookupPort; LookupHost/LookupIP package funcs; Dialer.Resolver
  field for API compatibility.
- UDPConn: ListenUDP, ReadFromUDP(AddrPort), WriteToUDP(AddrPort),
  SetReadBuffer, SetWriteBuffer.
- TCPConn: CloseRead, SetNoDelay, SetReadBuffer, SetWriteBuffer,
  ReadFrom(io.Reader).
- TCPListener: ListenTCP, AcceptTCP, SetDeadline; ListenPacket package func.
- Interface: Addrs, MulticastAddrs stubs.

* more complete PR for adding more net support (#64)

* format unixsock
2026-07-06 20:24:35 +02:00
deadprogram 1026408a38 http: fix t.roundTrip undefined on js/wasm builds
The js/wasm RoundTrip fallback called t.roundTrip(req), an upstream
Go private method that was never backported to TinyGo's Transport stub.

Replace the call with an explicit error return, since the empty
Transport has no dial capability and cannot perform a fallback
round-trip.

Fixes #52

Signed-off-by: deadprogram <ron@hybridgroup.com>
2026-04-27 08:33:56 +01:00
deadprogram e54965ed5e net: use internal/itoa for Go < 1.26 to fix build with older Go versions
internal/strconv was added in Go 1.26. For older Go versions, use
internal/itoa (already present in TinyGo) via a build tag shim.

Fixes TinyGo #5332

Signed-off-by: deadprogram <ron@hybridgroup.com>
2026-04-22 13:24:05 +01:00
Martin Robomaze b2d3a2b05b Add Transporter for wasm js target (#42)
* added js target specific roundtrip

* roundtrip_js dial parameter fix
2026-04-17 10:41:57 +02:00
Ron Evans 90d59cb488 udp: add UDPAddrFromAddrPort from Big Go implementation (#49)
Signed-off-by: deadprogram <ron@hybridgroup.com>
2026-04-16 12:07:07 -03:00
deadprogram 35e809e0e5 Upgrade net package from Go 1.21.4 to Go 1.26.2
Backport upstream Go standard library changes to TinyGo's net package.

Unmodified files (18) replaced directly from Go 1.26.2 source.
Modified files (22) merged via 3-way diff, preserving all TinyGo
netdev adaptations, TINYGO markers, and embedded-device constraints.

Notable upstream changes included:
- net/http: cookie handling improvements, fs enhancements,
  reverse proxy updates, chunked encoding fixes
- net/http: ServeMux routing and pattern matching updates
- net: IP parsing and MAC address handling improvements
- Various doc link syntax modernization (e.g. [Dial], [Buffers])

TinyGo-only files (netdev.go, tlssock.go) unchanged.
All TINYGO comment markers preserved.
2026-04-13 22:55:27 +01:00
deadprogram a3417d034f Add upgrade automation script and tooling
- upgrade.sh: automates backporting upstream Go net package changes
  - Downloads Go source tarballs for CUR and UPSTREAM versions
  - Copies unmodified files directly from upstream
  - Performs 3-way merge (diff3) for TinyGo-modified files
  - Detects per-file CUR version from TINYGO headers
  - Generates detailed diff reports in .upgrade-report/
  - Supports --dry-run mode for previewing changes
- .gitignore: add .upgrade-work/ and .upgrade-report/ directories
2026-04-13 22:52:34 +01:00
Itxaka fbef30f4ed feat: add pprof package (#44)
Signed-off-by: Itxaka <itxaka@kairos.io>
2026-04-01 16:49:26 +02:00
Itxaka 3f2503bfe4 Add httputil package
Signed-off-by: Itxaka <itxaka@kairos.io>
2026-04-01 16:40:18 +02:00
Char bb43b40f19 http: add CheckRedirect field to Client for compatibility with oauth2
This change introduces a CheckRedirect field to the http.Client struct in TinyGo, 
mirroring the behavior of Go's standard library http.Client.

The purpose of this addition is to resolve build errors in projects that rely 
on packages (such as golang.org/x/oauth2) expecting the Client to have a 
CheckRedirect field. The field provides no functional change within TinyGo’s 
HTTP client at this time — it is only included for API compatibility and 
compilation success. There is no effect on runtime behavior unless explicitly 
used by downstream code.
2025-09-18 01:04:30 -04:00
v1rtl 983d88dd7a add stub httptrace methods and interfaces 2025-08-10 12:22:57 +02:00
Dmytro 77be3968d1 add support for PathValue and SetPathValue from go v1.23.7 (#38)
* add support for PathValue and SetPathValue from go v1.23.7
* add TINYGO notice to pattern.go, proper copy paste of PathValue func
2025-06-11 14:26:56 +02:00
Scott Feldman ca7cd08f85 add lookupProtocol for windows
This fixes a problem with https://github.com/tinygo-org/net/pull/36
which causes the Windows TinyGO build to fail.  The problem is the
function lookupProtocol was missing for Windows build.  This PR adds the
function for Windows.

I can't really test this because I don't have a Windows system, but by
inspection is seems like it's correct.
2025-02-17 12:31:16 +01:00
Scott Feldman 4927c84aa4 net: implement ResolveIPAddr with stub for lookupProtocol, take #2
This is a rework of PR#24 based on review comments.  The rework mostly
involved moving changes to the proper files, aligning with upstream Go
src/net file layout.
2025-01-07 13:35:09 +01:00
Dmitry Shemin e4c3cdf599 Add httptest package 2025-01-04 23:37:56 +01:00
Scott Feldman a237059610 add NOP netdev as default
Initialize netdev to dummy NOP netdev that gracefully errors out all netdev
interface calls.  This is to catch cases where useNetdev() was not
called by the app to set netdev.
2024-07-20 15:46:21 +02:00
Ethan Lewis 7f3a3c9177 fix: add missing UnknownNetworkError 2024-07-16 07:57:37 +02:00
Rajat Jindal 001ceab784 add bare minimum DefaultTransport (#32)
* add bare minimum DefaultTransport

Signed-off-by: Rajat Jindal <rajatjindal83@gmail.com>

* use transport if explicitly configured by client

Signed-off-by: Rajat Jindal <rajatjindal83@gmail.com>

---------

Signed-off-by: Rajat Jindal <rajatjindal83@gmail.com>
2024-07-09 13:26:29 -07:00
Christian Stewart d9132d2a52 net: add ResolveUnixAddr
Signed-off-by: Christian Stewart <christian@aperture.us>
2024-04-13 12:33:35 +02:00
deadprogram a79417481d net: add back Buffers implementation which got lost along the way to the switchover to this package
Signed-off-by: deadprogram <ron@hybridgroup.com>
2024-03-09 10:19:45 -05:00
Elias Naur fcee4f5c46 add InterfaceAddrs stub
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2023-12-31 10:46:32 +01:00
Scott Feldman c134160ae4 correct netdever Accept() prototype
According to man page accept(2), accept returns new client sockfd and
remote peer ip:port.  This patch corrects the Accept() prototype in the
netdever interface to not take in an ip:port arg, but rather return an
ip:port for remote peer.
2023-12-18 10:15:35 +01:00
Scott Feldman 4bc93e2fd8 stub out more types/funcs to compile against golang.org/x/net/internal/socket (#17)
* stub out more types/funcs to compile against golang.org/x/net/internal/socket

These are changes need to compile github.com/domainr/dnsr/ with TinyGo. See issue tinygo-org/net#14.

These change are mostly to fix missing symbols in src/net. Missing types and functions are cut-and-pasted from go1.21.4. Functions are stubbed out returning errors.New("not implemented").

DNRS is compiled by running tinygo test:

   sfeldma@nuc:~/work/dnsr$ tinygo test -target=wasi

With this patch, and a corresponding patch for tinygo/ to fixup crypto/tls, you should get a clean compile.
2023-12-17 15:29:45 +01:00
deadprogram 4f0d9658ab tcpsock: define TCPListener as listener
Signed-off-by: deadprogram <ron@hybridgroup.com>
2023-12-09 10:34:42 +01:00
soypat 1c4a014aa7 add some docs to netdev interface 2023-12-08 14:21:22 +01:00
deadprogram 5fb7acc7b0 http: add Client RoundTripper/Transport
Signed-off-by: deadprogram <ron@hybridgroup.com>
2023-12-08 00:26:31 +01:00
Scott Feldman d6e5efd40a fix 'panic: runtime error: slice smaller than array' bug 2023-12-06 07:46:58 +01:00
soypat 1c04b2c40d fix netdev.Addr docstring 2023-12-04 11:38:39 +01:00
soypat f3548296e1 refurbish internal netdever interface to use netip types instead of net.IP 2023-12-04 11:38:39 +01:00
Scott Feldman 7c11b88ce6 update with changes from go1.20.5 to go1.21.4 2023-11-24 18:26:04 +01:00
Scott Feldman be25e1a28e revert some changes going from 1.19.3 to 1.20.5
There are some new features added in Go 1.20 like string.CutPrefix.
Those will have to wait to be ported until TinyGo MIN is >= 1.20.
2023-07-07 20:00:29 +02:00
Scott Feldman c3616d9365 update 'net' package from 1.19.3 to 1.20.5 2023-07-07 20:00:29 +02:00
Scott Feldman d0c97a5292 README: add documentation on maintaining 'net' package 2023-07-07 20:00:29 +02:00
Scott Feldman 8612dcd77b netdev: improve documentation
Call out netdev as L3/L4 interface for TinyGo, to differentiate between
netlink which is stricly L2.
2023-07-07 20:00:29 +02:00
Scott Feldman 044dc8d374 netdev: move GetIPAddr() from netlink to netdev
Netlink is for L2; netdev is for L3/L4
2023-07-07 20:00:29 +02:00
Scott Feldman 1b87eff727 add ErrClosed error type for natiu-mqtt 2023-07-07 20:00:29 +02:00
Scott Feldman 6bb697535a Unexport net constants copied over from syscall 2023-07-07 20:00:29 +02:00
Scott Feldman 2d46776c18 move syscall constants for networking into net space to avoid windows build issue 2023-07-07 20:00:29 +02:00
Scott Feldman d939a800a7 BUG Fix: use port :80 if server.Addr is empty 2023-07-07 20:00:29 +02:00
Scott Feldman 7898c5e946 BUG Fix: return proper net.OpError's for Read/Write operations
Need to return the same error structure/content as regular Go for
net.Conn Read/Write operations.  Found/fixed when testing deadlines
on Read/Write operations.
2023-07-07 20:00:29 +02:00
Scott Feldman ad745d61ae udpsock: remove TODO comment on ephemeral ports being racy 2023-07-07 20:00:29 +02:00
Scott Feldman 331151cafe move IPPROTO_TLS to netdev to avoid src/syscall dependency 2023-07-07 20:00:29 +02:00
Scott Feldman 693edae782 Add network device driver model, netdev
This PR adds a network device driver model called netdev. There will be a companion PR for TinyGo drivers to update the netdev drivers and network examples. This PR covers the core "net" package.

An RFC for the work is here: #tinygo-org/drivers#487. Some things have changed from the RFC, but nothing major.

The "net" package is a partial port of Go's "net" package, version 1.19.3. The src/net/README file has details on what is modified from Go's "net" package.

Most "net" features are working as they would in normal Go. TCP/UDP/TLS protocol support is there. As well as HTTP client and server support. Standard Go network packages such as golang.org/x/net/websockets and Paho MQTT client work as-is. Other packages are likely to work as-is.

Testing results are here (https://docs.google.com/spreadsheets/d/e/2PACX-1vT0cCjBvwXf9HJf6aJV2Sw198F2ief02gmbMV0sQocKT4y4RpfKv3dh6Jyew8lQW64FouZ8GwA2yjxI/pubhtml?gid=1013173032&single=true).
2023-07-07 20:00:29 +02:00