2009-11-01 12:15:34 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Unix domain sockets
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:35:38 -07:00
|
|
|
"os"
|
|
|
|
"syscall"
|
2009-11-01 12:15:34 -07:00
|
|
|
)
|
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
func unixSocket(net string, laddr, raddr *UnixAddr, mode string) (fd *netFD, err os.Error) {
|
2009-12-15 16:35:38 -07:00
|
|
|
var proto int
|
2009-11-01 12:15:34 -07:00
|
|
|
switch net {
|
|
|
|
default:
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, UnknownNetworkError(net)
|
2009-11-01 12:15:34 -07:00
|
|
|
case "unix":
|
2009-11-09 13:07:39 -07:00
|
|
|
proto = syscall.SOCK_STREAM
|
2009-11-02 19:37:30 -07:00
|
|
|
case "unixgram":
|
2009-11-09 13:07:39 -07:00
|
|
|
proto = syscall.SOCK_DGRAM
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
var la, ra syscall.Sockaddr
|
2009-11-01 12:15:34 -07:00
|
|
|
switch mode {
|
|
|
|
default:
|
2009-11-09 13:07:39 -07:00
|
|
|
panic("unixSocket", mode)
|
2009-11-01 12:15:34 -07:00
|
|
|
|
|
|
|
case "dial":
|
2009-11-02 19:37:30 -07:00
|
|
|
if laddr != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
la = &syscall.SockaddrUnix{Name: laddr.Name}
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2009-11-02 19:37:30 -07:00
|
|
|
if raddr != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
ra = &syscall.SockaddrUnix{Name: raddr.Name}
|
2009-11-02 19:37:30 -07:00
|
|
|
} else if proto != syscall.SOCK_DGRAM || laddr == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, &OpError{mode, net, nil, errMissingAddress}
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
case "listen":
|
2009-11-02 19:37:30 -07:00
|
|
|
if laddr == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, &OpError{mode, net, nil, errMissingAddress}
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
la = &syscall.SockaddrUnix{Name: laddr.Name}
|
2009-11-02 19:37:30 -07:00
|
|
|
if raddr != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, &OpError{mode, net, raddr, &AddrError{"unexpected remote address", raddr.String()}}
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
f := sockaddrToUnix
|
2009-11-02 19:37:30 -07:00
|
|
|
if proto != syscall.SOCK_STREAM {
|
2009-11-09 13:07:39 -07:00
|
|
|
f = sockaddrToUnixgram
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
fd, err = socket(net, syscall.AF_UNIX, proto, 0, la, ra, f)
|
2009-11-01 12:15:34 -07:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
goto Error
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return fd, nil
|
2009-11-01 12:15:34 -07:00
|
|
|
|
|
|
|
Error:
|
2009-12-15 16:35:38 -07:00
|
|
|
addr := raddr
|
2009-11-01 12:15:34 -07:00
|
|
|
if mode == "listen" {
|
2009-11-09 13:07:39 -07:00
|
|
|
addr = laddr
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return nil, &OpError{mode, net, addr, err}
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
// UnixAddr represents the address of a Unix domain socket end point.
|
|
|
|
type UnixAddr struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
Name string
|
|
|
|
Datagram bool
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func sockaddrToUnix(sa syscall.Sockaddr) Addr {
|
|
|
|
if s, ok := sa.(*syscall.SockaddrUnix); ok {
|
2009-11-09 13:07:39 -07:00
|
|
|
return &UnixAddr{s.Name, false}
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return nil
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func sockaddrToUnixgram(sa syscall.Sockaddr) Addr {
|
|
|
|
if s, ok := sa.(*syscall.SockaddrUnix); ok {
|
2009-11-09 13:07:39 -07:00
|
|
|
return &UnixAddr{s.Name, true}
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return nil
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Network returns the address's network name, "unix" or "unixgram".
|
|
|
|
func (a *UnixAddr) Network() string {
|
|
|
|
if a == nil || !a.Datagram {
|
2009-11-09 13:07:39 -07:00
|
|
|
return "unix"
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return "unixgram"
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UnixAddr) String() string {
|
|
|
|
if a == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return "<nil>"
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return a.Name
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UnixAddr) toAddr() Addr {
|
2009-12-15 16:35:38 -07:00
|
|
|
if a == nil { // nil *UnixAddr
|
|
|
|
return nil // nil interface
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return a
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveUnixAddr parses addr as a Unix domain socket address.
|
|
|
|
// The string net gives the network name, "unix" or "unixgram".
|
|
|
|
func ResolveUnixAddr(net, addr string) (*UnixAddr, os.Error) {
|
2009-12-15 16:35:38 -07:00
|
|
|
var datagram bool
|
2009-11-02 19:37:30 -07:00
|
|
|
switch net {
|
|
|
|
case "unix":
|
|
|
|
case "unixgram":
|
2009-11-09 13:07:39 -07:00
|
|
|
datagram = true
|
2009-11-02 19:37:30 -07:00
|
|
|
default:
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, UnknownNetworkError(net)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return &UnixAddr{addr, datagram}, nil
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnixConn is an implementation of the Conn interface
|
2009-11-01 12:15:34 -07:00
|
|
|
// for connections to Unix domain sockets.
|
2009-11-02 19:37:30 -07:00
|
|
|
type UnixConn struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
fd *netFD
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func newUnixConn(fd *netFD) *UnixConn { return &UnixConn{fd} }
|
2009-11-02 19:37:30 -07:00
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (c *UnixConn) ok() bool { return c != nil && c.fd != nil }
|
2009-11-02 19:37:30 -07:00
|
|
|
|
|
|
|
// Implementation of the Conn interface - see Conn for documentation.
|
|
|
|
|
|
|
|
// Read reads data from the Unix domain connection.
|
|
|
|
//
|
|
|
|
// Read can be made to time out and return err == os.EAGAIN
|
|
|
|
// after a fixed time limit; see SetTimeout and SetReadTimeout.
|
|
|
|
func (c *UnixConn) Read(b []byte) (n int, err os.Error) {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return c.fd.Read(b)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes data to the Unix domain connection.
|
|
|
|
//
|
|
|
|
// Write can be made to time out and return err == os.EAGAIN
|
|
|
|
// after a fixed time limit; see SetTimeout and SetReadTimeout.
|
|
|
|
func (c *UnixConn) Write(b []byte) (n int, err os.Error) {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return c.fd.Write(b)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the Unix domain connection.
|
|
|
|
func (c *UnixConn) Close() os.Error {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
err := c.fd.Close()
|
|
|
|
c.fd = nil
|
|
|
|
return err
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// LocalAddr returns the local network address, a *UnixAddr.
|
|
|
|
// Unlike in other protocols, LocalAddr is usually nil for dialed connections.
|
|
|
|
func (c *UnixConn) LocalAddr() Addr {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return c.fd.laddr
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteAddr returns the remote network address, a *UnixAddr.
|
|
|
|
// Unlike in other protocols, RemoteAddr is usually nil for connections
|
|
|
|
// accepted by a listener.
|
|
|
|
func (c *UnixConn) RemoteAddr() Addr {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return c.fd.raddr
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetTimeout sets the read and write deadlines associated
|
|
|
|
// with the connection.
|
|
|
|
func (c *UnixConn) SetTimeout(nsec int64) os.Error {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return setTimeout(c.fd, nsec)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetReadTimeout sets the time (in nanoseconds) that
|
|
|
|
// Read will wait for data before returning os.EAGAIN.
|
|
|
|
// Setting nsec == 0 (the default) disables the deadline.
|
|
|
|
func (c *UnixConn) SetReadTimeout(nsec int64) os.Error {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return setReadTimeout(c.fd, nsec)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetWriteTimeout sets the time (in nanoseconds) that
|
|
|
|
// Write will wait to send its data before returning os.EAGAIN.
|
|
|
|
// Setting nsec == 0 (the default) disables the deadline.
|
|
|
|
// Even if write times out, it may return n > 0, indicating that
|
|
|
|
// some of the data was successfully written.
|
|
|
|
func (c *UnixConn) SetWriteTimeout(nsec int64) os.Error {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return setWriteTimeout(c.fd, nsec)
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
// SetReadBuffer sets the size of the operating system's
|
|
|
|
// receive buffer associated with the connection.
|
|
|
|
func (c *UnixConn) SetReadBuffer(bytes int) os.Error {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return setReadBuffer(c.fd, bytes)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetWriteBuffer sets the size of the operating system's
|
|
|
|
// transmit buffer associated with the connection.
|
|
|
|
func (c *UnixConn) SetWriteBuffer(bytes int) os.Error {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return setWriteBuffer(c.fd, bytes)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFromUnix reads a packet from c, copying the payload into b.
|
|
|
|
// It returns the number of bytes copied into b and the return address
|
|
|
|
// that was on the packet.
|
|
|
|
//
|
|
|
|
// ReadFromUnix can be made to time out and return err == os.EAGAIN
|
|
|
|
// after a fixed time limit; see SetTimeout and SetReadTimeout.
|
|
|
|
func (c *UnixConn) ReadFromUnix(b []byte) (n int, addr *UnixAddr, err os.Error) {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, nil, os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
n, sa, err := c.fd.ReadFrom(b)
|
2009-11-02 19:37:30 -07:00
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrUnix:
|
2009-11-09 13:07:39 -07:00
|
|
|
addr = &UnixAddr{sa.Name, c.fd.proto == syscall.SOCK_DGRAM}
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFrom reads a packet from c, copying the payload into b.
|
|
|
|
// It returns the number of bytes copied into b and the return address
|
|
|
|
// that was on the packet.
|
|
|
|
//
|
|
|
|
// ReadFrom can be made to time out and return err == os.EAGAIN
|
|
|
|
// after a fixed time limit; see SetTimeout and SetReadTimeout.
|
|
|
|
func (c *UnixConn) ReadFrom(b []byte) (n int, addr Addr, err os.Error) {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, nil, os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
n, uaddr, err := c.ReadFromUnix(b)
|
|
|
|
return n, uaddr.toAddr(), err
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteToUnix writes a packet to addr via c, copying the payload from b.
|
|
|
|
//
|
|
|
|
// WriteToUnix can be made to time out and return err == os.EAGAIN
|
|
|
|
// after a fixed time limit; see SetTimeout and SetWriteTimeout.
|
|
|
|
// On packet-oriented connections such as UDP, write timeouts are rare.
|
|
|
|
func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (n int, err os.Error) {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
if addr.Datagram != (c.fd.proto == syscall.SOCK_DGRAM) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, os.EAFNOSUPPORT
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
sa := &syscall.SockaddrUnix{Name: addr.Name}
|
|
|
|
return c.fd.WriteTo(b, sa)
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
// WriteTo writes a packet to addr via c, copying the payload from b.
|
|
|
|
//
|
|
|
|
// WriteTo can be made to time out and return err == os.EAGAIN
|
|
|
|
// after a fixed time limit; see SetTimeout and SetWriteTimeout.
|
|
|
|
// On packet-oriented connections such as UDP, write timeouts are rare.
|
|
|
|
func (c *UnixConn) WriteTo(b []byte, addr Addr) (n int, err os.Error) {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
a, ok := addr.(*UnixAddr)
|
2009-11-02 19:37:30 -07:00
|
|
|
if !ok {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, &OpError{"writeto", "unix", addr, os.EINVAL}
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return c.WriteToUnix(b, a)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// DialUDP connects to the remote address raddr on the network net,
|
|
|
|
// which must be "unix" or "unixdgram". If laddr is not nil, it is used
|
|
|
|
// as the local address for the connection.
|
|
|
|
func DialUnix(net string, laddr, raddr *UnixAddr) (c *UnixConn, err os.Error) {
|
2009-12-15 16:35:38 -07:00
|
|
|
fd, e := unixSocket(net, laddr, raddr, "dial")
|
2009-11-01 12:15:34 -07:00
|
|
|
if e != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, e
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return newUnixConn(fd), nil
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
// UnixListener is a Unix domain socket listener.
|
2009-11-01 12:15:34 -07:00
|
|
|
// Clients should typically use variables of type Listener
|
|
|
|
// instead of assuming Unix domain sockets.
|
2009-11-02 19:37:30 -07:00
|
|
|
type UnixListener struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
fd *netFD
|
|
|
|
path string
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListenUnix announces on the Unix domain socket laddr and returns a Unix listener.
|
2009-11-02 19:37:30 -07:00
|
|
|
// Net must be "unix" (stream sockets).
|
|
|
|
func ListenUnix(net string, laddr *UnixAddr) (l *UnixListener, err os.Error) {
|
|
|
|
if net != "unix" && net != "unixgram" {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, UnknownNetworkError(net)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
if laddr != nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
laddr = &UnixAddr{laddr.Name, net == "unixgram"} // make our own copy
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
fd, e := unixSocket(net, laddr, nil, "listen")
|
2009-11-01 12:15:34 -07:00
|
|
|
if e != nil {
|
|
|
|
if pe, ok := e.(*os.PathError); ok {
|
2009-11-09 13:07:39 -07:00
|
|
|
e = pe.Error
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return nil, e
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
e1 := syscall.Listen(fd.sysfd, 8) // listenBacklog());
|
2009-11-01 12:15:34 -07:00
|
|
|
if e1 != 0 {
|
2009-12-15 16:35:38 -07:00
|
|
|
syscall.Close(fd.sysfd)
|
|
|
|
return nil, &OpError{"listen", "unix", laddr, os.Errno(e1)}
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return &UnixListener{fd, laddr.Name}, nil
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// AcceptUnix accepts the next incoming call and returns the new connection
|
|
|
|
// and the remote address.
|
2009-11-02 19:37:30 -07:00
|
|
|
func (l *UnixListener) AcceptUnix() (c *UnixConn, err os.Error) {
|
2009-12-02 00:28:57 -07:00
|
|
|
if l == nil || l.fd == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, os.EINVAL
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
fd, e := l.fd.accept(sockaddrToUnix)
|
2009-11-01 12:15:34 -07:00
|
|
|
if e != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, e
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
c = newUnixConn(fd)
|
|
|
|
return c, nil
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Accept implements the Accept method in the Listener interface;
|
|
|
|
// it waits for the next call and returns a generic Conn.
|
2009-11-02 19:37:30 -07:00
|
|
|
func (l *UnixListener) Accept() (c Conn, err os.Error) {
|
2009-12-15 16:35:38 -07:00
|
|
|
c1, err := l.AcceptUnix()
|
2009-11-02 19:37:30 -07:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, err
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return c1, nil
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close stops listening on the Unix address.
|
|
|
|
// Already accepted connections are not closed.
|
2009-11-02 19:37:30 -07:00
|
|
|
func (l *UnixListener) Close() os.Error {
|
2009-11-01 12:15:34 -07:00
|
|
|
if l == nil || l.fd == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return os.EINVAL
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// The operating system doesn't clean up
|
|
|
|
// the file that announcing created, so
|
|
|
|
// we have to clean it up ourselves.
|
|
|
|
// There's a race here--we can't know for
|
|
|
|
// sure whether someone else has come along
|
|
|
|
// and replaced our socket name already--
|
|
|
|
// but this sequence (remove then close)
|
|
|
|
// is at least compatible with the auto-remove
|
|
|
|
// sequence in ListenUnix. It's only non-Go
|
|
|
|
// programs that can mess us up.
|
2009-11-02 19:37:30 -07:00
|
|
|
if l.path[0] != '@' {
|
2009-11-09 13:07:39 -07:00
|
|
|
syscall.Unlink(l.path)
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
err := l.fd.Close()
|
|
|
|
l.fd = nil
|
|
|
|
return err
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Addr returns the listener's network address.
|
2009-12-15 16:35:38 -07:00
|
|
|
func (l *UnixListener) Addr() Addr { return l.fd.laddr }
|
2009-11-02 19:37:30 -07:00
|
|
|
|
|
|
|
// 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 UDP
|
|
|
|
// packets with per-packet addressing. The network net must be "unixgram".
|
|
|
|
func ListenUnixgram(net string, laddr *UnixAddr) (c *UDPConn, err os.Error) {
|
|
|
|
switch net {
|
|
|
|
case "unixgram":
|
|
|
|
default:
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, UnknownNetworkError(net)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
if laddr == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, &OpError{"listen", "unixgram", nil, errMissingAddress}
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
fd, e := unixSocket(net, laddr, nil, "listen")
|
2009-11-02 19:37:30 -07:00
|
|
|
if e != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, e
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return newUDPConn(fd), nil
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|