Skip to content

Commit

Permalink
Fix an issue with ipv6 struct in6_addr that is not the same in Glibc …
Browse files Browse the repository at this point in the history
…and Musl
  • Loading branch information
vliegeois committed Feb 1, 2025
1 parent 3cd2795 commit bf5b91a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
9 changes: 7 additions & 2 deletions Sources/DNSClient/DNSClient+Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extension DNSClient {
let result = self.sendQuery(forHost: host, type: .aaaa)

return result.map { message in
return message.answers.compactMap { answer in
return message.answers.compactMap { answer -> SocketAddress? in
guard
case .aaaa(let record) = answer,
record.resource.address.count == 16
Expand All @@ -45,11 +45,16 @@ extension DNSClient {
let scopeID: UInt32 = 0 // More info about scope_id/zone_id https://tools.ietf.org/html/rfc6874#page-3
let flowinfo: UInt32 = 0 // More info about flowinfo https://tools.ietf.org/html/rfc6437#page-4

#if os(Linux)
#if canImport(Glibc)
let ipAddress = address.withUnsafeBytes { buffer in
return buffer.bindMemory(to: in6_addr.__Unnamed_union___in6_u.self).baseAddress!.pointee
}
let sockaddr = sockaddr_in6(sin6_family: sa_family_t(AF_INET6), sin6_port: in_port_t(port), sin6_flowinfo: flowinfo, sin6_addr: in6_addr(__in6_u: ipAddress), sin6_scope_id: scopeID)
#elseif canImport(Musl)
let ipAddress = address.withUnsafeBytes { buffer in
return buffer.bindMemory(to: in6_addr.__Unnamed_union___in6_union.self).baseAddress!.pointee
}
let sockaddr = sockaddr_in6(sin6_family: sa_family_t(AF_INET6), sin6_port: in_port_t(port), sin6_flowinfo: flowinfo, sin6_addr: in6_addr(__in6_union: ipAddress), sin6_scope_id: scopeID)
#else
let ipAddress = address.withUnsafeBytes { buffer in
return buffer.bindMemory(to: in6_addr.__Unnamed_union___u6_addr.self).baseAddress!.pointee
Expand Down
24 changes: 23 additions & 1 deletion Sources/DNSClient/PTR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ extension DNSClient {
throw IOError(errnoCode: errno, reason: #function)
}

#if os(Linux)
#if canImport(Glibc)
let inAddrArpaDomain = String(format: "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
ipv6Addr.__in6_u.__u6_addr8.0,
ipv6Addr.__in6_u.__u6_addr8.1,
Expand All @@ -100,6 +100,28 @@ extension DNSClient {
.joined(separator: ".")
.appending(".ip6.arpa.")

#elseif canImport(Musl)
let inAddrArpaDomain = String(format: "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
ipv6Addr.__in6_union.__s6_addr.0,
ipv6Addr.__in6_union.__s6_addr.1,
ipv6Addr.__in6_union.__s6_addr.2,
ipv6Addr.__in6_union.__s6_addr.3,
ipv6Addr.__in6_union.__s6_addr.4,
ipv6Addr.__in6_union.__s6_addr.5,
ipv6Addr.__in6_union.__s6_addr.6,
ipv6Addr.__in6_union.__s6_addr.7,
ipv6Addr.__in6_union.__s6_addr.8,
ipv6Addr.__in6_union.__s6_addr.9,
ipv6Addr.__in6_union.__s6_addr.10,
ipv6Addr.__in6_union.__s6_addr.11,
ipv6Addr.__in6_union.__s6_addr.12,
ipv6Addr.__in6_union.__s6_addr.13,
ipv6Addr.__in6_union.__s6_addr.14,
ipv6Addr.__in6_union.__s6_addr.15
).reversed()
.map { "\($0)" }
.joined(separator: ".")
.appending(".ip6.arpa.")
#else
let inAddrArpaDomain = String(format: "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
ipv6Addr.__u6_addr.__u6_addr8.0,
Expand Down

0 comments on commit bf5b91a

Please sign in to comment.