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)
77 //---------------------------------------------------------------------------
78 NETTRANSACT::NETTRANSACT(const std::string & s, uint16_t p,
79 const std::string & la, uint16_t lp,
80 const std::string & l, const std::string & pwd)
90 //---------------------------------------------------------------------------
91 int NETTRANSACT::Connect()
93 outerSocket = socket(PF_INET, SOCK_STREAM, 0);
96 errorMsg = CREATE_SOCKET_ERROR;
100 if (!localAddress.empty())
105 unsigned long ip = inet_addr(localAddress.c_str());
107 if (ip == INADDR_NONE)
109 struct hostent * phe = gethostbyname(localAddress.c_str());
112 errorMsg = "DNS error.\nCan not reslove " + localAddress;
117 memcpy(&he, phe, sizeof(he));
118 ip = *((long *)he.h_addr_list[0]);
121 struct sockaddr_in localAddr;
122 memset(&localAddr, 0, sizeof(localAddr));
123 localAddr.sin_family = AF_INET;
124 localAddr.sin_port = htons(localPort);
125 localAddr.sin_addr.s_addr = ip;
127 if (bind(outerSocket, (struct sockaddr *)&localAddr, sizeof(localAddr)) < 0)
129 errorMsg = BIND_FAILED;
135 struct sockaddr_in outerAddr;
136 memset(&outerAddr, 0, sizeof(outerAddr));
138 unsigned long ip = inet_addr(server.c_str());
140 if (ip == INADDR_NONE)
142 struct hostent * phe = gethostbyname(server.c_str());
145 errorMsg = "DNS error.\nCan not reslove " + server;
150 memcpy(&he, phe, sizeof(he));
151 ip = *((long *)he.h_addr_list[0]);
154 outerAddr.sin_family = AF_INET;
155 outerAddr.sin_port = htons(port);
156 outerAddr.sin_addr.s_addr = ip;
158 if (connect(outerSocket, (struct sockaddr *)&outerAddr, sizeof(outerAddr)) < 0)
160 errorMsg = CONNECT_FAILED;
167 //---------------------------------------------------------------------------
168 void NETTRANSACT::Disconnect()
172 //---------------------------------------------------------------------------
173 int NETTRANSACT::Transact(const std::string & request, CALLBACK callback, void * data)
176 if ((ret = TxHeader()) != st_ok)
182 if ((ret = RxHeaderAnswer()) != st_ok)
188 if ((ret = TxLogin()) != st_ok)
194 if ((ret = RxLoginAnswer()) != st_ok)
200 if ((ret = TxLoginS()) != st_ok)
206 if ((ret = RxLoginSAnswer()) != st_ok)
212 if ((ret = TxData(request)) != st_ok)
218 if ((ret = RxDataAnswer(callback, data)) != st_ok)
226 //---------------------------------------------------------------------------
227 int NETTRANSACT::TxHeader()
229 if (send(outerSocket, STG_HEADER, strlen(STG_HEADER), 0) <= 0)
231 errorMsg = SEND_HEADER_ERROR;
237 //---------------------------------------------------------------------------
238 int NETTRANSACT::RxHeaderAnswer()
240 char buffer[sizeof(STG_HEADER) + 1];
242 if (recv(outerSocket, buffer, strlen(OK_HEADER), 0) <= 0)
244 printf("Receive header answer error: '%s'\n", strerror(errno));
245 errorMsg = RECV_HEADER_ANSWER_ERROR;
249 if (strncmp(OK_HEADER, buffer, strlen(OK_HEADER)) == 0)
255 if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0)
257 errorMsg = INCORRECT_HEADER;
258 return st_header_err;
262 errorMsg = UNKNOWN_ERROR;
263 return st_unknown_err;
267 //---------------------------------------------------------------------------
268 int NETTRANSACT::TxLogin()
270 char loginZ[ADM_LOGIN_LEN];
271 memset(loginZ, 0, ADM_LOGIN_LEN);
272 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
274 if (send(outerSocket, loginZ, ADM_LOGIN_LEN, 0) <= 0)
276 errorMsg = SEND_LOGIN_ERROR;
282 //---------------------------------------------------------------------------
283 int NETTRANSACT::RxLoginAnswer()
285 char buffer[sizeof(OK_LOGIN) + 1];
287 if (recv(outerSocket, buffer, strlen(OK_LOGIN), 0) <= 0)
289 printf("Receive login answer error: '%s'\n", strerror(errno));
290 errorMsg = RECV_LOGIN_ANSWER_ERROR;
294 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
300 if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
302 errorMsg = INCORRECT_LOGIN;
307 errorMsg = UNKNOWN_ERROR;
308 return st_unknown_err;
312 //---------------------------------------------------------------------------
313 int NETTRANSACT::TxLoginS()
315 char loginZ[ADM_LOGIN_LEN];
316 memset(loginZ, 0, ADM_LOGIN_LEN);
318 InitContext(password.c_str(), PASSWD_LEN, &ctx);
319 EncryptString(loginZ, login.c_str(), std::min(login.length(), ADM_LOGIN_LEN), &ctx);
320 if (send(outerSocket, loginZ, ADM_LOGIN_LEN, 0) <= 0)
322 errorMsg = SEND_LOGIN_ERROR;
327 //---------------------------------------------------------------------------
328 int NETTRANSACT::RxLoginSAnswer()
330 char buffer[sizeof(OK_LOGINS) + 1];
332 if (recv(outerSocket, buffer, strlen(OK_LOGINS), 0) <= 0)
334 printf("Receive secret login answer error: '%s'\n", strerror(errno));
335 errorMsg = RECV_LOGIN_ANSWER_ERROR;
339 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
345 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
347 errorMsg = INCORRECT_LOGIN;
348 return st_logins_err;
352 errorMsg = UNKNOWN_ERROR;
353 return st_unknown_err;
357 //---------------------------------------------------------------------------
358 int NETTRANSACT::TxData(const std::string & text)
361 InitContext(password.c_str(), PASSWD_LEN, &ctx);
362 char buffer[text.length()];
363 EncryptString(buffer, text.c_str(), text.length(), &ctx);
364 if (send(outerSocket, buffer, text.length(), 0) <= 0)
366 errorMsg = SEND_DATA_ERROR;
371 //---------------------------------------------------------------------------
372 int NETTRANSACT::RxDataAnswer(CALLBACK callback, void * data)
375 InitContext(password.c_str(), PASSWD_LEN, &ctx);
380 char bufferS[ENC_MSG_LEN];
381 size_t toRead = ENC_MSG_LEN;
384 int ret = recv(outerSocket, &bufferS[ENC_MSG_LEN - toRead], toRead, 0);
387 printf("Receive data error: '%s'\n", strerror(errno));
389 errorMsg = RECV_DATA_ANSWER_ERROR;
395 char buffer[ENC_MSG_LEN];
396 DecryptBlock(buffer, bufferS, &ctx);
400 for (; pos < ENC_MSG_LEN && buffer[pos] != 0; pos++) ;
401 if (pos < ENC_MSG_LEN && buffer[pos] == 0)
405 chunk.append(&buffer[0], &buffer[pos]);
407 if (chunk.length() > MAX_XML_CHUNK_LENGTH || final)
410 if (!callback(chunk, final, data))
411 return st_xml_parse_error;