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 "stg/common.h"
26 #include "stg/logger.h"
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
43 struct IsFinished : public std::unary_function<STG::Conn *, bool>
45 result_type operator()(const argument_type & arg)
47 return (arg->IsDone() && !arg->IsKeepAlive()) || !arg->IsOk();
51 struct RemoveConn : public std::unary_function<STG::Conn *, void>
53 result_type operator()(const argument_type & arg)
61 CONFIGPROTO::CONFIGPROTO(PLUGIN_LOGGER & l)
72 std::for_each(m_conns.begin(), m_conns.end(), RemoveConn());
75 int CONFIGPROTO::Prepare()
77 sigset_t sigmask, oldmask;
78 sigemptyset(&sigmask);
79 sigaddset(&sigmask, SIGINT);
80 sigaddset(&sigmask, SIGTERM);
81 sigaddset(&sigmask, SIGUSR1);
82 sigaddset(&sigmask, SIGHUP);
83 pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
85 m_listenSocket = socket(PF_INET, SOCK_STREAM, 0);
87 if (m_listenSocket < 0)
89 m_errorStr = std::string("Cannot create listen socket: '") + strerror(errno) + "'.";
94 struct sockaddr_in listenAddr;
95 listenAddr.sin_family = PF_INET;
96 listenAddr.sin_port = htons(m_port);
97 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0"); // TODO: arbitrary address
101 if (setsockopt(m_listenSocket, SOL_SOCKET, SO_REUSEADDR, &dummy, 4) != 0)
103 m_errorStr = std::string("Failed to set SO_REUSEADDR to the listen socket: '") + strerror(errno) + "'.";
104 m_logger(m_errorStr);
108 if (bind(m_listenSocket, reinterpret_cast<sockaddr *>(&listenAddr), sizeof(listenAddr)) == -1)
110 m_errorStr = std::string("Cannot bind listen socket: '") + strerror(errno) + "'.";
111 m_logger(m_errorStr);
115 if (listen(m_listenSocket, 64) == -1) // TODO: backlog length
117 m_errorStr = std::string("Failed to start listening for connections: '") + strerror(errno) + "'.";
118 m_logger(m_errorStr);
127 int CONFIGPROTO::Stop()
130 for (int i = 0; i < 5 && !m_stopped; ++i)
132 struct timespec ts = {0, 200000000};
133 nanosleep(&ts, NULL);
138 m_errorStr = "Cannot stop listenign thread.";
139 m_logger(m_errorStr);
143 shutdown(m_listenSocket, SHUT_RDWR);
144 close(m_listenSocket);
148 void CONFIGPROTO::Run()
160 int res = select(MaxFD() + 1, &fds, NULL, NULL, &tv);
163 m_errorStr = std::string("'select' is failed: '") + strerror(errno) + "'.";
164 m_logger(m_errorStr);
177 int CONFIGPROTO::MaxFD() const
179 int maxFD = m_listenSocket;
180 for (size_t i = 0; i < m_conns.size(); ++i)
181 if (maxFD < m_conns[i]->Sock())
182 maxFD = m_conns[i]->Sock();
186 void CONFIGPROTO::BuildFDSet(fd_set & fds) const
188 for (size_t i = 0; i < m_conns.size(); ++i)
189 FD_SET(m_conns[i]->Sock(), &fds);
192 void CONFIGPROTO::CleanupConns()
194 std::vector<STG::Conn *>::iterator pos;
195 pos = std::remove_if(m_conns.begin(), m_conns.end(), IsFinished());
196 if (pos == m_conns.end())
198 std::for_each(pos, m_conns.end(), RemoveConn());
199 m_conns.erase(pos, m_conns.end());
202 void CONFIGPROTO::HandleEvents(const fd_set & fds)
204 if (FD_ISSET(m_listenSocket, &fds))
208 for (size_t i = 0; i < m_conns.size(); ++i)
209 if (FD_ISSET(m_conns[i]->Sock(), &fds))
214 void CONFIGPROTO::AcceptConnection()
216 struct sockaddr_in outerAddr;
217 socklen_t outerAddrLen(sizeof(outerAddr));
218 int sock = accept(m_listenSocket, reinterpret_cast<sockaddr *>(&outerAddr), &outerAddrLen);
222 m_errorStr = std::string("Failed to accept connection: '") + strerror(errno) + "'.";
223 printfd(__FILE__, "%s", m_errorStr.c_str());
224 m_logger(m_errorStr);
228 assert(m_settings != NULL);
229 assert(m_admins != NULL);
230 assert(m_users != NULL);
231 assert(m_tariffs != NULL);
233 m_conns.push_back(new STG::Conn(*m_settings, *m_admins, *m_users, *m_tariffs, sock, outerAddr));
235 printfd(__FILE__, "New connection from %s:%d\n", inet_ntostring(m_conns.back()->IP()).c_str(), m_conns.back()->Port());
238 void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip)
240 m_logger("Admin's connection failed. IP %s", inet_ntostring(ip).c_str());