]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/netunit.cpp
df0af7f2c446512332ff4af3f353ea6b9429b831
[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
266 memset(loginZ, 0, ADM_LOGIN_LEN);
267 BLOWFISH_CTX ctx;
268 InitContext(password.c_str(), PASSWD_LEN, &ctx);
269 EncryptString(loginZ, login.c_str(), std::min<int>(login.length() + 1, ADM_LOGIN_LEN), &ctx);
270 if (send(outerSocket, loginZ, ADM_LOGIN_LEN, 0) <= 0)
271     {
272     errorMsg = SEND_LOGIN_ERROR;
273     return st_send_fail;
274     }
275 return st_ok;
276 }
277 //---------------------------------------------------------------------------
278 int NETTRANSACT::RxLoginSAnswer()
279 {
280 char buffer[sizeof(OK_LOGINS)+1];
281 int ret;
282
283 ret = recv(outerSocket, buffer, strlen(OK_LOGINS), 0);
284 if (ret <= 0)
285     {
286     errorMsg = RECV_LOGIN_ANSWER_ERROR;
287     return st_recv_fail;
288     }
289
290 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
291     {
292     return st_ok;
293     }
294 else
295     {
296     if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
297         {
298         errorMsg = INCORRECT_LOGIN;
299         return st_logins_err;
300         }
301     else
302         {
303         errorMsg = UNKNOWN_ERROR;
304         return st_unknown_err;
305         }
306     }
307 }
308 //---------------------------------------------------------------------------
309 int NETTRANSACT::TxData(const char * text)
310 {
311 BLOWFISH_CTX ctx;
312 InitContext(password.c_str(), PASSWD_LEN, &ctx);
313 size_t length = strlen(text);
314 char buffer[length + 9];
315 EncryptString(buffer, text, length, &ctx);
316 if (send(outerSocket, buffer, sizeof(buffer), 0) <= 0)
317     {
318     errorMsg = SEND_DATA_ERROR;
319     return st_send_fail;
320     }
321 return st_ok;
322 }
323 //---------------------------------------------------------------------------
324 int NETTRANSACT::RxDataAnswer()
325 {
326 int n = 0;
327 int ret;
328 char bufferS[ENC_MSG_LEN];
329 char buffer[ENC_MSG_LEN + 1];
330
331 BLOWFISH_CTX ctx;
332 InitContext(password.c_str(), PASSWD_LEN, &ctx);
333
334 while (1)
335     {
336     ret = recv(outerSocket, &bufferS[n++], 1, 0);
337     if (ret <= 0)
338         {
339         close(outerSocket);
340         errorMsg = RECV_DATA_ANSWER_ERROR;
341         return st_recv_fail;
342         }
343
344     if (n == ENC_MSG_LEN)
345         {
346         n = 0;
347         DecryptBlock(buffer, bufferS, &ctx);
348         buffer[ENC_MSG_LEN] = 0;
349
350         answerList.push_back(buffer);
351
352         for (int j = 0; j < ENC_MSG_LEN; j++)
353             {
354             if (buffer[j] == 0)
355                 {
356                 if (RxCallBack)
357                     if (st_ok != RxCallBack(dataRxCallBack, &answerList))
358                         {
359                         return st_xml_parse_error;
360                         }
361                 return st_ok;
362                 }
363             }
364         }
365     }
366 }
367 //---------------------------------------------------------------------------
368 void NETTRANSACT::SetLogin(const char * l)
369 {
370 login = l;
371 }
372 //---------------------------------------------------------------------------
373 void NETTRANSACT::SetPassword(const char * p)
374 {
375 password = p;
376 }
377 //---------------------------------------------------------------------------
378 void NETTRANSACT::SetServer(const char * serverName)
379 {
380 server = serverName;
381 }
382 //---------------------------------------------------------------------------
383 void NETTRANSACT::SetServerPort(short unsigned p)
384 {
385 port = p;
386 }
387 //---------------------------------------------------------------------------
388 void NETTRANSACT::SetRxCallback(void * data, RxCallback_t cb)
389 {
390 RxCallBack = cb;
391 dataRxCallBack = data;
392 }
393 //---------------------------------------------------------------------------
394 const std::string & NETTRANSACT::GetError() const
395 {
396 return errorMsg;
397 }
398 //---------------------------------------------------------------------------
399 void NETTRANSACT::Reset()
400 {
401 answerList.clear();
402 }
403 //---------------------------------------------------------------------------