Skip to content

Commit 4927c84

Browse files
scottfeldmandeadprogram
authored andcommitted
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.
1 parent e4c3cdf commit 4927c84

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

dial.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"context"
1717
"errors"
1818
"fmt"
19+
"internal/bytealg"
1920
"syscall"
2021
"time"
2122
)
@@ -191,6 +192,38 @@ func (lc *ListenConfig) ListenPacket(ctx context.Context, network, address strin
191192
return nil, errors.New("dial:ListenConfig:ListenPacket not implemented")
192193
}
193194

195+
func parseNetwork(ctx context.Context, network string, needsProto bool) (afnet string, proto int, err error) {
196+
i := bytealg.LastIndexByteString(network, ':')
197+
if i < 0 { // no colon
198+
switch network {
199+
case "tcp", "tcp4", "tcp6":
200+
case "udp", "udp4", "udp6":
201+
case "ip", "ip4", "ip6":
202+
if needsProto {
203+
return "", 0, UnknownNetworkError(network)
204+
}
205+
case "unix", "unixgram", "unixpacket":
206+
default:
207+
return "", 0, UnknownNetworkError(network)
208+
}
209+
return network, 0, nil
210+
}
211+
afnet = network[:i]
212+
switch afnet {
213+
case "ip", "ip4", "ip6":
214+
protostr := network[i+1:]
215+
proto, i, ok := dtoi(protostr)
216+
if !ok || i != len(protostr) {
217+
proto, err = lookupProtocol(ctx, protostr)
218+
if err != nil {
219+
return "", 0, err
220+
}
221+
}
222+
return afnet, proto, nil
223+
}
224+
return "", 0, UnknownNetworkError(network)
225+
}
226+
194227
// Listen announces on the local network address.
195228
//
196229
// See Go "net" package Listen() for more information.

iprawsock.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ func (a *IPAddr) opAddr() Addr {
6363
return a
6464
}
6565

66+
// ResolveIPAddr returns an address of IP end point.
67+
//
68+
// The network must be an IP network name.
69+
//
70+
// If the host in the address parameter is not a literal IP address,
71+
// ResolveIPAddr resolves the address to an address of IP end point.
72+
// Otherwise, it parses the address as a literal IP address.
73+
// The address parameter can use a host name, but this is not
74+
// recommended, because it will return at most one of the host name's
75+
// IP addresses.
76+
//
77+
// See func [Dial] for a description of the network and address
78+
// parameters.
79+
func ResolveIPAddr(network, address string) (*IPAddr, error) {
80+
return nil, errors.New("ResolveIPAddr not implemented")
81+
}
82+
6683
// IPConn is the implementation of the Conn and PacketConn interfaces
6784
// for IP network connections.
6885
type IPConn struct {

lookup_unix.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// TINYGO: The following is copied and modified from Go 1.22.0 official implementation.
2+
3+
// Copyright 2011 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
//go:build unix || js || wasip1
8+
9+
package net
10+
11+
import (
12+
"context"
13+
"errors"
14+
)
15+
16+
// lookupProtocol looks up IP protocol name in /etc/protocols and
17+
// returns correspondent protocol number.
18+
func lookupProtocol(_ context.Context, name string) (int, error) {
19+
return 0, errors.New("net:lookupProtocol not implemented")
20+
}

0 commit comments

Comments
 (0)