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()
86 outerSocket = socket(PF_INET, SOCK_STREAM, 0);
89 errorMsg = CREATE_SOCKET_ERROR;
93 struct sockaddr_in outerAddr;
94 memset(&outerAddr, 0, sizeof(outerAddr));
96 unsigned long ip = inet_addr(server.c_str());
98 if (ip == INADDR_NONE)
100 struct hostent * phe = gethostbyname(server.c_str());
103 errorMsg = "DNS error.\nCan not reslove " + server;
108 memcpy(&he, phe, sizeof(he));
109 ip = *((long *)he.h_addr_list[0]);
112 outerAddr.sin_family = AF_INET;
113 outerAddr.sin_port = htons(port);
114 outerAddr.sin_addr.s_addr = ip;
116 if (connect(outerSocket, (struct sockaddr *)&outerAddr, sizeof(outerAddr)) < 0)
118 errorMsg = CONNECT_FAILED;
125 //---------------------------------------------------------------------------
126 int NETTRANSACT::Disconnect()
131 //---------------------------------------------------------------------------
132 int NETTRANSACT::Transact(const char * data)
135 if ((ret = TxHeader()) != st_ok)
141 if ((ret = RxHeaderAnswer()) != st_ok)
147 if ((ret = TxLogin()) != st_ok)
153 if ((ret = RxLoginAnswer()) != st_ok)
159 if ((ret = TxLoginS()) != st_ok)
165 if ((ret = RxLoginSAnswer()) != st_ok)
171 if ((ret = TxData(data)) != st_ok)
177 if ((ret = RxDataAnswer()) != st_ok)
185 //---------------------------------------------------------------------------
186 int NETTRANSACT::TxHeader()
188 if (send(outerSocket, STG_HEADER, strlen(STG_HEADER), 0) <= 0)
190 errorMsg = SEND_HEADER_ERROR;
196 //---------------------------------------------------------------------------
197 int NETTRANSACT::RxHeaderAnswer()
199 char buffer[sizeof(STG_HEADER) + 1];
201 if (recv(outerSocket, buffer, strlen(OK_HEADER), 0) <= 0)
203 printf("Receive header answer error: '%s'\n", strerror(errno));
204 errorMsg = RECV_HEADER_ANSWER_ERROR;
208 if (strncmp(OK_HEADER, buffer, strlen(OK_HEADER)) == 0)
214 if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0)
216 errorMsg = INCORRECT_HEADER;
217 return st_header_err;
221 errorMsg = UNKNOWN_ERROR;
222 return st_unknown_err;
226 //---------------------------------------------------------------------------
227 int NETTRANSACT::TxLogin()
229 char loginZ[ADM_LOGIN_LEN];
230 memset(loginZ, 0, ADM_LOGIN_LEN);
231 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
233 if (send(outerSocket, loginZ, ADM_LOGIN_LEN, 0) <= 0)
235 errorMsg = SEND_LOGIN_ERROR;
241 //---------------------------------------------------------------------------
242 int NETTRANSACT::RxLoginAnswer()
244 char buffer[sizeof(OK_LOGIN) + 1];
246 if (recv(outerSocket, buffer, strlen(OK_LOGIN), 0) <= 0)
248 printf("Receive login answer error: '%s'\n", strerror(errno));
249 errorMsg = RECV_LOGIN_ANSWER_ERROR;
253 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
259 if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
261 errorMsg = INCORRECT_LOGIN;
266 errorMsg = UNKNOWN_ERROR;
267 return st_unknown_err;
271 //---------------------------------------------------------------------------
272 int NETTRANSACT::TxLoginS()
274 char loginZ[ADM_LOGIN_LEN];
275 memset(loginZ, 0, ADM_LOGIN_LEN);
276 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
279 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
281 for (int j = 0; j < ADM_LOGIN_LEN / ENC_MSG_LEN; j++)
283 char ct[ENC_MSG_LEN];
284 EncodeString(ct, loginZ + j * ENC_MSG_LEN, &ctx);
285 if (send(outerSocket, ct, ENC_MSG_LEN, 0) <= 0)
287 errorMsg = SEND_LOGIN_ERROR;
294 //---------------------------------------------------------------------------
295 int NETTRANSACT::RxLoginSAnswer()
297 char buffer[sizeof(OK_LOGINS) + 1];
299 if (recv(outerSocket, buffer, strlen(OK_LOGINS), 0) <= 0)
301 printf("Receive secret login answer error: '%s'\n", strerror(errno));
302 errorMsg = RECV_LOGIN_ANSWER_ERROR;
306 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
312 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
314 errorMsg = INCORRECT_LOGIN;
315 return st_logins_err;
319 errorMsg = UNKNOWN_ERROR;
320 return st_unknown_err;
324 //---------------------------------------------------------------------------
325 int NETTRANSACT::TxData(const char * text)
327 int n = strlen(text) / ENC_MSG_LEN;
328 int r = strlen(text) % ENC_MSG_LEN;
331 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
333 char textZ[ENC_MSG_LEN];
334 char ct[ENC_MSG_LEN];
336 for (int j = 0; j < n; j++)
338 strncpy(textZ, text + j * ENC_MSG_LEN, ENC_MSG_LEN);
339 EncodeString(ct, textZ, &ctx);
340 if (send(outerSocket, ct, ENC_MSG_LEN, 0) <= 0)
342 errorMsg = SEND_DATA_ERROR;
347 memset(textZ, 0, ENC_MSG_LEN);
350 strncpy(textZ, text + n * ENC_MSG_LEN, ENC_MSG_LEN);
352 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
354 EncodeString(ct, textZ, &ctx);
355 if (send(outerSocket, ct, ENC_MSG_LEN, 0) <= 0)
357 errorMsg = SEND_DATA_ERROR;
363 //---------------------------------------------------------------------------
364 int NETTRANSACT::TxData(char * data)
366 char passwd[ADM_PASSWD_LEN];
367 memset(passwd, 0, ADM_PASSWD_LEN);
368 strncpy(passwd, password.c_str(), ADM_PASSWD_LEN);
370 char buff[ENC_MSG_LEN];
371 memset(buff, 0, ENC_MSG_LEN);
373 int l = strlen(data) / ENC_MSG_LEN;
374 if (strlen(data) % ENC_MSG_LEN)
378 EnDecodeInit(passwd, PASSWD_LEN, &ctx);
380 for (int j = 0; j < l; j++)
382 strncpy(buff, &data[j * ENC_MSG_LEN], ENC_MSG_LEN);
383 char buffS[ENC_MSG_LEN];
384 EncodeString(buffS, buff, &ctx);
385 send(outerSocket, buffS, ENC_MSG_LEN, 0);
390 //---------------------------------------------------------------------------
391 int NETTRANSACT::RxDataAnswer()
394 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
399 char bufferS[ENC_MSG_LEN];
400 size_t toRead = ENC_MSG_LEN;
403 int ret = recv(outerSocket, &bufferS[ENC_MSG_LEN - toRead], toRead, 0);
406 printf("Receive data error: '%s'\n", strerror(errno));
408 errorMsg = RECV_DATA_ANSWER_ERROR;
414 char buffer[ENC_MSG_LEN];
415 DecodeString(buffer, bufferS, &ctx);
419 for (; pos < ENC_MSG_LEN && buffer[pos] != 0; pos++) ;
420 if (pos < ENC_MSG_LEN && buffer[pos] == 0)
424 chunk.append(&buffer[0], &buffer[pos]);
426 if (chunk.length() > MAX_XML_CHUNK_LENGTH || final)
428 if (RxCallBack != NULL)
429 if (!RxCallBack(dataRxCallBack, chunk, final))
430 return st_xml_parse_error;
438 //---------------------------------------------------------------------------
439 void NETTRANSACT::SetRxCallback(void * data, RxCallback_t cb)
442 dataRxCallBack = data;