github.com/JimmyHuang454/JLS-go@v0.0.0-20230831150107-90d536585ba0/internal/poll/fd_windows.go (about)

     1  // Copyright 2017 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  package poll
     6  
     7  import (
     8  	"errors"
     9  	"internal/race"
    10  	"internal/syscall/windows"
    11  	"io"
    12  	"sync"
    13  	"syscall"
    14  	"unicode/utf16"
    15  	"unicode/utf8"
    16  	"unsafe"
    17  )
    18  
    19  var (
    20  	initErr error
    21  	ioSync  uint64
    22  )
    23  
    24  // This package uses the SetFileCompletionNotificationModes Windows
    25  // API to skip calling GetQueuedCompletionStatus if an IO operation
    26  // completes synchronously. There is a known bug where
    27  // SetFileCompletionNotificationModes crashes on some systems (see
    28  // https://support.microsoft.com/kb/2568167 for details).
    29  
    30  var useSetFileCompletionNotificationModes bool // determines is SetFileCompletionNotificationModes is present and safe to use
    31  
    32  // checkSetFileCompletionNotificationModes verifies that
    33  // SetFileCompletionNotificationModes Windows API is present
    34  // on the system and is safe to use.
    35  // See https://support.microsoft.com/kb/2568167 for details.
    36  func checkSetFileCompletionNotificationModes() {
    37  	err := syscall.LoadSetFileCompletionNotificationModes()
    38  	if err != nil {
    39  		return
    40  	}
    41  	protos := [2]int32{syscall.IPPROTO_TCP, 0}
    42  	var buf [32]syscall.WSAProtocolInfo
    43  	len := uint32(unsafe.Sizeof(buf))
    44  	n, err := syscall.WSAEnumProtocols(&protos[0], &buf[0], &len)
    45  	if err != nil {
    46  		return
    47  	}
    48  	for i := int32(0); i < n; i++ {
    49  		if buf[i].ServiceFlags1&syscall.XP1_IFS_HANDLES == 0 {
    50  			return
    51  		}
    52  	}
    53  	useSetFileCompletionNotificationModes = true
    54  }
    55  
    56  func init() {
    57  	var d syscall.WSAData
    58  	e := syscall.WSAStartup(uint32(0x202), &d)
    59  	if e != nil {
    60  		initErr = e
    61  	}
    62  	checkSetFileCompletionNotificationModes()
    63  }
    64  
    65  // operation contains superset of data necessary to perform all async IO.
    66  type operation struct {
    67  	// Used by IOCP interface, it must be first field
    68  	// of the struct, as our code rely on it.
    69  	o syscall.Overlapped
    70  
    71  	// fields used by runtime.netpoll
    72  	runtimeCtx uintptr
    73  	mode       int32
    74  	errno      int32
    75  	qty        uint32
    76  
    77  	// fields used only by net package
    78  	fd     *FD
    79  	buf    syscall.WSABuf
    80  	msg    windows.WSAMsg
    81  	sa     syscall.Sockaddr
    82  	rsa    *syscall.RawSockaddrAny
    83  	rsan   int32
    84  	handle syscall.Handle
    85  	flags  uint32
    86  	bufs   []syscall.WSABuf
    87  }
    88  
    89  func (o *operation) InitBuf(buf []byte) {
    90  	o.buf.Len = uint32(len(buf))
    91  	o.buf.Buf = nil
    92  	if len(buf) != 0 {
    93  		o.buf.Buf = &buf[0]
    94  	}
    95  }
    96  
    97  func (o *operation) InitBufs(buf *[][]byte) {
    98  	if o.bufs == nil {
    99  		o.bufs = make([]syscall.WSABuf, 0, len(*buf))
   100  	} else {
   101  		o.bufs = o.bufs[:0]
   102  	}
   103  	for _, b := range *buf {
   104  		if len(b) == 0 {
   105  			o.bufs = append(o.bufs, syscall.WSABuf{})
   106  			continue
   107  		}
   108  		for len(b) > maxRW {
   109  			o.bufs = append(o.bufs, syscall.WSABuf{Len: maxRW, Buf: &b[0]})
   110  			b = b[maxRW:]
   111  		}
   112  		if len(b) > 0 {
   113  			o.bufs = append(o.bufs, syscall.WSABuf{Len: uint32(len(b)), Buf: &b[0]})
   114  		}
   115  	}
   116  }
   117  
   118  // ClearBufs clears all pointers to Buffers parameter captured
   119  // by InitBufs, so it can be released by garbage collector.
   120  func (o *operation) ClearBufs() {
   121  	for i := range o.bufs {
   122  		o.bufs[i].Buf = nil
   123  	}
   124  	o.bufs = o.bufs[:0]
   125  }
   126  
   127  func (o *operation) InitMsg(p []byte, oob []byte) {
   128  	o.InitBuf(p)
   129  	o.msg.Buffers = &o.buf
   130  	o.msg.BufferCount = 1
   131  
   132  	o.msg.Name = nil
   133  	o.msg.Namelen = 0
   134  
   135  	o.msg.Flags = 0
   136  	o.msg.Control.Len = uint32(len(oob))
   137  	o.msg.Control.Buf = nil
   138  	if len(oob) != 0 {
   139  		o.msg.Control.Buf = &oob[0]
   140  	}
   141  }
   142  
   143  // execIO executes a single IO operation o. It submits and cancels
   144  // IO in the current thread for systems where Windows CancelIoEx API
   145  // is available. Alternatively, it passes the request onto
   146  // runtime netpoll and waits for completion or cancels request.
   147  func execIO(o *operation, submit func(o *operation) error) (int, error) {
   148  	if o.fd.pd.runtimeCtx == 0 {
   149  		return 0, errors.New("internal error: polling on unsupported descriptor type")
   150  	}
   151  
   152  	fd := o.fd
   153  	// Notify runtime netpoll about starting IO.
   154  	err := fd.pd.prepare(int(o.mode), fd.isFile)
   155  	if err != nil {
   156  		return 0, err
   157  	}
   158  	// Start IO.
   159  	err = submit(o)
   160  	switch err {
   161  	case nil:
   162  		// IO completed immediately
   163  		if o.fd.skipSyncNotif {
   164  			// No completion message will follow, so return immediately.
   165  			return int(o.qty), nil
   166  		}
   167  		// Need to get our completion message anyway.
   168  	case syscall.ERROR_IO_PENDING:
   169  		// IO started, and we have to wait for its completion.
   170  		err = nil
   171  	default:
   172  		return 0, err
   173  	}
   174  	// Wait for our request to complete.
   175  	err = fd.pd.wait(int(o.mode), fd.isFile)
   176  	if err == nil {
   177  		// All is good. Extract our IO results and return.
   178  		if o.errno != 0 {
   179  			err = syscall.Errno(o.errno)
   180  			// More data available. Return back the size of received data.
   181  			if err == syscall.ERROR_MORE_DATA || err == windows.WSAEMSGSIZE {
   182  				return int(o.qty), err
   183  			}
   184  			return 0, err
   185  		}
   186  		return int(o.qty), nil
   187  	}
   188  	// IO is interrupted by "close" or "timeout"
   189  	netpollErr := err
   190  	switch netpollErr {
   191  	case ErrNetClosing, ErrFileClosing, ErrDeadlineExceeded:
   192  		// will deal with those.
   193  	default:
   194  		panic("unexpected runtime.netpoll error: " + netpollErr.Error())
   195  	}
   196  	// Cancel our request.
   197  	err = syscall.CancelIoEx(fd.Sysfd, &o.o)
   198  	// Assuming ERROR_NOT_FOUND is returned, if IO is completed.
   199  	if err != nil && err != syscall.ERROR_NOT_FOUND {
   200  		// TODO(brainman): maybe do something else, but panic.
   201  		panic(err)
   202  	}
   203  	// Wait for cancellation to complete.
   204  	fd.pd.waitCanceled(int(o.mode))
   205  	if o.errno != 0 {
   206  		err = syscall.Errno(o.errno)
   207  		if err == syscall.ERROR_OPERATION_ABORTED { // IO Canceled
   208  			err = netpollErr
   209  		}
   210  		return 0, err
   211  	}
   212  	// We issued a cancellation request. But, it seems, IO operation succeeded
   213  	// before the cancellation request run. We need to treat the IO operation as
   214  	// succeeded (the bytes are actually sent/recv from network).
   215  	return int(o.qty), nil
   216  }
   217  
   218  // FD is a file descriptor. The net and os packages embed this type in
   219  // a larger type representing a network connection or OS file.
   220  type FD struct {
   221  	// Lock sysfd and serialize access to Read and Write methods.
   222  	fdmu fdMutex
   223  
   224  	// System file descriptor. Immutable until Close.
   225  	Sysfd syscall.Handle
   226  
   227  	// Read operation.
   228  	rop operation
   229  	// Write operation.
   230  	wop operation
   231  
   232  	// I/O poller.
   233  	pd pollDesc
   234  
   235  	// Used to implement pread/pwrite.
   236  	l sync.Mutex
   237  
   238  	// For console I/O.
   239  	lastbits       []byte   // first few bytes of the last incomplete rune in last write
   240  	readuint16     []uint16 // buffer to hold uint16s obtained with ReadConsole
   241  	readbyte       []byte   // buffer to hold decoding of readuint16 from utf16 to utf8
   242  	readbyteOffset int      // readbyte[readOffset:] is yet to be consumed with file.Read
   243  
   244  	// Semaphore signaled when file is closed.
   245  	csema uint32
   246  
   247  	skipSyncNotif bool
   248  
   249  	// Whether this is a streaming descriptor, as opposed to a
   250  	// packet-based descriptor like a UDP socket.
   251  	IsStream bool
   252  
   253  	// Whether a zero byte read indicates EOF. This is false for a
   254  	// message based socket connection.
   255  	ZeroReadIsEOF bool
   256  
   257  	// Whether this is a file rather than a network socket.
   258  	isFile bool
   259  
   260  	// The kind of this file.
   261  	kind fileKind
   262  }
   263  
   264  // fileKind describes the kind of file.
   265  type fileKind byte
   266  
   267  const (
   268  	kindNet fileKind = iota
   269  	kindFile
   270  	kindConsole
   271  	kindPipe
   272  )
   273  
   274  // logInitFD is set by tests to enable file descriptor initialization logging.
   275  var logInitFD func(net string, fd *FD, err error)
   276  
   277  // Init initializes the FD. The Sysfd field should already be set.
   278  // This can be called multiple times on a single FD.
   279  // The net argument is a network name from the net package (e.g., "tcp"),
   280  // or "file" or "console" or "dir".
   281  // Set pollable to true if fd should be managed by runtime netpoll.
   282  func (fd *FD) Init(net string, pollable bool) (string, error) {
   283  	if initErr != nil {
   284  		return "", initErr
   285  	}
   286  
   287  	switch net {
   288  	case "file", "dir":
   289  		fd.kind = kindFile
   290  	case "console":
   291  		fd.kind = kindConsole
   292  	case "pipe":
   293  		fd.kind = kindPipe
   294  	case "tcp", "tcp4", "tcp6",
   295  		"udp", "udp4", "udp6",
   296  		"ip", "ip4", "ip6",
   297  		"unix", "unixgram", "unixpacket":
   298  		fd.kind = kindNet
   299  	default:
   300  		return "", errors.New("internal error: unknown network type " + net)
   301  	}
   302  	fd.isFile = fd.kind != kindNet
   303  
   304  	var err error
   305  	if pollable {
   306  		// Only call init for a network socket.
   307  		// This means that we don't add files to the runtime poller.
   308  		// Adding files to the runtime poller can confuse matters
   309  		// if the user is doing their own overlapped I/O.
   310  		// See issue #21172.
   311  		//
   312  		// In general the code below avoids calling the execIO
   313  		// function for non-network sockets. If some method does
   314  		// somehow call execIO, then execIO, and therefore the
   315  		// calling method, will return an error, because
   316  		// fd.pd.runtimeCtx will be 0.
   317  		err = fd.pd.init(fd)
   318  	}
   319  	if logInitFD != nil {
   320  		logInitFD(net, fd, err)
   321  	}
   322  	if err != nil {
   323  		return "", err
   324  	}
   325  	if pollable && useSetFileCompletionNotificationModes {
   326  		// We do not use events, so we can skip them always.
   327  		flags := uint8(syscall.FILE_SKIP_SET_EVENT_ON_HANDLE)
   328  		// It's not safe to skip completion notifications for UDP:
   329  		// https://docs.microsoft.com/en-us/archive/blogs/winserverperformance/designing-applications-for-high-performance-part-iii
   330  		if net == "tcp" {
   331  			flags |= syscall.FILE_SKIP_COMPLETION_PORT_ON_SUCCESS
   332  		}
   333  		err := syscall.SetFileCompletionNotificationModes(fd.Sysfd, flags)
   334  		if err == nil && flags&syscall.FILE_SKIP_COMPLETION_PORT_ON_SUCCESS != 0 {
   335  			fd.skipSyncNotif = true
   336  		}
   337  	}
   338  	// Disable SIO_UDP_CONNRESET behavior.
   339  	// http://support.microsoft.com/kb/263823
   340  	switch net {
   341  	case "udp", "udp4", "udp6":
   342  		ret := uint32(0)
   343  		flag := uint32(0)
   344  		size := uint32(unsafe.Sizeof(flag))
   345  		err := syscall.WSAIoctl(fd.Sysfd, syscall.SIO_UDP_CONNRESET, (*byte)(unsafe.Pointer(&flag)), size, nil, 0, &ret, nil, 0)
   346  		if err != nil {
   347  			return "wsaioctl", err
   348  		}
   349  	}
   350  	fd.rop.mode = 'r'
   351  	fd.wop.mode = 'w'
   352  	fd.rop.fd = fd
   353  	fd.wop.fd = fd
   354  	fd.rop.runtimeCtx = fd.pd.runtimeCtx
   355  	fd.wop.runtimeCtx = fd.pd.runtimeCtx
   356  	return "", nil
   357  }
   358  
   359  func (fd *FD) destroy() error {
   360  	if fd.Sysfd == syscall.InvalidHandle {
   361  		return syscall.EINVAL
   362  	}
   363  	// Poller may want to unregister fd in readiness notification mechanism,
   364  	// so this must be executed before fd.CloseFunc.
   365  	fd.pd.close()
   366  	var err error
   367  	switch fd.kind {
   368  	case kindNet:
   369  		// The net package uses the CloseFunc variable for testing.
   370  		err = CloseFunc(fd.Sysfd)
   371  	default:
   372  		err = syscall.CloseHandle(fd.Sysfd)
   373  	}
   374  	fd.Sysfd = syscall.InvalidHandle
   375  	runtime_Semrelease(&fd.csema)
   376  	return err
   377  }
   378  
   379  // Close closes the FD. The underlying file descriptor is closed by
   380  // the destroy method when there are no remaining references.
   381  func (fd *FD) Close() error {
   382  	if !fd.fdmu.increfAndClose() {
   383  		return errClosing(fd.isFile)
   384  	}
   385  	if fd.kind == kindPipe {
   386  		syscall.CancelIoEx(fd.Sysfd, nil)
   387  	}
   388  	// unblock pending reader and writer
   389  	fd.pd.evict()
   390  	err := fd.decref()
   391  	// Wait until the descriptor is closed. If this was the only
   392  	// reference, it is already closed.
   393  	runtime_Semacquire(&fd.csema)
   394  	return err
   395  }
   396  
   397  // Windows ReadFile and WSARecv use DWORD (uint32) parameter to pass buffer length.
   398  // This prevents us reading blocks larger than 4GB.
   399  // See golang.org/issue/26923.
   400  const maxRW = 1 << 30 // 1GB is large enough and keeps subsequent reads aligned
   401  
   402  // Read implements io.Reader.
   403  func (fd *FD) Read(buf []byte) (int, error) {
   404  	if err := fd.readLock(); err != nil {
   405  		return 0, err
   406  	}
   407  	defer fd.readUnlock()
   408  
   409  	if len(buf) > maxRW {
   410  		buf = buf[:maxRW]
   411  	}
   412  
   413  	var n int
   414  	var err error
   415  	if fd.isFile {
   416  		fd.l.Lock()
   417  		defer fd.l.Unlock()
   418  		switch fd.kind {
   419  		case kindConsole:
   420  			n, err = fd.readConsole(buf)
   421  		default:
   422  			n, err = syscall.Read(fd.Sysfd, buf)
   423  			if fd.kind == kindPipe && err == syscall.ERROR_OPERATION_ABORTED {
   424  				// Close uses CancelIoEx to interrupt concurrent I/O for pipes.
   425  				// If the fd is a pipe and the Read was interrupted by CancelIoEx,
   426  				// we assume it is interrupted by Close.
   427  				err = ErrFileClosing
   428  			}
   429  		}
   430  		if err != nil {
   431  			n = 0
   432  		}
   433  	} else {
   434  		o := &fd.rop
   435  		o.InitBuf(buf)
   436  		n, err = execIO(o, func(o *operation) error {
   437  			return syscall.WSARecv(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, &o.o, nil)
   438  		})
   439  		if race.Enabled {
   440  			race.Acquire(unsafe.Pointer(&ioSync))
   441  		}
   442  	}
   443  	if len(buf) != 0 {
   444  		err = fd.eofError(n, err)
   445  	}
   446  	return n, err
   447  }
   448  
   449  var ReadConsole = syscall.ReadConsole // changed for testing
   450  
   451  // readConsole reads utf16 characters from console File,
   452  // encodes them into utf8 and stores them in buffer b.
   453  // It returns the number of utf8 bytes read and an error, if any.
   454  func (fd *FD) readConsole(b []byte) (int, error) {
   455  	if len(b) == 0 {
   456  		return 0, nil
   457  	}
   458  
   459  	if fd.readuint16 == nil {
   460  		// Note: syscall.ReadConsole fails for very large buffers.
   461  		// The limit is somewhere around (but not exactly) 16384.
   462  		// Stay well below.
   463  		fd.readuint16 = make([]uint16, 0, 10000)
   464  		fd.readbyte = make([]byte, 0, 4*cap(fd.readuint16))
   465  	}
   466  
   467  	for fd.readbyteOffset >= len(fd.readbyte) {
   468  		n := cap(fd.readuint16) - len(fd.readuint16)
   469  		if n > len(b) {
   470  			n = len(b)
   471  		}
   472  		var nw uint32
   473  		err := ReadConsole(fd.Sysfd, &fd.readuint16[:len(fd.readuint16)+1][len(fd.readuint16)], uint32(n), &nw, nil)
   474  		if err != nil {
   475  			return 0, err
   476  		}
   477  		uint16s := fd.readuint16[:len(fd.readuint16)+int(nw)]
   478  		fd.readuint16 = fd.readuint16[:0]
   479  		buf := fd.readbyte[:0]
   480  		for i := 0; i < len(uint16s); i++ {
   481  			r := rune(uint16s[i])
   482  			if utf16.IsSurrogate(r) {
   483  				if i+1 == len(uint16s) {
   484  					if nw > 0 {
   485  						// Save half surrogate pair for next time.
   486  						fd.readuint16 = fd.readuint16[:1]
   487  						fd.readuint16[0] = uint16(r)
   488  						break
   489  					}
   490  					r = utf8.RuneError
   491  				} else {
   492  					r = utf16.DecodeRune(r, rune(uint16s[i+1]))
   493  					if r != utf8.RuneError {
   494  						i++
   495  					}
   496  				}
   497  			}
   498  			buf = utf8.AppendRune(buf, r)
   499  		}
   500  		fd.readbyte = buf
   501  		fd.readbyteOffset = 0
   502  		if nw == 0 {
   503  			break
   504  		}
   505  	}
   506  
   507  	src := fd.readbyte[fd.readbyteOffset:]
   508  	var i int
   509  	for i = 0; i < len(src) && i < len(b); i++ {
   510  		x := src[i]
   511  		if x == 0x1A { // Ctrl-Z
   512  			if i == 0 {
   513  				fd.readbyteOffset++
   514  			}
   515  			break
   516  		}
   517  		b[i] = x
   518  	}
   519  	fd.readbyteOffset += i
   520  	return i, nil
   521  }
   522  
   523  // Pread emulates the Unix pread system call.
   524  func (fd *FD) Pread(b []byte, off int64) (int, error) {
   525  	// Call incref, not readLock, because since pread specifies the
   526  	// offset it is independent from other reads.
   527  	if err := fd.incref(); err != nil {
   528  		return 0, err
   529  	}
   530  	defer fd.decref()
   531  
   532  	if len(b) > maxRW {
   533  		b = b[:maxRW]
   534  	}
   535  
   536  	fd.l.Lock()
   537  	defer fd.l.Unlock()
   538  	curoffset, e := syscall.Seek(fd.Sysfd, 0, io.SeekCurrent)
   539  	if e != nil {
   540  		return 0, e
   541  	}
   542  	defer syscall.Seek(fd.Sysfd, curoffset, io.SeekStart)
   543  	o := syscall.Overlapped{
   544  		OffsetHigh: uint32(off >> 32),
   545  		Offset:     uint32(off),
   546  	}
   547  	var done uint32
   548  	e = syscall.ReadFile(fd.Sysfd, b, &done, &o)
   549  	if e != nil {
   550  		done = 0
   551  		if e == syscall.ERROR_HANDLE_EOF {
   552  			e = io.EOF
   553  		}
   554  	}
   555  	if len(b) != 0 {
   556  		e = fd.eofError(int(done), e)
   557  	}
   558  	return int(done), e
   559  }
   560  
   561  // ReadFrom wraps the recvfrom network call.
   562  func (fd *FD) ReadFrom(buf []byte) (int, syscall.Sockaddr, error) {
   563  	if len(buf) == 0 {
   564  		return 0, nil, nil
   565  	}
   566  	if len(buf) > maxRW {
   567  		buf = buf[:maxRW]
   568  	}
   569  	if err := fd.readLock(); err != nil {
   570  		return 0, nil, err
   571  	}
   572  	defer fd.readUnlock()
   573  	o := &fd.rop
   574  	o.InitBuf(buf)
   575  	n, err := execIO(o, func(o *operation) error {
   576  		if o.rsa == nil {
   577  			o.rsa = new(syscall.RawSockaddrAny)
   578  		}
   579  		o.rsan = int32(unsafe.Sizeof(*o.rsa))
   580  		return syscall.WSARecvFrom(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, o.rsa, &o.rsan, &o.o, nil)
   581  	})
   582  	err = fd.eofError(n, err)
   583  	if err != nil {
   584  		return n, nil, err
   585  	}
   586  	sa, _ := o.rsa.Sockaddr()
   587  	return n, sa, nil
   588  }
   589  
   590  // ReadFromInet4 wraps the recvfrom network call for IPv4.
   591  func (fd *FD) ReadFromInet4(buf []byte, sa4 *syscall.SockaddrInet4) (int, error) {
   592  	if len(buf) == 0 {
   593  		return 0, nil
   594  	}
   595  	if len(buf) > maxRW {
   596  		buf = buf[:maxRW]
   597  	}
   598  	if err := fd.readLock(); err != nil {
   599  		return 0, err
   600  	}
   601  	defer fd.readUnlock()
   602  	o := &fd.rop
   603  	o.InitBuf(buf)
   604  	n, err := execIO(o, func(o *operation) error {
   605  		if o.rsa == nil {
   606  			o.rsa = new(syscall.RawSockaddrAny)
   607  		}
   608  		o.rsan = int32(unsafe.Sizeof(*o.rsa))
   609  		return syscall.WSARecvFrom(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, o.rsa, &o.rsan, &o.o, nil)
   610  	})
   611  	err = fd.eofError(n, err)
   612  	if err != nil {
   613  		return n, err
   614  	}
   615  	rawToSockaddrInet4(o.rsa, sa4)
   616  	return n, err
   617  }
   618  
   619  // ReadFromInet6 wraps the recvfrom network call for IPv6.
   620  func (fd *FD) ReadFromInet6(buf []byte, sa6 *syscall.SockaddrInet6) (int, error) {
   621  	if len(buf) == 0 {
   622  		return 0, nil
   623  	}
   624  	if len(buf) > maxRW {
   625  		buf = buf[:maxRW]
   626  	}
   627  	if err := fd.readLock(); err != nil {
   628  		return 0, err
   629  	}
   630  	defer fd.readUnlock()
   631  	o := &fd.rop
   632  	o.InitBuf(buf)
   633  	n, err := execIO(o, func(o *operation) error {
   634  		if o.rsa == nil {
   635  			o.rsa = new(syscall.RawSockaddrAny)
   636  		}
   637  		o.rsan = int32(unsafe.Sizeof(*o.rsa))
   638  		return syscall.WSARecvFrom(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, o.rsa, &o.rsan, &o.o, nil)
   639  	})
   640  	err = fd.eofError(n, err)
   641  	if err != nil {
   642  		return n, err
   643  	}
   644  	rawToSockaddrInet6(o.rsa, sa6)
   645  	return n, err
   646  }
   647  
   648  // Write implements io.Writer.
   649  func (fd *FD) Write(buf []byte) (int, error) {
   650  	if err := fd.writeLock(); err != nil {
   651  		return 0, err
   652  	}
   653  	defer fd.writeUnlock()
   654  	if fd.isFile {
   655  		fd.l.Lock()
   656  		defer fd.l.Unlock()
   657  	}
   658  
   659  	ntotal := 0
   660  	for len(buf) > 0 {
   661  		b := buf
   662  		if len(b) > maxRW {
   663  			b = b[:maxRW]
   664  		}
   665  		var n int
   666  		var err error
   667  		if fd.isFile {
   668  			switch fd.kind {
   669  			case kindConsole:
   670  				n, err = fd.writeConsole(b)
   671  			default:
   672  				n, err = syscall.Write(fd.Sysfd, b)
   673  				if fd.kind == kindPipe && err == syscall.ERROR_OPERATION_ABORTED {
   674  					// Close uses CancelIoEx to interrupt concurrent I/O for pipes.
   675  					// If the fd is a pipe and the Write was interrupted by CancelIoEx,
   676  					// we assume it is interrupted by Close.
   677  					err = ErrFileClosing
   678  				}
   679  			}
   680  			if err != nil {
   681  				n = 0
   682  			}
   683  		} else {
   684  			if race.Enabled {
   685  				race.ReleaseMerge(unsafe.Pointer(&ioSync))
   686  			}
   687  			o := &fd.wop
   688  			o.InitBuf(b)
   689  			n, err = execIO(o, func(o *operation) error {
   690  				return syscall.WSASend(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, &o.o, nil)
   691  			})
   692  		}
   693  		ntotal += n
   694  		if err != nil {
   695  			return ntotal, err
   696  		}
   697  		buf = buf[n:]
   698  	}
   699  	return ntotal, nil
   700  }
   701  
   702  // writeConsole writes len(b) bytes to the console File.
   703  // It returns the number of bytes written and an error, if any.
   704  func (fd *FD) writeConsole(b []byte) (int, error) {
   705  	n := len(b)
   706  	runes := make([]rune, 0, 256)
   707  	if len(fd.lastbits) > 0 {
   708  		b = append(fd.lastbits, b...)
   709  		fd.lastbits = nil
   710  
   711  	}
   712  	for len(b) >= utf8.UTFMax || utf8.FullRune(b) {
   713  		r, l := utf8.DecodeRune(b)
   714  		runes = append(runes, r)
   715  		b = b[l:]
   716  	}
   717  	if len(b) > 0 {
   718  		fd.lastbits = make([]byte, len(b))
   719  		copy(fd.lastbits, b)
   720  	}
   721  	// syscall.WriteConsole seems to fail, if given large buffer.
   722  	// So limit the buffer to 16000 characters. This number was
   723  	// discovered by experimenting with syscall.WriteConsole.
   724  	const maxWrite = 16000
   725  	for len(runes) > 0 {
   726  		m := len(runes)
   727  		if m > maxWrite {
   728  			m = maxWrite
   729  		}
   730  		chunk := runes[:m]
   731  		runes = runes[m:]
   732  		uint16s := utf16.Encode(chunk)
   733  		for len(uint16s) > 0 {
   734  			var written uint32
   735  			err := syscall.WriteConsole(fd.Sysfd, &uint16s[0], uint32(len(uint16s)), &written, nil)
   736  			if err != nil {
   737  				return 0, err
   738  			}
   739  			uint16s = uint16s[written:]
   740  		}
   741  	}
   742  	return n, nil
   743  }
   744  
   745  // Pwrite emulates the Unix pwrite system call.
   746  func (fd *FD) Pwrite(buf []byte, off int64) (int, error) {
   747  	// Call incref, not writeLock, because since pwrite specifies the
   748  	// offset it is independent from other writes.
   749  	if err := fd.incref(); err != nil {
   750  		return 0, err
   751  	}
   752  	defer fd.decref()
   753  
   754  	fd.l.Lock()
   755  	defer fd.l.Unlock()
   756  	curoffset, e := syscall.Seek(fd.Sysfd, 0, io.SeekCurrent)
   757  	if e != nil {
   758  		return 0, e
   759  	}
   760  	defer syscall.Seek(fd.Sysfd, curoffset, io.SeekStart)
   761  
   762  	ntotal := 0
   763  	for len(buf) > 0 {
   764  		b := buf
   765  		if len(b) > maxRW {
   766  			b = b[:maxRW]
   767  		}
   768  		var n uint32
   769  		o := syscall.Overlapped{
   770  			OffsetHigh: uint32(off >> 32),
   771  			Offset:     uint32(off),
   772  		}
   773  		e = syscall.WriteFile(fd.Sysfd, b, &n, &o)
   774  		ntotal += int(n)
   775  		if e != nil {
   776  			return ntotal, e
   777  		}
   778  		buf = buf[n:]
   779  		off += int64(n)
   780  	}
   781  	return ntotal, nil
   782  }
   783  
   784  // Writev emulates the Unix writev system call.
   785  func (fd *FD) Writev(buf *[][]byte) (int64, error) {
   786  	if len(*buf) == 0 {
   787  		return 0, nil
   788  	}
   789  	if err := fd.writeLock(); err != nil {
   790  		return 0, err
   791  	}
   792  	defer fd.writeUnlock()
   793  	if race.Enabled {
   794  		race.ReleaseMerge(unsafe.Pointer(&ioSync))
   795  	}
   796  	o := &fd.wop
   797  	o.InitBufs(buf)
   798  	n, err := execIO(o, func(o *operation) error {
   799  		return syscall.WSASend(o.fd.Sysfd, &o.bufs[0], uint32(len(o.bufs)), &o.qty, 0, &o.o, nil)
   800  	})
   801  	o.ClearBufs()
   802  	TestHookDidWritev(n)
   803  	consume(buf, int64(n))
   804  	return int64(n), err
   805  }
   806  
   807  // WriteTo wraps the sendto network call.
   808  func (fd *FD) WriteTo(buf []byte, sa syscall.Sockaddr) (int, error) {
   809  	if err := fd.writeLock(); err != nil {
   810  		return 0, err
   811  	}
   812  	defer fd.writeUnlock()
   813  
   814  	if len(buf) == 0 {
   815  		// handle zero-byte payload
   816  		o := &fd.wop
   817  		o.InitBuf(buf)
   818  		o.sa = sa
   819  		n, err := execIO(o, func(o *operation) error {
   820  			return syscall.WSASendto(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, o.sa, &o.o, nil)
   821  		})
   822  		return n, err
   823  	}
   824  
   825  	ntotal := 0
   826  	for len(buf) > 0 {
   827  		b := buf
   828  		if len(b) > maxRW {
   829  			b = b[:maxRW]
   830  		}
   831  		o := &fd.wop
   832  		o.InitBuf(b)
   833  		o.sa = sa
   834  		n, err := execIO(o, func(o *operation) error {
   835  			return syscall.WSASendto(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, o.sa, &o.o, nil)
   836  		})
   837  		ntotal += int(n)
   838  		if err != nil {
   839  			return ntotal, err
   840  		}
   841  		buf = buf[n:]
   842  	}
   843  	return ntotal, nil
   844  }
   845  
   846  // WriteToInet4 is WriteTo, specialized for syscall.SockaddrInet4.
   847  func (fd *FD) WriteToInet4(buf []byte, sa4 *syscall.SockaddrInet4) (int, error) {
   848  	if err := fd.writeLock(); err != nil {
   849  		return 0, err
   850  	}
   851  	defer fd.writeUnlock()
   852  
   853  	if len(buf) == 0 {
   854  		// handle zero-byte payload
   855  		o := &fd.wop
   856  		o.InitBuf(buf)
   857  		n, err := execIO(o, func(o *operation) error {
   858  			return windows.WSASendtoInet4(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, sa4, &o.o, nil)
   859  		})
   860  		return n, err
   861  	}
   862  
   863  	ntotal := 0
   864  	for len(buf) > 0 {
   865  		b := buf
   866  		if len(b) > maxRW {
   867  			b = b[:maxRW]
   868  		}
   869  		o := &fd.wop
   870  		o.InitBuf(b)
   871  		n, err := execIO(o, func(o *operation) error {
   872  			return windows.WSASendtoInet4(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, sa4, &o.o, nil)
   873  		})
   874  		ntotal += int(n)
   875  		if err != nil {
   876  			return ntotal, err
   877  		}
   878  		buf = buf[n:]
   879  	}
   880  	return ntotal, nil
   881  }
   882  
   883  // WriteToInet6 is WriteTo, specialized for syscall.SockaddrInet6.
   884  func (fd *FD) WriteToInet6(buf []byte, sa6 *syscall.SockaddrInet6) (int, error) {
   885  	if err := fd.writeLock(); err != nil {
   886  		return 0, err
   887  	}
   888  	defer fd.writeUnlock()
   889  
   890  	if len(buf) == 0 {
   891  		// handle zero-byte payload
   892  		o := &fd.wop
   893  		o.InitBuf(buf)
   894  		n, err := execIO(o, func(o *operation) error {
   895  			return windows.WSASendtoInet6(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, sa6, &o.o, nil)
   896  		})
   897  		return n, err
   898  	}
   899  
   900  	ntotal := 0
   901  	for len(buf) > 0 {
   902  		b := buf
   903  		if len(b) > maxRW {
   904  			b = b[:maxRW]
   905  		}
   906  		o := &fd.wop
   907  		o.InitBuf(b)
   908  		n, err := execIO(o, func(o *operation) error {
   909  			return windows.WSASendtoInet6(o.fd.Sysfd, &o.buf, 1, &o.qty, 0, sa6, &o.o, nil)
   910  		})
   911  		ntotal += int(n)
   912  		if err != nil {
   913  			return ntotal, err
   914  		}
   915  		buf = buf[n:]
   916  	}
   917  	return ntotal, nil
   918  }
   919  
   920  // Call ConnectEx. This doesn't need any locking, since it is only
   921  // called when the descriptor is first created. This is here rather
   922  // than in the net package so that it can use fd.wop.
   923  func (fd *FD) ConnectEx(ra syscall.Sockaddr) error {
   924  	o := &fd.wop
   925  	o.sa = ra
   926  	_, err := execIO(o, func(o *operation) error {
   927  		return ConnectExFunc(o.fd.Sysfd, o.sa, nil, 0, nil, &o.o)
   928  	})
   929  	return err
   930  }
   931  
   932  func (fd *FD) acceptOne(s syscall.Handle, rawsa []syscall.RawSockaddrAny, o *operation) (string, error) {
   933  	// Submit accept request.
   934  	o.handle = s
   935  	o.rsan = int32(unsafe.Sizeof(rawsa[0]))
   936  	_, err := execIO(o, func(o *operation) error {
   937  		return AcceptFunc(o.fd.Sysfd, o.handle, (*byte)(unsafe.Pointer(&rawsa[0])), 0, uint32(o.rsan), uint32(o.rsan), &o.qty, &o.o)
   938  	})
   939  	if err != nil {
   940  		CloseFunc(s)
   941  		return "acceptex", err
   942  	}
   943  
   944  	// Inherit properties of the listening socket.
   945  	err = syscall.Setsockopt(s, syscall.SOL_SOCKET, syscall.SO_UPDATE_ACCEPT_CONTEXT, (*byte)(unsafe.Pointer(&fd.Sysfd)), int32(unsafe.Sizeof(fd.Sysfd)))
   946  	if err != nil {
   947  		CloseFunc(s)
   948  		return "setsockopt", err
   949  	}
   950  
   951  	return "", nil
   952  }
   953  
   954  // Accept handles accepting a socket. The sysSocket parameter is used
   955  // to allocate the net socket.
   956  func (fd *FD) Accept(sysSocket func() (syscall.Handle, error)) (syscall.Handle, []syscall.RawSockaddrAny, uint32, string, error) {
   957  	if err := fd.readLock(); err != nil {
   958  		return syscall.InvalidHandle, nil, 0, "", err
   959  	}
   960  	defer fd.readUnlock()
   961  
   962  	o := &fd.rop
   963  	var rawsa [2]syscall.RawSockaddrAny
   964  	for {
   965  		s, err := sysSocket()
   966  		if err != nil {
   967  			return syscall.InvalidHandle, nil, 0, "", err
   968  		}
   969  
   970  		errcall, err := fd.acceptOne(s, rawsa[:], o)
   971  		if err == nil {
   972  			return s, rawsa[:], uint32(o.rsan), "", nil
   973  		}
   974  
   975  		// Sometimes we see WSAECONNRESET and ERROR_NETNAME_DELETED is
   976  		// returned here. These happen if connection reset is received
   977  		// before AcceptEx could complete. These errors relate to new
   978  		// connection, not to AcceptEx, so ignore broken connection and
   979  		// try AcceptEx again for more connections.
   980  		errno, ok := err.(syscall.Errno)
   981  		if !ok {
   982  			return syscall.InvalidHandle, nil, 0, errcall, err
   983  		}
   984  		switch errno {
   985  		case syscall.ERROR_NETNAME_DELETED, syscall.WSAECONNRESET:
   986  			// ignore these and try again
   987  		default:
   988  			return syscall.InvalidHandle, nil, 0, errcall, err
   989  		}
   990  	}
   991  }
   992  
   993  // Seek wraps syscall.Seek.
   994  func (fd *FD) Seek(offset int64, whence int) (int64, error) {
   995  	if err := fd.incref(); err != nil {
   996  		return 0, err
   997  	}
   998  	defer fd.decref()
   999  
  1000  	fd.l.Lock()
  1001  	defer fd.l.Unlock()
  1002  
  1003  	return syscall.Seek(fd.Sysfd, offset, whence)
  1004  }
  1005  
  1006  // Fchmod updates syscall.ByHandleFileInformation.Fileattributes when needed.
  1007  func (fd *FD) Fchmod(mode uint32) error {
  1008  	if err := fd.incref(); err != nil {
  1009  		return err
  1010  	}
  1011  	defer fd.decref()
  1012  
  1013  	var d syscall.ByHandleFileInformation
  1014  	if err := syscall.GetFileInformationByHandle(fd.Sysfd, &d); err != nil {
  1015  		return err
  1016  	}
  1017  	attrs := d.FileAttributes
  1018  	if mode&syscall.S_IWRITE != 0 {
  1019  		attrs &^= syscall.FILE_ATTRIBUTE_READONLY
  1020  	} else {
  1021  		attrs |= syscall.FILE_ATTRIBUTE_READONLY
  1022  	}
  1023  	if attrs == d.FileAttributes {
  1024  		return nil
  1025  	}
  1026  
  1027  	var du windows.FILE_BASIC_INFO
  1028  	du.FileAttributes = attrs
  1029  	l := uint32(unsafe.Sizeof(d))
  1030  	return windows.SetFileInformationByHandle(fd.Sysfd, windows.FileBasicInfo, uintptr(unsafe.Pointer(&du)), l)
  1031  }
  1032  
  1033  // Fchdir wraps syscall.Fchdir.
  1034  func (fd *FD) Fchdir() error {
  1035  	if err := fd.incref(); err != nil {
  1036  		return err
  1037  	}
  1038  	defer fd.decref()
  1039  	return syscall.Fchdir(fd.Sysfd)
  1040  }
  1041  
  1042  // GetFileType wraps syscall.GetFileType.
  1043  func (fd *FD) GetFileType() (uint32, error) {
  1044  	if err := fd.incref(); err != nil {
  1045  		return 0, err
  1046  	}
  1047  	defer fd.decref()
  1048  	return syscall.GetFileType(fd.Sysfd)
  1049  }
  1050  
  1051  // GetFileInformationByHandle wraps GetFileInformationByHandle.
  1052  func (fd *FD) GetFileInformationByHandle(data *syscall.ByHandleFileInformation) error {
  1053  	if err := fd.incref(); err != nil {
  1054  		return err
  1055  	}
  1056  	defer fd.decref()
  1057  	return syscall.GetFileInformationByHandle(fd.Sysfd, data)
  1058  }
  1059  
  1060  // RawRead invokes the user-defined function f for a read operation.
  1061  func (fd *FD) RawRead(f func(uintptr) bool) error {
  1062  	if err := fd.readLock(); err != nil {
  1063  		return err
  1064  	}
  1065  	defer fd.readUnlock()
  1066  	for {
  1067  		if f(uintptr(fd.Sysfd)) {
  1068  			return nil
  1069  		}
  1070  
  1071  		// Use a zero-byte read as a way to get notified when this
  1072  		// socket is readable. h/t https://stackoverflow.com/a/42019668/332798
  1073  		o := &fd.rop
  1074  		o.InitBuf(nil)
  1075  		if !fd.IsStream {
  1076  			o.flags |= windows.MSG_PEEK
  1077  		}
  1078  		_, err := execIO(o, func(o *operation) error {
  1079  			return syscall.WSARecv(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, &o.o, nil)
  1080  		})
  1081  		if err == windows.WSAEMSGSIZE {
  1082  			// expected with a 0-byte peek, ignore.
  1083  		} else if err != nil {
  1084  			return err
  1085  		}
  1086  	}
  1087  }
  1088  
  1089  // RawWrite invokes the user-defined function f for a write operation.
  1090  func (fd *FD) RawWrite(f func(uintptr) bool) error {
  1091  	if err := fd.writeLock(); err != nil {
  1092  		return err
  1093  	}
  1094  	defer fd.writeUnlock()
  1095  
  1096  	if f(uintptr(fd.Sysfd)) {
  1097  		return nil
  1098  	}
  1099  
  1100  	// TODO(tmm1): find a way to detect socket writability
  1101  	return syscall.EWINDOWS
  1102  }
  1103  
  1104  func sockaddrInet4ToRaw(rsa *syscall.RawSockaddrAny, sa *syscall.SockaddrInet4) int32 {
  1105  	*rsa = syscall.RawSockaddrAny{}
  1106  	raw := (*syscall.RawSockaddrInet4)(unsafe.Pointer(rsa))
  1107  	raw.Family = syscall.AF_INET
  1108  	p := (*[2]byte)(unsafe.Pointer(&raw.Port))
  1109  	p[0] = byte(sa.Port >> 8)
  1110  	p[1] = byte(sa.Port)
  1111  	raw.Addr = sa.Addr
  1112  	return int32(unsafe.Sizeof(*raw))
  1113  }
  1114  
  1115  func sockaddrInet6ToRaw(rsa *syscall.RawSockaddrAny, sa *syscall.SockaddrInet6) int32 {
  1116  	*rsa = syscall.RawSockaddrAny{}
  1117  	raw := (*syscall.RawSockaddrInet6)(unsafe.Pointer(rsa))
  1118  	raw.Family = syscall.AF_INET6
  1119  	p := (*[2]byte)(unsafe.Pointer(&raw.Port))
  1120  	p[0] = byte(sa.Port >> 8)
  1121  	p[1] = byte(sa.Port)
  1122  	raw.Scope_id = sa.ZoneId
  1123  	raw.Addr = sa.Addr
  1124  	return int32(unsafe.Sizeof(*raw))
  1125  }
  1126  
  1127  func rawToSockaddrInet4(rsa *syscall.RawSockaddrAny, sa *syscall.SockaddrInet4) {
  1128  	pp := (*syscall.RawSockaddrInet4)(unsafe.Pointer(rsa))
  1129  	p := (*[2]byte)(unsafe.Pointer(&pp.Port))
  1130  	sa.Port = int(p[0])<<8 + int(p[1])
  1131  	sa.Addr = pp.Addr
  1132  }
  1133  
  1134  func rawToSockaddrInet6(rsa *syscall.RawSockaddrAny, sa *syscall.SockaddrInet6) {
  1135  	pp := (*syscall.RawSockaddrInet6)(unsafe.Pointer(rsa))
  1136  	p := (*[2]byte)(unsafe.Pointer(&pp.Port))
  1137  	sa.Port = int(p[0])<<8 + int(p[1])
  1138  	sa.ZoneId = pp.Scope_id
  1139  	sa.Addr = pp.Addr
  1140  }
  1141  
  1142  func sockaddrToRaw(rsa *syscall.RawSockaddrAny, sa syscall.Sockaddr) (int32, error) {
  1143  	switch sa := sa.(type) {
  1144  	case *syscall.SockaddrInet4:
  1145  		sz := sockaddrInet4ToRaw(rsa, sa)
  1146  		return sz, nil
  1147  	case *syscall.SockaddrInet6:
  1148  		sz := sockaddrInet6ToRaw(rsa, sa)
  1149  		return sz, nil
  1150  	default:
  1151  		return 0, syscall.EWINDOWS
  1152  	}
  1153  }
  1154  
  1155  // ReadMsg wraps the WSARecvMsg network call.
  1156  func (fd *FD) ReadMsg(p []byte, oob []byte, flags int) (int, int, int, syscall.Sockaddr, error) {
  1157  	if err := fd.readLock(); err != nil {
  1158  		return 0, 0, 0, nil, err
  1159  	}
  1160  	defer fd.readUnlock()
  1161  
  1162  	if len(p) > maxRW {
  1163  		p = p[:maxRW]
  1164  	}
  1165  
  1166  	o := &fd.rop
  1167  	o.InitMsg(p, oob)
  1168  	if o.rsa == nil {
  1169  		o.rsa = new(syscall.RawSockaddrAny)
  1170  	}
  1171  	o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa))
  1172  	o.msg.Namelen = int32(unsafe.Sizeof(*o.rsa))
  1173  	o.msg.Flags = uint32(flags)
  1174  	n, err := execIO(o, func(o *operation) error {
  1175  		return windows.WSARecvMsg(o.fd.Sysfd, &o.msg, &o.qty, &o.o, nil)
  1176  	})
  1177  	err = fd.eofError(n, err)
  1178  	var sa syscall.Sockaddr
  1179  	if err == nil {
  1180  		sa, err = o.rsa.Sockaddr()
  1181  	}
  1182  	return n, int(o.msg.Control.Len), int(o.msg.Flags), sa, err
  1183  }
  1184  
  1185  // ReadMsgInet4 is ReadMsg, but specialized to return a syscall.SockaddrInet4.
  1186  func (fd *FD) ReadMsgInet4(p []byte, oob []byte, flags int, sa4 *syscall.SockaddrInet4) (int, int, int, error) {
  1187  	if err := fd.readLock(); err != nil {
  1188  		return 0, 0, 0, err
  1189  	}
  1190  	defer fd.readUnlock()
  1191  
  1192  	if len(p) > maxRW {
  1193  		p = p[:maxRW]
  1194  	}
  1195  
  1196  	o := &fd.rop
  1197  	o.InitMsg(p, oob)
  1198  	if o.rsa == nil {
  1199  		o.rsa = new(syscall.RawSockaddrAny)
  1200  	}
  1201  	o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa))
  1202  	o.msg.Namelen = int32(unsafe.Sizeof(*o.rsa))
  1203  	o.msg.Flags = uint32(flags)
  1204  	n, err := execIO(o, func(o *operation) error {
  1205  		return windows.WSARecvMsg(o.fd.Sysfd, &o.msg, &o.qty, &o.o, nil)
  1206  	})
  1207  	err = fd.eofError(n, err)
  1208  	if err == nil {
  1209  		rawToSockaddrInet4(o.rsa, sa4)
  1210  	}
  1211  	return n, int(o.msg.Control.Len), int(o.msg.Flags), err
  1212  }
  1213  
  1214  // ReadMsgInet6 is ReadMsg, but specialized to return a syscall.SockaddrInet6.
  1215  func (fd *FD) ReadMsgInet6(p []byte, oob []byte, flags int, sa6 *syscall.SockaddrInet6) (int, int, int, error) {
  1216  	if err := fd.readLock(); err != nil {
  1217  		return 0, 0, 0, err
  1218  	}
  1219  	defer fd.readUnlock()
  1220  
  1221  	if len(p) > maxRW {
  1222  		p = p[:maxRW]
  1223  	}
  1224  
  1225  	o := &fd.rop
  1226  	o.InitMsg(p, oob)
  1227  	if o.rsa == nil {
  1228  		o.rsa = new(syscall.RawSockaddrAny)
  1229  	}
  1230  	o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa))
  1231  	o.msg.Namelen = int32(unsafe.Sizeof(*o.rsa))
  1232  	o.msg.Flags = uint32(flags)
  1233  	n, err := execIO(o, func(o *operation) error {
  1234  		return windows.WSARecvMsg(o.fd.Sysfd, &o.msg, &o.qty, &o.o, nil)
  1235  	})
  1236  	err = fd.eofError(n, err)
  1237  	if err == nil {
  1238  		rawToSockaddrInet6(o.rsa, sa6)
  1239  	}
  1240  	return n, int(o.msg.Control.Len), int(o.msg.Flags), err
  1241  }
  1242  
  1243  // WriteMsg wraps the WSASendMsg network call.
  1244  func (fd *FD) WriteMsg(p []byte, oob []byte, sa syscall.Sockaddr) (int, int, error) {
  1245  	if len(p) > maxRW {
  1246  		return 0, 0, errors.New("packet is too large (only 1GB is allowed)")
  1247  	}
  1248  
  1249  	if err := fd.writeLock(); err != nil {
  1250  		return 0, 0, err
  1251  	}
  1252  	defer fd.writeUnlock()
  1253  
  1254  	o := &fd.wop
  1255  	o.InitMsg(p, oob)
  1256  	if sa != nil {
  1257  		if o.rsa == nil {
  1258  			o.rsa = new(syscall.RawSockaddrAny)
  1259  		}
  1260  		len, err := sockaddrToRaw(o.rsa, sa)
  1261  		if err != nil {
  1262  			return 0, 0, err
  1263  		}
  1264  		o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa))
  1265  		o.msg.Namelen = len
  1266  	}
  1267  	n, err := execIO(o, func(o *operation) error {
  1268  		return windows.WSASendMsg(o.fd.Sysfd, &o.msg, 0, &o.qty, &o.o, nil)
  1269  	})
  1270  	return n, int(o.msg.Control.Len), err
  1271  }
  1272  
  1273  // WriteMsgInet4 is WriteMsg specialized for syscall.SockaddrInet4.
  1274  func (fd *FD) WriteMsgInet4(p []byte, oob []byte, sa *syscall.SockaddrInet4) (int, int, error) {
  1275  	if len(p) > maxRW {
  1276  		return 0, 0, errors.New("packet is too large (only 1GB is allowed)")
  1277  	}
  1278  
  1279  	if err := fd.writeLock(); err != nil {
  1280  		return 0, 0, err
  1281  	}
  1282  	defer fd.writeUnlock()
  1283  
  1284  	o := &fd.wop
  1285  	o.InitMsg(p, oob)
  1286  	if o.rsa == nil {
  1287  		o.rsa = new(syscall.RawSockaddrAny)
  1288  	}
  1289  	len := sockaddrInet4ToRaw(o.rsa, sa)
  1290  	o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa))
  1291  	o.msg.Namelen = len
  1292  	n, err := execIO(o, func(o *operation) error {
  1293  		return windows.WSASendMsg(o.fd.Sysfd, &o.msg, 0, &o.qty, &o.o, nil)
  1294  	})
  1295  	return n, int(o.msg.Control.Len), err
  1296  }
  1297  
  1298  // WriteMsgInet6 is WriteMsg specialized for syscall.SockaddrInet6.
  1299  func (fd *FD) WriteMsgInet6(p []byte, oob []byte, sa *syscall.SockaddrInet6) (int, int, error) {
  1300  	if len(p) > maxRW {
  1301  		return 0, 0, errors.New("packet is too large (only 1GB is allowed)")
  1302  	}
  1303  
  1304  	if err := fd.writeLock(); err != nil {
  1305  		return 0, 0, err
  1306  	}
  1307  	defer fd.writeUnlock()
  1308  
  1309  	o := &fd.wop
  1310  	o.InitMsg(p, oob)
  1311  	if o.rsa == nil {
  1312  		o.rsa = new(syscall.RawSockaddrAny)
  1313  	}
  1314  	len := sockaddrInet6ToRaw(o.rsa, sa)
  1315  	o.msg.Name = (syscall.Pointer)(unsafe.Pointer(o.rsa))
  1316  	o.msg.Namelen = len
  1317  	n, err := execIO(o, func(o *operation) error {
  1318  		return windows.WSASendMsg(o.fd.Sysfd, &o.msg, 0, &o.qty, &o.o, nil)
  1319  	})
  1320  	return n, int(o.msg.Control.Len), err
  1321  }