]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/conn.h
Implemented parser registry.
[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 "stg/os_int.h"
27 #include "stg/const.h"
28
29 #include <stdexcept>
30 #include <string>
31
32 #include <expat.h>
33
34 #include <netinet/in.h>
35
36 class SETTINGS;
37 class ADMINS;
38 class USERS;
39 class TARIFFS;
40 class ADMIN;
41 class BASE_PARSER;
42
43 namespace STG
44 {
45
46 class DECRYPT_STREAM;
47
48 class Conn
49 {
50     public:
51         struct Error : public std::runtime_error
52         {
53             Error(const std::string& message) : runtime_error(message.c_str()) {}
54         };
55
56         Conn(const BASE_PARSER::REGISTRY & registry,
57              ADMINS & admins, int sock, const sockaddr_in& addr);
58         ~Conn();
59
60         int Sock() const { return m_sock; }
61         uint32_t IP() const { return *(uint32_t *)(&m_addr.sin_addr); }
62         uint16_t Port() const { return ntohs(m_addr.sin_port); }
63
64         bool Read();
65
66         bool IsOk() const { return m_state != ERROR; }
67         bool IsDone() const { return m_state == DONE; }
68         bool IsKeepAlive() const { return m_keepAlive; }
69
70     private:
71
72         static const char STG_HEADER[5];
73         static const char OK_HEADER[5];
74         static const char ERR_HEADER[5];
75         static const char OK_LOGIN[5];
76         static const char ERR_LOGIN[5];
77         static const char OK_LOGINS[5];
78         static const char ERR_LOGINS[5];
79
80         const BASE_PARSER::REGISTRY & m_registry;
81
82         ADMINS & m_admins;
83
84         ADMIN * m_admin;
85
86         int m_sock;
87         sockaddr_in m_addr;
88         bool m_keepAlive;
89
90         BASE_PARSER * m_parser;
91
92         XML_Parser m_xmlParser;
93
94         enum { HEADER, LOGIN, CRYPTO_LOGIN, DATA, DONE, ERROR } m_state;
95
96         void * m_buffer;
97         size_t m_bufferSize;
98         char m_header[sizeof(STG_HEADER)];
99         char m_login[ADM_LOGIN_LEN + 1];
100         char m_cryptoLogin[ADM_LOGIN_LEN + 1];
101         char m_data[1024];
102         STG::DECRYPT_STREAM * m_stream;
103
104         BASE_PARSER * GetParser(const std::string & tag) const;
105
106         bool HandleBuffer(size_t size);
107
108         bool HandleHeader();
109         bool HandleLogin();
110         bool HandleCryptoLogin();
111         bool HandleData(size_t size);
112
113         bool WriteAnswer(const void* buffer, size_t size);
114         bool WriteResponse();
115
116         struct DataState
117         {
118             DataState(bool f, Conn & c) : final(f), conn(c) {}
119             bool final;
120             Conn & conn;
121         } m_dataState;
122
123         static bool DataCallback(const void * block, size_t size, void * data);
124         static void ParseXMLStart(void * data, const char * el, const char ** attr);
125         static void ParseXMLEnd(void * data, const char * el);
126         static bool WriteCallback(const void * block, size_t size, void * data);
127 };
128
129 }
130
131 #endif