1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
import CNIODarwin
#elseif os(Linux) || os(FreeBSD) || os(Android)
import CNIOLinux
#endif
/// Memory for use as `cmsghdr` and associated data.
/// Supports multiple messages each with enough storage for multiple `cmsghdr`
struct UnsafeControlMessageStorage: Collection {
let bytesPerMessage: Int
var buffer: UnsafeMutableRawBufferPointer
/// Initialise which includes allocating memory
/// parameter:
/// - bytesPerMessage: How many bytes have been allocated for each supported message.
/// - buffer: The memory allocated to use for control messages.
private init(bytesPerMessage: Int, buffer: UnsafeMutableRawBufferPointer) {
self.bytesPerMessage = bytesPerMessage
self.buffer = buffer
}
/// Allocate new memory - Caller must call `deallocate` when no longer required.
/// parameter:
/// - msghdrCount: How many `msghdr` structures will be fed from this buffer - we assume 4 Int32 cmsgs for each.
static func allocate(msghdrCount: Int) -> UnsafeControlMessageStorage {
// Guess that 4 Int32 payload messages is enough for anyone.
let bytesPerMessage = NIOBSDSocketControlMessage.space(payloadSize: MemoryLayout<Int32>.stride) * 4
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: bytesPerMessage * msghdrCount,
alignment: MemoryLayout<cmsghdr>.alignment)
return UnsafeControlMessageStorage(bytesPerMessage: bytesPerMessage, buffer: buffer)
}
mutating func deallocate() {
self.buffer.deallocate()
self.buffer = UnsafeMutableRawBufferPointer(start: UnsafeMutableRawPointer(bitPattern: 0x7eadbeef), count: 0)
}
/// Get the part of the buffer for use with a message.
public subscript(position: Int) -> UnsafeMutableRawBufferPointer {
return UnsafeMutableRawBufferPointer(
fastRebase: self.buffer[(position * self.bytesPerMessage)..<((position+1) * self.bytesPerMessage)])
}
var startIndex: Int { return 0 }
var endIndex: Int { return self.buffer.count / self.bytesPerMessage }
func index(after: Int) -> Int {
return after + 1
}
}
/// Representation of a `cmsghdr` and associated data.
/// Unsafe as captures pointers and must not escape the scope where those pointers are valid.
struct UnsafeControlMessage {
var level: CInt
var type: CInt
var data: UnsafeRawBufferPointer?
}
/// Collection representation of `cmsghdr` structures and associated data from `recvmsg`
/// Unsafe as captures pointers held in msghdr structure which must not escape scope of validity.
struct UnsafeControlMessageCollection {
private var messageHeader: msghdr
init(messageHeader: msghdr) {
self.messageHeader = messageHeader
}
}
// Add the `Collection` functionality to UnsafeControlMessageCollection.
extension UnsafeControlMessageCollection: Collection {
typealias Element = UnsafeControlMessage
struct Index: Equatable, Comparable {
fileprivate var cmsgPointer: UnsafeMutablePointer<cmsghdr>?
static func < (lhs: UnsafeControlMessageCollection.Index,
rhs: UnsafeControlMessageCollection.Index) -> Bool {
// nil is high, as that's the end of the collection.
switch (lhs.cmsgPointer, rhs.cmsgPointer) {
case (.some(let lhs), .some(let rhs)):
return lhs < rhs
case (.some, .none):
return true
case (.none, .some), (.none, .none):
return false
}
}
fileprivate init(cmsgPointer: UnsafeMutablePointer<cmsghdr>?) {
self.cmsgPointer = cmsgPointer
}
}
var startIndex: Index {
var messageHeader = self.messageHeader
return withUnsafePointer(to: &messageHeader) { messageHeaderPtr in
let firstCMsg = NIOBSDSocketControlMessage.firstHeader(inside: messageHeaderPtr)
return Index(cmsgPointer: firstCMsg)
}
}
var endIndex: Index { return Index(cmsgPointer: nil) }
func index(after: Index) -> Index {
var msgHdr = messageHeader
return withUnsafeMutablePointer(to: &msgHdr) { messageHeaderPtr in
return Index(cmsgPointer: NIOBSDSocketControlMessage.nextHeader(inside: messageHeaderPtr,
after: after.cmsgPointer!))
}
}
public subscript(position: Index) -> Element {
let cmsg = position.cmsgPointer!
return UnsafeControlMessage(level: cmsg.pointee.cmsg_level,
type: cmsg.pointee.cmsg_type,
data: NIOBSDSocketControlMessage.data(for: cmsg))
}
}
/// Small struct to link a buffer used for control bytes and the processing of those bytes.
struct UnsafeReceivedControlBytes {
var controlBytesBuffer: UnsafeMutableRawBufferPointer
/// Set when a message is received which is using the controlBytesBuffer - the lifetime will be tied to that of `controlBytesBuffer`
var receivedControlMessages: UnsafeControlMessageCollection?
init(controlBytesBuffer: UnsafeMutableRawBufferPointer) {
self.controlBytesBuffer = controlBytesBuffer
}
}
/// Extract information from a collection of control messages.
struct ControlMessageParser {
var ecnValue: NIOExplicitCongestionNotificationState = .transportNotCapable // Default
var packetInfo: NIOPacketInfo? = nil
init(parsing controlMessagesReceived: UnsafeControlMessageCollection) {
for controlMessage in controlMessagesReceived {
self.receiveMessage(controlMessage)
}
}
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
private static let ipv4TosType = IP_RECVTOS
#else
private static let ipv4TosType = IP_TOS // Linux
#endif
static func _readCInt(data: UnsafeRawBufferPointer) -> CInt {
assert(data.count == MemoryLayout<CInt>.size)
precondition(data.count >= MemoryLayout<CInt>.size)
var readValue = CInt(0)
withUnsafeMutableBytes(of: &readValue) { valuePtr in
valuePtr.copyMemory(from: data)
}
return readValue
}
private mutating func receiveMessage(_ controlMessage: UnsafeControlMessage) {
if controlMessage.level == IPPROTO_IP {
self.receiveIPv4Message(controlMessage)
} else if controlMessage.level == IPPROTO_IPV6 {
self.receiveIPv6Message(controlMessage)
}
}
private mutating func receiveIPv4Message(_ controlMessage: UnsafeControlMessage) {
if controlMessage.type == ControlMessageParser.ipv4TosType {
if let data = controlMessage.data {
assert(data.count == 1)
precondition(data.count >= 1)
let readValue = CInt(data[0])
self.ecnValue = .init(receivedValue: readValue)
}
} else if controlMessage.type == Posix.IP_PKTINFO {
if let data = controlMessage.data {
let info = data.load(as: in_pktinfo.self)
var addr = sockaddr_in()
addr.sin_family = sa_family_t(NIOBSDSocket.AddressFamily.inet.rawValue)
addr.sin_port = in_port_t(0)
addr.sin_addr = info.ipi_addr
self.packetInfo = NIOPacketInfo(destinationAddress: SocketAddress(addr, host: ""),
interfaceIndex: Int(info.ipi_ifindex))
}
}
}
private mutating func receiveIPv6Message(_ controlMessage: UnsafeControlMessage) {
if controlMessage.type == IPV6_TCLASS {
if let data = controlMessage.data {
let readValue = ControlMessageParser._readCInt(data: data)
self.ecnValue = .init(receivedValue: readValue)
}
} else if controlMessage.type == Posix.IPV6_PKTINFO {
if let data = controlMessage.data {
let info = data.load(as: in6_pktinfo.self)
var addr = sockaddr_in6()
addr.sin6_family = sa_family_t(NIOBSDSocket.AddressFamily.inet6.rawValue)
addr.sin6_port = in_port_t(0)
addr.sin6_flowinfo = 0
addr.sin6_addr = info.ipi6_addr
addr.sin6_scope_id = 0
self.packetInfo = NIOPacketInfo(destinationAddress: SocketAddress(addr, host: ""),
interfaceIndex: Int(info.ipi6_ifindex))
}
}
}
}
extension NIOExplicitCongestionNotificationState {
/// Initialise a NIOExplicitCongestionNotificationState from a value received via either TCLASS or TOS cmsg.
init(receivedValue: CInt) {
switch receivedValue & Posix.IPTOS_ECN_MASK {
case Posix.IPTOS_ECN_ECT1:
self = .transportCapableFlag1
case Posix.IPTOS_ECN_ECT0:
self = .transportCapableFlag0
case Posix.IPTOS_ECN_CE:
self = .congestionExperienced
default:
self = .transportNotCapable
}
}
}
extension CInt {
/// Create a CInt encoding of ExplicitCongestionNotification suitable for sending in TCLASS or TOS cmsg.
init(ecnValue: NIOExplicitCongestionNotificationState) {
switch ecnValue {
case .transportNotCapable:
self = Posix.IPTOS_ECN_NOTECT
case .transportCapableFlag0:
self = Posix.IPTOS_ECN_ECT0
case .transportCapableFlag1:
self = Posix.IPTOS_ECN_ECT1
case .congestionExperienced:
self = Posix.IPTOS_ECN_CE
}
}
}
struct UnsafeOutboundControlBytes {
private var controlBytes: UnsafeMutableRawBufferPointer
private var writePosition: UnsafeMutableRawBufferPointer.Index
/// This structure must not outlive `controlBytes`
init(controlBytes: UnsafeMutableRawBufferPointer) {
self.controlBytes = controlBytes
self.writePosition = controlBytes.startIndex
}
mutating func appendControlMessage(level: CInt, type: CInt, payload: CInt) {
self.appendGenericControlMessage(level: level, type: type, payload: payload)
}
/// Appends a control message.
/// PayloadType needs to be trivial (eg CInt)
private mutating func appendGenericControlMessage<PayloadType>(level: CInt,
type: CInt,
payload: PayloadType) {
let writableBuffer = UnsafeMutableRawBufferPointer(fastRebase: self.controlBytes[writePosition...])
let requiredSize = NIOBSDSocketControlMessage.space(payloadSize: MemoryLayout.stride(ofValue: payload))
precondition(writableBuffer.count >= requiredSize, "Insufficient size for cmsghdr and data")
let bufferBase = writableBuffer.baseAddress!
// Binding to cmsghdr is safe here as this is the only place where we bind to non-Raw.
let cmsghdrPtr = bufferBase.bindMemory(to: cmsghdr.self, capacity: 1)
cmsghdrPtr.pointee.cmsg_level = level
cmsghdrPtr.pointee.cmsg_type = type
cmsghdrPtr.pointee.cmsg_len = .init(NIOBSDSocketControlMessage.length(payloadSize: MemoryLayout.size(ofValue: payload)))
let dataPointer = NIOBSDSocketControlMessage.data(for: cmsghdrPtr)!
precondition(dataPointer.count >= MemoryLayout<PayloadType>.stride)
dataPointer.storeBytes(of: payload, as: PayloadType.self)
self.writePosition += requiredSize
}
/// The result is only valid while this is valid.
var validControlBytes: UnsafeMutableRawBufferPointer {
if writePosition == 0 {
return UnsafeMutableRawBufferPointer(start: nil, count: 0)
}
return UnsafeMutableRawBufferPointer(fastRebase: self.controlBytes[0 ..< self.writePosition])
}
}
extension UnsafeOutboundControlBytes {
/// Add a message describing the explicit congestion state if requested in metadata and valid for this protocol.
/// Parameters:
/// - metadata: Metadata from the addressed envelope which will describe any desired state.
/// - protocolFamily: The type of protocol to encode for.
internal mutating func appendExplicitCongestionState(metadata: AddressedEnvelope<ByteBuffer>.Metadata?,
protocolFamily: NIOBSDSocket.ProtocolFamily?) {
guard let metadata = metadata else { return }
switch protocolFamily {
case .some(.inet):
self.appendControlMessage(level: .init(IPPROTO_IP),
type: IP_TOS,
payload: CInt(ecnValue: metadata.ecnState))
case .some(.inet6):
self.appendControlMessage(level: .init(IPPROTO_IPV6),
type: IPV6_TCLASS,
payload: CInt(ecnValue: metadata.ecnState))
default:
// Nothing to do - if we get here the user is probably making a mistake.
break
}
}
}
extension AddressedEnvelope.Metadata {
/// It's assumed the caller has checked that congestion information is required before calling.
internal init(from controlMessagesReceived: UnsafeControlMessageCollection) {
let controlMessageReceiver = ControlMessageParser(parsing: controlMessagesReceived)
self.init(ecnState: controlMessageReceiver.ecnValue, packetInfo: controlMessageReceiver.packetInfo)
}
}
|