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 aix darwin dragonfly freebsd hurd linux netbsd openbsd solaris
18 // Network file descriptor.
22 // immutable until Close
25 isConnected
bool // handshake completed or use of association with peer
31 func newFD(sysfd
, family
, sotype
int, net
string) (*netFD
, error
) {
35 IsStream
: sotype
== syscall
.SOCK_STREAM
,
36 ZeroReadIsEOF
: sotype
!= syscall
.SOCK_DGRAM
&& sotype
!= syscall
.SOCK_RAW
,
45 func (fd
*netFD
) init() error
{
46 return fd
.pfd
.Init(fd
.net
, true)
49 func (fd
*netFD
) setAddr(laddr
, raddr Addr
) {
52 runtime
.SetFinalizer(fd
, (*netFD
).Close
)
55 func (fd
*netFD
) name() string {
58 ls
= fd
.laddr
.String()
61 rs
= fd
.raddr
.String()
63 return fd
.net
+ ":" + ls
+ "->" + rs
66 func (fd
*netFD
) connect(ctx context
.Context
, la
, ra syscall
.Sockaddr
) (rsa syscall
.Sockaddr
, ret error
) {
67 // Do not need to call fd.writeLock here,
68 // because fd is not yet accessible to user,
69 // so no concurrent operations are possible.
70 switch err
:= connectFunc(fd
.pfd
.Sysfd
, ra
); err
{
71 case syscall
.EINPROGRESS
, syscall
.EALREADY
, syscall
.EINTR
:
72 case nil, syscall
.EISCONN
:
75 return nil, mapErr(ctx
.Err())
78 if err
:= fd
.pfd
.Init(fd
.net
, true); err
!= nil {
84 // On Solaris and illumos we can see EINVAL if the socket has
85 // already been accepted and closed by the server. Treat this
86 // as a successful connection--writes to the socket will see
87 // EOF. For details and a test case in C see
88 // https://golang.org/issue/6828.
89 if runtime
.GOOS
== "solaris" || runtime
.GOOS
== "illumos" {
94 return nil, os
.NewSyscallError("connect", err
)
96 if err
:= fd
.pfd
.Init(fd
.net
, true); err
!= nil {
99 if deadline
, hasDeadline
:= ctx
.Deadline(); hasDeadline
{
100 fd
.pfd
.SetWriteDeadline(deadline
)
101 defer fd
.pfd
.SetWriteDeadline(noDeadline
)
104 // Start the "interrupter" goroutine, if this context might be canceled.
105 // (The background context cannot)
107 // The interrupter goroutine waits for the context to be done and
108 // interrupts the dial (by altering the fd's write deadline, which
109 // wakes up waitWrite).
110 if ctx
!= context
.Background() {
111 // Wait for the interrupter goroutine to exit before returning
113 done
:= make(chan struct{})
114 interruptRes
:= make(chan error
)
117 if ctxErr
:= <-interruptRes
; ctxErr
!= nil && ret
== nil {
118 // The interrupter goroutine called SetWriteDeadline,
119 // but the connect code below had returned from
120 // waitWrite already and did a successful connect (ret
121 // == nil). Because we've now poisoned the connection
122 // by making it unwritable, don't return a successful
123 // dial. This was issue 16523.
125 fd
.Close() // prevent a leak
131 // Force the runtime's poller to immediately give up
132 // waiting for writability, unblocking waitWrite
134 fd
.pfd
.SetWriteDeadline(aLongTimeAgo
)
135 testHookCanceledDial()
136 interruptRes
<- ctx
.Err()
144 // Performing multiple connect system calls on a
145 // non-blocking socket under Unix variants does not
146 // necessarily result in earlier errors being
147 // returned. Instead, once runtime-integrated network
148 // poller tells us that the socket is ready, get the
149 // SO_ERROR socket option to see if the connection
150 // succeeded or failed. See issue 7474 for further
152 if err
:= fd
.pfd
.WaitWrite(); err
!= nil {
155 return nil, mapErr(ctx
.Err())
160 nerr
, err
:= getsockoptIntFunc(fd
.pfd
.Sysfd
, syscall
.SOL_SOCKET
, syscall
.SO_ERROR
)
162 return nil, os
.NewSyscallError("getsockopt", err
)
164 switch err
:= syscall
.Errno(nerr
); err
{
165 case syscall
.EINPROGRESS
, syscall
.EALREADY
, syscall
.EINTR
:
166 case syscall
.EISCONN
:
168 case syscall
.Errno(0):
169 // The runtime poller can wake us up spuriously;
170 // see issues 14548 and 19289. Check that we are
171 // really connected; if not, wait again.
172 if rsa
, err
:= syscall
.Getpeername(fd
.pfd
.Sysfd
); err
== nil {
176 return nil, os
.NewSyscallError("connect", err
)
178 runtime
.KeepAlive(fd
)
182 func (fd
*netFD
) Close() error
{
183 runtime
.SetFinalizer(fd
, nil)
184 return fd
.pfd
.Close()
187 func (fd
*netFD
) shutdown(how
int) error
{
188 err
:= fd
.pfd
.Shutdown(how
)
189 runtime
.KeepAlive(fd
)
190 return wrapSyscallError("shutdown", err
)
193 func (fd
*netFD
) closeRead() error
{
194 return fd
.shutdown(syscall
.SHUT_RD
)
197 func (fd
*netFD
) closeWrite() error
{
198 return fd
.shutdown(syscall
.SHUT_WR
)
201 func (fd
*netFD
) Read(p
[]byte) (n
int, err error
) {
202 n
, err
= fd
.pfd
.Read(p
)
203 runtime
.KeepAlive(fd
)
204 return n
, wrapSyscallError("read", err
)
207 func (fd
*netFD
) readFrom(p
[]byte) (n
int, sa syscall
.Sockaddr
, err error
) {
208 n
, sa
, err
= fd
.pfd
.ReadFrom(p
)
209 runtime
.KeepAlive(fd
)
210 return n
, sa
, wrapSyscallError("recvfrom", err
)
213 func (fd
*netFD
) readMsg(p
[]byte, oob
[]byte) (n
, oobn
, flags
int, sa syscall
.Sockaddr
, err error
) {
214 n
, oobn
, flags
, sa
, err
= fd
.pfd
.ReadMsg(p
, oob
)
215 runtime
.KeepAlive(fd
)
216 return n
, oobn
, flags
, sa
, wrapSyscallError("recvmsg", err
)
219 func (fd
*netFD
) Write(p
[]byte) (nn
int, err error
) {
220 nn
, err
= fd
.pfd
.Write(p
)
221 runtime
.KeepAlive(fd
)
222 return nn
, wrapSyscallError("write", err
)
225 func (fd
*netFD
) writeTo(p
[]byte, sa syscall
.Sockaddr
) (n
int, err error
) {
226 n
, err
= fd
.pfd
.WriteTo(p
, sa
)
227 runtime
.KeepAlive(fd
)
228 return n
, wrapSyscallError("sendto", err
)
231 func (fd
*netFD
) writeMsg(p
[]byte, oob
[]byte, sa syscall
.Sockaddr
) (n
int, oobn
int, err error
) {
232 n
, oobn
, err
= fd
.pfd
.WriteMsg(p
, oob
, sa
)
233 runtime
.KeepAlive(fd
)
234 return n
, oobn
, wrapSyscallError("sendmsg", err
)
237 func (fd
*netFD
) accept() (netfd
*netFD
, err error
) {
238 d
, rsa
, errcall
, err
:= fd
.pfd
.Accept()
241 err
= wrapSyscallError(errcall
, err
)
246 if netfd
, err
= newFD(d
, fd
.family
, fd
.sotype
, fd
.net
); err
!= nil {
250 if err
= netfd
.init(); err
!= nil {
254 lsa
, _
:= syscall
.Getsockname(netfd
.pfd
.Sysfd
)
255 netfd
.setAddr(netfd
.addrFunc()(lsa
), netfd
.addrFunc()(rsa
))
259 func (fd
*netFD
) dup() (f
*os
.File
, err error
) {
260 ns
, call
, err
:= fd
.pfd
.Dup()
263 err
= os
.NewSyscallError(call
, err
)
268 return os
.NewFile(uintptr(ns
), fd
.name()), nil
271 func (fd
*netFD
) SetDeadline(t time
.Time
) error
{
272 return fd
.pfd
.SetDeadline(t
)
275 func (fd
*netFD
) SetReadDeadline(t time
.Time
) error
{
276 return fd
.pfd
.SetReadDeadline(t
)
279 func (fd
*netFD
) SetWriteDeadline(t time
.Time
) error
{
280 return fd
.pfd
.SetWriteDeadline(t
)