X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnntp%2Fnntplistener.cpp;h=a34fc15bb15f6eba8448092455b132ddbe911655;hb=59a5414ec47a2932a7802fcd1d98c4d80166564f;hp=4d1c37dba0fe8bdca85f22d0311068f5e15758a4;hpb=6b896a9e1dc143bba86795be1e9336549db9b85f;p=fms.git diff --git a/src/nntp/nntplistener.cpp b/src/nntp/nntplistener.cpp index 4d1c37d..a34fc15 100644 --- a/src/nntp/nntplistener.cpp +++ b/src/nntp/nntplistener.cpp @@ -1,15 +1,19 @@ #include "../../include/nntp/nntplistener.h" #include "../../include/nntp/nntpconnection.h" #include "../../include/option.h" -#include "../../include/logfile.h" #include "../../include/global.h" #include "../../include/stringfunctions.h" +#include + +#include + #ifdef _WIN32 #include #include #else - + #include // gcc - IPPROTO_ consts + #include // gcc - addrinfo #endif #ifdef XMEM @@ -29,11 +33,15 @@ NNTPListener::~NNTPListener() void NNTPListener::run() { int rval; - struct fd_set readfs; + fd_set readfs; struct timeval tv; std::vector::iterator listeni; SOCKET highsocket; + m_log->debug("NNTPListener::run thread started."); + + LoadDatabase(); + StartListen(); do @@ -68,47 +76,52 @@ void NNTPListener::run() { SOCKET newsock; struct sockaddr_storage addr; - int addrlen=sizeof(addr); + socklen_t addrlen=sizeof(addr); newsock=accept((*listeni),(struct sockaddr *)&addr,&addrlen); - LogFile::instance()->WriteLog(LogFile::LOGLEVEL_INFO,"NNTPListener::run NNTP client connected"); - m_connections.execute(new NNTPConnection(newsock)); + m_log->information("NNTPListener::run NNTP client connected"); + m_connections.Start(new NNTPConnection(newsock)); } } } - }while(!ZThread::Thread::interrupted() && m_listensockets.size()>0); + }while(!IsCancelled() && m_listensockets.size()>0); - // see if any threads are still running - just calling interrupt without check would cause assert in debug mode - if(m_connections.wait(1)==false) + m_connections.Cancel(); + m_connections.Join(); + + for(listeni=m_listensockets.begin(); listeni!=m_listensockets.end(); listeni++) { - try - { - m_connections.interrupt(); - } - catch(...) - { - } - m_connections.wait(); + #ifdef _WIN32 + closesocket((*listeni)); + #else + close((*listeni)); + #endif } + m_listensockets.clear(); + + m_log->debug("NNTPListener::run thread exiting."); } void NNTPListener::StartListen() { - std::string bindaddresses; std::vector listenaddresses; + std::string bindaddresses; std::string nntpport; - if(Option::instance()->Get("NNTPListenPort",nntpport)==false) + Option option(m_db); + + if(option.Get("NNTPListenPort",nntpport)==false) { nntpport="1119"; - Option::instance()->Set("NNTPListenPort",nntpport); + option.Set("NNTPListenPort",nntpport); } - if(Option::instance()->Get("NNTPBindAddresses",bindaddresses)==false) + if(option.Get("NNTPBindAddresses",bindaddresses)==false) { bindaddresses="127.0.0.1"; - Option::instance()->Set("NNTPBindAddresses",bindaddresses); + option.Set("NNTPBindAddresses",bindaddresses); } + StringFunctions::Split(bindaddresses,",",listenaddresses); for(std::vector::iterator i=listenaddresses.begin(); i!=listenaddresses.end(); i++) @@ -121,25 +134,47 @@ void NNTPListener::StartListen() hint.ai_socktype=SOCK_STREAM; hint.ai_protocol=IPPROTO_TCP; hint.ai_flags=AI_PASSIVE; + + m_log->trace("NNTPListener::StartListen getting address info for "+(*i)); rval=getaddrinfo((*i).c_str(),nntpport.c_str(),&hint,&result); if(rval==0) { for(current=result; current!=NULL; current=current->ai_next) { - sock=socket(current->ai_family,current->ai_socktype,current->ai_protocol); - if(sock!=INVALID_SOCKET) + try { - if(bind(sock,current->ai_addr,current->ai_addrlen)==0) + Poco::Net::SocketAddress sa(current->ai_addr,current->ai_addrlen); + + m_log->debug("NNTPListener::StartListen trying to create socket, bind, and listen on "+sa.toString()); + + sock=socket(current->ai_family,current->ai_socktype,current->ai_protocol); + if(sock!=INVALID_SOCKET) { - if(listen(sock,10)==0) + #ifndef _WIN32 + const char optval='1'; + setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&optval,sizeof(optval)); + #endif + if(bind(sock,current->ai_addr,current->ai_addrlen)==0) { - LogFile::instance()->WriteLog(LogFile::LOGLEVEL_INFO,"NNTPListener::StartListen started listening at "+(*i)+":"+nntpport); - m_listensockets.push_back(sock); + if(listen(sock,10)==0) + { + m_log->information("NNTPListener::StartListen started listening on "+sa.toString()); + m_listensockets.push_back(sock); + } + else + { + m_log->error("NNTPListener::StartListen socket listen failed on "+sa.toString()); + #ifdef _WIN32 + closesocket(sock); + #else + close(sock); + #endif + } } else { - LogFile::instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen socket listen failed"); + m_log->error("NNTPListener::StartListen socket bind failed on "+sa.toString()); #ifdef _WIN32 closesocket(sock); #else @@ -149,17 +184,18 @@ void NNTPListener::StartListen() } else { - LogFile::instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen socket bind failed"); - #ifdef _WIN32 - closesocket(sock); - #else - close(sock); - #endif + m_log->error("NNTPListener::StartListen couldn't create socket on "+sa.toString()); } } - else + catch(Poco::Exception &e) + { + m_log->error("NNTPListener::StartListen caught "+e.displayText()); + continue; + } + catch(...) { - LogFile::instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen couldn't create socket"); + m_log->error("NNTPListener::StartListen caught unknown exception"); + continue; } } } @@ -170,6 +206,6 @@ void NNTPListener::StartListen() } if(m_listensockets.size()==0) { - LogFile::instance()->WriteLog(LogFile::LOGLEVEL_FATAL,"NNTPListener::StartListen couldn't start listening on any sockets"); + m_log->fatal("NNTPListener::StartListen couldn't start listening on any interfaces"); } }