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.
5 // +build darwin freebsd linux netbsd openbsd windows
17 func unixSocket(net string, laddr, raddr *UnixAddr, mode string) (fd *netFD, err error) {
21 return nil, UnknownNetworkError(net)
23 sotype = syscall.SOCK_STREAM
25 sotype = syscall.SOCK_DGRAM
27 sotype = syscall.SOCK_SEQPACKET
30 var la, ra syscall.Sockaddr
33 panic("unixSocket mode " + mode)
37 la = &syscall.SockaddrUnix{Name: laddr.Name}
40 ra = &syscall.SockaddrUnix{Name: raddr.Name}
41 } else if sotype != syscall.SOCK_DGRAM || laddr == nil {
42 return nil, &OpError{Op: mode, Net: net, Err: errMissingAddress}
47 return nil, &OpError{mode, net, nil, errMissingAddress}
49 la = &syscall.SockaddrUnix{Name: laddr.Name}
51 return nil, &OpError{Op: mode, Net: net, Addr: raddr, Err: &AddrError{Err: "unexpected remote address", Addr: raddr.String()}}
56 if sotype == syscall.SOCK_DGRAM {
57 f = sockaddrToUnixgram
58 } else if sotype == syscall.SOCK_SEQPACKET {
59 f = sockaddrToUnixpacket
62 fd, err = socket(net, syscall.AF_UNIX, sotype, 0, false, la, ra, f)
73 return nil, &OpError{Op: mode, Net: net, Addr: addr, Err: err}
76 func sockaddrToUnix(sa syscall.Sockaddr) Addr {
77 if s, ok := sa.(*syscall.SockaddrUnix); ok {
78 return &UnixAddr{s.Name, "unix"}
83 func sockaddrToUnixgram(sa syscall.Sockaddr) Addr {
84 if s, ok := sa.(*syscall.SockaddrUnix); ok {
85 return &UnixAddr{s.Name, "unixgram"}
90 func sockaddrToUnixpacket(sa syscall.Sockaddr) Addr {
91 if s, ok := sa.(*syscall.SockaddrUnix); ok {
92 return &UnixAddr{s.Name, "unixpacket"}
97 func sotypeToNet(sotype int) string {
99 case syscall.SOCK_STREAM:
101 case syscall.SOCK_SEQPACKET:
103 case syscall.SOCK_DGRAM:
106 panic("sotypeToNet unknown socket type")
111 // UnixConn is an implementation of the Conn interface
112 // for connections to Unix domain sockets.
113 type UnixConn struct {
117 func newUnixConn(fd *netFD) *UnixConn { return &UnixConn{fd} }
119 func (c *UnixConn) ok() bool { return c != nil && c.fd != nil }
121 // Implementation of the Conn interface - see Conn for documentation.
123 // Read implements the Conn Read method.
124 func (c *UnixConn) Read(b []byte) (n int, err error) {
126 return 0, syscall.EINVAL
131 // Write implements the Conn Write method.
132 func (c *UnixConn) Write(b []byte) (n int, err error) {
134 return 0, syscall.EINVAL
139 // Close closes the Unix domain connection.
140 func (c *UnixConn) Close() error {
142 return syscall.EINVAL
147 // LocalAddr returns the local network address, a *UnixAddr.
148 // Unlike in other protocols, LocalAddr is usually nil for dialed connections.
149 func (c *UnixConn) LocalAddr() Addr {
156 // RemoteAddr returns the remote network address, a *UnixAddr.
157 // Unlike in other protocols, RemoteAddr is usually nil for connections
158 // accepted by a listener.
159 func (c *UnixConn) RemoteAddr() Addr {
166 // SetDeadline implements the Conn SetDeadline method.
167 func (c *UnixConn) SetDeadline(t time.Time) error {
169 return syscall.EINVAL
171 return setDeadline(c.fd, t)
174 // SetReadDeadline implements the Conn SetReadDeadline method.
175 func (c *UnixConn) SetReadDeadline(t time.Time) error {
177 return syscall.EINVAL
179 return setReadDeadline(c.fd, t)
182 // SetWriteDeadline implements the Conn SetWriteDeadline method.
183 func (c *UnixConn) SetWriteDeadline(t time.Time) error {
185 return syscall.EINVAL
187 return setWriteDeadline(c.fd, t)
190 // SetReadBuffer sets the size of the operating system's
191 // receive buffer associated with the connection.
192 func (c *UnixConn) SetReadBuffer(bytes int) error {
194 return syscall.EINVAL
196 return setReadBuffer(c.fd, bytes)
199 // SetWriteBuffer sets the size of the operating system's
200 // transmit buffer associated with the connection.
201 func (c *UnixConn) SetWriteBuffer(bytes int) error {
203 return syscall.EINVAL
205 return setWriteBuffer(c.fd, bytes)
208 // ReadFromUnix reads a packet from c, copying the payload into b.
209 // It returns the number of bytes copied into b and the source address
212 // ReadFromUnix can be made to time out and return
213 // an error with Timeout() == true after a fixed time limit;
214 // see SetDeadline and SetReadDeadline.
215 func (c *UnixConn) ReadFromUnix(b []byte) (n int, addr *UnixAddr, err error) {
217 return 0, nil, syscall.EINVAL
219 n, sa, err := c.fd.ReadFrom(b)
220 switch sa := sa.(type) {
221 case *syscall.SockaddrUnix:
222 addr = &UnixAddr{sa.Name, sotypeToNet(c.fd.sotype)}
227 // ReadFrom implements the PacketConn ReadFrom method.
228 func (c *UnixConn) ReadFrom(b []byte) (n int, addr Addr, err error) {
230 return 0, nil, syscall.EINVAL
232 n, uaddr, err := c.ReadFromUnix(b)
233 return n, uaddr.toAddr(), err
236 // WriteToUnix writes a packet to addr via c, copying the payload from b.
238 // WriteToUnix can be made to time out and return
239 // an error with Timeout() == true after a fixed time limit;
240 // see SetDeadline and SetWriteDeadline.
241 // On packet-oriented connections, write timeouts are rare.
242 func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (n int, err error) {
244 return 0, syscall.EINVAL
246 if addr.Net != sotypeToNet(c.fd.sotype) {
247 return 0, syscall.EAFNOSUPPORT
249 sa := &syscall.SockaddrUnix{Name: addr.Name}
250 return c.fd.WriteTo(b, sa)
253 // WriteTo implements the PacketConn WriteTo method.
254 func (c *UnixConn) WriteTo(b []byte, addr Addr) (n int, err error) {
256 return 0, syscall.EINVAL
258 a, ok := addr.(*UnixAddr)
260 return 0, &OpError{"write", c.fd.net, addr, syscall.EINVAL}
262 return c.WriteToUnix(b, a)
265 // ReadMsgUnix reads a packet from c, copying the payload into b
266 // and the associated out-of-band data into oob.
267 // It returns the number of bytes copied into b, the number of
268 // bytes copied into oob, the flags that were set on the packet,
269 // and the source address of the packet.
270 func (c *UnixConn) ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *UnixAddr, err error) {
272 return 0, 0, 0, nil, syscall.EINVAL
274 n, oobn, flags, sa, err := c.fd.ReadMsg(b, oob)
275 switch sa := sa.(type) {
276 case *syscall.SockaddrUnix:
277 addr = &UnixAddr{sa.Name, sotypeToNet(c.fd.sotype)}
282 // WriteMsgUnix writes a packet to addr via c, copying the payload from b
283 // and the associated out-of-band data from oob. It returns the number
284 // of payload and out-of-band bytes written.
285 func (c *UnixConn) WriteMsgUnix(b, oob []byte, addr *UnixAddr) (n, oobn int, err error) {
287 return 0, 0, syscall.EINVAL
290 if addr.Net != sotypeToNet(c.fd.sotype) {
291 return 0, 0, syscall.EAFNOSUPPORT
293 sa := &syscall.SockaddrUnix{Name: addr.Name}
294 return c.fd.WriteMsg(b, oob, sa)
296 return c.fd.WriteMsg(b, oob, nil)
299 // File returns a copy of the underlying os.File, set to blocking mode.
300 // It is the caller's responsibility to close f when finished.
301 // Closing c does not affect f, and closing f does not affect c.
302 func (c *UnixConn) File() (f *os.File, err error) { return c.fd.dup() }
304 // DialUnix connects to the remote address raddr on the network net,
305 // which must be "unix" or "unixgram". If laddr is not nil, it is used
306 // as the local address for the connection.
307 func DialUnix(net string, laddr, raddr *UnixAddr) (*UnixConn, error) {
308 fd, err := unixSocket(net, laddr, raddr, "dial")
312 return newUnixConn(fd), nil
315 // UnixListener is a Unix domain socket listener.
316 // Clients should typically use variables of type Listener
317 // instead of assuming Unix domain sockets.
318 type UnixListener struct {
323 // ListenUnix announces on the Unix domain socket laddr and returns a Unix listener.
324 // Net must be "unix" (stream sockets).
325 func ListenUnix(net string, laddr *UnixAddr) (*UnixListener, error) {
326 if net != "unix" && net != "unixgram" && net != "unixpacket" {
327 return nil, UnknownNetworkError(net)
330 laddr = &UnixAddr{laddr.Name, net} // make our own copy
332 fd, err := unixSocket(net, laddr, nil, "listen")
336 err = syscall.Listen(fd.sysfd, listenerBacklog)
338 closesocket(fd.sysfd)
339 return nil, &OpError{Op: "listen", Net: net, Addr: laddr, Err: err}
341 return &UnixListener{fd, laddr.Name}, nil
344 // AcceptUnix accepts the next incoming call and returns the new connection
345 // and the remote address.
346 func (l *UnixListener) AcceptUnix() (*UnixConn, error) {
347 if l == nil || l.fd == nil {
348 return nil, syscall.EINVAL
350 fd, err := l.fd.accept(sockaddrToUnix)
358 // Accept implements the Accept method in the Listener interface;
359 // it waits for the next call and returns a generic Conn.
360 func (l *UnixListener) Accept() (c Conn, err error) {
361 c1, err := l.AcceptUnix()
368 // Close stops listening on the Unix address.
369 // Already accepted connections are not closed.
370 func (l *UnixListener) Close() error {
371 if l == nil || l.fd == nil {
372 return syscall.EINVAL
375 // The operating system doesn't clean up
376 // the file that announcing created, so
377 // we have to clean it up ourselves.
378 // There's a race here--we can't know for
379 // sure whether someone else has come along
380 // and replaced our socket name already--
381 // but this sequence (remove then close)
382 // is at least compatible with the auto-remove
383 // sequence in ListenUnix. It's only non-Go
384 // programs that can mess us up.
385 if l.path[0] != '@' {
386 syscall.Unlink(l.path)
393 // Addr returns the listener's network address.
394 func (l *UnixListener) Addr() Addr { return l.fd.laddr }
396 // SetDeadline sets the deadline associated with the listener.
397 // A zero time value disables the deadline.
398 func (l *UnixListener) SetDeadline(t time.Time) (err error) {
399 if l == nil || l.fd == nil {
400 return syscall.EINVAL
402 return setDeadline(l.fd, t)
405 // File returns a copy of the underlying os.File, set to blocking mode.
406 // It is the caller's responsibility to close f when finished.
407 // Closing l does not affect f, and closing f does not affect l.
408 func (l *UnixListener) File() (f *os.File, err error) { return l.fd.dup() }
410 // ListenUnixgram listens for incoming Unix datagram packets addressed to the
411 // local address laddr. The returned connection c's ReadFrom
412 // and WriteTo methods can be used to receive and send UDP
413 // packets with per-packet addressing. The network net must be "unixgram".
414 func ListenUnixgram(net string, laddr *UnixAddr) (*UDPConn, error) {
418 return nil, UnknownNetworkError(net)
421 return nil, &OpError{"listen", net, nil, errMissingAddress}
423 fd, err := unixSocket(net, laddr, nil, "listen")
427 return newUDPConn(fd), nil