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 //---------------------------------------------------------------------------
31 #include <arpa/inet.h>
37 //---------------------------------------------------------------------------
39 #define SEND_DATA_ERROR "Send data error!"
40 #define RECV_DATA_ANSWER_ERROR "Recv data answer error!"
41 #define UNKNOWN_ERROR "Unknown error!"
42 #define CONNECT_FAILED "Connect failed!"
43 #define INCORRECT_LOGIN "Incorrect login!"
44 #define INCORRECT_HEADER "Incorrect header!"
45 #define SEND_LOGIN_ERROR "Send login error!"
46 #define RECV_LOGIN_ANSWER_ERROR "Recv login answer error!"
47 #define CREATE_SOCKET_ERROR "Create socket failed!"
48 #define WSASTARTUP_FAILED "WSAStartup failed!"
49 #define SEND_HEADER_ERROR "Send header error!"
50 #define RECV_HEADER_ANSWER_ERROR "Recv header answer error!"
52 //---------------------------------------------------------------------------
53 NETTRANSACT::NETTRANSACT()
57 //-----------------------------------------------------------------------------
58 void NETTRANSACT::EnDecryptInit(const char * passwd, int, BLOWFISH_CTX *ctx)
60 unsigned char * keyL = NULL;//[PASSWD_LEN]; // ��� ������
62 keyL = new unsigned char[PASSWD_LEN];
64 memset(keyL, 0, PASSWD_LEN);
66 strncpy((char *)keyL, passwd, PASSWD_LEN);
68 Blowfish_Init(ctx, keyL, PASSWD_LEN);
72 //-----------------------------------------------------------------------------
73 void NETTRANSACT::Encrypt(char * d, const char * s, BLOWFISH_CTX *ctx)
75 /*unsigned char ss[8];
79 Blowfish_Encrypt(ctx, (uint32_t *)ss, (uint32_t *)(ss + 4));
82 EncodeString(d, s, ctx);
85 //---------------------------------------------------------------------------
86 void NETTRANSACT::Decrypt(char * d, const char * s, BLOWFISH_CTX *ctx)
88 /*unsigned char ss[8];
92 Blowfish_Decrypt(ctx, (uint32_t *)ss, (uint32_t *)(ss + 4));
95 DecodeString(d, s, ctx);
98 //---------------------------------------------------------------------------
99 int NETTRANSACT::Connect()
103 outerSocket = socket(PF_INET, SOCK_STREAM, 0);
106 strcpy(errorMsg, CREATE_SOCKET_ERROR);
110 memset(&outerAddr, 0, sizeof(outerAddr));
111 memset(&localAddr, 0, sizeof(localAddr));
114 struct hostent * phe;
117 ip = inet_addr(server);
119 if (ip == INADDR_NONE)
121 phe = gethostbyname(server);
124 sprintf(errorMsg, "DNS error.\nCan not reslove %s", server);
128 memcpy(&he, phe, sizeof(he));
129 ip = *((long*)he.h_addr_list[0]);
131 outerAddr.sin_family = AF_INET;
132 outerAddr.sin_port = htons(port);
133 outerAddr.sin_addr.s_addr = ip;
135 ret = connect(outerSocket, (struct sockaddr*)&outerAddr, sizeof(outerAddr));
139 strcpy(errorMsg, CONNECT_FAILED);
145 //---------------------------------------------------------------------------
146 int NETTRANSACT::Disconnect()
151 //---------------------------------------------------------------------------
152 int NETTRANSACT::Transact(const char * data)
155 if ((ret = TxHeader()) != st_ok)
161 if ((ret = RxHeaderAnswer()) != st_ok)
167 if ((ret = TxLogin()) != st_ok)
173 if ((ret = RxLoginAnswer()) != st_ok)
179 if ((ret = TxLoginS()) != st_ok)
185 if ((ret = RxLoginSAnswer()) != st_ok)
191 if ((ret = TxData(data)) != st_ok)
197 if ((ret = RxDataAnswer()) != st_ok)
205 //---------------------------------------------------------------------------
206 int NETTRANSACT::TxHeader()
209 ret = send(outerSocket, STG_HEADER, strlen(STG_HEADER), 0);
212 strcpy(errorMsg, SEND_HEADER_ERROR);
218 //---------------------------------------------------------------------------
219 int NETTRANSACT::RxHeaderAnswer()
221 char buffer[sizeof(STG_HEADER)+1];
224 ret = recv(outerSocket, buffer, strlen(OK_HEADER), 0);
227 //we = WSAGetLastError();
228 strcpy(errorMsg, RECV_HEADER_ANSWER_ERROR);
232 if (strncmp(OK_HEADER, buffer, strlen(OK_HEADER)) == 0)
238 if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0)
240 strcpy(errorMsg, INCORRECT_HEADER);
241 return st_header_err;
245 strcpy(errorMsg, UNKNOWN_ERROR);
246 return st_unknown_err;
250 //---------------------------------------------------------------------------
251 int NETTRANSACT::TxLogin()
253 char loginZ[ADM_LOGIN_LEN];
256 memset(loginZ, 0, ADM_LOGIN_LEN);
257 strncpy(loginZ, login, ADM_LOGIN_LEN);
258 ret = send(outerSocket, loginZ, ADM_LOGIN_LEN, 0);
262 strcpy(errorMsg, SEND_LOGIN_ERROR);
268 //---------------------------------------------------------------------------
269 int NETTRANSACT::RxLoginAnswer()
271 char buffer[sizeof(OK_LOGIN)+1];
274 ret = recv(outerSocket, buffer, strlen(OK_LOGIN), 0);
277 strcpy(errorMsg, RECV_LOGIN_ANSWER_ERROR);
281 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
287 if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
289 strcpy(errorMsg, INCORRECT_LOGIN);
294 strcpy(errorMsg, UNKNOWN_ERROR);
295 return st_unknown_err;
299 //---------------------------------------------------------------------------
300 int NETTRANSACT::TxLoginS()
302 char loginZ[ADM_LOGIN_LEN];
303 char ct[ENC_MSG_LEN];
306 memset(loginZ, 0, ADM_LOGIN_LEN);
307 strncpy(loginZ, login, ADM_LOGIN_LEN);
310 EnDecryptInit(password, PASSWD_LEN, &ctx);
312 for (int j = 0; j < ADM_LOGIN_LEN / ENC_MSG_LEN; j++)
314 Encrypt(ct, loginZ + j*ENC_MSG_LEN, &ctx);
315 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
318 strcpy(errorMsg, SEND_LOGIN_ERROR);
325 //---------------------------------------------------------------------------
326 int NETTRANSACT::RxLoginSAnswer()
328 char buffer[sizeof(OK_LOGINS)+1];
331 ret = recv(outerSocket, buffer, strlen(OK_LOGINS), 0);
334 strcpy(errorMsg, RECV_LOGIN_ANSWER_ERROR);
338 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
344 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
346 strcpy(errorMsg, INCORRECT_LOGIN);
347 return st_logins_err;
351 strcpy(errorMsg, UNKNOWN_ERROR);
352 return st_unknown_err;
356 //---------------------------------------------------------------------------
357 int NETTRANSACT::TxData(const char * text)
359 char textZ[ENC_MSG_LEN];
360 char ct[ENC_MSG_LEN];
364 int n = strlen(text) / ENC_MSG_LEN;
365 int r = strlen(text) % ENC_MSG_LEN;
368 EnDecryptInit(password, PASSWD_LEN, &ctx);
370 for (j = 0; j < n; j++)
372 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
373 Encrypt(ct, textZ, &ctx);
374 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
377 strcpy(errorMsg, SEND_DATA_ERROR);
382 memset(textZ, 0, ENC_MSG_LEN);
384 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
386 EnDecryptInit(password, PASSWD_LEN, &ctx);
388 Encrypt(ct, textZ, &ctx);
389 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
392 strcpy(errorMsg, SEND_DATA_ERROR);
398 //---------------------------------------------------------------------------
399 int NETTRANSACT::TxData(char * data)
401 char buff[ENC_MSG_LEN];
402 char buffS[ENC_MSG_LEN];
403 char passwd[ADM_PASSWD_LEN];
405 memset(passwd, 0, ADM_PASSWD_LEN);
406 strncpy(passwd, password, ADM_PASSWD_LEN);
407 memset(buff, 0, ENC_MSG_LEN);
409 int l = strlen(data)/ENC_MSG_LEN;
410 if (strlen(data)%ENC_MSG_LEN)
414 EnDecryptInit(passwd, PASSWD_LEN, &ctx);
416 for (int j = 0; j < l; j++)
418 strncpy(buff, &data[j*ENC_MSG_LEN], ENC_MSG_LEN);
419 Encrypt(buffS, buff, &ctx);
420 send(outerSocket, buffS, ENC_MSG_LEN, 0);
425 //---------------------------------------------------------------------------
426 int NETTRANSACT::RxDataAnswer()
430 char bufferS[ENC_MSG_LEN];
431 char buffer[ENC_MSG_LEN + 1];
434 EnDecryptInit(password, PASSWD_LEN, &ctx);
438 ret = recv(outerSocket, &bufferS[n++], 1, 0);
442 strcpy(errorMsg, RECV_DATA_ANSWER_ERROR);
446 if (n == ENC_MSG_LEN)
450 Decrypt(buffer, bufferS, &ctx);
451 buffer[ENC_MSG_LEN] = 0;
453 answerList.push_back(buffer);
455 for (int j = 0; j < ENC_MSG_LEN; j++)
460 if (st_ok != RxCallBack(dataRxCallBack, &answerList))
462 return st_xml_parse_error;
470 //---------------------------------------------------------------------------
471 void NETTRANSACT::SetLogin(const char * l)
473 strncpy(login, l, ADM_LOGIN_LEN);
475 //---------------------------------------------------------------------------
476 void NETTRANSACT::SetPassword(const char * p)
478 strncpy(password, p, ADM_PASSWD_LEN);
480 //---------------------------------------------------------------------------
481 void NETTRANSACT::SetServer(const char * serverName)
483 strncpy(server, serverName, SERVER_NAME_LEN);
485 //---------------------------------------------------------------------------
486 void NETTRANSACT::SetServerPort(short unsigned p)
490 //---------------------------------------------------------------------------
491 void NETTRANSACT::SetRxCallback(void * data, RxCallback_t cb)
494 dataRxCallBack = data;
496 //---------------------------------------------------------------------------
497 char * NETTRANSACT::GetError()
501 //---------------------------------------------------------------------------
502 void NETTRANSACT::Reset()
506 //---------------------------------------------------------------------------