]>
Commit | Line | Data |
---|---|---|
e14f094b BH |
1 | /* |
2 | PowerDNS Versatile Database Driven Nameserver | |
d0166f4a | 3 | Copyright (C) 2002 - 2005 PowerDNS.COM BV |
e14f094b BH |
4 | |
5 | This program is free software; you can redistribute it and/or modify | |
d0166f4a BH |
6 | it under the terms of the GNU General Public License version 2 as |
7 | published by the Free Software Foundation | |
e14f094b BH |
8 | |
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; if not, write to the Free Software | |
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | */ | |
36c5ee42 | 18 | #include "utility.hh" |
e14f094b BH |
19 | #include "lwres.hh" |
20 | #include <pthread.h> | |
21 | #include <semaphore.h> | |
22 | #include <iostream> | |
23 | #include <errno.h> | |
24 | #include "misc.hh" | |
25 | #include <algorithm> | |
26 | #include <sstream> | |
27 | #include <cstring> | |
28 | #include <string> | |
29 | #include <vector> | |
30 | #include "dnspacket.hh" | |
31 | #include "dns.hh" | |
32 | #include "qtype.hh" | |
33 | #include "tcpreceiver.hh" | |
34 | #include "ahuexception.hh" | |
35 | #include "statbag.hh" | |
36 | #include "arguments.hh" | |
5c633640 BH |
37 | #include "sstuff.hh" |
38 | #include "syncres.hh" | |
e14f094b BH |
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! */ | |
5c633640 | 58 | int LWRes::asyncresolve(const string &ip, const char *domain, int type, bool doTCP) |
e14f094b BH |
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; | |
5c633640 | 72 | Utility::socklen_t addrlen=sizeof(toaddr); |
e14f094b BH |
73 | Utility::inet_aton(ip.c_str(),&inp); |
74 | toaddr.sin_addr.s_addr=inp.s_addr; | |
75 | ||
76 | toaddr.sin_port=htons(53); | |
77 | toaddr.sin_family=AF_INET; | |
78 | ||
eefd15f9 BH |
79 | int ret; |
80 | ||
81 | DTime dt; | |
82 | dt.set(); | |
5c633640 BH |
83 | |
84 | if(!doTCP) { | |
85 | if(asendto(p.getData(), p.len, 0, (struct sockaddr*)(&toaddr), sizeof(toaddr),p.d.id)<0) { | |
86 | return -1; | |
87 | } | |
88 | ||
89 | // sleep until we see an answer to this, interface to mtasker | |
90 | ||
91 | ret=arecvfrom(reinterpret_cast<char *>(d_buf), d_bufsize-1,0,(struct sockaddr*)(&toaddr), &addrlen, &d_len, p.d.id); | |
e14f094b | 92 | } |
5c633640 BH |
93 | else { |
94 | Socket s(InterNetwork, Stream); | |
95 | IPEndpoint ie(ip, 53); | |
96 | s.setNonBlocking(); | |
97 | s.connect(ie); | |
98 | ||
99 | int len=htons(p.len); | |
100 | char *lenP=(char*)&len; | |
101 | const char *msgP=p.getData(); | |
102 | string packet=string(lenP, lenP+2)+string(msgP, msgP+p.len); | |
103 | ||
104 | if(asendtcp(packet, &s) == 0) { | |
d0166f4a BH |
105 | // cerr<<"asendtcp: timeout"<<endl; |
106 | return 0; | |
5c633640 | 107 | } |
eefd15f9 | 108 | |
5c633640 BH |
109 | packet.clear(); |
110 | if(arecvtcp(packet,2, &s)==0) { | |
d0166f4a BH |
111 | // cerr<<"arecvtcp: timeout"<<endl; |
112 | return 0; | |
5c633640 BH |
113 | } |
114 | ||
115 | memcpy(&len, packet.c_str(), 2); | |
116 | len=ntohs(len); | |
117 | ||
118 | // cerr<<"Now reading "<<len<<" bytes"<<endl; | |
119 | ||
120 | if(arecvtcp(packet, len, &s)==0) { | |
d0166f4a BH |
121 | // cerr<<"arecvtcp: timeout"<<endl; |
122 | return 0; | |
5c633640 BH |
123 | } |
124 | ||
125 | memcpy(d_buf, packet.c_str(), len); | |
126 | d_len=len; | |
127 | ret=1; | |
128 | } | |
129 | d_usec=dt.udiff(); | |
36c5ee42 | 130 | |
eefd15f9 | 131 | return ret; |
e14f094b BH |
132 | } |
133 | ||
134 | ||
eefd15f9 | 135 | LWRes::res_t LWRes::result() |
e14f094b BH |
136 | { |
137 | DNSPacket p; | |
138 | ||
c836dc19 BH |
139 | try { |
140 | if(p.parse((char *)d_buf, d_len)<0) | |
141 | throw LWResException("resolver: unable to parse packet of "+itoa(d_len)+" bytes"); | |
eefd15f9 | 142 | d_aabit=p.d.aa; |
d0166f4a | 143 | d_tcbit=p.d.tc; |
c836dc19 BH |
144 | d_rcode=p.d.rcode; |
145 | return p.getAnswers(); | |
146 | } | |
147 | catch(...) { | |
148 | d_rcode=RCode::ServFail; | |
149 | LWRes::res_t empty; | |
150 | return empty; | |
151 | } | |
e14f094b BH |
152 | } |
153 |