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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
|
//===----------------------------------------------------------------------===//
//
// 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(Windows)
import ucrt
import let WinSDK.INVALID_SOCKET
import let WinSDK.IPPROTO_IP
import let WinSDK.IPPROTO_IPV6
import let WinSDK.IPPROTO_TCP
import let WinSDK.IP_ADD_MEMBERSHIP
import let WinSDK.IP_DROP_MEMBERSHIP
import let WinSDK.IP_MULTICAST_IF
import let WinSDK.IP_MULTICAST_LOOP
import let WinSDK.IP_MULTICAST_TTL
import let WinSDK.IP_RECVTOS
import let WinSDK.IPV6_JOIN_GROUP
import let WinSDK.IPV6_LEAVE_GROUP
import let WinSDK.IPV6_MULTICAST_HOPS
import let WinSDK.IPV6_MULTICAST_IF
import let WinSDK.IPV6_MULTICAST_LOOP
import let WinSDK.IPV6_RECVTCLASS
import let WinSDK.IPV6_V6ONLY
import let WinSDK.AF_INET
import let WinSDK.AF_INET6
import let WinSDK.AF_UNIX
import let WinSDK.PF_INET
import let WinSDK.PF_INET6
import let WinSDK.PF_UNIX
import let WinSDK.SOCK_DGRAM
import let WinSDK.SOCK_STREAM
import let WinSDK.SO_ERROR
import let WinSDK.SO_KEEPALIVE
import let WinSDK.SO_LINGER
import let WinSDK.SO_RCVBUF
import let WinSDK.SO_RCVTIMEO
import let WinSDK.SO_REUSEADDR
import let WinSDK.SOL_SOCKET
import let WinSDK.TCP_NODELAY
import struct WinSDK.SOCKET
import struct WinSDK.WSACMSGHDR
import struct WinSDK.WSAMSG
import struct WinSDK.socklen_t
internal typealias msghdr = WSAMSG
internal typealias cmsghdr = WSACMSGHDR
#endif
protocol _SocketShutdownProtocol {
var cValue: CInt { get }
}
internal enum Shutdown: _SocketShutdownProtocol {
case RD
case WR
case RDWR
}
public enum NIOBSDSocket {
#if os(Windows)
public typealias Handle = SOCKET
#else
public typealias Handle = CInt
#endif
#if os(Windows)
internal static let invalidHandle: Handle = INVALID_SOCKET
#else
internal static let invalidHandle: Handle = -1
#endif
}
extension NIOBSDSocket {
/// Specifies the type of socket.
internal struct SocketType: RawRepresentable {
public typealias RawValue = CInt
public var rawValue: RawValue
public init(rawValue: RawValue) {
self.rawValue = rawValue
}
}
}
extension NIOBSDSocket.SocketType: Equatable {
}
extension NIOBSDSocket.SocketType: Hashable {
}
extension NIOBSDSocket {
/// Specifies the addressing scheme that the socket can use.
public struct AddressFamily: RawRepresentable {
public typealias RawValue = CInt
public var rawValue: RawValue
public init(rawValue: RawValue) {
self.rawValue = rawValue
}
}
}
extension NIOBSDSocket.AddressFamily: Equatable {
}
extension NIOBSDSocket.AddressFamily: Hashable {
}
extension NIOBSDSocket {
/// Specifies the type of protocol that the socket can use.
public struct ProtocolFamily: RawRepresentable {
public typealias RawValue = CInt
public var rawValue: RawValue
public init(rawValue: RawValue) {
self.rawValue = rawValue
}
}
}
extension NIOBSDSocket.ProtocolFamily: Equatable {
}
extension NIOBSDSocket.ProtocolFamily: Hashable {
}
extension NIOBSDSocket {
/// Defines socket option levels.
public struct OptionLevel: RawRepresentable {
public typealias RawValue = CInt
public var rawValue: RawValue
public init(rawValue: RawValue) {
self.rawValue = rawValue
}
}
}
extension NIOBSDSocket.OptionLevel: Equatable {
}
extension NIOBSDSocket.OptionLevel: Hashable {
}
extension NIOBSDSocket {
/// Defines configuration option names.
public struct Option: RawRepresentable {
public typealias RawValue = CInt
public var rawValue: RawValue
public init(rawValue: RawValue) {
self.rawValue = rawValue
}
}
}
extension NIOBSDSocket.Option: Equatable {
}
extension NIOBSDSocket.Option: Hashable {
}
// Address Family
extension NIOBSDSocket.AddressFamily {
/// Address for IP version 4.
public static let inet: NIOBSDSocket.AddressFamily =
NIOBSDSocket.AddressFamily(rawValue: AF_INET)
/// Address for IP version 6.
public static let inet6: NIOBSDSocket.AddressFamily =
NIOBSDSocket.AddressFamily(rawValue: AF_INET6)
/// Unix local to host address.
public static let unix: NIOBSDSocket.AddressFamily =
NIOBSDSocket.AddressFamily(rawValue: AF_UNIX)
}
// Protocol Family
extension NIOBSDSocket.ProtocolFamily {
/// IP network 4 protocol.
public static let inet: NIOBSDSocket.ProtocolFamily =
NIOBSDSocket.ProtocolFamily(rawValue: PF_INET)
/// IP network 6 protocol.
public static let inet6: NIOBSDSocket.ProtocolFamily =
NIOBSDSocket.ProtocolFamily(rawValue: PF_INET6)
/// UNIX local to the host.
public static let unix: NIOBSDSocket.ProtocolFamily =
NIOBSDSocket.ProtocolFamily(rawValue: PF_UNIX)
}
#if !os(Windows)
extension NIOBSDSocket.ProtocolFamily {
/// UNIX local to the host, alias for `PF_UNIX` (`.unix`)
public static let local: NIOBSDSocket.ProtocolFamily =
NIOBSDSocket.ProtocolFamily(rawValue: PF_LOCAL)
}
#endif
// Socket Types
extension NIOBSDSocket.SocketType {
/// Supports datagrams, which are connectionless, unreliable messages of a
/// fixed (typically small) maximum length.
#if os(Linux)
internal static let datagram: NIOBSDSocket.SocketType =
NIOBSDSocket.SocketType(rawValue: CInt(SOCK_DGRAM.rawValue))
#else
internal static let datagram: NIOBSDSocket.SocketType =
NIOBSDSocket.SocketType(rawValue: SOCK_DGRAM)
#endif
/// Supports reliable, two-way, connection-based byte streams without
/// duplication of data and without preservation of boundaries.
#if os(Linux)
internal static let stream: NIOBSDSocket.SocketType =
NIOBSDSocket.SocketType(rawValue: CInt(SOCK_STREAM.rawValue))
#else
internal static let stream: NIOBSDSocket.SocketType =
NIOBSDSocket.SocketType(rawValue: SOCK_STREAM)
#endif
}
// Option Level
extension NIOBSDSocket.OptionLevel {
/// Socket options that apply only to IP sockets.
#if os(Linux) || os(Android)
public static let ip: NIOBSDSocket.OptionLevel =
NIOBSDSocket.OptionLevel(rawValue: CInt(IPPROTO_IP))
#else
public static let ip: NIOBSDSocket.OptionLevel =
NIOBSDSocket.OptionLevel(rawValue: IPPROTO_IP)
#endif
/// Socket options that apply only to IPv6 sockets.
#if os(Linux) || os(Android)
public static let ipv6: NIOBSDSocket.OptionLevel =
NIOBSDSocket.OptionLevel(rawValue: CInt(IPPROTO_IPV6))
#elseif os(Windows)
public static let ipv6: NIOBSDSocket.OptionLevel =
NIOBSDSocket.OptionLevel(rawValue: IPPROTO_IPV6.rawValue)
#else
public static let ipv6: NIOBSDSocket.OptionLevel =
NIOBSDSocket.OptionLevel(rawValue: IPPROTO_IPV6)
#endif
/// Socket options that apply only to TCP sockets.
#if os(Linux) || os(Android)
public static let tcp: NIOBSDSocket.OptionLevel =
NIOBSDSocket.OptionLevel(rawValue: CInt(IPPROTO_TCP))
#elseif os(Windows)
public static let tcp: NIOBSDSocket.OptionLevel =
NIOBSDSocket.OptionLevel(rawValue: IPPROTO_TCP.rawValue)
#else
public static let tcp: NIOBSDSocket.OptionLevel =
NIOBSDSocket.OptionLevel(rawValue: IPPROTO_TCP)
#endif
/// Socket options that apply to all sockets.
public static let socket: NIOBSDSocket.OptionLevel =
NIOBSDSocket.OptionLevel(rawValue: SOL_SOCKET)
}
// IPv4 Options
extension NIOBSDSocket.Option {
/// Add a multicast group membership.
public static let ip_add_membership: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: IP_ADD_MEMBERSHIP)
/// Drop a multicast group membership.
public static let ip_drop_membership: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: IP_DROP_MEMBERSHIP)
/// Set the interface for outgoing multicast packets.
public static let ip_multicast_if: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: IP_MULTICAST_IF)
/// Control multicast loopback.
public static let ip_multicast_loop: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: IP_MULTICAST_LOOP)
/// Control multicast time-to-live.
public static let ip_multicast_ttl: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: IP_MULTICAST_TTL)
/// Request that we are passed type of service details when receiving
/// datagrams.
///
/// Not public as the way to request this is to use
/// `ChannelOptions.explicitCongestionNotification` which works for both
/// IPv4 and IPv6.
static let ip_recv_tos: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: IP_RECVTOS)
/// Request that we are passed destination address and the receiving interface index when
/// receiving datagrams.
///
/// This option is not public as the way to request this is to use
/// `ChannelOptions.receivePacketInfo` which works for both
/// IPv4 and IPv6.
static let ip_recv_pktinfo: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: Posix.IP_RECVPKTINFO)
}
// IPv6 Options
extension NIOBSDSocket.Option {
/// Add an IPv6 group membership.
public static let ipv6_join_group: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: IPV6_JOIN_GROUP)
/// Drop an IPv6 group membership.
public static let ipv6_leave_group: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: IPV6_LEAVE_GROUP)
/// Specify the maximum number of router hops for an IPv6 packet.
public static let ipv6_multicast_hops: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: IPV6_MULTICAST_HOPS)
/// Set the interface for outgoing multicast packets.
public static let ipv6_multicast_if: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: IPV6_MULTICAST_IF)
/// Control multicast loopback.
public static let ipv6_multicast_loop: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: IPV6_MULTICAST_LOOP)
/// Indicates if a socket created for the `AF_INET6` address family is
/// restricted to IPv6 only.
public static let ipv6_v6only: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: IPV6_V6ONLY)
/// Request that we are passed traffic class details when receiving
/// datagrams.
///
/// Not public as the way to request this is to use
/// `ChannelOptions.explicitCongestionNotification` which works for both
/// IPv4 and IPv6.
static let ipv6_recv_tclass: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: IPV6_RECVTCLASS)
/// Request that we are passed destination address and the receiving interface index when
/// receiving datagrams.
///
/// This option is not public as the way to request this is to use
/// `ChannelOptions.receivePacketInfo` which works for both
/// IPv4 and IPv6.
static let ipv6_recv_pktinfo: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: Posix.IPV6_RECVPKTINFO)
}
// TCP Options
extension NIOBSDSocket.Option {
/// Disables the Nagle algorithm for send coalescing.
public static let tcp_nodelay: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: TCP_NODELAY)
}
#if os(Linux) || os(FreeBSD) || os(Android)
extension NIOBSDSocket.Option {
/// Get information about the TCP connection.
public static let tcp_info: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: TCP_INFO)
}
#endif
#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
extension NIOBSDSocket.Option {
/// Get information about the TCP connection.
public static let tcp_connection_info: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: TCP_CONNECTION_INFO)
}
#endif
// Socket Options
extension NIOBSDSocket.Option {
/// Get the error status and clear.
public static let so_error: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: SO_ERROR)
/// Use keep-alives.
public static let so_keepalive: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: SO_KEEPALIVE)
/// Linger on close if unsent data is present.
public static let so_linger: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: SO_LINGER)
/// Specifies the total per-socket buffer space reserved for receives.
public static let so_rcvbuf: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: SO_RCVBUF)
/// Specifies the receive timeout.
public static let so_rcvtimeo: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: SO_RCVTIMEO)
/// Allows the socket to be bound to an address that is already in use.
public static let so_reuseaddr: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: SO_REUSEADDR)
}
#if !os(Windows)
extension NIOBSDSocket.Option {
/// Indicate when to generate timestamps.
public static let so_timestamp: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: SO_TIMESTAMP)
}
#endif
/// The requested UDS path exists and has wrong type (not a socket).
public struct UnixDomainSocketPathWrongType: Error {}
/// This protocol defines the methods that are expected to be found on
/// `NIOBSDSocket`. While defined as a protocol there is no expectation that any
/// object other than `NIOBSDSocket` will implement this protocol: instead, this
/// protocol acts as a reference for what new supported operating systems must
/// implement.
protocol _BSDSocketProtocol {
static func accept(socket s: NIOBSDSocket.Handle,
address addr: UnsafeMutablePointer<sockaddr>?,
address_len addrlen: UnsafeMutablePointer<socklen_t>?) throws -> NIOBSDSocket.Handle?
static func bind(socket s: NIOBSDSocket.Handle,
address addr: UnsafePointer<sockaddr>,
address_len namelen: socklen_t) throws
static func close(socket s: NIOBSDSocket.Handle) throws
static func connect(socket s: NIOBSDSocket.Handle,
address name: UnsafePointer<sockaddr>,
address_len namelen: socklen_t) throws -> Bool
static func getpeername(socket s: NIOBSDSocket.Handle,
address name: UnsafeMutablePointer<sockaddr>,
address_len namelen: UnsafeMutablePointer<socklen_t>) throws
static func getsockname(socket s: NIOBSDSocket.Handle,
address name: UnsafeMutablePointer<sockaddr>,
address_len namelen: UnsafeMutablePointer<socklen_t>) throws
static func getsockopt(socket: NIOBSDSocket.Handle,
level: NIOBSDSocket.OptionLevel,
option_name optname: NIOBSDSocket.Option,
option_value optval: UnsafeMutableRawPointer,
option_len optlen: UnsafeMutablePointer<socklen_t>) throws
static func listen(socket s: NIOBSDSocket.Handle, backlog: CInt) throws
static func recv(socket s: NIOBSDSocket.Handle,
buffer buf: UnsafeMutableRawPointer,
length len: size_t) throws -> IOResult<size_t>
// NOTE: this should return a `ssize_t`, however, that is not a standard
// type, and defining that type is difficult. Opt to return a `size_t`
// which is the same size, but is unsigned.
static func recvmsg(socket: NIOBSDSocket.Handle,
msgHdr: UnsafeMutablePointer<msghdr>, flags: CInt)
throws -> IOResult<size_t>
// NOTE: this should return a `ssize_t`, however, that is not a standard
// type, and defining that type is difficult. Opt to return a `size_t`
// which is the same size, but is unsigned.
static func sendmsg(socket: NIOBSDSocket.Handle,
msgHdr: UnsafePointer<msghdr>, flags: CInt)
throws -> IOResult<size_t>
static func send(socket s: NIOBSDSocket.Handle,
buffer buf: UnsafeRawPointer,
length len: size_t) throws -> IOResult<size_t>
static func setsockopt(socket: NIOBSDSocket.Handle,
level: NIOBSDSocket.OptionLevel,
option_name optname: NIOBSDSocket.Option,
option_value optval: UnsafeRawPointer,
option_len optlen: socklen_t) throws
static func shutdown(socket: NIOBSDSocket.Handle, how: Shutdown) throws
static func socket(domain af: NIOBSDSocket.ProtocolFamily,
type: NIOBSDSocket.SocketType,
`protocol`: CInt) throws -> NIOBSDSocket.Handle
static func recvmmsg(socket: NIOBSDSocket.Handle,
msgvec: UnsafeMutablePointer<MMsgHdr>,
vlen: CUnsignedInt,
flags: CInt,
timeout: UnsafeMutablePointer<timespec>?) throws -> IOResult<Int>
static func sendmmsg(socket: NIOBSDSocket.Handle,
msgvec: UnsafeMutablePointer<MMsgHdr>,
vlen: CUnsignedInt,
flags: CInt) throws -> IOResult<Int>
// NOTE: this should return a `ssize_t`, however, that is not a standard
// type, and defining that type is difficult. Opt to return a `size_t`
// which is the same size, but is unsigned.
static func pread(socket: NIOBSDSocket.Handle,
pointer: UnsafeMutableRawPointer,
size: size_t,
offset: off_t) throws -> IOResult<size_t>
// NOTE: this should return a `ssize_t`, however, that is not a standard
// type, and defining that type is difficult. Opt to return a `size_t`
// which is the same size, but is unsigned.
static func pwrite(socket: NIOBSDSocket.Handle,
pointer: UnsafeRawPointer,
size: size_t,
offset: off_t) throws -> IOResult<size_t>
#if !os(Windows)
// NOTE: We do not support this on Windows as WSAPoll behaves differently
// from poll with reporting of failed connections (Connect Report 309411),
// which recommended that you use NetAPI instead.
//
// This is safe to exclude as this is a testing-only API.
static func poll(fds: UnsafeMutablePointer<pollfd>, nfds: nfds_t,
timeout: CInt) throws -> CInt
#endif
static func inet_ntop(af family: NIOBSDSocket.AddressFamily,
src addr: UnsafeRawPointer,
dst dstBuf: UnsafeMutablePointer<CChar>,
size dstSize: socklen_t) throws -> UnsafePointer<CChar>?
static func inet_pton(af family: NIOBSDSocket.AddressFamily,
src description: UnsafePointer<CChar>,
dst address: UnsafeMutableRawPointer) throws
static func sendfile(socket s: NIOBSDSocket.Handle,
fd: CInt,
offset: off_t,
len: off_t) throws -> IOResult<Int>
// MARK: non-BSD APIs added by NIO
static func setNonBlocking(socket: NIOBSDSocket.Handle) throws
static func cleanupUnixDomainSocket(atPath path: String) throws
}
/// If this extension is hitting a compile error, your platform is missing one
/// of the functions defined above!
extension NIOBSDSocket: _BSDSocketProtocol { }
/// This protocol defines the methods that are expected to be found on
/// `NIOBSDControlMessage`. While defined as a protocol there is no expectation
/// that any object other than `NIOBSDControlMessage` will implement this
/// protocol: instead, this protocol acts as a reference for what new supported
/// operating systems must implement.
protocol _BSDSocketControlMessageProtocol {
static func firstHeader(inside msghdr: UnsafePointer<msghdr>)
-> UnsafeMutablePointer<cmsghdr>?
static func nextHeader(inside msghdr: UnsafeMutablePointer<msghdr>,
after: UnsafeMutablePointer<cmsghdr>)
-> UnsafeMutablePointer<cmsghdr>?
static func data(for header: UnsafePointer<cmsghdr>)
-> UnsafeRawBufferPointer?
static func data(for header: UnsafeMutablePointer<cmsghdr>)
-> UnsafeMutableRawBufferPointer?
static func length(payloadSize: size_t) -> size_t
static func space(payloadSize: size_t) -> size_t
}
/// If this extension is hitting a compile error, your platform is missing one
/// of the functions defined above!
enum NIOBSDSocketControlMessage: _BSDSocketControlMessageProtocol { }
|