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"
26 #include "stg/bfstream.h"
28 #include <algorithm> // std::min
35 #include <arpa/inet.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
50 NETTRANSACT::CALLBACK callback;
57 //---------------------------------------------------------------------------
59 #define SEND_DATA_ERROR "Send data error!"
60 #define RECV_DATA_ANSWER_ERROR "Recv data answer error!"
61 #define UNKNOWN_ERROR "Unknown error!"
62 #define CONNECT_FAILED "Connect failed!"
63 #define BIND_FAILED "Bind failed!"
64 #define INCORRECT_LOGIN "Incorrect login!"
65 #define INCORRECT_HEADER "Incorrect header!"
66 #define SEND_LOGIN_ERROR "Send login error!"
67 #define RECV_LOGIN_ANSWER_ERROR "Recv login answer error!"
68 #define CREATE_SOCKET_ERROR "Create socket failed!"
69 #define WSASTARTUP_FAILED "WSAStartup failed!"
70 #define SEND_HEADER_ERROR "Send header error!"
71 #define RECV_HEADER_ANSWER_ERROR "Recv header answer error!"
73 //---------------------------------------------------------------------------
74 NETTRANSACT::NETTRANSACT(const std::string & s, uint16_t p,
75 const std::string & l, const std::string & pwd)
84 //---------------------------------------------------------------------------
85 NETTRANSACT::NETTRANSACT(const std::string & s, uint16_t p,
86 const std::string & la, uint16_t lp,
87 const std::string & l, const std::string & pwd)
97 //---------------------------------------------------------------------------
98 NETTRANSACT::~NETTRANSACT()
102 //---------------------------------------------------------------------------
103 int NETTRANSACT::Connect()
105 sock = socket(PF_INET, SOCK_STREAM, 0);
108 errorMsg = CREATE_SOCKET_ERROR;
112 if (!localAddress.empty())
117 unsigned long ip = inet_addr(localAddress.c_str());
119 if (ip == INADDR_NONE)
121 struct hostent * phe = gethostbyname(localAddress.c_str());
124 errorMsg = "Can not reslove '" + localAddress + "'";
129 memcpy(&he, phe, sizeof(he));
130 ip = *((long *)he.h_addr_list[0]);
133 struct sockaddr_in localAddr;
134 memset(&localAddr, 0, sizeof(localAddr));
135 localAddr.sin_family = AF_INET;
136 localAddr.sin_port = htons(localPort);
137 localAddr.sin_addr.s_addr = ip;
139 if (bind(sock, (struct sockaddr *)&localAddr, sizeof(localAddr)) < 0)
141 errorMsg = BIND_FAILED;
146 struct sockaddr_in outerAddr;
147 memset(&outerAddr, 0, sizeof(outerAddr));
149 unsigned long ip = inet_addr(server.c_str());
151 if (ip == INADDR_NONE)
153 struct hostent * phe = gethostbyname(server.c_str());
156 errorMsg = "Can not reslove '" + server + "'";
161 memcpy(&he, phe, sizeof(he));
162 ip = *((long *)he.h_addr_list[0]);
165 outerAddr.sin_family = AF_INET;
166 outerAddr.sin_port = htons(port);
167 outerAddr.sin_addr.s_addr = ip;
169 if (connect(sock, (struct sockaddr *)&outerAddr, sizeof(outerAddr)) < 0)
171 errorMsg = CONNECT_FAILED;
177 //---------------------------------------------------------------------------
178 void NETTRANSACT::Disconnect()
182 shutdown(sock, SHUT_RDWR);
187 //---------------------------------------------------------------------------
188 int NETTRANSACT::Transact(const std::string & request, CALLBACK callback, void * data)
191 if ((ret = TxHeader()) != st_ok)
194 if ((ret = RxHeaderAnswer()) != st_ok)
197 if ((ret = TxLogin()) != st_ok)
200 if ((ret = RxLoginAnswer()) != st_ok)
203 if ((ret = TxLoginS()) != st_ok)
206 if ((ret = RxLoginSAnswer()) != st_ok)
209 if ((ret = TxData(request)) != st_ok)
212 if ((ret = RxDataAnswer(callback, data)) != st_ok)
217 //---------------------------------------------------------------------------
218 int NETTRANSACT::TxHeader()
220 if (!WriteAll(sock, STG_HEADER, strlen(STG_HEADER)))
222 errorMsg = SEND_HEADER_ERROR;
228 //---------------------------------------------------------------------------
229 int NETTRANSACT::RxHeaderAnswer()
231 char buffer[sizeof(STG_HEADER) + 1];
233 if (!ReadAll(sock, buffer, strlen(OK_HEADER)))
235 errorMsg = RECV_HEADER_ANSWER_ERROR;
239 if (strncmp(OK_HEADER, buffer, strlen(OK_HEADER)) == 0)
242 if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0)
244 errorMsg = INCORRECT_HEADER;
245 return st_header_err;
249 errorMsg = UNKNOWN_ERROR;
250 return st_unknown_err;
253 //---------------------------------------------------------------------------
254 int NETTRANSACT::TxLogin()
256 char loginZ[ADM_LOGIN_LEN + 1];
257 memset(loginZ, 0, ADM_LOGIN_LEN + 1);
258 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
260 if (!WriteAll(sock, loginZ, ADM_LOGIN_LEN))
262 errorMsg = SEND_LOGIN_ERROR;
268 //---------------------------------------------------------------------------
269 int NETTRANSACT::RxLoginAnswer()
271 char buffer[sizeof(OK_LOGIN) + 1];
273 if (!ReadAll(sock, buffer, strlen(OK_LOGIN)))
275 errorMsg = RECV_LOGIN_ANSWER_ERROR;
279 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
282 if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
284 errorMsg = INCORRECT_LOGIN;
289 errorMsg = UNKNOWN_ERROR;
290 return st_unknown_err;
293 //---------------------------------------------------------------------------
294 int NETTRANSACT::TxLoginS()
296 char loginZ[ADM_LOGIN_LEN + 1];
297 memset(loginZ, 0, ADM_LOGIN_LEN + 1);
300 InitContext(password.c_str(), PASSWD_LEN, &ctx);
301 EncryptString(loginZ, login.c_str(), std::min<size_t>(login.length() + 1, ADM_LOGIN_LEN), &ctx);
302 if (!WriteAll(sock, loginZ, ADM_LOGIN_LEN))
304 errorMsg = SEND_LOGIN_ERROR;
310 //---------------------------------------------------------------------------
311 int NETTRANSACT::RxLoginSAnswer()
313 char buffer[sizeof(OK_LOGINS) + 1];
315 if (!ReadAll(sock, buffer, strlen(OK_LOGINS)))
317 errorMsg = RECV_LOGIN_ANSWER_ERROR;
321 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
324 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
326 errorMsg = INCORRECT_LOGIN;
327 return st_logins_err;
331 errorMsg = UNKNOWN_ERROR;
332 return st_unknown_err;
335 //---------------------------------------------------------------------------
336 int NETTRANSACT::TxData(const std::string & text)
338 STG::ENCRYPT_STREAM stream(password, TxCrypto, this);
339 stream.Put(text.c_str(), text.length() + 1, true);
342 errorMsg = SEND_DATA_ERROR;
348 //---------------------------------------------------------------------------
349 int NETTRANSACT::RxDataAnswer(CALLBACK callback, void * data)
351 ReadState state = {false, callback, data, this};
352 STG::DECRYPT_STREAM stream(password, RxCrypto, &state);
356 ssize_t res = read(sock, buffer, sizeof(buffer));
359 errorMsg = RECV_DATA_ANSWER_ERROR;
362 stream.Put(buffer, res, res == 0);
364 return st_xml_parse_error;
369 //---------------------------------------------------------------------------
370 bool NETTRANSACT::TxCrypto(const void * block, size_t size, void * data)
372 assert(data != NULL);
373 NETTRANSACT & nt = *static_cast<NETTRANSACT *>(data);
374 if (!WriteAll(nt.sock, block, size))
378 //---------------------------------------------------------------------------
379 bool NETTRANSACT::RxCrypto(const void * block, size_t size, void * data)
381 assert(data != NULL);
382 ReadState & state = *static_cast<ReadState *>(data);
384 const char * buffer = static_cast<const char *>(block);
385 for (size_t pos = 0; pos < size; ++pos)
386 if (buffer[pos] == 0)
389 size = pos; // Adjust string size
393 if (!state.callback(std::string(buffer, size), state.final, state.callbackData))