-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
win32unix: use WSADuplicateSocket in bindings of dup and dup2
The WinAPI documentation of [DuplicateHandle][] gives a list of types of objects on which it is safe to call DuplicateHandle. In particular, the doc reads: > You should not use DuplicateHandle to duplicate handles to the > following objects: > > - Sockets. No error is returned, but the duplicate handle may not be > recognized by Winsock at the target process. Also, using > DuplicateHandle interferes with internal reference counting on the > underlying object. To duplicate a socket handle, use the > WSADuplicateSocket function. Rework the code to use DuplicateHandle for handles and [WSADuplicateSocket][] for sockets. WSADuplicateSocket returns an info structure that we give to WSASocket to create the duplicated handle. However, there seem to be no way of explicitely preserving the set of flags to give to WSASocket. Considering that as of now, OCaml uses the socket function instead of WSASocket, and that socket only sets WSA_FLAG_OVERLAPPED, re-set that flag and hope for the best (that's also the current behavior of libuv). This commit also fixes a little bug. In the error path of DuplicateHandle there was: win32_maperr(GetLastError()); return -1; but the function returns a value, and from the OCaml code above it's better to raise an exception instead. [DuplicateHandle]: https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-duplicatehandle [WSADuplicateSocket]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsaduplicatesocketw
- Loading branch information
Showing
3 changed files
with
104 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters