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>
42 const char STG_HEADER[] = "SG04";
43 const char OK_HEADER[] = "OKHD";
44 const char ERR_HEADER[] = "ERHD";
45 const char OK_LOGIN[] = "OKLG";
46 const char ERR_LOGIN[] = "ERLG";
47 const char OK_LOGINS[] = "OKLS";
48 const char ERR_LOGINS[] = "ERLS";
58 NetTransact::Callback callback;
65 //---------------------------------------------------------------------------
67 const char SEND_DATA_ERROR[] = "Error sending data.";
68 const char RECV_DATA_ANSWER_ERROR[] = "Error receiving data answer.";
69 const char UNKNOWN_ERROR[] = "Unknown error";
70 const char CONNECT_FAILED[] = "Failed to connect.";
71 const char BIND_FAILED[] = "Failed to bind.";
72 const char INCORRECT_LOGIN[] = "Incorrect login.";
73 const char INCORRECT_HEADER[] = "Incorrect header.";
74 const char SEND_LOGIN_ERROR[] = "Error sending login.";
75 const char RECV_LOGIN_ANSWER_ERROR[] = "Error receiving login answer.";
76 const char CREATE_SOCKET_ERROR[] = "Failed to create socket.";
77 const char SEND_HEADER_ERROR[] = "Error sending header.";
78 const char RECV_HEADER_ANSWER_ERROR[] = "Error receiving header answer.";
80 //---------------------------------------------------------------------------
81 NetTransact::NetTransact(const std::string& s, uint16_t p,
82 const std::string& l, const std::string& pwd)
91 //---------------------------------------------------------------------------
92 NetTransact::NetTransact(const std::string& s, uint16_t p,
93 const std::string& la, uint16_t lp,
94 const std::string& l, const std::string& pwd)
104 //---------------------------------------------------------------------------
105 NetTransact::~NetTransact()
109 //---------------------------------------------------------------------------
110 int NetTransact::Connect()
112 sock = socket(PF_INET, SOCK_STREAM, 0);
115 errorMsg = CREATE_SOCKET_ERROR;
119 if (!localAddress.empty())
124 unsigned long ip = inet_addr(localAddress.c_str());
126 if (ip == INADDR_NONE)
128 auto phe = gethostbyname(localAddress.c_str());
131 errorMsg = "Can not reslove '" + localAddress + "'";
136 memcpy(&he, phe, sizeof(he));
137 ip = *((long *)he.h_addr_list[0]);
140 struct sockaddr_in localAddr;
141 memset(&localAddr, 0, sizeof(localAddr));
142 localAddr.sin_family = AF_INET;
143 localAddr.sin_port = htons(localPort);
144 localAddr.sin_addr.s_addr = ip;
146 if (bind(sock, (struct sockaddr *)&localAddr, sizeof(localAddr)) < 0)
148 errorMsg = BIND_FAILED;
153 struct sockaddr_in outerAddr;
154 memset(&outerAddr, 0, sizeof(outerAddr));
156 unsigned long ip = inet_addr(server.c_str());
158 if (ip == INADDR_NONE)
160 auto phe = gethostbyname(server.c_str());
163 errorMsg = "Can not reslove '" + server + "'";
168 memcpy(&he, phe, sizeof(he));
169 ip = *((long *)he.h_addr_list[0]);
172 outerAddr.sin_family = AF_INET;
173 outerAddr.sin_port = htons(port);
174 outerAddr.sin_addr.s_addr = ip;
176 if (connect(sock, (struct sockaddr *)&outerAddr, sizeof(outerAddr)) < 0)
178 errorMsg = CONNECT_FAILED;
184 //---------------------------------------------------------------------------
185 void NetTransact::Disconnect()
189 shutdown(sock, SHUT_RDWR);
194 //---------------------------------------------------------------------------
195 int NetTransact::Transact(const std::string& request, Callback callback, void* data)
198 if ((ret = TxHeader()) != st_ok)
201 if ((ret = RxHeaderAnswer()) != st_ok)
204 if ((ret = TxLogin()) != st_ok)
207 if ((ret = RxLoginAnswer()) != st_ok)
210 if ((ret = TxLoginS()) != st_ok)
213 if ((ret = RxLoginSAnswer()) != st_ok)
216 if ((ret = TxData(request)) != st_ok)
219 if ((ret = RxDataAnswer(callback, data)) != st_ok)
224 //---------------------------------------------------------------------------
225 int NetTransact::TxHeader()
227 if (!WriteAll(sock, STG_HEADER, strlen(STG_HEADER)))
229 errorMsg = SEND_HEADER_ERROR;
235 //---------------------------------------------------------------------------
236 int NetTransact::RxHeaderAnswer()
238 char buffer[sizeof(STG_HEADER) + 1];
240 if (!ReadAll(sock, buffer, strlen(OK_HEADER)))
242 errorMsg = RECV_HEADER_ANSWER_ERROR;
246 if (strncmp(OK_HEADER, buffer, strlen(OK_HEADER)) == 0)
249 if (strncmp(ERR_HEADER, buffer, strlen(ERR_HEADER)) == 0)
251 errorMsg = INCORRECT_HEADER;
252 return st_header_err;
255 errorMsg = UNKNOWN_ERROR;
256 return st_unknown_err;
258 //---------------------------------------------------------------------------
259 int NetTransact::TxLogin()
261 char loginZ[ADM_LOGIN_LEN + 1];
262 memset(loginZ, 0, ADM_LOGIN_LEN + 1);
263 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
265 if (!WriteAll(sock, loginZ, ADM_LOGIN_LEN))
267 errorMsg = SEND_LOGIN_ERROR;
273 //---------------------------------------------------------------------------
274 int NetTransact::RxLoginAnswer()
276 char buffer[sizeof(OK_LOGIN) + 1];
278 if (!ReadAll(sock, buffer, strlen(OK_LOGIN)))
280 errorMsg = RECV_LOGIN_ANSWER_ERROR;
284 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
287 if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
289 errorMsg = INCORRECT_LOGIN;
293 errorMsg = UNKNOWN_ERROR;
294 return st_unknown_err;
296 //---------------------------------------------------------------------------
297 int NetTransact::TxLoginS()
299 char loginZ[ADM_LOGIN_LEN + 1];
300 memset(loginZ, 0, ADM_LOGIN_LEN + 1);
303 InitContext(password.c_str(), PASSWD_LEN, &ctx);
304 EncryptString(loginZ, login.c_str(), std::min<size_t>(login.length() + 1, ADM_LOGIN_LEN), &ctx);
305 if (!WriteAll(sock, loginZ, ADM_LOGIN_LEN))
307 errorMsg = SEND_LOGIN_ERROR;
313 //---------------------------------------------------------------------------
314 int NetTransact::RxLoginSAnswer()
316 char buffer[sizeof(OK_LOGINS) + 1];
318 if (!ReadAll(sock, buffer, strlen(OK_LOGINS)))
320 errorMsg = RECV_LOGIN_ANSWER_ERROR;
324 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
327 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
329 errorMsg = INCORRECT_LOGIN;
330 return st_logins_err;
333 errorMsg = UNKNOWN_ERROR;
334 return st_unknown_err;
336 //---------------------------------------------------------------------------
337 int NetTransact::TxData(const std::string& text)
339 STG::ENCRYPT_STREAM stream(password, TxCrypto, this);
340 stream.Put(text.c_str(), text.length() + 1, true);
343 errorMsg = SEND_DATA_ERROR;
349 //---------------------------------------------------------------------------
350 int NetTransact::RxDataAnswer(Callback callback, void* data)
352 ReadState state = {false, callback, data, this};
353 STG::DECRYPT_STREAM stream(password, RxCrypto, &state);
357 ssize_t res = read(sock, buffer, sizeof(buffer));
360 errorMsg = RECV_DATA_ANSWER_ERROR;
363 stream.Put(buffer, res, res == 0);
365 return st_xml_parse_error;
370 //---------------------------------------------------------------------------
371 bool NetTransact::TxCrypto(const void* block, size_t size, void* data)
373 assert(data != NULL);
374 auto& nt = *static_cast<NetTransact*>(data);
375 if (!WriteAll(nt.sock, block, size))
379 //---------------------------------------------------------------------------
380 bool NetTransact::RxCrypto(const void* block, size_t size, void* data)
382 assert(data != NULL);
383 auto& state = *static_cast<ReadState *>(data);
385 const char* buffer = static_cast<const char *>(block);
386 for (size_t pos = 0; pos < size; ++pos)
387 if (buffer[pos] == 0)
390 size = pos; // Adjust string size
394 if (!state.callback(std::string(buffer, size), state.last, state.callbackData))