-
Notifications
You must be signed in to change notification settings - Fork 22
Implement DNS resolver #4
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,255 @@ | ||||||
/* | ||||||
* Copyright 2024, gRPC Authors All rights reserved. | ||||||
* | ||||||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
* you may not use this file except in compliance with the License. | ||||||
* You may obtain a copy of the License at | ||||||
* | ||||||
* http://www.apache.org/licenses/LICENSE-2.0 | ||||||
* | ||||||
* Unless required by applicable law or agreed to in writing, software | ||||||
* distributed under the License is distributed on an "AS IS" BASIS, | ||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
* See the License for the specific language governing permissions and | ||||||
* limitations under the License. | ||||||
*/ | ||||||
|
||||||
private import Dispatch | ||||||
|
||||||
#if canImport(Darwin) | ||||||
package import Darwin | ||||||
#elseif canImport(Glibc) | ||||||
package import Glibc | ||||||
#elseif canImport(Musl) | ||||||
package import Musl | ||||||
#else | ||||||
#error("The GRPCNIOTransportCore module was unable to identify your C library.") | ||||||
#endif | ||||||
|
||||||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) | ||||||
/// An asynchronous non-blocking DNS resolver built on top of the libc `getaddrinfo` function. | ||||||
package enum DNSResolver { | ||||||
private static let dispatchQueue = DispatchQueue( | ||||||
label: "io.grpc.DNSResolver" | ||||||
) | ||||||
|
||||||
/// Resolves a hostname and port number to a list of socket addresses. This method is non-blocking. | ||||||
package static func resolve(host: String, port: Int) async throws -> [SocketAddress] { | ||||||
try Task.checkCancellation() | ||||||
|
||||||
return try await withCheckedThrowingContinuation { continuation in | ||||||
Self.dispatchQueue.async { | ||||||
do { | ||||||
let result = try Self.resolveBlocking(host: host, port: port) | ||||||
continuation.resume(returning: result) | ||||||
} catch { | ||||||
continuation.resume(throwing: error) | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
/// Resolves a hostname and port number to a list of socket addresses. | ||||||
/// | ||||||
/// Calls to `getaddrinfo` are blocking and this method calls `getaddrinfo` directly. Hence, this method is also blocking. | ||||||
private static func resolveBlocking(host: String, port: Int) throws -> [SocketAddress] { | ||||||
var result: UnsafeMutablePointer<addrinfo>? | ||||||
defer { | ||||||
if let result { | ||||||
// Release memory allocated by a successful call to getaddrinfo | ||||||
freeaddrinfo(result) | ||||||
} | ||||||
} | ||||||
|
||||||
var hints = addrinfo() | ||||||
hints.ai_socktype = SOCK_STREAM | ||||||
glbrntt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
hints.ai_protocol = IPPROTO_TCP | ||||||
|
hints.ai_protocol = IPPROTO_TCP | |
hints.ai_protocol = CInt(IPPROTO_TCP) |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just make this a struct
which stores the errno
as a CInt
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,37 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* Copyright 2024, gRPC Authors All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* You may obtain a copy of the License at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* limitations under the License. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import GRPCNIOTransportCore | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import Testing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@Suite("DNSResolver") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
struct DNSResolverTests { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@Test("Resolve hostname") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
func resolve() async throws { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Note: This test checks the IPv4 and IPv6 addresses separately (instead of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// `DNSResolver.resolve(host: "localhost", port: 80)`) because the ordering of the resulting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// list containing both IP address versions can be different. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let expectedIPv4Result: [SocketAddress] = [.ipv4(host: "127.0.0.1", port: 80)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let expectedIPv6Result: [SocketAddress] = [.ipv6(host: "::1", port: 80)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let ipv4Result = try await DNSResolver.resolve(host: "127.0.0.1", port: 80) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let ipv6Result = try await DNSResolver.resolve(host: "::1", port: 80) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#expect(ipv4Result == expectedIPv4Result) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#expect(ipv6Result == expectedIPv6Result) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@Test("Resolve hostname") | |
func resolve() async throws { | |
// Note: This test checks the IPv4 and IPv6 addresses separately (instead of | |
// `DNSResolver.resolve(host: "localhost", port: 80)`) because the ordering of the resulting | |
// list containing both IP address versions can be different. | |
let expectedIPv4Result: [SocketAddress] = [.ipv4(host: "127.0.0.1", port: 80)] | |
let expectedIPv6Result: [SocketAddress] = [.ipv6(host: "::1", port: 80)] | |
let ipv4Result = try await DNSResolver.resolve(host: "127.0.0.1", port: 80) | |
let ipv6Result = try await DNSResolver.resolve(host: "::1", port: 80) | |
#expect(ipv4Result == expectedIPv4Result) | |
#expect(ipv6Result == expectedIPv6Result) | |
} | |
@Test( | |
"Resolve hostname", | |
arguments: [ | |
("127.0.0.1", .ipv4(host: "127.0.0.1", port: 80)), | |
("::1", .ipv6(host: "::1", port: 80)), | |
] as [(String, SocketAddress)].self | |
) | |
func resolve(host: String, expected: SocketAddress) async throws { | |
// Note: This test checks the IPv4 and IPv6 addresses separately (instead of | |
// `DNSResolver.resolve(host: "localhost", port: 80)`) because the ordering of the resulting | |
// list containing both IP address versions can be different. | |
let result = try await DNSResolver.resolve(host: host, port: 80) | |
#expect(result == expected) | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these should be
private
, not sure it makes sense for types from lib C to make it beyond the scope of this file.