]>
Commit | Line | Data |
---|---|---|
12c86877 BH |
1 | /* |
2 | PowerDNS Versatile Database Driven Nameserver | |
2e7834cb | 3 | Copyright (C) 2002-2012 PowerDNS.COM BV |
12c86877 BH |
4 | |
5 | This program is free software; you can redistribute it and/or modify | |
22dc646a BH |
6 | it under the terms of the GNU General Public License version 2 |
7 | as published by the Free Software Foundation | |
8 | ||
12c86877 BH |
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 | |
06bd9ccf | 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
12c86877 | 17 | */ |
379ab445 | 18 | #include "packetcache.hh" |
1258abe0 | 19 | #include "utility.hh" |
add640c0 | 20 | #include "dnssecinfra.hh" |
4c1474f3 | 21 | #include "dnsseckeeper.hh" |
12c86877 | 22 | #include <cstdio> |
4888e4b2 | 23 | #include "base32.hh" |
12c86877 BH |
24 | #include <cstring> |
25 | #include <cstdlib> | |
26 | #include <sys/types.h> | |
27 | #include <iostream> | |
28 | #include <string> | |
29 | #include "tcpreceiver.hh" | |
67d74e49 | 30 | #include "sstuff.hh" |
8edfedf1 | 31 | #include <boost/foreach.hpp> |
12c86877 BH |
32 | #include <errno.h> |
33 | #include <signal.h> | |
78bcb858 | 34 | #include "base64.hh" |
12c86877 BH |
35 | #include "ueberbackend.hh" |
36 | #include "dnspacket.hh" | |
37 | #include "nameserver.hh" | |
38 | #include "distributor.hh" | |
39 | #include "lock.hh" | |
40 | #include "logger.hh" | |
41 | #include "arguments.hh" | |
379ab445 | 42 | |
12c86877 BH |
43 | #include "packethandler.hh" |
44 | #include "statbag.hh" | |
45 | #include "resolver.hh" | |
46 | #include "communicator.hh" | |
61b26744 | 47 | #include "namespaces.hh" |
8e9b7d99 | 48 | #include "signingpipe.hh" |
12c86877 BH |
49 | extern PacketCache PC; |
50 | extern StatBag S; | |
51 | ||
52 | /** | |
53 | \file tcpreceiver.cc | |
54 | \brief This file implements the tcpreceiver that receives and answers questions over TCP/IP | |
55 | */ | |
56 | ||
ac2bb9e7 | 57 | pthread_mutex_t TCPNameserver::s_plock = PTHREAD_MUTEX_INITIALIZER; |
12c86877 BH |
58 | Semaphore *TCPNameserver::d_connectionroom_sem; |
59 | PacketHandler *TCPNameserver::s_P; | |
60 | int TCPNameserver::s_timeout; | |
9f1d5826 | 61 | NetmaskGroup TCPNameserver::d_ng; |
12c86877 | 62 | |
12c86877 BH |
63 | void TCPNameserver::go() |
64 | { | |
65 | L<<Logger::Error<<"Creating backend connection for TCP"<<endl; | |
66 | s_P=0; | |
67 | try { | |
68 | s_P=new PacketHandler; | |
69 | } | |
70 | catch(AhuException &ae) { | |
71 | L<<Logger::Error<<Logger::NTLog<<"TCP server is unable to launch backends - will try again when questions come in"<<endl; | |
fd8bc993 | 72 | L<<Logger::Error<<"TCP server is unable to launch backends - will try again when questions come in: "<<ae.reason<<endl; |
12c86877 BH |
73 | } |
74 | pthread_create(&d_tid, 0, launcher, static_cast<void *>(this)); | |
75 | } | |
76 | ||
77 | void *TCPNameserver::launcher(void *data) | |
78 | { | |
79 | static_cast<TCPNameserver *>(data)->thread(); | |
80 | return 0; | |
81 | } | |
82 | ||
6a3e5d1a BH |
83 | // throws AhuException if things didn't go according to plan, returns 0 if really 0 bytes were read |
84 | int readnWithTimeout(int fd, void* buffer, unsigned int n, bool throwOnEOF=true) | |
12c86877 | 85 | { |
6a3e5d1a BH |
86 | unsigned int bytes=n; |
87 | char *ptr = (char*)buffer; | |
88 | int ret; | |
89 | while(bytes) { | |
90 | ret=read(fd, ptr, bytes); | |
91 | if(ret < 0) { | |
92 | if(errno==EAGAIN) { | |
4957a608 BH |
93 | ret=waitForData(fd, 5); |
94 | if(ret < 0) | |
95 | throw NetworkError("Waiting for data read"); | |
96 | if(!ret) | |
97 | throw NetworkError("Timeout reading data"); | |
98 | continue; | |
6a3e5d1a BH |
99 | } |
100 | else | |
4957a608 | 101 | throw NetworkError("Reading data: "+stringerror()); |
6a3e5d1a BH |
102 | } |
103 | if(!ret) { | |
104 | if(!throwOnEOF && n == bytes) | |
4957a608 | 105 | return 0; |
6a3e5d1a | 106 | else |
4957a608 | 107 | throw NetworkError("Did not fulfill read from TCP due to EOF"); |
6a3e5d1a BH |
108 | } |
109 | ||
110 | ptr += ret; | |
111 | bytes -= ret; | |
112 | } | |
113 | return n; | |
114 | } | |
12c86877 | 115 | |
6a3e5d1a BH |
116 | // ditto |
117 | void writenWithTimeout(int fd, const void *buffer, unsigned int n) | |
118 | { | |
119 | unsigned int bytes=n; | |
120 | const char *ptr = (char*)buffer; | |
121 | int ret; | |
122 | while(bytes) { | |
123 | ret=write(fd, ptr, bytes); | |
124 | if(ret < 0) { | |
125 | if(errno==EAGAIN) { | |
4957a608 BH |
126 | ret=waitForRWData(fd, false, 5, 0); |
127 | if(ret < 0) | |
128 | throw NetworkError("Waiting for data write"); | |
129 | if(!ret) | |
130 | throw NetworkError("Timeout writing data"); | |
131 | continue; | |
6a3e5d1a BH |
132 | } |
133 | else | |
4957a608 | 134 | throw NetworkError("Writing data: "+stringerror()); |
6a3e5d1a | 135 | } |
12c86877 | 136 | if(!ret) { |
67d74e49 | 137 | throw NetworkError("Did not fulfill TCP write due to EOF"); |
12c86877 | 138 | } |
6a3e5d1a BH |
139 | |
140 | ptr += ret; | |
141 | bytes -= ret; | |
12c86877 | 142 | } |
12c86877 BH |
143 | } |
144 | ||
6a3e5d1a | 145 | void connectWithTimeout(int fd, struct sockaddr* remote, size_t socklen) |
12c86877 | 146 | { |
6a3e5d1a BH |
147 | int err; |
148 | Utility::socklen_t len=sizeof(err); | |
149 | ||
150 | #ifndef WIN32 | |
151 | if((err=connect(fd, remote, socklen))<0 && errno!=EINPROGRESS) | |
152 | #else | |
153 | if((err=connect(clisock, remote, socklen))<0 && WSAGetLastError() != WSAEWOULDBLOCK ) | |
154 | #endif // WIN32 | |
67d74e49 | 155 | throw NetworkError("connect: "+stringerror()); |
6a3e5d1a BH |
156 | |
157 | if(!err) | |
158 | goto done; | |
159 | ||
160 | err=waitForRWData(fd, false, 5, 0); | |
161 | if(err == 0) | |
67d74e49 | 162 | throw NetworkError("Timeout connecting to remote"); |
6a3e5d1a | 163 | if(err < 0) |
67d74e49 | 164 | throw NetworkError("Error connecting to remote"); |
12c86877 | 165 | |
6a3e5d1a | 166 | if(getsockopt(fd, SOL_SOCKET,SO_ERROR,(char *)&err,&len)<0) |
67d74e49 | 167 | throw NetworkError("Error connecting to remote: "+stringerror()); // Solaris |
6a3e5d1a BH |
168 | |
169 | if(err) | |
67d74e49 | 170 | throw NetworkError("Error connecting to remote: "+string(strerror(err))); |
6a3e5d1a BH |
171 | |
172 | done: | |
173 | ; | |
174 | } | |
12c86877 | 175 | |
6a3e5d1a BH |
176 | void TCPNameserver::sendPacket(shared_ptr<DNSPacket> p, int outsock) |
177 | { | |
fbaa5e09 BH |
178 | uint16_t len=htons(p->getString().length()); |
179 | string buffer((const char*)&len, 2); | |
180 | buffer.append(p->getString()); | |
78bcb858 | 181 | writenWithTimeout(outsock, buffer.c_str(), buffer.length()); |
6a3e5d1a BH |
182 | } |
183 | ||
184 | ||
185 | void TCPNameserver::getQuestion(int fd, char *mesg, int pktlen, const ComboAddress &remote) | |
186 | try | |
187 | { | |
188 | readnWithTimeout(fd, mesg, pktlen); | |
189 | } | |
67d74e49 BH |
190 | catch(NetworkError& ae) { |
191 | throw NetworkError("Error reading DNS data from TCP client "+remote.toString()+": "+ae.what()); | |
12c86877 BH |
192 | } |
193 | ||
ff76e8b4 BH |
194 | static void proxyQuestion(shared_ptr<DNSPacket> packet) |
195 | { | |
196 | int sock=socket(AF_INET, SOCK_STREAM, 0); | |
326484be | 197 | |
42c235e5 | 198 | Utility::setCloseOnExec(sock); |
ff76e8b4 | 199 | if(sock < 0) |
67d74e49 | 200 | throw NetworkError("Error making TCP connection socket to recursor: "+stringerror()); |
ff76e8b4 | 201 | |
6a3e5d1a BH |
202 | Utility::setNonBlocking(sock); |
203 | ServiceTuple st; | |
204 | st.port=53; | |
379ab445 | 205 | parseService(::arg()["recursor"],st); |
6a3e5d1a | 206 | |
ff76e8b4 | 207 | try { |
ff76e8b4 | 208 | ComboAddress recursor(st.host, st.port); |
6a3e5d1a | 209 | connectWithTimeout(sock, (struct sockaddr*)&recursor, recursor.getSocklen()); |
ff76e8b4 BH |
210 | const string &buffer=packet->getString(); |
211 | ||
212 | uint16_t len=htons(buffer.length()), slen; | |
213 | ||
6a3e5d1a BH |
214 | writenWithTimeout(sock, &len, 2); |
215 | writenWithTimeout(sock, buffer.c_str(), buffer.length()); | |
ff76e8b4 | 216 | |
c20eca2b | 217 | readnWithTimeout(sock, &len, 2); |
ff76e8b4 BH |
218 | len=ntohs(len); |
219 | ||
220 | char answer[len]; | |
c20eca2b | 221 | readnWithTimeout(sock, answer, len); |
ff76e8b4 BH |
222 | |
223 | slen=htons(len); | |
6a3e5d1a | 224 | writenWithTimeout(packet->getSocket(), &slen, 2); |
ff76e8b4 | 225 | |
6a3e5d1a | 226 | writenWithTimeout(packet->getSocket(), answer, len); |
ff76e8b4 | 227 | } |
67d74e49 | 228 | catch(NetworkError& ae) { |
ff76e8b4 | 229 | close(sock); |
67d74e49 | 230 | throw NetworkError("While proxying a question to recursor "+st.host+": " +ae.what()); |
ff76e8b4 BH |
231 | } |
232 | close(sock); | |
233 | return; | |
234 | } | |
235 | ||
12c86877 BH |
236 | void *TCPNameserver::doConnection(void *data) |
237 | { | |
ff76e8b4 | 238 | shared_ptr<DNSPacket> packet; |
b014ad98 BH |
239 | // Fix gcc-4.0 error (on AMD64) |
240 | int fd=(int)(long)data; // gotta love C (generates a harmless warning on opteron) | |
12c86877 | 241 | pthread_detach(pthread_self()); |
027ffd26 | 242 | Utility::setNonBlocking(fd); |
12c86877 | 243 | try { |
12c86877 BH |
244 | char mesg[512]; |
245 | ||
246 | DLOG(L<<"TCP Connection accepted on fd "<<fd<<endl); | |
21a303f3 | 247 | bool logDNSQueries= ::arg().mustDo("log-dns-queries"); |
12c86877 | 248 | for(;;) { |
809fe23f | 249 | ComboAddress remote; |
38a9b470 BH |
250 | socklen_t remotelen=sizeof(remote); |
251 | if(getpeername(fd, (struct sockaddr *)&remote, &remotelen) < 0) { | |
4957a608 BH |
252 | L<<Logger::Error<<"Received question from socket which had no remote address, dropping ("<<stringerror()<<")"<<endl; |
253 | break; | |
38a9b470 | 254 | } |
6a3e5d1a BH |
255 | |
256 | uint16_t pktlen; | |
257 | if(!readnWithTimeout(fd, &pktlen, 2, false)) | |
4957a608 | 258 | break; |
6a3e5d1a | 259 | else |
4957a608 | 260 | pktlen=ntohs(pktlen); |
12c86877 BH |
261 | |
262 | if(pktlen>511) { | |
4957a608 BH |
263 | L<<Logger::Error<<"Received an overly large question from "<<remote.toString()<<", dropping"<<endl; |
264 | break; | |
12c86877 BH |
265 | } |
266 | ||
78bcb858 | 267 | getQuestion(fd, mesg, pktlen, remote); |
12c86877 | 268 | S.inc("tcp-queries"); |
3e579e91 | 269 | |
ff76e8b4 | 270 | packet=shared_ptr<DNSPacket>(new DNSPacket); |
809fe23f | 271 | packet->setRemote(&remote); |
e9dd48f9 | 272 | packet->d_tcp=true; |
ff76e8b4 | 273 | packet->setSocket(fd); |
c1663439 | 274 | if(packet->parse(mesg, pktlen)<0) |
4957a608 | 275 | break; |
c1663439 | 276 | |
af1311e4 | 277 | if(packet->qtype.getCode()==QType::AXFR || packet->qtype.getCode()==QType::IXFR ) { |
4957a608 BH |
278 | if(doAXFR(packet->qdomain, packet, fd)) |
279 | S.inc("tcp-answers"); | |
280 | continue; | |
12c86877 BH |
281 | } |
282 | ||
ff76e8b4 | 283 | shared_ptr<DNSPacket> reply; |
ff76e8b4 | 284 | shared_ptr<DNSPacket> cached= shared_ptr<DNSPacket>(new DNSPacket); |
fe498ace BH |
285 | if(logDNSQueries) { |
286 | string remote; | |
287 | if(packet->hasEDNSSubnet()) | |
288 | remote = packet->getRemote() + "<-" + packet->getRealRemote().toString(); | |
289 | else | |
290 | remote = packet->getRemote(); | |
291 | L << Logger::Notice<<"TCP Remote "<< remote <<" wants '" << packet->qdomain<<"|"<<packet->qtype.getName() << | |
bb5903e2 | 292 | "', do = " <<packet->d_dnssecOk <<", bufsize = "<< packet->getMaxReplyLen()<<": "; |
fe498ace | 293 | } |
bb5903e2 | 294 | |
ff76e8b4 | 295 | |
bb5903e2 | 296 | if(!packet->d.rd && packet->couldBeCached() && PC.get(packet.get(), cached.get())) { // short circuit - does the PacketCache recognize this question? |
21a303f3 | 297 | if(logDNSQueries) |
bb5903e2 | 298 | L<<"packetcache HIT"<<endl; |
d06799d4 | 299 | cached->setRemote(&packet->d_remote); |
4957a608 BH |
300 | cached->d.id=packet->d.id; |
301 | cached->d.rd=packet->d.rd; // copy in recursion desired bit | |
302 | cached->commitD(); // commit d to the packet inlined | |
303 | ||
e02d0a59 | 304 | sendPacket(cached, fd); // presigned, don't do it again |
4957a608 BH |
305 | S.inc("tcp-answers"); |
306 | continue; | |
12c86877 | 307 | } |
21a303f3 | 308 | if(logDNSQueries) |
bb5903e2 | 309 | L<<"packetcache MISS"<<endl; |
12c86877 | 310 | { |
4957a608 BH |
311 | Lock l(&s_plock); |
312 | if(!s_P) { | |
313 | L<<Logger::Error<<"TCP server is without backend connections, launching"<<endl; | |
314 | s_P=new PacketHandler; | |
315 | } | |
316 | bool shouldRecurse; | |
317 | ||
318 | reply=shared_ptr<DNSPacket>(s_P->questionOrRecurse(packet.get(), &shouldRecurse)); // we really need to ask the backend :-) | |
319 | ||
320 | if(shouldRecurse) { | |
321 | proxyQuestion(packet); | |
322 | continue; | |
323 | } | |
12c86877 BH |
324 | } |
325 | ||
12c86877 | 326 | if(!reply) // unable to write an answer? |
4957a608 BH |
327 | break; |
328 | ||
12c86877 | 329 | S.inc("tcp-answers"); |
ff76e8b4 | 330 | sendPacket(reply, fd); |
12c86877 | 331 | } |
12c86877 | 332 | } |
cc3afe25 | 333 | catch(DBException &e) { |
556252ea BH |
334 | Lock l(&s_plock); |
335 | delete s_P; | |
336 | s_P = 0; | |
337 | ||
338 | L<<Logger::Error<<"TCP Connection Thread unable to answer a question because of a backend error, cycling"<<endl; | |
12c86877 | 339 | } |
ef1d2f44 | 340 | catch(AhuException &ae) { |
556252ea BH |
341 | Lock l(&s_plock); |
342 | delete s_P; | |
343 | s_P = 0; // on next call, backend will be recycled | |
027ffd26 | 344 | L<<Logger::Error<<"TCP nameserver had error, cycling backend: "<<ae.reason<<endl; |
ef1d2f44 | 345 | } |
0afa9049 | 346 | catch(NetworkError &e) { |
2e7834cb | 347 | L<<Logger::Info<<"TCP Connection Thread died because of network error: "<<e.what()<<endl; |
0afa9049 BH |
348 | } |
349 | ||
adc10f99 | 350 | catch(std::exception &e) { |
12c86877 BH |
351 | L<<Logger::Error<<"TCP Connection Thread died because of STL error: "<<e.what()<<endl; |
352 | } | |
353 | catch( ... ) | |
354 | { | |
355 | L << Logger::Error << "TCP Connection Thread caught unknown exception." << endl; | |
356 | } | |
12c86877 | 357 | d_connectionroom_sem->post(); |
c38f6509 | 358 | Utility::closesocket(fd); |
12c86877 BH |
359 | |
360 | return 0; | |
361 | } | |
362 | ||
78bcb858 | 363 | |
e082fb4c | 364 | // call this method with s_plock held! |
ff76e8b4 | 365 | bool TCPNameserver::canDoAXFR(shared_ptr<DNSPacket> q) |
12c86877 | 366 | { |
379ab445 | 367 | if(::arg().mustDo("disable-axfr")) |
318c3ec6 BH |
368 | return false; |
369 | ||
78bcb858 BH |
370 | if(q->d_havetsig) { // if you have one, it must be good |
371 | TSIGRecordContent trc; | |
372 | string keyname, secret; | |
78bcb858 BH |
373 | if(!checkForCorrectTSIG(q.get(), s_P->getBackend(), &keyname, &secret, &trc)) |
374 | return false; | |
375 | ||
376 | DNSSECKeeper dk; | |
377 | ||
378 | if(!dk.TSIGGrantsAccess(q->qdomain, keyname, trc.d_algoName)) { | |
379 | L<<Logger::Error<<"AXFR '"<<q->qdomain<<"' denied: key with name '"<<keyname<<"' and algorithm '"<<trc.d_algoName<<"' does not grant access to zone"<<endl; | |
380 | return false; | |
381 | } | |
382 | else { | |
383 | L<<Logger::Warning<<"AXFR of domain '"<<q->qdomain<<"' allowed: TSIG signed request with authorized key '"<<keyname<<"' and algorithm '"<<trc.d_algoName<<"'"<<endl; | |
384 | return true; | |
385 | } | |
386 | } | |
93afc0a3 PD |
387 | |
388 | // cerr<<"checking allow-axfr-ips"<<endl; | |
389 | if(!(::arg()["allow-axfr-ips"].empty()) && d_ng.match( (ComboAddress *) &q->d_remote )) { | |
390 | L<<Logger::Warning<<"AXFR of domain '"<<q->qdomain<<"' allowed: client IP "<<q->getRemote()<<" is in allow-axfr-ips"<<endl; | |
12c86877 | 391 | return true; |
ab5edd12 | 392 | } |
93afc0a3 PD |
393 | |
394 | FindNS fns; | |
395 | ||
396 | // cerr<<"doing per-zone-axfr-acls"<<endl; | |
397 | SOAData sd; | |
398 | sd.db=(DNSBackend *)-1; | |
399 | if(s_P->getBackend()->getSOA(q->qdomain,sd)) { | |
400 | // cerr<<"got backend and SOA"<<endl; | |
401 | DNSBackend *B=sd.db; | |
402 | vector<string> acl; | |
403 | B->getDomainMetadata(q->qdomain, "ALLOW-AXFR-FROM", acl); | |
404 | for (vector<string>::const_iterator i = acl.begin(); i != acl.end(); ++i) { | |
405 | // cerr<<"matching against "<<*i<<endl; | |
406 | if(pdns_iequals(*i, "AUTO-NS")) { | |
407 | // cerr<<"AUTO-NS magic please!"<<endl; | |
408 | ||
409 | DNSResourceRecord rr; | |
410 | set<string> nsset; | |
411 | ||
412 | B->lookup(QType(QType::NS),q->qdomain); | |
413 | while(B->get(rr)) | |
414 | nsset.insert(rr.content); | |
415 | for(set<string>::const_iterator j=nsset.begin();j!=nsset.end();++j) { | |
416 | vector<string> nsips=fns.lookup(*j, B); | |
417 | for(vector<string>::const_iterator k=nsips.begin();k!=nsips.end();++k) { | |
418 | // cerr<<"got "<<*k<<" from AUTO-NS"<<endl; | |
419 | if(*k == q->getRemote()) | |
420 | { | |
421 | // cerr<<"got AUTO-NS hit"<<endl; | |
422 | L<<Logger::Warning<<"AXFR of domain '"<<q->qdomain<<"' allowed: client IP "<<q->getRemote()<<" is in NSset"<<endl; | |
423 | return true; | |
424 | } | |
425 | } | |
426 | } | |
427 | } | |
428 | else | |
429 | { | |
430 | Netmask nm = Netmask(*i); | |
431 | if(nm.match( (ComboAddress *) &q->d_remote )) | |
432 | { | |
433 | L<<Logger::Warning<<"AXFR of domain '"<<q->qdomain<<"' allowed: client IP "<<q->getRemote()<<" is in per-domain ACL"<<endl; | |
434 | // cerr<<"hit!"<<endl; | |
435 | return true; | |
436 | } | |
437 | } | |
438 | } | |
439 | } | |
440 | ||
12c86877 BH |
441 | extern CommunicatorClass Communicator; |
442 | ||
443 | if(Communicator.justNotified(q->qdomain, q->getRemote())) { // we just notified this ip | |
444 | L<<Logger::Warning<<"Approved AXFR of '"<<q->qdomain<<"' from recently notified slave "<<q->getRemote()<<endl; | |
445 | return true; | |
446 | } | |
447 | ||
93afc0a3 | 448 | L<<Logger::Error<<"AXFR of domain '"<<q->qdomain<<"' denied: client IP "<<q->getRemote()<<" has no permission"<<endl; |
12c86877 BH |
449 | return false; |
450 | } | |
451 | ||
b317b510 | 452 | namespace { |
54d84273 PD |
453 | struct NSECXEntry |
454 | { | |
455 | set<uint16_t> d_set; | |
456 | unsigned int d_ttl; | |
457 | }; | |
8e9b7d99 | 458 | |
54d84273 PD |
459 | DNSResourceRecord makeDNSRRFromSOAData(const SOAData& sd) |
460 | { | |
461 | DNSResourceRecord soa; | |
462 | soa.qname= sd.qname; | |
463 | soa.qtype=QType::SOA; | |
464 | soa.content=serializeSOAData(sd); | |
465 | soa.ttl=sd.ttl; | |
466 | soa.domain_id=sd.domain_id; | |
467 | soa.auth = true; | |
468 | soa.d_place=DNSResourceRecord::ANSWER; | |
469 | return soa; | |
470 | } | |
8e9b7d99 | 471 | |
54d84273 PD |
472 | shared_ptr<DNSPacket> getFreshAXFRPacket(shared_ptr<DNSPacket> q) |
473 | { | |
474 | shared_ptr<DNSPacket> ret = shared_ptr<DNSPacket>(q->replyPacket()); | |
475 | ret->setCompress(false); | |
476 | ret->d_dnssecOk=false; // RFC 5936, 2.2.5 | |
477 | ret->d_tcp = true; | |
478 | return ret; | |
479 | } | |
8e9b7d99 BH |
480 | } |
481 | ||
54d84273 | 482 | |
12c86877 | 483 | /** do the actual zone transfer. Return 0 in case of error, 1 in case of success */ |
ff76e8b4 | 484 | int TCPNameserver::doAXFR(const string &target, shared_ptr<DNSPacket> q, int outsock) |
12c86877 | 485 | { |
4888e4b2 BH |
486 | bool noAXFRBecauseOfNSEC3Narrow=false; |
487 | NSEC3PARAMRecordContent ns3pr; | |
488 | bool narrow; | |
489 | bool NSEC3Zone=false; | |
8e9b7d99 BH |
490 | |
491 | DNSSECKeeper dk; | |
627d2ca2 | 492 | dk.clearCaches(target); |
8267bd2c | 493 | bool securedZone = dk.isSecuredZone(target); |
4888e4b2 BH |
494 | if(dk.getNSEC3PARAM(target, &ns3pr, &narrow)) { |
495 | NSEC3Zone=true; | |
496 | if(narrow) { | |
497 | L<<Logger::Error<<"Not doing AXFR of an NSEC3 narrow zone.."<<endl; | |
498 | noAXFRBecauseOfNSEC3Narrow=true; | |
499 | } | |
83fcecff | 500 | } |
78bcb858 | 501 | |
8e9b7d99 | 502 | shared_ptr<DNSPacket> outpacket= getFreshAXFRPacket(q); |
c67e46a1 | 503 | if(q->d_dnssecOk) |
05e24311 | 504 | outpacket->d_dnssecOk=true; // RFC 5936, 2.2.5 'SHOULD' |
8e9b7d99 | 505 | |
e082fb4c | 506 | if(noAXFRBecauseOfNSEC3Narrow) { |
20ca8e7d | 507 | L<<Logger::Error<<"AXFR of domain '"<<target<<"' denied to "<<q->getRemote()<<endl; |
12c86877 BH |
508 | outpacket->setRcode(RCode::Refused); |
509 | // FIXME: should actually figure out if we are auth over a zone, and send out 9 if we aren't | |
ff76e8b4 | 510 | sendPacket(outpacket,outsock); |
12c86877 BH |
511 | return 0; |
512 | } | |
83fcecff | 513 | |
20ca8e7d | 514 | L<<Logger::Error<<"AXFR of domain '"<<target<<"' initiated by "<<q->getRemote()<<endl; |
12c86877 BH |
515 | |
516 | SOAData sd; | |
3de83124 | 517 | sd.db=(DNSBackend *)-1; // force uncached answer |
12c86877 BH |
518 | { |
519 | Lock l(&s_plock); | |
8e9b7d99 | 520 | DLOG(L<<"Looking for SOA"<<endl); // find domain_id via SOA and list complete domain. No SOA, no AXFR |
12a965c5 BH |
521 | if(!s_P) { |
522 | L<<Logger::Error<<"TCP server is without backend connections in doAXFR, launching"<<endl; | |
523 | s_P=new PacketHandler; | |
524 | } | |
12c86877 | 525 | |
e082fb4c | 526 | if(!s_P->getBackend()->getSOA(target, sd) || !canDoAXFR(q)) { |
d10f9034 | 527 | L<<Logger::Error<<"AXFR of domain '"<<target<<"' failed: not authoritative"<<endl; |
12c86877 | 528 | outpacket->setRcode(9); // 'NOTAUTH' |
ff76e8b4 | 529 | sendPacket(outpacket,outsock); |
12c86877 BH |
530 | return 0; |
531 | } | |
3de83124 | 532 | } |
8e9b7d99 BH |
533 | |
534 | UeberBackend db; | |
3de83124 | 535 | sd.db=(DNSBackend *)-1; // force uncached answer |
8e9b7d99 BH |
536 | if(!db.getSOA(target, sd)) { |
537 | L<<Logger::Error<<"AXFR of domain '"<<target<<"' failed: not authoritative in second instance"<<endl; | |
3de83124 | 538 | outpacket->setRcode(9); // 'NOTAUTH' |
ff76e8b4 | 539 | sendPacket(outpacket,outsock); |
3de83124 BH |
540 | return 0; |
541 | } | |
7b308b7e | 542 | |
3de83124 BH |
543 | if(!sd.db || sd.db==(DNSBackend *)-1) { |
544 | L<<Logger::Error<<"Error determining backend for domain '"<<target<<"' trying to serve an AXFR"<<endl; | |
545 | outpacket->setRcode(RCode::ServFail); | |
ff76e8b4 | 546 | sendPacket(outpacket,outsock); |
3de83124 | 547 | return 0; |
12c86877 | 548 | } |
3de83124 | 549 | |
78bcb858 BH |
550 | TSIGRecordContent trc; |
551 | string tsigkeyname, tsigsecret; | |
552 | ||
553 | q->getTSIGDetails(&trc, &tsigkeyname, 0); | |
554 | ||
555 | if(!tsigkeyname.empty()) { | |
556 | string tsig64, algorithm; | |
557 | Lock l(&s_plock); | |
558 | s_P->getBackend()->getTSIGKey(tsigkeyname, &algorithm, &tsig64); | |
559 | B64Decode(tsig64, tsigsecret); | |
560 | } | |
8e9b7d99 | 561 | |
8e9b7d99 BH |
562 | |
563 | UeberBackend signatureDB; | |
8e9b7d99 | 564 | |
8267bd2c | 565 | // SOA *must* go out first, our signing pipe might reorder |
12c86877 | 566 | DLOG(L<<"Sending out SOA"<<endl); |
8267bd2c BH |
567 | DNSResourceRecord soa = makeDNSRRFromSOAData(sd); |
568 | outpacket->addRecord(soa); | |
92c90b44 | 569 | editSOA(dk, sd.qname, outpacket.get()); |
8d3cbffa BH |
570 | if(securedZone) { |
571 | set<string, CIStringCompare> authSet; | |
572 | authSet.insert(target); | |
573 | addRRSigs(dk, signatureDB, authSet, outpacket->getRRS()); | |
574 | } | |
8e9b7d99 | 575 | |
78bcb858 BH |
576 | if(!tsigkeyname.empty()) |
577 | outpacket->setTSIGDetails(trc, tsigkeyname, tsigsecret, trc.d_mac); // first answer is 'normal' | |
578 | ||
8267bd2c | 579 | sendPacket(outpacket, outsock); |
78bcb858 BH |
580 | |
581 | trc.d_mac = outpacket->d_trc.d_mac; | |
8267bd2c BH |
582 | outpacket = getFreshAXFRPacket(q); |
583 | ||
1c6d9830 | 584 | ChunkedSigningPipe csp(target, securedZone, "", ::arg().asNum("signing-threads")); |
8e9b7d99 | 585 | |
bec14a20 | 586 | typedef map<string, NSECXEntry> nsecxrepo_t; |
9d3151d9 | 587 | nsecxrepo_t nsecxrepo; |
4888e4b2 BH |
588 | |
589 | // this is where the DNSKEYs go in | |
0c350cb5 | 590 | |
4c1474f3 | 591 | DNSSECKeeper::keyset_t keys = dk.getKeys(target); |
0c350cb5 | 592 | |
8e9b7d99 | 593 | DNSResourceRecord rr; |
0c350cb5 | 594 | |
794c2f92 PD |
595 | rr.qname = target; |
596 | rr.ttl = sd.default_ttl; | |
597 | rr.auth = 1; // please sign! | |
598 | ||
4c1474f3 | 599 | BOOST_FOREACH(const DNSSECKeeper::keyset_t::value_type& value, keys) { |
4c1474f3 | 600 | rr.qtype = QType(QType::DNSKEY); |
4c1474f3 | 601 | rr.content = value.first.getDNSKEY().getZoneRepresentation(); |
bec14a20 | 602 | string keyname = NSEC3Zone ? hashQNameWithSalt(ns3pr.d_iterations, ns3pr.d_salt, rr.qname) : labelReverse(rr.qname); |
9d3151d9 | 603 | NSECXEntry& ne = nsecxrepo[keyname]; |
b317b510 BH |
604 | |
605 | ne.d_set.insert(rr.qtype.getCode()); | |
794c2f92 | 606 | ne.d_ttl = sd.default_ttl; |
8e9b7d99 | 607 | csp.submit(rr); |
4c1474f3 | 608 | } |
0c350cb5 | 609 | |
9097239c | 610 | if(::arg().mustDo("experimental-direct-dnskey")) { |
6dae726d PD |
611 | sd.db->lookup(QType(QType::DNSKEY), target, NULL, sd.domain_id); |
612 | while(sd.db->get(rr)) { | |
613 | rr.ttl = sd.default_ttl; | |
614 | csp.submit(rr); | |
615 | } | |
616 | } | |
617 | ||
95c5bc40 | 618 | if(NSEC3Zone) { // now stuff in the NSEC3PARAM |
ce464268 | 619 | rr.qtype = QType(QType::NSEC3PARAM); |
95c5bc40 | 620 | ns3pr.d_flags = 0; |
ce464268 | 621 | rr.content = ns3pr.getZoneRepresentation(); |
95c5bc40 | 622 | ns3pr.d_flags = 1; |
ce464268 BH |
623 | string keyname = hashQNameWithSalt(ns3pr.d_iterations, ns3pr.d_salt, rr.qname); |
624 | NSECXEntry& ne = nsecxrepo[keyname]; | |
625 | ||
626 | ne.d_set.insert(rr.qtype.getCode()); | |
627 | csp.submit(rr); | |
628 | } | |
8e9b7d99 | 629 | |
0c350cb5 BH |
630 | // now start list zone |
631 | if(!(sd.db->list(target, sd.domain_id))) { | |
632 | L<<Logger::Error<<"Backend signals error condition"<<endl; | |
633 | outpacket->setRcode(2); // 'SERVFAIL' | |
634 | sendPacket(outpacket,outsock); | |
635 | return 0; | |
636 | } | |
637 | ||
12c86877 | 638 | /* now write all other records */ |
8e9b7d99 | 639 | |
9d3151d9 | 640 | string keyname; |
1c6d9830 BH |
641 | DTime dt; |
642 | dt.set(); | |
bec14a20 | 643 | int records=0; |
8e9b7d99 | 644 | while(sd.db->get(rr)) { |
794c2f92 PD |
645 | if (rr.qtype.getCode() == QType::RRSIG) |
646 | continue; | |
6dae726d PD |
647 | |
648 | // only skip the DNSKEY if direct-dnskey is enabled, to avoid changing behaviour | |
649 | // when it is not enabled. | |
9097239c | 650 | if(::arg().mustDo("experimental-direct-dnskey") && rr.qtype.getCode() == QType::DNSKEY) |
6dae726d PD |
651 | continue; |
652 | ||
bec14a20 | 653 | records++; |
27045410 | 654 | if(securedZone && (rr.auth || (!NSEC3Zone && rr.qtype.getCode() == QType::NS) || rr.qtype.getCode() == QType::DS)) { // this is probably NSEC specific, NSEC3 is different |
b5baefaf PD |
655 | if (NSEC3Zone || rr.qtype.getCode()) { |
656 | keyname = NSEC3Zone ? hashQNameWithSalt(ns3pr.d_iterations, ns3pr.d_salt, rr.qname) : labelReverse(rr.qname); | |
657 | NSECXEntry& ne = nsecxrepo[keyname]; | |
658 | ne.d_ttl = sd.default_ttl; | |
659 | if (rr.qtype.getCode()) { | |
660 | ne.d_set.insert(rr.qtype.getCode()); | |
661 | } | |
662 | } | |
b317b510 | 663 | } |
b5baefaf PD |
664 | |
665 | if (!rr.qtype.getCode()) | |
666 | continue; // skip empty non-terminals | |
667 | ||
add640c0 | 668 | if(rr.qtype.getCode() == QType::SOA) |
12c86877 | 669 | continue; // skip SOA - would indicate end of AXFR |
add640c0 | 670 | |
1c6d9830 BH |
671 | if(csp.submit(rr)) { |
672 | for(;;) { | |
673 | outpacket->getRRS() = csp.getChunk(); | |
674 | if(!outpacket->getRRS().empty()) { | |
54d84273 PD |
675 | if(!tsigkeyname.empty()) |
676 | outpacket->setTSIGDetails(trc, tsigkeyname, tsigsecret, trc.d_mac, true); | |
1c6d9830 | 677 | sendPacket(outpacket, outsock); |
78bcb858 | 678 | trc.d_mac=outpacket->d_trc.d_mac; |
1c6d9830 BH |
679 | outpacket=getFreshAXFRPacket(q); |
680 | } | |
681 | else | |
682 | break; | |
683 | } | |
12c86877 BH |
684 | } |
685 | } | |
1c6d9830 | 686 | unsigned int udiff=dt.udiffNoReset(); |
78bcb858 | 687 | /* |
1c6d9830 BH |
688 | cerr<<"Starting NSEC: "<<csp.d_signed/(udiff/1000000.0)<<" sigs/s, "<<csp.d_signed<<" / "<<udiff/1000000.0<<endl; |
689 | cerr<<"Outstanding: "<<csp.d_outstanding<<", "<<csp.d_queued - csp.d_signed << endl; | |
690 | cerr<<"Ready for consumption: "<<csp.getReady()<<endl; | |
78bcb858 | 691 | */ |
420faf6b | 692 | if(securedZone) { |
4888e4b2 | 693 | if(NSEC3Zone) { |
9d3151d9 BH |
694 | for(nsecxrepo_t::const_iterator iter = nsecxrepo.begin(); iter != nsecxrepo.end(); ++iter) { |
695 | NSEC3RecordContent n3rc; | |
696 | n3rc.d_set = iter->second.d_set; | |
b5baefaf PD |
697 | if (n3rc.d_set.size()) |
698 | n3rc.d_set.insert(QType::RRSIG); | |
9d3151d9 | 699 | n3rc.d_salt=ns3pr.d_salt; |
a1282cdd | 700 | n3rc.d_flags = ns3pr.d_flags; |
9d3151d9 BH |
701 | n3rc.d_iterations = ns3pr.d_iterations; |
702 | n3rc.d_algorithm = 1; // SHA1, fixed in PowerDNS for now | |
703 | if(boost::next(iter) != nsecxrepo.end()) { | |
704 | n3rc.d_nexthash = boost::next(iter)->first; | |
705 | } | |
706 | else | |
707 | n3rc.d_nexthash=nsecxrepo.begin()->first; | |
708 | ||
709 | rr.qname = dotConcat(toLower(toBase32Hex(iter->first)), sd.qname); | |
710 | ||
b5baefaf | 711 | rr.ttl = sd.default_ttl; |
9d3151d9 BH |
712 | rr.content = n3rc.getZoneRepresentation(); |
713 | rr.qtype = QType::NSEC3; | |
714 | rr.d_place = DNSResourceRecord::ANSWER; | |
715 | rr.auth=true; | |
8e9b7d99 | 716 | if(csp.submit(rr)) { |
1c6d9830 BH |
717 | for(;;) { |
718 | outpacket->getRRS() = csp.getChunk(); | |
719 | if(!outpacket->getRRS().empty()) { | |
78bcb858 BH |
720 | if(!tsigkeyname.empty()) |
721 | outpacket->setTSIGDetails(trc, tsigkeyname, tsigsecret, trc.d_mac, true); | |
1c6d9830 | 722 | sendPacket(outpacket, outsock); |
78bcb858 | 723 | trc.d_mac=outpacket->d_trc.d_mac; |
1c6d9830 BH |
724 | outpacket=getFreshAXFRPacket(q); |
725 | } | |
726 | else | |
727 | break; | |
728 | } | |
8e9b7d99 | 729 | } |
4888e4b2 BH |
730 | } |
731 | } | |
9d3151d9 | 732 | else for(nsecxrepo_t::const_iterator iter = nsecxrepo.begin(); iter != nsecxrepo.end(); ++iter) { |
ed9c3a50 | 733 | NSECRecordContent nrc; |
b317b510 | 734 | nrc.d_set = iter->second.d_set; |
ed9c3a50 BH |
735 | nrc.d_set.insert(QType::RRSIG); |
736 | nrc.d_set.insert(QType::NSEC); | |
9d3151d9 | 737 | if(boost::next(iter) != nsecxrepo.end()) { |
bec14a20 | 738 | nrc.d_next = labelReverse(boost::next(iter)->first); |
ed9c3a50 BH |
739 | } |
740 | else | |
bec14a20 | 741 | nrc.d_next=labelReverse(nsecxrepo.begin()->first); |
ed9c3a50 | 742 | |
bec14a20 | 743 | rr.qname = labelReverse(iter->first); |
ed9c3a50 | 744 | |
b5baefaf | 745 | rr.ttl = sd.default_ttl; |
ed9c3a50 BH |
746 | rr.content = nrc.getZoneRepresentation(); |
747 | rr.qtype = QType::NSEC; | |
748 | rr.d_place = DNSResourceRecord::ANSWER; | |
5f5221b4 | 749 | rr.auth=true; |
8e9b7d99 | 750 | if(csp.submit(rr)) { |
1c6d9830 BH |
751 | for(;;) { |
752 | outpacket->getRRS() = csp.getChunk(); | |
753 | if(!outpacket->getRRS().empty()) { | |
78bcb858 BH |
754 | if(!tsigkeyname.empty()) |
755 | outpacket->setTSIGDetails(trc, tsigkeyname, tsigsecret, trc.d_mac, true); | |
1c6d9830 | 756 | sendPacket(outpacket, outsock); |
78bcb858 | 757 | trc.d_mac=outpacket->d_trc.d_mac; |
1c6d9830 BH |
758 | outpacket=getFreshAXFRPacket(q); |
759 | } | |
760 | else | |
761 | break; | |
762 | } | |
8e9b7d99 | 763 | } |
add640c0 | 764 | } |
add640c0 | 765 | } |
1c6d9830 | 766 | udiff=dt.udiffNoReset(); |
78bcb858 | 767 | /* |
1c6d9830 BH |
768 | cerr<<"Flushing pipe: "<<csp.d_signed/(udiff/1000000.0)<<" sigs/s, "<<csp.d_signed<<" / "<<udiff/1000000.0<<endl; |
769 | cerr<<"Outstanding: "<<csp.d_outstanding<<", "<<csp.d_queued - csp.d_signed << endl; | |
770 | cerr<<"Ready for consumption: "<<csp.getReady()<<endl; | |
78bcb858 | 771 | * */ |
bec14a20 BH |
772 | for(;;) { |
773 | outpacket->getRRS() = csp.getChunk(true); // flush the pipe | |
774 | if(!outpacket->getRRS().empty()) { | |
78bcb858 BH |
775 | if(!tsigkeyname.empty()) |
776 | outpacket->setTSIGDetails(trc, tsigkeyname, tsigsecret, trc.d_mac, true); // first answer is 'normal' | |
bec14a20 | 777 | sendPacket(outpacket, outsock); |
78bcb858 | 778 | trc.d_mac=outpacket->d_trc.d_mac; |
bec14a20 BH |
779 | outpacket=getFreshAXFRPacket(q); |
780 | } | |
781 | else | |
782 | break; | |
12c86877 | 783 | } |
8e9b7d99 | 784 | |
1c6d9830 | 785 | udiff=dt.udiffNoReset(); |
f1f85f12 BH |
786 | if(securedZone) |
787 | L<<Logger::Info<<"Done signing: "<<csp.d_signed/(udiff/1000000.0)<<" sigs/s, "<<endl; | |
1c6d9830 | 788 | |
12c86877 BH |
789 | DLOG(L<<"Done writing out records"<<endl); |
790 | /* and terminate with yet again the SOA record */ | |
8e9b7d99 | 791 | outpacket=getFreshAXFRPacket(q); |
12c86877 | 792 | outpacket->addRecord(soa); |
92c90b44 | 793 | editSOA(dk, sd.qname, outpacket.get()); |
78bcb858 BH |
794 | if(!tsigkeyname.empty()) |
795 | outpacket->setTSIGDetails(trc, tsigkeyname, tsigsecret, trc.d_mac, true); | |
92c90b44 | 796 | |
ff76e8b4 | 797 | sendPacket(outpacket, outsock); |
78bcb858 | 798 | |
12c86877 | 799 | DLOG(L<<"last packet - close"<<endl); |
20ca8e7d | 800 | L<<Logger::Error<<"AXFR of domain '"<<target<<"' to "<<q->getRemote()<<" finished"<<endl; |
12c86877 BH |
801 | |
802 | return 1; | |
803 | } | |
804 | ||
805 | TCPNameserver::~TCPNameserver() | |
806 | { | |
807 | delete d_connectionroom_sem; | |
808 | } | |
809 | ||
810 | TCPNameserver::TCPNameserver() | |
811 | { | |
379ab445 BH |
812 | // sem_init(&d_connectionroom_sem,0,::arg().asNum("max-tcp-connections")); |
813 | d_connectionroom_sem = new Semaphore( ::arg().asNum( "max-tcp-connections" )); | |
12c86877 BH |
814 | |
815 | s_timeout=10; | |
816 | vector<string>locals; | |
379ab445 | 817 | stringtok(locals,::arg()["local-address"]," ,"); |
12c86877 BH |
818 | |
819 | vector<string>locals6; | |
379ab445 | 820 | stringtok(locals6,::arg()["local-ipv6"]," ,"); |
12c86877 | 821 | |
12c86877 BH |
822 | if(locals.empty() && locals6.empty()) |
823 | throw AhuException("No local address specified"); | |
824 | ||
9f1d5826 | 825 | vector<string> parts; |
379ab445 | 826 | stringtok( parts, ::arg()["allow-axfr-ips"], ", \t" ); // is this IP on the guestlist? |
9f1d5826 BH |
827 | for( vector<string>::const_iterator i = parts.begin(); i != parts.end(); ++i ) { |
828 | d_ng.addMask( *i ); | |
829 | } | |
830 | ||
12c86877 BH |
831 | #ifndef WIN32 |
832 | signal(SIGPIPE,SIG_IGN); | |
833 | #endif // WIN32 | |
12c86877 BH |
834 | |
835 | for(vector<string>::const_iterator laddr=locals.begin();laddr!=locals.end();++laddr) { | |
12c86877 | 836 | int s=socket(AF_INET,SOCK_STREAM,0); |
42c235e5 | 837 | Utility::setCloseOnExec(s); |
326484be | 838 | |
12c86877 BH |
839 | if(s<0) |
840 | throw AhuException("Unable to acquire TCP socket: "+stringerror()); | |
12c86877 | 841 | |
379ab445 | 842 | ComboAddress local(*laddr, ::arg().asNum("local-port")); |
12c86877 BH |
843 | |
844 | int tmp=1; | |
845 | if(setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char*)&tmp,sizeof tmp)<0) { | |
846 | L<<Logger::Error<<"Setsockopt failed"<<endl; | |
847 | exit(1); | |
848 | } | |
326484be | 849 | |
379ab445 | 850 | if(::bind(s, (sockaddr*)&local, local.getSocklen())<0) { |
12c86877 BH |
851 | L<<Logger::Error<<"binding to TCP socket: "<<strerror(errno)<<endl; |
852 | throw AhuException("Unable to bind to TCP socket"); | |
853 | } | |
854 | ||
855 | listen(s,128); | |
ff76e8b4 | 856 | L<<Logger::Error<<"TCP server bound to "<<local.toStringWithPort()<<endl; |
12c86877 | 857 | d_sockets.push_back(s); |
8edfedf1 BH |
858 | struct pollfd pfd; |
859 | memset(&pfd, 0, sizeof(pfd)); | |
860 | pfd.fd = s; | |
861 | pfd.events = POLLIN; | |
862 | ||
863 | d_prfds.push_back(pfd); | |
12c86877 BH |
864 | } |
865 | ||
1258abe0 | 866 | #if !WIN32 && HAVE_IPV6 |
12c86877 | 867 | for(vector<string>::const_iterator laddr=locals6.begin();laddr!=locals6.end();++laddr) { |
12c86877 | 868 | int s=socket(AF_INET6,SOCK_STREAM,0); |
42c235e5 | 869 | Utility::setCloseOnExec(s); |
12c86877 BH |
870 | |
871 | if(s<0) | |
872 | throw AhuException("Unable to acquire TCPv6 socket: "+stringerror()); | |
178d5134 | 873 | |
379ab445 | 874 | ComboAddress local(*laddr, ::arg().asNum("local-port")); |
12c86877 BH |
875 | |
876 | int tmp=1; | |
877 | if(setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char*)&tmp,sizeof tmp)<0) { | |
878 | L<<Logger::Error<<"Setsockopt failed"<<endl; | |
879 | exit(1); | |
880 | } | |
326484be BH |
881 | if(setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &tmp, sizeof(tmp)) < 0) { |
882 | L<<Logger::Error<<"Failed to set IPv6 socket to IPv6 only, continuing anyhow: "<<strerror(errno)<<endl; | |
883 | } | |
ff76e8b4 | 884 | if(bind(s, (const sockaddr*)&local, local.getSocklen())<0) { |
12c86877 BH |
885 | L<<Logger::Error<<"binding to TCP socket: "<<strerror(errno)<<endl; |
886 | throw AhuException("Unable to bind to TCPv6 socket"); | |
887 | } | |
888 | ||
889 | listen(s,128); | |
506a9050 | 890 | L<<Logger::Error<<"TCPv6 server bound to "<<local.toStringWithPort()<<endl; // this gets %eth0 right |
12c86877 | 891 | d_sockets.push_back(s); |
8edfedf1 BH |
892 | |
893 | struct pollfd pfd; | |
894 | memset(&pfd, 0, sizeof(pfd)); | |
895 | pfd.fd = s; | |
896 | pfd.events = POLLIN; | |
897 | ||
898 | d_prfds.push_back(pfd); | |
12c86877 BH |
899 | } |
900 | #endif // WIN32 | |
901 | } | |
902 | ||
903 | ||
ff76e8b4 | 904 | //! Start of TCP operations thread, we launch a new thread for each incoming TCP question |
12c86877 BH |
905 | void TCPNameserver::thread() |
906 | { | |
12c86877 BH |
907 | try { |
908 | for(;;) { | |
909 | int fd; | |
910 | struct sockaddr_in remote; | |
911 | Utility::socklen_t addrlen=sizeof(remote); | |
912 | ||
8edfedf1 | 913 | int ret=poll(&d_prfds[0], d_prfds.size(), -1); // blocks, forever if need be |
8a63d3ce | 914 | if(ret <= 0) |
4957a608 | 915 | continue; |
8a63d3ce | 916 | |
12c86877 | 917 | int sock=-1; |
8edfedf1 | 918 | BOOST_FOREACH(const struct pollfd& pfd, d_prfds) { |
4957a608 BH |
919 | if(pfd.revents == POLLIN) { |
920 | sock = pfd.fd; | |
921 | addrlen=sizeof(remote); | |
922 | ||
923 | if((fd=accept(sock, (sockaddr*)&remote, &addrlen))<0) { | |
924 | L<<Logger::Error<<"TCP question accept error: "<<strerror(errno)<<endl; | |
925 | ||
926 | if(errno==EMFILE) { | |
927 | L<<Logger::Error<<Logger::NTLog<<"TCP handler out of filedescriptors, exiting, won't recover from this"<<endl; | |
928 | exit(1); | |
929 | } | |
930 | } | |
931 | else { | |
932 | pthread_t tid; | |
933 | d_connectionroom_sem->wait(); // blocks if no connections are available | |
934 | ||
935 | int room; | |
936 | d_connectionroom_sem->getValue( &room); | |
937 | if(room<1) | |
938 | L<<Logger::Warning<<Logger::NTLog<<"Limit of simultaneous TCP connections reached - raise max-tcp-connections"<<endl; | |
939 | ||
940 | if(pthread_create(&tid, 0, &doConnection, (void *)fd)) { | |
941 | L<<Logger::Error<<"Error creating thread: "<<stringerror()<<endl; | |
942 | d_connectionroom_sem->post(); | |
943 | } | |
944 | } | |
945 | } | |
12c86877 BH |
946 | } |
947 | } | |
948 | } | |
949 | catch(AhuException &AE) { | |
abc1d928 | 950 | L<<Logger::Error<<"TCP Nameserver thread dying because of fatal error: "<<AE.reason<<endl; |
12c86877 BH |
951 | } |
952 | catch(...) { | |
953 | L<<Logger::Error<<"TCPNameserver dying because of an unexpected fatal error"<<endl; | |
954 | } | |
955 | exit(1); // take rest of server with us | |
956 | } | |
957 | ||
958 |