1 #include "../../include/nntp/nntplistener.h"
\r
2 #include "../../include/nntp/nntpconnection.h"
\r
3 #include "../../include/option.h"
\r
4 #include "../../include/logfile.h"
\r
5 #include "../../include/global.h"
\r
6 #include "../../include/stringfunctions.h"
\r
9 #include <winsock2.h>
\r
10 #include <ws2tcpip.h>
\r
12 #include <netinet/in.h> // gcc - IPPROTO_ consts
\r
13 #include <netdb.h> // gcc - addrinfo
\r
20 NNTPListener::NNTPListener()
\r
25 NNTPListener::~NNTPListener()
\r
30 void NNTPListener::Run()
\r
35 std::vector<SOCKET>::iterator listeni;
\r
38 LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPListener::run thread started.");
\r
52 // put all listen sockets on the fd set
\r
53 for(listeni=m_listensockets.begin(); listeni!=m_listensockets.end(); listeni++)
\r
55 FD_SET((*listeni),&readfs);
\r
56 if((*listeni)>highsocket)
\r
58 highsocket=(*listeni);
\r
62 // see if any connections are waiting
\r
63 rval=select(highsocket+1,&readfs,0,0,&tv);
\r
65 // check for new connections
\r
68 for(listeni=m_listensockets.begin(); listeni!=m_listensockets.end(); listeni++)
\r
70 if(FD_ISSET((*listeni),&readfs))
\r
73 struct sockaddr_storage addr;
\r
74 socklen_t addrlen=sizeof(addr);
\r
75 newsock=accept((*listeni),(struct sockaddr *)&addr,&addrlen);
\r
76 LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_INFO,"NNTPListener::run NNTP client connected");
\r
77 //m_connections.execute(new NNTPConnection(newsock));
\r
78 m_connectionthreads.push_back(new PThread::Thread(new NNTPConnection(newsock)));
\r
83 // check for any non-running connection threads that we can delete
\r
84 for(std::vector<PThread::Thread *>::iterator i=m_connectionthreads.begin(); i!=m_connectionthreads.end(); )
\r
86 if((*i)->IsRunning()==false)
\r
89 i=m_connectionthreads.erase(i);
\r
91 if(i!=m_connectionthreads.end())
\r
97 //}while(!ZThread::Thread::interrupted() && m_listensockets.size()>0);
\r
98 }while(!IsCancelled() && m_listensockets.size()>0);
\r
100 // see if any threads are still running - just calling interrupt without check would cause assert in debug mode
\r
102 if(m_connections.wait(1)==false)
\r
104 LogFile::instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPListener::run interrupting connection threads and waiting 60 seconds for exit.");
\r
107 m_connections.interrupt();
\r
111 LogFile::instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPListener::run caught unhandled exception.");
\r
113 if(m_connections.wait(60000)==false)
\r
115 LogFile::instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPListener::run connection threads did not exit after 60 seconds.");
\r
119 for(std::vector<PThread::Thread *>::iterator i=m_connectionthreads.begin(); i!=m_connectionthreads.end(); i++)
\r
121 if((*i)->IsRunning())
\r
123 LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPListener::Run waiting for connection thread to exit.");
\r
130 for(listeni=m_listensockets.begin(); listeni!=m_listensockets.end(); listeni++)
\r
133 closesocket((*listeni));
\r
138 m_listensockets.clear();
\r
140 LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPListener::run thread exiting.");
\r
144 void NNTPListener::StartListen()
\r
147 std::string bindaddresses;
\r
148 std::vector<std::string> listenaddresses;
\r
149 std::string nntpport;
\r
150 if(Option::Instance()->Get("NNTPListenPort",nntpport)==false)
\r
153 Option::Instance()->Set("NNTPListenPort",nntpport);
\r
155 if(Option::Instance()->Get("NNTPBindAddresses",bindaddresses)==false)
\r
157 bindaddresses="127.0.0.1";
\r
158 Option::Instance()->Set("NNTPBindAddresses",bindaddresses);
\r
160 StringFunctions::Split(bindaddresses,",",listenaddresses);
\r
162 for(std::vector<std::string>::iterator i=listenaddresses.begin(); i!=listenaddresses.end(); i++)
\r
166 struct addrinfo hint,*result,*current;
\r
167 result=current=NULL;
\r
168 memset(&hint,0,sizeof(hint));
\r
169 hint.ai_socktype=SOCK_STREAM;
\r
170 hint.ai_protocol=IPPROTO_TCP;
\r
171 hint.ai_flags=AI_PASSIVE;
\r
173 rval=getaddrinfo((*i).c_str(),nntpport.c_str(),&hint,&result);
\r
176 for(current=result; current!=NULL; current=current->ai_next)
\r
178 sock=socket(current->ai_family,current->ai_socktype,current->ai_protocol);
\r
179 if(sock!=INVALID_SOCKET)
\r
181 if(bind(sock,current->ai_addr,current->ai_addrlen)==0)
\r
183 if(listen(sock,10)==0)
\r
185 LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_INFO,"NNTPListener::StartListen started listening at "+(*i)+":"+nntpport);
\r
186 m_listensockets.push_back(sock);
\r
190 LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen socket listen failed");
\r
200 LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen socket bind failed");
\r
210 LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen couldn't create socket");
\r
216 freeaddrinfo(result);
\r
219 if(m_listensockets.size()==0)
\r
221 LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_FATAL,"NNTPListener::StartListen couldn't start listening on any sockets");
\r