Permalink
Browse files

wait what I don't even this code, my eyes! Built in webserver can now…

… listen on IPv6, fixes #843

Also silences some useless messages about timeouts.
  • Loading branch information...
1 parent 2cc4fdf commit 3640473d0a6ab127d0d1563fc52f479ac8e53e00 @ahupowerdns ahupowerdns committed Jun 11, 2013
Showing with 16 additions and 45 deletions.
  1. +10 −39 pdns/session.cc
  2. +3 −3 pdns/session.hh
  3. +3 −3 pdns/webserver.cc
View
@@ -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());
}
View
@@ -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
View
@@ -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;

0 comments on commit 3640473

Please sign in to comment.