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