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 INCORRECT_LOGIN "Incorrect login!"
57 #define INCORRECT_HEADER "Incorrect header!"
58 #define SEND_LOGIN_ERROR "Send login error!"
59 #define RECV_LOGIN_ANSWER_ERROR "Recv login answer error!"
60 #define CREATE_SOCKET_ERROR "Create socket failed!"
61 #define WSASTARTUP_FAILED "WSAStartup failed!"
62 #define SEND_HEADER_ERROR "Send header error!"
63 #define RECV_HEADER_ANSWER_ERROR "Recv header answer error!"
65 //---------------------------------------------------------------------------
66 NETTRANSACT::NETTRANSACT(const std::string & s, uint16_t p,
67 const std::string & l, const std::string & pwd)
75 //---------------------------------------------------------------------------
76 int NETTRANSACT::Connect()
78 outerSocket = socket(PF_INET, SOCK_STREAM, 0);
81 errorMsg = CREATE_SOCKET_ERROR;
85 struct sockaddr_in outerAddr;
86 memset(&outerAddr, 0, sizeof(outerAddr));
88 unsigned long ip = inet_addr(server.c_str());
90 if (ip == INADDR_NONE)
92 struct hostent * phe = gethostbyname(server.c_str());
95 errorMsg = "DNS error.\nCan not reslove " + server;
100 memcpy(&he, phe, sizeof(he));
101 ip = *((long *)he.h_addr_list[0]);
104 outerAddr.sin_family = AF_INET;
105 outerAddr.sin_port = htons(port);
106 outerAddr.sin_addr.s_addr = ip;
108 if (connect(outerSocket, (struct sockaddr *)&outerAddr, sizeof(outerAddr)) < 0)
110 errorMsg = CONNECT_FAILED;
117 //---------------------------------------------------------------------------
118 void NETTRANSACT::Disconnect()
122 //---------------------------------------------------------------------------
123 int NETTRANSACT::Transact(const std::string & request, CALLBACK callback, void * data)
126 if ((ret = TxHeader()) != st_ok)
132 if ((ret = RxHeaderAnswer()) != st_ok)
138 if ((ret = TxLogin()) != st_ok)
144 if ((ret = RxLoginAnswer()) != st_ok)
150 if ((ret = TxLoginS()) != st_ok)
156 if ((ret = RxLoginSAnswer()) != st_ok)
162 if ((ret = TxData(request)) != st_ok)
168 if ((ret = RxDataAnswer(callback, data)) != st_ok)
176 //---------------------------------------------------------------------------
177 int NETTRANSACT::TxHeader()
179 if (send(outerSocket, STG_HEADER, strlen(STG_HEADER), 0) <= 0)
181 errorMsg = SEND_HEADER_ERROR;
187 //---------------------------------------------------------------------------
188 int NETTRANSACT::RxHeaderAnswer()
190 char buffer[sizeof(STG_HEADER) + 1];
192 if (recv(outerSocket, buffer, strlen(OK_HEADER), 0) <= 0)
194 printf("Receive header answer error: '%s'\n", strerror(errno));
195 errorMsg = RECV_HEADER_ANSWER_ERROR;
199 if (strncmp(OK_HEADER, buffer, strlen(OK_HEADER)) == 0)
205 if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0)
207 errorMsg = INCORRECT_HEADER;
208 return st_header_err;
212 errorMsg = UNKNOWN_ERROR;
213 return st_unknown_err;
217 //---------------------------------------------------------------------------
218 int NETTRANSACT::TxLogin()
220 char loginZ[ADM_LOGIN_LEN];
221 memset(loginZ, 0, ADM_LOGIN_LEN);
222 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
224 if (send(outerSocket, loginZ, ADM_LOGIN_LEN, 0) <= 0)
226 errorMsg = SEND_LOGIN_ERROR;
232 //---------------------------------------------------------------------------
233 int NETTRANSACT::RxLoginAnswer()
235 char buffer[sizeof(OK_LOGIN) + 1];
237 if (recv(outerSocket, buffer, strlen(OK_LOGIN), 0) <= 0)
239 printf("Receive login answer error: '%s'\n", strerror(errno));
240 errorMsg = RECV_LOGIN_ANSWER_ERROR;
244 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
250 if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
252 errorMsg = INCORRECT_LOGIN;
257 errorMsg = UNKNOWN_ERROR;
258 return st_unknown_err;
262 //---------------------------------------------------------------------------
263 int NETTRANSACT::TxLoginS()
265 char loginZ[ADM_LOGIN_LEN];
266 memset(loginZ, 0, ADM_LOGIN_LEN);
267 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
270 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
272 for (int j = 0; j < ADM_LOGIN_LEN / ENC_MSG_LEN; j++)
274 char ct[ENC_MSG_LEN];
275 EncodeString(ct, loginZ + j * ENC_MSG_LEN, &ctx);
276 if (send(outerSocket, ct, ENC_MSG_LEN, 0) <= 0)
278 errorMsg = SEND_LOGIN_ERROR;
285 //---------------------------------------------------------------------------
286 int NETTRANSACT::RxLoginSAnswer()
288 char buffer[sizeof(OK_LOGINS) + 1];
290 if (recv(outerSocket, buffer, strlen(OK_LOGINS), 0) <= 0)
292 printf("Receive secret login answer error: '%s'\n", strerror(errno));
293 errorMsg = RECV_LOGIN_ANSWER_ERROR;
297 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
303 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
305 errorMsg = INCORRECT_LOGIN;
306 return st_logins_err;
310 errorMsg = UNKNOWN_ERROR;
311 return st_unknown_err;
315 //---------------------------------------------------------------------------
316 int NETTRANSACT::TxData(const std::string & text)
319 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
322 while (pos < text.size())
324 char textZ[ENC_MSG_LEN];
325 if (text.size() - pos < ENC_MSG_LEN)
326 memset(textZ, 0, ENC_MSG_LEN);
327 strncpy(textZ, text.c_str() + pos, std::min(ENC_MSG_LEN, (int)(text.size() - pos)));
328 char ct[ENC_MSG_LEN];
329 EncodeString(ct, textZ, &ctx);
330 if (send(outerSocket, ct, ENC_MSG_LEN, 0) <= 0)
332 errorMsg = SEND_DATA_ERROR;
340 //---------------------------------------------------------------------------
341 int NETTRANSACT::RxDataAnswer(CALLBACK callback, void * data)
344 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
349 char bufferS[ENC_MSG_LEN];
350 size_t toRead = ENC_MSG_LEN;
353 int ret = recv(outerSocket, &bufferS[ENC_MSG_LEN - toRead], toRead, 0);
356 printf("Receive data error: '%s'\n", strerror(errno));
358 errorMsg = RECV_DATA_ANSWER_ERROR;
364 char buffer[ENC_MSG_LEN];
365 DecodeString(buffer, bufferS, &ctx);
369 for (; pos < ENC_MSG_LEN && buffer[pos] != 0; pos++) ;
370 if (pos < ENC_MSG_LEN && buffer[pos] == 0)
374 chunk.append(&buffer[0], &buffer[pos]);
376 if (chunk.length() > MAX_XML_CHUNK_LENGTH || final)
379 if (!callback(chunk, final, data))
380 return st_xml_parse_error;