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.
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.
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
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23 $Date: 2009/02/06 10:25:54 $
27 //---------------------------------------------------------------------------
29 #include <arpa/inet.h>
35 #include "stg/netunit.h"
36 #include "stg/common.h"
38 //---------------------------------------------------------------------------
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!"
53 //---------------------------------------------------------------------------
54 NETTRANSACT::NETTRANSACT()
61 //---------------------------------------------------------------------------
62 int NETTRANSACT::Connect()
66 outerSocket = socket(PF_INET, SOCK_STREAM, 0);
69 errorMsg = CREATE_SOCKET_ERROR;
73 struct sockaddr_in outerAddr;
74 memset(&outerAddr, 0, sizeof(outerAddr));
80 ip = inet_addr(server.c_str());
82 if (ip == INADDR_NONE)
84 phe = gethostbyname(server.c_str());
87 errorMsg = "DNS error.\nCan not reslove " + server;
91 memcpy(&he, phe, sizeof(he));
92 ip = *((long*)he.h_addr_list[0]);
94 outerAddr.sin_family = AF_INET;
95 outerAddr.sin_port = htons(port);
96 outerAddr.sin_addr.s_addr = ip;
98 ret = connect(outerSocket, (struct sockaddr*)&outerAddr, sizeof(outerAddr));
102 errorMsg = CONNECT_FAILED;
108 //---------------------------------------------------------------------------
109 int NETTRANSACT::Disconnect()
114 //---------------------------------------------------------------------------
115 int NETTRANSACT::Transact(const char * data)
118 if ((ret = TxHeader()) != st_ok)
124 if ((ret = RxHeaderAnswer()) != st_ok)
130 if ((ret = TxLogin()) != st_ok)
136 if ((ret = RxLoginAnswer()) != st_ok)
142 if ((ret = TxLoginS()) != st_ok)
148 if ((ret = RxLoginSAnswer()) != st_ok)
154 if ((ret = TxData(data)) != st_ok)
160 if ((ret = RxDataAnswer()) != st_ok)
168 //---------------------------------------------------------------------------
169 int NETTRANSACT::TxHeader()
172 ret = send(outerSocket, STG_HEADER, strlen(STG_HEADER), 0);
175 errorMsg = SEND_HEADER_ERROR;
181 //---------------------------------------------------------------------------
182 int NETTRANSACT::RxHeaderAnswer()
184 char buffer[sizeof(STG_HEADER)+1];
187 ret = recv(outerSocket, buffer, strlen(OK_HEADER), 0);
190 errorMsg = RECV_HEADER_ANSWER_ERROR;
194 if (strncmp(OK_HEADER, buffer, strlen(OK_HEADER)) == 0)
200 if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0)
202 errorMsg = INCORRECT_HEADER;
203 return st_header_err;
207 errorMsg = UNKNOWN_ERROR;
208 return st_unknown_err;
212 //---------------------------------------------------------------------------
213 int NETTRANSACT::TxLogin()
215 char loginZ[ADM_LOGIN_LEN];
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);
224 errorMsg = SEND_LOGIN_ERROR;
230 //---------------------------------------------------------------------------
231 int NETTRANSACT::RxLoginAnswer()
233 char buffer[sizeof(OK_LOGIN)+1];
236 ret = recv(outerSocket, buffer, strlen(OK_LOGIN), 0);
239 errorMsg = RECV_LOGIN_ANSWER_ERROR;
243 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
249 if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
251 errorMsg = INCORRECT_LOGIN;
256 errorMsg = UNKNOWN_ERROR;
257 return st_unknown_err;
261 //---------------------------------------------------------------------------
262 int NETTRANSACT::TxLoginS()
264 char loginZ[ADM_LOGIN_LEN];
265 char ct[ENC_MSG_LEN];
268 memset(loginZ, 0, ADM_LOGIN_LEN);
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)
274 errorMsg = SEND_LOGIN_ERROR;
279 //---------------------------------------------------------------------------
280 int NETTRANSACT::RxLoginSAnswer()
282 char buffer[sizeof(OK_LOGINS)+1];
285 ret = recv(outerSocket, buffer, strlen(OK_LOGINS), 0);
288 errorMsg = RECV_LOGIN_ANSWER_ERROR;
292 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
298 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
300 errorMsg = INCORRECT_LOGIN;
301 return st_logins_err;
305 errorMsg = UNKNOWN_ERROR;
306 return st_unknown_err;
310 //---------------------------------------------------------------------------
311 int NETTRANSACT::TxData(const char * text)
313 char textZ[ENC_MSG_LEN];
314 char ct[ENC_MSG_LEN];
318 int n = strlen(text) / ENC_MSG_LEN;
319 int r = strlen(text) % ENC_MSG_LEN;
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)
327 errorMsg = SEND_DATA_ERROR;
332 //---------------------------------------------------------------------------
333 int NETTRANSACT::TxData(char * data)
335 char buff[ENC_MSG_LEN];
336 char buffS[ENC_MSG_LEN];
337 char passwd[ADM_PASSWD_LEN];
339 memset(passwd, 0, ADM_PASSWD_LEN);
340 strncpy(passwd, password.c_str(), ADM_PASSWD_LEN);
341 memset(buff, 0, ENC_MSG_LEN);
343 int l = strlen(data)/ENC_MSG_LEN;
344 if (strlen(data)%ENC_MSG_LEN)
348 InitContext(passwd, PASSWD_LEN, &ctx);
350 for (int j = 0; j < l; j++)
352 strncpy(buff, &data[j*ENC_MSG_LEN], ENC_MSG_LEN);
353 EncryptBlock(buffS, buff, &ctx);
354 send(outerSocket, buffS, ENC_MSG_LEN, 0);
359 //---------------------------------------------------------------------------
360 int NETTRANSACT::RxDataAnswer()
364 char bufferS[ENC_MSG_LEN];
365 char buffer[ENC_MSG_LEN + 1];
368 InitContext(password.c_str(), PASSWD_LEN, &ctx);
372 ret = recv(outerSocket, &bufferS[n++], 1, 0);
376 errorMsg = RECV_DATA_ANSWER_ERROR;
380 if (n == ENC_MSG_LEN)
383 DecryptBlock(buffer, bufferS, &ctx);
384 buffer[ENC_MSG_LEN] = 0;
386 answerList.push_back(buffer);
388 for (int j = 0; j < ENC_MSG_LEN; j++)
393 if (st_ok != RxCallBack(dataRxCallBack, &answerList))
395 return st_xml_parse_error;
403 //---------------------------------------------------------------------------
404 void NETTRANSACT::SetLogin(const char * l)
408 //---------------------------------------------------------------------------
409 void NETTRANSACT::SetPassword(const char * p)
413 //---------------------------------------------------------------------------
414 void NETTRANSACT::SetServer(const char * serverName)
418 //---------------------------------------------------------------------------
419 void NETTRANSACT::SetServerPort(short unsigned p)
423 //---------------------------------------------------------------------------
424 void NETTRANSACT::SetRxCallback(void * data, RxCallback_t cb)
427 dataRxCallBack = data;
429 //---------------------------------------------------------------------------
430 const std::string & NETTRANSACT::GetError() const
434 //---------------------------------------------------------------------------
435 void NETTRANSACT::Reset()
439 //---------------------------------------------------------------------------