Skip to content

Commit e55c19d

Browse files
authored
Add nice wrappers for sockaddr and sockaddr_in (#1)
Add Swifty wrappers for socket and IP addresses
1 parent c11d8fa commit e55c19d

File tree

14 files changed

+911
-11
lines changed

14 files changed

+911
-11
lines changed

.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1250"
4+
version = "1.7">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "SystemPackage"
18+
BuildableName = "SystemPackage"
19+
BlueprintName = "SystemPackage"
20+
ReferencedContainer = "container:">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
<BuildActionEntry
24+
buildForTesting = "YES"
25+
buildForRunning = "YES"
26+
buildForProfiling = "NO"
27+
buildForArchiving = "NO"
28+
buildForAnalyzing = "YES">
29+
<BuildableReference
30+
BuildableIdentifier = "primary"
31+
BlueprintIdentifier = "SystemTests"
32+
BuildableName = "SystemTests"
33+
BlueprintName = "SystemTests"
34+
ReferencedContainer = "container:">
35+
</BuildableReference>
36+
</BuildActionEntry>
37+
<BuildActionEntry
38+
buildForTesting = "YES"
39+
buildForRunning = "YES"
40+
buildForProfiling = "YES"
41+
buildForArchiving = "YES"
42+
buildForAnalyzing = "YES">
43+
<BuildableReference
44+
BuildableIdentifier = "primary"
45+
BlueprintIdentifier = "CSystem"
46+
BuildableName = "CSystem"
47+
BlueprintName = "CSystem"
48+
ReferencedContainer = "container:">
49+
</BuildableReference>
50+
</BuildActionEntry>
51+
</BuildActionEntries>
52+
</BuildAction>
53+
<TestAction
54+
buildConfiguration = "Debug"
55+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
56+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
57+
shouldUseLaunchSchemeArgsEnv = "YES"
58+
codeCoverageEnabled = "YES">
59+
<Testables>
60+
<TestableReference
61+
skipped = "NO">
62+
<BuildableReference
63+
BuildableIdentifier = "primary"
64+
BlueprintIdentifier = "SystemTests"
65+
BuildableName = "SystemTests"
66+
BlueprintName = "SystemTests"
67+
ReferencedContainer = "container:">
68+
</BuildableReference>
69+
</TestableReference>
70+
</Testables>
71+
</TestAction>
72+
<LaunchAction
73+
buildConfiguration = "Debug"
74+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
75+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
76+
launchStyle = "0"
77+
useCustomWorkingDirectory = "NO"
78+
ignoresPersistentStateOnLaunch = "NO"
79+
debugDocumentVersioning = "YES"
80+
debugServiceExtension = "internal"
81+
allowLocationSimulation = "YES">
82+
</LaunchAction>
83+
<ProfileAction
84+
buildConfiguration = "Release"
85+
shouldUseLaunchSchemeArgsEnv = "YES"
86+
savedToolIdentifier = ""
87+
useCustomWorkingDirectory = "NO"
88+
debugDocumentVersioning = "YES">
89+
<MacroExpansion>
90+
<BuildableReference
91+
BuildableIdentifier = "primary"
92+
BlueprintIdentifier = "SystemPackage"
93+
BuildableName = "SystemPackage"
94+
BlueprintName = "SystemPackage"
95+
ReferencedContainer = "container:">
96+
</BuildableReference>
97+
</MacroExpansion>
98+
</ProfileAction>
99+
<AnalyzeAction
100+
buildConfiguration = "Debug">
101+
</AnalyzeAction>
102+
<ArchiveAction
103+
buildConfiguration = "Release"
104+
revealArchiveInOrganizer = "YES">
105+
</ArchiveAction>
106+
<InstallAction
107+
buildConfiguration = "Release">
108+
</InstallAction>
109+
</Scheme>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2021 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
*/
9+
10+
extension String {
11+
internal init(
12+
_unsafeUninitializedCapacity capacity: Int,
13+
initializingUTF8With body: (UnsafeMutableBufferPointer<UInt8>) throws -> Int
14+
) rethrows {
15+
if #available(macOS 11, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
16+
self = try String(
17+
unsafeUninitializedCapacity: capacity,
18+
initializingUTF8With: body)
19+
return
20+
}
21+
22+
let array = try Array<UInt8>(
23+
unsafeUninitializedCapacity: capacity
24+
) { buffer, count in
25+
count = try body(buffer)
26+
}
27+
self = String(decoding: array, as: UTF8.self)
28+
}
29+
}

Sources/System/Internals/CInterop.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift System open source project
33

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

77
See https://swift.org/LICENSE.txt for license information
@@ -29,8 +29,6 @@ import ucrt
2929
/// A namespace for C and platform types
3030
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
3131
public enum CInterop {
32-
public typealias Mode = mode_t
33-
3432
/// The C `char` type
3533
public typealias Char = CChar
3634

@@ -59,4 +57,21 @@ public enum CInterop {
5957
/// on API.
6058
public typealias PlatformUnicodeEncoding = UTF8
6159
#endif
60+
61+
public typealias Mode = mode_t
62+
63+
public typealias SockAddr = sockaddr
64+
public typealias SockLen = socklen_t
65+
public typealias SAFamily = sa_family_t
66+
67+
public typealias SockAddrIn = sockaddr_in
68+
public typealias InAddr = in_addr
69+
public typealias InAddrT = in_addr_t
70+
71+
public typealias In6Addr = in6_addr
72+
73+
public typealias InPort = in_port_t
74+
75+
public typealias SockAddrIn6 = sockaddr_in6
76+
public typealias SockAddrUn = sockaddr_un
6277
}

Sources/System/Internals/Constants.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift System open source project
33

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

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

725+
@_alwaysEmitIntoClient
726+
internal var _INET_ADDRSTRLEN: CInt { INET_ADDRSTRLEN }
725727

726-
728+
@_alwaysEmitIntoClient
729+
internal var _INET6_ADDRSTRLEN: CInt { INET6_ADDRSTRLEN }

Sources/System/Internals/Exports.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift System open source project
33

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

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

2727
internal typealias _COffT = off_t
28-
internal typealias _CSockLenT = socklen_t
2928

3029
// MARK: syscalls and variables
3130

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2021 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
*/
9+
10+
extension FixedWidthInteger {
11+
internal var _networkOrder: Self {
12+
bigEndian
13+
}
14+
15+
internal init(_networkOrder value: Self) {
16+
self.init(bigEndian: value)
17+
}
18+
}

Sources/System/Internals/Syscalls.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift System open source project
33

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

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

184+
internal func system_inet_ntop(
185+
_ af: CInt,
186+
_ src: UnsafeRawPointer,
187+
_ dst: UnsafeMutablePointer<CChar>,
188+
_ size: CInterop.SockLen
189+
) -> CInt { // Note: returns 0 on success, -1 on failure, unlike the original
190+
#if ENABLE_MOCKING
191+
if mockingEnabled { return _mock(af, src, dst, size) }
192+
#endif
193+
let res = inet_ntop(af, src, dst, size)
194+
if Int(bitPattern: res) == 0 { return -1 }
195+
assert(Int(bitPattern: res) == Int(bitPattern: dst))
196+
return 0
197+
}
198+
199+
internal func system_inet_pton(
200+
_ af: CInt,
201+
_ src: UnsafePointer<CChar>,
202+
_ dst: UnsafeMutableRawPointer
203+
) -> CInt {
204+
#if ENABLE_MOCKING
205+
if mockingEnabled { return _mock(af, src, dst) }
206+
#endif
207+
return inet_pton(af, src, dst)
208+
}

0 commit comments

Comments
 (0)