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.