2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
22 #include "configproto.h"
26 #include "stg/common.h"
27 #include "stg/logger.h"
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
44 struct IsFinished : public std::unary_function<STG::Conn *, bool>
46 result_type operator()(const argument_type & arg)
48 return (arg->IsDone() && !arg->IsKeepAlive()) || !arg->IsOk();
52 struct RemoveConn : public std::unary_function<STG::Conn *, void>
54 result_type operator()(const argument_type & arg)
62 CONFIGPROTO::CONFIGPROTO(PLUGIN_LOGGER & l)
73 std::for_each(m_conns.begin(), m_conns.end(), RemoveConn());
76 int CONFIGPROTO::Prepare()
78 sigset_t sigmask, oldmask;
79 sigemptyset(&sigmask);
80 sigaddset(&sigmask, SIGINT);
81 sigaddset(&sigmask, SIGTERM);
82 sigaddset(&sigmask, SIGUSR1);
83 sigaddset(&sigmask, SIGHUP);
84 pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
86 m_listenSocket = socket(PF_INET, SOCK_STREAM, 0);
88 if (m_listenSocket < 0)
90 m_errorStr = std::string("Cannot create listen socket: '") + strerror(errno) + "'.";
95 struct sockaddr_in listenAddr;
96 listenAddr.sin_family = PF_INET;
97 listenAddr.sin_port = htons(m_port);
98 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0"); // TODO: arbitrary address
102 if (setsockopt(m_listenSocket, SOL_SOCKET, SO_REUSEADDR, &dummy, 4) != 0)
104 m_errorStr = std::string("Failed to set SO_REUSEADDR to the listen socket: '") + strerror(errno) + "'.";
105 m_logger(m_errorStr);
109 if (bind(m_listenSocket, reinterpret_cast<sockaddr *>(&listenAddr), sizeof(listenAddr)) == -1)
111 m_errorStr = std::string("Cannot bind listen socket: '") + strerror(errno) + "'.";
112 m_logger(m_errorStr);
116 if (listen(m_listenSocket, 64) == -1) // TODO: backlog length
118 m_errorStr = std::string("Failed to start listening for connections: '") + strerror(errno) + "'.";
119 m_logger(m_errorStr);
128 int CONFIGPROTO::Stop()
131 for (int i = 0; i < 5 && !m_stopped; ++i)
133 struct timespec ts = {0, 200000000};
134 nanosleep(&ts, NULL);
139 m_errorStr = "Cannot stop listenign thread.";
140 m_logger(m_errorStr);
144 shutdown(m_listenSocket, SHUT_RDWR);
145 close(m_listenSocket);
149 void CONFIGPROTO::Run()
161 int res = select(MaxFD() + 1, &fds, NULL, NULL, &tv);
164 m_errorStr = std::string("'select' is failed: '") + strerror(errno) + "'.";
165 m_logger(m_errorStr);
178 int CONFIGPROTO::MaxFD() const
180 int maxFD = m_listenSocket;
181 for (size_t i = 0; i < m_conns.size(); ++i)
182 if (maxFD < m_conns[i]->Sock())
183 maxFD = m_conns[i]->Sock();
187 void CONFIGPROTO::BuildFDSet(fd_set & fds) const
189 for (size_t i = 0; i < m_conns.size(); ++i)
190 FD_SET(m_conns[i]->Sock(), &fds);
193 void CONFIGPROTO::CleanupConns()
195 std::vector<STG::Conn *>::iterator pos;
196 pos = std::remove_if(m_conns.begin(), m_conns.end(), IsFinished());
197 if (pos == m_conns.end())
199 std::for_each(pos, m_conns.end(), RemoveConn());
200 m_conns.erase(pos, m_conns.end());
203 void CONFIGPROTO::HandleEvents(const fd_set & fds)
205 if (FD_ISSET(m_listenSocket, &fds))
209 for (size_t i = 0; i < m_conns.size(); ++i)
210 if (FD_ISSET(m_conns[i]->Sock(), &fds))
215 void CONFIGPROTO::AcceptConnection()
217 struct sockaddr_in outerAddr;
218 socklen_t outerAddrLen(sizeof(outerAddr));
219 int sock = accept(m_listenSocket, reinterpret_cast<sockaddr *>(&outerAddr), &outerAddrLen);
223 m_errorStr = std::string("Failed to accept connection: '") + strerror(errno) + "'.";
224 printfd(__FILE__, "%s", m_errorStr.c_str());
225 m_logger(m_errorStr);
229 assert(m_settings != NULL);
230 assert(m_admins != NULL);
231 assert(m_users != NULL);
232 assert(m_tariffs != NULL);
234 m_conns.push_back(new STG::Conn(*m_settings, *m_admins, *m_users, *m_tariffs, sock, outerAddr));
236 printfd(__FILE__, "New connection from %s:%d\n", inet_ntostring(m_conns.back()->IP()).c_str(), m_conns.back()->Port());
239 void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip)
241 m_logger("Admin's connection failed. IP %s", inet_ntostring(ip).c_str());