diff options
Diffstat (limited to 'src/pkg/net/ipsock.go')
-rw-r--r-- | src/pkg/net/ipsock.go | 92 |
1 files changed, 46 insertions, 46 deletions
diff --git a/src/pkg/net/ipsock.go b/src/pkg/net/ipsock.go index f61d22ac9..879bae33e 100644 --- a/src/pkg/net/ipsock.go +++ b/src/pkg/net/ipsock.go @@ -7,8 +7,8 @@ package net import ( - "os"; - "syscall"; + "os" + "syscall" ) // Should we try to use the IPv4 socket interface if we're @@ -22,11 +22,11 @@ func kernelSupportsIPv6() bool { if syscall.OS == "freebsd" { return false } - fd, e := syscall.Socket(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP); + fd, e := syscall.Socket(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP) if fd >= 0 { syscall.Close(fd) } - return e == 0; + return e == 0 } var preferIPv4 = !kernelSupportsIPv6() @@ -34,22 +34,22 @@ var preferIPv4 = !kernelSupportsIPv6() // TODO(rsc): if syscall.OS == "linux", we're supposd to read // /proc/sys/net/core/somaxconn, // to take advantage of kernels that have raised the limit. -func listenBacklog() int { return syscall.SOMAXCONN } +func listenBacklog() int { return syscall.SOMAXCONN } // Internet sockets (TCP, UDP) // A sockaddr represents a TCP or UDP network address that can // be converted into a syscall.Sockaddr. type sockaddr interface { - Addr; - sockaddr(family int) (syscall.Sockaddr, os.Error); - family() int; + Addr + sockaddr(family int) (syscall.Sockaddr, os.Error) + family() int } func internetSocket(net string, laddr, raddr sockaddr, proto int, mode string, toAddr func(syscall.Sockaddr) Addr) (fd *netFD, err os.Error) { // Figure out IP version. // If network has a suffix like "tcp4", obey it. - family := syscall.AF_INET6; + family := syscall.AF_INET6 switch net[len(net)-1] { case '4': family = syscall.AF_INET @@ -65,7 +65,7 @@ func internetSocket(net string, laddr, raddr sockaddr, proto int, mode string, t } } - var la, ra syscall.Sockaddr; + var la, ra syscall.Sockaddr if laddr != nil { if la, err = laddr.sockaddr(family); err != nil { goto Error @@ -76,25 +76,25 @@ func internetSocket(net string, laddr, raddr sockaddr, proto int, mode string, t goto Error } } - fd, err = socket(net, family, proto, 0, la, ra, toAddr); + fd, err = socket(net, family, proto, 0, la, ra, toAddr) if err != nil { goto Error } - return fd, nil; + return fd, nil Error: - addr := raddr; + addr := raddr if mode == "listen" { addr = laddr } - return nil, &OpError{mode, net, addr, err}; + return nil, &OpError{mode, net, addr, err} } func getip(fd int, remote bool) (ip []byte, port int, ok bool) { // No attempt at error reporting because // there are no possible errors, and the // caller won't report them anyway. - var sa syscall.Sockaddr; + var sa syscall.Sockaddr if remote { sa, _ = syscall.Getpeername(fd) } else { @@ -106,7 +106,7 @@ func getip(fd int, remote bool) (ip []byte, port int, ok bool) { case *syscall.SockaddrInet6: return &sa.Addr, sa.Port, true } - return; + return } func ipToSockaddr(family int, ip IP, port int) (syscall.Sockaddr, os.Error) { @@ -118,12 +118,12 @@ func ipToSockaddr(family int, ip IP, port int) (syscall.Sockaddr, os.Error) { if ip = ip.To4(); ip == nil { return nil, os.EINVAL } - s := new(syscall.SockaddrInet4); + s := new(syscall.SockaddrInet4) for i := 0; i < IPv4len; i++ { s.Addr[i] = ip[i] } - s.Port = port; - return s, nil; + s.Port = port + return s, nil case syscall.AF_INET6: if len(ip) == 0 { ip = IPzero @@ -137,27 +137,27 @@ func ipToSockaddr(family int, ip IP, port int) (syscall.Sockaddr, os.Error) { if ip = ip.To16(); ip == nil { return nil, os.EINVAL } - s := new(syscall.SockaddrInet6); + s := new(syscall.SockaddrInet6) for i := 0; i < IPv6len; i++ { s.Addr[i] = ip[i] } - s.Port = port; - return s, nil; + s.Port = port + return s, nil } - return nil, os.EINVAL; + return nil, os.EINVAL } // Split "host:port" into "host" and "port". // Host cannot contain colons unless it is bracketed. func splitHostPort(hostport string) (host, port string, err os.Error) { // The port starts after the last colon. - i := last(hostport, ':'); + i := last(hostport, ':') if i < 0 { - err = &AddrError{"missing port in address", hostport}; - return; + err = &AddrError{"missing port in address", hostport} + return } - host, port = hostport[0:i], hostport[i+1:]; + host, port = hostport[0:i], hostport[i+1:] // Can put brackets around host ... if len(host) > 0 && host[0] == '[' && host[len(host)-1] == ']' { @@ -165,11 +165,11 @@ func splitHostPort(hostport string) (host, port string, err os.Error) { } else { // ... but if there are no brackets, no colons. if byteIndex(host, ':') >= 0 { - err = &AddrError{"too many colons in address", hostport}; - return; + err = &AddrError{"too many colons in address", hostport} + return } } - return; + return } // Join "host" and "port" into "host:port". @@ -179,50 +179,50 @@ func joinHostPort(host, port string) string { if byteIndex(host, ':') >= 0 { return "[" + host + "]:" + port } - return host + ":" + port; + return host + ":" + port } // Convert "host:port" into IP address and port. func hostPortToIP(net, hostport string) (ip IP, iport int, err os.Error) { - host, port, err := splitHostPort(hostport); + host, port, err := splitHostPort(hostport) if err != nil { goto Error } - var addr IP; + var addr IP if host != "" { // Try as an IP address. - addr = ParseIP(host); + addr = ParseIP(host) if addr == nil { // Not an IP address. Try as a DNS name. - _, addrs, err1 := LookupHost(host); + _, addrs, err1 := LookupHost(host) if err1 != nil { - err = err1; - goto Error; + err = err1 + goto Error } - addr = ParseIP(addrs[0]); + addr = ParseIP(addrs[0]) if addr == nil { // should not happen - err = &AddrError{"LookupHost returned invalid address", addrs[0]}; - goto Error; + err = &AddrError{"LookupHost returned invalid address", addrs[0]} + goto Error } } } - p, i, ok := dtoi(port, 0); + p, i, ok := dtoi(port, 0) if !ok || i != len(port) { - p, err = LookupPort(net, port); + p, err = LookupPort(net, port) if err != nil { goto Error } } if p < 0 || p > 0xFFFF { - err = &AddrError{"invalid port", port}; - goto Error; + err = &AddrError{"invalid port", port} + goto Error } - return addr, p, nil; + return addr, p, nil Error: - return nil, 0, err; + return nil, 0, err } |