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>
41 //---------------------------------------------------------------------------
43 #define SEND_DATA_ERROR "Send data error!"
44 #define RECV_DATA_ANSWER_ERROR "Recv data answer error!"
45 #define UNKNOWN_ERROR "Unknown error!"
46 #define CONNECT_FAILED "Connect failed!"
47 #define INCORRECT_LOGIN "Incorrect login!"
48 #define INCORRECT_HEADER "Incorrect header!"
49 #define SEND_LOGIN_ERROR "Send login error!"
50 #define RECV_LOGIN_ANSWER_ERROR "Recv login answer error!"
51 #define CREATE_SOCKET_ERROR "Create socket failed!"
52 #define WSASTARTUP_FAILED "WSAStartup failed!"
53 #define SEND_HEADER_ERROR "Send header error!"
54 #define RECV_HEADER_ANSWER_ERROR "Recv header answer error!"
56 //---------------------------------------------------------------------------
57 NETTRANSACT::NETTRANSACT(const std::string & s, uint16_t p,
58 const std::string & l, const std::string & pwd)
68 //---------------------------------------------------------------------------
69 int NETTRANSACT::Connect()
73 outerSocket = socket(PF_INET, SOCK_STREAM, 0);
76 errorMsg = CREATE_SOCKET_ERROR;
80 struct sockaddr_in outerAddr;
81 memset(&outerAddr, 0, sizeof(outerAddr));
87 ip = inet_addr(server.c_str());
89 if (ip == INADDR_NONE)
91 phe = gethostbyname(server.c_str());
94 errorMsg = "DNS error.\nCan not reslove " + server;
98 memcpy(&he, phe, sizeof(he));
99 ip = *((long*)he.h_addr_list[0]);
101 outerAddr.sin_family = AF_INET;
102 outerAddr.sin_port = htons(port);
103 outerAddr.sin_addr.s_addr = ip;
105 ret = connect(outerSocket, (struct sockaddr*)&outerAddr, sizeof(outerAddr));
109 errorMsg = CONNECT_FAILED;
115 //---------------------------------------------------------------------------
116 int NETTRANSACT::Disconnect()
121 //---------------------------------------------------------------------------
122 int NETTRANSACT::Transact(const char * data)
125 if ((ret = TxHeader()) != st_ok)
131 if ((ret = RxHeaderAnswer()) != st_ok)
137 if ((ret = TxLogin()) != st_ok)
143 if ((ret = RxLoginAnswer()) != st_ok)
149 if ((ret = TxLoginS()) != st_ok)
155 if ((ret = RxLoginSAnswer()) != st_ok)
161 if ((ret = TxData(data)) != st_ok)
167 if ((ret = RxDataAnswer()) != st_ok)
175 //---------------------------------------------------------------------------
176 int NETTRANSACT::TxHeader()
179 ret = send(outerSocket, STG_HEADER, strlen(STG_HEADER), 0);
182 errorMsg = SEND_HEADER_ERROR;
188 //---------------------------------------------------------------------------
189 int NETTRANSACT::RxHeaderAnswer()
191 char buffer[sizeof(STG_HEADER)+1];
194 ret = recv(outerSocket, buffer, strlen(OK_HEADER), 0);
197 printf("Receive header answer error: '%s'\n", strerror(errno));
198 errorMsg = RECV_HEADER_ANSWER_ERROR;
202 if (strncmp(OK_HEADER, buffer, strlen(OK_HEADER)) == 0)
208 if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0)
210 errorMsg = INCORRECT_HEADER;
211 return st_header_err;
215 errorMsg = UNKNOWN_ERROR;
216 return st_unknown_err;
220 //---------------------------------------------------------------------------
221 int NETTRANSACT::TxLogin()
223 char loginZ[ADM_LOGIN_LEN];
226 memset(loginZ, 0, ADM_LOGIN_LEN);
227 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
228 ret = send(outerSocket, loginZ, ADM_LOGIN_LEN, 0);
232 errorMsg = SEND_LOGIN_ERROR;
238 //---------------------------------------------------------------------------
239 int NETTRANSACT::RxLoginAnswer()
241 char buffer[sizeof(OK_LOGIN)+1];
244 ret = recv(outerSocket, buffer, strlen(OK_LOGIN), 0);
247 printf("Receive login answer error: '%s'\n", strerror(errno));
248 errorMsg = RECV_LOGIN_ANSWER_ERROR;
252 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
258 if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
260 errorMsg = INCORRECT_LOGIN;
265 errorMsg = UNKNOWN_ERROR;
266 return st_unknown_err;
270 //---------------------------------------------------------------------------
271 int NETTRANSACT::TxLoginS()
273 char loginZ[ADM_LOGIN_LEN];
274 char ct[ENC_MSG_LEN];
277 memset(loginZ, 0, ADM_LOGIN_LEN);
278 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
281 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
283 for (int j = 0; j < ADM_LOGIN_LEN / ENC_MSG_LEN; j++)
285 EncodeString(ct, loginZ + j*ENC_MSG_LEN, &ctx);
286 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
289 errorMsg = SEND_LOGIN_ERROR;
296 //---------------------------------------------------------------------------
297 int NETTRANSACT::RxLoginSAnswer()
299 char buffer[sizeof(OK_LOGINS)+1];
302 ret = recv(outerSocket, buffer, strlen(OK_LOGINS), 0);
305 printf("Receive secret login answer error: '%s'\n", strerror(errno));
306 errorMsg = RECV_LOGIN_ANSWER_ERROR;
310 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
316 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
318 errorMsg = INCORRECT_LOGIN;
319 return st_logins_err;
323 errorMsg = UNKNOWN_ERROR;
324 return st_unknown_err;
328 //---------------------------------------------------------------------------
329 int NETTRANSACT::TxData(const char * text)
331 char textZ[ENC_MSG_LEN];
332 char ct[ENC_MSG_LEN];
336 int n = strlen(text) / ENC_MSG_LEN;
337 int r = strlen(text) % ENC_MSG_LEN;
340 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
342 for (j = 0; j < n; j++)
344 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
345 EncodeString(ct, textZ, &ctx);
346 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
349 errorMsg = SEND_DATA_ERROR;
354 memset(textZ, 0, ENC_MSG_LEN);
356 strncpy(textZ, text + j*ENC_MSG_LEN, ENC_MSG_LEN);
358 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
360 EncodeString(ct, textZ, &ctx);
361 ret = send(outerSocket, ct, ENC_MSG_LEN, 0);
364 errorMsg = SEND_DATA_ERROR;
370 //---------------------------------------------------------------------------
371 int NETTRANSACT::TxData(char * data)
373 char buff[ENC_MSG_LEN];
374 char buffS[ENC_MSG_LEN];
375 char passwd[ADM_PASSWD_LEN];
377 memset(passwd, 0, ADM_PASSWD_LEN);
378 strncpy(passwd, password.c_str(), ADM_PASSWD_LEN);
379 memset(buff, 0, ENC_MSG_LEN);
381 int l = strlen(data)/ENC_MSG_LEN;
382 if (strlen(data)%ENC_MSG_LEN)
386 EnDecodeInit(passwd, PASSWD_LEN, &ctx);
388 for (int j = 0; j < l; j++)
390 strncpy(buff, &data[j*ENC_MSG_LEN], ENC_MSG_LEN);
391 EncodeString(buffS, buff, &ctx);
392 send(outerSocket, buffS, ENC_MSG_LEN, 0);
397 //---------------------------------------------------------------------------
398 int NETTRANSACT::RxDataAnswer()
401 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
405 char bufferS[ENC_MSG_LEN];
406 size_t toRead = ENC_MSG_LEN;
409 int ret = recv(outerSocket, &bufferS[ENC_MSG_LEN - toRead], toRead, 0);
412 printf("Receive data error: '%s'\n", strerror(errno));
414 errorMsg = RECV_DATA_ANSWER_ERROR;
420 char buffer[ENC_MSG_LEN + 1];
421 DecodeString(buffer, bufferS, &ctx);
422 buffer[ENC_MSG_LEN] = 0;
424 answerList.push_back(buffer);
426 for (size_t i = 0; i < ENC_MSG_LEN; i++)
431 if (st_ok != RxCallBack(dataRxCallBack, &answerList))
432 return st_xml_parse_error;
438 //---------------------------------------------------------------------------
439 void NETTRANSACT::SetRxCallback(void * data, RxCallback_t cb)
442 dataRxCallBack = data;
444 //---------------------------------------------------------------------------
445 const std::string & NETTRANSACT::GetError() const
449 //---------------------------------------------------------------------------
450 void NETTRANSACT::Reset()
454 //---------------------------------------------------------------------------