-
Notifications
You must be signed in to change notification settings - Fork 14
Network support for WASI preview 2 #40
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
base: main
Are you sure you want to change the base?
Conversation
dsseng
commented
Aug 2, 2025
- Add _AF_INET6 for WASI sockets
- HACK: explicitly default TCP listeners to 0.0.0.0
- Initial WASIp2 network driver
- fixup! Initial WASIp2 network driver
- net: wasip2: implement GetHostByName
- net: wasip2: refactor, support TCP connect
- net: wasip2: UDP client
Very interesting work in progress here @dsseng 👀 |
Okay, UDP server needs more work in common code, let's review this as is and keep UDP server for later |
Paging Dr. @dgryski 😸 |
@dsseng this is really interesting. Can you please point me to some example of this in use, so I can try it out! |
Not at my PC now. Please try http Server and Client samples from Go by example, should work for TCP. I probably also used the UDP client from there |
@dsseng perhaps when you are back around, you can provide specific instructions on how you built and tested this? What code you built, what command you built it with, and how you executed it please. Thank you! |
This includes PATH env for the needed components. These are the samples that should work: Full command: |
Do you need the newer (v123) binaryen from
with these versions:
Any thoughts? |
Weird. I did not see such an error. Is it HTTP client? Try http server, and try adding some logging. I will probably be able to try to reproduce in no shorter than a week, unfortunately too busy now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are the tests for this beyond the standard net tests? Do we need to investigate fuzzing this driver?
a := []udp.OutgoingDatagram{ | ||
{ | ||
Data: cm.NewList(&buf[0], len(buf)), | ||
// RemoteAddress: cm.Some(c.raddr), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be commented out? If so, remove. Otherwise, probably worth fixing. Does the remote address get set somewhere else?
This looked interesting for some work I am doing, so I tested it, but I am encountering an issue when sending data larger than 4096 bytes. What I tested: package main
import (
"fmt"
"net"
)
func main() {
conn, err := net.Dial("tcp", "localhost:8092")
if err != nil {
panic(err)
}
defer conn.Close()
buf := make([]byte, 4097)
n, err := conn.Read(buf)
if err != nil {
panic(err)
}
fmt.Printf("Received %d bytes\n", n)
} package main
import (
"fmt"
"net"
)
func main() {
ln, err := net.Listen("tcp", ":8092")
if err != nil {
panic(err)
}
defer ln.Close()
fmt.Println("Server listening on :8092")
conn, err := ln.Accept()
if err != nil {
panic(err)
}
defer conn.Close()
data := make([]byte, 4097)
for i := range data {
data[i] = byte(i % 256)
}
n, err := conn.Write(data)
if err != nil {
panic(err)
}
fmt.Printf("Sent %d bytes\n", n)
} In one shell run: tinygo build -target=wasip2 -o server.wasm tcp-server.go
wasmtime run -Sinherit-network server.wasm In another shell run: go run tcp-client.go You get this error:
When running normally with I think the offending code is this part: https://github.com/tinygo-org/net/blob/043bd3f10e41db40d971bbc3bcffe10b8c186214/tcp_wasip2.go#L141C1-L141C57 It is essentially the same issue that I am having with printing larger data that I reported here: tinygo-org/tinygo#5012 Sorry if I am on the wrong track, new to WASM :) |
@dgryski @ErikFrankling thanks for your feedback! Sorry for long delay. Here's the test program for TCP: PATH=$PATH:/tmp/wasm-tools-1.239.0-x86_64-linux:/tmp/binaryen-version_124/bin/ GOOS=wasip2 GOARCH=wasm /tmp/tinygo/build/tinygo build -o /tmp/srv.wasm /tmp/srv.go && WASMTIME_BACKTRACE_DETAILS=1 /tmp/wasmtime-v36.0.2-x86_64-linux/wasmtime run -S inherit-network=y,inherit-env=y,allow-ip-name-lookup=y /tmp/srv.wasm Serverpackage main
import (
"fmt"
"net"
"slices"
)
func main() {
ln, err := net.Listen("tcp", ":8092")
if err != nil {
panic(err)
}
defer ln.Close()
fmt.Println("Server listening on :8092")
for {
conn, err := ln.Accept()
if err != nil {
panic(err)
}
defer conn.Close()
buf := make([]byte, 64*1024)
n, err := conn.Read(buf)
if err != nil {
panic(err)
}
fmt.Println(string(buf[:50]) + "...")
fmt.Println("..." + string(buf[n-50:n]))
fmt.Printf("Received %d bytes\n", n)
mirrored := buf[:n]
slices.Reverse(mirrored)
n, err = conn.Write(mirrored)
if err != nil {
panic(err)
}
fmt.Printf("Sent %d bytes\n", n)
}
} PATH=$PATH:/tmp/wasm-tools-1.239.0-x86_64-linux:/tmp/binaryen-version_124/bin/ GOOS=wasip2 GOARCH=wasm /tmp/tinygo/build/tinygo build -o /tmp/cl.wasm /tmp/cl.go && WASMTIME_BACKTRACE_DETAILS=1 /tmp/wasmtime-v36.0.2-x86_64-linux/wasmtime run -S inherit-network=y,inherit-env=y,allow-ip-name-lookup=y /tmp/cl.wasm Client - feel free to replace 12000 with sth less or more, e.g. 20 to send 61 bytes totalpackage main
import (
"fmt"
"net"
"strconv"
)
func main() {
conn, err := net.Dial("tcp", "localhost:8092")
if err != nil {
panic(err)
}
defer conn.Close()
str := "hello,"
for i := range 12000 {
str += strconv.Itoa(i) + ","
}
str += "world"
n, err := conn.Write([]byte(str))
if err != nil {
panic(err)
}
fmt.Printf("Sent %d bytes\n", n)
buf := make([]byte, 64*1024)
n, err = conn.Read(buf)
if err != nil {
panic(err)
}
fmt.Println(string(buf[:50]) + "...")
fmt.Println("..." + string(buf[n-50:n]))
fmt.Printf("Received %d bytes\n", n)
} Haven't touched the UDP part yet |
|
||
const ( | ||
_AF_INET = 0x2 | ||
_AF_INET6 = 0xA |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't end up really used, nor did I test IPv6. Should we drop it for now?