1 |
747 |
jeremybenn |
// 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 freebsd linux netbsd openbsd windows
|
6 |
|
|
|
7 |
|
|
// Unix domain sockets
|
8 |
|
|
|
9 |
|
|
package net
|
10 |
|
|
|
11 |
|
|
import (
|
12 |
|
|
"os"
|
13 |
|
|
"syscall"
|
14 |
|
|
"time"
|
15 |
|
|
)
|
16 |
|
|
|
17 |
|
|
func unixSocket(net string, laddr, raddr *UnixAddr, mode string) (fd *netFD, err error) {
|
18 |
|
|
var sotype int
|
19 |
|
|
switch net {
|
20 |
|
|
default:
|
21 |
|
|
return nil, UnknownNetworkError(net)
|
22 |
|
|
case "unix":
|
23 |
|
|
sotype = syscall.SOCK_STREAM
|
24 |
|
|
case "unixgram":
|
25 |
|
|
sotype = syscall.SOCK_DGRAM
|
26 |
|
|
case "unixpacket":
|
27 |
|
|
sotype = syscall.SOCK_SEQPACKET
|
28 |
|
|
}
|
29 |
|
|
|
30 |
|
|
var la, ra syscall.Sockaddr
|
31 |
|
|
switch mode {
|
32 |
|
|
default:
|
33 |
|
|
panic("unixSocket mode " + mode)
|
34 |
|
|
|
35 |
|
|
case "dial":
|
36 |
|
|
if laddr != nil {
|
37 |
|
|
la = &syscall.SockaddrUnix{Name: laddr.Name}
|
38 |
|
|
}
|
39 |
|
|
if raddr != nil {
|
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}
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
case "listen":
|
46 |
|
|
if laddr == nil {
|
47 |
|
|
return nil, &OpError{mode, net, nil, errMissingAddress}
|
48 |
|
|
}
|
49 |
|
|
la = &syscall.SockaddrUnix{Name: laddr.Name}
|
50 |
|
|
if raddr != nil {
|
51 |
|
|
return nil, &OpError{Op: mode, Net: net, Addr: raddr, Err: &AddrError{Err: "unexpected remote address", Addr: raddr.String()}}
|
52 |
|
|
}
|
53 |
|
|
}
|
54 |
|
|
|
55 |
|
|
f := sockaddrToUnix
|
56 |
|
|
if sotype == syscall.SOCK_DGRAM {
|
57 |
|
|
f = sockaddrToUnixgram
|
58 |
|
|
} else if sotype == syscall.SOCK_SEQPACKET {
|
59 |
|
|
f = sockaddrToUnixpacket
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
fd, oserr := socket(net, syscall.AF_UNIX, sotype, 0, la, ra, f)
|
63 |
|
|
if oserr != nil {
|
64 |
|
|
goto Error
|
65 |
|
|
}
|
66 |
|
|
return fd, nil
|
67 |
|
|
|
68 |
|
|
Error:
|
69 |
|
|
addr := raddr
|
70 |
|
|
if mode == "listen" {
|
71 |
|
|
addr = laddr
|
72 |
|
|
}
|
73 |
|
|
return nil, &OpError{Op: mode, Net: net, Addr: addr, Err: oserr}
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
func sockaddrToUnix(sa syscall.Sockaddr) Addr {
|
77 |
|
|
if s, ok := sa.(*syscall.SockaddrUnix); ok {
|
78 |
|
|
return &UnixAddr{s.Name, "unix"}
|
79 |
|
|
}
|
80 |
|
|
return nil
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
func sockaddrToUnixgram(sa syscall.Sockaddr) Addr {
|
84 |
|
|
if s, ok := sa.(*syscall.SockaddrUnix); ok {
|
85 |
|
|
return &UnixAddr{s.Name, "unixgram"}
|
86 |
|
|
}
|
87 |
|
|
return nil
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
func sockaddrToUnixpacket(sa syscall.Sockaddr) Addr {
|
91 |
|
|
if s, ok := sa.(*syscall.SockaddrUnix); ok {
|
92 |
|
|
return &UnixAddr{s.Name, "unixpacket"}
|
93 |
|
|
}
|
94 |
|
|
return nil
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
func sotypeToNet(sotype int) string {
|
98 |
|
|
switch sotype {
|
99 |
|
|
case syscall.SOCK_STREAM:
|
100 |
|
|
return "unix"
|
101 |
|
|
case syscall.SOCK_SEQPACKET:
|
102 |
|
|
return "unixpacket"
|
103 |
|
|
case syscall.SOCK_DGRAM:
|
104 |
|
|
return "unixgram"
|
105 |
|
|
default:
|
106 |
|
|
panic("sotypeToNet unknown socket type")
|
107 |
|
|
}
|
108 |
|
|
return ""
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
// UnixConn is an implementation of the Conn interface
|
112 |
|
|
// for connections to Unix domain sockets.
|
113 |
|
|
type UnixConn struct {
|
114 |
|
|
fd *netFD
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
func newUnixConn(fd *netFD) *UnixConn { return &UnixConn{fd} }
|
118 |
|
|
|
119 |
|
|
func (c *UnixConn) ok() bool { return c != nil && c.fd != nil }
|
120 |
|
|
|
121 |
|
|
// Implementation of the Conn interface - see Conn for documentation.
|
122 |
|
|
|
123 |
|
|
// Read implements the Conn Read method.
|
124 |
|
|
func (c *UnixConn) Read(b []byte) (n int, err error) {
|
125 |
|
|
if !c.ok() {
|
126 |
|
|
return 0, os.EINVAL
|
127 |
|
|
}
|
128 |
|
|
return c.fd.Read(b)
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
// Write implements the Conn Write method.
|
132 |
|
|
func (c *UnixConn) Write(b []byte) (n int, err error) {
|
133 |
|
|
if !c.ok() {
|
134 |
|
|
return 0, os.EINVAL
|
135 |
|
|
}
|
136 |
|
|
return c.fd.Write(b)
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
// Close closes the Unix domain connection.
|
140 |
|
|
func (c *UnixConn) Close() error {
|
141 |
|
|
if !c.ok() {
|
142 |
|
|
return os.EINVAL
|
143 |
|
|
}
|
144 |
|
|
err := c.fd.Close()
|
145 |
|
|
c.fd = nil
|
146 |
|
|
return err
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
// LocalAddr returns the local network address, a *UnixAddr.
|
150 |
|
|
// Unlike in other protocols, LocalAddr is usually nil for dialed connections.
|
151 |
|
|
func (c *UnixConn) LocalAddr() Addr {
|
152 |
|
|
if !c.ok() {
|
153 |
|
|
return nil
|
154 |
|
|
}
|
155 |
|
|
return c.fd.laddr
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
// RemoteAddr returns the remote network address, a *UnixAddr.
|
159 |
|
|
// Unlike in other protocols, RemoteAddr is usually nil for connections
|
160 |
|
|
// accepted by a listener.
|
161 |
|
|
func (c *UnixConn) RemoteAddr() Addr {
|
162 |
|
|
if !c.ok() {
|
163 |
|
|
return nil
|
164 |
|
|
}
|
165 |
|
|
return c.fd.raddr
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
// SetDeadline implements the Conn SetDeadline method.
|
169 |
|
|
func (c *UnixConn) SetDeadline(t time.Time) error {
|
170 |
|
|
if !c.ok() {
|
171 |
|
|
return os.EINVAL
|
172 |
|
|
}
|
173 |
|
|
return setDeadline(c.fd, t)
|
174 |
|
|
}
|
175 |
|
|
|
176 |
|
|
// SetReadDeadline implements the Conn SetReadDeadline method.
|
177 |
|
|
func (c *UnixConn) SetReadDeadline(t time.Time) error {
|
178 |
|
|
if !c.ok() {
|
179 |
|
|
return os.EINVAL
|
180 |
|
|
}
|
181 |
|
|
return setReadDeadline(c.fd, t)
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
// SetWriteDeadline implements the Conn SetWriteDeadline method.
|
185 |
|
|
func (c *UnixConn) SetWriteDeadline(t time.Time) error {
|
186 |
|
|
if !c.ok() {
|
187 |
|
|
return os.EINVAL
|
188 |
|
|
}
|
189 |
|
|
return setWriteDeadline(c.fd, t)
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
// SetReadBuffer sets the size of the operating system's
|
193 |
|
|
// receive buffer associated with the connection.
|
194 |
|
|
func (c *UnixConn) SetReadBuffer(bytes int) error {
|
195 |
|
|
if !c.ok() {
|
196 |
|
|
return os.EINVAL
|
197 |
|
|
}
|
198 |
|
|
return setReadBuffer(c.fd, bytes)
|
199 |
|
|
}
|
200 |
|
|
|
201 |
|
|
// SetWriteBuffer sets the size of the operating system's
|
202 |
|
|
// transmit buffer associated with the connection.
|
203 |
|
|
func (c *UnixConn) SetWriteBuffer(bytes int) error {
|
204 |
|
|
if !c.ok() {
|
205 |
|
|
return os.EINVAL
|
206 |
|
|
}
|
207 |
|
|
return setWriteBuffer(c.fd, bytes)
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
// ReadFromUnix reads a packet from c, copying the payload into b.
|
211 |
|
|
// It returns the number of bytes copied into b and the return address
|
212 |
|
|
// that was on the packet.
|
213 |
|
|
//
|
214 |
|
|
// ReadFromUnix can be made to time out and return
|
215 |
|
|
// an error with Timeout() == true after a fixed time limit;
|
216 |
|
|
// see SetDeadline and SetReadDeadline.
|
217 |
|
|
func (c *UnixConn) ReadFromUnix(b []byte) (n int, addr *UnixAddr, err error) {
|
218 |
|
|
if !c.ok() {
|
219 |
|
|
return 0, nil, os.EINVAL
|
220 |
|
|
}
|
221 |
|
|
n, sa, err := c.fd.ReadFrom(b)
|
222 |
|
|
switch sa := sa.(type) {
|
223 |
|
|
case *syscall.SockaddrUnix:
|
224 |
|
|
addr = &UnixAddr{sa.Name, sotypeToNet(c.fd.sotype)}
|
225 |
|
|
}
|
226 |
|
|
return
|
227 |
|
|
}
|
228 |
|
|
|
229 |
|
|
// ReadFrom implements the PacketConn ReadFrom method.
|
230 |
|
|
func (c *UnixConn) ReadFrom(b []byte) (n int, addr Addr, err error) {
|
231 |
|
|
if !c.ok() {
|
232 |
|
|
return 0, nil, os.EINVAL
|
233 |
|
|
}
|
234 |
|
|
n, uaddr, err := c.ReadFromUnix(b)
|
235 |
|
|
return n, uaddr.toAddr(), err
|
236 |
|
|
}
|
237 |
|
|
|
238 |
|
|
// WriteToUnix writes a packet to addr via c, copying the payload from b.
|
239 |
|
|
//
|
240 |
|
|
// WriteToUnix can be made to time out and return
|
241 |
|
|
// an error with Timeout() == true after a fixed time limit;
|
242 |
|
|
// see SetDeadline and SetWriteDeadline.
|
243 |
|
|
// On packet-oriented connections, write timeouts are rare.
|
244 |
|
|
func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (n int, err error) {
|
245 |
|
|
if !c.ok() {
|
246 |
|
|
return 0, os.EINVAL
|
247 |
|
|
}
|
248 |
|
|
if addr.Net != sotypeToNet(c.fd.sotype) {
|
249 |
|
|
return 0, os.EAFNOSUPPORT
|
250 |
|
|
}
|
251 |
|
|
sa := &syscall.SockaddrUnix{Name: addr.Name}
|
252 |
|
|
return c.fd.WriteTo(b, sa)
|
253 |
|
|
}
|
254 |
|
|
|
255 |
|
|
// WriteTo implements the PacketConn WriteTo method.
|
256 |
|
|
func (c *UnixConn) WriteTo(b []byte, addr Addr) (n int, err error) {
|
257 |
|
|
if !c.ok() {
|
258 |
|
|
return 0, os.EINVAL
|
259 |
|
|
}
|
260 |
|
|
a, ok := addr.(*UnixAddr)
|
261 |
|
|
if !ok {
|
262 |
|
|
return 0, &OpError{"write", c.fd.net, addr, os.EINVAL}
|
263 |
|
|
}
|
264 |
|
|
return c.WriteToUnix(b, a)
|
265 |
|
|
}
|
266 |
|
|
|
267 |
|
|
func (c *UnixConn) ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *UnixAddr, err error) {
|
268 |
|
|
if !c.ok() {
|
269 |
|
|
return 0, 0, 0, nil, os.EINVAL
|
270 |
|
|
}
|
271 |
|
|
n, oobn, flags, sa, err := c.fd.ReadMsg(b, oob)
|
272 |
|
|
switch sa := sa.(type) {
|
273 |
|
|
case *syscall.SockaddrUnix:
|
274 |
|
|
addr = &UnixAddr{sa.Name, sotypeToNet(c.fd.sotype)}
|
275 |
|
|
}
|
276 |
|
|
return
|
277 |
|
|
}
|
278 |
|
|
|
279 |
|
|
func (c *UnixConn) WriteMsgUnix(b, oob []byte, addr *UnixAddr) (n, oobn int, err error) {
|
280 |
|
|
if !c.ok() {
|
281 |
|
|
return 0, 0, os.EINVAL
|
282 |
|
|
}
|
283 |
|
|
if addr != nil {
|
284 |
|
|
if addr.Net != sotypeToNet(c.fd.sotype) {
|
285 |
|
|
return 0, 0, os.EAFNOSUPPORT
|
286 |
|
|
}
|
287 |
|
|
sa := &syscall.SockaddrUnix{Name: addr.Name}
|
288 |
|
|
return c.fd.WriteMsg(b, oob, sa)
|
289 |
|
|
}
|
290 |
|
|
return c.fd.WriteMsg(b, oob, nil)
|
291 |
|
|
}
|
292 |
|
|
|
293 |
|
|
// File returns a copy of the underlying os.File, set to blocking mode.
|
294 |
|
|
// It is the caller's responsibility to close f when finished.
|
295 |
|
|
// Closing c does not affect f, and closing f does not affect c.
|
296 |
|
|
func (c *UnixConn) File() (f *os.File, err error) { return c.fd.dup() }
|
297 |
|
|
|
298 |
|
|
// DialUnix connects to the remote address raddr on the network net,
|
299 |
|
|
// which must be "unix" or "unixgram". If laddr is not nil, it is used
|
300 |
|
|
// as the local address for the connection.
|
301 |
|
|
func DialUnix(net string, laddr, raddr *UnixAddr) (*UnixConn, error) {
|
302 |
|
|
fd, err := unixSocket(net, laddr, raddr, "dial")
|
303 |
|
|
if err != nil {
|
304 |
|
|
return nil, err
|
305 |
|
|
}
|
306 |
|
|
return newUnixConn(fd), nil
|
307 |
|
|
}
|
308 |
|
|
|
309 |
|
|
// UnixListener is a Unix domain socket listener.
|
310 |
|
|
// Clients should typically use variables of type Listener
|
311 |
|
|
// instead of assuming Unix domain sockets.
|
312 |
|
|
type UnixListener struct {
|
313 |
|
|
fd *netFD
|
314 |
|
|
path string
|
315 |
|
|
}
|
316 |
|
|
|
317 |
|
|
// ListenUnix announces on the Unix domain socket laddr and returns a Unix listener.
|
318 |
|
|
// Net must be "unix" (stream sockets).
|
319 |
|
|
func ListenUnix(net string, laddr *UnixAddr) (*UnixListener, error) {
|
320 |
|
|
if net != "unix" && net != "unixgram" && net != "unixpacket" {
|
321 |
|
|
return nil, UnknownNetworkError(net)
|
322 |
|
|
}
|
323 |
|
|
if laddr != nil {
|
324 |
|
|
laddr = &UnixAddr{laddr.Name, net} // make our own copy
|
325 |
|
|
}
|
326 |
|
|
fd, err := unixSocket(net, laddr, nil, "listen")
|
327 |
|
|
if err != nil {
|
328 |
|
|
return nil, err
|
329 |
|
|
}
|
330 |
|
|
err = syscall.Listen(fd.sysfd, listenerBacklog)
|
331 |
|
|
if err != nil {
|
332 |
|
|
closesocket(fd.sysfd)
|
333 |
|
|
return nil, &OpError{Op: "listen", Net: net, Addr: laddr, Err: err}
|
334 |
|
|
}
|
335 |
|
|
return &UnixListener{fd, laddr.Name}, nil
|
336 |
|
|
}
|
337 |
|
|
|
338 |
|
|
// AcceptUnix accepts the next incoming call and returns the new connection
|
339 |
|
|
// and the remote address.
|
340 |
|
|
func (l *UnixListener) AcceptUnix() (*UnixConn, error) {
|
341 |
|
|
if l == nil || l.fd == nil {
|
342 |
|
|
return nil, os.EINVAL
|
343 |
|
|
}
|
344 |
|
|
fd, err := l.fd.accept(sockaddrToUnix)
|
345 |
|
|
if err != nil {
|
346 |
|
|
return nil, err
|
347 |
|
|
}
|
348 |
|
|
c := newUnixConn(fd)
|
349 |
|
|
return c, nil
|
350 |
|
|
}
|
351 |
|
|
|
352 |
|
|
// Accept implements the Accept method in the Listener interface;
|
353 |
|
|
// it waits for the next call and returns a generic Conn.
|
354 |
|
|
func (l *UnixListener) Accept() (c Conn, err error) {
|
355 |
|
|
c1, err := l.AcceptUnix()
|
356 |
|
|
if err != nil {
|
357 |
|
|
return nil, err
|
358 |
|
|
}
|
359 |
|
|
return c1, nil
|
360 |
|
|
}
|
361 |
|
|
|
362 |
|
|
// Close stops listening on the Unix address.
|
363 |
|
|
// Already accepted connections are not closed.
|
364 |
|
|
func (l *UnixListener) Close() error {
|
365 |
|
|
if l == nil || l.fd == nil {
|
366 |
|
|
return os.EINVAL
|
367 |
|
|
}
|
368 |
|
|
|
369 |
|
|
// The operating system doesn't clean up
|
370 |
|
|
// the file that announcing created, so
|
371 |
|
|
// we have to clean it up ourselves.
|
372 |
|
|
// There's a race here--we can't know for
|
373 |
|
|
// sure whether someone else has come along
|
374 |
|
|
// and replaced our socket name already--
|
375 |
|
|
// but this sequence (remove then close)
|
376 |
|
|
// is at least compatible with the auto-remove
|
377 |
|
|
// sequence in ListenUnix. It's only non-Go
|
378 |
|
|
// programs that can mess us up.
|
379 |
|
|
if l.path[0] != '@' {
|
380 |
|
|
syscall.Unlink(l.path)
|
381 |
|
|
}
|
382 |
|
|
err := l.fd.Close()
|
383 |
|
|
l.fd = nil
|
384 |
|
|
return err
|
385 |
|
|
}
|
386 |
|
|
|
387 |
|
|
// Addr returns the listener's network address.
|
388 |
|
|
func (l *UnixListener) Addr() Addr { return l.fd.laddr }
|
389 |
|
|
|
390 |
|
|
// SetDeadline sets the deadline associated with the listener.
|
391 |
|
|
// A zero time value disables the deadline.
|
392 |
|
|
func (l *UnixListener) SetDeadline(t time.Time) (err error) {
|
393 |
|
|
if l == nil || l.fd == nil {
|
394 |
|
|
return os.EINVAL
|
395 |
|
|
}
|
396 |
|
|
return setDeadline(l.fd, t)
|
397 |
|
|
}
|
398 |
|
|
|
399 |
|
|
// File returns a copy of the underlying os.File, set to blocking mode.
|
400 |
|
|
// It is the caller's responsibility to close f when finished.
|
401 |
|
|
// Closing c does not affect f, and closing f does not affect c.
|
402 |
|
|
func (l *UnixListener) File() (f *os.File, err error) { return l.fd.dup() }
|
403 |
|
|
|
404 |
|
|
// ListenUnixgram listens for incoming Unix datagram packets addressed to the
|
405 |
|
|
// local address laddr. The returned connection c's ReadFrom
|
406 |
|
|
// and WriteTo methods can be used to receive and send UDP
|
407 |
|
|
// packets with per-packet addressing. The network net must be "unixgram".
|
408 |
|
|
func ListenUnixgram(net string, laddr *UnixAddr) (*UDPConn, error) {
|
409 |
|
|
switch net {
|
410 |
|
|
case "unixgram":
|
411 |
|
|
default:
|
412 |
|
|
return nil, UnknownNetworkError(net)
|
413 |
|
|
}
|
414 |
|
|
if laddr == nil {
|
415 |
|
|
return nil, &OpError{"listen", net, nil, errMissingAddress}
|
416 |
|
|
}
|
417 |
|
|
fd, err := unixSocket(net, laddr, nil, "listen")
|
418 |
|
|
if err != nil {
|
419 |
|
|
return nil, err
|
420 |
|
|
}
|
421 |
|
|
return newUDPConn(fd), nil
|
422 |
|
|
}
|