diff options
author | Michael Stapelberg <stapelberg@debian.org> | 2013-05-14 18:39:35 +0200 |
---|---|---|
committer | Michael Stapelberg <michael@stapelberg.de> | 2013-05-14 18:39:35 +0200 |
commit | efcc50dfdc94c82ee0292bf71992ecb7c0123061 (patch) | |
tree | 17dca99d1dc7fc4e9fe49c2cf6a99d337d4c039f /src/pkg/net/unixsock_posix.go | |
parent | 04b08da9af0c450d645ab7389d1467308cfc2db8 (diff) | |
download | golang-efcc50dfdc94c82ee0292bf71992ecb7c0123061.tar.gz |
Imported Upstream version 1.1upstream/1.1
Diffstat (limited to 'src/pkg/net/unixsock_posix.go')
-rw-r--r-- | src/pkg/net/unixsock_posix.go | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/src/pkg/net/unixsock_posix.go b/src/pkg/net/unixsock_posix.go index 6d6ce3f5e..5db30df95 100644 --- a/src/pkg/net/unixsock_posix.go +++ b/src/pkg/net/unixsock_posix.go @@ -4,8 +4,6 @@ // +build darwin freebsd linux netbsd openbsd windows -// Unix domain sockets - package net import ( @@ -15,6 +13,13 @@ import ( "time" ) +func (a *UnixAddr) isUnnamed() bool { + if a == nil || a.Name == "" { + return true + } + return false +} + func unixSocket(net string, laddr, raddr *UnixAddr, mode string, deadline time.Time) (*netFD, error) { var sotype int switch net { @@ -31,12 +36,12 @@ func unixSocket(net string, laddr, raddr *UnixAddr, mode string, deadline time.T var la, ra syscall.Sockaddr switch mode { case "dial": - if laddr != nil { + if !laddr.isUnnamed() { la = &syscall.SockaddrUnix{Name: laddr.Name} } if raddr != nil { ra = &syscall.SockaddrUnix{Name: raddr.Name} - } else if sotype != syscall.SOCK_DGRAM || laddr == nil { + } else if sotype != syscall.SOCK_DGRAM || laddr.isUnnamed() { return nil, &OpError{Op: mode, Net: net, Err: errMissingAddress} } case "listen": @@ -69,21 +74,21 @@ error: func sockaddrToUnix(sa syscall.Sockaddr) Addr { if s, ok := sa.(*syscall.SockaddrUnix); ok { - return &UnixAddr{s.Name, "unix"} + return &UnixAddr{Name: s.Name, Net: "unix"} } return nil } func sockaddrToUnixgram(sa syscall.Sockaddr) Addr { if s, ok := sa.(*syscall.SockaddrUnix); ok { - return &UnixAddr{s.Name, "unixgram"} + return &UnixAddr{Name: s.Name, Net: "unixgram"} } return nil } func sockaddrToUnixpacket(sa syscall.Sockaddr) Addr { if s, ok := sa.(*syscall.SockaddrUnix); ok { - return &UnixAddr{s.Name, "unixpacket"} + return &UnixAddr{Name: s.Name, Net: "unixpacket"} } return nil } @@ -92,14 +97,13 @@ func sotypeToNet(sotype int) string { switch sotype { case syscall.SOCK_STREAM: return "unix" - case syscall.SOCK_SEQPACKET: - return "unixpacket" case syscall.SOCK_DGRAM: return "unixgram" + case syscall.SOCK_SEQPACKET: + return "unixpacket" default: panic("sotypeToNet unknown socket type") } - return "" } // UnixConn is an implementation of the Conn interface for connections @@ -125,7 +129,7 @@ func (c *UnixConn) ReadFromUnix(b []byte) (n int, addr *UnixAddr, err error) { switch sa := sa.(type) { case *syscall.SockaddrUnix: if sa.Name != "" { - addr = &UnixAddr{sa.Name, sotypeToNet(c.fd.sotype)} + addr = &UnixAddr{Name: sa.Name, Net: sotypeToNet(c.fd.sotype)} } } return @@ -152,7 +156,7 @@ func (c *UnixConn) ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *UnixAdd switch sa := sa.(type) { case *syscall.SockaddrUnix: if sa.Name != "" { - addr = &UnixAddr{sa.Name, sotypeToNet(c.fd.sotype)} + addr = &UnixAddr{Name: sa.Name, Net: sotypeToNet(c.fd.sotype)} } } return @@ -267,7 +271,7 @@ func ListenUnix(net string, laddr *UnixAddr) (*UnixListener, error) { } err = syscall.Listen(fd.sysfd, listenerBacklog) if err != nil { - closesocket(fd.sysfd) + fd.Close() return nil, &OpError{Op: "listen", Net: net, Addr: laddr, Err: err} } return &UnixListener{fd, laddr.Name}, nil @@ -335,12 +339,16 @@ func (l *UnixListener) SetDeadline(t time.Time) (err error) { // File returns a copy of the underlying os.File, set to blocking // mode. It is the caller's responsibility to close f when finished. // Closing l does not affect f, and closing f does not affect l. +// +// The returned os.File's file descriptor is different from the +// connection's. Attempting to change properties of the original +// using this duplicate may or may not have the desired effect. func (l *UnixListener) File() (f *os.File, err error) { return l.fd.dup() } // ListenUnixgram listens for incoming Unix datagram packets addressed -// to the local address laddr. The returned connection c's ReadFrom -// and WriteTo methods can be used to receive and send packets with -// per-packet addressing. The network net must be "unixgram". +// to the local address laddr. The network net must be "unixgram". +// The returned connection's ReadFrom and WriteTo methods can be used +// to receive and send packets with per-packet addressing. func ListenUnixgram(net string, laddr *UnixAddr) (*UnixConn, error) { switch net { case "unixgram": |