Skip to content

Commit 35a7df2

Browse files
compnerdLukasa
andauthored
X509: make the code more portable (#72)
* X509: make the code more portable `CoreFoundation` is not available on all platforms. It never was meant to be available on Linux, but was accidentally pushed into the release and is now stuck. Windows intentionally did not make this module visible. With the future work with the Foundation rewrite `CoreFoundation` will disappear in more locations. Windows does not support `inet_pton` except unless the string is explicitly ANSI and not UTF-8. We must use the proper spelling of `InetPtonW` which takes a UTF-16 string. The import of `CoreFoundation` was more for the dependency on the C library as a side-effect. On Windows, the networking functions are provided by a separate user space library rather than the C library. This is now highlighted in the imports. * Clean up indentation * Re-add CoreFoundation on Linux --------- Co-authored-by: Cory Benfield <lukasa@apple.com>
1 parent 0121e24 commit 35a7df2

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

Sources/X509/Verifier/RFC5280/URIConstraints.swift

+17-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import CoreFoundation
1615
import Foundation
16+
#if os(Windows)
17+
import WinSDK
18+
#elseif os(Linux) || os(Android)
19+
import Glibc
20+
import CoreFoundation
21+
#elseif os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
22+
import Darwin
23+
#endif
1724

1825
extension NameConstraintsPolicy {
1926
/// Validates that a URI name matches a name constraint.
@@ -58,14 +65,22 @@ extension NameConstraintsPolicy {
5865
extension String {
5966
@inlinable
6067
var isIPAddress: Bool {
68+
#if os(Windows)
69+
var v4: IN_ADDR = IN_ADDR()
70+
var v6: IN6_ADDR = IN6_ADDR()
71+
return self.withCString(encodedAs: UTF16.self) {
72+
return InetPtonW(AF_INET, $0, &v4) == 1 ||
73+
InetPtonW(AF_INET6, $0, &v6) == 1
74+
}
75+
#else
6176
// We need some scratch space to let inet_pton write into.
6277
var ipv4Addr = in_addr()
6378
var ipv6Addr = in6_addr()
64-
6579
return self.withCString { ptr in
6680
return inet_pton(AF_INET, ptr, &ipv4Addr) == 1 ||
6781
inet_pton(AF_INET6, ptr, &ipv6Addr) == 1
6882
}
83+
#endif
6984
}
7085
}
7186

0 commit comments

Comments
 (0)