]> granicus.if.org Git - pdns/commitdiff
lwres
authorBert Hubert <bert.hubert@netherlabs.nl>
Fri, 10 Jan 2003 18:47:55 +0000 (18:47 +0000)
committerBert Hubert <bert.hubert@netherlabs.nl>
Fri, 10 Jan 2003 18:47:55 +0000 (18:47 +0000)
git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@125 d19b8d6e-7fed-0310-83ef-9ca221ded41b

pdns/lwres.cc [new file with mode: 0644]

diff --git a/pdns/lwres.cc b/pdns/lwres.cc
new file mode 100644 (file)
index 0000000..eccfcbd
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+    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();
+}
+