X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/7df336e60c959c440b2c330f7a7ca9b0251db2a1..afcbfd1a09e22ff4839ba5db42047c96f355506c:/projects/stargazer/plugins/configuration/sgconfig/configproto.cpp diff --git a/projects/stargazer/plugins/configuration/sgconfig/configproto.cpp b/projects/stargazer/plugins/configuration/sgconfig/configproto.cpp index 7052ecc2..020c1365 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/configproto.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/configproto.cpp @@ -19,6 +19,7 @@ * Author : Maxim Mamontov */ +#include "configproto.h" #include "conn.h" @@ -27,7 +28,6 @@ #include "parser_tariffs.h" #include "parser_users.h" #include "parser_message.h" -#include "parser_user_info.h" #include "parser_auth_by.h" #include "stg/common.h" @@ -35,6 +35,7 @@ #include #include +#include #include #include #include @@ -43,37 +44,18 @@ #include #include #include -#include +#include namespace SP = STG::PARSER; -namespace -{ - -struct IsFinished : public std::unary_function -{ - result_type operator()(const argument_type & arg) - { - return (arg->IsDone() && !arg->IsKeepAlive()) || !arg->IsOk(); - } -}; - -struct RemoveConn : public std::unary_function -{ - result_type operator()(const argument_type & arg) - { - delete arg; - } -}; - -} - CONFIGPROTO::CONFIGPROTO(PLUGIN_LOGGER & l) : m_settings(NULL), m_admins(NULL), m_tariffs(NULL), m_users(NULL), + m_store(NULL), m_port(0), + m_bindAddress("0.0.0.0"), m_running(false), m_stopped(true), m_logger(l), @@ -83,7 +65,16 @@ CONFIGPROTO::CONFIGPROTO(PLUGIN_LOGGER & l) CONFIGPROTO::~CONFIGPROTO() { - std::for_each(m_conns.begin(), m_conns.end(), RemoveConn()); + { + std::deque::iterator it; + for (it = m_conns.begin(); it != m_conns.end(); ++it) + delete *it; + } + { + BASE_PARSER::REGISTRY::iterator it; + for (it = m_registry.begin(); it != m_registry.end(); ++it) + delete it->second; + } } int CONFIGPROTO::Prepare() @@ -105,11 +96,6 @@ int CONFIGPROTO::Prepare() return -1; } - struct sockaddr_in listenAddr; - listenAddr.sin_family = PF_INET; - listenAddr.sin_port = htons(m_port); - listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0"); // TODO: arbitrary address - int dummy = 1; if (setsockopt(m_listenSocket, SOL_SOCKET, SO_REUSEADDR, &dummy, 4) != 0) @@ -119,12 +105,8 @@ int CONFIGPROTO::Prepare() return -1; } - if (bind(m_listenSocket, reinterpret_cast(&listenAddr), sizeof(listenAddr)) == -1) - { - m_errorStr = std::string("Cannot bind listen socket: '") + strerror(errno) + "'."; - m_logger(m_errorStr); + if (!Bind()) return -1; - } if (listen(m_listenSocket, 64) == -1) // TODO: backlog length { @@ -177,6 +159,7 @@ void CONFIGPROTO::Run() if (res < 0) { m_errorStr = std::string("'select' is failed: '") + strerror(errno) + "'."; + printfd(__FILE__, "%s\n", m_errorStr.c_str()); m_logger(m_errorStr); break; } @@ -190,6 +173,40 @@ void CONFIGPROTO::Run() m_stopped = true; } +bool CONFIGPROTO::Bind() +{ + const hostent * he = gethostbyname(m_bindAddress.c_str()); + if (he == NULL) + { + m_errorStr = "Failed to resolve name '" + m_bindAddress + "': '" + hstrerror(h_errno) + "'."; + printfd(__FILE__, "%s\n", m_errorStr.c_str()); + m_logger(m_errorStr); + return false; + } + + char ** ptr = he->h_addr_list; + while (*ptr != NULL) + { + struct sockaddr_in listenAddr; + listenAddr.sin_family = PF_INET; + listenAddr.sin_port = htons(m_port); + listenAddr.sin_addr.s_addr = *reinterpret_cast(*ptr); + + printfd(__FILE__, "Trying to bind to %s:%d\n", inet_ntostring(listenAddr.sin_addr.s_addr).c_str(), m_port); + + if (bind(m_listenSocket, reinterpret_cast(&listenAddr), sizeof(listenAddr)) == 0) + return true; + + m_errorStr = std::string("Cannot bind listen socket: '") + strerror(errno) + "'."; + printfd(__FILE__, "%s\n", m_errorStr.c_str()); + m_logger(m_errorStr); + + ++ptr; + } + + return false; +} + void CONFIGPROTO::RegisterParsers() { assert(m_settings != NULL); @@ -220,32 +237,38 @@ void CONFIGPROTO::RegisterParsers() SP::SEND_MESSAGE::FACTORY::Register(m_registry, *m_users); SP::AUTH_BY::FACTORY::Register(m_registry, *m_users); - - SP::USER_INFO::FACTORY::Register(m_registry, *m_users); } int CONFIGPROTO::MaxFD() const { int maxFD = m_listenSocket; - for (size_t i = 0; i < m_conns.size(); ++i) - if (maxFD < m_conns[i]->Sock()) - maxFD = m_conns[i]->Sock(); + std::deque::const_iterator it; + for (it = m_conns.begin(); it != m_conns.end(); ++it) + if (maxFD < (*it)->Sock()) + maxFD = (*it)->Sock(); return maxFD; } void CONFIGPROTO::BuildFDSet(fd_set & fds) const { - for (size_t i = 0; i < m_conns.size(); ++i) - FD_SET(m_conns[i]->Sock(), &fds); + FD_ZERO(&fds); + FD_SET(m_listenSocket, &fds); + std::deque::const_iterator it; + for (it = m_conns.begin(); it != m_conns.end(); ++it) + FD_SET((*it)->Sock(), &fds); } void CONFIGPROTO::CleanupConns() { - std::vector::iterator pos; - pos = std::remove_if(m_conns.begin(), m_conns.end(), IsFinished()); - if (pos == m_conns.end()) - return; - std::for_each(pos, m_conns.end(), RemoveConn()); + std::deque::iterator pos; + for (pos = m_conns.begin(); pos != m_conns.end(); ++pos) + if (((*pos)->IsDone() && !(*pos)->IsKeepAlive()) || !(*pos)->IsOk()) + { + delete *pos; + *pos = NULL; + } + + pos = std::remove(m_conns.begin(), m_conns.end(), static_cast(NULL)); m_conns.erase(pos, m_conns.end()); } @@ -255,9 +278,10 @@ void CONFIGPROTO::HandleEvents(const fd_set & fds) AcceptConnection(); else { - for (size_t i = 0; i < m_conns.size(); ++i) - if (FD_ISSET(m_conns[i]->Sock(), &fds)) - m_conns[i]->Read(); + std::deque::iterator it; + for (it = m_conns.begin(); it != m_conns.end(); ++it) + if (FD_ISSET((*it)->Sock(), &fds)) + (*it)->Read(); } } @@ -270,7 +294,7 @@ void CONFIGPROTO::AcceptConnection() if (sock < 0) { m_errorStr = std::string("Failed to accept connection: '") + strerror(errno) + "'."; - printfd(__FILE__, "%s", m_errorStr.c_str()); + printfd(__FILE__, "%s\n", m_errorStr.c_str()); m_logger(m_errorStr); return; } @@ -279,8 +303,8 @@ void CONFIGPROTO::AcceptConnection() try { - m_conns.push_back(new STG::Conn(m_registry, *m_admins, sock, outerAddr)); - printfd(__FILE__, "New connection from %s:%d\n", inet_ntostring(m_conns.back()->IP()).c_str(), m_conns.back()->Port()); + m_conns.push_back(new STG::Conn(m_registry, *m_admins, sock, outerAddr, m_logger)); + 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()); } catch (const STG::Conn::Error & error) { @@ -288,9 +312,3 @@ void CONFIGPROTO::AcceptConnection() m_logger(std::string("Failed to create new client connection: '") + error.what() + "'."); } } -/* -void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip) -{ - m_logger("Admin's connection failed. IP %s", inet_ntostring(ip).c_str()); -} -*/