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>
50 const std::string::size_type MAX_XML_CHUNK_LENGTH = 2048;
54 //---------------------------------------------------------------------------
56 #define SEND_DATA_ERROR "Send data error!"
57 #define RECV_DATA_ANSWER_ERROR "Recv data answer error!"
58 #define UNKNOWN_ERROR "Unknown error!"
59 #define CONNECT_FAILED "Connect failed!"
60 #define INCORRECT_LOGIN "Incorrect login!"
61 #define INCORRECT_HEADER "Incorrect header!"
62 #define SEND_LOGIN_ERROR "Send login error!"
63 #define RECV_LOGIN_ANSWER_ERROR "Recv login answer error!"
64 #define CREATE_SOCKET_ERROR "Create socket failed!"
65 #define WSASTARTUP_FAILED "WSAStartup failed!"
66 #define SEND_HEADER_ERROR "Send header error!"
67 #define RECV_HEADER_ANSWER_ERROR "Recv header answer error!"
69 //---------------------------------------------------------------------------
70 NETTRANSACT::NETTRANSACT(const std::string & s, uint16_t p,
71 const std::string & l, const std::string & pwd)
81 //---------------------------------------------------------------------------
82 int NETTRANSACT::Connect()
86 outerSocket = socket(PF_INET, SOCK_STREAM, 0);
89 errorMsg = CREATE_SOCKET_ERROR;
93 struct sockaddr_in outerAddr;
94 memset(&outerAddr, 0, sizeof(outerAddr));
100 ip = inet_addr(server.c_str());
102 if (ip == INADDR_NONE)
104 phe = gethostbyname(server.c_str());
107 errorMsg = "DNS error.\nCan not reslove " + server;
111 memcpy(&he, phe, sizeof(he));
112 ip = *((long*)he.h_addr_list[0]);
114 outerAddr.sin_family = AF_INET;
115 outerAddr.sin_port = htons(port);
116 outerAddr.sin_addr.s_addr = ip;
118 ret = connect(outerSocket, (struct sockaddr*)&outerAddr, sizeof(outerAddr));
122 errorMsg = CONNECT_FAILED;
128 //---------------------------------------------------------------------------
129 int NETTRANSACT::Disconnect()
134 //---------------------------------------------------------------------------
135 int NETTRANSACT::Transact(const char * data)
138 if ((ret = TxHeader()) != st_ok)
144 if ((ret = RxHeaderAnswer()) != st_ok)
150 if ((ret = TxLogin()) != st_ok)
156 if ((ret = RxLoginAnswer()) != st_ok)
162 if ((ret = TxLoginS()) != st_ok)
168 if ((ret = RxLoginSAnswer()) != st_ok)
174 if ((ret = TxData(data)) != st_ok)
180 if ((ret = RxDataAnswer()) != st_ok)
188 //---------------------------------------------------------------------------
189 int NETTRANSACT::TxHeader()
192 ret = send(outerSocket, STG_HEADER, strlen(STG_HEADER), 0);
195 errorMsg = SEND_HEADER_ERROR;
201 //---------------------------------------------------------------------------
202 int NETTRANSACT::RxHeaderAnswer()
204 char buffer[sizeof(STG_HEADER)+1];
207 ret = recv(outerSocket, buffer, strlen(OK_HEADER), 0);
210 printf("Receive header answer error: '%s'\n", strerror(errno));
211 errorMsg = RECV_HEADER_ANSWER_ERROR;
215 if (strncmp(OK_HEADER, buffer, strlen(OK_HEADER)) == 0)
221 if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0)
223 errorMsg = INCORRECT_HEADER;
224 return st_header_err;
228 errorMsg = UNKNOWN_ERROR;
229 return st_unknown_err;
233 //---------------------------------------------------------------------------
234 int NETTRANSACT::TxLogin()
236 char loginZ[ADM_LOGIN_LEN];
239 memset(loginZ, 0, ADM_LOGIN_LEN);
240 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
241 ret = send(outerSocket, loginZ, ADM_LOGIN_LEN, 0);
245 errorMsg = SEND_LOGIN_ERROR;
251 //---------------------------------------------------------------------------
252 int NETTRANSACT::RxLoginAnswer()
254 char buffer[sizeof(OK_LOGIN)+1];
257 ret = recv(outerSocket, buffer, strlen(OK_LOGIN), 0);
260 printf("Receive login answer error: '%s'\n", strerror(errno));
261 errorMsg = RECV_LOGIN_ANSWER_ERROR;
265 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
271 if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
273 errorMsg = INCORRECT_LOGIN;
278 errorMsg = UNKNOWN_ERROR;
279 return st_unknown_err;
283 //---------------------------------------------------------------------------
284 int NETTRANSACT::TxLoginS()
286 char loginZ[ADM_LOGIN_LEN];
287 char ct[ENC_MSG_LEN];
290 memset(loginZ, 0, ADM_LOGIN_LEN);
291 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
294 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
296 for (int j = 0; j < ADM_LOGIN_LEN / ENC_MSG_LEN; j++)
298 EncodeString(ct, loginZ + j*ENC_MSG_LEN, &ctx);
299 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
302 errorMsg = SEND_LOGIN_ERROR;
309 //---------------------------------------------------------------------------
310 int NETTRANSACT::RxLoginSAnswer()
312 char buffer[sizeof(OK_LOGINS)+1];
315 ret = recv(outerSocket, buffer, strlen(OK_LOGINS), 0);
318 printf("Receive secret login answer error: '%s'\n", strerror(errno));
319 errorMsg = RECV_LOGIN_ANSWER_ERROR;
323 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
329 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
331 errorMsg = INCORRECT_LOGIN;
332 return st_logins_err;
336 errorMsg = UNKNOWN_ERROR;
337 return st_unknown_err;
341 //---------------------------------------------------------------------------
342 int NETTRANSACT::TxData(const char * text)
344 char textZ[ENC_MSG_LEN];
345 char ct[ENC_MSG_LEN];
349 int n = strlen(text) / ENC_MSG_LEN;
350 int r = strlen(text) % ENC_MSG_LEN;
353 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
355 for (j = 0; j < n; j++)
357 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
358 EncodeString(ct, textZ, &ctx);
359 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
362 errorMsg = SEND_DATA_ERROR;
367 memset(textZ, 0, ENC_MSG_LEN);
369 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
371 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
373 EncodeString(ct, textZ, &ctx);
374 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
377 errorMsg = SEND_DATA_ERROR;
383 //---------------------------------------------------------------------------
384 int NETTRANSACT::TxData(char * data)
386 char buff[ENC_MSG_LEN];
387 char buffS[ENC_MSG_LEN];
388 char passwd[ADM_PASSWD_LEN];
390 memset(passwd, 0, ADM_PASSWD_LEN);
391 strncpy(passwd, password.c_str(), ADM_PASSWD_LEN);
392 memset(buff, 0, ENC_MSG_LEN);
394 int l = strlen(data)/ENC_MSG_LEN;
395 if (strlen(data)%ENC_MSG_LEN)
399 EnDecodeInit(passwd, PASSWD_LEN, &ctx);
401 for (int j = 0; j < l; j++)
403 strncpy(buff, &data[j*ENC_MSG_LEN], ENC_MSG_LEN);
404 EncodeString(buffS, buff, &ctx);
405 send(outerSocket, buffS, ENC_MSG_LEN, 0);
410 //---------------------------------------------------------------------------
411 int NETTRANSACT::RxDataAnswer()
414 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
419 char bufferS[ENC_MSG_LEN];
420 size_t toRead = ENC_MSG_LEN;
423 int ret = recv(outerSocket, &bufferS[ENC_MSG_LEN - toRead], toRead, 0);
426 printf("Receive data error: '%s'\n", strerror(errno));
428 errorMsg = RECV_DATA_ANSWER_ERROR;
434 char buffer[ENC_MSG_LEN];
435 DecodeString(buffer, bufferS, &ctx);
439 for (; pos < ENC_MSG_LEN && buffer[pos] != 0; pos++);
440 if (pos < ENC_MSG_LEN && buffer[pos] == 0)
444 chunk.append(&buffer[0], &buffer[pos]);
446 if (chunk.length() > MAX_XML_CHUNK_LENGTH || final)
448 if (RxCallBack != NULL)
449 if (!RxCallBack(dataRxCallBack, chunk, final))
450 return st_xml_parse_error;
458 //---------------------------------------------------------------------------
459 void NETTRANSACT::SetRxCallback(void * data, RxCallback_t cb)
462 dataRxCallBack = data;
464 //---------------------------------------------------------------------------
465 const std::string & NETTRANSACT::GetError() const
469 //---------------------------------------------------------------------------