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>
25 #include "parser_server_info.h"
26 #include "parser_admins.h"
27 #include "parser_tariffs.h"
28 #include "parser_users.h"
29 #include "parser_message.h"
30 #include "parser_user_info.h"
31 #include "parser_auth_by.h"
33 #include "stg/common.h"
34 #include "stg/logger.h"
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
48 namespace SP = STG::PARSER;
53 struct IsFinished : public std::unary_function<STG::Conn *, bool>
55 result_type operator()(const argument_type & arg)
57 return (arg->IsDone() && !arg->IsKeepAlive()) || !arg->IsOk();
61 struct RemoveConn : public std::unary_function<STG::Conn *, void>
63 result_type operator()(const argument_type & arg)
71 CONFIGPROTO::CONFIGPROTO(PLUGIN_LOGGER & l)
84 CONFIGPROTO::~CONFIGPROTO()
86 std::for_each(m_conns.begin(), m_conns.end(), RemoveConn());
89 int CONFIGPROTO::Prepare()
91 sigset_t sigmask, oldmask;
92 sigemptyset(&sigmask);
93 sigaddset(&sigmask, SIGINT);
94 sigaddset(&sigmask, SIGTERM);
95 sigaddset(&sigmask, SIGUSR1);
96 sigaddset(&sigmask, SIGHUP);
97 pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
99 m_listenSocket = socket(PF_INET, SOCK_STREAM, 0);
101 if (m_listenSocket < 0)
103 m_errorStr = std::string("Cannot create listen socket: '") + strerror(errno) + "'.";
104 m_logger(m_errorStr);
108 struct sockaddr_in listenAddr;
109 listenAddr.sin_family = PF_INET;
110 listenAddr.sin_port = htons(m_port);
111 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0"); // TODO: arbitrary address
115 if (setsockopt(m_listenSocket, SOL_SOCKET, SO_REUSEADDR, &dummy, 4) != 0)
117 m_errorStr = std::string("Failed to set SO_REUSEADDR to the listen socket: '") + strerror(errno) + "'.";
118 m_logger(m_errorStr);
122 if (bind(m_listenSocket, reinterpret_cast<sockaddr *>(&listenAddr), sizeof(listenAddr)) == -1)
124 m_errorStr = std::string("Cannot bind listen socket: '") + strerror(errno) + "'.";
125 m_logger(m_errorStr);
129 if (listen(m_listenSocket, 64) == -1) // TODO: backlog length
131 m_errorStr = std::string("Failed to start listening for connections: '") + strerror(errno) + "'.";
132 m_logger(m_errorStr);
143 int CONFIGPROTO::Stop()
146 for (int i = 0; i < 5 && !m_stopped; ++i)
148 struct timespec ts = {0, 200000000};
149 nanosleep(&ts, NULL);
154 m_errorStr = "Cannot stop listenign thread.";
155 m_logger(m_errorStr);
159 shutdown(m_listenSocket, SHUT_RDWR);
160 close(m_listenSocket);
164 void CONFIGPROTO::Run()
176 int res = select(MaxFD() + 1, &fds, NULL, NULL, &tv);
179 m_errorStr = std::string("'select' is failed: '") + strerror(errno) + "'.";
180 m_logger(m_errorStr);
193 void CONFIGPROTO::RegisterParsers()
195 assert(m_settings != NULL);
196 assert(m_store != NULL);
197 assert(m_admins != NULL);
198 assert(m_users != NULL);
199 assert(m_tariffs != NULL);
201 SP::GET_SERVER_INFO::FACTORY::Register(m_registry, *m_settings, *m_users, *m_tariffs);
203 SP::GET_ADMINS::FACTORY::Register(m_registry, *m_admins);
204 SP::ADD_ADMIN::FACTORY::Register(m_registry, *m_admins);
205 SP::DEL_ADMIN::FACTORY::Register(m_registry, *m_admins);
206 SP::CHG_ADMIN::FACTORY::Register(m_registry, *m_admins);
208 SP::GET_TARIFFS::FACTORY::Register(m_registry, *m_tariffs);
209 SP::ADD_TARIFF::FACTORY::Register(m_registry, *m_tariffs);
210 SP::DEL_TARIFF::FACTORY::Register(m_registry, *m_tariffs, *m_users);
211 SP::CHG_TARIFF::FACTORY::Register(m_registry, *m_tariffs);
213 SP::GET_USERS::FACTORY::Register(m_registry, *m_users);
214 SP::GET_USER::FACTORY::Register(m_registry, *m_users);
215 SP::ADD_USER::FACTORY::Register(m_registry, *m_users);
216 SP::DEL_USER::FACTORY::Register(m_registry, *m_users);
217 SP::CHG_USER::FACTORY::Register(m_registry, *m_users, *m_store, *m_tariffs);
218 SP::CHECK_USER::FACTORY::Register(m_registry, *m_users);
220 SP::SEND_MESSAGE::FACTORY::Register(m_registry, *m_users);
222 SP::AUTH_BY::FACTORY::Register(m_registry, *m_users);
224 SP::USER_INFO::FACTORY::Register(m_registry, *m_users);
227 int CONFIGPROTO::MaxFD() const
229 int maxFD = m_listenSocket;
230 for (size_t i = 0; i < m_conns.size(); ++i)
231 if (maxFD < m_conns[i]->Sock())
232 maxFD = m_conns[i]->Sock();
236 void CONFIGPROTO::BuildFDSet(fd_set & fds) const
238 for (size_t i = 0; i < m_conns.size(); ++i)
239 FD_SET(m_conns[i]->Sock(), &fds);
242 void CONFIGPROTO::CleanupConns()
244 std::vector<STG::Conn *>::iterator pos;
245 pos = std::remove_if(m_conns.begin(), m_conns.end(), IsFinished());
246 if (pos == m_conns.end())
248 std::for_each(pos, m_conns.end(), RemoveConn());
249 m_conns.erase(pos, m_conns.end());
252 void CONFIGPROTO::HandleEvents(const fd_set & fds)
254 if (FD_ISSET(m_listenSocket, &fds))
258 for (size_t i = 0; i < m_conns.size(); ++i)
259 if (FD_ISSET(m_conns[i]->Sock(), &fds))
264 void CONFIGPROTO::AcceptConnection()
266 struct sockaddr_in outerAddr;
267 socklen_t outerAddrLen(sizeof(outerAddr));
268 int sock = accept(m_listenSocket, reinterpret_cast<sockaddr *>(&outerAddr), &outerAddrLen);
272 m_errorStr = std::string("Failed to accept connection: '") + strerror(errno) + "'.";
273 printfd(__FILE__, "%s", m_errorStr.c_str());
274 m_logger(m_errorStr);
278 assert(m_admins != NULL);
282 m_conns.push_back(new STG::Conn(m_registry, *m_admins, sock, outerAddr));
283 printfd(__FILE__, "New connection from %s:%d\n", inet_ntostring(m_conns.back()->IP()).c_str(), m_conns.back()->Port());
285 catch (const STG::Conn::Error & error)
288 m_logger(std::string("Failed to create new client connection: '") + error.what() + "'.");
292 void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip)
294 m_logger("Admin's connection failed. IP %s", inet_ntostring(ip).c_str());