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