NameResolver+IPv4.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright 2024, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import GRPCCore
  17. extension ResolvableTargets {
  18. /// A resolvable target for IPv4 addresses.
  19. ///
  20. /// IPv4 addresses can be resolved by the ``NameResolvers/IPv4`` resolver which creates a
  21. /// separate ``Endpoint`` for each address.
  22. public struct IPv4: ResolvableTarget {
  23. /// The IPv4 addresses.
  24. public var addresses: [SocketAddress.IPv4]
  25. /// Create a new IPv4 target.
  26. /// - Parameter addresses: The IPv4 addresses.
  27. public init(addresses: [SocketAddress.IPv4]) {
  28. self.addresses = addresses
  29. }
  30. }
  31. }
  32. extension ResolvableTarget where Self == ResolvableTargets.IPv4 {
  33. /// Creates a new resolvable IPv4 target for a single address.
  34. /// - Parameters:
  35. /// - host: The host address.
  36. /// - port: The port on the host.
  37. /// - Returns: A ``ResolvableTarget``.
  38. public static func ipv4(host: String, port: Int = 443) -> Self {
  39. let address = SocketAddress.IPv4(host: host, port: port)
  40. return Self(addresses: [address])
  41. }
  42. /// Creates a new resolvable IPv4 target from the provided host-port pairs.
  43. ///
  44. /// - Parameter pairs: An array of host-port pairs.
  45. /// - Returns: A ``ResolvableTarget``.
  46. public static func ipv4(pairs: [(host: String, port: Int)]) -> Self {
  47. let address = pairs.map { SocketAddress.IPv4(host: $0.host, port: $0.port) }
  48. return Self(addresses: address)
  49. }
  50. }
  51. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  52. extension NameResolvers {
  53. /// A ``NameResolverFactory`` for ``ResolvableTargets/IPv4`` targets.
  54. ///
  55. /// The name resolver for a given target always produces the same values, with one endpoint per
  56. /// address in the target. This resolver doesn't support fetching service configuration.
  57. public struct IPv4: NameResolverFactory {
  58. public typealias Target = ResolvableTargets.IPv4
  59. /// Create a new IPv4 resolver factory.
  60. public init() {}
  61. public func resolver(for target: Target) -> NameResolver {
  62. let endpoints = target.addresses.map { Endpoint(addresses: [.ipv4($0)]) }
  63. let resolutionResult = NameResolutionResult(endpoints: endpoints, serviceConfiguration: nil)
  64. return NameResolver(names: .constant(resolutionResult), updateMode: .pull)
  65. }
  66. }
  67. }