Development discussion of WireGuard
 help / color / mirror / Atom feed
From: sven-dev@ertel-net.de
To: wireguard@lists.zx2c4.com
Cc: Sven Ertel <sven-dev@ertel-net.de>
Subject: [PATCH]  Prefer IPv6 in DNSResolver
Date: Mon, 30 Mar 2020 16:38:02 +0200	[thread overview]
Message-ID: <20200330143802.4607-1-sven-dev@ertel-net.de> (raw)

From: Sven Ertel <sven-dev@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


                 reply	other threads:[~2020-03-31  3:30 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200330143802.4607-1-sven-dev@ertel-net.de \
    --to=sven-dev@ertel-net.de \
    --cc=wireguard@lists.zx2c4.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).