diff --git a/dial.go b/dial.go index b92a798..31114e6 100644 --- a/dial.go +++ b/dial.go @@ -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. diff --git a/iprawsock.go b/iprawsock.go index 9895c06..052cd36 100644 --- a/iprawsock.go +++ b/iprawsock.go @@ -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 { diff --git a/lookup_unix.go b/lookup_unix.go new file mode 100644 index 0000000..e683140 --- /dev/null +++ b/lookup_unix.go @@ -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") +}