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 #include "stg/servconf_types.h"
24 #include "stg/common.h"
25 #include "stg/blowfish.h"
27 #include <algorithm> // std::min
34 #include <arpa/inet.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
46 const std::string::size_type MAX_XML_CHUNK_LENGTH = 2048;
50 //---------------------------------------------------------------------------
52 #define SEND_DATA_ERROR "Send data error!"
53 #define RECV_DATA_ANSWER_ERROR "Recv data answer error!"
54 #define UNKNOWN_ERROR "Unknown error!"
55 #define CONNECT_FAILED "Connect failed!"
56 #define BIND_FAILED "Bind failed!"
57 #define INCORRECT_LOGIN "Incorrect login!"
58 #define INCORRECT_HEADER "Incorrect header!"
59 #define SEND_LOGIN_ERROR "Send login error!"
60 #define RECV_LOGIN_ANSWER_ERROR "Recv login answer error!"
61 #define CREATE_SOCKET_ERROR "Create socket failed!"
62 #define WSASTARTUP_FAILED "WSAStartup failed!"
63 #define SEND_HEADER_ERROR "Send header error!"
64 #define RECV_HEADER_ANSWER_ERROR "Recv header answer error!"
66 //---------------------------------------------------------------------------
67 NETTRANSACT::NETTRANSACT(const std::string & s, uint16_t p,
68 const std::string & l, const std::string & pwd)
76 //---------------------------------------------------------------------------
77 NETTRANSACT::NETTRANSACT(const std::string & s, uint16_t p,
78 const std::string & la, uint16_t lp,
79 const std::string & l, const std::string & pwd)
89 //---------------------------------------------------------------------------
90 int NETTRANSACT::Connect()
92 outerSocket = socket(PF_INET, SOCK_STREAM, 0);
95 errorMsg = CREATE_SOCKET_ERROR;
99 if (!localAddress.empty())
104 unsigned long ip = inet_addr(localAddress.c_str());
106 if (ip == INADDR_NONE)
108 struct hostent * phe = gethostbyname(localAddress.c_str());
111 errorMsg = "DNS error.\nCan not reslove " + localAddress;
116 memcpy(&he, phe, sizeof(he));
117 ip = *((long *)he.h_addr_list[0]);
120 struct sockaddr_in localAddr;
121 memset(&localAddr, 0, sizeof(localAddr));
122 localAddr.sin_family = AF_INET;
123 localAddr.sin_port = htons(localPort);
124 localAddr.sin_addr.s_addr = ip;
126 if (bind(outerSocket, (struct sockaddr *)&localAddr, sizeof(localAddr)) < 0)
128 errorMsg = BIND_FAILED;
134 struct sockaddr_in outerAddr;
135 memset(&outerAddr, 0, sizeof(outerAddr));
137 unsigned long ip = inet_addr(server.c_str());
139 if (ip == INADDR_NONE)
141 struct hostent * phe = gethostbyname(server.c_str());
144 errorMsg = "DNS error.\nCan not reslove " + server;
149 memcpy(&he, phe, sizeof(he));
150 ip = *((long *)he.h_addr_list[0]);
153 outerAddr.sin_family = AF_INET;
154 outerAddr.sin_port = htons(port);
155 outerAddr.sin_addr.s_addr = ip;
157 if (connect(outerSocket, (struct sockaddr *)&outerAddr, sizeof(outerAddr)) < 0)
159 errorMsg = CONNECT_FAILED;
166 //---------------------------------------------------------------------------
167 void NETTRANSACT::Disconnect()
171 //---------------------------------------------------------------------------
172 int NETTRANSACT::Transact(const std::string & request, CALLBACK callback, void * data)
175 if ((ret = TxHeader()) != st_ok)
181 if ((ret = RxHeaderAnswer()) != st_ok)
187 if ((ret = TxLogin()) != st_ok)
193 if ((ret = RxLoginAnswer()) != st_ok)
199 if ((ret = TxLoginS()) != st_ok)
205 if ((ret = RxLoginSAnswer()) != st_ok)
211 if ((ret = TxData(request)) != st_ok)
217 if ((ret = RxDataAnswer(callback, data)) != st_ok)
225 //---------------------------------------------------------------------------
226 int NETTRANSACT::TxHeader()
228 if (send(outerSocket, STG_HEADER, strlen(STG_HEADER), 0) <= 0)
230 errorMsg = SEND_HEADER_ERROR;
236 //---------------------------------------------------------------------------
237 int NETTRANSACT::RxHeaderAnswer()
239 char buffer[sizeof(STG_HEADER) + 1];
241 if (recv(outerSocket, buffer, strlen(OK_HEADER), 0) <= 0)
243 printf("Receive header answer error: '%s'\n", strerror(errno));
244 errorMsg = RECV_HEADER_ANSWER_ERROR;
248 if (strncmp(OK_HEADER, buffer, strlen(OK_HEADER)) == 0)
254 if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0)
256 errorMsg = INCORRECT_HEADER;
257 return st_header_err;
261 errorMsg = UNKNOWN_ERROR;
262 return st_unknown_err;
266 //---------------------------------------------------------------------------
267 int NETTRANSACT::TxLogin()
269 char loginZ[ADM_LOGIN_LEN];
270 memset(loginZ, 0, ADM_LOGIN_LEN);
271 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
273 if (send(outerSocket, loginZ, ADM_LOGIN_LEN, 0) <= 0)
275 errorMsg = SEND_LOGIN_ERROR;
281 //---------------------------------------------------------------------------
282 int NETTRANSACT::RxLoginAnswer()
284 char buffer[sizeof(OK_LOGIN) + 1];
286 if (recv(outerSocket, buffer, strlen(OK_LOGIN), 0) <= 0)
288 printf("Receive login answer error: '%s'\n", strerror(errno));
289 errorMsg = RECV_LOGIN_ANSWER_ERROR;
293 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
299 if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
301 errorMsg = INCORRECT_LOGIN;
306 errorMsg = UNKNOWN_ERROR;
307 return st_unknown_err;
311 //---------------------------------------------------------------------------
312 int NETTRANSACT::TxLoginS()
314 char loginZ[ADM_LOGIN_LEN];
315 memset(loginZ, 0, ADM_LOGIN_LEN);
316 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
319 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
321 for (int j = 0; j < ADM_LOGIN_LEN / ENC_MSG_LEN; j++)
323 char ct[ENC_MSG_LEN];
324 EncodeString(ct, loginZ + j * ENC_MSG_LEN, &ctx);
325 if (send(outerSocket, ct, ENC_MSG_LEN, 0) <= 0)
327 errorMsg = SEND_LOGIN_ERROR;
334 //---------------------------------------------------------------------------
335 int NETTRANSACT::RxLoginSAnswer()
337 char buffer[sizeof(OK_LOGINS) + 1];
339 if (recv(outerSocket, buffer, strlen(OK_LOGINS), 0) <= 0)
341 printf("Receive secret login answer error: '%s'\n", strerror(errno));
342 errorMsg = RECV_LOGIN_ANSWER_ERROR;
346 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
352 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
354 errorMsg = INCORRECT_LOGIN;
355 return st_logins_err;
359 errorMsg = UNKNOWN_ERROR;
360 return st_unknown_err;
364 //---------------------------------------------------------------------------
365 int NETTRANSACT::TxData(const std::string & text)
368 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
371 while (pos < text.size())
373 char textZ[ENC_MSG_LEN];
374 if (text.size() - pos < ENC_MSG_LEN)
375 memset(textZ, 0, ENC_MSG_LEN);
376 strncpy(textZ, text.c_str() + pos, std::min(ENC_MSG_LEN, (int)(text.size() - pos)));
377 char ct[ENC_MSG_LEN];
378 EncodeString(ct, textZ, &ctx);
379 if (send(outerSocket, ct, ENC_MSG_LEN, 0) <= 0)
381 errorMsg = SEND_DATA_ERROR;
389 //---------------------------------------------------------------------------
390 int NETTRANSACT::RxDataAnswer(CALLBACK callback, void * data)
393 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
398 char bufferS[ENC_MSG_LEN];
399 size_t toRead = ENC_MSG_LEN;
402 int ret = recv(outerSocket, &bufferS[ENC_MSG_LEN - toRead], toRead, 0);
405 printf("Receive data error: '%s'\n", strerror(errno));
407 errorMsg = RECV_DATA_ANSWER_ERROR;
413 char buffer[ENC_MSG_LEN];
414 DecodeString(buffer, bufferS, &ctx);
418 for (; pos < ENC_MSG_LEN && buffer[pos] != 0; pos++) ;
419 if (pos < ENC_MSG_LEN && buffer[pos] == 0)
423 chunk.append(&buffer[0], &buffer[pos]);
425 if (chunk.length() > MAX_XML_CHUNK_LENGTH || final)
428 if (!callback(chunk, final, data))
429 return st_xml_parse_error;