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_services.h"
31 #include "parser_message.h"
32 #include "parser_user_info.h"
33 #include "parser_auth_by.h"
35 #include "stg/common.h"
36 #include "stg/logger.h"
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
51 namespace SP = STG::PARSER;
53 CONFIGPROTO::CONFIGPROTO(STG::PluginLogger & l)
62 m_bindAddress("0.0.0.0"),
69 CONFIGPROTO::~CONFIGPROTO()
72 std::deque<STG::Conn *>::iterator it;
73 for (it = m_conns.begin(); it != m_conns.end(); ++it)
77 BASE_PARSER::REGISTRY::iterator it;
78 for (it = m_registry.begin(); it != m_registry.end(); ++it)
83 int CONFIGPROTO::Prepare()
85 sigset_t sigmask, oldmask;
86 sigemptyset(&sigmask);
87 sigaddset(&sigmask, SIGINT);
88 sigaddset(&sigmask, SIGTERM);
89 sigaddset(&sigmask, SIGUSR1);
90 sigaddset(&sigmask, SIGHUP);
91 pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
92 m_listenSocket = socket(PF_INET, SOCK_STREAM, 0);
94 if (m_listenSocket < 0)
96 m_errorStr = std::string("Cannot create listen socket: '") + strerror(errno) + "'.";
103 if (setsockopt(m_listenSocket, SOL_SOCKET, SO_REUSEADDR, &dummy, 4) != 0)
105 m_errorStr = std::string("Failed to set SO_REUSEADDR to the listen socket: '") + strerror(errno) + "'.";
106 m_logger(m_errorStr);
113 if (listen(m_listenSocket, 64) == -1) // TODO: backlog length
115 m_errorStr = std::string("Failed to start listening for connections: '") + strerror(errno) + "'.";
116 m_logger(m_errorStr);
126 int CONFIGPROTO::Stop()
128 for (int i = 0; i < 5 && !m_stopped; ++i)
130 struct timespec ts = {0, 200000000};
131 nanosleep(&ts, NULL);
136 m_errorStr = "Cannot stop listenign thread.";
137 m_logger(m_errorStr);
141 shutdown(m_listenSocket, SHUT_RDWR);
142 close(m_listenSocket);
146 void CONFIGPROTO::Run(std::stop_token token)
148 while (!token.stop_requested())
158 int res = select(MaxFD() + 1, &fds, NULL, NULL, &tv);
161 m_errorStr = std::string("'select' is failed: '") + strerror(errno) + "'.";
162 printfd(__FILE__, "%s\n", m_errorStr.c_str());
163 m_logger(m_errorStr);
166 if (token.stop_requested())
176 bool CONFIGPROTO::Bind()
178 const hostent * he = gethostbyname(m_bindAddress.c_str());
181 m_errorStr = "Failed to resolve name '" + m_bindAddress + "': '" + hstrerror(h_errno) + "'.";
182 printfd(__FILE__, "%s\n", m_errorStr.c_str());
183 m_logger(m_errorStr);
187 char ** ptr = he->h_addr_list;
190 struct sockaddr_in listenAddr;
191 listenAddr.sin_family = PF_INET;
192 listenAddr.sin_port = htons(m_port);
193 listenAddr.sin_addr.s_addr = *reinterpret_cast<in_addr_t *>(*ptr);
195 printfd(__FILE__, "Trying to bind to %s:%d\n", inet_ntostring(listenAddr.sin_addr.s_addr).c_str(), m_port);
197 if (bind(m_listenSocket, reinterpret_cast<sockaddr *>(&listenAddr), sizeof(listenAddr)) == 0)
200 m_errorStr = std::string("Cannot bind listen socket: '") + strerror(errno) + "'.";
201 printfd(__FILE__, "%s\n", m_errorStr.c_str());
202 m_logger(m_errorStr);
210 void CONFIGPROTO::RegisterParsers()
212 assert(m_settings != NULL);
213 assert(m_store != NULL);
214 assert(m_admins != NULL);
215 assert(m_users != NULL);
216 assert(m_tariffs != NULL);
217 assert(m_services != NULL);
218 assert(m_corporations != NULL);
220 SP::GET_SERVER_INFO::FACTORY::Register(m_registry, *m_settings, *m_users, *m_tariffs);
222 SP::GET_ADMINS::FACTORY::Register(m_registry, *m_admins);
223 SP::ADD_ADMIN::FACTORY::Register(m_registry, *m_admins);
224 SP::DEL_ADMIN::FACTORY::Register(m_registry, *m_admins);
225 SP::CHG_ADMIN::FACTORY::Register(m_registry, *m_admins);
227 SP::GET_TARIFFS::FACTORY::Register(m_registry, *m_tariffs);
228 SP::ADD_TARIFF::FACTORY::Register(m_registry, *m_tariffs);
229 SP::DEL_TARIFF::FACTORY::Register(m_registry, *m_tariffs, *m_users);
230 SP::CHG_TARIFF::FACTORY::Register(m_registry, *m_tariffs);
232 SP::GET_USERS::FACTORY::Register(m_registry, *m_users);
233 SP::GET_USER::FACTORY::Register(m_registry, *m_users);
234 SP::ADD_USER::FACTORY::Register(m_registry, *m_users);
235 SP::DEL_USER::FACTORY::Register(m_registry, *m_users);
236 SP::CHG_USER::FACTORY::Register(m_registry, *m_users, *m_store, *m_tariffs);
237 SP::CHECK_USER::FACTORY::Register(m_registry, *m_users);
239 SP::GET_SERVICES::FACTORY::Register(m_registry, *m_services);
240 SP::GET_SERVICE::FACTORY::Register(m_registry, *m_services);
241 SP::ADD_SERVICE::FACTORY::Register(m_registry, *m_services);
242 SP::DEL_SERVICE::FACTORY::Register(m_registry, *m_services);
243 SP::CHG_SERVICE::FACTORY::Register(m_registry, *m_services);
245 SP::SEND_MESSAGE::FACTORY::Register(m_registry, *m_users);
247 SP::AUTH_BY::FACTORY::Register(m_registry, *m_users);
249 SP::USER_INFO::FACTORY::Register(m_registry, *m_users);
252 int CONFIGPROTO::MaxFD() const
254 int maxFD = m_listenSocket;
255 std::deque<STG::Conn *>::const_iterator it;
256 for (it = m_conns.begin(); it != m_conns.end(); ++it)
257 if (maxFD < (*it)->Sock())
258 maxFD = (*it)->Sock();
262 void CONFIGPROTO::BuildFDSet(fd_set & fds) const
265 FD_SET(m_listenSocket, &fds);
266 std::deque<STG::Conn *>::const_iterator it;
267 for (it = m_conns.begin(); it != m_conns.end(); ++it)
268 FD_SET((*it)->Sock(), &fds);
271 void CONFIGPROTO::CleanupConns()
273 std::deque<STG::Conn *>::iterator pos;
274 for (pos = m_conns.begin(); pos != m_conns.end(); ++pos)
275 if (((*pos)->IsDone() && !(*pos)->IsKeepAlive()) || !(*pos)->IsOk())
281 pos = std::remove(m_conns.begin(), m_conns.end(), static_cast<STG::Conn *>(NULL));
282 m_conns.erase(pos, m_conns.end());
285 void CONFIGPROTO::HandleEvents(const fd_set & fds)
287 if (FD_ISSET(m_listenSocket, &fds))
291 std::deque<STG::Conn *>::iterator it;
292 for (it = m_conns.begin(); it != m_conns.end(); ++it)
293 if (FD_ISSET((*it)->Sock(), &fds))
298 void CONFIGPROTO::AcceptConnection()
300 struct sockaddr_in outerAddr;
301 socklen_t outerAddrLen(sizeof(outerAddr));
302 int sock = accept(m_listenSocket, reinterpret_cast<sockaddr *>(&outerAddr), &outerAddrLen);
306 m_errorStr = std::string("Failed to accept connection: '") + strerror(errno) + "'.";
307 printfd(__FILE__, "%s\n", m_errorStr.c_str());
308 m_logger(m_errorStr);
312 assert(m_admins != NULL);
316 m_conns.push_back(new STG::Conn(m_registry, *m_admins, sock, outerAddr, m_logger));
317 printfd(__FILE__, "New connection from %s:%d. Total connections: %d\n", inet_ntostring(m_conns.back()->IP()).c_str(), m_conns.back()->Port(), m_conns.size());
319 catch (const STG::Conn::Error & error)
322 m_logger(std::string("Failed to create new client connection: '") + error.what() + "'.");