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(PLUGIN_LOGGER & l)
62 m_bindAddress("0.0.0.0"),
70 CONFIGPROTO::~CONFIGPROTO()
72 std::deque<STG::Conn *>::iterator it;
73 for (it = m_conns.begin(); it != m_conns.end(); ++it)
77 int CONFIGPROTO::Prepare()
79 sigset_t sigmask, oldmask;
80 sigemptyset(&sigmask);
81 sigaddset(&sigmask, SIGINT);
82 sigaddset(&sigmask, SIGTERM);
83 sigaddset(&sigmask, SIGUSR1);
84 sigaddset(&sigmask, SIGHUP);
85 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) + "'.";
97 if (setsockopt(m_listenSocket, SOL_SOCKET, SO_REUSEADDR, &dummy, 4) != 0)
99 m_errorStr = std::string("Failed to set SO_REUSEADDR to the listen socket: '") + strerror(errno) + "'.";
100 m_logger(m_errorStr);
107 if (listen(m_listenSocket, 64) == -1) // TODO: backlog length
109 m_errorStr = std::string("Failed to start listening for connections: '") + strerror(errno) + "'.";
110 m_logger(m_errorStr);
121 int CONFIGPROTO::Stop()
124 for (int i = 0; i < 5 && !m_stopped; ++i)
126 struct timespec ts = {0, 200000000};
127 nanosleep(&ts, NULL);
132 m_errorStr = "Cannot stop listenign thread.";
133 m_logger(m_errorStr);
137 shutdown(m_listenSocket, SHUT_RDWR);
138 close(m_listenSocket);
142 void CONFIGPROTO::Run()
154 int res = select(MaxFD() + 1, &fds, NULL, NULL, &tv);
157 m_errorStr = std::string("'select' is failed: '") + strerror(errno) + "'.";
158 printfd(__FILE__, "%s\n", m_errorStr.c_str());
159 m_logger(m_errorStr);
172 bool CONFIGPROTO::Bind()
174 const hostent * he = gethostbyname(m_bindAddress.c_str());
177 m_errorStr = "Failed to resolve name '" + m_bindAddress + "': '" + hstrerror(h_errno) + "'.";
178 printfd(__FILE__, "%s\n", m_errorStr.c_str());
179 m_logger(m_errorStr);
183 char ** ptr = he->h_addr_list;
186 struct sockaddr_in listenAddr;
187 listenAddr.sin_family = PF_INET;
188 listenAddr.sin_port = htons(m_port);
189 listenAddr.sin_addr.s_addr = *reinterpret_cast<in_addr_t *>(*ptr);
191 printfd(__FILE__, "Trying to bind to %s:%d\n", inet_ntostring(listenAddr.sin_addr.s_addr).c_str(), m_port);
193 if (bind(m_listenSocket, reinterpret_cast<sockaddr *>(&listenAddr), sizeof(listenAddr)) == 0)
196 m_errorStr = std::string("Cannot bind listen socket: '") + strerror(errno) + "'.";
197 printfd(__FILE__, "%s\n", m_errorStr.c_str());
198 m_logger(m_errorStr);
206 void CONFIGPROTO::RegisterParsers()
208 assert(m_settings != NULL);
209 assert(m_store != NULL);
210 assert(m_admins != NULL);
211 assert(m_users != NULL);
212 assert(m_tariffs != NULL);
213 assert(m_services != NULL);
214 assert(m_corporations != NULL);
216 SP::GET_SERVER_INFO::FACTORY::Register(m_registry, *m_settings, *m_users, *m_tariffs);
218 SP::GET_ADMINS::FACTORY::Register(m_registry, *m_admins);
219 SP::ADD_ADMIN::FACTORY::Register(m_registry, *m_admins);
220 SP::DEL_ADMIN::FACTORY::Register(m_registry, *m_admins);
221 SP::CHG_ADMIN::FACTORY::Register(m_registry, *m_admins);
223 SP::GET_TARIFFS::FACTORY::Register(m_registry, *m_tariffs);
224 SP::ADD_TARIFF::FACTORY::Register(m_registry, *m_tariffs);
225 SP::DEL_TARIFF::FACTORY::Register(m_registry, *m_tariffs, *m_users);
226 SP::CHG_TARIFF::FACTORY::Register(m_registry, *m_tariffs);
228 SP::GET_USERS::FACTORY::Register(m_registry, *m_users);
229 SP::GET_USER::FACTORY::Register(m_registry, *m_users);
230 SP::ADD_USER::FACTORY::Register(m_registry, *m_users);
231 SP::DEL_USER::FACTORY::Register(m_registry, *m_users);
232 SP::CHG_USER::FACTORY::Register(m_registry, *m_users, *m_store, *m_tariffs);
233 SP::CHECK_USER::FACTORY::Register(m_registry, *m_users);
235 SP::GET_SERVICES::FACTORY::Register(m_registry, *m_services);
236 SP::GET_SERVICE::FACTORY::Register(m_registry, *m_services);
237 SP::ADD_SERVICE::FACTORY::Register(m_registry, *m_services);
238 SP::DEL_SERVICE::FACTORY::Register(m_registry, *m_services);
239 SP::CHG_SERVICE::FACTORY::Register(m_registry, *m_services);
241 SP::SEND_MESSAGE::FACTORY::Register(m_registry, *m_users);
243 SP::AUTH_BY::FACTORY::Register(m_registry, *m_users);
245 SP::USER_INFO::FACTORY::Register(m_registry, *m_users);
248 int CONFIGPROTO::MaxFD() const
250 int maxFD = m_listenSocket;
251 std::deque<STG::Conn *>::const_iterator it;
252 for (it = m_conns.begin(); it != m_conns.end(); ++it)
253 if (maxFD < (*it)->Sock())
254 maxFD = (*it)->Sock();
258 void CONFIGPROTO::BuildFDSet(fd_set & fds) const
261 FD_SET(m_listenSocket, &fds);
262 std::deque<STG::Conn *>::const_iterator it;
263 for (it = m_conns.begin(); it != m_conns.end(); ++it)
264 FD_SET((*it)->Sock(), &fds);
267 void CONFIGPROTO::CleanupConns()
269 std::deque<STG::Conn *>::iterator pos;
270 for (pos = m_conns.begin(); pos != m_conns.end(); ++pos)
271 if (((*pos)->IsDone() && !(*pos)->IsKeepAlive()) || !(*pos)->IsOk())
277 pos = std::remove(m_conns.begin(), m_conns.end(), static_cast<STG::Conn *>(NULL));
278 m_conns.erase(pos, m_conns.end());
281 void CONFIGPROTO::HandleEvents(const fd_set & fds)
283 if (FD_ISSET(m_listenSocket, &fds))
287 std::deque<STG::Conn *>::iterator it;
288 for (it = m_conns.begin(); it != m_conns.end(); ++it)
289 if (FD_ISSET((*it)->Sock(), &fds))
294 void CONFIGPROTO::AcceptConnection()
296 struct sockaddr_in outerAddr;
297 socklen_t outerAddrLen(sizeof(outerAddr));
298 int sock = accept(m_listenSocket, reinterpret_cast<sockaddr *>(&outerAddr), &outerAddrLen);
302 m_errorStr = std::string("Failed to accept connection: '") + strerror(errno) + "'.";
303 printfd(__FILE__, "%s\n", m_errorStr.c_str());
304 m_logger(m_errorStr);
308 assert(m_admins != NULL);
312 m_conns.push_back(new STG::Conn(m_registry, *m_admins, sock, outerAddr, m_logger));
313 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());
315 catch (const STG::Conn::Error & error)
318 m_logger(std::string("Failed to create new client connection: '") + error.what() + "'.");