DomainDig/ReverseDNSService.swift

2ad5d8160338d289a65d6a4d1c214a02bf916f1f
domain-dig/DomainDig/ReverseDNSService.swift history · blame · raw

27 lines · 857 bytes

2 symbols in this file
 1import Foundation
 2
 3struct ReverseDNSService {
 4    static func lookup(ip: String, resolverURLString: String) async -> ServiceResult<String> {
 5        let octets = ip.split(separator: ".")
 6        guard octets.count == 4 else {
 7            return .error("Invalid IPv4 address")
 8        }
 9
10        let reversed = octets.reversed().joined(separator: ".")
11        let ptrDomain = "\(reversed).in-addr.arpa"
12
13        do {
14            let records = try await DNSLookupService.lookup(
15                domain: ptrDomain,
16                recordType: .PTR,
17                resolverURLString: resolverURLString
18            )
19            if let record = records.first?.value {
20                return .success(record)
21            }
22            return .empty("No PTR record found")
23        } catch {
24            return .error(error.localizedDescription)
25        }
26    }
27}