]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/netunit.cpp
Search parameter in map case insensitive
[stg.git] / stglibs / srvconf.lib / netunit.cpp
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 : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21 #include "netunit.h"
22
23 #include "stg/servconf_types.h"
24 #include "stg/common.h"
25 #include "stg/blowfish.h"
26
27 #include <algorithm> // std::min
28
29 #include <cstdio>
30 #include <cerrno>
31 #include <cstring>
32
33 #include <netdb.h>
34 #include <arpa/inet.h>
35 #include <unistd.h>
36
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40
41 using namespace STG;
42
43 namespace
44 {
45
46 const std::string::size_type MAX_XML_CHUNK_LENGTH = 2048;
47
48 }
49
50 //---------------------------------------------------------------------------
51
52 #define SEND_DATA_ERROR             "Send data error!"
53 #define RECV_DATA_ANSWER_ERROR      "Recv data answer error!"
54 #define UNKNOWN_ERROR               "Unknown error!"
55 #define CONNECT_FAILED              "Connect failed!"
56 #define INCORRECT_LOGIN             "Incorrect login!"
57 #define INCORRECT_HEADER            "Incorrect header!"
58 #define SEND_LOGIN_ERROR            "Send login error!"
59 #define RECV_LOGIN_ANSWER_ERROR     "Recv login answer error!"
60 #define CREATE_SOCKET_ERROR         "Create socket failed!"
61 #define WSASTARTUP_FAILED           "WSAStartup failed!"
62 #define SEND_HEADER_ERROR           "Send header error!"
63 #define RECV_HEADER_ANSWER_ERROR    "Recv header answer error!"
64
65 //---------------------------------------------------------------------------
66 NETTRANSACT::NETTRANSACT(const std::string & s, uint16_t p,
67                          const std::string & l, const std::string & pwd)
68     : server(s),
69       port(p),
70       login(l),
71       password(pwd),
72       outerSocket(-1)
73 {
74 }
75 //---------------------------------------------------------------------------
76 int NETTRANSACT::Connect()
77 {
78 outerSocket = socket(PF_INET, SOCK_STREAM, 0);
79 if (outerSocket < 0)
80     {
81     errorMsg = CREATE_SOCKET_ERROR;
82     return st_conn_fail;
83     }
84
85 struct sockaddr_in outerAddr;
86 memset(&outerAddr, 0, sizeof(outerAddr));
87
88 unsigned long ip = inet_addr(server.c_str());
89
90 if (ip == INADDR_NONE)
91     {
92     struct hostent * phe = gethostbyname(server.c_str());
93     if (phe == NULL)
94         {
95         errorMsg = "DNS error.\nCan not reslove " + server;
96         return st_dns_err;
97         }
98
99     struct hostent he;
100     memcpy(&he, phe, sizeof(he));
101     ip = *((long *)he.h_addr_list[0]);
102     }
103
104 outerAddr.sin_family = AF_INET;
105 outerAddr.sin_port = htons(port);
106 outerAddr.sin_addr.s_addr = ip;
107
108 if (connect(outerSocket, (struct sockaddr *)&outerAddr, sizeof(outerAddr)) < 0)
109     {
110     errorMsg = CONNECT_FAILED;
111     close(outerSocket);
112     return st_conn_fail;
113     }
114
115 return st_ok;
116 }
117 //---------------------------------------------------------------------------
118 void NETTRANSACT::Disconnect()
119 {
120 close(outerSocket);
121 }
122 //---------------------------------------------------------------------------
123 int NETTRANSACT::Transact(const std::string & request, CALLBACK callback, void * data)
124 {
125 int ret;
126 if ((ret = TxHeader()) != st_ok)
127     {
128     Disconnect();
129     return ret;
130     }
131
132 if ((ret = RxHeaderAnswer()) != st_ok)
133     {
134     Disconnect();
135     return ret;
136     }
137
138 if ((ret = TxLogin()) != st_ok)
139     {
140     Disconnect();
141     return ret;
142     }
143
144 if ((ret = RxLoginAnswer()) != st_ok)
145     {
146     Disconnect();
147     return ret;
148     }
149
150 if ((ret = TxLoginS()) != st_ok)
151     {
152     Disconnect();
153     return ret;
154     }
155
156 if ((ret = RxLoginSAnswer()) != st_ok)
157     {
158     Disconnect();
159     return ret;
160     }
161
162 if ((ret = TxData(request)) != st_ok)
163     {
164     Disconnect();
165     return ret;
166     }
167
168 if ((ret = RxDataAnswer(callback, data)) != st_ok)
169     {
170     Disconnect();
171     return ret;
172     }
173
174 return st_ok;
175 }
176 //---------------------------------------------------------------------------
177 int NETTRANSACT::TxHeader()
178 {
179 if (send(outerSocket, STG_HEADER, strlen(STG_HEADER), 0) <= 0)
180     {
181     errorMsg = SEND_HEADER_ERROR;
182     return st_send_fail;
183     }
184
185 return st_ok;
186 }
187 //---------------------------------------------------------------------------
188 int NETTRANSACT::RxHeaderAnswer()
189 {
190 char buffer[sizeof(STG_HEADER) + 1];
191
192 if (recv(outerSocket, buffer, strlen(OK_HEADER), 0) <= 0)
193     {
194     printf("Receive header answer error: '%s'\n", strerror(errno));
195     errorMsg = RECV_HEADER_ANSWER_ERROR;
196     return st_recv_fail;
197     }
198
199 if (strncmp(OK_HEADER, buffer, strlen(OK_HEADER)) == 0)
200     {
201     return st_ok;
202     }
203 else
204     {
205     if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0)
206         {
207         errorMsg = INCORRECT_HEADER;
208         return st_header_err;
209         }
210     else
211         {
212         errorMsg = UNKNOWN_ERROR;
213         return st_unknown_err;
214         }
215     }
216 }
217 //---------------------------------------------------------------------------
218 int NETTRANSACT::TxLogin()
219 {
220 char loginZ[ADM_LOGIN_LEN];
221 memset(loginZ, 0, ADM_LOGIN_LEN);
222 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
223
224 if (send(outerSocket, loginZ, ADM_LOGIN_LEN, 0) <= 0)
225     {
226     errorMsg = SEND_LOGIN_ERROR;
227     return st_send_fail;
228     }
229
230 return st_ok;
231 }
232 //---------------------------------------------------------------------------
233 int NETTRANSACT::RxLoginAnswer()
234 {
235 char buffer[sizeof(OK_LOGIN) + 1];
236
237 if (recv(outerSocket, buffer, strlen(OK_LOGIN), 0) <= 0)
238     {
239     printf("Receive login answer error: '%s'\n", strerror(errno));
240     errorMsg = RECV_LOGIN_ANSWER_ERROR;
241     return st_recv_fail;
242     }
243
244 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
245     {
246     return st_ok;
247     }
248 else
249     {
250     if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
251         {
252         errorMsg = INCORRECT_LOGIN;
253         return st_login_err;
254         }
255     else
256         {
257         errorMsg = UNKNOWN_ERROR;
258         return st_unknown_err;
259         }
260     }
261 }
262 //---------------------------------------------------------------------------
263 int NETTRANSACT::TxLoginS()
264 {
265 char loginZ[ADM_LOGIN_LEN];
266 memset(loginZ, 0, ADM_LOGIN_LEN);
267 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
268
269 BLOWFISH_CTX ctx;
270 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
271
272 for (int j = 0; j < ADM_LOGIN_LEN / ENC_MSG_LEN; j++)
273     {
274     char ct[ENC_MSG_LEN];
275     EncodeString(ct, loginZ + j * ENC_MSG_LEN, &ctx);
276     if (send(outerSocket, ct, ENC_MSG_LEN, 0) <= 0)
277         {
278         errorMsg = SEND_LOGIN_ERROR;
279         return st_send_fail;
280         }
281     }
282
283 return st_ok;
284 }
285 //---------------------------------------------------------------------------
286 int NETTRANSACT::RxLoginSAnswer()
287 {
288 char buffer[sizeof(OK_LOGINS) + 1];
289
290 if (recv(outerSocket, buffer, strlen(OK_LOGINS), 0) <= 0)
291     {
292     printf("Receive secret login answer error: '%s'\n", strerror(errno));
293     errorMsg = RECV_LOGIN_ANSWER_ERROR;
294     return st_recv_fail;
295     }
296
297 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
298     {
299     return st_ok;
300     }
301 else
302     {
303     if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
304         {
305         errorMsg = INCORRECT_LOGIN;
306         return st_logins_err;
307         }
308     else
309         {
310         errorMsg = UNKNOWN_ERROR;
311         return st_unknown_err;
312         }
313     }
314 }
315 //---------------------------------------------------------------------------
316 int NETTRANSACT::TxData(const std::string & text)
317 {
318 BLOWFISH_CTX ctx;
319 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
320
321 size_t pos = 0;
322 while (pos < text.size())
323     {
324     char textZ[ENC_MSG_LEN];
325     if (text.size() - pos < ENC_MSG_LEN)
326         memset(textZ, 0, ENC_MSG_LEN);
327     strncpy(textZ, text.c_str() + pos, std::min(ENC_MSG_LEN, (int)(text.size() - pos)));
328     char ct[ENC_MSG_LEN];
329     EncodeString(ct, textZ, &ctx);
330     if (send(outerSocket, ct, ENC_MSG_LEN, 0) <= 0)
331         {
332         errorMsg = SEND_DATA_ERROR;
333         return st_send_fail;
334         }
335     pos += ENC_MSG_LEN;
336     }
337
338 return st_ok;
339 }
340 //---------------------------------------------------------------------------
341 int NETTRANSACT::RxDataAnswer(CALLBACK callback, void * data)
342 {
343 BLOWFISH_CTX ctx;
344 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
345
346 std::string chunk;
347 while (true)
348     {
349     char bufferS[ENC_MSG_LEN];
350     size_t toRead = ENC_MSG_LEN;
351     while (toRead > 0)
352         {
353         int ret = recv(outerSocket, &bufferS[ENC_MSG_LEN - toRead], toRead, 0);
354         if (ret <= 0)
355             {
356             printf("Receive data error: '%s'\n", strerror(errno));
357             close(outerSocket);
358             errorMsg = RECV_DATA_ANSWER_ERROR;
359             return st_recv_fail;
360             }
361         toRead -= ret;
362         }
363
364     char buffer[ENC_MSG_LEN];
365     DecodeString(buffer, bufferS, &ctx);
366
367     bool final = false;
368     size_t pos = 0;
369     for (; pos < ENC_MSG_LEN && buffer[pos] != 0; pos++) ;
370     if (pos < ENC_MSG_LEN && buffer[pos] == 0)
371         final = true;
372
373     if (pos > 0)
374         chunk.append(&buffer[0], &buffer[pos]);
375
376     if (chunk.length() > MAX_XML_CHUNK_LENGTH || final)
377         {
378         if (callback)
379             if (!callback(chunk, final, data))
380                 return st_xml_parse_error;
381         chunk.clear();
382         }
383
384     if (final)
385         return st_ok;
386     }
387 }