Software /
code /
prosody
Comparison
net/dns.lua @ 6054:7a5ddbaf758d
Merge 0.9->0.10
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 02 Apr 2014 17:41:38 +0100 |
parent | 5776:bd0ff8ae98a8 |
child | 6289:a29cc79295e6 |
comparison
equal
deleted
inserted
replaced
6053:2f93a04564b2 | 6054:7a5ddbaf758d |
---|---|
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 sock and self.socket_wrapper then sock, err = self.socket_wrapper(sock, self); end | 632 if sock and self.socket_wrapper then sock, err = self.socket_wrapper(sock, self); end |
626 if not sock then | 633 if not sock then |
627 return nil, err; | 634 return nil, err; |
628 end | 635 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 |
744 local conn, err = self:getsocket(o.server) | 751 local conn, err = self:getsocket(o.server) |
745 if not conn then | 752 if not conn then |
746 return nil, err; | 753 return nil, err; |
747 end | 754 end |
748 conn:send (o.packet) | 755 conn:send (o.packet) |
749 | 756 |
750 if timer and self.timeout then | 757 if timer and self.timeout then |
751 local num_servers = #self.server; | 758 local num_servers = #self.server; |
752 local i = 1; | 759 local i = 1; |
753 timer.add_task(self.timeout, function () | 760 timer.add_task(self.timeout, function () |
754 if get(self.wanted, qclass, qtype, qname, co) then | 761 if get(self.wanted, qclass, qtype, qname, co) then |
840 end | 847 end |
841 | 848 |
842 -- retire the query | 849 -- retire the query |
843 local queries = self.active[response.header.id]; | 850 local queries = self.active[response.header.id]; |
844 queries[response.question.raw] = nil; | 851 queries[response.question.raw] = nil; |
845 | 852 |
846 if not next(queries) then self.active[response.header.id] = nil; end | 853 if not next(queries) then self.active[response.header.id] = nil; end |
847 if not next(self.active) then self:closeall(); end | 854 if not next(self.active) then self:closeall(); end |
848 | 855 |
849 -- was the query on the wanted list? | 856 -- was the query on the wanted list? |
850 local q = response.question[1]; | 857 local q = response.question[1]; |
855 if coroutine.status(co) == "suspended" then coroutine.resume(co); end | 862 if coroutine.status(co) == "suspended" then coroutine.resume(co); end |
856 end | 863 end |
857 set(self.wanted, q.class, q.type, q.name, nil); | 864 set(self.wanted, q.class, q.type, q.name, nil); |
858 end | 865 end |
859 end | 866 end |
860 | 867 |
861 end | 868 end |
862 end | 869 end |
863 end | 870 end |
864 | 871 |
865 return response; | 872 return response; |