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/servconf_types.h"
31 #include "stg/common.h"
32 #include "stg/blowfish.h"
39 #include <arpa/inet.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
49 const std::string::size_type MAX_XML_CHUNK_LENGTH = 2048;
53 //---------------------------------------------------------------------------
55 #define SEND_DATA_ERROR "Send data error!"
56 #define RECV_DATA_ANSWER_ERROR "Recv data answer error!"
57 #define UNKNOWN_ERROR "Unknown error!"
58 #define CONNECT_FAILED "Connect failed!"
59 #define INCORRECT_LOGIN "Incorrect login!"
60 #define INCORRECT_HEADER "Incorrect header!"
61 #define SEND_LOGIN_ERROR "Send login error!"
62 #define RECV_LOGIN_ANSWER_ERROR "Recv login answer error!"
63 #define CREATE_SOCKET_ERROR "Create socket failed!"
64 #define WSASTARTUP_FAILED "WSAStartup failed!"
65 #define SEND_HEADER_ERROR "Send header error!"
66 #define RECV_HEADER_ANSWER_ERROR "Recv header answer error!"
68 //---------------------------------------------------------------------------
69 NETTRANSACT::NETTRANSACT(const std::string & s, uint16_t p,
70 const std::string & l, const std::string & pwd)
80 //---------------------------------------------------------------------------
81 int NETTRANSACT::Connect()
85 outerSocket = socket(PF_INET, SOCK_STREAM, 0);
88 errorMsg = CREATE_SOCKET_ERROR;
92 struct sockaddr_in outerAddr;
93 memset(&outerAddr, 0, sizeof(outerAddr));
99 ip = inet_addr(server.c_str());
101 if (ip == INADDR_NONE)
103 phe = gethostbyname(server.c_str());
106 errorMsg = "DNS error.\nCan not reslove " + server;
110 memcpy(&he, phe, sizeof(he));
111 ip = *((long*)he.h_addr_list[0]);
113 outerAddr.sin_family = AF_INET;
114 outerAddr.sin_port = htons(port);
115 outerAddr.sin_addr.s_addr = ip;
117 ret = connect(outerSocket, (struct sockaddr*)&outerAddr, sizeof(outerAddr));
121 errorMsg = CONNECT_FAILED;
127 //---------------------------------------------------------------------------
128 int NETTRANSACT::Disconnect()
133 //---------------------------------------------------------------------------
134 int NETTRANSACT::Transact(const char * data)
137 if ((ret = TxHeader()) != st_ok)
143 if ((ret = RxHeaderAnswer()) != st_ok)
149 if ((ret = TxLogin()) != st_ok)
155 if ((ret = RxLoginAnswer()) != st_ok)
161 if ((ret = TxLoginS()) != st_ok)
167 if ((ret = RxLoginSAnswer()) != st_ok)
173 if ((ret = TxData(data)) != st_ok)
179 if ((ret = RxDataAnswer()) != st_ok)
187 //---------------------------------------------------------------------------
188 int NETTRANSACT::TxHeader()
191 ret = send(outerSocket, STG_HEADER, strlen(STG_HEADER), 0);
194 errorMsg = SEND_HEADER_ERROR;
200 //---------------------------------------------------------------------------
201 int NETTRANSACT::RxHeaderAnswer()
203 char buffer[sizeof(STG_HEADER)+1];
206 ret = recv(outerSocket, buffer, strlen(OK_HEADER), 0);
209 printf("Receive header answer error: '%s'\n", strerror(errno));
210 errorMsg = RECV_HEADER_ANSWER_ERROR;
214 if (strncmp(OK_HEADER, buffer, strlen(OK_HEADER)) == 0)
220 if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0)
222 errorMsg = INCORRECT_HEADER;
223 return st_header_err;
227 errorMsg = UNKNOWN_ERROR;
228 return st_unknown_err;
232 //---------------------------------------------------------------------------
233 int NETTRANSACT::TxLogin()
235 char loginZ[ADM_LOGIN_LEN];
238 memset(loginZ, 0, ADM_LOGIN_LEN);
239 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
240 ret = send(outerSocket, loginZ, ADM_LOGIN_LEN, 0);
244 errorMsg = SEND_LOGIN_ERROR;
250 //---------------------------------------------------------------------------
251 int NETTRANSACT::RxLoginAnswer()
253 char buffer[sizeof(OK_LOGIN)+1];
256 ret = recv(outerSocket, buffer, strlen(OK_LOGIN), 0);
259 printf("Receive login answer error: '%s'\n", strerror(errno));
260 errorMsg = RECV_LOGIN_ANSWER_ERROR;
264 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
270 if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
272 errorMsg = INCORRECT_LOGIN;
277 errorMsg = UNKNOWN_ERROR;
278 return st_unknown_err;
282 //---------------------------------------------------------------------------
283 int NETTRANSACT::TxLoginS()
285 char loginZ[ADM_LOGIN_LEN];
286 char ct[ENC_MSG_LEN];
289 memset(loginZ, 0, ADM_LOGIN_LEN);
290 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
293 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
295 for (int j = 0; j < ADM_LOGIN_LEN / ENC_MSG_LEN; j++)
297 EncodeString(ct, loginZ + j*ENC_MSG_LEN, &ctx);
298 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
301 errorMsg = SEND_LOGIN_ERROR;
308 //---------------------------------------------------------------------------
309 int NETTRANSACT::RxLoginSAnswer()
311 char buffer[sizeof(OK_LOGINS)+1];
314 ret = recv(outerSocket, buffer, strlen(OK_LOGINS), 0);
317 printf("Receive secret login answer error: '%s'\n", strerror(errno));
318 errorMsg = RECV_LOGIN_ANSWER_ERROR;
322 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
328 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
330 errorMsg = INCORRECT_LOGIN;
331 return st_logins_err;
335 errorMsg = UNKNOWN_ERROR;
336 return st_unknown_err;
340 //---------------------------------------------------------------------------
341 int NETTRANSACT::TxData(const char * text)
343 char textZ[ENC_MSG_LEN];
344 char ct[ENC_MSG_LEN];
348 int n = strlen(text) / ENC_MSG_LEN;
349 int r = strlen(text) % ENC_MSG_LEN;
352 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
354 for (j = 0; j < n; j++)
356 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
357 EncodeString(ct, textZ, &ctx);
358 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
361 errorMsg = SEND_DATA_ERROR;
366 memset(textZ, 0, ENC_MSG_LEN);
368 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
370 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
372 EncodeString(ct, textZ, &ctx);
373 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
376 errorMsg = SEND_DATA_ERROR;
382 //---------------------------------------------------------------------------
383 int NETTRANSACT::TxData(char * data)
385 char buff[ENC_MSG_LEN];
386 char buffS[ENC_MSG_LEN];
387 char passwd[ADM_PASSWD_LEN];
389 memset(passwd, 0, ADM_PASSWD_LEN);
390 strncpy(passwd, password.c_str(), ADM_PASSWD_LEN);
391 memset(buff, 0, ENC_MSG_LEN);
393 int l = strlen(data)/ENC_MSG_LEN;
394 if (strlen(data)%ENC_MSG_LEN)
398 EnDecodeInit(passwd, PASSWD_LEN, &ctx);
400 for (int j = 0; j < l; j++)
402 strncpy(buff, &data[j*ENC_MSG_LEN], ENC_MSG_LEN);
403 EncodeString(buffS, buff, &ctx);
404 send(outerSocket, buffS, ENC_MSG_LEN, 0);
409 //---------------------------------------------------------------------------
410 int NETTRANSACT::RxDataAnswer()
413 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
418 char bufferS[ENC_MSG_LEN];
419 size_t toRead = ENC_MSG_LEN;
422 int ret = recv(outerSocket, &bufferS[ENC_MSG_LEN - toRead], toRead, 0);
425 printf("Receive data error: '%s'\n", strerror(errno));
427 errorMsg = RECV_DATA_ANSWER_ERROR;
433 char buffer[ENC_MSG_LEN];
434 DecodeString(buffer, bufferS, &ctx);
438 for (; pos < ENC_MSG_LEN && buffer[pos] != 0; pos++);
439 if (pos < ENC_MSG_LEN && buffer[pos] == 0)
443 chunk.append(&buffer[0], &buffer[pos]);
445 if (chunk.length() > MAX_XML_CHUNK_LENGTH || final)
447 if (RxCallBack != NULL)
448 if (!RxCallBack(dataRxCallBack, chunk, final))
449 return st_xml_parse_error;
457 //---------------------------------------------------------------------------
458 void NETTRANSACT::SetRxCallback(void * data, RxCallback_t cb)
461 dataRxCallBack = data;
463 //---------------------------------------------------------------------------
464 const std::string & NETTRANSACT::GetError() const
468 //---------------------------------------------------------------------------