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
19 NNTPListener::NNTPListener()
\r
24 NNTPListener::~NNTPListener()
\r
29 void NNTPListener::run()
\r
32 struct fd_set readfs;
\r
34 std::vector<SOCKET>::iterator listeni;
\r
49 // put all listen sockets on the fd set
\r
50 for(listeni=m_listensockets.begin(); listeni!=m_listensockets.end(); listeni++)
\r
52 FD_SET((*listeni),&readfs);
\r
53 if((*listeni)>highsocket)
\r
55 highsocket=(*listeni);
\r
59 // see if any connections are waiting
\r
60 rval=select(highsocket+1,&readfs,0,0,&tv);
\r
62 // check for new connections
\r
65 for(listeni=m_listensockets.begin(); listeni!=m_listensockets.end(); listeni++)
\r
67 if(FD_ISSET((*listeni),&readfs))
\r
70 struct sockaddr_storage addr;
\r
71 int addrlen=sizeof(addr);
\r
72 newsock=accept((*listeni),(struct sockaddr *)&addr,&addrlen);
\r
73 LogFile::instance()->WriteLog(LogFile::LOGLEVEL_INFO,"NNTPListener::run NNTP client connected");
\r
74 m_connections.execute(new NNTPConnection(newsock));
\r
79 }while(!ZThread::Thread::interrupted() && m_listensockets.size()>0);
\r
81 // see if any threads are still running - just calling interrupt without check would cause assert in debug mode
\r
82 if(m_connections.wait(1)==false)
\r
86 m_connections.interrupt();
\r
91 m_connections.wait();
\r
96 void NNTPListener::StartListen()
\r
99 std::string bindaddresses;
\r
100 std::vector<std::string> listenaddresses;
\r
101 std::string nntpport;
\r
102 if(Option::instance()->Get("NNTPListenPort",nntpport)==false)
\r
105 Option::instance()->Set("NNTPListenPort",nntpport);
\r
107 if(Option::instance()->Get("NNTPBindAddresses",bindaddresses)==false)
\r
109 bindaddresses="127.0.0.1";
\r
110 Option::instance()->Set("NNTPBindAddresses",bindaddresses);
\r
112 StringFunctions::Split(bindaddresses,",",listenaddresses);
\r
114 for(std::vector<std::string>::iterator i=listenaddresses.begin(); i!=listenaddresses.end(); i++)
\r
118 struct addrinfo hint,*result,*current;
\r
119 result=current=NULL;
\r
120 memset(&hint,0,sizeof(hint));
\r
121 hint.ai_socktype=SOCK_STREAM;
\r
122 hint.ai_protocol=IPPROTO_TCP;
\r
123 hint.ai_flags=AI_PASSIVE;
\r
125 rval=getaddrinfo((*i).c_str(),nntpport.c_str(),&hint,&result);
\r
128 for(current=result; current!=NULL; current=current->ai_next)
\r
130 sock=socket(current->ai_family,current->ai_socktype,current->ai_protocol);
\r
131 if(sock!=INVALID_SOCKET)
\r
133 if(bind(sock,current->ai_addr,current->ai_addrlen)==0)
\r
135 if(listen(sock,10)==0)
\r
137 LogFile::instance()->WriteLog(LogFile::LOGLEVEL_INFO,"NNTPListener::StartListen started listening at "+(*i)+":"+nntpport);
\r
138 m_listensockets.push_back(sock);
\r
142 LogFile::instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen socket listen failed");
\r
152 LogFile::instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen socket bind failed");
\r
162 LogFile::instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen couldn't create socket");
\r
168 freeaddrinfo(result);
\r
171 if(m_listensockets.size()==0)
\r
173 LogFile::instance()->WriteLog(LogFile::LOGLEVEL_FATAL,"NNTPListener::StartListen couldn't start listening on any sockets");
\r