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"
31 #include "stg/blowfish.h"
38 #include <arpa/inet.h>
44 const std::string::size_type MAX_XML_CHUNK_LENGTH = 2048;
48 //---------------------------------------------------------------------------
50 #define SEND_DATA_ERROR "Send data error!"
51 #define RECV_DATA_ANSWER_ERROR "Recv data answer error!"
52 #define UNKNOWN_ERROR "Unknown error!"
53 #define CONNECT_FAILED "Connect failed!"
54 #define INCORRECT_LOGIN "Incorrect login!"
55 #define INCORRECT_HEADER "Incorrect header!"
56 #define SEND_LOGIN_ERROR "Send login error!"
57 #define RECV_LOGIN_ANSWER_ERROR "Recv login answer error!"
58 #define CREATE_SOCKET_ERROR "Create socket failed!"
59 #define WSASTARTUP_FAILED "WSAStartup failed!"
60 #define SEND_HEADER_ERROR "Send header error!"
61 #define RECV_HEADER_ANSWER_ERROR "Recv header answer error!"
63 //---------------------------------------------------------------------------
64 NETTRANSACT::NETTRANSACT(const std::string & s, uint16_t p,
65 const std::string & l, const std::string & pwd)
75 //---------------------------------------------------------------------------
76 int NETTRANSACT::Connect()
80 outerSocket = socket(PF_INET, SOCK_STREAM, 0);
83 errorMsg = CREATE_SOCKET_ERROR;
87 struct sockaddr_in outerAddr;
88 memset(&outerAddr, 0, sizeof(outerAddr));
94 ip = inet_addr(server.c_str());
96 if (ip == INADDR_NONE)
98 phe = gethostbyname(server.c_str());
101 errorMsg = "DNS error.\nCan not reslove " + server;
105 memcpy(&he, phe, sizeof(he));
106 ip = *((long*)he.h_addr_list[0]);
108 outerAddr.sin_family = AF_INET;
109 outerAddr.sin_port = htons(port);
110 outerAddr.sin_addr.s_addr = ip;
112 ret = connect(outerSocket, (struct sockaddr*)&outerAddr, sizeof(outerAddr));
116 errorMsg = CONNECT_FAILED;
122 //---------------------------------------------------------------------------
123 int NETTRANSACT::Disconnect()
128 //---------------------------------------------------------------------------
129 int NETTRANSACT::Transact(const char * data)
132 if ((ret = TxHeader()) != st_ok)
138 if ((ret = RxHeaderAnswer()) != st_ok)
144 if ((ret = TxLogin()) != st_ok)
150 if ((ret = RxLoginAnswer()) != st_ok)
156 if ((ret = TxLoginS()) != st_ok)
162 if ((ret = RxLoginSAnswer()) != st_ok)
168 if ((ret = TxData(data)) != st_ok)
174 if ((ret = RxDataAnswer()) != st_ok)
182 //---------------------------------------------------------------------------
183 int NETTRANSACT::TxHeader()
186 ret = send(outerSocket, STG_HEADER, strlen(STG_HEADER), 0);
189 errorMsg = SEND_HEADER_ERROR;
195 //---------------------------------------------------------------------------
196 int NETTRANSACT::RxHeaderAnswer()
198 char buffer[sizeof(STG_HEADER)+1];
201 ret = recv(outerSocket, buffer, strlen(OK_HEADER), 0);
204 printf("Receive header answer error: '%s'\n", strerror(errno));
205 errorMsg = RECV_HEADER_ANSWER_ERROR;
209 if (strncmp(OK_HEADER, buffer, strlen(OK_HEADER)) == 0)
215 if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0)
217 errorMsg = INCORRECT_HEADER;
218 return st_header_err;
222 errorMsg = UNKNOWN_ERROR;
223 return st_unknown_err;
227 //---------------------------------------------------------------------------
228 int NETTRANSACT::TxLogin()
230 char loginZ[ADM_LOGIN_LEN];
233 memset(loginZ, 0, ADM_LOGIN_LEN);
234 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
235 ret = send(outerSocket, loginZ, ADM_LOGIN_LEN, 0);
239 errorMsg = SEND_LOGIN_ERROR;
245 //---------------------------------------------------------------------------
246 int NETTRANSACT::RxLoginAnswer()
248 char buffer[sizeof(OK_LOGIN)+1];
251 ret = recv(outerSocket, buffer, strlen(OK_LOGIN), 0);
254 printf("Receive login answer error: '%s'\n", strerror(errno));
255 errorMsg = RECV_LOGIN_ANSWER_ERROR;
259 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
265 if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
267 errorMsg = INCORRECT_LOGIN;
272 errorMsg = UNKNOWN_ERROR;
273 return st_unknown_err;
277 //---------------------------------------------------------------------------
278 int NETTRANSACT::TxLoginS()
280 char loginZ[ADM_LOGIN_LEN];
281 char ct[ENC_MSG_LEN];
284 memset(loginZ, 0, ADM_LOGIN_LEN);
285 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
288 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
290 for (int j = 0; j < ADM_LOGIN_LEN / ENC_MSG_LEN; j++)
292 EncodeString(ct, loginZ + j*ENC_MSG_LEN, &ctx);
293 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
296 errorMsg = SEND_LOGIN_ERROR;
303 //---------------------------------------------------------------------------
304 int NETTRANSACT::RxLoginSAnswer()
306 char buffer[sizeof(OK_LOGINS)+1];
309 ret = recv(outerSocket, buffer, strlen(OK_LOGINS), 0);
312 printf("Receive secret login answer error: '%s'\n", strerror(errno));
313 errorMsg = RECV_LOGIN_ANSWER_ERROR;
317 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
323 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
325 errorMsg = INCORRECT_LOGIN;
326 return st_logins_err;
330 errorMsg = UNKNOWN_ERROR;
331 return st_unknown_err;
335 //---------------------------------------------------------------------------
336 int NETTRANSACT::TxData(const char * text)
338 char textZ[ENC_MSG_LEN];
339 char ct[ENC_MSG_LEN];
343 int n = strlen(text) / ENC_MSG_LEN;
344 int r = strlen(text) % ENC_MSG_LEN;
347 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
349 for (j = 0; j < n; j++)
351 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
352 EncodeString(ct, textZ, &ctx);
353 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
356 errorMsg = SEND_DATA_ERROR;
361 memset(textZ, 0, ENC_MSG_LEN);
363 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
365 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
367 EncodeString(ct, textZ, &ctx);
368 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
371 errorMsg = SEND_DATA_ERROR;
377 //---------------------------------------------------------------------------
378 int NETTRANSACT::TxData(char * data)
380 char buff[ENC_MSG_LEN];
381 char buffS[ENC_MSG_LEN];
382 char passwd[ADM_PASSWD_LEN];
384 memset(passwd, 0, ADM_PASSWD_LEN);
385 strncpy(passwd, password.c_str(), ADM_PASSWD_LEN);
386 memset(buff, 0, ENC_MSG_LEN);
388 int l = strlen(data)/ENC_MSG_LEN;
389 if (strlen(data)%ENC_MSG_LEN)
393 EnDecodeInit(passwd, PASSWD_LEN, &ctx);
395 for (int j = 0; j < l; j++)
397 strncpy(buff, &data[j*ENC_MSG_LEN], ENC_MSG_LEN);
398 EncodeString(buffS, buff, &ctx);
399 send(outerSocket, buffS, ENC_MSG_LEN, 0);
404 //---------------------------------------------------------------------------
405 int NETTRANSACT::RxDataAnswer()
408 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
413 char bufferS[ENC_MSG_LEN];
414 size_t toRead = ENC_MSG_LEN;
417 int ret = recv(outerSocket, &bufferS[ENC_MSG_LEN - toRead], toRead, 0);
420 printf("Receive data error: '%s'\n", strerror(errno));
422 errorMsg = RECV_DATA_ANSWER_ERROR;
428 char buffer[ENC_MSG_LEN];
429 DecodeString(buffer, bufferS, &ctx);
433 for (; pos < ENC_MSG_LEN && buffer[pos] != 0; pos++);
434 if (pos < ENC_MSG_LEN && buffer[pos] == 0)
438 chunk.append(&buffer[0], &buffer[pos]);
440 if (chunk.length() > MAX_XML_CHUNK_LENGTH || final)
442 if (RxCallBack != NULL)
443 if (!RxCallBack(dataRxCallBack, chunk, final))
444 return st_xml_parse_error;
452 //---------------------------------------------------------------------------
453 void NETTRANSACT::SetRxCallback(void * data, RxCallback_t cb)
456 dataRxCallBack = data;
458 //---------------------------------------------------------------------------
459 const std::string & NETTRANSACT::GetError() const
463 //---------------------------------------------------------------------------