Skip to content

Add nice wrappers for sockaddr and sockaddr_in #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 109 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/swift-system.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1250"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SystemPackage"
BuildableName = "SystemPackage"
BlueprintName = "SystemPackage"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SystemTests"
BuildableName = "SystemTests"
BlueprintName = "SystemTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CSystem"
BuildableName = "CSystem"
BlueprintName = "CSystem"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SystemTests"
BuildableName = "SystemTests"
BlueprintName = "SystemTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SystemPackage"
BuildableName = "SystemPackage"
BlueprintName = "SystemPackage"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
<InstallAction
buildConfiguration = "Release">
</InstallAction>
</Scheme>
29 changes: 29 additions & 0 deletions Sources/System/Internals/Backcompat.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
This source file is part of the Swift System open source project

Copyright (c) 2021 Apple Inc. and the Swift System project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
*/

extension String {
internal init(
_unsafeUninitializedCapacity capacity: Int,
initializingUTF8With body: (UnsafeMutableBufferPointer<UInt8>) throws -> Int
) rethrows {
if #available(macOS 11, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
self = try String(
unsafeUninitializedCapacity: capacity,
initializingUTF8With: body)
return
}

let array = try Array<UInt8>(
unsafeUninitializedCapacity: capacity
) { buffer, count in
count = try body(buffer)
}
self = String(decoding: array, as: UTF8.self)
}
}
21 changes: 18 additions & 3 deletions Sources/System/Internals/CInterop.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This source file is part of the Swift System open source project

Copyright (c) 2020 Apple Inc. and the Swift System project authors
Copyright (c) 2020 - 2021 Apple Inc. and the Swift System project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -29,8 +29,6 @@ import ucrt
/// A namespace for C and platform types
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
public enum CInterop {
public typealias Mode = mode_t

/// The C `char` type
public typealias Char = CChar

Expand Down Expand Up @@ -59,4 +57,21 @@ public enum CInterop {
/// on API.
public typealias PlatformUnicodeEncoding = UTF8
#endif

public typealias Mode = mode_t

public typealias SockAddr = sockaddr
public typealias SockLen = socklen_t
public typealias SAFamily = sa_family_t

public typealias SockAddrIn = sockaddr_in
public typealias InAddr = in_addr
public typealias InAddrT = in_addr_t

public typealias In6Addr = in6_addr

public typealias InPort = in_port_t

public typealias SockAddrIn6 = sockaddr_in6
public typealias SockAddrUn = sockaddr_un
}
7 changes: 5 additions & 2 deletions Sources/System/Internals/Constants.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This source file is part of the Swift System open source project

Copyright (c) 2020 Apple Inc. and the Swift System project authors
Copyright (c) 2020 - 2021 Apple Inc. and the Swift System project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -722,5 +722,8 @@ internal var _IPPROTO_TCP: CInt { IPPROTO_TCP }
@_alwaysEmitIntoClient
internal var _SOL_SOCKET: CInt { SOL_SOCKET }

@_alwaysEmitIntoClient
internal var _INET_ADDRSTRLEN: CInt { INET_ADDRSTRLEN }


@_alwaysEmitIntoClient
internal var _INET6_ADDRSTRLEN: CInt { INET6_ADDRSTRLEN }
3 changes: 1 addition & 2 deletions Sources/System/Internals/Exports.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This source file is part of the Swift System open source project

Copyright (c) 2020 Apple Inc. and the Swift System project authors
Copyright (c) 2020 - 2021 Apple Inc. and the Swift System project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
Expand All @@ -25,7 +25,6 @@ import ucrt
#endif

internal typealias _COffT = off_t
internal typealias _CSockLenT = socklen_t

// MARK: syscalls and variables

Expand Down
18 changes: 18 additions & 0 deletions Sources/System/Internals/NetworkOrder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
This source file is part of the Swift System open source project

Copyright (c) 2021 Apple Inc. and the Swift System project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
*/

extension FixedWidthInteger {
internal var _networkOrder: Self {
bigEndian
}

internal init(_networkOrder value: Self) {
self.init(bigEndian: value)
}
}
27 changes: 26 additions & 1 deletion Sources/System/Internals/Syscalls.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This source file is part of the Swift System open source project

Copyright (c) 2020 Apple Inc. and the Swift System project authors
Copyright (c) 2020 - 2021 Apple Inc. and the Swift System project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -181,3 +181,28 @@ internal func system_setsockopt(
return setsockopt(socket, level, option, value, length)
}

internal func system_inet_ntop(
_ af: CInt,
_ src: UnsafeRawPointer,
_ dst: UnsafeMutablePointer<CChar>,
_ size: CInterop.SockLen
) -> CInt { // Note: returns 0 on success, -1 on failure, unlike the original
#if ENABLE_MOCKING
if mockingEnabled { return _mock(af, src, dst, size) }
#endif
let res = inet_ntop(af, src, dst, size)
if Int(bitPattern: res) == 0 { return -1 }
assert(Int(bitPattern: res) == Int(bitPattern: dst))
return 0
}

internal func system_inet_pton(
_ af: CInt,
_ src: UnsafePointer<CChar>,
_ dst: UnsafeMutableRawPointer
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled { return _mock(af, src, dst) }
#endif
return inet_pton(af, src, dst)
}
Loading