Replace the use of raw constants with the internal enumeration. This
ensures that the constant names are uniform and don't leak structural
information from the underlying information.
Create a wrapper over `strerror` which is considered deprecated due to
the use of a shared global buffer. Use `strerror_s` which requires a
buffer and size to prevent shared state or a buffer overrun. This is
documented by Microsoft as requiring at most 94 characters, so we use a
buffer of size 95.
This cleans up a reference to `freeifaddrs` as that is no longer
referenced in the code base. This avoids an error on Windows as
a side-effect.
Co-authored-by: Cory Benfield <lukasa@apple.com>
Use the wrapper function for duplicating the file descriptor. This
allows us to swap out the implementation for different targets making
the code more portable. It additionally adds additional error handling
and checking as the wrappers are intended to perform error handling.
Co-authored-by: Cory Benfield <lukasa@apple.com>
Reduce unnecessary noise in the files after previous refactoring
shuffled the use of the imported interfaces. We can centralise most of
the imports and internal typealiases which avoids cluttering the shared
interfaces with compatibility handling for Windows.
Extend more of the internal interfaces for the BSD API coverage on
Windows to get the same API surface when possible.
Co-authored-by: Cory Benfield <lukasa@apple.com>
Windows does not provide a definition for `socklen_t`. Create an
internal typealias for it to provide source stability to the internal
interfaces.
Co-authored-by: George Barnett <gbarnett@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
Windows does not provide a 3 parameter `open` and needs to be passed all
the parameters as `_open` is a variadic function (as per the
specification). This provides a Windows specific path for `open` and
`read` as on Darwin and Linux, `read` uses a non-standard return type
(`ssize_t`). As it happens, the `size` parameter on Windows also uses
`unsigned int` rather than `size_t` which would break on different
bitnesses.
Co-authored-by: Cory Benfield <lukasa@apple.com>
Windows does not have the same set of POSIX flag constants. In
particular, there is no equivalent to group permissions as the ACL model
on Windows is far richer and requires the full specification of the
DACLs. Additionally, isolate `O_CLOEXEC` to aid Windows which does not
have this flag.
Co-authored-by: Cory Benfield <lukasa@apple.com>
`errno` is a thread local variable, and as such on many platforms is
accessed indirectly. This is implemented as macro, but is considered
complex by the clang importer and cannot be directly used. Resolve this
by using the accessor for the value on Windows.
Co-authored-by: Cory Benfield <lukasa@apple.com>
Motivation:
On Windows we have a need to transform a subnet prefix into a subnet
mask when enumerating devices. This is a straightforward transformation
that we can easily do in pure-Swift, so let's do that. Additionally, the
functions have small enough domains that we can just exhaustively test
them.
Modifications:
- Added functions to create SocketAddress objects from subnet prefixes.
- Added exhaustive tests for those functions.
- Use these functions in Windows code.
Result:
Correct transformations of prefixes into masks on Windows.
Windows does not have an `ifaddr` interface, and instead we need to
construct the interface from the adapter and logical interface.
Replicate this for the construction of the deprecated type just for
repairing the build.
Co-authored-by: Cory Benfield <lukasa@apple.com>
This type does not exist in the current SDK (and previous iterations
also did not use the `in_addr_t` type). Remove the type and use the
de-sugared `UInt` type instead on Windows.
The socket address handling logic depends on multiple C library
interfaces which requires the import of the C library. Add that on
Windows.
Co-authored-by: Cory Benfield <lukasa@apple.com>
Although WinSock conceptually supports many of the BSD Socket APIs, it
does not provide source compatibility. This adds a few type aliases for
adjusting towards source compatibility.
Co-authored-by: Cory Benfield <lukasa@apple.com>
Windows does not have a `socklen_t` type, instead using `CInt` for the
value. This adds an explicit type cast to repair the build on Windows.
Co-authored-by: Cory Benfield <lukasa@apple.com>
Match the non-Windows path and unwrap the address pointer before use.
The two paths had diverged, and now converge in behaviour even if not in
spelling.
Co-authored-by: Cory Benfield <lukasa@apple.com>
ByteBuffer is in the same NIOCore module, so no need to classify it (and even if, it should be `NIOCore` instead).
`warning: Topic reference 'ByteBuffer' couldn't be resolved. No local documentation matches this reference.`
Update the callsites for the new labels for the BSDSocketAPI interfaces
that were not updated to reflect the new labels.
Co-authored-by: Cory Benfield <lukasa@apple.com>
The `sockaddr_t` type on Windows is not laid out identically to the Unix
counterpart (due to pedantically correct reasons). There exist an inner
union type for the reinterpretation of the storage which requires naming
the union type.
* NIOCore: replace `mode_t` with `CInt`
Windows does not have a `mode_t` type alias, instead using the
de-sugared `CInt` type. De-sugar the instances to permit building on
Windows.
* Update Sources/NIOCore/FileHandle.swift
Co-authored-by: Cory Benfield <lukasa@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
`errno` on Windows cannot be access as a raw name as it goes through a
"complex" macro for the TLS access. Use the `_get_errno` helper instead
of accessing the `errno` through the macro for building on Windows.
* socket: Make destinationPtr param optional in sendmsg(...)
Signed-off-by: Si Beaumont <beaumont@apple.com>
* pdwm: Fixup documentation: scalar writes use sendmsg, not sendto
Signed-off-by: Si Beaumont <beaumont@apple.com>
* pdwm: Make sockaddr pointer param optional in scalarWriteOperation
Signed-off-by: Si Beaumont <beaumont@apple.com>
* pdwm: Add isConnected property to PendingDatagramWritesState
Signed-off-by: Si Beaumont <beaumont@apple.com>
* pdwm: If socket is connected use NULL msg_name in sendmsg(2)
Signed-off-by: Si Beaumont <beaumont@apple.com>
* BaseSocketChannel: Support connect after bind
Signed-off-by: Si Beaumont <beaumont@apple.com>
* DatagramChannel: Implement connectSocket(to:)
Signed-off-by: Si Beaumont <beaumont@apple.com>
* bootstrap: Rename bind0(makeChannel:registerAndBind:) to withNewChannel(makeChannel:bringup:)
Signed-off-by: Si Beaumont <beaumont@apple.com>
* bootstrap: Add set of DatagramBootstrap.connect(...) APIs
Signed-off-by: Si Beaumont <beaumont@apple.com>
* test: Remove DatagramChannelTests.testConnectionFails
Signed-off-by: Si Beaumont <beaumont@apple.com>
* test: Add ConnectedDatagramChannelTests, inheriting from DatagramChannelTests
Signed-off-by: Si Beaumont <beaumont@apple.com>
* NIOUDPEchoClient: Use connected-mode UDP
Signed-off-by: Si Beaumont <beaumont@apple.com>
* soundness: Update copyright notice
Signed-off-by: Si Beaumont <beaumont@apple.com>
* fixup: cleanup bootstrap APIs
Signed-off-by: Si Beaumont <beaumont@apple.com>
* pdwm: Check address of pending write if connected and add test
Signed-off-by: Si Beaumont <beaumont@apple.com>
* Revert "pdwm: Check address of pending write if connected and add test"
This reverts commit a4ee0756d5.
* channel: Fail buffered writes on connect and validate writes when connected
Signed-off-by: Si Beaumont <beaumont@apple.com>
* Run soundness.sh to get linux tests generated
Signed-off-by: Si Beaumont <beaumont@apple.com>
* NIOUDPEchoClient: Connect socket to remote only if --connect is used
Signed-off-by: Si Beaumont <beaumont@apple.com>
* socket: Support ByteBuffer (without AddressedEnvelope) for DatagramChannel
Signed-off-by: Si Beaumont <beaumont@apple.com>
* test: Simplify some test code
Signed-off-by: Si Beaumont <beaumont@apple.com>
* pdwm: Factor out common, private add(_ pendingWrite:)
Signed-off-by: Si Beaumont <beaumont@apple.com>
* channel: Support AddressedEnvelope on connected socket for control messages
Signed-off-by: Si Beaumont <beaumont@apple.com>
* channel: Defer to common unwrapData for error handling
Signed-off-by: Si Beaumont <beaumont@apple.com>
* channel: Throw more specific (new) errors, instead of IOError
Signed-off-by: Si Beaumont <beaumont@apple.com>
* SocketChannelLifecycleManager: Add supportsReconnect boolean property, used in DatagramChannel
Signed-off-by: Si Beaumont <beaumont@apple.com>
`NIOBSDSocket.inet_pton` and `NIOBSDSocket.inet_ntop` altered the label
for the family parameter but failed to update the Windows path. Adjust
the name accordingly.
The `NIOCore` depends on the platform specific implementation but the
dependency for the Windows platform content was missing. Adjust this
to repair the build graph for Windows.
Co-authored-by: Cory Benfield <lukasa@apple.com>
* DatagramChannelTests: Handle receiving datagrams out-of-order in ECN tests
Signed-off-by: Si Beaumont <beaumont@apple.com>
* fixup: Simplify calls to write and read integer in payload
Signed-off-by: Si Beaumont <beaumont@apple.com>
* fixup: Iterate through expected reads, not actual reads
Signed-off-by: Si Beaumont <beaumont@apple.com>
* fixup: Order reads by seq payload and use array equality
* Update Tests/NIOPosixTests/DatagramChannelTests.swift
Co-authored-by: Cory Benfield <lukasa@apple.com>
* Adopt `Sendable` for `EventLoopFuture` and `EventLoopPromise`
* use internal typealias to deduplicate method bodies
* fix `Sendable` warnings for nightly toolchains
* fix compliation for Swift 5.5 & 5.6
* Adopt `Sendable` only in Swift 5.7+
Co-authored-by: Cory Benfield <lukasa@apple.com>
* Adopt `Sendable` in `EventLoop.swift`
* only adopt `Sendable` in Swift 5.6+
* add `@Sendable` only for Swift 5.7
* fix swift 5.5
* use internal typealias to deduplicate method bodies
* wip: Use clock_gettime for NIODeadline.now()
Signed-off-by: Si Beaumont <beaumont@apple.com>
* fixup: Add #if os(Linux) for clock_gettime use
* fixup: Add doc comments
Signed-off-by: Si Beaumont <beaumont@apple.com>
* NIOPerformanceTester: Add DeadlineNowBenchmark for NIODeadline.now()
Signed-off-by: Si Beaumont <beaumont@apple.com>
* fixup: Return counter from benchmark
Signed-off-by: Si Beaumont <beaumont@apple.com>