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