Smaller fixups

This commit is contained in:
Franz Busch committed 2026-10-07 15:51:03 +02:00
1 parent b01fd78b91
commit fc55a43b0c
20 files changed
+207 -131

No files matched your search

+4 -3
View File
@@ -13,11 +13,12 @@ import SystemSockets
#if SYSTEM_PACKAGE_DARWIN
import Darwin
#elseif canImport(Glibc)
import Glibc
// Glibc declares `stdin`, `stdout` and `stderr` as mutable globals.
@preconcurrency import Glibc
#elseif canImport(Musl)
import Musl
@preconcurrency import Musl
#elseif canImport(Android)
import Android
@preconcurrency import Android
#else
#error("Unsupported Platform")
#endif
+1 -1
View File
@@ -10,7 +10,7 @@
import ArgumentParser
struct SystemSamples: ParsableCommand {
static var configuration = CommandConfiguration(
static let configuration = CommandConfiguration(
commandName: "system-samples",
abstract: "A collection of little programs exercising some System features.",
subcommands: [
@@ -12,13 +12,13 @@
#if SYSTEM_PACKAGE_DARWIN
import Darwin
#elseif canImport(Glibc)
@_implementationOnly import CSystem
import CSystem
import Glibc
#elseif canImport(Musl)
@_implementationOnly import CSystem
import CSystem
import Musl
#elseif canImport(Android)
@_implementationOnly import CSystem
import CSystem
import Android
#else
#error("Unsupported Platform")
@@ -26,7 +26,7 @@ import Android
import SystemPackage
@available(System 99, *)
@available(System 199, *)
extension CInterop {
/// Socket address structure.
///
@@ -12,13 +12,13 @@
#if SYSTEM_PACKAGE_DARWIN
import Darwin
#elseif canImport(Glibc)
@_implementationOnly import CSystem
import CSystem
import Glibc
#elseif canImport(Musl)
@_implementationOnly import CSystem
import CSystem
import Musl
#elseif canImport(Android)
@_implementationOnly import CSystem
import CSystem
import Android
#else
#error("Unsupported Platform")
@@ -14,10 +14,13 @@
#if SYSTEM_PACKAGE_DARWIN
import Darwin
#elseif canImport(Glibc)
import CSystem
import Glibc
#elseif canImport(Musl)
import CSystem
import Musl
#elseif canImport(Android)
import CSystem
import Android
#endif
@@ -12,13 +12,13 @@
#if SYSTEM_PACKAGE_DARWIN
import Darwin
#elseif canImport(Glibc)
@_implementationOnly import CSystem
import CSystem
import Glibc
#elseif canImport(Musl)
@_implementationOnly import CSystem
import CSystem
import Musl
#elseif canImport(Android)
@_implementationOnly import CSystem
import CSystem
import Android
#else
#error("Unsupported Platform")
@@ -10,13 +10,13 @@
#if SYSTEM_PACKAGE_DARWIN
import Darwin
#elseif canImport(Glibc)
@_implementationOnly import CSystem
import CSystem
import Glibc
#elseif canImport(Musl)
@_implementationOnly import CSystem
import CSystem
import Musl
#elseif canImport(Android)
@_implementationOnly import CSystem
import CSystem
import Android
#else
#error("Unsupported Platform")
@@ -24,7 +24,7 @@ import Android
import SystemPackage
@available(System 99, *)
@available(System 199, *)
extension SocketAddress {
@frozen
/// The address family identifier
@@ -90,7 +90,7 @@ extension SocketAddress {
}
}
@available(System 99, *)
@available(System 199, *)
extension SocketAddress.Family: CustomStringConvertible {
public var description: String {
switch self {
@@ -12,10 +12,13 @@ import SystemPackage
#if SYSTEM_PACKAGE_DARWIN
import Darwin
#elseif canImport(Glibc)
import CSystem
import Glibc
#elseif canImport(Musl)
import CSystem
import Musl
#elseif canImport(Android)
import CSystem
import Android
#else
#error("Unsupported Platform")
@@ -25,7 +28,7 @@ import Android
///
/// An IPv4 address consists of a 32-bit IP address and a 16-bit port number.
@frozen
@available(System 99, *)
@available(System 199, *)
public struct IPv4Address: Sendable, Equatable, Hashable {
/// The underlying C structure.
@usableFromInline
@@ -93,7 +96,7 @@ public struct IPv4Address: Sendable, Equatable, Hashable {
// MARK: - Properties
@available(System 99, *)
@available(System 199, *)
extension IPv4Address {
/// The port number in host byte order.
@_alwaysEmitIntoClient
@@ -114,7 +117,7 @@ extension IPv4Address {
// MARK: - Pointer Access
@available(System 99, *)
@available(System 199, *)
extension IPv4Address {
/// Calls the given closure with a pointer to the underlying sockaddr.
@_alwaysEmitIntoClient
@@ -131,7 +134,7 @@ extension IPv4Address {
// MARK: - CustomStringConvertible
@available(System 99, *)
@available(System 199, *)
extension IPv4Address: CustomStringConvertible {
public var description: String {
"\(addressString):\(port)"
@@ -140,7 +143,7 @@ extension IPv4Address: CustomStringConvertible {
// MARK: - ExpressibleByStringLiteral
@available(System 99, *)
@available(System 199, *)
extension IPv4Address: ExpressibleByStringLiteral {
/// Creates an IPv4 address from a string literal.
///
@@ -160,7 +163,7 @@ extension IPv4Address: ExpressibleByStringLiteral {
// MARK: - Equatable and Hashable
@available(System 99, *)
@available(System 199, *)
extension IPv4Address {
@_alwaysEmitIntoClient
public static func == (lhs: IPv4Address, rhs: IPv4Address) -> Bool {
@@ -179,7 +182,7 @@ extension IPv4Address {
// MARK: - SocketAddress Integration
@available(System 99, *)
@available(System 199, *)
extension SocketAddress {
/// Creates a socket address from an IPv4 address.
@_alwaysEmitIntoClient
@@ -12,10 +12,13 @@ import SystemPackage
#if SYSTEM_PACKAGE_DARWIN
import Darwin
#elseif canImport(Glibc)
import CSystem
import Glibc
#elseif canImport(Musl)
import CSystem
import Musl
#elseif canImport(Android)
import CSystem
import Android
#else
#error("Unsupported Platform")
@@ -26,7 +29,7 @@ import Android
/// An IPv6 address consists of a 128-bit IP address, a 16-bit port number,
/// a flow label, and a scope ID.
@frozen
@available(System 99, *)
@available(System 199, *)
public struct IPv6Address: Sendable, Hashable {
/// The underlying C structure.
@usableFromInline
@@ -94,7 +97,7 @@ public struct IPv6Address: Sendable, Hashable {
// MARK: - Properties
@available(System 99, *)
@available(System 199, *)
extension IPv6Address {
/// The port number in host byte order.
@_alwaysEmitIntoClient
@@ -127,7 +130,7 @@ extension IPv6Address {
// MARK: - Pointer Access
@available(System 99, *)
@available(System 199, *)
extension IPv6Address {
/// Calls the given closure with a pointer to the underlying sockaddr.
@_alwaysEmitIntoClient
@@ -144,7 +147,7 @@ extension IPv6Address {
// MARK: - CustomStringConvertible
@available(System 99, *)
@available(System 199, *)
extension IPv6Address: CustomStringConvertible {
public var description: String {
"[\(addressString)]:\(port)"
@@ -153,7 +156,7 @@ extension IPv6Address: CustomStringConvertible {
// MARK: - ExpressibleByStringLiteral
@available(System 99, *)
@available(System 199, *)
extension IPv6Address: ExpressibleByStringLiteral {
/// Creates an IPv6 address from a string literal.
///
@@ -186,7 +189,7 @@ extension IPv6Address: ExpressibleByStringLiteral {
// MARK: - Equatable and Hashable
@available(System 99, *)
@available(System 199, *)
extension IPv6Address {
@_alwaysEmitIntoClient
public static func == (lhs: IPv6Address, rhs: IPv6Address) -> Bool {
@@ -207,7 +210,7 @@ extension IPv6Address {
// MARK: - SocketAddress Integration
@available(System 99, *)
@available(System 199, *)
extension SocketAddress {
/// Creates a socket address from an IPv6 address.
@_alwaysEmitIntoClient
@@ -12,10 +12,13 @@ import SystemPackage
#if SYSTEM_PACKAGE_DARWIN
import Darwin
#elseif canImport(Glibc)
import CSystem
import Glibc
#elseif canImport(Musl)
import CSystem
import Musl
#elseif canImport(Android)
import CSystem
import Android
#else
#error("Unsupported Platform")
@@ -25,7 +28,7 @@ import Android
/// An error returned by address resolution functions.
@frozen
@available(System 99, *)
@available(System 199, *)
public struct AddressResolutionError: Error, Hashable, Sendable {
/// The error code returned by getaddrinfo or getnameinfo.
public let code: CInt
@@ -41,7 +44,7 @@ public struct AddressResolutionError: Error, Hashable, Sendable {
}
}
@available(System 99, *)
@available(System 199, *)
extension AddressResolutionError: CustomStringConvertible {
public var description: String {
"AddressResolutionError(\(code)): \(message)"
@@ -50,7 +53,7 @@ extension AddressResolutionError: CustomStringConvertible {
// MARK: - Resolution Hints
@available(System 99, *)
@available(System 199, *)
extension SocketAddress {
/// Hints for address resolution.
public struct ResolutionHints: Sendable {
@@ -128,7 +131,7 @@ extension SocketAddress {
// MARK: - Resolution Result
@available(System 99, *)
@available(System 199, *)
extension SocketAddress {
/// A result from address resolution.
public struct ResolvedAddress: Sendable {
@@ -165,7 +168,7 @@ extension SocketAddress {
// MARK: - Address Resolution
@available(System 99, *)
@available(System 199, *)
extension SocketAddress {
/// Resolves a hostname and service to a list of socket addresses.
///
@@ -250,7 +253,7 @@ extension SocketAddress {
// MARK: - Reverse Resolution
@available(System 99, *)
@available(System 199, *)
extension SocketAddress {
/// Flags for reverse resolution.
@frozen
@@ -12,10 +12,13 @@ import SystemPackage
#if SYSTEM_PACKAGE_DARWIN
import Darwin
#elseif canImport(Glibc)
import CSystem
import Glibc
#elseif canImport(Musl)
import CSystem
import Musl
#elseif canImport(Android)
import CSystem
import Android
#else
#error("Unsupported Platform")
@@ -25,7 +28,7 @@ import Android
///
/// Unix domain socket addresses use a filesystem path to identify the socket.
@frozen
@available(System 99, *)
@available(System 199, *)
public struct UnixAddress: Sendable, Equatable, Hashable {
/// The underlying C structure.
@usableFromInline
@@ -78,7 +81,7 @@ public struct UnixAddress: Sendable, Equatable, Hashable {
// MARK: - Properties
@available(System 99, *)
@available(System 199, *)
extension UnixAddress {
/// The path as a string.
public var path: String {
@@ -93,7 +96,7 @@ extension UnixAddress {
// MARK: - Pointer Access
@available(System 99, *)
@available(System 199, *)
extension UnixAddress {
/// Calls the given closure with a pointer to the underlying sockaddr.
@_alwaysEmitIntoClient
@@ -117,7 +120,7 @@ extension UnixAddress {
// MARK: - CustomStringConvertible
@available(System 99, *)
@available(System 199, *)
extension UnixAddress: CustomStringConvertible {
public var description: String {
path
@@ -126,7 +129,7 @@ extension UnixAddress: CustomStringConvertible {
// MARK: - Equatable and Hashable
@available(System 99, *)
@available(System 199, *)
extension UnixAddress {
@_alwaysEmitIntoClient
public static func == (lhs: UnixAddress, rhs: UnixAddress) -> Bool {
@@ -149,7 +152,7 @@ extension UnixAddress {
// MARK: - SocketAddress Integration
@available(System 99, *)
@available(System 199, *)
extension SocketAddress {
/// Creates a socket address from a Unix address.
@_alwaysEmitIntoClient
@@ -12,10 +12,13 @@ import SystemPackage
#if SYSTEM_PACKAGE_DARWIN
import Darwin
#elseif canImport(Glibc)
import CSystem
import Glibc
#elseif canImport(Musl)
import CSystem
import Musl
#elseif canImport(Android)
import CSystem
import Android
#else
#error("Unsupported Platform")
@@ -44,7 +47,7 @@ import Android
/// `SocketAddress` uses `sockaddr_storage` internally, which is large enough
/// to hold any socket address type.
@frozen
@available(System 99, *)
@available(System 199, *)
public struct SocketAddress: Sendable {
/// The underlying storage using sockaddr_storage.
@usableFromInline
@@ -93,7 +96,7 @@ public struct SocketAddress: Sendable {
// MARK: - Properties
@available(System 99, *)
@available(System 199, *)
extension SocketAddress {
/// The address family of this socket address.
@_alwaysEmitIntoClient
@@ -119,7 +122,7 @@ extension SocketAddress {
// MARK: - Accessing Raw Bytes
@available(System 99, *)
@available(System 199, *)
extension SocketAddress {
/// Calls the given closure with a pointer to the underlying socket address.
@_alwaysEmitIntoClient
@@ -168,7 +171,7 @@ extension SocketAddress {
// MARK: - Equatable and Hashable
@available(System 99, *)
@available(System 199, *)
extension SocketAddress: Equatable {
@_alwaysEmitIntoClient
public static func == (lhs: SocketAddress, rhs: SocketAddress) -> Bool {
@@ -181,7 +184,7 @@ extension SocketAddress: Equatable {
}
}
@available(System 99, *)
@available(System 199, *)
extension SocketAddress: Hashable {
@_alwaysEmitIntoClient
public func hash(into hasher: inout Hasher) {
@@ -194,7 +197,7 @@ extension SocketAddress: Hashable {
// MARK: - CustomStringConvertible
@available(System 99, *)
@available(System 199, *)
extension SocketAddress: CustomStringConvertible {
public var description: String {
if isEmpty {
@@ -12,15 +12,28 @@ import SystemPackage
#if SYSTEM_PACKAGE_DARWIN
import Darwin
#elseif canImport(Glibc)
import CSystem
import Glibc
#elseif canImport(Musl)
import CSystem
import Musl
#elseif canImport(Android)
import CSystem
import Android
#else
#error("Unsupported Platform")
#endif
// Glibc imports the socket types as a C enum, every other platform imports
// them as plain integers.
#if canImport(Glibc)
@_alwaysEmitIntoClient
internal func _socketType(_ type: __socket_type) -> CInt { CInt(type.rawValue) }
#else
@_alwaysEmitIntoClient
internal func _socketType(_ type: CInt) -> CInt { type }
#endif
/// A socket descriptor.
///
/// A `SocketDescriptor` is a type-safe wrapper around an operating system
@@ -30,7 +43,7 @@ import Android
/// socket rather than a general file descriptor, enabling socket-specific
/// operations like `bind`, `listen`, `accept`, and `connect`.
@frozen
@available(System 99, *)
@available(System 199, *)
public struct SocketDescriptor: RawRepresentable, Hashable, Codable, Sendable {
/// The raw C socket handle.
@_alwaysEmitIntoClient
@@ -41,7 +54,7 @@ public struct SocketDescriptor: RawRepresentable, Hashable, Codable, Sendable {
public init(rawValue: CInt) { self.rawValue = rawValue }
}
@available(System 99, *)
@available(System 199, *)
extension SocketDescriptor {
/// The file descriptor for this socket.
///
@@ -64,7 +77,7 @@ extension SocketDescriptor {
// MARK: - Domain (Protocol Family)
@available(System 99, *)
@available(System 199, *)
extension SocketDescriptor {
/// Communications domain, identifying the protocol family.
///
@@ -156,7 +169,7 @@ extension SocketDescriptor {
// MARK: - Connection Type (Socket Type)
@available(System 99, *)
@available(System 199, *)
extension SocketDescriptor {
/// The socket type, specifying the semantics of communication.
///
@@ -178,26 +191,26 @@ extension SocketDescriptor {
///
/// The corresponding C constant is `SOCK_STREAM`.
@_alwaysEmitIntoClient
public static var stream: ConnectionType { ConnectionType(SOCK_STREAM) }
public static var stream: ConnectionType { ConnectionType(_socketType(SOCK_STREAM)) }
/// Connectionless, unreliable datagrams of a fixed maximum length.
///
/// The corresponding C constant is `SOCK_DGRAM`.
@_alwaysEmitIntoClient
public static var datagram: ConnectionType { ConnectionType(SOCK_DGRAM) }
public static var datagram: ConnectionType { ConnectionType(_socketType(SOCK_DGRAM)) }
/// Raw network protocol access.
///
/// The corresponding C constant is `SOCK_RAW`.
@_alwaysEmitIntoClient
public static var raw: ConnectionType { ConnectionType(SOCK_RAW) }
public static var raw: ConnectionType { ConnectionType(_socketType(SOCK_RAW)) }
/// Reliably-delivered message.
///
/// The corresponding C constant is `SOCK_RDM`.
@_alwaysEmitIntoClient
public static var reliablyDeliveredMessage: ConnectionType {
ConnectionType(SOCK_RDM)
ConnectionType(_socketType(SOCK_RDM))
}
/// Sequenced packet stream.
@@ -205,7 +218,7 @@ extension SocketDescriptor {
/// The corresponding C constant is `SOCK_SEQPACKET`.
@_alwaysEmitIntoClient
public static var sequencedPacketStream: ConnectionType {
ConnectionType(SOCK_SEQPACKET)
ConnectionType(_socketType(SOCK_SEQPACKET))
}
public var description: String {
@@ -223,7 +236,7 @@ extension SocketDescriptor {
// MARK: - Protocol ID
@available(System 99, *)
@available(System 199, *)
extension SocketDescriptor {
/// Identifies a particular protocol to use for communication.
///
@@ -248,37 +261,44 @@ extension SocketDescriptor {
///
/// The corresponding C constant is `IPPROTO_IP`.
@_alwaysEmitIntoClient
public static var ip: ProtocolID { Self(IPPROTO_IP) }
public static var ip: ProtocolID { Self(CInt(IPPROTO_IP)) }
/// Transmission Control Protocol (TCP).
///
/// The corresponding C constant is `IPPROTO_TCP`.
@_alwaysEmitIntoClient
public static var tcp: ProtocolID { Self(IPPROTO_TCP) }
public static var tcp: ProtocolID { Self(CInt(IPPROTO_TCP)) }
/// User Datagram Protocol (UDP).
///
/// The corresponding C constant is `IPPROTO_UDP`.
@_alwaysEmitIntoClient
public static var udp: ProtocolID { Self(IPPROTO_UDP) }
public static var udp: ProtocolID { Self(CInt(IPPROTO_UDP)) }
/// IPv4 encapsulation.
///
/// The corresponding C constant is `IPPROTO_IPV4`.
@_alwaysEmitIntoClient
public static var ipv4: ProtocolID { Self(IPPROTO_IPV4) }
public static var ipv4: ProtocolID {
#if canImport(Glibc) || canImport(Musl) || canImport(Android)
// Linux names this protocol `IPPROTO_IPIP`.
Self(CInt(IPPROTO_IPIP))
#else
Self(CInt(IPPROTO_IPV4))
#endif
}
/// IPv6 header.
///
/// The corresponding C constant is `IPPROTO_IPV6`.
@_alwaysEmitIntoClient
public static var ipv6: ProtocolID { Self(IPPROTO_IPV6) }
public static var ipv6: ProtocolID { Self(CInt(IPPROTO_IPV6)) }
/// Raw IP packet.
///
/// The corresponding C constant is `IPPROTO_RAW`.
@_alwaysEmitIntoClient
public static var raw: ProtocolID { Self(IPPROTO_RAW) }
public static var raw: ProtocolID { Self(CInt(IPPROTO_RAW)) }
public var description: String {
rawValue.description
@@ -288,7 +308,7 @@ extension SocketDescriptor {
// MARK: - Message Flags
@available(System 99, *)
@available(System 199, *)
extension SocketDescriptor {
/// Flags for send and receive operations.
@frozen
@@ -310,50 +330,50 @@ extension SocketDescriptor {
///
/// The corresponding C constant is `MSG_OOB`.
@_alwaysEmitIntoClient
public static var outOfBand: MessageFlags { MessageFlags(MSG_OOB) }
public static var outOfBand: MessageFlags { MessageFlags(CInt(MSG_OOB)) }
/// Bypass routing, use direct interface.
///
/// The corresponding C constant is `MSG_DONTROUTE`.
@_alwaysEmitIntoClient
public static var doNotRoute: MessageFlags { MessageFlags(MSG_DONTROUTE) }
public static var doNotRoute: MessageFlags { MessageFlags(CInt(MSG_DONTROUTE)) }
/// Peek at incoming message without removing it.
///
/// The corresponding C constant is `MSG_PEEK`.
@_alwaysEmitIntoClient
public static var peek: MessageFlags { MessageFlags(MSG_PEEK) }
public static var peek: MessageFlags { MessageFlags(CInt(MSG_PEEK)) }
/// Wait for full request or error.
///
/// The corresponding C constant is `MSG_WAITALL`.
@_alwaysEmitIntoClient
public static var waitForAll: MessageFlags { MessageFlags(MSG_WAITALL) }
public static var waitForAll: MessageFlags { MessageFlags(CInt(MSG_WAITALL)) }
/// End-of-record marker.
///
/// The corresponding C constant is `MSG_EOR`.
@_alwaysEmitIntoClient
public static var endOfRecord: MessageFlags { MessageFlags(MSG_EOR) }
public static var endOfRecord: MessageFlags { MessageFlags(CInt(MSG_EOR)) }
/// Data was truncated.
///
/// The corresponding C constant is `MSG_TRUNC`.
@_alwaysEmitIntoClient
public static var dataTruncated: MessageFlags { MessageFlags(MSG_TRUNC) }
public static var dataTruncated: MessageFlags { MessageFlags(CInt(MSG_TRUNC)) }
/// Control data was truncated.
///
/// The corresponding C constant is `MSG_CTRUNC`.
@_alwaysEmitIntoClient
public static var controlTruncated: MessageFlags { MessageFlags(MSG_CTRUNC) }
public static var controlTruncated: MessageFlags { MessageFlags(CInt(MSG_CTRUNC)) }
#if !SYSTEM_PACKAGE_DARWIN
/// Do not generate SIGPIPE on broken pipe.
///
/// The corresponding C constant is `MSG_NOSIGNAL`.
@_alwaysEmitIntoClient
public static var noSignal: MessageFlags { MessageFlags(MSG_NOSIGNAL) }
public static var noSignal: MessageFlags { MessageFlags(CInt(MSG_NOSIGNAL)) }
#endif
public var description: String {
@@ -376,7 +396,7 @@ extension SocketDescriptor {
// MARK: - Shutdown Kind
@available(System 99, *)
@available(System 199, *)
extension SocketDescriptor {
/// Specifies which parts of a full-duplex connection to shut down.
@frozen
@@ -391,19 +411,19 @@ extension SocketDescriptor {
///
/// The corresponding C constant is `SHUT_RD`.
@_alwaysEmitIntoClient
public static var read: ShutdownKind { ShutdownKind(rawValue: SHUT_RD) }
public static var read: ShutdownKind { ShutdownKind(rawValue: CInt(SHUT_RD)) }
/// Disallow further sends.
///
/// The corresponding C constant is `SHUT_WR`.
@_alwaysEmitIntoClient
public static var write: ShutdownKind { ShutdownKind(rawValue: SHUT_WR) }
public static var write: ShutdownKind { ShutdownKind(rawValue: CInt(SHUT_WR)) }
/// Disallow further sends and receives.
///
/// The corresponding C constant is `SHUT_RDWR`.
@_alwaysEmitIntoClient
public static var readWrite: ShutdownKind { ShutdownKind(rawValue: SHUT_RDWR) }
public static var readWrite: ShutdownKind { ShutdownKind(rawValue: CInt(SHUT_RDWR)) }
public var description: String {
switch self {
@@ -418,7 +438,7 @@ extension SocketDescriptor {
// MARK: - Socket Option
@available(System 99, *)
@available(System 199, *)
extension SocketDescriptor {
/// A generic socket option identifier.
///
@@ -9,7 +9,7 @@
import SystemPackage
@available(System 99, *)
@available(System 199, *)
extension SocketDescriptor {
/// Writes a sequence of bytes to the socket
///
@@ -9,7 +9,22 @@
import SystemPackage
@available(System 99, *)
#if SYSTEM_PACKAGE_DARWIN
import Darwin
#elseif canImport(Glibc)
import CSystem
import Glibc
#elseif canImport(Musl)
import CSystem
import Musl
#elseif canImport(Android)
import CSystem
import Android
#else
#error("Unsupported Platform")
#endif
@available(System 199, *)
extension SocketDescriptor {
/// A reusable collection of variable-sized ancillary messages
/// sent or received over a socket. These represent protocol control
@@ -119,7 +134,8 @@ extension SocketDescriptor {
let length = try body(
UnsafeMutableRawBufferPointer(start: p + headerSize, count: capacity))
precondition(length >= 0 && length <= capacity)
header.pointee.cmsg_len = CInterop.SockLen(headerSize + length)
// `cmsg_len` is a `socklen_t` on Darwin and a `size_t` on Linux.
header.pointee.cmsg_len = numericCast(headerSize + length)
return headerSize + length
}
_endOffset += messageLength
@@ -167,7 +183,7 @@ extension SocketDescriptor {
}
}
@available(System 99, *)
@available(System 199, *)
extension SocketDescriptor.AncillaryMessageBuffer: Collection {
/// The index type in an ancillary message buffer.
@frozen
@@ -276,7 +292,7 @@ extension SocketDescriptor.AncillaryMessageBuffer: Collection {
}
}
@available(System 99, *)
@available(System 199, *)
extension SocketDescriptor.AncillaryMessageBuffer.Message {
internal var _header: CInterop.CMsgHdr {
_base._withUnsafeBytes { buffer in
@@ -12,10 +12,13 @@ import SystemPackage
#if SYSTEM_PACKAGE_DARWIN
import Darwin
#elseif canImport(Glibc)
import CSystem
import Glibc
#elseif canImport(Musl)
import CSystem
import Musl
#elseif canImport(Android)
import CSystem
import Android
#else
#error("Unsupported Platform")
@@ -23,7 +26,7 @@ import Android
// MARK: - Socket Creation and Lifecycle
@available(System 99, *)
@available(System 199, *)
extension SocketDescriptor {
/// Creates an endpoint for communication.
///
@@ -94,7 +97,7 @@ extension SocketDescriptor {
// MARK: - Connection Operations
@available(System 99, *)
@available(System 199, *)
extension SocketDescriptor {
/// Binds a socket to an address.
///
@@ -202,7 +205,7 @@ extension SocketDescriptor {
// MARK: - Send and Receive
@available(System 99, *)
@available(System 199, *)
extension SocketDescriptor {
/// Sends data on the socket.
///
@@ -347,7 +350,7 @@ extension SocketDescriptor {
// MARK: - Socket Information
@available(System 99, *)
@available(System 199, *)
extension SocketDescriptor {
/// Gets the local address of the socket.
///
@@ -12,10 +12,13 @@ import SystemPackage
#if SYSTEM_PACKAGE_DARWIN
import Darwin
#elseif canImport(Glibc)
import CSystem
import Glibc
#elseif canImport(Musl)
import CSystem
import Musl
#elseif canImport(Android)
import CSystem
import Android
#else
#error("Unsupported Platform")
@@ -23,7 +26,7 @@ import Android
// MARK: - Option Level
@available(System 99, *)
@available(System 199, *)
extension SocketDescriptor {
/// Socket option levels.
@frozen
@@ -44,31 +47,31 @@ extension SocketDescriptor {
///
/// The corresponding C constant is `IPPROTO_IP`.
@_alwaysEmitIntoClient
public static var ip: OptionLevel { OptionLevel(rawValue: IPPROTO_IP) }
public static var ip: OptionLevel { OptionLevel(rawValue: CInt(IPPROTO_IP)) }
/// IPv6 protocol options.
///
/// The corresponding C constant is `IPPROTO_IPV6`.
@_alwaysEmitIntoClient
public static var ipv6: OptionLevel { OptionLevel(rawValue: IPPROTO_IPV6) }
public static var ipv6: OptionLevel { OptionLevel(rawValue: CInt(IPPROTO_IPV6)) }
/// TCP protocol options.
///
/// The corresponding C constant is `IPPROTO_TCP`.
@_alwaysEmitIntoClient
public static var tcp: OptionLevel { OptionLevel(rawValue: IPPROTO_TCP) }
public static var tcp: OptionLevel { OptionLevel(rawValue: CInt(IPPROTO_TCP)) }
/// UDP protocol options.
///
/// The corresponding C constant is `IPPROTO_UDP`.
@_alwaysEmitIntoClient
public static var udp: OptionLevel { OptionLevel(rawValue: IPPROTO_UDP) }
public static var udp: OptionLevel { OptionLevel(rawValue: CInt(IPPROTO_UDP)) }
}
}
// MARK: - Socket-Level Options
@available(System 99, *)
@available(System 199, *)
extension SocketDescriptor {
/// Socket-level options for use with `getOption` and `setOption`.
@frozen
@@ -171,7 +174,7 @@ extension SocketDescriptor {
// MARK: - TCP Options
@available(System 99, *)
@available(System 199, *)
extension SocketDescriptor {
/// TCP-level options for use with `getOption` and `setOption`.
@frozen
@@ -227,7 +230,7 @@ extension SocketDescriptor {
// MARK: - Get and Set Options
@available(System 99, *)
@available(System 199, *)
extension SocketDescriptor {
/// Gets a socket option value.
///
@@ -312,7 +315,7 @@ extension SocketDescriptor {
// MARK: - Convenience Methods
@available(System 99, *)
@available(System 199, *)
extension SocketDescriptor {
/// Gets a socket-level option as an integer.
@_alwaysEmitIntoClient
@@ -14,10 +14,13 @@ import Testing
#if SYSTEM_PACKAGE_DARWIN
import Darwin
#elseif canImport(Glibc)
import CSystem
import Glibc
#elseif canImport(Musl)
import CSystem
import Musl
#elseif canImport(Android)
import CSystem
import Android
#else
#error("Unsupported Platform")
@@ -31,7 +34,7 @@ private struct SocketAddressTests {
// MARK: - IPv4 Address Tests
@available(System 99, *)
@available(System 199, *)
@Test func ipv4BasicCreation() {
let addr = IPv4Address("127.0.0.1", port: 8080)
#expect(addr != nil)
@@ -39,34 +42,34 @@ private struct SocketAddressTests {
#expect(addr?.addressString == "127.0.0.1")
}
@available(System 99, *)
@available(System 199, *)
@Test func ipv4InvalidAddress() {
let addr = IPv4Address("not.an.ip.address", port: 8080)
#expect(addr == nil)
}
@available(System 99, *)
@available(System 199, *)
@Test func ipv4Any() {
let addr = IPv4Address.any(port: 0)
#expect(addr.port == 0)
#expect(addr.addressString == "0.0.0.0")
}
@available(System 99, *)
@available(System 199, *)
@Test func ipv4Loopback() {
let addr = IPv4Address.loopback(port: 80)
#expect(addr.port == 80)
#expect(addr.addressString == "127.0.0.1")
}
@available(System 99, *)
@available(System 199, *)
@Test func ipv4StringLiteral() {
let addr: IPv4Address = "192.168.1.1:443"
#expect(addr.port == 443)
#expect(addr.addressString == "192.168.1.1")
}
@available(System 99, *)
@available(System 199, *)
@Test func ipv4Equality() {
let addr1 = IPv4Address("10.0.0.1", port: 8080)!
let addr2 = IPv4Address("10.0.0.1", port: 8080)!
@@ -78,7 +81,7 @@ private struct SocketAddressTests {
#expect(addr1 != addr4)
}
@available(System 99, *)
@available(System 199, *)
@Test func ipv4Description() {
let addr = IPv4Address("8.8.8.8", port: 53)!
#expect(addr.description == "8.8.8.8:53")
@@ -86,7 +89,7 @@ private struct SocketAddressTests {
// MARK: - IPv6 Address Tests
@available(System 99, *)
@available(System 199, *)
@Test func ipv6BasicCreation() {
let addr = IPv6Address("::1", port: 8080)
#expect(addr != nil)
@@ -94,33 +97,33 @@ private struct SocketAddressTests {
#expect(addr?.addressString == "::1")
}
@available(System 99, *)
@available(System 199, *)
@Test func ipv6InvalidAddress() {
let addr = IPv6Address("not:an:ipv6:address", port: 8080)
#expect(addr == nil)
}
@available(System 99, *)
@available(System 199, *)
@Test func ipv6Any() {
let addr = IPv6Address.any(port: 0)
#expect(addr.port == 0)
#expect(addr.addressString == "::")
}
@available(System 99, *)
@available(System 199, *)
@Test func ipv6Loopback() {
let addr = IPv6Address.loopback(port: 80)
#expect(addr.port == 80)
#expect(addr.addressString == "::1")
}
@available(System 99, *)
@available(System 199, *)
@Test func ipv6Description() {
let addr = IPv6Address("2001:db8::1", port: 443)!
#expect(addr.description == "[2001:db8::1]:443")
}
@available(System 99, *)
@available(System 199, *)
@Test func ipv6StringLiteral() {
let addr: IPv6Address = "[::1]:8080"
#expect(addr.port == 8080)
@@ -129,20 +132,20 @@ private struct SocketAddressTests {
// MARK: - Unix Address Tests
@available(System 99, *)
@available(System 199, *)
@Test func unixBasicCreation() {
let addr = UnixAddress("/tmp/test.sock")
#expect(addr != nil)
#expect(addr?.path == "/tmp/test.sock")
}
@available(System 99, *)
@available(System 199, *)
@Test func unixDescription() {
let addr = UnixAddress("/var/run/daemon.sock")!
#expect(addr.description == "/var/run/daemon.sock")
}
@available(System 99, *)
@available(System 199, *)
@Test func unixEquality() {
let addr1 = UnixAddress("/tmp/a.sock")!
let addr2 = UnixAddress("/tmp/a.sock")!
@@ -154,7 +157,7 @@ private struct SocketAddressTests {
// MARK: - SocketAddress Container Tests
@available(System 99, *)
@available(System 199, *)
@Test func socketAddressFromIPv4() {
let ipv4 = IPv4Address.loopback(port: 8080)
let sockAddr = SocketAddress(ipv4: ipv4)
@@ -166,7 +169,7 @@ private struct SocketAddressTests {
#expect(sockAddr.unix == nil)
}
@available(System 99, *)
@available(System 199, *)
@Test func socketAddressFromIPv6() {
let ipv6 = IPv6Address.loopback(port: 443)
let sockAddr = SocketAddress(ipv6: ipv6)
@@ -178,7 +181,7 @@ private struct SocketAddressTests {
#expect(sockAddr.unix == nil)
}
@available(System 99, *)
@available(System 199, *)
@Test func socketAddressFromUnix() {
let unix = UnixAddress("/tmp/test.sock")!
let sockAddr = SocketAddress(unix: unix)
@@ -14,10 +14,13 @@ import Testing
#if SYSTEM_PACKAGE_DARWIN
import Darwin
#elseif canImport(Glibc)
import CSystem
import Glibc
#elseif canImport(Musl)
import CSystem
import Musl
#elseif canImport(Android)
import CSystem
import Android
#else
#error("Unsupported Platform")
@@ -38,8 +41,14 @@ private struct SocketDescriptorTests {
}
@Test func connectionTypeValues() {
// Glibc imports the socket types as a C enum.
#if canImport(Glibc)
#expect(SocketDescriptor.ConnectionType.stream.rawValue == CInt(SOCK_STREAM.rawValue))
#expect(SocketDescriptor.ConnectionType.datagram.rawValue == CInt(SOCK_DGRAM.rawValue))
#else
#expect(SocketDescriptor.ConnectionType.stream.rawValue == SOCK_STREAM)
#expect(SocketDescriptor.ConnectionType.datagram.rawValue == SOCK_DGRAM)
#endif
}
@Test func protocolValues() {
@@ -49,28 +58,28 @@ private struct SocketDescriptorTests {
// MARK: - Socket Creation Tests
@available(System 99, *)
@available(System 199, *)
@Test func createTCPSocket() throws {
let socket = try SocketDescriptor.open(.ipv4, .stream, protocol: .tcp)
#expect(socket.rawValue >= 0)
try socket.close()
}
@available(System 99, *)
@available(System 199, *)
@Test func createUDPSocket() throws {
let socket = try SocketDescriptor.open(.ipv4, .datagram, protocol: .udp)
#expect(socket.rawValue >= 0)
try socket.close()
}
@available(System 99, *)
@available(System 199, *)
@Test func createIPv6TCPSocket() throws {
let socket = try SocketDescriptor.open(.ipv6, .stream, protocol: .tcp)
#expect(socket.rawValue >= 0)
try socket.close()
}
@available(System 99, *)
@available(System 199, *)
@Test func createUnixSocket() throws {
let socket = try SocketDescriptor.open(.local, .stream)
#expect(socket.rawValue >= 0)
@@ -14,10 +14,13 @@ import Testing
#if SYSTEM_PACKAGE_DARWIN
import Darwin
#elseif canImport(Glibc)
import CSystem
import Glibc
#elseif canImport(Musl)
import CSystem
import Musl
#elseif canImport(Android)
import CSystem
import Android
#else
#error("Unsupported Platform")
@@ -31,7 +34,7 @@ private struct SocketOperationsTests {
// MARK: - Bind Tests
@available(System 99, *)
@available(System 199, *)
@Test func bindToAnyPort() throws {
let socket = try SocketDescriptor.open(.ipv4, .stream, protocol: .tcp)
defer { try? socket.close() }
@@ -46,7 +49,7 @@ private struct SocketOperationsTests {
#expect(localAddr.ipv4?.port != 0) // Port should be assigned
}
@available(System 99, *)
@available(System 199, *)
@Test func bindToLoopback() throws {
let socket = try SocketDescriptor.open(.ipv4, .stream, protocol: .tcp)
defer { try? socket.close() }
@@ -61,7 +64,7 @@ private struct SocketOperationsTests {
// MARK: - Listen Tests
@available(System 99, *)
@available(System 199, *)
@Test func listenWithDefaultBacklog() throws {
let socket = try SocketDescriptor.open(.ipv4, .stream, protocol: .tcp)
defer { try? socket.close() }
@@ -73,7 +76,7 @@ private struct SocketOperationsTests {
// MARK: - Connect and Accept Tests
@available(System 99, *)
@available(System 199, *)
@Test func tcpConnectAndAccept() throws {
// Create server socket
let server = try SocketDescriptor.open(.ipv4, .stream, protocol: .tcp)
@@ -106,7 +109,7 @@ private struct SocketOperationsTests {
// MARK: - Send and Receive Tests
@available(System 99, *)
@available(System 199, *)
@Test func tcpSendReceive() throws {
// Create server socket
let server = try SocketDescriptor.open(.ipv4, .stream, protocol: .tcp)
@@ -152,7 +155,7 @@ private struct SocketOperationsTests {
// MARK: - UDP Tests
@available(System 99, *)
@available(System 199, *)
@Test func udpSendReceive() throws {
// Create receiver socket
let receiver = try SocketDescriptor.open(.ipv4, .datagram, protocol: .udp)
@@ -193,7 +196,7 @@ private struct SocketOperationsTests {
// MARK: - Socket Options Tests
@available(System 99, *)
@available(System 199, *)
@Test func setReuseAddress() throws {
let socket = try SocketDescriptor.open(.ipv4, .stream, protocol: .tcp)
defer { try? socket.close() }
@@ -203,7 +206,7 @@ private struct SocketOperationsTests {
#expect(value != 0)
}
@available(System 99, *)
@available(System 199, *)
@Test func getSendBufferSize() throws {
let socket = try SocketDescriptor.open(.ipv4, .stream, protocol: .tcp)
defer { try? socket.close() }
@@ -212,7 +215,7 @@ private struct SocketOperationsTests {
#expect(size > 0)
}
@available(System 99, *)
@available(System 199, *)
@Test func getReceiveBufferSize() throws {
let socket = try SocketDescriptor.open(.ipv4, .stream, protocol: .tcp)
defer { try? socket.close() }