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 <arpa/inet.h>
35 #include "stg/netunit.h"
36 #include "stg/common.h"
38 //---------------------------------------------------------------------------
40 #define SEND_DATA_ERROR "Send data error!"
41 #define RECV_DATA_ANSWER_ERROR "Recv data answer error!"
42 #define UNKNOWN_ERROR "Unknown error!"
43 #define CONNECT_FAILED "Connect failed!"
44 #define INCORRECT_LOGIN "Incorrect login!"
45 #define INCORRECT_HEADER "Incorrect header!"
46 #define SEND_LOGIN_ERROR "Send login error!"
47 #define RECV_LOGIN_ANSWER_ERROR "Recv login answer error!"
48 #define CREATE_SOCKET_ERROR "Create socket failed!"
49 #define WSASTARTUP_FAILED "WSAStartup failed!"
50 #define SEND_HEADER_ERROR "Send header error!"
51 #define RECV_HEADER_ANSWER_ERROR "Recv header answer error!"
53 //---------------------------------------------------------------------------
54 NETTRANSACT::NETTRANSACT()
61 //---------------------------------------------------------------------------
62 int NETTRANSACT::Connect()
66 outerSocket = socket(PF_INET, SOCK_STREAM, 0);
69 errorMsg = CREATE_SOCKET_ERROR;
73 struct sockaddr_in outerAddr;
74 memset(&outerAddr, 0, sizeof(outerAddr));
80 ip = inet_addr(server.c_str());
82 if (ip == INADDR_NONE)
84 phe = gethostbyname(server.c_str());
87 errorMsg = "DNS error.\nCan not reslove " + server;
91 memcpy(&he, phe, sizeof(he));
92 ip = *((long*)he.h_addr_list[0]);
94 outerAddr.sin_family = AF_INET;
95 outerAddr.sin_port = htons(port);
96 outerAddr.sin_addr.s_addr = ip;
98 ret = connect(outerSocket, (struct sockaddr*)&outerAddr, sizeof(outerAddr));
102 errorMsg = CONNECT_FAILED;
108 //---------------------------------------------------------------------------
109 int NETTRANSACT::Disconnect()
114 //---------------------------------------------------------------------------
115 int NETTRANSACT::Transact(const char * data)
118 if ((ret = TxHeader()) != st_ok)
124 if ((ret = RxHeaderAnswer()) != st_ok)
130 if ((ret = TxLogin()) != st_ok)
136 if ((ret = RxLoginAnswer()) != st_ok)
142 if ((ret = TxLoginS()) != st_ok)
148 if ((ret = RxLoginSAnswer()) != st_ok)
154 if ((ret = TxData(data)) != st_ok)
160 if ((ret = RxDataAnswer()) != st_ok)
168 //---------------------------------------------------------------------------
169 int NETTRANSACT::TxHeader()
172 ret = send(outerSocket, STG_HEADER, strlen(STG_HEADER), 0);
175 errorMsg = SEND_HEADER_ERROR;
181 //---------------------------------------------------------------------------
182 int NETTRANSACT::RxHeaderAnswer()
184 char buffer[sizeof(STG_HEADER)+1];
187 ret = recv(outerSocket, buffer, strlen(OK_HEADER), 0);
190 errorMsg = RECV_HEADER_ANSWER_ERROR;
194 if (strncmp(OK_HEADER, buffer, strlen(OK_HEADER)) == 0)
200 if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0)
202 errorMsg = INCORRECT_HEADER;
203 return st_header_err;
207 errorMsg = UNKNOWN_ERROR;
208 return st_unknown_err;
212 //---------------------------------------------------------------------------
213 int NETTRANSACT::TxLogin()
215 char loginZ[ADM_LOGIN_LEN];
218 memset(loginZ, 0, ADM_LOGIN_LEN);
219 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
220 ret = send(outerSocket, loginZ, ADM_LOGIN_LEN, 0);
224 errorMsg = SEND_LOGIN_ERROR;
230 //---------------------------------------------------------------------------
231 int NETTRANSACT::RxLoginAnswer()
233 char buffer[sizeof(OK_LOGIN)+1];
236 ret = recv(outerSocket, buffer, strlen(OK_LOGIN), 0);
239 errorMsg = RECV_LOGIN_ANSWER_ERROR;
243 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
249 if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
251 errorMsg = INCORRECT_LOGIN;
256 errorMsg = UNKNOWN_ERROR;
257 return st_unknown_err;
261 //---------------------------------------------------------------------------
262 int NETTRANSACT::TxLoginS()
264 char loginZ[ADM_LOGIN_LEN];
266 memset(loginZ, 0, ADM_LOGIN_LEN);
268 InitContext(password.c_str(), PASSWD_LEN, &ctx);
269 EncryptString(loginZ, login.c_str(), std::min<int>(login.length() + 1, ADM_LOGIN_LEN), &ctx);
270 if (send(outerSocket, loginZ, ADM_LOGIN_LEN, 0) <= 0)
272 errorMsg = SEND_LOGIN_ERROR;
277 //---------------------------------------------------------------------------
278 int NETTRANSACT::RxLoginSAnswer()
280 char buffer[sizeof(OK_LOGINS)+1];
283 ret = recv(outerSocket, buffer, strlen(OK_LOGINS), 0);
286 errorMsg = RECV_LOGIN_ANSWER_ERROR;
290 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
296 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
298 errorMsg = INCORRECT_LOGIN;
299 return st_logins_err;
303 errorMsg = UNKNOWN_ERROR;
304 return st_unknown_err;
308 //---------------------------------------------------------------------------
309 int NETTRANSACT::TxData(const char * text)
312 InitContext(password.c_str(), PASSWD_LEN, &ctx);
313 size_t length = strlen(text);
314 char buffer[length + 9];
315 EncryptString(buffer, text, length, &ctx);
316 if (send(outerSocket, buffer, sizeof(buffer), 0) <= 0)
318 errorMsg = SEND_DATA_ERROR;
323 //---------------------------------------------------------------------------
324 int NETTRANSACT::RxDataAnswer()
328 char bufferS[ENC_MSG_LEN];
329 char buffer[ENC_MSG_LEN + 1];
332 InitContext(password.c_str(), PASSWD_LEN, &ctx);
336 ret = recv(outerSocket, &bufferS[n++], 1, 0);
340 errorMsg = RECV_DATA_ANSWER_ERROR;
344 if (n == ENC_MSG_LEN)
347 DecryptBlock(buffer, bufferS, &ctx);
348 buffer[ENC_MSG_LEN] = 0;
350 answerList.push_back(buffer);
352 for (int j = 0; j < ENC_MSG_LEN; j++)
357 if (st_ok != RxCallBack(dataRxCallBack, &answerList))
359 return st_xml_parse_error;
367 //---------------------------------------------------------------------------
368 void NETTRANSACT::SetLogin(const char * l)
372 //---------------------------------------------------------------------------
373 void NETTRANSACT::SetPassword(const char * p)
377 //---------------------------------------------------------------------------
378 void NETTRANSACT::SetServer(const char * serverName)
382 //---------------------------------------------------------------------------
383 void NETTRANSACT::SetServerPort(short unsigned p)
387 //---------------------------------------------------------------------------
388 void NETTRANSACT::SetRxCallback(void * data, RxCallback_t cb)
391 dataRxCallBack = data;
393 //---------------------------------------------------------------------------
394 const std::string & NETTRANSACT::GetError() const
398 //---------------------------------------------------------------------------
399 void NETTRANSACT::Reset()
403 //---------------------------------------------------------------------------