Diff

net/dns.lua @ 5705:6eebd5808fbc

net.dns: Support IPv6 addresses in resolv.conf
author Florian Zeitz <florob@babelmonkeys.de>
date Tue, 18 Jun 2013 23:02:20 +0200
parent 5566:74ae3e7e8779
child 5731:902927f1c96f
line wrap: on
line diff
--- a/net/dns.lua	Tue Jun 18 12:17:06 2013 +0100
+++ b/net/dns.lua	Tue Jun 18 23:02:20 2013 +0200
@@ -14,6 +14,7 @@
 
 local socket = require "socket";
 local timer = require "util.timer";
+local new_ip = require "util.ip".new_ip;
 
 local _, windows = pcall(require, "util.windows");
 local is_windows = (_ and windows) or os.getenv("WINDIR");
@@ -597,11 +598,12 @@
 		if resolv_conf then
 			for line in resolv_conf:lines() do
 				line = line:gsub("#.*$", "")
-					:match('^%s*nameserver%s+(.*)%s*$');
+					:match('^%s*nameserver%s+([%x:%.]*)%s*$');
 				if line then
-					line:gsub("%f[%d.](%d+%.%d+%.%d+%.%d+)%f[^%d.]", function (address)
-						self:addnameserver(address)
-					end);
+					local ip = new_ip(line);
+					if ip then
+						self:addnameserver(ip.addr);
+					end
 				end
 			end
 		end
@@ -621,7 +623,12 @@
 	if sock then return sock; end
 
 	local err;
-	sock, err = socket.udp();
+	local peer = self.server[servernum];
+	if peer:find(":") then
+		sock, err = socket.udp6();
+	else
+		sock, err = socket.udp();
+	end
 	if not sock then
 		return nil, err;
 	end
@@ -629,7 +636,7 @@
 	sock:settimeout(0);
 	-- todo: attempt to use a random port, fallback to 0
 	sock:setsockname('*', 0);
-	sock:setpeername(self.server[servernum], 53);
+	sock:setpeername(peer, 53);
 	self.socket[servernum] = sock;
 	self.socketset[sock] = servernum;
 	return sock;