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(const std::string & s, uint16_t p,
57 const std::string & l, const std::string & pwd)
67 //-----------------------------------------------------------------------------
68 void NETTRANSACT::EnDecryptInit(const char * passwd, int, BLOWFISH_CTX *ctx)
70 unsigned char * keyL = NULL;
72 keyL = new unsigned char[PASSWD_LEN];
74 memset(keyL, 0, PASSWD_LEN);
76 strncpy((char *)keyL, passwd, PASSWD_LEN);
78 Blowfish_Init(ctx, keyL, PASSWD_LEN);
82 //-----------------------------------------------------------------------------
83 void NETTRANSACT::Encrypt(char * d, const char * s, BLOWFISH_CTX *ctx)
85 EncodeString(d, s, ctx);
87 //---------------------------------------------------------------------------
88 void NETTRANSACT::Decrypt(char * d, const char * s, BLOWFISH_CTX *ctx)
90 DecodeString(d, s, ctx);
92 //---------------------------------------------------------------------------
93 int NETTRANSACT::Connect()
97 outerSocket = socket(PF_INET, SOCK_STREAM, 0);
100 errorMsg = CREATE_SOCKET_ERROR;
104 struct sockaddr_in outerAddr;
105 memset(&outerAddr, 0, sizeof(outerAddr));
108 struct hostent * phe;
111 ip = inet_addr(server.c_str());
113 if (ip == INADDR_NONE)
115 phe = gethostbyname(server.c_str());
118 errorMsg = "DNS error.\nCan not reslove " + server;
122 memcpy(&he, phe, sizeof(he));
123 ip = *((long*)he.h_addr_list[0]);
125 outerAddr.sin_family = AF_INET;
126 outerAddr.sin_port = htons(port);
127 outerAddr.sin_addr.s_addr = ip;
129 ret = connect(outerSocket, (struct sockaddr*)&outerAddr, sizeof(outerAddr));
133 errorMsg = CONNECT_FAILED;
139 //---------------------------------------------------------------------------
140 int NETTRANSACT::Disconnect()
145 //---------------------------------------------------------------------------
146 int NETTRANSACT::Transact(const char * data)
149 if ((ret = TxHeader()) != st_ok)
155 if ((ret = RxHeaderAnswer()) != st_ok)
161 if ((ret = TxLogin()) != st_ok)
167 if ((ret = RxLoginAnswer()) != st_ok)
173 if ((ret = TxLoginS()) != st_ok)
179 if ((ret = RxLoginSAnswer()) != st_ok)
185 if ((ret = TxData(data)) != st_ok)
191 if ((ret = RxDataAnswer()) != st_ok)
199 //---------------------------------------------------------------------------
200 int NETTRANSACT::TxHeader()
203 ret = send(outerSocket, STG_HEADER, strlen(STG_HEADER), 0);
206 errorMsg = SEND_HEADER_ERROR;
212 //---------------------------------------------------------------------------
213 int NETTRANSACT::RxHeaderAnswer()
215 char buffer[sizeof(STG_HEADER)+1];
218 ret = recv(outerSocket, buffer, strlen(OK_HEADER), 0);
221 printf("Receive header answer error: '%s'\n", strerror(errno));
222 errorMsg = RECV_HEADER_ANSWER_ERROR;
226 if (strncmp(OK_HEADER, buffer, strlen(OK_HEADER)) == 0)
232 if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0)
234 errorMsg = INCORRECT_HEADER;
235 return st_header_err;
239 errorMsg = UNKNOWN_ERROR;
240 return st_unknown_err;
244 //---------------------------------------------------------------------------
245 int NETTRANSACT::TxLogin()
247 char loginZ[ADM_LOGIN_LEN];
250 memset(loginZ, 0, ADM_LOGIN_LEN);
251 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
252 ret = send(outerSocket, loginZ, ADM_LOGIN_LEN, 0);
256 errorMsg = SEND_LOGIN_ERROR;
262 //---------------------------------------------------------------------------
263 int NETTRANSACT::RxLoginAnswer()
265 char buffer[sizeof(OK_LOGIN)+1];
268 ret = recv(outerSocket, buffer, strlen(OK_LOGIN), 0);
271 printf("Receive login answer error: '%s'\n", strerror(errno));
272 errorMsg = RECV_LOGIN_ANSWER_ERROR;
276 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
282 if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
284 errorMsg = INCORRECT_LOGIN;
289 errorMsg = UNKNOWN_ERROR;
290 return st_unknown_err;
294 //---------------------------------------------------------------------------
295 int NETTRANSACT::TxLoginS()
297 char loginZ[ADM_LOGIN_LEN];
298 char ct[ENC_MSG_LEN];
301 memset(loginZ, 0, ADM_LOGIN_LEN);
302 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
305 EnDecryptInit(password.c_str(), PASSWD_LEN, &ctx);
307 for (int j = 0; j < ADM_LOGIN_LEN / ENC_MSG_LEN; j++)
309 Encrypt(ct, loginZ + j*ENC_MSG_LEN, &ctx);
310 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
313 errorMsg = SEND_LOGIN_ERROR;
320 //---------------------------------------------------------------------------
321 int NETTRANSACT::RxLoginSAnswer()
323 char buffer[sizeof(OK_LOGINS)+1];
326 ret = recv(outerSocket, buffer, strlen(OK_LOGINS), 0);
329 printf("Receive secret login answer error: '%s'\n", strerror(errno));
330 errorMsg = RECV_LOGIN_ANSWER_ERROR;
334 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
340 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
342 errorMsg = INCORRECT_LOGIN;
343 return st_logins_err;
347 errorMsg = UNKNOWN_ERROR;
348 return st_unknown_err;
352 //---------------------------------------------------------------------------
353 int NETTRANSACT::TxData(const char * text)
355 char textZ[ENC_MSG_LEN];
356 char ct[ENC_MSG_LEN];
360 int n = strlen(text) / ENC_MSG_LEN;
361 int r = strlen(text) % ENC_MSG_LEN;
364 EnDecryptInit(password.c_str(), PASSWD_LEN, &ctx);
366 for (j = 0; j < n; j++)
368 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
369 Encrypt(ct, textZ, &ctx);
370 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
373 errorMsg = SEND_DATA_ERROR;
378 memset(textZ, 0, ENC_MSG_LEN);
380 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
382 EnDecryptInit(password.c_str(), PASSWD_LEN, &ctx);
384 Encrypt(ct, textZ, &ctx);
385 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
388 errorMsg = SEND_DATA_ERROR;
394 //---------------------------------------------------------------------------
395 int NETTRANSACT::TxData(char * data)
397 char buff[ENC_MSG_LEN];
398 char buffS[ENC_MSG_LEN];
399 char passwd[ADM_PASSWD_LEN];
401 memset(passwd, 0, ADM_PASSWD_LEN);
402 strncpy(passwd, password.c_str(), ADM_PASSWD_LEN);
403 memset(buff, 0, ENC_MSG_LEN);
405 int l = strlen(data)/ENC_MSG_LEN;
406 if (strlen(data)%ENC_MSG_LEN)
410 EnDecryptInit(passwd, PASSWD_LEN, &ctx);
412 for (int j = 0; j < l; j++)
414 strncpy(buff, &data[j*ENC_MSG_LEN], ENC_MSG_LEN);
415 Encrypt(buffS, buff, &ctx);
416 send(outerSocket, buffS, ENC_MSG_LEN, 0);
421 //---------------------------------------------------------------------------
422 int NETTRANSACT::RxDataAnswer()
426 char bufferS[ENC_MSG_LEN];
427 char buffer[ENC_MSG_LEN + 1];
430 EnDecryptInit(password.c_str(), PASSWD_LEN, &ctx);
434 ret = recv(outerSocket, &bufferS[n++], 1, 0);
437 printf("Receive data error: '%s'\n", strerror(errno));
439 errorMsg = RECV_DATA_ANSWER_ERROR;
443 if (n == ENC_MSG_LEN)
446 Decrypt(buffer, bufferS, &ctx);
447 buffer[ENC_MSG_LEN] = 0;
449 printf("%s", buffer);
451 answerList.push_back(buffer);
453 for (int j = 0; j < ENC_MSG_LEN; j++)
459 if (st_ok != RxCallBack(dataRxCallBack, &answerList))
460 return st_xml_parse_error;
467 //---------------------------------------------------------------------------
468 void NETTRANSACT::SetRxCallback(void * data, RxCallback_t cb)
471 dataRxCallBack = data;
473 //---------------------------------------------------------------------------
474 const std::string & NETTRANSACT::GetError() const
478 //---------------------------------------------------------------------------
479 void NETTRANSACT::Reset()
483 //---------------------------------------------------------------------------