1
0
mirror of https://github.com/golang/go synced 2024-11-20 08:54:40 -07:00

net: reject invalid net:proto network names

R=rsc
CC=golang-dev
https://golang.org/cl/4129042
This commit is contained in:
Olivier Antoine 2011-02-16 15:03:47 -05:00 committed by Russ Cox
parent acad4e0cf9
commit df4b22fcff
3 changed files with 19 additions and 23 deletions

View File

@ -24,7 +24,7 @@ import "os"
// Dial("tcp", "127.0.0.1:123", "127.0.0.1:88")
//
func Dial(net, laddr, raddr string) (c Conn, err os.Error) {
switch prefixBefore(net, ':') {
switch net {
case "tcp", "tcp4", "tcp6":
var la, ra *TCPAddr
if laddr != "" {
@ -137,7 +137,7 @@ func Listen(net, laddr string) (l Listener, err os.Error) {
// The network string net must be a packet-oriented network:
// "udp", "udp4", "udp6", or "unixgram".
func ListenPacket(net, laddr string) (c PacketConn, err os.Error) {
switch prefixBefore(net, ':') {
switch net {
case "udp", "udp4", "udp6":
var la *UDPAddr
if laddr != "" {
@ -162,18 +162,24 @@ func ListenPacket(net, laddr string) (c PacketConn, err os.Error) {
return nil, err
}
return c, nil
case "ip", "ip4", "ip6":
var la *IPAddr
if laddr != "" {
if la, err = ResolveIPAddr(laddr); err != nil {
}
if i := last(net, ':'); i > 0 {
switch net[0:i] {
case "ip", "ip4", "ip6":
var la *IPAddr
if laddr != "" {
if la, err = ResolveIPAddr(laddr); err != nil {
return nil, err
}
}
c, err := ListenIP(net, la)
if err != nil {
return nil, err
}
return c, nil
}
c, err := ListenIP(net, la)
if err != nil {
return nil, err
}
return c, nil
}
return nil, UnknownNetworkError(net)
}

View File

@ -311,7 +311,7 @@ func DialIP(netProto string, laddr, raddr *IPAddr) (c *IPConn, err os.Error) {
if err != nil {
return
}
switch prefixBefore(net, ':') {
switch net {
case "ip", "ip4", "ip6":
default:
return nil, UnknownNetworkError(net)
@ -335,7 +335,7 @@ func ListenIP(netProto string, laddr *IPAddr) (c *IPConn, err os.Error) {
if err != nil {
return
}
switch prefixBefore(net, ':') {
switch net {
case "ip", "ip4", "ip6":
default:
return nil, UnknownNetworkError(net)

View File

@ -192,16 +192,6 @@ func count(s string, b byte) int {
return n
}
// Returns the prefix of s up to but not including the character c
func prefixBefore(s string, c byte) string {
for i, v := range s {
if v == int(c) {
return s[0:i]
}
}
return s
}
// Index of rightmost occurrence of b in s.
func last(s string, b byte) int {
i := len(s)