Skip to content

Commit 96a18c6

Browse files
authored
Add concrete IPv4 and IPv6 types (#21)
Motivation: The `ARecord` and `AAAARecord` use the `IPAddress` `enum` as their address types. However, they should only use IPv4 and IPv6 addresses respectively. Modifications: - Add `IPAddress.IPv4` and IPAddress.IPv6` types and use them as the associated values in the `IPAddress` enum - Change the casing of the `IPAddress` cases from `.IPv4` and `.IPv6` to `.ipv4` and `.ipv6` as Swift case names are usually lower-camel case Result: - The types of IP address used by `ARecord` and `AAAARecord` are clearer
1 parent 62fbedd commit 96a18c6

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

Sources/AsyncDNSResolver/AsyncDNSResolver.swift

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,21 +185,39 @@ enum QueryType {
185185
// MARK: - Query reply types
186186

187187
public enum IPAddress: Sendable, Equatable, CustomStringConvertible {
188-
case IPv4(String)
189-
case IPv6(String)
188+
case ipv4(IPv4)
189+
case ipv6(IPv6)
190190

191191
public var description: String {
192192
switch self {
193-
case .IPv4(let address):
194-
return address
195-
case .IPv6(let address):
196-
return address
193+
case .ipv4(let address):
194+
return String(describing: address)
195+
case .ipv6(let address):
196+
return String(describing: address)
197+
}
198+
}
199+
200+
public struct IPv4: Sendable, Hashable, CustomStringConvertible {
201+
public var address: String
202+
public var description: String { self.address }
203+
204+
public init(address: String) {
205+
self.address = address
206+
}
207+
}
208+
209+
public struct IPv6: Sendable, Hashable, CustomStringConvertible {
210+
public var address: String
211+
public var description: String { self.address }
212+
213+
public init(address: String) {
214+
self.address = address
197215
}
198216
}
199217
}
200218

201219
public struct ARecord: Sendable, Equatable, CustomStringConvertible {
202-
public let address: IPAddress
220+
public let address: IPAddress.IPv4
203221
public let ttl: Int32?
204222

205223
public var description: String {
@@ -208,7 +226,7 @@ public struct ARecord: Sendable, Equatable, CustomStringConvertible {
208226
}
209227

210228
public struct AAAARecord: Sendable, Equatable, CustomStringConvertible {
211-
public let address: IPAddress
229+
public let address: IPAddress.IPv6
212230
public let ttl: Int32?
213231

214232
public var description: String {

Sources/AsyncDNSResolver/c-ares/DNSResolver_c-ares.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,32 +579,34 @@ private func toStringArray(_ arrayPointer: UnsafeMutablePointer<UnsafeMutablePoi
579579
return result
580580
}
581581

582-
extension IPAddress {
582+
extension IPAddress.IPv4 {
583583
init(_ address: in_addr) {
584584
var address = address
585585
var addressBytes = [CChar](repeating: 0, count: Int(INET_ADDRSTRLEN))
586586
inet_ntop(AF_INET, &address, &addressBytes, socklen_t(INET_ADDRSTRLEN))
587-
self = .IPv4(String(cString: addressBytes))
587+
self = .init(address: String(cString: addressBytes))
588588
}
589+
}
589590

591+
extension IPAddress.IPv6 {
590592
init(_ address: ares_in6_addr) {
591593
var address = address
592594
var addressBytes = [CChar](repeating: 0, count: Int(INET6_ADDRSTRLEN))
593595
inet_ntop(AF_INET6, &address, &addressBytes, socklen_t(INET6_ADDRSTRLEN))
594-
self = .IPv6(String(cString: addressBytes))
596+
self = .init(address: String(cString: addressBytes))
595597
}
596598
}
597599

598600
extension ARecord {
599601
init(_ addrttl: ares_addrttl) {
600-
self.address = IPAddress(addrttl.ipaddr)
602+
self.address = IPAddress.IPv4(addrttl.ipaddr)
601603
self.ttl = addrttl.ttl
602604
}
603605
}
604606

605607
extension AAAARecord {
606608
init(_ addrttl: ares_addr6ttl) {
607-
self.address = IPAddress(addrttl.ip6addr)
609+
self.address = IPAddress.IPv6(addrttl.ip6addr)
608610
self.ttl = addrttl.ttl
609611
}
610612
}

Sources/AsyncDNSResolver/dnssd/DNSResolver_dnssd.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ extension DNSSD {
230230
var parsedAddressBytes = [CChar](repeating: 0, count: Int(INET_ADDRSTRLEN))
231231
inet_ntop(AF_INET, ptr, &parsedAddressBytes, socklen_t(INET_ADDRSTRLEN))
232232
let parsedAddress = String(cString: parsedAddressBytes)
233-
return ARecord(address: .IPv4(parsedAddress), ttl: nil)
233+
return ARecord(address: .init(address: parsedAddress), ttl: nil)
234234
}
235235

236236
func generateReply(records: [ARecord]) throws -> [ARecord] {
@@ -253,7 +253,7 @@ extension DNSSD {
253253
var parsedAddressBytes = [CChar](repeating: 0, count: Int(INET6_ADDRSTRLEN))
254254
inet_ntop(AF_INET6, ptr, &parsedAddressBytes, socklen_t(INET6_ADDRSTRLEN))
255255
let parsedAddress = String(cString: parsedAddressBytes)
256-
return AAAARecord(address: .IPv6(parsedAddress), ttl: nil)
256+
return AAAARecord(address: .init(address: parsedAddress), ttl: nil)
257257
}
258258

259259
func generateReply(records: [AAAARecord]) throws -> [AAAARecord] {

Tests/AsyncDNSResolverTests/dnssd/DNSSDDNSResolverTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ final class DNSSDDNSResolverTests: XCTestCase {
108108
let addrBytes: [UInt8] = [38, 32, 1, 73]
109109
try addrBytes.withUnsafeBufferPointer {
110110
let record = try DNSSD.AQueryReplyHandler.instance.parseRecord(data: $0.baseAddress, length: UInt16($0.count))
111-
XCTAssertEqual(record, ARecord(address: .IPv4("38.32.1.73"), ttl: nil))
111+
XCTAssertEqual(record, ARecord(address: .init(address: "38.32.1.73"), ttl: nil))
112112
}
113113
}
114114

0 commit comments

Comments
 (0)