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 struct hostent * 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 struct hostent * 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;
256 errorMsg = UNKNOWN_ERROR;
257 return st_unknown_err;
260 //---------------------------------------------------------------------------
261 int NETTRANSACT::TxLogin()
263 char loginZ[ADM_LOGIN_LEN + 1];
264 memset(loginZ, 0, ADM_LOGIN_LEN + 1);
265 strncpy(loginZ, login.c_str(), ADM_LOGIN_LEN);
267 if (!WriteAll(sock, loginZ, ADM_LOGIN_LEN))
269 errorMsg = SEND_LOGIN_ERROR;
275 //---------------------------------------------------------------------------
276 int NETTRANSACT::RxLoginAnswer()
278 char buffer[sizeof(OK_LOGIN) + 1];
280 if (!ReadAll(sock, buffer, strlen(OK_LOGIN)))
282 errorMsg = RECV_LOGIN_ANSWER_ERROR;
286 if (strncmp(OK_LOGIN, buffer, strlen(OK_LOGIN)) == 0)
289 if (strncmp(ERR_LOGIN, buffer, strlen(ERR_LOGIN)) == 0)
291 errorMsg = INCORRECT_LOGIN;
296 errorMsg = UNKNOWN_ERROR;
297 return st_unknown_err;
300 //---------------------------------------------------------------------------
301 int NETTRANSACT::TxLoginS()
303 char loginZ[ADM_LOGIN_LEN + 1];
304 memset(loginZ, 0, ADM_LOGIN_LEN + 1);
307 InitContext(password.c_str(), PASSWD_LEN, &ctx);
308 EncryptString(loginZ, login.c_str(), std::min<size_t>(login.length() + 1, ADM_LOGIN_LEN), &ctx);
309 if (!WriteAll(sock, loginZ, ADM_LOGIN_LEN))
311 errorMsg = SEND_LOGIN_ERROR;
317 //---------------------------------------------------------------------------
318 int NETTRANSACT::RxLoginSAnswer()
320 char buffer[sizeof(OK_LOGINS) + 1];
322 if (!ReadAll(sock, buffer, strlen(OK_LOGINS)))
324 errorMsg = RECV_LOGIN_ANSWER_ERROR;
328 if (strncmp(OK_LOGINS, buffer, strlen(OK_LOGINS)) == 0)
331 if (strncmp(ERR_LOGINS, buffer, strlen(ERR_LOGINS)) == 0)
333 errorMsg = INCORRECT_LOGIN;
334 return st_logins_err;
338 errorMsg = UNKNOWN_ERROR;
339 return st_unknown_err;
342 //---------------------------------------------------------------------------
343 int NETTRANSACT::TxData(const std::string & text)
345 STG::ENCRYPT_STREAM stream(password, TxCrypto, this);
346 stream.Put(text.c_str(), text.length() + 1, true);
349 errorMsg = SEND_DATA_ERROR;
355 //---------------------------------------------------------------------------
356 int NETTRANSACT::RxDataAnswer(CALLBACK callback, void * data)
358 ReadState state = {false, callback, data, this};
359 STG::DECRYPT_STREAM stream(password, RxCrypto, &state);
363 ssize_t res = read(sock, buffer, sizeof(buffer));
366 errorMsg = RECV_DATA_ANSWER_ERROR;
369 stream.Put(buffer, res, res == 0);
371 return st_xml_parse_error;
376 //---------------------------------------------------------------------------
377 bool NETTRANSACT::TxCrypto(const void * block, size_t size, void * data)
379 assert(data != NULL);
380 NETTRANSACT & nt = *static_cast<NETTRANSACT *>(data);
381 if (!WriteAll(nt.sock, block, size))
385 //---------------------------------------------------------------------------
386 bool NETTRANSACT::RxCrypto(const void * block, size_t size, void * data)
388 assert(data != NULL);
389 ReadState & state = *static_cast<ReadState *>(data);
391 const char * buffer = static_cast<const char *>(block);
392 for (size_t pos = 0; pos < size; ++pos)
393 if (buffer[pos] == 0)
396 size = pos; // Adjust string size
400 if (!state.callback(std::string(buffer, size), state.final, state.callbackData))