summaryrefslogtreecommitdiff
path: root/src/lib/net/net.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2009-02-15 14:18:39 -0800
committerRuss Cox <rsc@golang.org>2009-02-15 14:18:39 -0800
commite61ee0bb7bec30ab196f51f90e97ae05dadf100b (patch)
tree3438f481a6b8a30aa83ed69188d0d61313291003 /src/lib/net/net.go
parentdbaaba236fc383e49e95029934d335949f363682 (diff)
downloadgolang-e61ee0bb7bec30ab196f51f90e97ae05dadf100b.tar.gz
cleanups:
get rid of _ on private names in net. fix os_test file name list. newline not needed on Errorf. R=r DELTA=305 (34 added, 2 deleted, 269 changed) OCL=25047 CL=25047
Diffstat (limited to 'src/lib/net/net.go')
-rw-r--r--src/lib/net/net.go137
1 files changed, 74 insertions, 63 deletions
diff --git a/src/lib/net/net.go b/src/lib/net/net.go
index 5d9550cc8..b81e99268 100644
--- a/src/lib/net/net.go
+++ b/src/lib/net/net.go
@@ -18,14 +18,14 @@ var (
UnknownHost = os.NewError("unknown host");
DNS_Error = os.NewError("dns error looking up host");
UnknownPort = os.NewError("unknown port");
- Unknown_SocketFamily = os.NewError("unknown socket family");
+ UnknownsocketFamily = os.NewError("unknown socket family");
)
func LookupHost(name string) (name1 string, addrs []string, err *os.Error)
// 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) {
+func splitHostPort(hostport string) (host, port string, err *os.Error) {
// The port starts after the last colon.
var i int;
for i = len(hostport)-1; i >= 0; i-- {
@@ -45,7 +45,7 @@ func _SplitHostPort(hostport string) (host, port string, err *os.Error) {
host = host[1:len(host)-1]
} else {
// ... but if there are no brackets, no colons.
- if _ByteIndex(host, ':') >= 0 {
+ if byteIndex(host, ':') >= 0 {
return "", "", BadAddress
}
}
@@ -54,9 +54,9 @@ func _SplitHostPort(hostport string) (host, port string, err *os.Error) {
// Join "host" and "port" into "host:port".
// If host contains colons, will join into "[host]:port".
-func _JoinHostPort(host, port string) string {
+func joinHostPort(host, port string) string {
// If host has colons, have to bracket it.
- if _ByteIndex(host, ':') >= 0 {
+ if byteIndex(host, ':') >= 0 {
return "[" + host + "]:" + port
}
return host + ":" + port
@@ -65,9 +65,9 @@ func _JoinHostPort(host, port string) string {
// Convert "host:port" into IP address and port.
// For now, host and port must be numeric literals.
// Eventually, we'll have name resolution.
-func _HostPortToIP(net, hostport, mode string) (ip []byte, iport int, err *os.Error) {
+func hostPortToIP(net, hostport, mode string) (ip []byte, iport int, err *os.Error) {
var host, port string;
- host, port, err = _SplitHostPort(hostport);
+ host, port, err = splitHostPort(hostport);
if err != nil {
return nil, 0, err
}
@@ -101,7 +101,7 @@ func _HostPortToIP(net, hostport, mode string) (ip []byte, iport int, err *os.Er
}
}
- p, i, ok := _Dtoi(port, 0);
+ p, i, ok := dtoi(port, 0);
if !ok || i != len(port) {
p, ok = LookupPort(net, port);
if !ok {
@@ -116,7 +116,7 @@ func _HostPortToIP(net, hostport, mode string) (ip []byte, iport int, err *os.Er
}
// Convert socket address into "host:port".
-func _SockaddrToHostPort(sa *syscall.Sockaddr) (hostport string, err *os.Error) {
+func sockaddrToHostPort(sa *syscall.Sockaddr) (hostport string, err *os.Error) {
switch sa.Family {
case syscall.AF_INET, syscall.AF_INET6:
addr, port, e := SockaddrToIP(sa);
@@ -124,9 +124,9 @@ func _SockaddrToHostPort(sa *syscall.Sockaddr) (hostport string, err *os.Error)
return "", e
}
host := IPToString(addr);
- return _JoinHostPort(host, strconv.Itoa(port)), nil;
+ return joinHostPort(host, strconv.Itoa(port)), nil;
default:
- return "", Unknown_SocketFamily
+ return "", UnknownsocketFamily
}
return "", nil // not reached
}
@@ -139,8 +139,10 @@ func boolint(b bool) int {
return 0
}
-// Generic _Socket creation.
-func _Socket(f, p, t int64, la, ra *syscall.Sockaddr) (fd *FD, err *os.Error) {
+// Generic socket creation.
+func socket(net, laddr, raddr string, f, p, t int64, la, ra *syscall.Sockaddr)
+ (fd *netFD, err *os.Error)
+{
s, e := syscall.Socket(f, p, t);
if e != 0 {
return nil, os.ErrnoToError(e)
@@ -166,7 +168,7 @@ func _Socket(f, p, t int64, la, ra *syscall.Sockaddr) (fd *FD, err *os.Error) {
}
}
- fd, err = NewFD(s);
+ fd, err = newFD(s, net, laddr, raddr);
if err != nil {
syscall.Close(s);
return nil, err
@@ -177,29 +179,36 @@ func _Socket(f, p, t int64, la, ra *syscall.Sockaddr) (fd *FD, err *os.Error) {
// Generic implementation of Conn interface; not exported.
-type _ConnBase struct {
- fd *FD;
+type connBase struct {
+ fd *netFD;
raddr string;
}
-func (c *_ConnBase) FD() int64 {
+func (c *connBase) FD() *os.FD {
+ if c == nil {
+ return nil
+ }
+ return c.fd.osfd;
+}
+
+func (c *connBase) sysFD() int64 {
if c == nil || c.fd == nil {
- return -1
+ return -1;
}
- return c.fd.fd
+ return c.fd.fd;
}
-func (c *_ConnBase) Read(b []byte) (n int, err *os.Error) {
+func (c *connBase) Read(b []byte) (n int, err *os.Error) {
n, err = c.fd.Read(b);
return n, err
}
-func (c *_ConnBase) Write(b []byte) (n int, err *os.Error) {
+func (c *connBase) Write(b []byte) (n int, err *os.Error) {
n, err = c.fd.Write(b);
return n, err
}
-func (c *_ConnBase) ReadFrom(b []byte) (n int, raddr string, err *os.Error) {
+func (c *connBase) ReadFrom(b []byte) (n int, raddr string, err *os.Error) {
if c == nil {
return -1, "", os.EINVAL
}
@@ -207,7 +216,7 @@ func (c *_ConnBase) ReadFrom(b []byte) (n int, raddr string, err *os.Error) {
return n, c.raddr, err
}
-func (c *_ConnBase) WriteTo(raddr string, b []byte) (n int, err *os.Error) {
+func (c *connBase) WriteTo(raddr string, b []byte) (n int, err *os.Error) {
if c == nil {
return -1, os.EINVAL
}
@@ -218,7 +227,7 @@ func (c *_ConnBase) WriteTo(raddr string, b []byte) (n int, err *os.Error) {
return n, err
}
-func (c *_ConnBase) Close() *os.Error {
+func (c *connBase) Close() *os.Error {
if c == nil {
return os.EINVAL
}
@@ -234,48 +243,48 @@ func setsockopt_tv(fd, level, opt int64, nsec int64) *os.Error {
return os.ErrnoToError(syscall.Setsockopt_tv(fd, level, opt, nsec));
}
-func (c *_ConnBase) SetReadBuffer(bytes int) *os.Error {
- return setsockopt_int(c.FD(), syscall.SOL_SOCKET, syscall.SO_RCVBUF, bytes);
+func (c *connBase) SetReadBuffer(bytes int) *os.Error {
+ return setsockopt_int(c.sysFD(), syscall.SOL_SOCKET, syscall.SO_RCVBUF, bytes);
}
-func (c *_ConnBase) SetWriteBuffer(bytes int) *os.Error {
- return setsockopt_int(c.FD(), syscall.SOL_SOCKET, syscall.SO_SNDBUF, bytes);
+func (c *connBase) SetWriteBuffer(bytes int) *os.Error {
+ return setsockopt_int(c.sysFD(), syscall.SOL_SOCKET, syscall.SO_SNDBUF, bytes);
}
-func (c *_ConnBase) SetReadTimeout(nsec int64) *os.Error {
- return setsockopt_tv(c.FD(), syscall.SOL_SOCKET, syscall.SO_RCVTIMEO, nsec);
+func (c *connBase) SetReadTimeout(nsec int64) *os.Error {
+ return setsockopt_tv(c.sysFD(), syscall.SOL_SOCKET, syscall.SO_RCVTIMEO, nsec);
}
-func (c *_ConnBase) SetWriteTimeout(nsec int64) *os.Error {
- return setsockopt_tv(c.FD(), syscall.SOL_SOCKET, syscall.SO_SNDTIMEO, nsec);
+func (c *connBase) SetWriteTimeout(nsec int64) *os.Error {
+ return setsockopt_tv(c.sysFD(), syscall.SOL_SOCKET, syscall.SO_SNDTIMEO, nsec);
}
-func (c *_ConnBase) SetTimeout(nsec int64) *os.Error {
+func (c *connBase) SetTimeout(nsec int64) *os.Error {
if e := c.SetReadTimeout(nsec); e != nil {
return e
}
return c.SetWriteTimeout(nsec)
}
-func (c *_ConnBase) SetReuseAddr(reuse bool) *os.Error {
- return setsockopt_int(c.FD(), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, boolint(reuse));
+func (c *connBase) SetReuseAddr(reuse bool) *os.Error {
+ return setsockopt_int(c.sysFD(), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, boolint(reuse));
}
-func (c *_ConnBase) BindToDevice(dev string) *os.Error {
- // TODO: call setsockopt with null-terminated string pointer
+func (c *connBase) BindToDevice(dev string) *os.Error {
+ // TODO(rsc): call setsockopt with null-terminated string pointer
return os.EINVAL
}
-func (c *_ConnBase) SetDontRoute(dontroute bool) *os.Error {
- return setsockopt_int(c.FD(), syscall.SOL_SOCKET, syscall.SO_DONTROUTE, boolint(dontroute));
+func (c *connBase) SetDontRoute(dontroute bool) *os.Error {
+ return setsockopt_int(c.sysFD(), syscall.SOL_SOCKET, syscall.SO_DONTROUTE, boolint(dontroute));
}
-func (c *_ConnBase) SetKeepAlive(keepalive bool) *os.Error {
- return setsockopt_int(c.FD(), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, boolint(keepalive));
+func (c *connBase) SetKeepAlive(keepalive bool) *os.Error {
+ return setsockopt_int(c.sysFD(), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, boolint(keepalive));
}
-func (c *_ConnBase) SetLinger(sec int) *os.Error {
- e := syscall.Setsockopt_linger(c.FD(), syscall.SOL_SOCKET, syscall.SO_LINGER, sec);
+func (c *connBase) SetLinger(sec int) *os.Error {
+ e := syscall.Setsockopt_linger(c.sysFD(), syscall.SOL_SOCKET, syscall.SO_LINGER, sec);
return os.ErrnoToError(e);
}
@@ -287,23 +296,25 @@ func (c *_ConnBase) SetLinger(sec int) *os.Error {
// understands IPv6, it's okay to pass IPv4 addresses to the IPv6
// interface. That simplifies our code and is most general.
// If we need to build on a system without IPv6 support, setting
-// _PreferIPv4 here should fall back to the IPv4 socket interface when possible.
-const _PreferIPv4 = false
+// preferIPv4 here should fall back to the IPv4 socket interface when possible.
+const preferIPv4 = false
-func _InternetSocket(net, laddr, raddr string, proto int64, mode string) (fd *FD, err *os.Error) {
+func internetSocket(net, laddr, raddr string, proto int64, mode string)
+ (fd *netFD, err *os.Error)
+{
// Parse addresses (unless they are empty).
var lip, rip []byte;
var lport, rport int;
var lerr, rerr *os.Error;
if laddr != "" {
- lip, lport, lerr = _HostPortToIP(net, laddr, mode);
+ lip, lport, lerr = hostPortToIP(net, laddr, mode);
if lerr != nil {
return nil, lerr
}
}
if raddr != "" {
- rip, rport, rerr = _HostPortToIP(net, raddr, mode);
+ rip, rport, rerr = hostPortToIP(net, raddr, mode);
if rerr != nil {
return nil, rerr
}
@@ -320,7 +331,7 @@ func _InternetSocket(net, laddr, raddr string, proto int64, mode string) (fd *FD
default:
// Otherwise, guess.
// If the addresses are IPv4 and we prefer IPv4, use 4; else 6.
- if _PreferIPv4 && ToIPv4(lip) != nil && ToIPv4(rip) != nil {
+ if preferIPv4 && ToIPv4(lip) != nil && ToIPv4(rip) != nil {
vers = 4
} else {
vers = 6
@@ -351,7 +362,7 @@ func _InternetSocket(net, laddr, raddr string, proto int64, mode string) (fd *FD
}
}
- fd, err = _Socket(family, proto, 0, la, ra);
+ fd, err = socket(net, laddr, raddr, family, proto, 0, la, ra);
return fd, err
}
@@ -359,17 +370,17 @@ func _InternetSocket(net, laddr, raddr string, proto int64, mode string) (fd *FD
// TCP connections.
type ConnTCP struct {
- _ConnBase
+ connBase
}
func (c *ConnTCP) SetNoDelay(nodelay bool) *os.Error {
if c == nil {
return os.EINVAL
}
- return setsockopt_int(c.FD(), syscall.IPPROTO_TCP, syscall.TCP_NODELAY, boolint(nodelay))
+ return setsockopt_int(c.sysFD(), syscall.IPPROTO_TCP, syscall.TCP_NODELAY, boolint(nodelay))
}
-func _NewConnTCP(fd *FD, raddr string) *ConnTCP {
+func newConnTCP(fd *netFD, raddr string) *ConnTCP {
c := new(ConnTCP);
c.fd = fd;
c.raddr = raddr;
@@ -381,11 +392,11 @@ func DialTCP(net, laddr, raddr string) (c *ConnTCP, err *os.Error) {
if raddr == "" {
return nil, MissingAddress
}
- fd, e := _InternetSocket(net, laddr, raddr, syscall.SOCK_STREAM, "dial");
+ fd, e := internetSocket(net, laddr, raddr, syscall.SOCK_STREAM, "dial");
if e != nil {
return nil, e
}
- return _NewConnTCP(fd, raddr), nil
+ return newConnTCP(fd, raddr), nil
}
@@ -394,10 +405,10 @@ func DialTCP(net, laddr, raddr string) (c *ConnTCP, err *os.Error) {
// TODO(rsc): UDP headers mode
type ConnUDP struct {
- _ConnBase
+ connBase
}
-func _NewConnUDP(fd *FD, raddr string) *ConnUDP {
+func newConnUDP(fd *netFD, raddr string) *ConnUDP {
c := new(ConnUDP);
c.fd = fd;
c.raddr = raddr;
@@ -408,11 +419,11 @@ func DialUDP(net, laddr, raddr string) (c *ConnUDP, err *os.Error) {
if raddr == "" {
return nil, MissingAddress
}
- fd, e := _InternetSocket(net, laddr, raddr, syscall.SOCK_DGRAM, "dial");
+ fd, e := internetSocket(net, laddr, raddr, syscall.SOCK_DGRAM, "dial");
if e != nil {
return nil, e
}
- return _NewConnUDP(fd, raddr), nil
+ return newConnUDP(fd, raddr), nil
}
@@ -483,12 +494,12 @@ type Listener interface {
}
type ListenerTCP struct {
- fd *FD;
+ fd *netFD;
laddr string
}
func ListenTCP(net, laddr string) (l *ListenerTCP, err *os.Error) {
- fd, e := _InternetSocket(net, laddr, "", syscall.SOCK_STREAM, "listen");
+ fd, e := internetSocket(net, laddr, "", syscall.SOCK_STREAM, "listen");
if e != nil {
return nil, e
}
@@ -511,12 +522,12 @@ func (l *ListenerTCP) AcceptTCP() (c *ConnTCP, raddr string, err *os.Error) {
if e != nil {
return nil, "", e
}
- raddr, err = _SockaddrToHostPort(&sa);
+ raddr, err = sockaddrToHostPort(&sa);
if err != nil {
fd.Close();
return nil, "", err
}
- return _NewConnTCP(fd, raddr), raddr, nil
+ return newConnTCP(fd, raddr), raddr, nil
}
func (l *ListenerTCP) Accept() (c Conn, raddr string, err *os.Error) {