Commit 73896bdc authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net: break up and simplify Dial a bit

No API or functional change.

R=jacek.masiulaniec, mikioh.mikioh, rsc
CC=golang-dev
https://golang.org/cl/4630081
parent de36d19d
...@@ -6,6 +6,28 @@ package net ...@@ -6,6 +6,28 @@ package net
import "os" import "os"
func resolveNetAddr(op, net, addr string) (a Addr, err os.Error) {
if addr == "" {
return nil, &OpError{op, net, nil, errMissingAddress}
}
switch net {
case "tcp", "tcp4", "tcp6":
a, err = ResolveTCPAddr(net, addr)
case "udp", "udp4", "udp6":
a, err = ResolveUDPAddr(net, addr)
case "unix", "unixgram", "unixpacket":
a, err = ResolveUnixAddr(net, addr)
case "ip", "ip4", "ip6":
a, err = ResolveIPAddr(net, addr)
default:
err = UnknownNetworkError(net)
}
if err != nil {
return nil, &OpError{op, net + " " + addr, nil, err}
}
return
}
// Dial connects to the address addr on the network net. // Dial connects to the address addr on the network net.
// //
// Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), // Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only),
...@@ -23,56 +45,26 @@ import "os" ...@@ -23,56 +45,26 @@ import "os"
// Dial("tcp", "[de:ad:be:ef::ca:fe]:80") // Dial("tcp", "[de:ad:be:ef::ca:fe]:80")
// //
func Dial(net, addr string) (c Conn, err os.Error) { func Dial(net, addr string) (c Conn, err os.Error) {
raddr := addr addri, err := resolveNetAddr("dial", net, addr)
if raddr == "" { if err != nil {
return nil, &OpError{"dial", net, nil, errMissingAddress} return nil, err
} }
switch net { switch ra := addri.(type) {
case "tcp", "tcp4", "tcp6": case *TCPAddr:
var ra *TCPAddr c, err = DialTCP(net, nil, ra)
if ra, err = ResolveTCPAddr(net, raddr); err != nil { case *UDPAddr:
goto Error c, err = DialUDP(net, nil, ra)
} case *UnixAddr:
c, err := DialTCP(net, nil, ra)
if err != nil {
return nil, err
}
return c, nil
case "udp", "udp4", "udp6":
var ra *UDPAddr
if ra, err = ResolveUDPAddr(net, raddr); err != nil {
goto Error
}
c, err := DialUDP(net, nil, ra)
if err != nil {
return nil, err
}
return c, nil
case "unix", "unixgram", "unixpacket":
var ra *UnixAddr
if ra, err = ResolveUnixAddr(net, raddr); err != nil {
goto Error
}
c, err = DialUnix(net, nil, ra) c, err = DialUnix(net, nil, ra)
if err != nil { case *IPAddr:
return nil, err c, err = DialIP(net, nil, ra)
} default:
return c, nil err = UnknownNetworkError(net)
case "ip", "ip4", "ip6": }
var ra *IPAddr if err != nil {
if ra, err = ResolveIPAddr(net, raddr); err != nil { return nil, &OpError{"dial", net + " " + addr, nil, err}
goto Error
}
c, err := DialIP(net, nil, ra)
if err != nil {
return nil, err
}
return c, nil
} }
err = UnknownNetworkError(net) return
Error:
return nil, &OpError{"dial", net + " " + raddr, nil, err}
} }
// Listen announces on the local network address laddr. // Listen announces on the local network address laddr.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment