Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 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 | |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 5 | package net |
| 6 | |
| 7 | import ( |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 8 | "os" |
Mikio Hara | 03d4c7c | 2012-02-17 10:59:30 +0900 | [diff] [blame] | 9 | "syscall" |
Mikio Hara | b58b5ba | 2012-01-19 12:25:37 +0900 | [diff] [blame] | 10 | "time" |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 11 | ) |
| 12 | |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 13 | // UnixConn is an implementation of the Conn interface for connections |
| 14 | // to Unix domain sockets. |
Anthony Martin | 253ed02 | 2012-11-30 11:41:50 -0800 | [diff] [blame] | 15 | type UnixConn struct { |
| 16 | conn |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | // ReadFromUnix reads a packet from c, copying the payload into b. It |
| 20 | // returns the number of bytes copied into b and the source address of |
| 21 | // the packet. |
| 22 | // |
| 23 | // ReadFromUnix can be made to time out and return an error with |
| 24 | // Timeout() == true after a fixed time limit; see SetDeadline and |
| 25 | // SetReadDeadline. |
| 26 | func (c *UnixConn) ReadFromUnix(b []byte) (int, *UnixAddr, error) { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 27 | return 0, nil, &OpError{Op: "read", Net: c.fd.dir, Source: c.fd.laddr, Addr: c.fd.raddr, Err: syscall.EPLAN9} |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 28 | } |
| 29 | |
Mikio Hara | 3d400db | 2012-01-29 19:11:05 +0900 | [diff] [blame] | 30 | // ReadFrom implements the PacketConn ReadFrom method. |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 31 | func (c *UnixConn) ReadFrom(b []byte) (int, Addr, error) { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 32 | return 0, nil, &OpError{Op: "read", Net: c.fd.dir, Source: c.fd.laddr, Addr: c.fd.raddr, Err: syscall.EPLAN9} |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // ReadMsgUnix reads a packet from c, copying the payload into b and |
| 36 | // the associated out-of-band data into oob. It returns the number of |
| 37 | // bytes copied into b, the number of bytes copied into oob, the flags |
| 38 | // that were set on the packet, and the source address of the packet. |
| 39 | func (c *UnixConn) ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *UnixAddr, err error) { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 40 | return 0, 0, 0, nil, &OpError{Op: "read", Net: c.fd.dir, Source: c.fd.laddr, Addr: c.fd.raddr, Err: syscall.EPLAN9} |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // WriteToUnix writes a packet to addr via c, copying the payload from b. |
| 44 | // |
| 45 | // WriteToUnix can be made to time out and return an error with |
| 46 | // Timeout() == true after a fixed time limit; see SetDeadline and |
| 47 | // SetWriteDeadline. On packet-oriented connections, write timeouts |
| 48 | // are rare. |
| 49 | func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (int, error) { |
Mikio Hara | 22829bd | 2015-05-30 07:33:16 +0900 | [diff] [blame] | 50 | return 0, &OpError{Op: "write", Net: c.fd.dir, Source: c.fd.laddr, Addr: addr.opAddr(), Err: syscall.EPLAN9} |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 51 | } |
| 52 | |
Mikio Hara | 3d400db | 2012-01-29 19:11:05 +0900 | [diff] [blame] | 53 | // WriteTo implements the PacketConn WriteTo method. |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 54 | func (c *UnixConn) WriteTo(b []byte, addr Addr) (int, error) { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 55 | return 0, &OpError{Op: "write", Net: c.fd.dir, Source: c.fd.laddr, Addr: addr, Err: syscall.EPLAN9} |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 56 | } |
| 57 | |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 58 | // WriteMsgUnix writes a packet to addr via c, copying the payload |
| 59 | // from b and the associated out-of-band data from oob. It returns |
| 60 | // the number of payload and out-of-band bytes written. |
| 61 | func (c *UnixConn) WriteMsgUnix(b, oob []byte, addr *UnixAddr) (n, oobn int, err error) { |
Mikio Hara | 22829bd | 2015-05-30 07:33:16 +0900 | [diff] [blame] | 62 | return 0, 0, &OpError{Op: "write", Net: c.fd.dir, Source: c.fd.laddr, Addr: addr.opAddr(), Err: syscall.EPLAN9} |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 63 | } |
| 64 | |
Mikio Hara | 0d19725 | 2012-12-16 11:51:47 +0900 | [diff] [blame] | 65 | // CloseRead shuts down the reading side of the Unix domain connection. |
| 66 | // Most callers should just use Close. |
Rémy Oudompheng | 0ce9045 | 2012-05-30 00:08:58 +0200 | [diff] [blame] | 67 | func (c *UnixConn) CloseRead() error { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 68 | return &OpError{Op: "close", Net: c.fd.dir, Source: c.fd.laddr, Addr: c.fd.raddr, Err: syscall.EPLAN9} |
Rémy Oudompheng | 0ce9045 | 2012-05-30 00:08:58 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Mikio Hara | 0d19725 | 2012-12-16 11:51:47 +0900 | [diff] [blame] | 71 | // CloseWrite shuts down the writing side of the Unix domain connection. |
| 72 | // Most callers should just use Close. |
Rémy Oudompheng | 0ce9045 | 2012-05-30 00:08:58 +0200 | [diff] [blame] | 73 | func (c *UnixConn) CloseWrite() error { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 74 | return &OpError{Op: "close", Net: c.fd.dir, Source: c.fd.laddr, Addr: c.fd.raddr, Err: syscall.EPLAN9} |
Rémy Oudompheng | 0ce9045 | 2012-05-30 00:08:58 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 77 | // DialUnix connects to the remote address raddr on the network net, |
Mikio Hara | 0d19725 | 2012-12-16 11:51:47 +0900 | [diff] [blame] | 78 | // which must be "unix", "unixgram" or "unixpacket". If laddr is not |
| 79 | // nil, it is used as the local address for the connection. |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 80 | func DialUnix(net string, laddr, raddr *UnixAddr) (*UnixConn, error) { |
Brad Fitzpatrick | ef6806f | 2012-11-08 10:35:16 -0600 | [diff] [blame] | 81 | return dialUnix(net, laddr, raddr, noDeadline) |
| 82 | } |
| 83 | |
| 84 | func dialUnix(net string, laddr, raddr *UnixAddr, deadline time.Time) (*UnixConn, error) { |
Mikio Hara | 22829bd | 2015-05-30 07:33:16 +0900 | [diff] [blame] | 85 | return nil, &OpError{Op: "dial", Net: net, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: syscall.EPLAN9} |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 86 | } |
| 87 | |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 88 | // UnixListener is a Unix domain socket listener. Clients should |
| 89 | // typically use variables of type Listener instead of assuming Unix |
| 90 | // domain sockets. |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 91 | type UnixListener struct { |
| 92 | fd *netFD |
| 93 | } |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 94 | |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 95 | // ListenUnix announces on the Unix domain socket laddr and returns a |
Mikio Hara | a0430da | 2013-02-09 08:18:32 +0900 | [diff] [blame] | 96 | // Unix listener. The network net must be "unix" or "unixpacket". |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 97 | func ListenUnix(net string, laddr *UnixAddr) (*UnixListener, error) { |
Mikio Hara | 22829bd | 2015-05-30 07:33:16 +0900 | [diff] [blame] | 98 | return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: syscall.EPLAN9} |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 99 | } |
| 100 | |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 101 | // AcceptUnix accepts the next incoming call and returns the new |
Kamil Kisiel | a3834a2 | 2013-09-06 12:00:03 -0700 | [diff] [blame] | 102 | // connection. |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 103 | func (l *UnixListener) AcceptUnix() (*UnixConn, error) { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 104 | return nil, &OpError{Op: "accept", Net: l.fd.dir, Source: nil, Addr: l.fd.laddr, Err: syscall.EPLAN9} |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 107 | // Accept implements the Accept method in the Listener interface; it |
| 108 | // waits for the next call and returns a generic Conn. |
| 109 | func (l *UnixListener) Accept() (Conn, error) { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 110 | return nil, &OpError{Op: "accept", Net: l.fd.dir, Source: nil, Addr: l.fd.laddr, Err: syscall.EPLAN9} |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // Close stops listening on the Unix address. Already accepted |
| 114 | // connections are not closed. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 115 | func (l *UnixListener) Close() error { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 116 | return &OpError{Op: "close", Net: l.fd.dir, Source: nil, Addr: l.fd.laddr, Err: syscall.EPLAN9} |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Addr returns the listener's network address. |
Shenghou Ma | 7e43aee | 2015-02-03 12:59:40 -0500 | [diff] [blame] | 120 | // The Addr returned is shared by all invocations of Addr, so |
| 121 | // do not modify it. |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 122 | func (l *UnixListener) Addr() Addr { return nil } |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 123 | |
| 124 | // SetDeadline sets the deadline associated with the listener. |
| 125 | // A zero time value disables the deadline. |
| 126 | func (l *UnixListener) SetDeadline(t time.Time) error { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 127 | return &OpError{Op: "set", Net: l.fd.dir, Source: nil, Addr: l.fd.laddr, Err: syscall.EPLAN9} |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // File returns a copy of the underlying os.File, set to blocking |
| 131 | // mode. It is the caller's responsibility to close f when finished. |
| 132 | // Closing l does not affect f, and closing f does not affect l. |
Mikio Hara | 73417e4 | 2013-04-24 08:32:11 +0900 | [diff] [blame] | 133 | // |
| 134 | // The returned os.File's file descriptor is different from the |
| 135 | // connection's. Attempting to change properties of the original |
| 136 | // using this duplicate may or may not have the desired effect. |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 137 | func (l *UnixListener) File() (*os.File, error) { |
Mikio Hara | afd2d2b | 2015-04-21 22:53:47 +0900 | [diff] [blame] | 138 | return nil, &OpError{Op: "file", Net: l.fd.dir, Source: nil, Addr: l.fd.laddr, Err: syscall.EPLAN9} |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // ListenUnixgram listens for incoming Unix datagram packets addressed |
Mikio Hara | 8a448ef | 2013-03-31 16:48:18 +0900 | [diff] [blame] | 142 | // to the local address laddr. The network net must be "unixgram". |
| 143 | // The returned connection's ReadFrom and WriteTo methods can be used |
| 144 | // to receive and send packets with per-packet addressing. |
Mikio Hara | 0d19725 | 2012-12-16 11:51:47 +0900 | [diff] [blame] | 145 | func ListenUnixgram(net string, laddr *UnixAddr) (*UnixConn, error) { |
Mikio Hara | 22829bd | 2015-05-30 07:33:16 +0900 | [diff] [blame] | 146 | return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: syscall.EPLAN9} |
Mikio Hara | d6665bc | 2012-09-26 16:11:49 +0900 | [diff] [blame] | 147 | } |