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);
294 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
297 EnDecryptInit(password.c_str(), PASSWD_LEN, &ctx);
299 for (int j = 0; j < ADM_LOGIN_LEN / ENC_MSG_LEN; j++)
301 Encrypt(ct, loginZ + j*ENC_MSG_LEN, &ctx);
302 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
305 errorMsg = SEND_LOGIN_ERROR;
312 //---------------------------------------------------------------------------
313 int NETTRANSACT::RxLoginSAnswer()
315 char buffer[sizeof(OK_LOGINS)+1];
318 ret = recv(outerSocket, buffer, strlen(OK_LOGINS), 0);
321 errorMsg = RECV_LOGIN_ANSWER_ERROR;
325 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
331 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
333 errorMsg = INCORRECT_LOGIN;
334 return st_logins_err;
338 errorMsg = UNKNOWN_ERROR;
339 return st_unknown_err;
343 //---------------------------------------------------------------------------
344 int NETTRANSACT::TxData(const char * text)
346 char textZ[ENC_MSG_LEN];
347 char ct[ENC_MSG_LEN];
351 int n = strlen(text) / ENC_MSG_LEN;
352 int r = strlen(text) % ENC_MSG_LEN;
355 EnDecryptInit(password.c_str(), PASSWD_LEN, &ctx);
357 for (j = 0; j < n; j++)
359 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
360 Encrypt(ct, textZ, &ctx);
361 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
364 errorMsg = SEND_DATA_ERROR;
369 memset(textZ, 0, ENC_MSG_LEN);
371 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
373 EnDecryptInit(password.c_str(), PASSWD_LEN, &ctx);
375 Encrypt(ct, textZ, &ctx);
376 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
379 errorMsg = SEND_DATA_ERROR;
385 //---------------------------------------------------------------------------
386 int NETTRANSACT::TxData(char * data)
388 char buff[ENC_MSG_LEN];
389 char buffS[ENC_MSG_LEN];
390 char passwd[ADM_PASSWD_LEN];
392 memset(passwd, 0, ADM_PASSWD_LEN);
393 strncpy(passwd, password.c_str(), ADM_PASSWD_LEN);
394 memset(buff, 0, ENC_MSG_LEN);
396 int l = strlen(data)/ENC_MSG_LEN;
397 if (strlen(data)%ENC_MSG_LEN)
401 EnDecryptInit(passwd, PASSWD_LEN, &ctx);
403 for (int j = 0; j < l; j++)
405 strncpy(buff, &data[j*ENC_MSG_LEN], ENC_MSG_LEN);
406 Encrypt(buffS, buff, &ctx);
407 send(outerSocket, buffS, ENC_MSG_LEN, 0);
412 //---------------------------------------------------------------------------
413 int NETTRANSACT::RxDataAnswer()
417 char bufferS[ENC_MSG_LEN];
418 char buffer[ENC_MSG_LEN + 1];
421 EnDecryptInit(password.c_str(), PASSWD_LEN, &ctx);
425 ret = recv(outerSocket, &bufferS[n++], 1, 0);
429 errorMsg = RECV_DATA_ANSWER_ERROR;
433 if (n == ENC_MSG_LEN)
436 Decrypt(buffer, bufferS, &ctx);
437 buffer[ENC_MSG_LEN] = 0;
439 answerList.push_back(buffer);
441 for (int j = 0; j < ENC_MSG_LEN; j++)
446 if (st_ok != RxCallBack(dataRxCallBack, &answerList))
448 return st_xml_parse_error;
456 //---------------------------------------------------------------------------
457 void NETTRANSACT::SetLogin(const char * l)
461 //---------------------------------------------------------------------------
462 void NETTRANSACT::SetPassword(const char * p)
466 //---------------------------------------------------------------------------
467 void NETTRANSACT::SetServer(const char * serverName)
471 //---------------------------------------------------------------------------
472 void NETTRANSACT::SetServerPort(short unsigned p)
476 //---------------------------------------------------------------------------
477 void NETTRANSACT::SetRxCallback(void * data, RxCallback_t cb)
480 dataRxCallBack = data;
482 //---------------------------------------------------------------------------
483 const std::string & NETTRANSACT::GetError() const
487 //---------------------------------------------------------------------------
488 void NETTRANSACT::Reset()
492 //---------------------------------------------------------------------------