2011-09-16 17:47:21 +02: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.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2012-10-23 06:31:11 +02:00
|
|
|
"os"
|
2012-03-02 21:01:37 +01:00
|
|
|
"syscall"
|
2012-01-25 22:54:22 +01:00
|
|
|
"time"
|
2011-09-16 17:47:21 +02:00
|
|
|
)
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// UnixConn is an implementation of the Conn interface for connections
|
|
|
|
// to Unix domain sockets.
|
2012-12-13 00:13:29 +01:00
|
|
|
type UnixConn struct {
|
|
|
|
conn
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFromUnix reads a packet from c, copying the payload into b. It
|
|
|
|
// returns the number of bytes copied into b and the source address of
|
|
|
|
// the packet.
|
|
|
|
//
|
|
|
|
// ReadFromUnix can be made to time out and return an error with
|
|
|
|
// Timeout() == true after a fixed time limit; see SetDeadline and
|
|
|
|
// SetReadDeadline.
|
|
|
|
func (c *UnixConn) ReadFromUnix(b []byte) (int, *UnixAddr, error) {
|
2015-10-31 01:59:47 +01:00
|
|
|
return 0, nil, &OpError{Op: "read", Net: c.fd.dir, Source: c.fd.laddr, Addr: c.fd.raddr, Err: syscall.EPLAN9}
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
// ReadFrom implements the PacketConn ReadFrom method.
|
2012-10-23 06:31:11 +02:00
|
|
|
func (c *UnixConn) ReadFrom(b []byte) (int, Addr, error) {
|
2015-10-31 01:59:47 +01:00
|
|
|
return 0, nil, &OpError{Op: "read", Net: c.fd.dir, Source: c.fd.laddr, Addr: c.fd.raddr, Err: syscall.EPLAN9}
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadMsgUnix reads a packet from c, copying the payload into b and
|
|
|
|
// the associated out-of-band data into oob. It returns the number of
|
|
|
|
// bytes copied into b, the number of bytes copied into oob, the flags
|
|
|
|
// that were set on the packet, and the source address of the packet.
|
|
|
|
func (c *UnixConn) ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *UnixAddr, err error) {
|
2015-10-31 01:59:47 +01:00
|
|
|
return 0, 0, 0, nil, &OpError{Op: "read", Net: c.fd.dir, Source: c.fd.laddr, Addr: c.fd.raddr, Err: syscall.EPLAN9}
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteToUnix writes a packet to addr via c, copying the payload from b.
|
|
|
|
//
|
|
|
|
// WriteToUnix can be made to time out and return an error with
|
|
|
|
// Timeout() == true after a fixed time limit; see SetDeadline and
|
|
|
|
// SetWriteDeadline. On packet-oriented connections, write timeouts
|
|
|
|
// are rare.
|
|
|
|
func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (int, error) {
|
2015-10-31 01:59:47 +01:00
|
|
|
return 0, &OpError{Op: "write", Net: c.fd.dir, Source: c.fd.laddr, Addr: addr.opAddr(), Err: syscall.EPLAN9}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
// WriteTo implements the PacketConn WriteTo method.
|
2012-10-23 06:31:11 +02:00
|
|
|
func (c *UnixConn) WriteTo(b []byte, addr Addr) (int, error) {
|
2015-10-31 01:59:47 +01:00
|
|
|
return 0, &OpError{Op: "write", Net: c.fd.dir, Source: c.fd.laddr, Addr: addr, Err: syscall.EPLAN9}
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteMsgUnix writes a packet to addr via c, copying the payload
|
|
|
|
// from b and the associated out-of-band data from oob. It returns
|
|
|
|
// the number of payload and out-of-band bytes written.
|
|
|
|
func (c *UnixConn) WriteMsgUnix(b, oob []byte, addr *UnixAddr) (n, oobn int, err error) {
|
2015-10-31 01:59:47 +01:00
|
|
|
return 0, 0, &OpError{Op: "write", Net: c.fd.dir, Source: c.fd.laddr, Addr: addr.opAddr(), Err: syscall.EPLAN9}
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
|
|
|
|
2012-12-22 02:15:33 +01:00
|
|
|
// CloseRead shuts down the reading side of the Unix domain connection.
|
|
|
|
// Most callers should just use Close.
|
2012-10-23 06:31:11 +02:00
|
|
|
func (c *UnixConn) CloseRead() error {
|
2015-10-31 01:59:47 +01:00
|
|
|
return &OpError{Op: "close", Net: c.fd.dir, Source: c.fd.laddr, Addr: c.fd.raddr, Err: syscall.EPLAN9}
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
|
|
|
|
2012-12-22 02:15:33 +01:00
|
|
|
// CloseWrite shuts down the writing side of the Unix domain connection.
|
|
|
|
// Most callers should just use Close.
|
2012-10-23 06:31:11 +02:00
|
|
|
func (c *UnixConn) CloseWrite() error {
|
2015-10-31 01:59:47 +01:00
|
|
|
return &OpError{Op: "close", Net: c.fd.dir, Source: c.fd.laddr, Addr: c.fd.raddr, Err: syscall.EPLAN9}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DialUnix connects to the remote address raddr on the network net,
|
2012-12-22 02:15:33 +01:00
|
|
|
// which must be "unix", "unixgram" or "unixpacket". If laddr is not
|
|
|
|
// nil, it is used as the local address for the connection.
|
2012-10-23 06:31:11 +02:00
|
|
|
func DialUnix(net string, laddr, raddr *UnixAddr) (*UnixConn, error) {
|
2012-11-21 08:03:38 +01:00
|
|
|
return dialUnix(net, laddr, raddr, noDeadline)
|
|
|
|
}
|
|
|
|
|
|
|
|
func dialUnix(net string, laddr, raddr *UnixAddr, deadline time.Time) (*UnixConn, error) {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, &OpError{Op: "dial", Net: net, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: syscall.EPLAN9}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// UnixListener is a Unix domain socket listener. Clients should
|
|
|
|
// typically use variables of type Listener instead of assuming Unix
|
|
|
|
// domain sockets.
|
2015-10-31 01:59:47 +01:00
|
|
|
type UnixListener struct {
|
|
|
|
fd *netFD
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// ListenUnix announces on the Unix domain socket laddr and returns a
|
2013-07-16 08:54:42 +02:00
|
|
|
// Unix listener. The network net must be "unix" or "unixpacket".
|
2012-10-23 06:31:11 +02:00
|
|
|
func ListenUnix(net string, laddr *UnixAddr) (*UnixListener, error) {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: syscall.EPLAN9}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// AcceptUnix accepts the next incoming call and returns the new
|
2013-11-06 20:49:01 +01:00
|
|
|
// connection.
|
2012-10-23 06:31:11 +02:00
|
|
|
func (l *UnixListener) AcceptUnix() (*UnixConn, error) {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, &OpError{Op: "accept", Net: l.fd.dir, Source: nil, Addr: l.fd.laddr, Err: syscall.EPLAN9}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// Accept implements the Accept method in the Listener interface; it
|
|
|
|
// waits for the next call and returns a generic Conn.
|
|
|
|
func (l *UnixListener) Accept() (Conn, error) {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, &OpError{Op: "accept", Net: l.fd.dir, Source: nil, Addr: l.fd.laddr, Err: syscall.EPLAN9}
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close stops listening on the Unix address. Already accepted
|
|
|
|
// connections are not closed.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (l *UnixListener) Close() error {
|
2015-10-31 01:59:47 +01:00
|
|
|
return &OpError{Op: "close", Net: l.fd.dir, Source: nil, Addr: l.fd.laddr, Err: syscall.EPLAN9}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Addr returns the listener's network address.
|
2015-10-31 01:59:47 +01:00
|
|
|
// The Addr returned is shared by all invocations of Addr, so
|
|
|
|
// do not modify it.
|
2011-09-16 17:47:21 +02:00
|
|
|
func (l *UnixListener) Addr() Addr { return nil }
|
2012-10-23 06:31:11 +02:00
|
|
|
|
|
|
|
// SetDeadline sets the deadline associated with the listener.
|
|
|
|
// A zero time value disables the deadline.
|
|
|
|
func (l *UnixListener) SetDeadline(t time.Time) error {
|
2015-10-31 01:59:47 +01:00
|
|
|
return &OpError{Op: "set", Net: l.fd.dir, Source: nil, Addr: l.fd.laddr, Err: syscall.EPLAN9}
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2013-07-16 08:54:42 +02:00
|
|
|
//
|
|
|
|
// 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.
|
2012-10-23 06:31:11 +02:00
|
|
|
func (l *UnixListener) File() (*os.File, error) {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, &OpError{Op: "file", Net: l.fd.dir, Source: nil, Addr: l.fd.laddr, Err: syscall.EPLAN9}
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListenUnixgram listens for incoming Unix datagram packets addressed
|
2013-07-16 08:54:42 +02:00
|
|
|
// 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.
|
2012-12-22 02:15:33 +01:00
|
|
|
func ListenUnixgram(net string, laddr *UnixAddr) (*UnixConn, error) {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: syscall.EPLAN9}
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|