[PATCH] Prefer IPv6 in DNSResolver

sven-dev at ertel-net.de sven-dev at ertel-net.de
Mon Mar 30 16:38:02 CEST 2020


From: Sven Ertel <sven-dev at ertel-net.de>

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(-)

diff --git a/WireGuard/WireGuardNetworkExtension/DNSResolver.swift b/WireGuard/WireGuardNetworkExtension/DNSResolver.swift
index cdb9665..48afea6 100644
--- a/WireGuard/WireGuardNetworkExtension/DNSResolver.swift
+++ b/WireGuard/WireGuardNetworkExtension/DNSResolver.swift
@@ -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
             }
-- 
2.17.1



More information about the WireGuard mailing list