Skip to contentSkip to conversation

Prefer IPv6 in DNSResolver

PATCH New [email protected] 1 participant 1 message Mar 31, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Prefer IPv6 in DNSResolver

From: Sven Ertel <[email protected]>

Hi all,

WireGuard is great! So is IPv6, but I've the feeling that IPv6 is a little bit neglected in here and I want to help to improve that.

Attached a patch to prefer IPv6 over IPv4 in the Apple client.

This is my first patch, so please be kind ;)

Best regards

Sven

...

Change DNSResolver to prefer IPv6 addresses over IPv4 addresses.

---
 .../WireGuardNetworkExtension/DNSResolver.swift      | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
WireGuard/WireGuardNetworkExtension/DNSResolver.swift +6 -6
index cdb9665..48afea6 100644
@@ -82,20 +82,20 @@ class DNSResolver {
                 if result.ai_family == AF_INET && result.ai_addrlen == MemoryLayout<sockaddr_in>.size {
                     var sa4 = UnsafeRawPointer(result.ai_addr)!.assumingMemoryBound(to: sockaddr_in.self).pointee
                     ipv4Address = IPv4Address(Data(bytes: &sa4.sin_addr, count: MemoryLayout<in_addr>.size))
-                    break // If we found an IPv4 address, we can stop
+                    continue // If we found an IPv4 address, continue looking for an IPv6 address
                 } else if result.ai_family == AF_INET6 && result.ai_addrlen == MemoryLayout<sockaddr_in6>.size {
                     var sa6 = UnsafeRawPointer(result.ai_addr)!.assumingMemoryBound(to: sockaddr_in6.self).pointee
                     ipv6Address = IPv6Address(Data(bytes: &sa6.sin6_addr, count: MemoryLayout<in6_addr>.size))
-                    continue // If we already have an IPv6 address, we can skip this one
+                    break // If we found an IPv6 address, we can stop
                 }
             }
             freeaddrinfo(resultPointer)
 
-            // We prefer an IPv4 address over an IPv6 address
-            if let ipv4Address = ipv4Address {
-                return Endpoint(host: .ipv4(ipv4Address), port: endpoint.port)
-            } else if let ipv6Address = ipv6Address {
+            // We prefer an IPv6 address over an IPv4 address
+            if let ipv6Address = ipv6Address {
                 return Endpoint(host: .ipv6(ipv6Address), port: endpoint.port)
+            } else if let ipv4Address = ipv4Address {
+                return Endpoint(host: .ipv4(ipv4Address), port: endpoint.port)
             } else {
                 return nil
             }
Signature · 1 line
2.17.1