Comparison

net/dns.lua @ 7054:94d5e2f33a10

net.dns: Support IPv6 addresses in resolv.conf [Backported from 0.10]
author Florian Zeitz <florob@babelmonkeys.de>
date Tue, 18 Jun 2013 23:02:20 +0200
parent 6863:88d54bec26b7
child 7056:7b0651e4534f
comparison
equal deleted inserted replaced
7053:3049137d14b6 7054:94d5e2f33a10
12 -- reference: http://tools.ietf.org/html/rfc1876 (LOC) 12 -- reference: http://tools.ietf.org/html/rfc1876 (LOC)
13 13
14 14
15 local socket = require "socket"; 15 local socket = require "socket";
16 local timer = require "util.timer"; 16 local timer = require "util.timer";
17 local new_ip = require "util.ip".new_ip;
17 18
18 local _, windows = pcall(require, "util.windows"); 19 local _, windows = pcall(require, "util.windows");
19 local is_windows = (_ and windows) or os.getenv("WINDIR"); 20 local is_windows = (_ and windows) or os.getenv("WINDIR");
20 21
21 local coroutine, io, math, string, table = 22 local coroutine, io, math, string, table =
597 else -- posix 598 else -- posix
598 local resolv_conf = io.open("/etc/resolv.conf"); 599 local resolv_conf = io.open("/etc/resolv.conf");
599 if resolv_conf then 600 if resolv_conf then
600 for line in resolv_conf:lines() do 601 for line in resolv_conf:lines() do
601 line = line:gsub("#.*$", "") 602 line = line:gsub("#.*$", "")
602 :match('^%s*nameserver%s+(.*)%s*$'); 603 :match('^%s*nameserver%s+([%x:%.]*)%s*$');
603 if line then 604 if line then
604 line:gsub("%f[%d.](%d+%.%d+%.%d+%.%d+)%f[^%d.]", function (address) 605 local ip = new_ip(line);
605 self:addnameserver(address) 606 if ip then
606 end); 607 self:addnameserver(ip.addr);
608 end
607 end 609 end
608 end 610 end
609 end 611 end
610 if not self.server or #self.server == 0 then 612 if not self.server or #self.server == 0 then
611 -- TODO log warning about no nameservers, adding localhost as the default nameserver 613 -- TODO log warning about no nameservers, adding localhost as the default nameserver
621 623
622 local sock = self.socket[servernum]; 624 local sock = self.socket[servernum];
623 if sock then return sock; end 625 if sock then return sock; end
624 626
625 local ok, err; 627 local ok, err;
626 sock, err = socket.udp(); 628 local peer = self.server[servernum];
629 if peer:find(":") then
630 sock, err = socket.udp6();
631 else
632 sock, err = socket.udp();
633 end
627 if sock and self.socket_wrapper then sock, err = self.socket_wrapper(sock, self); end 634 if sock and self.socket_wrapper then sock, err = self.socket_wrapper(sock, self); end
628 if not sock then 635 if not sock then
629 return nil, err; 636 return nil, err;
630 end 637 end
631 sock:settimeout(0); 638 sock:settimeout(0);
634 self.socketset[sock] = servernum; 641 self.socketset[sock] = servernum;
635 -- set{sock,peer}name can fail, eg because of local routing table 642 -- set{sock,peer}name can fail, eg because of local routing table
636 -- if so, try the next server 643 -- if so, try the next server
637 ok, err = sock:setsockname('*', 0); 644 ok, err = sock:setsockname('*', 0);
638 if not ok then return self:servfail(sock, err); end 645 if not ok then return self:servfail(sock, err); end
639 ok, err = sock:setpeername(self.server[servernum], 53); 646 ok, err = sock:setpeername(peer, 53);
640 if not ok then return self:servfail(sock, err); end 647 if not ok then return self:servfail(sock, err); end
641 return sock; 648 return sock;
642 end 649 end
643 650
644 function resolver:voidsocket(sock) 651 function resolver:voidsocket(sock)