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 void NETTRANSACT::EnDecryptInit(const char * passwd, int, BLOWFISH_CTX *ctx)
64 unsigned char * keyL = NULL;
66 keyL = new unsigned char[PASSWD_LEN];
68 memset(keyL, 0, PASSWD_LEN);
70 strncpy((char *)keyL, passwd, PASSWD_LEN);
72 Blowfish_Init(ctx, keyL, PASSWD_LEN);
76 //-----------------------------------------------------------------------------
77 void NETTRANSACT::Encrypt(char * d, const char * s, BLOWFISH_CTX *ctx)
79 EncodeString(d, s, ctx);
81 //---------------------------------------------------------------------------
82 void NETTRANSACT::Decrypt(char * d, const char * s, BLOWFISH_CTX *ctx)
84 DecodeString(d, s, ctx);
86 //---------------------------------------------------------------------------
87 int NETTRANSACT::Connect()
91 outerSocket = socket(PF_INET, SOCK_STREAM, 0);
94 errorMsg = CREATE_SOCKET_ERROR;
98 struct sockaddr_in outerAddr;
99 memset(&outerAddr, 0, sizeof(outerAddr));
102 struct hostent * phe;
105 ip = inet_addr(server.c_str());
107 if (ip == INADDR_NONE)
109 phe = gethostbyname(server.c_str());
112 errorMsg = "DNS error.\nCan not reslove " + server;
116 memcpy(&he, phe, sizeof(he));
117 ip = *((long*)he.h_addr_list[0]);
119 outerAddr.sin_family = AF_INET;
120 outerAddr.sin_port = htons(port);
121 outerAddr.sin_addr.s_addr = ip;
123 ret = connect(outerSocket, (struct sockaddr*)&outerAddr, sizeof(outerAddr));
127 errorMsg = CONNECT_FAILED;
133 //---------------------------------------------------------------------------
134 int NETTRANSACT::Disconnect()
139 //---------------------------------------------------------------------------
140 int NETTRANSACT::Transact(const char * data)
143 if ((ret = TxHeader()) != st_ok)
149 if ((ret = RxHeaderAnswer()) != st_ok)
155 if ((ret = TxLogin()) != st_ok)
161 if ((ret = RxLoginAnswer()) != st_ok)
167 if ((ret = TxLoginS()) != st_ok)
173 if ((ret = RxLoginSAnswer()) != st_ok)
179 if ((ret = TxData(data)) != st_ok)
185 if ((ret = RxDataAnswer()) != st_ok)
193 //---------------------------------------------------------------------------
194 int NETTRANSACT::TxHeader()
197 ret = send(outerSocket, STG_HEADER, strlen(STG_HEADER), 0);
200 errorMsg = SEND_HEADER_ERROR;
206 //---------------------------------------------------------------------------
207 int NETTRANSACT::RxHeaderAnswer()
209 char buffer[sizeof(STG_HEADER)+1];
212 ret = recv(outerSocket, buffer, strlen(OK_HEADER), 0);
215 errorMsg = RECV_HEADER_ANSWER_ERROR;
219 if (strncmp(OK_HEADER, buffer, strlen(OK_HEADER)) == 0)
225 if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0)
227 errorMsg = INCORRECT_HEADER;
228 return st_header_err;
232 errorMsg = UNKNOWN_ERROR;
233 return st_unknown_err;
237 //---------------------------------------------------------------------------
238 int NETTRANSACT::TxLogin()
240 char loginZ[ADM_LOGIN_LEN];
243 memset(loginZ, 0, ADM_LOGIN_LEN);
244 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
245 ret = send(outerSocket, loginZ, ADM_LOGIN_LEN, 0);
249 errorMsg = SEND_LOGIN_ERROR;
255 //---------------------------------------------------------------------------
256 int NETTRANSACT::RxLoginAnswer()
258 char buffer[sizeof(OK_LOGIN)+1];
261 ret = recv(outerSocket, buffer, strlen(OK_LOGIN), 0);
264 errorMsg = RECV_LOGIN_ANSWER_ERROR;
268 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
274 if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
276 errorMsg = INCORRECT_LOGIN;
281 errorMsg = UNKNOWN_ERROR;
282 return st_unknown_err;
286 //---------------------------------------------------------------------------
287 int NETTRANSACT::TxLoginS()
289 char loginZ[ADM_LOGIN_LEN];
290 char ct[ENC_MSG_LEN];
293 memset(loginZ, 0, ADM_LOGIN_LEN);
295 InitContext(password.c_str(), PASSWD_LEN, &ctx);
296 EncryptString(loginZ, login.c_str(), std::min(login.length(), ADM_LOGIN_LEN), &ctx);
297 if (send(outerSocket, loginZ, ADM_LOGIN_LEN, 0) <= 0)
299 errorMsg = SEND_LOGIN_ERROR;
304 //---------------------------------------------------------------------------
305 int NETTRANSACT::RxLoginSAnswer()
307 char buffer[sizeof(OK_LOGINS)+1];
310 ret = recv(outerSocket, buffer, strlen(OK_LOGINS), 0);
313 errorMsg = RECV_LOGIN_ANSWER_ERROR;
317 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
323 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
325 errorMsg = INCORRECT_LOGIN;
326 return st_logins_err;
330 errorMsg = UNKNOWN_ERROR;
331 return st_unknown_err;
335 //---------------------------------------------------------------------------
336 int NETTRANSACT::TxData(const char * text)
338 char textZ[ENC_MSG_LEN];
339 char ct[ENC_MSG_LEN];
343 int n = strlen(text) / ENC_MSG_LEN;
344 int r = strlen(text) % ENC_MSG_LEN;
347 InitContext(password.c_str(), PASSWD_LEN, &ctx);
348 char buffer[text.length()];
349 EncryptString(buffer, text.c_str(), text.length(), &ctx);
350 if (send(outerSocket, buffer, text.length(), 0) <= 0)
352 errorMsg = SEND_DATA_ERROR;
357 //---------------------------------------------------------------------------
358 int NETTRANSACT::TxData(char * data)
360 char buff[ENC_MSG_LEN];
361 char buffS[ENC_MSG_LEN];
362 char passwd[ADM_PASSWD_LEN];
364 memset(passwd, 0, ADM_PASSWD_LEN);
365 strncpy(passwd, password.c_str(), ADM_PASSWD_LEN);
366 memset(buff, 0, ENC_MSG_LEN);
368 int l = strlen(data)/ENC_MSG_LEN;
369 if (strlen(data)%ENC_MSG_LEN)
373 InitContext(password.c_str(), PASSWD_LEN, &ctx);
375 for (int j = 0; j < l; j++)
377 strncpy(buff, &data[j*ENC_MSG_LEN], ENC_MSG_LEN);
378 Encrypt(buffS, buff, &ctx);
379 send(outerSocket, buffS, ENC_MSG_LEN, 0);
384 //---------------------------------------------------------------------------
385 int NETTRANSACT::RxDataAnswer()
389 char bufferS[ENC_MSG_LEN];
390 char buffer[ENC_MSG_LEN + 1];
393 EnDecryptInit(password.c_str(), PASSWD_LEN, &ctx);
397 ret = recv(outerSocket, &bufferS[n++], 1, 0);
401 errorMsg = RECV_DATA_ANSWER_ERROR;
405 if (n == ENC_MSG_LEN)
408 Decrypt(buffer, bufferS, &ctx);
409 buffer[ENC_MSG_LEN] = 0;
411 answerList.push_back(buffer);
413 for (int j = 0; j < ENC_MSG_LEN; j++)
418 if (st_ok != RxCallBack(dataRxCallBack, &answerList))
420 return st_xml_parse_error;
428 //---------------------------------------------------------------------------
429 void NETTRANSACT::SetLogin(const char * l)
433 //---------------------------------------------------------------------------
434 void NETTRANSACT::SetPassword(const char * p)
438 //---------------------------------------------------------------------------
439 void NETTRANSACT::SetServer(const char * serverName)
443 //---------------------------------------------------------------------------
444 void NETTRANSACT::SetServerPort(short unsigned p)
448 //---------------------------------------------------------------------------
449 void NETTRANSACT::SetRxCallback(void * data, RxCallback_t cb)
452 dataRxCallBack = data;
454 //---------------------------------------------------------------------------
455 const std::string & NETTRANSACT::GetError() const
459 //---------------------------------------------------------------------------
460 void NETTRANSACT::Reset()
464 //---------------------------------------------------------------------------