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 "parser_server_info.h"
27 #include "parser_admins.h"
28 #include "parser_tariffs.h"
29 #include "parser_users.h"
30 #include "parser_message.h"
31 #include "parser_user_info.h"
32 #include "parser_auth_by.h"
34 #include "stg/common.h"
35 #include "stg/logger.h"
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
49 namespace SP = STG::PARSER;
54 struct IsFinished : public std::unary_function<STG::Conn *, bool>
56 result_type operator()(const argument_type & arg)
58 return (arg->IsDone() && !arg->IsKeepAlive()) || !arg->IsOk();
62 struct RemoveConn : public std::unary_function<STG::Conn *, void>
64 result_type operator()(const argument_type & arg)
72 CONFIGPROTO::CONFIGPROTO(PLUGIN_LOGGER & l)
85 CONFIGPROTO::~CONFIGPROTO()
87 std::for_each(m_conns.begin(), m_conns.end(), RemoveConn());
90 int CONFIGPROTO::Prepare()
92 sigset_t sigmask, oldmask;
93 sigemptyset(&sigmask);
94 sigaddset(&sigmask, SIGINT);
95 sigaddset(&sigmask, SIGTERM);
96 sigaddset(&sigmask, SIGUSR1);
97 sigaddset(&sigmask, SIGHUP);
98 pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
100 m_listenSocket = socket(PF_INET, SOCK_STREAM, 0);
102 if (m_listenSocket < 0)
104 m_errorStr = std::string("Cannot create listen socket: '") + strerror(errno) + "'.";
105 m_logger(m_errorStr);
109 struct sockaddr_in listenAddr;
110 listenAddr.sin_family = PF_INET;
111 listenAddr.sin_port = htons(m_port);
112 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0"); // TODO: arbitrary address
116 if (setsockopt(m_listenSocket, SOL_SOCKET, SO_REUSEADDR, &dummy, 4) != 0)
118 m_errorStr = std::string("Failed to set SO_REUSEADDR to the listen socket: '") + strerror(errno) + "'.";
119 m_logger(m_errorStr);
123 if (bind(m_listenSocket, reinterpret_cast<sockaddr *>(&listenAddr), sizeof(listenAddr)) == -1)
125 m_errorStr = std::string("Cannot bind listen socket: '") + strerror(errno) + "'.";
126 m_logger(m_errorStr);
130 if (listen(m_listenSocket, 64) == -1) // TODO: backlog length
132 m_errorStr = std::string("Failed to start listening for connections: '") + strerror(errno) + "'.";
133 m_logger(m_errorStr);
144 int CONFIGPROTO::Stop()
147 for (int i = 0; i < 5 && !m_stopped; ++i)
149 struct timespec ts = {0, 200000000};
150 nanosleep(&ts, NULL);
155 m_errorStr = "Cannot stop listenign thread.";
156 m_logger(m_errorStr);
160 shutdown(m_listenSocket, SHUT_RDWR);
161 close(m_listenSocket);
165 void CONFIGPROTO::Run()
177 int res = select(MaxFD() + 1, &fds, NULL, NULL, &tv);
180 m_errorStr = std::string("'select' is failed: '") + strerror(errno) + "'.";
181 m_logger(m_errorStr);
194 void CONFIGPROTO::RegisterParsers()
196 assert(m_settings != NULL);
197 assert(m_store != NULL);
198 assert(m_admins != NULL);
199 assert(m_users != NULL);
200 assert(m_tariffs != NULL);
202 SP::GET_SERVER_INFO::FACTORY::Register(m_registry, *m_settings, *m_users, *m_tariffs);
204 SP::GET_ADMINS::FACTORY::Register(m_registry, *m_admins);
205 SP::ADD_ADMIN::FACTORY::Register(m_registry, *m_admins);
206 SP::DEL_ADMIN::FACTORY::Register(m_registry, *m_admins);
207 SP::CHG_ADMIN::FACTORY::Register(m_registry, *m_admins);
209 SP::GET_TARIFFS::FACTORY::Register(m_registry, *m_tariffs);
210 SP::ADD_TARIFF::FACTORY::Register(m_registry, *m_tariffs);
211 SP::DEL_TARIFF::FACTORY::Register(m_registry, *m_tariffs, *m_users);
212 SP::CHG_TARIFF::FACTORY::Register(m_registry, *m_tariffs);
214 SP::GET_USERS::FACTORY::Register(m_registry, *m_users);
215 SP::GET_USER::FACTORY::Register(m_registry, *m_users);
216 SP::ADD_USER::FACTORY::Register(m_registry, *m_users);
217 SP::DEL_USER::FACTORY::Register(m_registry, *m_users);
218 SP::CHG_USER::FACTORY::Register(m_registry, *m_users, *m_store, *m_tariffs);
219 SP::CHECK_USER::FACTORY::Register(m_registry, *m_users);
221 SP::SEND_MESSAGE::FACTORY::Register(m_registry, *m_users);
223 SP::AUTH_BY::FACTORY::Register(m_registry, *m_users);
225 SP::USER_INFO::FACTORY::Register(m_registry, *m_users);
228 int CONFIGPROTO::MaxFD() const
230 int maxFD = m_listenSocket;
231 for (size_t i = 0; i < m_conns.size(); ++i)
232 if (maxFD < m_conns[i]->Sock())
233 maxFD = m_conns[i]->Sock();
237 void CONFIGPROTO::BuildFDSet(fd_set & fds) const
239 for (size_t i = 0; i < m_conns.size(); ++i)
240 FD_SET(m_conns[i]->Sock(), &fds);
243 void CONFIGPROTO::CleanupConns()
245 std::vector<STG::Conn *>::iterator pos;
246 pos = std::remove_if(m_conns.begin(), m_conns.end(), IsFinished());
247 if (pos == m_conns.end())
249 std::for_each(pos, m_conns.end(), RemoveConn());
250 m_conns.erase(pos, m_conns.end());
253 void CONFIGPROTO::HandleEvents(const fd_set & fds)
255 if (FD_ISSET(m_listenSocket, &fds))
259 for (size_t i = 0; i < m_conns.size(); ++i)
260 if (FD_ISSET(m_conns[i]->Sock(), &fds))
265 void CONFIGPROTO::AcceptConnection()
267 struct sockaddr_in outerAddr;
268 socklen_t outerAddrLen(sizeof(outerAddr));
269 int sock = accept(m_listenSocket, reinterpret_cast<sockaddr *>(&outerAddr), &outerAddrLen);
273 m_errorStr = std::string("Failed to accept connection: '") + strerror(errno) + "'.";
274 printfd(__FILE__, "%s", m_errorStr.c_str());
275 m_logger(m_errorStr);
279 assert(m_admins != NULL);
283 m_conns.push_back(new STG::Conn(m_registry, *m_admins, sock, outerAddr));
284 printfd(__FILE__, "New connection from %s:%d\n", inet_ntostring(m_conns.back()->IP()).c_str(), m_conns.back()->Port());
286 catch (const STG::Conn::Error & error)
289 m_logger(std::string("Failed to create new client connection: '") + error.what() + "'.");
293 void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip)
295 m_logger("Admin's connection failed. IP %s", inet_ntostring(ip).c_str());