|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftNIO open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022 Apple Inc. and the SwiftNIO project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | +import NIOCore |
| 15 | + |
| 16 | +/// A `RawSocketBootstrap` is an easy way to interact with IP based protocols other then TCP and UDP. |
| 17 | +/// |
| 18 | +/// Example: |
| 19 | +/// |
| 20 | +/// ```swift |
| 21 | +/// let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 22 | +/// defer { |
| 23 | +/// try! group.syncShutdownGracefully() |
| 24 | +/// } |
| 25 | +/// let bootstrap = RawSocketBootstrap(group: group) |
| 26 | +/// .channelInitializer { channel in |
| 27 | +/// channel.pipeline.addHandler(MyChannelHandler()) |
| 28 | +/// } |
| 29 | +/// let channel = try! bootstrap.bind(host: "127.0.0.1", ipProtocol: .icmp).wait() |
| 30 | +/// /* the Channel is now ready to send/receive IP packets */ |
| 31 | +/// |
| 32 | +/// try channel.closeFuture.wait() // Wait until the channel un-binds. |
| 33 | +/// ``` |
| 34 | +/// |
| 35 | +/// The `Channel` will operate on `AddressedEnvelope<ByteBuffer>` as inbound and outbound messages. |
| 36 | +public final class NIORawSocketBootstrap { |
| 37 | + |
| 38 | + private let group: EventLoopGroup |
| 39 | + private var channelInitializer: Optional<ChannelInitializerCallback> |
| 40 | + @usableFromInline |
| 41 | + internal var _channelOptions: ChannelOptions.Storage |
| 42 | + |
| 43 | + /// Create a `RawSocketBootstrap` on the `EventLoopGroup` `group`. |
| 44 | + /// |
| 45 | + /// The `EventLoopGroup` `group` must be compatible, otherwise the program will crash. `RawSocketBootstrap` is |
| 46 | + /// compatible only with `MultiThreadedEventLoopGroup` as well as the `EventLoop`s returned by |
| 47 | + /// `MultiThreadedEventLoopGroup.next`. See `init(validatingGroup:)` for a fallible initializer for |
| 48 | + /// situations where it's impossible to tell ahead of time if the `EventLoopGroup` is compatible or not. |
| 49 | + /// |
| 50 | + /// - parameters: |
| 51 | + /// - group: The `EventLoopGroup` to use. |
| 52 | + public convenience init(group: EventLoopGroup) { |
| 53 | + guard NIOOnSocketsBootstraps.isCompatible(group: group) else { |
| 54 | + preconditionFailure("RawSocketBootstrap is only compatible with MultiThreadedEventLoopGroup and " + |
| 55 | + "SelectableEventLoop. You tried constructing one with \(group) which is incompatible.") |
| 56 | + } |
| 57 | + self.init(validatingGroup: group)! |
| 58 | + } |
| 59 | + |
| 60 | + /// Create a `RawSocketBootstrap` on the `EventLoopGroup` `group`, validating that `group` is compatible. |
| 61 | + /// |
| 62 | + /// - parameters: |
| 63 | + /// - group: The `EventLoopGroup` to use. |
| 64 | + public init?(validatingGroup group: EventLoopGroup) { |
| 65 | + guard NIOOnSocketsBootstraps.isCompatible(group: group) else { |
| 66 | + return nil |
| 67 | + } |
| 68 | + self._channelOptions = ChannelOptions.Storage() |
| 69 | + self.group = group |
| 70 | + self.channelInitializer = nil |
| 71 | + } |
| 72 | + |
| 73 | + /// Initialize the bound `Channel` with `initializer`. The most common task in initializer is to add |
| 74 | + /// `ChannelHandler`s to the `ChannelPipeline`. |
| 75 | + /// |
| 76 | + /// - parameters: |
| 77 | + /// - handler: A closure that initializes the provided `Channel`. |
| 78 | + public func channelInitializer(_ handler: @escaping @Sendable (Channel) -> EventLoopFuture<Void>) -> Self { |
| 79 | + self.channelInitializer = handler |
| 80 | + return self |
| 81 | + } |
| 82 | + |
| 83 | + /// Specifies a `ChannelOption` to be applied to the `Channel`. |
| 84 | + /// |
| 85 | + /// - parameters: |
| 86 | + /// - option: The option to be applied. |
| 87 | + /// - value: The value for the option. |
| 88 | + @inlinable |
| 89 | + public func channelOption<Option: ChannelOption>(_ option: Option, value: Option.Value) -> Self { |
| 90 | + self._channelOptions.append(key: option, value: value) |
| 91 | + return self |
| 92 | + } |
| 93 | + |
| 94 | + /// Bind the `Channel` to `host`. |
| 95 | + /// All packets or errors matching the `ipProtocol` specified are passed to the resulting `Channel`. |
| 96 | + /// |
| 97 | + /// - parameters: |
| 98 | + /// - host: The host to bind on. |
| 99 | + /// - ipProtocol: The IP protocol used in the IP protocol/nextHeader field. |
| 100 | + public func bind(host: String, ipProtocol: NIOIPProtocol) -> EventLoopFuture<Channel> { |
| 101 | + return bind0(ipProtocol: ipProtocol) { |
| 102 | + return try SocketAddress.makeAddressResolvingHost(host, port: 0) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private func bind0(ipProtocol: NIOIPProtocol, _ makeSocketAddress: () throws -> SocketAddress) -> EventLoopFuture<Channel> { |
| 107 | + let address: SocketAddress |
| 108 | + do { |
| 109 | + address = try makeSocketAddress() |
| 110 | + } catch { |
| 111 | + return group.next().makeFailedFuture(error) |
| 112 | + } |
| 113 | + precondition(address.port == nil || address.port == 0, "port must be 0 or not set") |
| 114 | + func makeChannel(_ eventLoop: SelectableEventLoop) throws -> DatagramChannel { |
| 115 | + return try DatagramChannel(eventLoop: eventLoop, |
| 116 | + protocolFamily: address.protocol, |
| 117 | + protocolSubtype: .init(ipProtocol), |
| 118 | + socketType: .raw) |
| 119 | + } |
| 120 | + return withNewChannel(makeChannel: makeChannel) { (eventLoop, channel) in |
| 121 | + channel.register().flatMap { |
| 122 | + channel.bind(to: address) |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /// Connect the `Channel` to `host`. |
| 128 | + /// |
| 129 | + /// - parameters: |
| 130 | + /// - host: The host to connect to. |
| 131 | + /// - ipProtocol: The IP protocol used in the IP protocol/nextHeader field. |
| 132 | + public func connect(host: String, ipProtocol: NIOIPProtocol) -> EventLoopFuture<Channel> { |
| 133 | + return connect0(ipProtocol: ipProtocol) { |
| 134 | + return try SocketAddress.makeAddressResolvingHost(host, port: 0) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private func connect0(ipProtocol: NIOIPProtocol, _ makeSocketAddress: () throws -> SocketAddress) -> EventLoopFuture<Channel> { |
| 139 | + let address: SocketAddress |
| 140 | + do { |
| 141 | + address = try makeSocketAddress() |
| 142 | + } catch { |
| 143 | + return group.next().makeFailedFuture(error) |
| 144 | + } |
| 145 | + func makeChannel(_ eventLoop: SelectableEventLoop) throws -> DatagramChannel { |
| 146 | + return try DatagramChannel(eventLoop: eventLoop, |
| 147 | + protocolFamily: address.protocol, |
| 148 | + protocolSubtype: .init(ipProtocol), |
| 149 | + socketType: .raw) |
| 150 | + } |
| 151 | + return withNewChannel(makeChannel: makeChannel) { (eventLoop, channel) in |
| 152 | + channel.register().flatMap { |
| 153 | + channel.connect(to: address) |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private func withNewChannel(makeChannel: (_ eventLoop: SelectableEventLoop) throws -> DatagramChannel, _ bringup: @escaping (EventLoop, DatagramChannel) -> EventLoopFuture<Void>) -> EventLoopFuture<Channel> { |
| 159 | + let eventLoop = self.group.next() |
| 160 | + let channelInitializer = self.channelInitializer ?? { _ in eventLoop.makeSucceededFuture(()) } |
| 161 | + let channelOptions = self._channelOptions |
| 162 | + |
| 163 | + let channel: DatagramChannel |
| 164 | + do { |
| 165 | + channel = try makeChannel(eventLoop as! SelectableEventLoop) |
| 166 | + } catch { |
| 167 | + return eventLoop.makeFailedFuture(error) |
| 168 | + } |
| 169 | + |
| 170 | + func setupChannel() -> EventLoopFuture<Channel> { |
| 171 | + eventLoop.assertInEventLoop() |
| 172 | + return channelOptions.applyAllChannelOptions(to: channel).flatMap { |
| 173 | + channelInitializer(channel) |
| 174 | + }.flatMap { |
| 175 | + eventLoop.assertInEventLoop() |
| 176 | + return bringup(eventLoop, channel) |
| 177 | + }.map { |
| 178 | + channel |
| 179 | + }.flatMapError { error in |
| 180 | + eventLoop.makeFailedFuture(error) |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + if eventLoop.inEventLoop { |
| 185 | + return setupChannel() |
| 186 | + } else { |
| 187 | + return eventLoop.flatSubmit { |
| 188 | + setupChannel() |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +#if swift(>=5.6) |
| 195 | +@available(*, unavailable) |
| 196 | +extension NIORawSocketBootstrap: Sendable {} |
| 197 | +#endif |
0 commit comments