Skip to content

Commit 42d5845

Browse files
committed
Put current method behind a build tag
1 parent fadb8bc commit 42d5845

File tree

3 files changed

+103
-10
lines changed

3 files changed

+103
-10
lines changed

README.md

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,39 @@ func main() {
8484

8585
## Name Resolution
8686

87-
Go has a built-in name resolver that sidesteps CGO (e.g. `getaddrinfo(3)`)
88-
calls.
87+
There are two methods available for resolving a set of IP addresses
88+
for a hostname.
8989

90-
This library will automatically configure the `net.DefaultResolver`
91-
from the standard library to use the `Dial` function from this library.
92-
You just need the following import somewhere:
90+
### getaddrinfo
91+
92+
The `sock_getaddrinfo` host function is used to implement name resolution.
93+
This requires WasmEdge, or a WasmEdge compatible WASI layer
94+
(e.g. [wasi-go](http://github.com/stealthrocket/wasi-go)).
95+
96+
When using this method, the standard library resolver **will not work**. You
97+
_cannot_ use `net.DefaultResolver`, `net.LookupIP`, etc. with this approach
98+
because the standard library does not allow us to patch it with an alternative
99+
implementation.
100+
101+
Note that `sock_getaddrinfo` may block!
102+
103+
### Pure Go Resolver
104+
105+
The pure Go name resolver is not currently enabled for GOOS=wasip1.
106+
107+
The following series of CLs will change this: https://go-review.googlesource.com/c/go/+/500576.
108+
This will hopefully land in Go v1.22 in ~February 2024.
109+
110+
If you're using a version of Go that has the CL's included, you can
111+
instruct this library to use the pure Go resolver by including the
112+
`purego` build tag.
113+
114+
The library will then automatically configure the `net.DefaultResolver`.
115+
All you need is the following import somewhere in your application:
93116

94117
```go
95118
import _ "github.com/stealthrocket/net"
96119
```
97120

98121
You should then be able to use the lookup functions from the standard
99122
library (e.g. `net.LookupIP(host)`).
100-
101-
Note that name resolution currently depends on the following series of CLs:
102-
https://go-review.googlesource.com/c/go/+/500576

dial_wasip1.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
)
1212

1313
func init() {
14-
net.DefaultResolver.Dial = DialContext
15-
1614
if t, ok := http.DefaultTransport.(*http.Transport); ok {
1715
t.DialContext = DialContext
1816
}

lookup_wasip1_purego.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//go:build wasip1 && purego
2+
3+
package net
4+
5+
import (
6+
"fmt"
7+
"net"
8+
)
9+
10+
func init() {
11+
net.DefaultResolver.Dial = DialContext
12+
}
13+
14+
func lookupAddr(context, network, address string) (net.Addr, error) {
15+
switch network {
16+
case "tcp", "tcp4", "tcp6":
17+
case "udp", "udp4", "udp6":
18+
case "unix", "unixgram":
19+
return &net.UnixAddr{Name: address, Net: network}, nil
20+
default:
21+
return nil, fmt.Errorf("not implemented: %s", network)
22+
}
23+
host, portstr, err := net.SplitHostPort(address)
24+
if err != nil {
25+
return nil, err
26+
}
27+
port, err := net.LookupPort(network, portstr)
28+
if err != nil {
29+
return nil, err
30+
}
31+
if host == "" {
32+
if context == "listen" {
33+
switch network {
34+
case "tcp", "tcp4":
35+
return &net.TCPAddr{IP: net.IPv4zero, Port: port}, nil
36+
case "tcp6":
37+
return &net.TCPAddr{IP: net.IPv6zero, Port: port}, nil
38+
}
39+
}
40+
return nil, fmt.Errorf("invalid address %q for %s", address, context)
41+
}
42+
ips, err := net.LookupIP(host)
43+
if err != nil {
44+
return nil, err
45+
}
46+
if network == "tcp" || network == "tcp4" {
47+
for _, ip := range ips {
48+
if len(ip) == net.IPv4len {
49+
return &net.TCPAddr{IP: ip, Port: port}, nil
50+
}
51+
}
52+
}
53+
if network == "tcp" || network == "tcp6" {
54+
for _, ip := range ips {
55+
if len(ip) == net.IPv6len {
56+
return &net.TCPAddr{IP: ip, Port: port}, nil
57+
}
58+
}
59+
}
60+
if network == "udp" || network == "udp4" {
61+
for _, ip := range ips {
62+
if len(ip) == net.IPv4len {
63+
return &net.UDPAddr{IP: ip, Port: port}, nil
64+
}
65+
}
66+
}
67+
if network == "udp" || network == "udp6" {
68+
for _, ip := range ips {
69+
if len(ip) == net.IPv6len {
70+
return &net.UDPAddr{IP: ip, Port: port}, nil
71+
}
72+
}
73+
}
74+
return nil, fmt.Errorf("cannot listen on %q", host)
75+
}

0 commit comments

Comments
 (0)