-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunix_sock_linux.go
169 lines (147 loc) · 3.83 KB
/
unix_sock_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// ©Hayabusa Cloud Co., Ltd. 2022. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//go:build linux
package sox
import (
"errors"
"golang.org/x/sys/unix"
"time"
)
type UnixSocket struct {
*socket
}
func newUnixSocket(sa Sockaddr) (*UnixSocket, error) {
fd, err := 0, error(nil)
if _, ok := sa.(*SockaddrUnix); ok {
fd, err = unix.Socket(unix.AF_UNIX, unix.SOCK_SEQPACKET|unix.SOCK_NONBLOCK|unix.SOCK_CLOEXEC, 0)
if err != nil {
return nil, err
}
} else {
return nil, UnknownNetworkError("unexpected family")
}
err = unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_PASSSEC, 1)
if err != nil {
return nil, errFromUnixErrno(err)
}
so := &UnixSocket{socket: newSocket(NetworkUnix, fd, sa)}
return so, nil
}
func newUnixSocketPair() (so [2]*UnixSocket, err error) {
fd, err := unix.Socketpair(unix.AF_UNIX, unix.SOCK_SEQPACKET|unix.SOCK_NONBLOCK|unix.SOCK_CLOEXEC, 0)
if err != nil {
return [2]*UnixSocket{}, errFromUnixErrno(err)
}
so[0] = &UnixSocket{socket: newSocket(NetworkUnix, fd[0], &SockaddrUnix{})}
so[1] = &UnixSocket{socket: newSocket(NetworkUnix, fd[1], &SockaddrUnix{})}
return so, nil
}
func (so *UnixSocket) Protocol() UnderlyingProtocol {
return UnderlyingProtocolSeqPacket
}
func (so *UnixSocket) Shutdown(how int) error {
err := unix.Shutdown(so.fd, how)
if err != nil {
return errFromUnixErrno(err)
}
return nil
}
type UnixConn struct {
*UnixSocket
laddr *UnixAddr
raddr *UnixAddr
}
func NewUnixConn(localAddr Addr, remoteSock *UnixSocket) (Conn, error) {
if localAddr == nil {
localAddr = &UnixAddr{}
}
unixAddr, ok := localAddr.(*UnixAddr)
if !ok {
return nil, &AddrError{Err: "unexpected address type", Addr: localAddr.String()}
}
remoteAddr := unixAddrFromSockaddr(remoteSock.sa, UnderlyingProtocolSeqPacket)
return &UnixConn{UnixSocket: remoteSock, laddr: unixAddr, raddr: remoteAddr}, nil
}
func (conn *UnixConn) LocalAddr() Addr {
return conn.laddr
}
func (conn *UnixConn) RemoteAddr() Addr {
return conn.raddr
}
func (conn *UnixConn) SetDeadline(t time.Time) error {
return nil
}
func (conn *UnixConn) SetReadDeadline(t time.Time) error {
return nil
}
func (conn *UnixConn) SetWriteDeadline(t time.Time) error {
return nil
}
type UnixListener struct {
*UnixSocket
laddr *UnixAddr
}
func (l *UnixListener) Accept() (Conn, error) {
nfd, sa, err := acceptWait(l.fd)
if err != nil {
return nil, err
}
so := &UnixSocket{socket: newSocket(NetworkUnix, nfd, sa)}
conn, err := NewUnixConn(l.Addr(), so)
if err != nil {
return nil, err
}
return conn, err
}
func (l *UnixListener) Close() error {
sa := l.sa.(*SockaddrUnix)
if len(sa.Name) > 0 {
_ = unix.Unlink(sa.Name)
}
return l.UnixSocket.Close()
}
func (l *UnixListener) Addr() Addr {
if l.laddr != nil {
return l.laddr
}
return unixAddrFromSockaddr(l.sa, UnderlyingProtocolSeqPacket)
}
func ListenUnix(laddr *UnixAddr) (*UnixListener, error) {
if laddr == nil {
return nil, InvalidAddrError("nil local address")
}
so, err := newUnixSocket(unixAddrToSockaddr(laddr))
if err != nil {
return nil, err
}
err = unix.Bind(so.fd, unixAddrToSockaddr(laddr))
if err != nil {
return nil, errFromUnixErrno(err)
}
err = unix.Listen(so.fd, defaultBacklog)
if err != nil {
return nil, errFromUnixErrno(err)
}
lis := &UnixListener{UnixSocket: so, laddr: laddr}
return lis, nil
}
func DialUnix(laddr *UnixAddr, raddr *UnixAddr) (*UnixConn, error) {
if raddr == nil {
return nil, &OpError{Op: "dial", Net: "unix", Source: laddr, Addr: nil, Err: errors.New("missing address")}
}
so, err := newUnixSocket(unixAddrToSockaddr(laddr))
if err != nil {
return nil, err
}
err = connectWait(so.fd, unixAddrToSockaddr(raddr))
if err != nil {
return nil, err
}
conn := &UnixConn{
UnixSocket: so,
laddr: laddr,
raddr: raddr,
}
return conn, nil
}