Skip to content

Commit

Permalink
net: implement ResolveIPAddr with stub for lookupProtocol, take tinyg…
Browse files Browse the repository at this point in the history
…o-org#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.
  • Loading branch information
scottfeldman authored and deadprogram committed Jan 7, 2025
1 parent e4c3cdf commit 4927c84
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
33 changes: 33 additions & 0 deletions dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"context"
"errors"
"fmt"
"internal/bytealg"
"syscall"
"time"
)
Expand Down Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions iprawsock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions lookup_unix.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 4927c84

Please sign in to comment.