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"
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
50 namespace SP = STG::PARSER;
52 CONFIGPROTO::CONFIGPROTO(PLUGIN_LOGGER & l)
58 m_bindAddress("0.0.0.0"),
66 CONFIGPROTO::~CONFIGPROTO()
68 std::deque<STG::Conn *>::iterator it;
69 for (it = m_conns.begin(); it != m_conns.end(); ++it)
73 int CONFIGPROTO::Prepare()
75 sigset_t sigmask, oldmask;
76 sigemptyset(&sigmask);
77 sigaddset(&sigmask, SIGINT);
78 sigaddset(&sigmask, SIGTERM);
79 sigaddset(&sigmask, SIGUSR1);
80 sigaddset(&sigmask, SIGHUP);
81 pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
83 m_listenSocket = socket(PF_INET, SOCK_STREAM, 0);
85 if (m_listenSocket < 0)
87 m_errorStr = std::string("Cannot create listen socket: '") + strerror(errno) + "'.";
94 if (setsockopt(m_listenSocket, SOL_SOCKET, SO_REUSEADDR, &dummy, 4) != 0)
96 m_errorStr = std::string("Failed to set SO_REUSEADDR to the listen socket: '") + strerror(errno) + "'.";
104 if (listen(m_listenSocket, 64) == -1) // TODO: backlog length
106 m_errorStr = std::string("Failed to start listening for connections: '") + strerror(errno) + "'.";
107 m_logger(m_errorStr);
118 int CONFIGPROTO::Stop()
121 for (int i = 0; i < 5 && !m_stopped; ++i)
123 struct timespec ts = {0, 200000000};
124 nanosleep(&ts, NULL);
129 m_errorStr = "Cannot stop listenign thread.";
130 m_logger(m_errorStr);
134 shutdown(m_listenSocket, SHUT_RDWR);
135 close(m_listenSocket);
139 void CONFIGPROTO::Run()
151 int res = select(MaxFD() + 1, &fds, NULL, NULL, &tv);
154 m_errorStr = std::string("'select' is failed: '") + strerror(errno) + "'.";
155 printfd(__FILE__, "%s\n", m_errorStr.c_str());
156 m_logger(m_errorStr);
169 bool CONFIGPROTO::Bind()
171 const hostent * he = gethostbyname(m_bindAddress.c_str());
174 m_errorStr = "Failed to resolve name '" + m_bindAddress + "': '" + hstrerror(h_errno) + "'.";
175 printfd(__FILE__, "%s\n", m_errorStr.c_str());
176 m_logger(m_errorStr);
180 char ** ptr = he->h_addr_list;
183 struct sockaddr_in listenAddr;
184 listenAddr.sin_family = PF_INET;
185 listenAddr.sin_port = htons(m_port);
186 listenAddr.sin_addr.s_addr = *reinterpret_cast<in_addr_t *>(*ptr);
188 printfd(__FILE__, "Trying to bind to %s:%d\n", inet_ntostring(listenAddr.sin_addr.s_addr).c_str(), m_port);
190 if (bind(m_listenSocket, reinterpret_cast<sockaddr *>(&listenAddr), sizeof(listenAddr)) == 0)
193 m_errorStr = std::string("Cannot bind listen socket: '") + strerror(errno) + "'.";
194 printfd(__FILE__, "%s\n", m_errorStr.c_str());
195 m_logger(m_errorStr);
203 void CONFIGPROTO::RegisterParsers()
205 assert(m_settings != NULL);
206 assert(m_store != NULL);
207 assert(m_admins != NULL);
208 assert(m_users != NULL);
209 assert(m_tariffs != NULL);
211 SP::GET_SERVER_INFO::FACTORY::Register(m_registry, *m_settings, *m_users, *m_tariffs);
213 SP::GET_ADMINS::FACTORY::Register(m_registry, *m_admins);
214 SP::ADD_ADMIN::FACTORY::Register(m_registry, *m_admins);
215 SP::DEL_ADMIN::FACTORY::Register(m_registry, *m_admins);
216 SP::CHG_ADMIN::FACTORY::Register(m_registry, *m_admins);
218 SP::GET_TARIFFS::FACTORY::Register(m_registry, *m_tariffs);
219 SP::ADD_TARIFF::FACTORY::Register(m_registry, *m_tariffs);
220 SP::DEL_TARIFF::FACTORY::Register(m_registry, *m_tariffs, *m_users);
221 SP::CHG_TARIFF::FACTORY::Register(m_registry, *m_tariffs);
223 SP::GET_USERS::FACTORY::Register(m_registry, *m_users);
224 SP::GET_USER::FACTORY::Register(m_registry, *m_users);
225 SP::ADD_USER::FACTORY::Register(m_registry, *m_users);
226 SP::DEL_USER::FACTORY::Register(m_registry, *m_users);
227 SP::CHG_USER::FACTORY::Register(m_registry, *m_users, *m_store, *m_tariffs);
228 SP::CHECK_USER::FACTORY::Register(m_registry, *m_users);
230 SP::SEND_MESSAGE::FACTORY::Register(m_registry, *m_users);
232 SP::AUTH_BY::FACTORY::Register(m_registry, *m_users);
234 SP::USER_INFO::FACTORY::Register(m_registry, *m_users);
237 int CONFIGPROTO::MaxFD() const
239 int maxFD = m_listenSocket;
240 std::deque<STG::Conn *>::const_iterator it;
241 for (it = m_conns.begin(); it != m_conns.end(); ++it)
242 if (maxFD < (*it)->Sock())
243 maxFD = (*it)->Sock();
247 void CONFIGPROTO::BuildFDSet(fd_set & fds) const
250 FD_SET(m_listenSocket, &fds);
251 std::deque<STG::Conn *>::const_iterator it;
252 for (it = m_conns.begin(); it != m_conns.end(); ++it)
253 FD_SET((*it)->Sock(), &fds);
256 void CONFIGPROTO::CleanupConns()
258 std::deque<STG::Conn *>::iterator pos;
259 for (pos = m_conns.begin(); pos != m_conns.end(); ++pos)
260 if (((*pos)->IsDone() && !(*pos)->IsKeepAlive()) || !(*pos)->IsOk())
266 pos = std::remove(m_conns.begin(), m_conns.end(), static_cast<STG::Conn *>(NULL));
267 m_conns.erase(pos, m_conns.end());
270 void CONFIGPROTO::HandleEvents(const fd_set & fds)
272 if (FD_ISSET(m_listenSocket, &fds))
276 std::deque<STG::Conn *>::iterator it;
277 for (it = m_conns.begin(); it != m_conns.end(); ++it)
278 if (FD_ISSET((*it)->Sock(), &fds))
283 void CONFIGPROTO::AcceptConnection()
285 struct sockaddr_in outerAddr;
286 socklen_t outerAddrLen(sizeof(outerAddr));
287 int sock = accept(m_listenSocket, reinterpret_cast<sockaddr *>(&outerAddr), &outerAddrLen);
291 m_errorStr = std::string("Failed to accept connection: '") + strerror(errno) + "'.";
292 printfd(__FILE__, "%s\n", m_errorStr.c_str());
293 m_logger(m_errorStr);
297 assert(m_admins != NULL);
301 m_conns.push_back(new STG::Conn(m_registry, *m_admins, sock, outerAddr, m_logger));
302 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());
304 catch (const STG::Conn::Error & error)
307 m_logger(std::string("Failed to create new client connection: '") + error.what() + "'.");