--- /dev/null
+/*
+ PowerDNS Versatile Database Driven Nameserver
+ Copyright (C) 2002 PowerDNS.COM BV
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+#include "utility.hh"\r
+#include "lwres.hh"
+#include <pthread.h>
+#include <semaphore.h>
+#include <iostream>
+#include <errno.h>
+#include "misc.hh"
+#include <algorithm>
+#include <sstream>
+#include <cstring>
+#include <string>
+#include <vector>
+#include "dnspacket.hh"
+#include "dns.hh"
+#include "qtype.hh"
+#include "tcpreceiver.hh"
+#include "ahuexception.hh"
+#include "statbag.hh"
+#include "arguments.hh"
+
+
+LWRes::LWRes()
+{
+ d_sock=-1;
+ d_timeout=500000;
+ d_buf=new unsigned char[66000];
+}
+
+LWRes::~LWRes()
+{
+ if(d_sock>=0)
+ Utility::closesocket(d_sock);
+ delete[] d_buf;
+}
+
+
+//! returns -1 for permanent error, 0 for timeout, 1 for success
+/** Never throws! */
+int LWRes::asyncresolve(const string &ip, const char *domain, int type)
+{
+ DNSPacket p;
+ p.setQuestion(Opcode::Query,domain,type);
+ p.setRD(false);
+ p.wrapup();
+
+ d_domain=domain;
+ d_type=type;
+ d_inaxfr=false;
+
+ struct sockaddr_in toaddr;
+ struct in_addr inp;
+ Utility::inet_aton(ip.c_str(),&inp);
+ toaddr.sin_addr.s_addr=inp.s_addr;
+
+ toaddr.sin_port=htons(53);
+ toaddr.sin_family=AF_INET;
+
+ if(asendto(p.getData(), p.len, 0, (struct sockaddr*)(&toaddr), sizeof(toaddr),p.d.id)<0) {
+ return -1;
+ }
+
+ Utility::socklen_t addrlen=sizeof(toaddr);
+
+ // sleep until we see an answer to this
+ return arecvfrom(reinterpret_cast<char *>(d_buf), 512,0,(struct sockaddr*)(&toaddr), &addrlen, &d_len, p.d.id);
+}
+
+
+LWRes::res_t LWRes::result(bool &aabit)
+{
+ DNSPacket p;
+
+ if(p.parse((char *)d_buf, d_len)<0)
+ throw LWResException("resolver: unable to parse packet of "+itoa(d_len)+" bytes");
+
+ aabit=p.d.aa;
+ d_rcode=p.d.rcode;
+ return p.getAnswers();
+}
+