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 "stg/netunit.h"
30 #include "stg/common.h"
37 #include <arpa/inet.h>
40 //---------------------------------------------------------------------------
42 #define SEND_DATA_ERROR "Send data error!"
43 #define RECV_DATA_ANSWER_ERROR "Recv data answer error!"
44 #define UNKNOWN_ERROR "Unknown error!"
45 #define CONNECT_FAILED "Connect failed!"
46 #define INCORRECT_LOGIN "Incorrect login!"
47 #define INCORRECT_HEADER "Incorrect header!"
48 #define SEND_LOGIN_ERROR "Send login error!"
49 #define RECV_LOGIN_ANSWER_ERROR "Recv login answer error!"
50 #define CREATE_SOCKET_ERROR "Create socket failed!"
51 #define WSASTARTUP_FAILED "WSAStartup failed!"
52 #define SEND_HEADER_ERROR "Send header error!"
53 #define RECV_HEADER_ANSWER_ERROR "Recv header answer error!"
55 //---------------------------------------------------------------------------
56 NETTRANSACT::NETTRANSACT()
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 printf("Receive header answer error: '%s'\n", strerror(errno));
218 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 errorMsg = INCORRECT_HEADER;
231 return st_header_err;
235 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.c_str(), ADM_LOGIN_LEN);
248 ret = send(outerSocket, loginZ, ADM_LOGIN_LEN, 0);
252 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 printf("Receive login answer error: '%s'\n", strerror(errno));
268 errorMsg = RECV_LOGIN_ANSWER_ERROR;
272 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
278 if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
280 errorMsg = INCORRECT_LOGIN;
285 errorMsg = UNKNOWN_ERROR;
286 return st_unknown_err;
290 //---------------------------------------------------------------------------
291 int NETTRANSACT::TxLoginS()
293 char loginZ[ADM_LOGIN_LEN];
294 char ct[ENC_MSG_LEN];
297 memset(loginZ, 0, ADM_LOGIN_LEN);
298 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
301 EnDecryptInit(password.c_str(), PASSWD_LEN, &ctx);
303 for (int j = 0; j < ADM_LOGIN_LEN / ENC_MSG_LEN; j++)
305 Encrypt(ct, loginZ + j*ENC_MSG_LEN, &ctx);
306 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
309 errorMsg = SEND_LOGIN_ERROR;
316 //---------------------------------------------------------------------------
317 int NETTRANSACT::RxLoginSAnswer()
319 char buffer[sizeof(OK_LOGINS)+1];
322 ret = recv(outerSocket, buffer, strlen(OK_LOGINS), 0);
325 printf("Receive secret login answer error: '%s'\n", strerror(errno));
326 errorMsg = RECV_LOGIN_ANSWER_ERROR;
330 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
336 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
338 errorMsg = INCORRECT_LOGIN;
339 return st_logins_err;
343 errorMsg = UNKNOWN_ERROR;
344 return st_unknown_err;
348 //---------------------------------------------------------------------------
349 int NETTRANSACT::TxData(const char * text)
351 char textZ[ENC_MSG_LEN];
352 char ct[ENC_MSG_LEN];
356 int n = strlen(text) / ENC_MSG_LEN;
357 int r = strlen(text) % ENC_MSG_LEN;
360 EnDecryptInit(password.c_str(), PASSWD_LEN, &ctx);
362 for (j = 0; j < n; j++)
364 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
365 Encrypt(ct, textZ, &ctx);
366 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
369 errorMsg = SEND_DATA_ERROR;
374 memset(textZ, 0, ENC_MSG_LEN);
376 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
378 EnDecryptInit(password.c_str(), PASSWD_LEN, &ctx);
380 Encrypt(ct, textZ, &ctx);
381 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
384 errorMsg = SEND_DATA_ERROR;
390 //---------------------------------------------------------------------------
391 int NETTRANSACT::TxData(char * data)
393 char buff[ENC_MSG_LEN];
394 char buffS[ENC_MSG_LEN];
395 char passwd[ADM_PASSWD_LEN];
397 memset(passwd, 0, ADM_PASSWD_LEN);
398 strncpy(passwd, password.c_str(), ADM_PASSWD_LEN);
399 memset(buff, 0, ENC_MSG_LEN);
401 int l = strlen(data)/ENC_MSG_LEN;
402 if (strlen(data)%ENC_MSG_LEN)
406 EnDecryptInit(passwd, PASSWD_LEN, &ctx);
408 for (int j = 0; j < l; j++)
410 strncpy(buff, &data[j*ENC_MSG_LEN], ENC_MSG_LEN);
411 Encrypt(buffS, buff, &ctx);
412 send(outerSocket, buffS, ENC_MSG_LEN, 0);
417 //---------------------------------------------------------------------------
418 int NETTRANSACT::RxDataAnswer()
422 char bufferS[ENC_MSG_LEN];
423 char buffer[ENC_MSG_LEN + 1];
426 EnDecryptInit(password.c_str(), PASSWD_LEN, &ctx);
430 ret = recv(outerSocket, &bufferS[n++], 1, 0);
433 printf("Receive data error: '%s'\n", strerror(errno));
435 errorMsg = RECV_DATA_ANSWER_ERROR;
439 if (n == ENC_MSG_LEN)
442 Decrypt(buffer, bufferS, &ctx);
443 buffer[ENC_MSG_LEN] = 0;
445 printf("%s", buffer);
447 answerList.push_back(buffer);
449 for (int j = 0; j < ENC_MSG_LEN; j++)
455 if (st_ok != RxCallBack(dataRxCallBack, &answerList))
456 return st_xml_parse_error;
463 //---------------------------------------------------------------------------
464 void NETTRANSACT::SetLogin(const char * l)
468 //---------------------------------------------------------------------------
469 void NETTRANSACT::SetPassword(const char * p)
473 //---------------------------------------------------------------------------
474 void NETTRANSACT::SetServer(const char * serverName)
478 //---------------------------------------------------------------------------
479 void NETTRANSACT::SetServerPort(short unsigned p)
483 //---------------------------------------------------------------------------
484 void NETTRANSACT::SetRxCallback(void * data, RxCallback_t cb)
487 dataRxCallBack = data;
489 //---------------------------------------------------------------------------
490 const std::string & NETTRANSACT::GetError() const
494 //---------------------------------------------------------------------------
495 void NETTRANSACT::Reset()
499 //---------------------------------------------------------------------------