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