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.
This commit is contained in:
Scott Feldman
2025-01-04 16:12:24 -08:00
committed by Ron Evans
parent e4c3cdf599
commit 4927c84aa4
3 changed files with 70 additions and 0 deletions
+33
View File
@@ -16,6 +16,7 @@ import (
"context"
"errors"
"fmt"
"internal/bytealg"
"syscall"
"time"
)
@@ -191,6 +192,38 @@ func (lc *ListenConfig) ListenPacket(ctx context.Context, network, address strin
return nil, errors.New("dial:ListenConfig:ListenPacket not implemented")
}
func parseNetwork(ctx context.Context, network string, needsProto bool) (afnet string, proto int, err error) {
i := bytealg.LastIndexByteString(network, ':')
if i < 0 { // no colon
switch network {
case "tcp", "tcp4", "tcp6":
case "udp", "udp4", "udp6":
case "ip", "ip4", "ip6":
if needsProto {
return "", 0, UnknownNetworkError(network)
}
case "unix", "unixgram", "unixpacket":
default:
return "", 0, UnknownNetworkError(network)
}
return network, 0, nil
}
afnet = network[:i]
switch afnet {
case "ip", "ip4", "ip6":
protostr := network[i+1:]
proto, i, ok := dtoi(protostr)
if !ok || i != len(protostr) {
proto, err = lookupProtocol(ctx, protostr)
if err != nil {
return "", 0, err
}
}
return afnet, proto, nil
}
return "", 0, UnknownNetworkError(network)
}
// Listen announces on the local network address.
//
// See Go "net" package Listen() for more information.
+17
View File
@@ -63,6 +63,23 @@ func (a *IPAddr) opAddr() Addr {
return a
}
// ResolveIPAddr returns an address of IP end point.
//
// The network must be an IP network name.
//
// If the host in the address parameter is not a literal IP address,
// ResolveIPAddr resolves the address to an address of IP end point.
// Otherwise, it parses the address as a literal IP address.
// The address parameter can use a host name, but this is not
// recommended, because it will return at most one of the host name's
// IP addresses.
//
// See func [Dial] for a description of the network and address
// parameters.
func ResolveIPAddr(network, address string) (*IPAddr, error) {
return nil, errors.New("ResolveIPAddr not implemented")
}
// IPConn is the implementation of the Conn and PacketConn interfaces
// for IP network connections.
type IPConn struct {
+20
View File
@@ -0,0 +1,20 @@
// TINYGO: The following is copied and modified from Go 1.22.0 official implementation.
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build unix || js || wasip1
package net
import (
"context"
"errors"
)
// lookupProtocol looks up IP protocol name in /etc/protocols and
// returns correspondent protocol number.
func lookupProtocol(_ context.Context, name string) (int, error) {
return 0, errors.New("net:lookupProtocol not implemented")
}