]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/netunit.cpp
Simplified NETTRANSACT.
[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 {
80 }
81 //---------------------------------------------------------------------------
82 int NETTRANSACT::Connect()
83 {
84 outerSocket = socket(PF_INET, SOCK_STREAM, 0);
85 if (outerSocket < 0)
86     {
87     errorMsg = CREATE_SOCKET_ERROR;
88     return st_conn_fail;
89     }
90
91 struct sockaddr_in outerAddr;
92 memset(&outerAddr, 0, sizeof(outerAddr));
93
94 unsigned long ip = inet_addr(server.c_str());
95
96 if (ip == INADDR_NONE)
97     {
98     struct hostent * 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     struct hostent he;
106     memcpy(&he, phe, sizeof(he));
107     ip = *((long *)he.h_addr_list[0]);
108     }
109
110 outerAddr.sin_family = AF_INET;
111 outerAddr.sin_port = htons(port);
112 outerAddr.sin_addr.s_addr = ip;
113
114 if (connect(outerSocket, (struct sockaddr *)&outerAddr, sizeof(outerAddr)) < 0)
115     {
116     errorMsg = CONNECT_FAILED;
117     close(outerSocket);
118     return st_conn_fail;
119     }
120
121 return st_ok;
122 }
123 //---------------------------------------------------------------------------
124 void NETTRANSACT::Disconnect()
125 {
126 close(outerSocket);
127 }
128 //---------------------------------------------------------------------------
129 int NETTRANSACT::Transact(const char * request, CALLBACK callback, void * 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(request)) != st_ok)
169     {
170     Disconnect();
171     return ret;
172     }
173
174 if ((ret = RxDataAnswer(callback, data)) != st_ok)
175     {
176     Disconnect();
177     return ret;
178     }
179
180 return st_ok;
181 }
182 //---------------------------------------------------------------------------
183 int NETTRANSACT::TxHeader()
184 {
185 if (send(outerSocket, STG_HEADER, strlen(STG_HEADER), 0) <= 0)
186     {
187     errorMsg = SEND_HEADER_ERROR;
188     return st_send_fail;
189     }
190
191 return st_ok;
192 }
193 //---------------------------------------------------------------------------
194 int NETTRANSACT::RxHeaderAnswer()
195 {
196 char buffer[sizeof(STG_HEADER) + 1];
197
198 if (recv(outerSocket, buffer, strlen(OK_HEADER), 0) <= 0)
199     {
200     printf("Receive header answer error: '%s'\n", strerror(errno));
201     errorMsg = RECV_HEADER_ANSWER_ERROR;
202     return st_recv_fail;
203     }
204
205 if (strncmp(OK_HEADER, buffer, strlen(OK_HEADER)) == 0)
206     {
207     return st_ok;
208     }
209 else
210     {
211     if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0)
212         {
213         errorMsg = INCORRECT_HEADER;
214         return st_header_err;
215         }
216     else
217         {
218         errorMsg = UNKNOWN_ERROR;
219         return st_unknown_err;
220         }
221     }
222 }
223 //---------------------------------------------------------------------------
224 int NETTRANSACT::TxLogin()
225 {
226 char loginZ[ADM_LOGIN_LEN];
227 memset(loginZ, 0, ADM_LOGIN_LEN);
228 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
229
230 if (send(outerSocket, loginZ, ADM_LOGIN_LEN, 0) <= 0)
231     {
232     errorMsg = SEND_LOGIN_ERROR;
233     return st_send_fail;
234     }
235
236 return st_ok;
237 }
238 //---------------------------------------------------------------------------
239 int NETTRANSACT::RxLoginAnswer()
240 {
241 char buffer[sizeof(OK_LOGIN) + 1];
242
243 if (recv(outerSocket, buffer, strlen(OK_LOGIN), 0) <= 0)
244     {
245     printf("Receive login answer error: '%s'\n", strerror(errno));
246     errorMsg = RECV_LOGIN_ANSWER_ERROR;
247     return st_recv_fail;
248     }
249
250 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
251     {
252     return st_ok;
253     }
254 else
255     {
256     if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
257         {
258         errorMsg = INCORRECT_LOGIN;
259         return st_login_err;
260         }
261     else
262         {
263         errorMsg = UNKNOWN_ERROR;
264         return st_unknown_err;
265         }
266     }
267 }
268 //---------------------------------------------------------------------------
269 int NETTRANSACT::TxLoginS()
270 {
271 char loginZ[ADM_LOGIN_LEN];
272 memset(loginZ, 0, ADM_LOGIN_LEN);
273 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
274
275 BLOWFISH_CTX ctx;
276 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
277
278 for (int j = 0; j < ADM_LOGIN_LEN / ENC_MSG_LEN; j++)
279     {
280     char ct[ENC_MSG_LEN];
281     EncodeString(ct, loginZ + j * ENC_MSG_LEN, &ctx);
282     if (send(outerSocket, ct, ENC_MSG_LEN, 0) <= 0)
283         {
284         errorMsg = SEND_LOGIN_ERROR;
285         return st_send_fail;
286         }
287     }
288
289 return st_ok;
290 }
291 //---------------------------------------------------------------------------
292 int NETTRANSACT::RxLoginSAnswer()
293 {
294 char buffer[sizeof(OK_LOGINS) + 1];
295
296 if (recv(outerSocket, buffer, strlen(OK_LOGINS), 0) <= 0)
297     {
298     printf("Receive secret login answer error: '%s'\n", strerror(errno));
299     errorMsg = RECV_LOGIN_ANSWER_ERROR;
300     return st_recv_fail;
301     }
302
303 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
304     {
305     return st_ok;
306     }
307 else
308     {
309     if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
310         {
311         errorMsg = INCORRECT_LOGIN;
312         return st_logins_err;
313         }
314     else
315         {
316         errorMsg = UNKNOWN_ERROR;
317         return st_unknown_err;
318         }
319     }
320 }
321 //---------------------------------------------------------------------------
322 int NETTRANSACT::TxData(const char * text)
323 {
324 int n = strlen(text) / ENC_MSG_LEN;
325 int r = strlen(text) % ENC_MSG_LEN;
326
327 BLOWFISH_CTX ctx;
328 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
329
330 char textZ[ENC_MSG_LEN];
331 char ct[ENC_MSG_LEN];
332
333 for (int j = 0; j < n; j++)
334     {
335     strncpy(textZ, text + j * ENC_MSG_LEN, ENC_MSG_LEN);
336     EncodeString(ct, textZ, &ctx);
337     if (send(outerSocket, ct, ENC_MSG_LEN, 0) <= 0)
338         {
339         errorMsg = SEND_DATA_ERROR;
340         return st_send_fail;
341         }
342     }
343
344 memset(textZ, 0, ENC_MSG_LEN);
345
346 if (r)
347     strncpy(textZ, text + n * ENC_MSG_LEN, ENC_MSG_LEN);
348
349 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
350
351 EncodeString(ct, textZ, &ctx);
352 if (send(outerSocket, ct, ENC_MSG_LEN, 0) <= 0)
353     {
354     errorMsg = SEND_DATA_ERROR;
355     return st_send_fail;
356     }
357
358 return st_ok;
359 }
360 //---------------------------------------------------------------------------
361 int NETTRANSACT::TxData(char * data)
362 {
363 char passwd[ADM_PASSWD_LEN];
364 memset(passwd, 0, ADM_PASSWD_LEN);
365 strncpy(passwd, password.c_str(), ADM_PASSWD_LEN);
366
367 char buff[ENC_MSG_LEN];
368 memset(buff, 0, ENC_MSG_LEN);
369
370 int l = strlen(data) / ENC_MSG_LEN;
371 if (strlen(data) % ENC_MSG_LEN)
372     l++;
373
374 BLOWFISH_CTX ctx;
375 EnDecodeInit(passwd, PASSWD_LEN, &ctx);
376
377 for (int j = 0; j < l; j++)
378     {
379     strncpy(buff, &data[j * ENC_MSG_LEN], ENC_MSG_LEN);
380     char buffS[ENC_MSG_LEN];
381     EncodeString(buffS, buff, &ctx);
382     send(outerSocket, buffS, ENC_MSG_LEN, 0);
383     }
384
385 return 0;
386 }
387 //---------------------------------------------------------------------------
388 int NETTRANSACT::RxDataAnswer(CALLBACK callback, void * data)
389 {
390 BLOWFISH_CTX ctx;
391 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
392
393 std::string chunk;
394 while (true)
395     {
396     char bufferS[ENC_MSG_LEN];
397     size_t toRead = ENC_MSG_LEN;
398     while (toRead > 0)
399         {
400         int ret = recv(outerSocket, &bufferS[ENC_MSG_LEN - toRead], toRead, 0);
401         if (ret <= 0)
402             {
403             printf("Receive data error: '%s'\n", strerror(errno));
404             close(outerSocket);
405             errorMsg = RECV_DATA_ANSWER_ERROR;
406             return st_recv_fail;
407             }
408         toRead -= ret;
409         }
410
411     char buffer[ENC_MSG_LEN];
412     DecodeString(buffer, bufferS, &ctx);
413
414     bool final = false;
415     size_t pos = 0;
416     for (; pos < ENC_MSG_LEN && buffer[pos] != 0; pos++) ;
417     if (pos < ENC_MSG_LEN && buffer[pos] == 0)
418         final = true;
419
420     if (pos > 0)
421         chunk.append(&buffer[0], &buffer[pos]);
422
423     if (chunk.length() > MAX_XML_CHUNK_LENGTH || final)
424         {
425         if (callback)
426             if (!callback(chunk, final, data))
427                 return st_xml_parse_error;
428         chunk.clear();
429         }
430
431     if (final)
432         return st_ok;
433     }
434 }