]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/conn.h
Various fixes of issues reported by static analyzers.
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / conn.h
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #ifndef __STG_SGCONFIG_CONN_H__
22 #define __STG_SGCONFIG_CONN_H__
23
24 #include "parser.h"
25
26 #include "dumphelpers.h"
27
28 #include "stg/os_int.h"
29 #include "stg/const.h"
30
31 #include <stdexcept>
32 #include <string>
33
34 #include <expat.h>
35
36 #include <netinet/in.h>
37
38 class SETTINGS;
39 class ADMINS;
40 class USERS;
41 class TARIFFS;
42 class ADMIN;
43 class BASE_PARSER;
44 class PLUGIN_LOGGER;
45
46 namespace STG
47 {
48
49 class DECRYPT_STREAM;
50
51 class Conn
52 {
53     public:
54         struct Error : public std::runtime_error
55         {
56             explicit Error(const std::string& message) : runtime_error(message.c_str()) {}
57         };
58
59         Conn(const BASE_PARSER::REGISTRY & registry,
60              ADMINS & admins, int sock, const sockaddr_in& addr,
61              PLUGIN_LOGGER & logger);
62         ~Conn();
63
64         int Sock() const { return m_sock; }
65         uint32_t IP() const { return *(uint32_t *)(&m_addr.sin_addr); }
66         uint16_t Port() const { return ntohs(m_addr.sin_port); }
67
68         std::string endpoint() const { return inet_ntostring(IP()) + ":" + x2str(Port()); }
69
70         bool Read();
71
72         bool IsOk() const { return m_state != ERROR; }
73         bool IsDone() const { return m_state == DONE; }
74         bool IsKeepAlive() const { return m_keepAlive; }
75
76         void SetKeepAlive() { m_keepAlive = true; }
77
78     private:
79
80         static const char STG_HEADER[5];
81         static const char OK_HEADER[5];
82         static const char ERR_HEADER[5];
83         static const char OK_LOGIN[5];
84         static const char ERR_LOGIN[5];
85         static const char OK_LOGINS[5];
86         static const char ERR_LOGINS[5];
87
88         const BASE_PARSER::REGISTRY & m_registry;
89
90         ADMINS & m_admins;
91
92         ADMIN * m_admin;
93
94         int m_sock;
95         sockaddr_in m_addr;
96         bool m_keepAlive;
97
98         BASE_PARSER * m_parser;
99
100         XML_Parser m_xmlParser;
101
102         enum { HEADER, LOGIN, CRYPTO_LOGIN, DATA, DONE, ERROR } m_state;
103
104         void * m_buffer;
105         size_t m_bufferSize;
106         char m_header[sizeof(STG_HEADER) - 1]; // Without \0
107         char m_login[ADM_LOGIN_LEN]; // Without \0
108         char m_cryptoLogin[ADM_LOGIN_LEN]; // Without \0
109         char m_data[1024];
110         STG::DECRYPT_STREAM * m_stream;
111         PLUGIN_LOGGER &  m_logger;
112
113         BASE_PARSER * GetParser(const std::string & tag) const;
114
115         bool HandleBuffer(size_t size);
116
117         bool HandleHeader();
118         bool HandleLogin();
119         bool HandleCryptoLogin();
120         bool HandleData(size_t size);
121
122         bool WriteAnswer(const void* buffer, size_t size);
123         bool WriteResponse();
124
125         void Log(const char * file, const std::string & message);
126
127         struct DataState
128         {
129             DataState(bool f, Conn & c) : final(f), conn(c) {}
130             bool final;
131             Conn & conn;
132         } m_dataState;
133
134 #ifdef DUMPCRYPTO
135         Dumper m_dumper;
136 #endif
137
138         static bool DataCallback(const void * block, size_t size, void * data);
139         static void ParseXMLStart(void * data, const char * el, const char ** attr);
140         static void ParseXMLEnd(void * data, const char * el);
141         static bool WriteCallback(const void * block, size_t size, void * data);
142 };
143
144 }
145
146 #endif