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>
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()
60 memset(login, 0, ADM_LOGIN_LEN);
61 memset(password, 0, ADM_PASSWD_LEN);
63 //-----------------------------------------------------------------------------
64 void NETTRANSACT::EnDecryptInit(const char * passwd, int, BLOWFISH_CTX *ctx)
66 unsigned char * keyL = NULL;
68 keyL = new unsigned char[PASSWD_LEN];
70 memset(keyL, 0, PASSWD_LEN);
72 strncpy((char *)keyL, passwd, PASSWD_LEN);
74 Blowfish_Init(ctx, keyL, PASSWD_LEN);
78 //-----------------------------------------------------------------------------
79 void NETTRANSACT::Encrypt(char * d, const char * s, BLOWFISH_CTX *ctx)
81 EncodeString(d, s, ctx);
83 //---------------------------------------------------------------------------
84 void NETTRANSACT::Decrypt(char * d, const char * s, BLOWFISH_CTX *ctx)
86 DecodeString(d, s, ctx);
88 //---------------------------------------------------------------------------
89 int NETTRANSACT::Connect()
93 outerSocket = socket(PF_INET, SOCK_STREAM, 0);
96 errorMsg = CREATE_SOCKET_ERROR;
100 struct sockaddr_in outerAddr;
101 memset(&outerAddr, 0, sizeof(outerAddr));
104 struct hostent * phe;
107 ip = inet_addr(server.c_str());
109 if (ip == INADDR_NONE)
111 phe = gethostbyname(server.c_str());
114 errorMsg = "DNS error.\nCan not reslove " + server;
118 memcpy(&he, phe, sizeof(he));
119 ip = *((long*)he.h_addr_list[0]);
121 outerAddr.sin_family = AF_INET;
122 outerAddr.sin_port = htons(port);
123 outerAddr.sin_addr.s_addr = ip;
125 ret = connect(outerSocket, (struct sockaddr*)&outerAddr, sizeof(outerAddr));
129 errorMsg = CONNECT_FAILED;
135 //---------------------------------------------------------------------------
136 int NETTRANSACT::Disconnect()
141 //---------------------------------------------------------------------------
142 int NETTRANSACT::Transact(const char * data)
145 if ((ret = TxHeader()) != st_ok)
151 if ((ret = RxHeaderAnswer()) != st_ok)
157 if ((ret = TxLogin()) != st_ok)
163 if ((ret = RxLoginAnswer()) != st_ok)
169 if ((ret = TxLoginS()) != st_ok)
175 if ((ret = RxLoginSAnswer()) != st_ok)
181 if ((ret = TxData(data)) != st_ok)
187 if ((ret = RxDataAnswer()) != st_ok)
195 //---------------------------------------------------------------------------
196 int NETTRANSACT::TxHeader()
199 ret = send(outerSocket, STG_HEADER, strlen(STG_HEADER), 0);
202 errorMsg = SEND_HEADER_ERROR;
208 //---------------------------------------------------------------------------
209 int NETTRANSACT::RxHeaderAnswer()
211 char buffer[sizeof(STG_HEADER)+1];
214 ret = recv(outerSocket, buffer, strlen(OK_HEADER), 0);
217 errorMsg = RECV_HEADER_ANSWER_ERROR;
221 if (strncmp(OK_HEADER, buffer, strlen(OK_HEADER)) == 0)
227 if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0)
229 errorMsg = INCORRECT_HEADER;
230 return st_header_err;
234 errorMsg = UNKNOWN_ERROR;
235 return st_unknown_err;
239 //---------------------------------------------------------------------------
240 int NETTRANSACT::TxLogin()
242 char loginZ[ADM_LOGIN_LEN];
245 memset(loginZ, 0, ADM_LOGIN_LEN);
246 strncpy(loginZ, login, ADM_LOGIN_LEN);
247 ret = send(outerSocket, loginZ, ADM_LOGIN_LEN, 0);
251 errorMsg = SEND_LOGIN_ERROR;
257 //---------------------------------------------------------------------------
258 int NETTRANSACT::RxLoginAnswer()
260 char buffer[sizeof(OK_LOGIN)+1];
263 ret = recv(outerSocket, buffer, strlen(OK_LOGIN), 0);
266 errorMsg = RECV_LOGIN_ANSWER_ERROR;
270 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
276 if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
278 errorMsg = INCORRECT_LOGIN;
283 errorMsg = UNKNOWN_ERROR;
284 return st_unknown_err;
288 //---------------------------------------------------------------------------
289 int NETTRANSACT::TxLoginS()
291 char loginZ[ADM_LOGIN_LEN];
292 char ct[ENC_MSG_LEN];
295 memset(loginZ, 0, ADM_LOGIN_LEN);
296 strncpy(loginZ, login, ADM_LOGIN_LEN);
299 EnDecryptInit(password, PASSWD_LEN, &ctx);
301 for (int j = 0; j < ADM_LOGIN_LEN / ENC_MSG_LEN; j++)
303 Encrypt(ct, loginZ + j*ENC_MSG_LEN, &ctx);
304 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
307 errorMsg = SEND_LOGIN_ERROR;
314 //---------------------------------------------------------------------------
315 int NETTRANSACT::RxLoginSAnswer()
317 char buffer[sizeof(OK_LOGINS)+1];
320 ret = recv(outerSocket, buffer, strlen(OK_LOGINS), 0);
323 errorMsg = RECV_LOGIN_ANSWER_ERROR;
327 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
333 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
335 errorMsg = INCORRECT_LOGIN;
336 return st_logins_err;
340 errorMsg = UNKNOWN_ERROR;
341 return st_unknown_err;
345 //---------------------------------------------------------------------------
346 int NETTRANSACT::TxData(const char * text)
348 char textZ[ENC_MSG_LEN];
349 char ct[ENC_MSG_LEN];
353 int n = strlen(text) / ENC_MSG_LEN;
354 int r = strlen(text) % ENC_MSG_LEN;
357 EnDecryptInit(password, PASSWD_LEN, &ctx);
359 for (j = 0; j < n; j++)
361 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
362 Encrypt(ct, textZ, &ctx);
363 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
366 errorMsg = SEND_DATA_ERROR;
371 memset(textZ, 0, ENC_MSG_LEN);
373 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
375 EnDecryptInit(password, PASSWD_LEN, &ctx);
377 Encrypt(ct, textZ, &ctx);
378 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
381 errorMsg = SEND_DATA_ERROR;
387 //---------------------------------------------------------------------------
388 int NETTRANSACT::TxData(char * data)
390 char buff[ENC_MSG_LEN];
391 char buffS[ENC_MSG_LEN];
392 char passwd[ADM_PASSWD_LEN];
394 memset(passwd, 0, ADM_PASSWD_LEN);
395 strncpy(passwd, password, ADM_PASSWD_LEN);
396 memset(buff, 0, ENC_MSG_LEN);
398 int l = strlen(data)/ENC_MSG_LEN;
399 if (strlen(data)%ENC_MSG_LEN)
403 EnDecryptInit(passwd, PASSWD_LEN, &ctx);
405 for (int j = 0; j < l; j++)
407 strncpy(buff, &data[j*ENC_MSG_LEN], ENC_MSG_LEN);
408 Encrypt(buffS, buff, &ctx);
409 send(outerSocket, buffS, ENC_MSG_LEN, 0);
414 //---------------------------------------------------------------------------
415 int NETTRANSACT::RxDataAnswer()
419 char bufferS[ENC_MSG_LEN];
420 char buffer[ENC_MSG_LEN + 1];
423 EnDecryptInit(password, PASSWD_LEN, &ctx);
427 ret = recv(outerSocket, &bufferS[n++], 1, 0);
431 errorMsg = RECV_DATA_ANSWER_ERROR;
435 if (n == ENC_MSG_LEN)
439 Decrypt(buffer, bufferS, &ctx);
440 buffer[ENC_MSG_LEN] = 0;
442 answerList.push_back(buffer);
444 for (int j = 0; j < ENC_MSG_LEN; j++)
449 if (st_ok != RxCallBack(dataRxCallBack, &answerList))
451 return st_xml_parse_error;
459 //---------------------------------------------------------------------------
460 void NETTRANSACT::SetLogin(const char * l)
462 strncpy(login, l, ADM_LOGIN_LEN);
464 //---------------------------------------------------------------------------
465 void NETTRANSACT::SetPassword(const char * p)
467 strncpy(password, p, ADM_PASSWD_LEN);
469 //---------------------------------------------------------------------------
470 void NETTRANSACT::SetServer(const char * serverName)
474 //---------------------------------------------------------------------------
475 void NETTRANSACT::SetServerPort(short unsigned p)
479 //---------------------------------------------------------------------------
480 void NETTRANSACT::SetRxCallback(void * data, RxCallback_t cb)
483 dataRxCallBack = data;
485 //---------------------------------------------------------------------------
486 const std::string & NETTRANSACT::GetError() const
490 //---------------------------------------------------------------------------
491 void NETTRANSACT::Reset()
495 //---------------------------------------------------------------------------