]>
Commit | Line | Data |
---|---|---|
e14f094b BH |
1 | /* |
2 | PowerDNS Versatile Database Driven Nameserver | |
3 | Copyright (C) 2002 PowerDNS.COM BV | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | */ | |
36c5ee42 | 19 | #include "utility.hh" |
e14f094b BH |
20 | #include "lwres.hh" |
21 | #include <pthread.h> | |
22 | #include <semaphore.h> | |
23 | #include <iostream> | |
24 | #include <errno.h> | |
25 | #include "misc.hh" | |
26 | #include <algorithm> | |
27 | #include <sstream> | |
28 | #include <cstring> | |
29 | #include <string> | |
30 | #include <vector> | |
31 | #include "dnspacket.hh" | |
32 | #include "dns.hh" | |
33 | #include "qtype.hh" | |
34 | #include "tcpreceiver.hh" | |
35 | #include "ahuexception.hh" | |
36 | #include "statbag.hh" | |
37 | #include "arguments.hh" | |
38 | ||
39 | ||
40 | LWRes::LWRes() | |
41 | { | |
42 | d_sock=-1; | |
43 | d_timeout=500000; | |
b636533b BH |
44 | d_bufsize=1500; |
45 | d_buf=new unsigned char[d_bufsize]; | |
e14f094b BH |
46 | } |
47 | ||
48 | LWRes::~LWRes() | |
49 | { | |
50 | if(d_sock>=0) | |
51 | Utility::closesocket(d_sock); | |
52 | delete[] d_buf; | |
53 | } | |
54 | ||
55 | ||
56 | //! returns -1 for permanent error, 0 for timeout, 1 for success | |
57 | /** Never throws! */ | |
58 | int LWRes::asyncresolve(const string &ip, const char *domain, int type) | |
59 | { | |
60 | DNSPacket p; | |
61 | p.setQuestion(Opcode::Query,domain,type); | |
62 | p.setRD(false); | |
63 | p.wrapup(); | |
64 | ||
65 | d_domain=domain; | |
66 | d_type=type; | |
67 | d_inaxfr=false; | |
36f5e3db | 68 | d_rcode=0; |
e14f094b BH |
69 | |
70 | struct sockaddr_in toaddr; | |
71 | struct in_addr inp; | |
72 | Utility::inet_aton(ip.c_str(),&inp); | |
73 | toaddr.sin_addr.s_addr=inp.s_addr; | |
74 | ||
75 | toaddr.sin_port=htons(53); | |
76 | toaddr.sin_family=AF_INET; | |
77 | ||
eefd15f9 BH |
78 | int ret; |
79 | ||
80 | DTime dt; | |
81 | dt.set(); | |
e14f094b BH |
82 | if(asendto(p.getData(), p.len, 0, (struct sockaddr*)(&toaddr), sizeof(toaddr),p.d.id)<0) { |
83 | return -1; | |
84 | } | |
eefd15f9 | 85 | |
e14f094b | 86 | Utility::socklen_t addrlen=sizeof(toaddr); |
eefd15f9 BH |
87 | |
88 | // sleep until we see an answer to this, interface to mtasker | |
89 | ||
90 | ret=arecvfrom(reinterpret_cast<char *>(d_buf), d_bufsize-1,0,(struct sockaddr*)(&toaddr), &addrlen, &d_len, p.d.id); | |
36c5ee42 BH |
91 | d_usec=dt.udiff(); |
92 | ||
eefd15f9 | 93 | return ret; |
e14f094b BH |
94 | } |
95 | ||
96 | ||
eefd15f9 | 97 | LWRes::res_t LWRes::result() |
e14f094b BH |
98 | { |
99 | DNSPacket p; | |
100 | ||
c836dc19 BH |
101 | try { |
102 | if(p.parse((char *)d_buf, d_len)<0) | |
103 | throw LWResException("resolver: unable to parse packet of "+itoa(d_len)+" bytes"); | |
eefd15f9 | 104 | d_aabit=p.d.aa; |
c836dc19 BH |
105 | d_rcode=p.d.rcode; |
106 | return p.getAnswers(); | |
107 | } | |
108 | catch(...) { | |
109 | d_rcode=RCode::ServFail; | |
110 | LWRes::res_t empty; | |
111 | return empty; | |
112 | } | |
e14f094b BH |
113 | } |
114 |