]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
wait what I don't even this code, my eyes! Built in webserver can now listen on IPv6...
authorbert hubert <bert.hubert@netherlabs.nl>
Tue, 11 Jun 2013 15:17:52 +0000 (17:17 +0200)
committerbert hubert <bert.hubert@netherlabs.nl>
Tue, 11 Jun 2013 15:17:52 +0000 (17:17 +0200)
Also silences some useless messages about timeouts.

pdns/session.cc
pdns/session.hh
pdns/webserver.cc

index 13059abe6b3139a3b8b5174b521c2130ff012dc0..2fcbd80be7cfd31ac1fa0ab93d223255dbfce0df 100644 (file)
@@ -26,6 +26,7 @@
 #include <fcntl.h>
 #include <sstream>
 #include "misc.hh"
+#include "iputils.hh"
 
 void Session::init()
 {
@@ -284,54 +285,24 @@ Session *Server::accept()
   return new Session(clisock, remote);
 }
 
-
-
-Server::Server(int p, const string &p_localaddress)
+Server::Server(int port, const string &localaddress)
 {
-  d_localaddress="0.0.0.0";
-  string localaddress=p_localaddress;
-  port=p;
-
-  struct sockaddr_in local;
-  s=socket(AF_INET,SOCK_STREAM,0);
-  Utility::setCloseOnExec(s);
+  d_local = ComboAddress(localaddress.empty() ? "0.0.0.0" : localaddress, port);
+  s = socket(d_local.sin4.sin_family ,SOCK_STREAM,0);
 
-  if(s<0)
+  if(s < 0)
     throw Exception(string("socket: ")+strerror(errno));
-  
-  memset(&local,0,sizeof(local));
-  
-  local.sin_family=AF_INET;
-  
-  struct hostent *h;
-  if(localaddress=="")
-    localaddress=d_localaddress;
-
-  if ( localaddress != "0.0.0.0" )
-  {
-    h=gethostbyname(localaddress.c_str());
-
-    if(!h)
-      throw Exception(); 
-  
-    local.sin_addr.s_addr=*(int*)h->h_addr;
-  }
-  else
-  {
-    local.sin_addr.s_addr = INADDR_ANY;
-  }
 
-  local.sin_port=htons(port);
+  Utility::setCloseOnExec(s);
   
   int tmp=1;
-  if(setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char*)&tmp,sizeof tmp)<0)
+  if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR,(char*)&tmp,sizeof tmp)<0)
     throw SessionException(string("Setsockopt failed: ")+strerror(errno));
 
-  if(bind(s, (sockaddr*)&local,sizeof(local))<0)
-      throw SessionException("binding to port "+itoa(port)+string(" on ")+localaddress+": "+strerror(errno));
+  if(bind(s, (sockaddr*)&d_local, d_local.getSocklen())<0)
+    throw SessionException("binding to "+d_local.toStringWithPort()+": "+strerror(errno));
   
   if(listen(s,128)<0)
-      throw SessionException("listen: "+stringerror());
-
+    throw SessionException("listen: "+stringerror());
 }
 
index b9a5a70d75a16113a2c9207f778d5940db1f82b8..91ff6aab4c695f3b7f5de86adccb97e579051878 100644 (file)
@@ -1,6 +1,6 @@
 /*
     PowerDNS Versatile Database Driven Nameserver
-    Copyright (C) 2002  PowerDNS.COM BV
+    Copyright (C) 2002 - 2013  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 version 2
@@ -34,7 +34,7 @@
 # include <strings.h>
 
 #endif // WIN32
-
+#include "iputils.hh"
 #include "ahuexception.hh"
 
 class SessionException: public AhuException
@@ -98,12 +98,12 @@ class Server
 public:
   Server(int p, const string &localaddress=""); //!< port on which to listen
   Session* accept(); //!< Call accept() in an endless loop to accept new connections
+  ComboAddress d_local;
 private:
   int s;
   int port;
   int backlog;
 
-  string d_localaddress;
 };
 
 class Exception
index 9eba79c146c6fcfe8a37fe3e0a1f4815d8a37a2b..97b6fa1d348a3790d4dd40a251eb7372e35ed95d 100644 (file)
@@ -177,7 +177,7 @@ void *WebServer::serveConnection(void *p)
 
   }
   catch(SessionTimeoutException &e) {
-    L<<Logger::Error<<"Timeout in webserver"<<endl;
+    // L<<Logger::Error<<"Timeout in webserver"<<endl;
   }
   catch(SessionException &e) {
     L<<Logger::Error<<"Fatal error in webserver: "<<e.reason<<endl;
@@ -221,14 +221,14 @@ void WebServer::go()
     Session *client;
     pthread_t tid;
     
-    L<<Logger::Error<<"Launched webserver on "<<d_listenaddress<<":"<<d_port<<endl;
+    L<<Logger::Error<<"Launched webserver on " << d_server->d_local.toStringWithPort() <<endl;
 
     while((client=d_server->accept())) {
       pthread_create(&tid, 0 , &serveConnection, (void *)client);
     }
   }
   catch(SessionTimeoutException &e) {
-    L<<Logger::Error<<"Timeout in webserver"<<endl;
+    //    L<<Logger::Error<<"Timeout in webserver"<<endl;
   }
   catch(SessionException &e) {
     L<<Logger::Error<<"Fatal error in webserver: "<<e.reason<<endl;