...
Run Format

Source file src/pkg/net/unixsock_posix.go

     1	// Copyright 2009 The Go Authors.  All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
     6	
     7	package net
     8	
     9	import (
    10		"errors"
    11		"os"
    12		"syscall"
    13		"time"
    14	)
    15	
    16	func unixSocket(net string, laddr, raddr sockaddr, mode string, deadline time.Time) (*netFD, error) {
    17		var sotype int
    18		switch net {
    19		case "unix":
    20			sotype = syscall.SOCK_STREAM
    21		case "unixgram":
    22			sotype = syscall.SOCK_DGRAM
    23		case "unixpacket":
    24			sotype = syscall.SOCK_SEQPACKET
    25		default:
    26			return nil, UnknownNetworkError(net)
    27		}
    28	
    29		switch mode {
    30		case "dial":
    31			if laddr != nil && laddr.isWildcard() {
    32				laddr = nil
    33			}
    34			if raddr != nil && raddr.isWildcard() {
    35				raddr = nil
    36			}
    37			if raddr == nil && (sotype != syscall.SOCK_DGRAM || laddr == nil) {
    38				return nil, errMissingAddress
    39			}
    40		case "listen":
    41		default:
    42			return nil, errors.New("unknown mode: " + mode)
    43		}
    44	
    45		f := sockaddrToUnix
    46		if sotype == syscall.SOCK_DGRAM {
    47			f = sockaddrToUnixgram
    48		} else if sotype == syscall.SOCK_SEQPACKET {
    49			f = sockaddrToUnixpacket
    50		}
    51	
    52		fd, err := socket(net, syscall.AF_UNIX, sotype, 0, false, laddr, raddr, deadline, f)
    53		if err != nil {
    54			return nil, err
    55		}
    56		return fd, nil
    57	}
    58	
    59	func sockaddrToUnix(sa syscall.Sockaddr) Addr {
    60		if s, ok := sa.(*syscall.SockaddrUnix); ok {
    61			return &UnixAddr{Name: s.Name, Net: "unix"}
    62		}
    63		return nil
    64	}
    65	
    66	func sockaddrToUnixgram(sa syscall.Sockaddr) Addr {
    67		if s, ok := sa.(*syscall.SockaddrUnix); ok {
    68			return &UnixAddr{Name: s.Name, Net: "unixgram"}
    69		}
    70		return nil
    71	}
    72	
    73	func sockaddrToUnixpacket(sa syscall.Sockaddr) Addr {
    74		if s, ok := sa.(*syscall.SockaddrUnix); ok {
    75			return &UnixAddr{Name: s.Name, Net: "unixpacket"}
    76		}
    77		return nil
    78	}
    79	
    80	func sotypeToNet(sotype int) string {
    81		switch sotype {
    82		case syscall.SOCK_STREAM:
    83			return "unix"
    84		case syscall.SOCK_DGRAM:
    85			return "unixgram"
    86		case syscall.SOCK_SEQPACKET:
    87			return "unixpacket"
    88		default:
    89			panic("sotypeToNet unknown socket type")
    90		}
    91	}
    92	
    93	func (a *UnixAddr) family() int {
    94		return syscall.AF_UNIX
    95	}
    96	
    97	func (a *UnixAddr) isWildcard() bool {
    98		return a == nil || a.Name == ""
    99	}
   100	
   101	func (a *UnixAddr) sockaddr(family int) (syscall.Sockaddr, error) {
   102		if a == nil {
   103			return nil, nil
   104		}
   105		return &syscall.SockaddrUnix{Name: a.Name}, nil
   106	}
   107	
   108	// UnixConn is an implementation of the Conn interface for connections
   109	// to Unix domain sockets.
   110	type UnixConn struct {
   111		conn
   112	}
   113	
   114	func newUnixConn(fd *netFD) *UnixConn { return &UnixConn{conn{fd}} }
   115	
   116	// ReadFromUnix reads a packet from c, copying the payload into b.  It
   117	// returns the number of bytes copied into b and the source address of
   118	// the packet.
   119	//
   120	// ReadFromUnix can be made to time out and return an error with
   121	// Timeout() == true after a fixed time limit; see SetDeadline and
   122	// SetReadDeadline.
   123	func (c *UnixConn) ReadFromUnix(b []byte) (n int, addr *UnixAddr, err error) {
   124		if !c.ok() {
   125			return 0, nil, syscall.EINVAL
   126		}
   127		n, sa, err := c.fd.readFrom(b)
   128		switch sa := sa.(type) {
   129		case *syscall.SockaddrUnix:
   130			if sa.Name != "" {
   131				addr = &UnixAddr{Name: sa.Name, Net: sotypeToNet(c.fd.sotype)}
   132			}
   133		}
   134		return
   135	}
   136	
   137	// ReadFrom implements the PacketConn ReadFrom method.
   138	func (c *UnixConn) ReadFrom(b []byte) (int, Addr, error) {
   139		if !c.ok() {
   140			return 0, nil, syscall.EINVAL
   141		}
   142		n, addr, err := c.ReadFromUnix(b)
   143		return n, addr.toAddr(), err
   144	}
   145	
   146	// ReadMsgUnix reads a packet from c, copying the payload into b and
   147	// the associated out-of-band data into oob.  It returns the number of
   148	// bytes copied into b, the number of bytes copied into oob, the flags
   149	// that were set on the packet, and the source address of the packet.
   150	func (c *UnixConn) ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *UnixAddr, err error) {
   151		if !c.ok() {
   152			return 0, 0, 0, nil, syscall.EINVAL
   153		}
   154		n, oobn, flags, sa, err := c.fd.readMsg(b, oob)
   155		switch sa := sa.(type) {
   156		case *syscall.SockaddrUnix:
   157			if sa.Name != "" {
   158				addr = &UnixAddr{Name: sa.Name, Net: sotypeToNet(c.fd.sotype)}
   159			}
   160		}
   161		return
   162	}
   163	
   164	// WriteToUnix writes a packet to addr via c, copying the payload from b.
   165	//
   166	// WriteToUnix can be made to time out and return an error with
   167	// Timeout() == true after a fixed time limit; see SetDeadline and
   168	// SetWriteDeadline.  On packet-oriented connections, write timeouts
   169	// are rare.
   170	func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (n int, err error) {
   171		if !c.ok() {
   172			return 0, syscall.EINVAL
   173		}
   174		if c.fd.isConnected {
   175			return 0, &OpError{Op: "write", Net: c.fd.net, Addr: addr, Err: ErrWriteToConnected}
   176		}
   177		if addr == nil {
   178			return 0, &OpError{Op: "write", Net: c.fd.net, Addr: nil, Err: errMissingAddress}
   179		}
   180		if addr.Net != sotypeToNet(c.fd.sotype) {
   181			return 0, syscall.EAFNOSUPPORT
   182		}
   183		sa := &syscall.SockaddrUnix{Name: addr.Name}
   184		return c.fd.writeTo(b, sa)
   185	}
   186	
   187	// WriteTo implements the PacketConn WriteTo method.
   188	func (c *UnixConn) WriteTo(b []byte, addr Addr) (n int, err error) {
   189		if !c.ok() {
   190			return 0, syscall.EINVAL
   191		}
   192		a, ok := addr.(*UnixAddr)
   193		if !ok {
   194			return 0, &OpError{"write", c.fd.net, addr, syscall.EINVAL}
   195		}
   196		return c.WriteToUnix(b, a)
   197	}
   198	
   199	// WriteMsgUnix writes a packet to addr via c, copying the payload
   200	// from b and the associated out-of-band data from oob.  It returns
   201	// the number of payload and out-of-band bytes written.
   202	func (c *UnixConn) WriteMsgUnix(b, oob []byte, addr *UnixAddr) (n, oobn int, err error) {
   203		if !c.ok() {
   204			return 0, 0, syscall.EINVAL
   205		}
   206		if c.fd.sotype == syscall.SOCK_DGRAM && c.fd.isConnected {
   207			return 0, 0, &OpError{Op: "write", Net: c.fd.net, Addr: addr, Err: ErrWriteToConnected}
   208		}
   209		if addr != nil {
   210			if addr.Net != sotypeToNet(c.fd.sotype) {
   211				return 0, 0, syscall.EAFNOSUPPORT
   212			}
   213			sa := &syscall.SockaddrUnix{Name: addr.Name}
   214			return c.fd.writeMsg(b, oob, sa)
   215		}
   216		return c.fd.writeMsg(b, oob, nil)
   217	}
   218	
   219	// CloseRead shuts down the reading side of the Unix domain connection.
   220	// Most callers should just use Close.
   221	func (c *UnixConn) CloseRead() error {
   222		if !c.ok() {
   223			return syscall.EINVAL
   224		}
   225		return c.fd.closeRead()
   226	}
   227	
   228	// CloseWrite shuts down the writing side of the Unix domain connection.
   229	// Most callers should just use Close.
   230	func (c *UnixConn) CloseWrite() error {
   231		if !c.ok() {
   232			return syscall.EINVAL
   233		}
   234		return c.fd.closeWrite()
   235	}
   236	
   237	// DialUnix connects to the remote address raddr on the network net,
   238	// which must be "unix", "unixgram" or "unixpacket".  If laddr is not
   239	// nil, it is used as the local address for the connection.
   240	func DialUnix(net string, laddr, raddr *UnixAddr) (*UnixConn, error) {
   241		switch net {
   242		case "unix", "unixgram", "unixpacket":
   243		default:
   244			return nil, &OpError{Op: "dial", Net: net, Addr: raddr, Err: UnknownNetworkError(net)}
   245		}
   246		return dialUnix(net, laddr, raddr, noDeadline)
   247	}
   248	
   249	func dialUnix(net string, laddr, raddr *UnixAddr, deadline time.Time) (*UnixConn, error) {
   250		fd, err := unixSocket(net, laddr, raddr, "dial", deadline)
   251		if err != nil {
   252			return nil, &OpError{Op: "dial", Net: net, Addr: raddr, Err: err}
   253		}
   254		return newUnixConn(fd), nil
   255	}
   256	
   257	// UnixListener is a Unix domain socket listener.  Clients should
   258	// typically use variables of type Listener instead of assuming Unix
   259	// domain sockets.
   260	type UnixListener struct {
   261		fd   *netFD
   262		path string
   263	}
   264	
   265	// ListenUnix announces on the Unix domain socket laddr and returns a
   266	// Unix listener.  The network net must be "unix" or "unixpacket".
   267	func ListenUnix(net string, laddr *UnixAddr) (*UnixListener, error) {
   268		switch net {
   269		case "unix", "unixpacket":
   270		default:
   271			return nil, &OpError{Op: "listen", Net: net, Addr: laddr, Err: UnknownNetworkError(net)}
   272		}
   273		if laddr == nil {
   274			return nil, &OpError{Op: "listen", Net: net, Addr: nil, Err: errMissingAddress}
   275		}
   276		fd, err := unixSocket(net, laddr, nil, "listen", noDeadline)
   277		if err != nil {
   278			return nil, &OpError{Op: "listen", Net: net, Addr: laddr, Err: err}
   279		}
   280		return &UnixListener{fd, fd.laddr.String()}, nil
   281	}
   282	
   283	// AcceptUnix accepts the next incoming call and returns the new
   284	// connection.
   285	func (l *UnixListener) AcceptUnix() (*UnixConn, error) {
   286		if l == nil || l.fd == nil {
   287			return nil, syscall.EINVAL
   288		}
   289		toAddr := sockaddrToUnix
   290		if l.fd.sotype == syscall.SOCK_SEQPACKET {
   291			toAddr = sockaddrToUnixpacket
   292		}
   293		fd, err := l.fd.accept(toAddr)
   294		if err != nil {
   295			return nil, err
   296		}
   297		c := newUnixConn(fd)
   298		return c, nil
   299	}
   300	
   301	// Accept implements the Accept method in the Listener interface; it
   302	// waits for the next call and returns a generic Conn.
   303	func (l *UnixListener) Accept() (c Conn, err error) {
   304		c1, err := l.AcceptUnix()
   305		if err != nil {
   306			return nil, err
   307		}
   308		return c1, nil
   309	}
   310	
   311	// Close stops listening on the Unix address.  Already accepted
   312	// connections are not closed.
   313	func (l *UnixListener) Close() error {
   314		if l == nil || l.fd == nil {
   315			return syscall.EINVAL
   316		}
   317	
   318		// The operating system doesn't clean up
   319		// the file that announcing created, so
   320		// we have to clean it up ourselves.
   321		// There's a race here--we can't know for
   322		// sure whether someone else has come along
   323		// and replaced our socket name already--
   324		// but this sequence (remove then close)
   325		// is at least compatible with the auto-remove
   326		// sequence in ListenUnix.  It's only non-Go
   327		// programs that can mess us up.
   328		if l.path[0] != '@' {
   329			syscall.Unlink(l.path)
   330		}
   331		return l.fd.Close()
   332	}
   333	
   334	// Addr returns the listener's network address.
   335	func (l *UnixListener) Addr() Addr { return l.fd.laddr }
   336	
   337	// SetDeadline sets the deadline associated with the listener.
   338	// A zero time value disables the deadline.
   339	func (l *UnixListener) SetDeadline(t time.Time) (err error) {
   340		if l == nil || l.fd == nil {
   341			return syscall.EINVAL
   342		}
   343		return l.fd.setDeadline(t)
   344	}
   345	
   346	// File returns a copy of the underlying os.File, set to blocking
   347	// mode.  It is the caller's responsibility to close f when finished.
   348	// Closing l does not affect f, and closing f does not affect l.
   349	//
   350	// The returned os.File's file descriptor is different from the
   351	// connection's.  Attempting to change properties of the original
   352	// using this duplicate may or may not have the desired effect.
   353	func (l *UnixListener) File() (f *os.File, err error) { return l.fd.dup() }
   354	
   355	// ListenUnixgram listens for incoming Unix datagram packets addressed
   356	// to the local address laddr.  The network net must be "unixgram".
   357	// The returned connection's ReadFrom and WriteTo methods can be used
   358	// to receive and send packets with per-packet addressing.
   359	func ListenUnixgram(net string, laddr *UnixAddr) (*UnixConn, error) {
   360		switch net {
   361		case "unixgram":
   362		default:
   363			return nil, &OpError{Op: "listen", Net: net, Addr: laddr, Err: UnknownNetworkError(net)}
   364		}
   365		if laddr == nil {
   366			return nil, &OpError{Op: "listen", Net: net, Addr: nil, Err: errMissingAddress}
   367		}
   368		fd, err := unixSocket(net, laddr, nil, "listen", noDeadline)
   369		if err != nil {
   370			return nil, &OpError{Op: "listen", Net: net, Addr: laddr, Err: err}
   371		}
   372		return newUnixConn(fd), nil
   373	}
   374	

View as plain text