From 799c2d96cd6f5b4e868b851c995693f3b1e4ce5f Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Fri, 2 Nov 2018 05:34:17 +0100 Subject: [PATCH] DNSResolver: getaddrinfo returns a sockaddr and inet_ntop takes strlen The way this was written before was totally wrong. --- WireGuard/WireGuard/VPN/DNSResolver.swift | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/WireGuard/WireGuard/VPN/DNSResolver.swift b/WireGuard/WireGuard/VPN/DNSResolver.swift index 9de2d8e..43e7c41 100644 --- a/WireGuard/WireGuard/VPN/DNSResolver.swift +++ b/WireGuard/WireGuard/VPN/DNSResolver.swift @@ -111,18 +111,20 @@ extension DNSResolver { while (resultPointer != nil) { let result = resultPointer!.pointee resultPointer = result.ai_next - if (result.ai_family == AF_INET && result.ai_addrlen == INET_ADDRSTRLEN) { - if (inet_ntop(result.ai_family, result.ai_addr, ipv4Buffer, result.ai_addrlen) != nil) { + if (result.ai_family == AF_INET && result.ai_addrlen == MemoryLayout.size) { + var sa4 = UnsafeRawPointer(result.ai_addr)!.assumingMemoryBound(to: sockaddr_in.self).pointee + if (inet_ntop(result.ai_family, &sa4.sin_addr, ipv4Buffer, socklen_t(INET_ADDRSTRLEN)) != nil) { ipv4AddressString = String(cString: ipv4Buffer) // If we found an IPv4 address, we can stop break } - } else if (result.ai_family == AF_INET6 && result.ai_addrlen == INET6_ADDRSTRLEN) { + } else if (result.ai_family == AF_INET6 && result.ai_addrlen == MemoryLayout.size) { if (ipv6AddressString != nil) { // If we already have an IPv6 address, we can skip this one continue } - if (inet_ntop(result.ai_family, result.ai_addr, ipv6Buffer, result.ai_addrlen) != nil) { + var sa6 = UnsafeRawPointer(result.ai_addr)!.assumingMemoryBound(to: sockaddr_in6.self).pointee + if (inet_ntop(result.ai_family, &sa6.sin6_addr, ipv6Buffer, socklen_t(INET6_ADDRSTRLEN)) != nil) { ipv6AddressString = String(cString: ipv6Buffer) } }