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 "stg/servconf_types.h"
32 #include "stg/common.h"
33 #include "stg/blowfish.h"
40 #include <arpa/inet.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
52 const std::string::size_type MAX_XML_CHUNK_LENGTH = 2048;
56 //---------------------------------------------------------------------------
58 #define SEND_DATA_ERROR "Send data error!"
59 #define RECV_DATA_ANSWER_ERROR "Recv data answer error!"
60 #define UNKNOWN_ERROR "Unknown error!"
61 #define CONNECT_FAILED "Connect failed!"
62 #define INCORRECT_LOGIN "Incorrect login!"
63 #define INCORRECT_HEADER "Incorrect header!"
64 #define SEND_LOGIN_ERROR "Send login error!"
65 #define RECV_LOGIN_ANSWER_ERROR "Recv login answer error!"
66 #define CREATE_SOCKET_ERROR "Create socket failed!"
67 #define WSASTARTUP_FAILED "WSAStartup failed!"
68 #define SEND_HEADER_ERROR "Send header error!"
69 #define RECV_HEADER_ANSWER_ERROR "Recv header answer error!"
71 //---------------------------------------------------------------------------
72 NETTRANSACT::NETTRANSACT(const std::string & s, uint16_t p,
73 const std::string & l, const std::string & pwd)
83 //---------------------------------------------------------------------------
84 int NETTRANSACT::Connect()
88 outerSocket = socket(PF_INET, SOCK_STREAM, 0);
91 errorMsg = CREATE_SOCKET_ERROR;
95 struct sockaddr_in outerAddr;
96 memset(&outerAddr, 0, sizeof(outerAddr));
102 ip = inet_addr(server.c_str());
104 if (ip == INADDR_NONE)
106 phe = gethostbyname(server.c_str());
109 errorMsg = "DNS error.\nCan not reslove " + server;
113 memcpy(&he, phe, sizeof(he));
114 ip = *((long*)he.h_addr_list[0]);
116 outerAddr.sin_family = AF_INET;
117 outerAddr.sin_port = htons(port);
118 outerAddr.sin_addr.s_addr = ip;
120 ret = connect(outerSocket, (struct sockaddr*)&outerAddr, sizeof(outerAddr));
124 errorMsg = CONNECT_FAILED;
130 //---------------------------------------------------------------------------
131 int NETTRANSACT::Disconnect()
136 //---------------------------------------------------------------------------
137 int NETTRANSACT::Transact(const char * data)
140 if ((ret = TxHeader()) != st_ok)
146 if ((ret = RxHeaderAnswer()) != st_ok)
152 if ((ret = TxLogin()) != st_ok)
158 if ((ret = RxLoginAnswer()) != st_ok)
164 if ((ret = TxLoginS()) != st_ok)
170 if ((ret = RxLoginSAnswer()) != st_ok)
176 if ((ret = TxData(data)) != st_ok)
182 if ((ret = RxDataAnswer()) != st_ok)
190 //---------------------------------------------------------------------------
191 int NETTRANSACT::TxHeader()
194 ret = send(outerSocket, STG_HEADER, strlen(STG_HEADER), 0);
197 errorMsg = SEND_HEADER_ERROR;
203 //---------------------------------------------------------------------------
204 int NETTRANSACT::RxHeaderAnswer()
206 char buffer[sizeof(STG_HEADER)+1];
209 ret = recv(outerSocket, buffer, strlen(OK_HEADER), 0);
212 printf("Receive header answer error: '%s'\n", strerror(errno));
213 errorMsg = RECV_HEADER_ANSWER_ERROR;
217 if (strncmp(OK_HEADER, buffer, strlen(OK_HEADER)) == 0)
223 if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0)
225 errorMsg = INCORRECT_HEADER;
226 return st_header_err;
230 errorMsg = UNKNOWN_ERROR;
231 return st_unknown_err;
235 //---------------------------------------------------------------------------
236 int NETTRANSACT::TxLogin()
238 char loginZ[ADM_LOGIN_LEN];
241 memset(loginZ, 0, ADM_LOGIN_LEN);
242 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
243 ret = send(outerSocket, loginZ, ADM_LOGIN_LEN, 0);
247 errorMsg = SEND_LOGIN_ERROR;
253 //---------------------------------------------------------------------------
254 int NETTRANSACT::RxLoginAnswer()
256 char buffer[sizeof(OK_LOGIN)+1];
259 ret = recv(outerSocket, buffer, strlen(OK_LOGIN), 0);
262 printf("Receive login answer error: '%s'\n", strerror(errno));
263 errorMsg = RECV_LOGIN_ANSWER_ERROR;
267 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
273 if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
275 errorMsg = INCORRECT_LOGIN;
280 errorMsg = UNKNOWN_ERROR;
281 return st_unknown_err;
285 //---------------------------------------------------------------------------
286 int NETTRANSACT::TxLoginS()
288 char loginZ[ADM_LOGIN_LEN];
289 char ct[ENC_MSG_LEN];
292 memset(loginZ, 0, ADM_LOGIN_LEN);
293 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
296 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
298 for (int j = 0; j < ADM_LOGIN_LEN / ENC_MSG_LEN; j++)
300 EncodeString(ct, loginZ + j*ENC_MSG_LEN, &ctx);
301 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
304 errorMsg = SEND_LOGIN_ERROR;
311 //---------------------------------------------------------------------------
312 int NETTRANSACT::RxLoginSAnswer()
314 char buffer[sizeof(OK_LOGINS)+1];
317 ret = recv(outerSocket, buffer, strlen(OK_LOGINS), 0);
320 printf("Receive secret login answer error: '%s'\n", strerror(errno));
321 errorMsg = RECV_LOGIN_ANSWER_ERROR;
325 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
331 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
333 errorMsg = INCORRECT_LOGIN;
334 return st_logins_err;
338 errorMsg = UNKNOWN_ERROR;
339 return st_unknown_err;
343 //---------------------------------------------------------------------------
344 int NETTRANSACT::TxData(const char * text)
346 char textZ[ENC_MSG_LEN];
347 char ct[ENC_MSG_LEN];
351 int n = strlen(text) / ENC_MSG_LEN;
352 int r = strlen(text) % ENC_MSG_LEN;
355 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
357 for (j = 0; j < n; j++)
359 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
360 EncodeString(ct, textZ, &ctx);
361 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
364 errorMsg = SEND_DATA_ERROR;
369 memset(textZ, 0, ENC_MSG_LEN);
371 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
373 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
375 EncodeString(ct, textZ, &ctx);
376 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
379 errorMsg = SEND_DATA_ERROR;
385 //---------------------------------------------------------------------------
386 int NETTRANSACT::TxData(char * data)
388 char buff[ENC_MSG_LEN];
389 char buffS[ENC_MSG_LEN];
390 char passwd[ADM_PASSWD_LEN];
392 memset(passwd, 0, ADM_PASSWD_LEN);
393 strncpy(passwd, password.c_str(), ADM_PASSWD_LEN);
394 memset(buff, 0, ENC_MSG_LEN);
396 int l = strlen(data)/ENC_MSG_LEN;
397 if (strlen(data)%ENC_MSG_LEN)
401 EnDecodeInit(passwd, PASSWD_LEN, &ctx);
403 for (int j = 0; j < l; j++)
405 strncpy(buff, &data[j*ENC_MSG_LEN], ENC_MSG_LEN);
406 EncodeString(buffS, buff, &ctx);
407 send(outerSocket, buffS, ENC_MSG_LEN, 0);
412 //---------------------------------------------------------------------------
413 int NETTRANSACT::RxDataAnswer()
416 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
421 char bufferS[ENC_MSG_LEN];
422 size_t toRead = ENC_MSG_LEN;
425 int ret = recv(outerSocket, &bufferS[ENC_MSG_LEN - toRead], toRead, 0);
428 printf("Receive data error: '%s'\n", strerror(errno));
430 errorMsg = RECV_DATA_ANSWER_ERROR;
436 char buffer[ENC_MSG_LEN];
437 DecodeString(buffer, bufferS, &ctx);
441 for (; pos < ENC_MSG_LEN && buffer[pos] != 0; pos++);
442 if (pos < ENC_MSG_LEN && buffer[pos] == 0)
446 chunk.append(&buffer[0], &buffer[pos]);
448 if (chunk.length() > MAX_XML_CHUNK_LENGTH || final)
450 if (RxCallBack != NULL)
451 if (!RxCallBack(dataRxCallBack, chunk, final))
452 return st_xml_parse_error;
460 //---------------------------------------------------------------------------
461 void NETTRANSACT::SetRxCallback(void * data, RxCallback_t cb)
464 dataRxCallBack = data;
466 //---------------------------------------------------------------------------
467 const std::string & NETTRANSACT::GetError() const
471 //---------------------------------------------------------------------------