Comparison

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
comparison
equal deleted inserted replaced
5704:76e8eb9f0c1e 5705:6eebd5808fbc
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 =
595 else -- posix 596 else -- posix
596 local resolv_conf = io.open("/etc/resolv.conf"); 597 local resolv_conf = io.open("/etc/resolv.conf");
597 if resolv_conf then 598 if resolv_conf then
598 for line in resolv_conf:lines() do 599 for line in resolv_conf:lines() do
599 line = line:gsub("#.*$", "") 600 line = line:gsub("#.*$", "")
600 :match('^%s*nameserver%s+(.*)%s*$'); 601 :match('^%s*nameserver%s+([%x:%.]*)%s*$');
601 if line then 602 if line then
602 line:gsub("%f[%d.](%d+%.%d+%.%d+%.%d+)%f[^%d.]", function (address) 603 local ip = new_ip(line);
603 self:addnameserver(address) 604 if ip then
604 end); 605 self:addnameserver(ip.addr);
606 end
605 end 607 end
606 end 608 end
607 end 609 end
608 if not self.server or #self.server == 0 then 610 if not self.server or #self.server == 0 then
609 -- TODO log warning about no nameservers, adding localhost as the default nameserver 611 -- TODO log warning about no nameservers, adding localhost as the default nameserver
619 621
620 local sock = self.socket[servernum]; 622 local sock = self.socket[servernum];
621 if sock then return sock; end 623 if sock then return sock; end
622 624
623 local err; 625 local err;
624 sock, err = socket.udp(); 626 local peer = self.server[servernum];
627 if peer:find(":") then
628 sock, err = socket.udp6();
629 else
630 sock, err = socket.udp();
631 end
625 if not sock then 632 if not sock then
626 return nil, err; 633 return nil, err;
627 end 634 end
628 if self.socket_wrapper then sock = self.socket_wrapper(sock, self); end 635 if self.socket_wrapper then sock = self.socket_wrapper(sock, self); end
629 sock:settimeout(0); 636 sock:settimeout(0);
630 -- todo: attempt to use a random port, fallback to 0 637 -- todo: attempt to use a random port, fallback to 0
631 sock:setsockname('*', 0); 638 sock:setsockname('*', 0);
632 sock:setpeername(self.server[servernum], 53); 639 sock:setpeername(peer, 53);
633 self.socket[servernum] = sock; 640 self.socket[servernum] = sock;
634 self.socketset[sock] = servernum; 641 self.socketset[sock] = servernum;
635 return sock; 642 return sock;
636 end 643 end
637 644