Commit Graph

2169 Commits

Author SHA1 Message Date
Saleem Abdulrasool 055043e151
NIOPosix: use internal enumeration for GAI resolver (#2177)
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.
2022-06-09 08:21:24 +01:00
Saleem Abdulrasool 2560725d71
NIOPosix: use typealias instead of C name (#2180)
Switch to the typealias rather than the raw name to support Windows and
make the code homogeneous with the rest of the codebase.
2022-06-08 10:07:32 -07:00
Saleem Abdulrasool e697fd315a
NIOPosix: silence some deprecation warnings on Windows (#2171)
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.
2022-06-08 10:20:52 +01:00
Saleem Abdulrasool 4448bb04aa
NIOPosix: remove unused alias for function (#2172)
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>
2022-06-07 10:48:29 -07:00
Saleem Abdulrasool cd70dd3ec1
NIOPosix: shuffle typealias location (#2174)
Move the `sockaddr` typealias for Windows into the `BSDSocketAPIWindows`
file which is a better home for the platform specific typealias.
2022-06-07 10:24:35 -07:00
Saleem Abdulrasool 4f20e48a78
NIOPosix: use wrapper function for duplicating file descriptor (#2173)
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>
2022-06-07 10:08:52 -07:00
Saleem Abdulrasool 65c3f576b1
NIOPosix: reduce imports for Windows (#2170)
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.
2022-06-07 09:37:11 -07:00
Saleem Abdulrasool 207e6e0f7e
NIOPosix: add additional imports and typealiases on Windows (#2168)
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>
2022-06-07 07:38:50 -07:00
David Nadoba 0e2818bf86
Remove `@preconcurrency` from internal `EventLoopFuture` methods (#2135)
* add @preconcurrency again as it otherwise results in one more allcation in 1000_udpconnections
2022-06-07 07:09:44 -07:00
Saleem Abdulrasool fae6522f6d
NIOCore: adjust the BSDSocketAPI for Windows (#2164)
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>
2022-06-07 06:39:48 -07:00
Saleem Abdulrasool b4e4f0211f
NIOCore: adjust system call helpers for Windows (#2155)
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>
2022-06-07 06:08:32 -07:00
Saleem Abdulrasool bb2aae798f
NIOCore: extract and modify POSIX flag constants (#2150)
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>
2022-06-07 05:44:36 -07:00
Saleem Abdulrasool a8bb008516
NIOPosix: use `errno` accessor on Windows (#2167)
`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>
2022-06-07 05:10:12 -07:00
Cory Benfield 67c0fd36d3
Add support for translating subnet prefixes to masks (#2169)
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.
2022-06-07 04:49:19 -07:00
Saleem Abdulrasool b883295b0e
NIOPosix: re-export C library on Windows to match (#2166)
This matches the behaviour on the non-Windows targets and re-exports the
C library.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-07 04:08:57 -07:00
Saleem Abdulrasool d2bd0d03fe
NIOCore: duplicate the NIODevice implementation on Windows (#2165)
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>
2022-06-07 01:16:41 -07:00
Saleem Abdulrasool c73c4d60f8
NIOCore: remove reference to `in_addr_t` on Windows (#2163)
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.
2022-06-07 08:31:13 +01:00
Saleem Abdulrasool b5a185b62b
NIOCore: import the C library interfaces (#2158)
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>
2022-06-06 06:23:21 -07:00
Saleem Abdulrasool 544742cb5e
NIOCore: import and alias some BSD socket types on Windows (#2157)
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>
2022-06-06 05:50:32 -07:00
Saleem Abdulrasool 38d8c7105c
NIOCore: add explicit type cast for Windows compatibility (#2156)
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>
2022-06-06 05:28:23 -07:00
Saleem Abdulrasool 9f9a0b2bc0
NIOCore: adjust socket address handling on Windows (#2152)
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>
2022-06-06 03:40:59 -07:00
Konrad `ktoso` Malawski b94a0696df
Fix docc warning about missing symbol (#2162)
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.`
2022-06-06 11:26:06 +01:00
Saleem Abdulrasool eff9c964fe
NIOCore: import additional socket types on Windows (#2151)
Add imports for additional socket types to enable building more of
NIOCore on Windows.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-01 13:08:22 -07:00
David Nadoba 9c60117db0
Adopt `Sendable` in `ChannelPipeline.swift` and `ChannelInvoker.swift` (#2143) 2022-06-01 08:42:24 -07:00
David Nadoba 4574bdde82
Adopt `Sendable` in `Codec.swift` and `SingleStepByteToMessageDecoder.swift` (#2149)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-01 08:24:21 -07:00
David Nadoba 97a568ac61
Adopt `Sendable` in `RecvByteBufferAllocator.swift` (#2148)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-01 16:08:04 +01:00
David Nadoba 6f119c2995
Adopt `Sendable` in `ChannelHandlers.swift` (#2144)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-01 06:32:17 -07:00
David Nadoba 2f8d5408ae
Adopt `Sendable` in `ConvenienceOptionSupport.swift` (#2145)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-01 05:58:16 -07:00
David Nadoba 6178126298
Adopt `Sendable` in `NIOCloseOnErrorHandler.swift` (#2146)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-01 05:28:43 -07:00
Saleem Abdulrasool 13a32273f7
NIOCore: adjust labels for `inet_{ntop,pton}` on Windows (#2141)
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>
2022-06-01 05:05:50 -07:00
Cory Benfield 32f62a89d9
Fix flake AsyncEmbeddedEventLoopTest (#2147)
Motivation:

Flaky tests are really annoying.

Modifications:

- Stop assuming that the item is not immediately nil

Result:

Test now passes consistently.
2022-06-01 03:44:59 -07:00
Saleem Abdulrasool afbf7566d5
NIOCore: handle structural differences on Windows (#2140)
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.
2022-05-31 16:07:32 -07:00
Saleem Abdulrasool 15aaf884b4
NIOCore: replace `mode_t` with `CInt` (#2132)
* 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>
2022-05-31 14:15:47 -07:00
Saleem Abdulrasool 4d4c942c35
NIOCore: use `errno` accessor on Windows (#2139)
`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.
2022-05-31 10:30:32 -07:00
Saleem Abdulrasool 8b3acb66b4
NIOCore: import `INET{,6}_ADDRSTRLEN` constants (#2138)
This imports the address string length constants from WinSock for
Windows.
2022-05-31 17:50:08 +01:00
Si Beaumont 9bf5075241
Add initial support for connected datagram sockets (#2084)
* 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>
2022-05-31 13:21:41 +01:00
Saleem Abdulrasool 083eba3652
NIOCore: correct typo in error namespace on Windows (#2133)
There was a missing `r` in the error type which caused a build failure
on Windows.  Correct the spelling for the error on Windows.
2022-05-31 10:52:07 +01:00
Saleem Abdulrasool e109615309
NIOCore: import `WSAGetLastError`, `GetLastError` (#2131)
We use these functions to report errors from WinSock.  This was likely
excluded accidentally from previous commits.
2022-05-30 11:00:34 -07:00
Saleem Abdulrasool 0f93ed145b
NIOCore: adjust for label change on Windows (#2130)
`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.
2022-05-30 10:32:30 -07:00
Saleem Abdulrasool 78c97a3cda
NIOCore: add missing import for `IO` (#2129)
Import `ucrt` for access to `strerror` on Windows.
2022-05-30 18:15:24 +01:00
Saleem Abdulrasool 5f8608bf0b
_NIODataStructures: repair the build on Windows (#2128)
Import `ucrt` which is the MSVC C library to match the behaviour of the
other platforms.  This allows building `_NIODataStructures` on Windows.
2022-05-30 09:51:13 -07:00
Saleem Abdulrasool 78da9a1799
build: correct dependencies for `NIOCore` (#2127)
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>
2022-05-30 09:33:04 -07:00
Si Beaumont b7308b1cd0
DatagramChannelTests: Handle receiving datagrams out-of-order in ECN tests (#2123)
* 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>
2022-05-30 03:51:52 -07:00
David Nadoba e16a340c57
Adopt `Sendable` for `EventLoopFuture` and `EventLoopPromise` (#2107)
* 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>
2022-05-30 11:35:23 +01:00
David Nadoba 94969e7280
Adopt `Sendable` in `EventLoop.swift` (#2101)
* 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
2022-05-30 10:41:18 +01:00
Si Beaumont 8b25ba9761
readme: Fix API docs link following NIO core refactoring (#2126)
Signed-off-by: Si Beaumont <beaumont@apple.com>
2022-05-30 01:03:30 -07:00
Si Beaumont 92d20d92b0
wip: Use clock_gettime for NIODeadline.now() (#2119)
* 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>
2022-05-19 09:28:31 +01:00
Si Beaumont 9294f8da3b
NIOPerformanceTester: Increase operations used in lock benchmarks from 1M to 10M (#2121) 2022-05-18 10:53:51 +01:00
David Nadoba 3dcb0f5e9b
Introduce internal `UnsafeTransfer` type (#2115)
Motivation:

Soon we will need to silence some `Sendable` warnings. 
The recommended way is to use a wrapper type that is unconditionally 
`Sendable`. 

https://github.com/apple/swift-evolution/blob/main/proposals/0302-concurrent-value-and-concurrent-closures.md#adaptor-types-for-legacy-codebases

Modifications:

- add internal `UnsafeTransfer` type

Result:

We can silence `Sendable` warnings and errors by using `UnsafeTransfer` to pass non-`Sendable` values around.
2022-05-17 08:58:11 +01:00
Si Beaumont 771257cc23
NIOPerformanceTester: Add DeadlineNowBenchmark for NIODeadline.now() (#2117)
* 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>
2022-05-16 19:21:31 +01:00