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()
59 memset(server, 0, SERVER_NAME_LEN);
60 memset(login, 0, ADM_LOGIN_LEN);
61 memset(password, 0, ADM_PASSWD_LEN);
62 memset(errorMsg, 0, MAX_ERR_STR_LEN);
64 //-----------------------------------------------------------------------------
65 void NETTRANSACT::EnDecryptInit(const char * passwd, int, BLOWFISH_CTX *ctx)
67 unsigned char * keyL = NULL; // ��� ������
69 keyL = new unsigned char[PASSWD_LEN];
71 memset(keyL, 0, PASSWD_LEN);
73 strncpy((char *)keyL, passwd, PASSWD_LEN);
75 Blowfish_Init(ctx, keyL, PASSWD_LEN);
79 //-----------------------------------------------------------------------------
80 void NETTRANSACT::Encrypt(char * d, const char * s, BLOWFISH_CTX *ctx)
82 EncodeString(d, s, ctx);
84 //---------------------------------------------------------------------------
85 void NETTRANSACT::Decrypt(char * d, const char * s, BLOWFISH_CTX *ctx)
87 DecodeString(d, s, ctx);
89 //---------------------------------------------------------------------------
90 int NETTRANSACT::Connect()
94 outerSocket = socket(PF_INET, SOCK_STREAM, 0);
97 strcpy(errorMsg, CREATE_SOCKET_ERROR);
101 struct sockaddr_in outerAddr;
102 memset(&outerAddr, 0, sizeof(outerAddr));
105 struct hostent * phe;
108 ip = inet_addr(server);
110 if (ip == INADDR_NONE)
112 phe = gethostbyname(server);
115 sprintf(errorMsg, "DNS error.\nCan not reslove %s", server);
119 memcpy(&he, phe, sizeof(he));
120 ip = *((long*)he.h_addr_list[0]);
122 outerAddr.sin_family = AF_INET;
123 outerAddr.sin_port = htons(port);
124 outerAddr.sin_addr.s_addr = ip;
126 ret = connect(outerSocket, (struct sockaddr*)&outerAddr, sizeof(outerAddr));
130 strcpy(errorMsg, CONNECT_FAILED);
136 //---------------------------------------------------------------------------
137 int NETTRANSACT::Disconnect()
142 //---------------------------------------------------------------------------
143 int NETTRANSACT::Transact(const char * data)
146 if ((ret = TxHeader()) != st_ok)
152 if ((ret = RxHeaderAnswer()) != st_ok)
158 if ((ret = TxLogin()) != st_ok)
164 if ((ret = RxLoginAnswer()) != st_ok)
170 if ((ret = TxLoginS()) != st_ok)
176 if ((ret = RxLoginSAnswer()) != st_ok)
182 if ((ret = TxData(data)) != st_ok)
188 if ((ret = RxDataAnswer()) != st_ok)
196 //---------------------------------------------------------------------------
197 int NETTRANSACT::TxHeader()
200 ret = send(outerSocket, STG_HEADER, strlen(STG_HEADER), 0);
203 strcpy(errorMsg, SEND_HEADER_ERROR);
209 //---------------------------------------------------------------------------
210 int NETTRANSACT::RxHeaderAnswer()
212 char buffer[sizeof(STG_HEADER)+1];
215 ret = recv(outerSocket, buffer, strlen(OK_HEADER), 0);
218 strcpy(errorMsg, RECV_HEADER_ANSWER_ERROR);
222 if (strncmp(OK_HEADER, buffer, strlen(OK_HEADER)) == 0)
228 if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0)
230 strcpy(errorMsg, INCORRECT_HEADER);
231 return st_header_err;
235 strcpy(errorMsg, UNKNOWN_ERROR);
236 return st_unknown_err;
240 //---------------------------------------------------------------------------
241 int NETTRANSACT::TxLogin()
243 char loginZ[ADM_LOGIN_LEN];
246 memset(loginZ, 0, ADM_LOGIN_LEN);
247 strncpy(loginZ, login, ADM_LOGIN_LEN);
248 ret = send(outerSocket, loginZ, ADM_LOGIN_LEN, 0);
252 strcpy(errorMsg, SEND_LOGIN_ERROR);
258 //---------------------------------------------------------------------------
259 int NETTRANSACT::RxLoginAnswer()
261 char buffer[sizeof(OK_LOGIN)+1];
264 ret = recv(outerSocket, buffer, strlen(OK_LOGIN), 0);
267 strcpy(errorMsg, RECV_LOGIN_ANSWER_ERROR);
271 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
277 if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
279 strcpy(errorMsg, INCORRECT_LOGIN);
284 strcpy(errorMsg, UNKNOWN_ERROR);
285 return st_unknown_err;
289 //---------------------------------------------------------------------------
290 int NETTRANSACT::TxLoginS()
292 char loginZ[ADM_LOGIN_LEN];
293 char ct[ENC_MSG_LEN];
296 memset(loginZ, 0, ADM_LOGIN_LEN);
297 strncpy(loginZ, login, ADM_LOGIN_LEN);
300 EnDecryptInit(password, PASSWD_LEN, &ctx);
302 for (int j = 0; j < ADM_LOGIN_LEN / ENC_MSG_LEN; j++)
304 Encrypt(ct, loginZ + j*ENC_MSG_LEN, &ctx);
305 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
308 strcpy(errorMsg, SEND_LOGIN_ERROR);
315 //---------------------------------------------------------------------------
316 int NETTRANSACT::RxLoginSAnswer()
318 char buffer[sizeof(OK_LOGINS)+1];
321 ret = recv(outerSocket, buffer, strlen(OK_LOGINS), 0);
324 strcpy(errorMsg, RECV_LOGIN_ANSWER_ERROR);
328 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
334 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
336 strcpy(errorMsg, INCORRECT_LOGIN);
337 return st_logins_err;
341 strcpy(errorMsg, UNKNOWN_ERROR);
342 return st_unknown_err;
346 //---------------------------------------------------------------------------
347 int NETTRANSACT::TxData(const char * text)
349 char textZ[ENC_MSG_LEN];
350 char ct[ENC_MSG_LEN];
354 int n = strlen(text) / ENC_MSG_LEN;
355 int r = strlen(text) % ENC_MSG_LEN;
358 EnDecryptInit(password, PASSWD_LEN, &ctx);
360 for (j = 0; j < n; j++)
362 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
363 Encrypt(ct, textZ, &ctx);
364 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
367 strcpy(errorMsg, SEND_DATA_ERROR);
372 memset(textZ, 0, ENC_MSG_LEN);
374 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
376 EnDecryptInit(password, PASSWD_LEN, &ctx);
378 Encrypt(ct, textZ, &ctx);
379 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
382 strcpy(errorMsg, SEND_DATA_ERROR);
388 //---------------------------------------------------------------------------
389 int NETTRANSACT::TxData(char * data)
391 char buff[ENC_MSG_LEN];
392 char buffS[ENC_MSG_LEN];
393 char passwd[ADM_PASSWD_LEN];
395 memset(passwd, 0, ADM_PASSWD_LEN);
396 strncpy(passwd, password, ADM_PASSWD_LEN);
397 memset(buff, 0, ENC_MSG_LEN);
399 int l = strlen(data)/ENC_MSG_LEN;
400 if (strlen(data)%ENC_MSG_LEN)
404 EnDecryptInit(passwd, PASSWD_LEN, &ctx);
406 for (int j = 0; j < l; j++)
408 strncpy(buff, &data[j*ENC_MSG_LEN], ENC_MSG_LEN);
409 Encrypt(buffS, buff, &ctx);
410 send(outerSocket, buffS, ENC_MSG_LEN, 0);
415 //---------------------------------------------------------------------------
416 int NETTRANSACT::RxDataAnswer()
420 char bufferS[ENC_MSG_LEN];
421 char buffer[ENC_MSG_LEN + 1];
424 EnDecryptInit(password, PASSWD_LEN, &ctx);
428 ret = recv(outerSocket, &bufferS[n++], 1, 0);
432 strcpy(errorMsg, RECV_DATA_ANSWER_ERROR);
436 if (n == ENC_MSG_LEN)
440 Decrypt(buffer, bufferS, &ctx);
441 buffer[ENC_MSG_LEN] = 0;
443 answerList.push_back(buffer);
445 for (int j = 0; j < ENC_MSG_LEN; j++)
450 if (st_ok != RxCallBack(dataRxCallBack, &answerList))
452 return st_xml_parse_error;
460 //---------------------------------------------------------------------------
461 void NETTRANSACT::SetLogin(const char * l)
463 strncpy(login, l, ADM_LOGIN_LEN);
465 //---------------------------------------------------------------------------
466 void NETTRANSACT::SetPassword(const char * p)
468 strncpy(password, p, ADM_PASSWD_LEN);
470 //---------------------------------------------------------------------------
471 void NETTRANSACT::SetServer(const char * serverName)
473 strncpy(server, serverName, SERVER_NAME_LEN);
475 //---------------------------------------------------------------------------
476 void NETTRANSACT::SetServerPort(short unsigned p)
480 //---------------------------------------------------------------------------
481 void NETTRANSACT::SetRxCallback(void * data, RxCallback_t cb)
484 dataRxCallBack = data;
486 //---------------------------------------------------------------------------
487 char * NETTRANSACT::GetError()
491 //---------------------------------------------------------------------------
492 void NETTRANSACT::Reset()
496 //---------------------------------------------------------------------------