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 : Maxim Mamontov <faust@stargazer.dp.ua>
25 #include "dumphelpers.h"
27 #include "stg/const.h"
35 #include <netinet/in.h>
52 struct Error : public std::runtime_error
54 explicit Error(const std::string& message) : runtime_error(message.c_str()) {}
57 Conn(const BASE_PARSER::REGISTRY & registry,
58 Admins & admins, int sock, const sockaddr_in& addr,
59 PluginLogger & logger);
62 int Sock() const { return m_sock; }
63 uint32_t IP() const { return *reinterpret_cast<const uint32_t *>(&m_addr.sin_addr); }
64 uint16_t Port() const { return ntohs(m_addr.sin_port); }
66 std::string endpoint() const { return inet_ntostring(IP()) + ":" + std::to_string(Port()); }
70 bool IsOk() const { return m_state != ERROR; }
71 bool IsDone() const { return m_state == DONE; }
72 bool IsKeepAlive() const { return m_keepAlive; }
74 void SetKeepAlive() { m_keepAlive = true; }
78 static const char STG_HEADER[5];
79 static const char OK_HEADER[5];
80 static const char ERR_HEADER[5];
81 static const char OK_LOGIN[5];
82 static const char ERR_LOGIN[5];
83 static const char OK_LOGINS[5];
84 static const char ERR_LOGINS[5];
86 const BASE_PARSER::REGISTRY & m_registry;
96 BASE_PARSER * m_parser;
98 XML_Parser m_xmlParser;
100 enum { HEADER, LOGIN, CRYPTO_LOGIN, DATA, DONE, ERROR } m_state;
104 char m_header[sizeof(STG_HEADER) - 1]; // Without \0
105 char m_login[ADM_LOGIN_LEN]; // Without \0
106 char m_cryptoLogin[ADM_LOGIN_LEN]; // Without \0
108 STG::DECRYPT_STREAM * m_stream;
109 PluginLogger & m_logger;
111 BASE_PARSER * GetParser(const std::string & tag) const;
113 bool HandleBuffer(size_t size);
117 bool HandleCryptoLogin();
118 bool HandleData(size_t size);
120 bool WriteAnswer(const void* buffer, size_t size);
121 bool WriteResponse();
123 void Log(const char * file, const std::string & message);
127 DataState(bool f, Conn & c) : final(f), conn(c) {}
136 static bool DataCallback(const void * block, size_t size, void * data);
137 static void ParseXMLStart(void * data, const char * el, const char ** attr);
138 static void ParseXMLEnd(void * data, const char * el);
139 static bool WriteCallback(const void * block, size_t size, void * data);