dispatchrun / net

Go package implementing WASI socket extensions
Apache License 2.0
141 stars 8 forks source link

wasip1: change lookupAddr to correctly handle numeric IP addresses #8

Closed ydnar closed 1 year ago

ydnar commented 1 year ago

This PR makes 3 changes:

  1. Adds hints.flags |= AI_NUMERICHOST if lookupAddr is called with an IP address. This allows wasip1.DialContext() to accept and resolve an IP address.
  2. Adds hints.flags |= AI_NUMERICSERV if lookupAddr is called with a port number. This optimizes handling of numeric service / port numbers.
  3. Fixes IPv6 handling in getaddrinfo. This allows getaddrinfo to correctly return an IPv6 address.

Would it make sense to export lookupAddr or add a Resolver type to the wasip1 package that can resolve addresses? Given that the underlying syscall is blocking, I’d like to be able to cache DNS lookups and bypass the syscall when opening sockets.

ydnar commented 1 year ago

I also fixed an IPv6 bug in 8bf310a.

ydnar commented 1 year ago

Thanks for the PR!

Would it make sense to export lookupAddr or add a Resolver type to the wasip1 package that can resolve addresses? Given that the underlying syscall is blocking, I’d like to be able to cache DNS lookups and bypass the syscall when opening sockets.

At the very least, top-level LookupIP and LookupPort functions that are compatible with the standard net package would be useful here. We could also make it so that when you call these functions and the purego build tag was specified it just forwards the calls to the standard library.

  1. Agree. I was thinking a top-level LookupHost function that accepts a Context and returns a slice of stringified IP addresses: https://pkg.go.dev/net#LookupHost
  2. What’s the interaction with the purego build tag and this package? Does that disable syscalls? (Asking because purego breaks net.Resolver entirely, even on gotip.
chriso commented 1 year ago

What’s the interaction with the purego build tag and this package? Does that disable syscalls? (Asking because purego breaks net.Resolver entirely, even on gotip.

See https://github.com/stealthrocket/net#name-resolution. It instructs the library to use the non-blocking pure Go resolver rather than the blocking sock_getaddrinfo system call. Unfortunately the pure Go resolver doesn't work yet, we're waiting on https://go-review.googlesource.com/c/go/+/500579.