Source file
src/net/unixsock.go
1
2
3
4
5 package net
6
7 import (
8 "context"
9 "os"
10 "sync"
11 "syscall"
12 "time"
13 )
14
15
16
17
18
19
20
21
22 type UnixAddr struct {
23 Name string
24 Net string
25 }
26
27
28
29 func (a *UnixAddr) Network() string {
30 return a.Net
31 }
32
33 func (a *UnixAddr) String() string {
34 if a == nil {
35 return "<nil>"
36 }
37 return a.Name
38 }
39
40 func (a *UnixAddr) isWildcard() bool {
41 return a == nil || a.Name == ""
42 }
43
44 func (a *UnixAddr) opAddr() Addr {
45 if a == nil {
46 return nil
47 }
48 return a
49 }
50
51
52
53
54
55
56
57 func ResolveUnixAddr(network, address string) (*UnixAddr, error) {
58 switch network {
59 case "unix", "unixgram", "unixpacket":
60 return &UnixAddr{Name: address, Net: network}, nil
61 default:
62 return nil, UnknownNetworkError(network)
63 }
64 }
65
66
67
68 type UnixConn struct {
69 conn
70 }
71
72
73
74 func (c *UnixConn) SyscallConn() (syscall.RawConn, error) {
75 if !c.ok() {
76 return nil, syscall.EINVAL
77 }
78 return newRawConn(c.fd), nil
79 }
80
81
82
83 func (c *UnixConn) CloseRead() error {
84 if !c.ok() {
85 return syscall.EINVAL
86 }
87 if err := c.fd.closeRead(); err != nil {
88 return &OpError{Op: "close", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
89 }
90 return nil
91 }
92
93
94
95 func (c *UnixConn) CloseWrite() error {
96 if !c.ok() {
97 return syscall.EINVAL
98 }
99 if err := c.fd.closeWrite(); err != nil {
100 return &OpError{Op: "close", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
101 }
102 return nil
103 }
104
105
106 func (c *UnixConn) ReadFromUnix(b []byte) (int, *UnixAddr, error) {
107 if !c.ok() {
108 return 0, nil, syscall.EINVAL
109 }
110 n, addr, err := c.readFrom(b)
111 if err != nil {
112 err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
113 }
114 return n, addr, err
115 }
116
117
118 func (c *UnixConn) ReadFrom(b []byte) (int, Addr, error) {
119 if !c.ok() {
120 return 0, nil, syscall.EINVAL
121 }
122 n, addr, err := c.readFrom(b)
123 if err != nil {
124 err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
125 }
126 if addr == nil {
127 return n, nil, err
128 }
129 return n, addr, err
130 }
131
132
133
134
135
136
137
138
139 func (c *UnixConn) ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *UnixAddr, err error) {
140 if !c.ok() {
141 return 0, 0, 0, nil, syscall.EINVAL
142 }
143 n, oobn, flags, addr, err = c.readMsg(b, oob)
144 if err != nil {
145 err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
146 }
147 return
148 }
149
150
151 func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (int, error) {
152 if !c.ok() {
153 return 0, syscall.EINVAL
154 }
155 n, err := c.writeTo(b, addr)
156 if err != nil {
157 err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
158 }
159 return n, err
160 }
161
162
163 func (c *UnixConn) WriteTo(b []byte, addr Addr) (int, error) {
164 if !c.ok() {
165 return 0, syscall.EINVAL
166 }
167 a, ok := addr.(*UnixAddr)
168 if !ok {
169 return 0, &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr, Err: syscall.EINVAL}
170 }
171 n, err := c.writeTo(b, a)
172 if err != nil {
173 err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: a.opAddr(), Err: err}
174 }
175 return n, err
176 }
177
178
179
180
181
182
183
184 func (c *UnixConn) WriteMsgUnix(b, oob []byte, addr *UnixAddr) (n, oobn int, err error) {
185 if !c.ok() {
186 return 0, 0, syscall.EINVAL
187 }
188 n, oobn, err = c.writeMsg(b, oob, addr)
189 if err != nil {
190 err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
191 }
192 return
193 }
194
195 func newUnixConn(fd *netFD) *UnixConn { return &UnixConn{conn{fd}} }
196
197
198
199
200
201
202
203 func DialUnix(network string, laddr, raddr *UnixAddr) (*UnixConn, error) {
204 switch network {
205 case "unix", "unixgram", "unixpacket":
206 default:
207 return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: UnknownNetworkError(network)}
208 }
209 sd := &sysDialer{network: network, address: raddr.String()}
210 c, err := sd.dialUnix(context.Background(), laddr, raddr)
211 if err != nil {
212 return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err}
213 }
214 return c, nil
215 }
216
217
218
219
220 type UnixListener struct {
221 fd *netFD
222 path string
223 unlink bool
224 unlinkOnce sync.Once
225 }
226
227 func (ln *UnixListener) ok() bool { return ln != nil && ln.fd != nil }
228
229
230
231
232
233
234 func (l *UnixListener) SyscallConn() (syscall.RawConn, error) {
235 if !l.ok() {
236 return nil, syscall.EINVAL
237 }
238 return newRawListener(l.fd), nil
239 }
240
241
242
243 func (l *UnixListener) AcceptUnix() (*UnixConn, error) {
244 if !l.ok() {
245 return nil, syscall.EINVAL
246 }
247 c, err := l.accept()
248 if err != nil {
249 return nil, &OpError{Op: "accept", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
250 }
251 return c, nil
252 }
253
254
255
256 func (l *UnixListener) Accept() (Conn, error) {
257 if !l.ok() {
258 return nil, syscall.EINVAL
259 }
260 c, err := l.accept()
261 if err != nil {
262 return nil, &OpError{Op: "accept", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
263 }
264 return c, nil
265 }
266
267
268
269 func (l *UnixListener) Close() error {
270 if !l.ok() {
271 return syscall.EINVAL
272 }
273 if err := l.close(); err != nil {
274 return &OpError{Op: "close", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
275 }
276 return nil
277 }
278
279
280
281
282 func (l *UnixListener) Addr() Addr { return l.fd.laddr }
283
284
285
286 func (l *UnixListener) SetDeadline(t time.Time) error {
287 if !l.ok() {
288 return syscall.EINVAL
289 }
290 return l.fd.SetDeadline(t)
291 }
292
293
294
295
296
297
298
299
300 func (l *UnixListener) File() (f *os.File, err error) {
301 if !l.ok() {
302 return nil, syscall.EINVAL
303 }
304 f, err = l.file()
305 if err != nil {
306 err = &OpError{Op: "file", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
307 }
308 return
309 }
310
311
312
313
314 func ListenUnix(network string, laddr *UnixAddr) (*UnixListener, error) {
315 switch network {
316 case "unix", "unixpacket":
317 default:
318 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(network)}
319 }
320 if laddr == nil {
321 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: errMissingAddress}
322 }
323 sl := &sysListener{network: network, address: laddr.String()}
324 ln, err := sl.listenUnix(context.Background(), laddr)
325 if err != nil {
326 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: err}
327 }
328 return ln, nil
329 }
330
331
332
333
334 func ListenUnixgram(network string, laddr *UnixAddr) (*UnixConn, error) {
335 switch network {
336 case "unixgram":
337 default:
338 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(network)}
339 }
340 if laddr == nil {
341 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: nil, Err: errMissingAddress}
342 }
343 sl := &sysListener{network: network, address: laddr.String()}
344 c, err := sl.listenUnixgram(context.Background(), laddr)
345 if err != nil {
346 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: err}
347 }
348 return c, nil
349 }
350
View as plain text