mox/vendor/github.com/mjl-/adns/dial.go
2024-03-08 15:31:54 +01:00

44 lines
1.1 KiB
Go

// Copyright 2010 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.
package adns
import (
"context"
"net"
"github.com/mjl-/adns/internal/bytealg"
)
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, net.UnknownNetworkError(network)
}
case "unix", "unixgram", "unixpacket":
default:
return "", 0, net.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, net.UnknownNetworkError(network)
}