Skip to content

Commit

Permalink
wait what I don't even this code, my eyes! Built in webserver can now…
Browse files Browse the repository at this point in the history
… listen on IPv6, fixes #843

Also silences some useless messages about timeouts.
  • Loading branch information
ahupowerdns committed Jun 11, 2013
1 parent 682549f commit 96a2625
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 45 deletions.
49 changes: 10 additions & 39 deletions pdns/session.cc
Expand Up @@ -26,6 +26,7 @@
#include <fcntl.h>
#include <sstream>
#include "misc.hh"
#include "iputils.hh"

void Session::init()
{
Expand Down Expand Up @@ -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());
}

6 changes: 3 additions & 3 deletions pdns/session.hh
@@ -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
Expand Down Expand Up @@ -34,7 +34,7 @@
# include <strings.h>

#endif // WIN32

#include "iputils.hh"
#include "ahuexception.hh"

class SessionException: public AhuException
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pdns/webserver.cc
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 96a2625

Please sign in to comment.