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 1, or (at your option)
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Date: 2010/04/16 11:28:03 $
25 * Boris Mikhailenko <stg34@stargazer.dp.ua>
26 * Maxim Mamontov <faust@stargazer.dp.ua>
27 * Andrey Rakhmanov <andrey_rakhmanov@yahoo.com> - bugfixes.
30 //---------------------------------------------------------------------------
32 #include "stg/common.h"
49 #include <arpa/inet.h>
55 #define IA_CONNECT (1)
56 #define IA_DISCONNECT (2)
58 #define IA_DEBUGPROTO 1
61 //---------------------------------------------------------------------------
62 //---------------------------------------------------------------------------
63 //---------------------------------------------------------------------------
66 //---------------------------------------------------------------------------
70 gettimeofday(&tv, NULL);
71 return tv.tv_sec*1000 + tv.tv_usec/1000;
74 //---------------------------------------------------------------------------
79 bool HostNameToIP(const std::string & hostName, uint32_t & ip)
81 ip = inet_addr(hostName.c_str());
82 if (ip == INADDR_NONE)
84 hostent * phe = gethostbyname(hostName.c_str());
87 ip = *reinterpret_cast<uint32_t*>(phe->h_addr_list[0]);
100 //---------------------------------------------------------------------------
101 //---------------------------------------------------------------------------
102 //---------------------------------------------------------------------------
103 IA_CLIENT_PROT::IA_CLIENT_PROT(const std::string & sn, unsigned short p,
104 const std::string & ln, uint16_t lp)
109 m_isNetPrepared(false),
116 m_firstConnect(true),
123 m_pStatusChangedCb(NULL),
124 m_pStatChangedCb(NULL),
128 m_statusChangedCbData(NULL),
129 m_statChangedCbData(NULL),
132 m_dirNameCbData(NULL),
139 m_disconnSynAck8(NULL),
143 memset(&m_stat, 0, sizeof(m_stat));
146 WSAStartup(MAKEWORD(2, 0), &m_wsaData);
149 m_packetTypes["CONN_SYN"] = CONN_SYN_N;
150 m_packetTypes["CONN_SYN_ACK"] = CONN_SYN_ACK_N;
151 m_packetTypes["CONN_ACK"] = CONN_ACK_N;
152 m_packetTypes["ALIVE_SYN"] = ALIVE_SYN_N;
153 m_packetTypes["ALIVE_ACK"] = ALIVE_ACK_N;
154 m_packetTypes["DISCONN_SYN"] = DISCONN_SYN_N;
155 m_packetTypes["DISCONN_SYN_ACK"] = DISCONN_SYN_ACK_N;
156 m_packetTypes["DISCONN_ACK"] = DISCONN_ACK_N;
157 m_packetTypes["FIN"] = FIN_N;
158 m_packetTypes["ERR"] = ERROR_N;
159 m_packetTypes["INFO"] = INFO_N;
160 m_packetTypes["INFO_7"] = INFO_7_N;
161 m_packetTypes["INFO_8"] = INFO_8_N;
163 char key[IA_PASSWD_LEN];
164 memset(key, 0, IA_PASSWD_LEN);
165 strncpy(key, "pr7Hhen", 8);
166 Blowfish_Init(&m_ctxHdr, key, IA_PASSWD_LEN);
168 memset(key, 0, IA_PASSWD_LEN);
169 Blowfish_Init(&m_ctxPass, key, IA_PASSWD_LEN);
171 for (size_t i = 0; i < DIR_NUM; ++i)
172 m_selectedDirs[i] = false;
174 m_servAddr.sin_family = AF_INET;
175 m_servAddr.sin_port = htons(m_port);
176 m_servAddr.sin_addr.s_addr = m_ip;
178 //---------------------------------------------------------------------------
179 void IA_CLIENT_PROT::PrepareNet()
181 /*struct hostent * phe;
184 ip = inet_addr(serverName.c_str());
185 if (ip == INADDR_NONE)
187 phe = gethostbyname(serverName.c_str());
190 ip = *((unsigned long*)phe->h_addr_list[0]);
194 strError = string("Unknown host ") + "\'" + serverName + "\'";
195 codeError = IA_GETHOSTBYNAME_ERROR;
196 if (pErrorCb != NULL)
197 pErrorCb(strError, IA_GETHOSTBYNAME_ERROR, errorCbData);
201 if (!HostNameToIP(m_serverName, m_ip))
204 m_strError = std::string("Unknown host ") + "\'" + m_serverName + "\'";
205 m_codeError = IA_GETHOSTBYNAME_ERROR;
206 if (m_pErrorCb != NULL)
207 m_pErrorCb(m_strError, IA_GETHOSTBYNAME_ERROR, m_errorCbData);
214 closesocket(m_sockr);
217 m_sockr = socket(AF_INET, SOCK_DGRAM, 0);
219 struct sockaddr_in localAddrR;
220 localAddrR.sin_family = AF_INET;
223 localAddrR.sin_port = htons(m_localPort);
225 localAddrR.sin_port = htons(m_port);
227 if (!m_localName.empty())
229 if (!HostNameToIP(m_localName, m_localIP))
231 m_strError = std::string("Unknown host ") + "\'" + m_serverName + "\'";
232 m_codeError = IA_GETHOSTBYNAME_ERROR;
233 if (m_pErrorCb != NULL)
234 m_pErrorCb(m_strError, IA_GETHOSTBYNAME_ERROR, m_errorCbData);
235 m_localIP = INADDR_ANY;
240 m_localIP = INADDR_ANY;
243 localAddrR.sin_addr.s_addr = m_localIP;
245 m_servAddr.sin_family = AF_INET;
246 m_servAddr.sin_port = htons(m_port);
247 m_servAddr.sin_addr.s_addr = m_ip;
249 int res = bind(m_sockr, reinterpret_cast<sockaddr*>(&localAddrR), sizeof(localAddrR));
252 m_strError = "bind error";
253 m_codeError = IA_BIND_ERROR;
254 if (m_pErrorCb != NULL)
255 m_pErrorCb(m_strError, IA_BIND_ERROR, m_errorCbData);
260 unsigned long arg = 1;
261 ioctlsocket(m_sockr, FIONBIO, &arg);
263 if (0 != fcntl(m_sockr, F_SETFL, O_NONBLOCK))
265 m_strError = "fcntl error";
266 m_codeError = IA_FCNTL_ERROR;
267 if (m_pErrorCb != NULL)
268 m_pErrorCb(m_strError, IA_FCNTL_ERROR, m_errorCbData);
273 //---------------------------------------------------------------------------
274 IA_CLIENT_PROT::~IA_CLIENT_PROT()
279 closesocket(m_sockr);
283 //---------------------------------------------------------------------------
284 int IA_CLIENT_PROT::DeterminatePacketType(const char * buffer)
286 std::map<std::string, int>::iterator pi;
287 pi = m_packetTypes.find(buffer);
288 if (pi == m_packetTypes.end())
297 //---------------------------------------------------------------------------
298 void IA_CLIENT_PROT::FillHdr8(char* buffer, unsigned long)
300 strncpy(buffer, IA_ID, 6);
301 buffer[IA_MAGIC_LEN] = 0;
302 buffer[IA_MAGIC_LEN + 1] = IA_PROTO_VER;
303 strncpy(buffer + sizeof(HDR_8), m_login.c_str(), IA_LOGIN_LEN);
305 //---------------------------------------------------------------------------
306 int IA_CLIENT_PROT::Send(char * buffer, int len)
308 if (!m_isNetPrepared)
311 m_isNetPrepared = true;
314 int db = sizeof(HDR_8);
315 EncryptString(buffer + db, buffer + db, IA_LOGIN_LEN, &m_ctxHdr);
318 int encLen = (len - sizeof(HDR_8) - IA_LOGIN_LEN);
319 EncryptString(buffer + db, buffer + db, encLen, &m_ctxPass);
321 return sendto(m_sockr, buffer, len, 0, reinterpret_cast<sockaddr*>(&m_servAddr), sizeof(m_servAddr));
323 //---------------------------------------------------------------------------
324 int IA_CLIENT_PROT::Recv(char * buffer, int len)
332 struct sockaddr_in addr;
333 fromLen = sizeof(addr);
334 int res = recvfrom(m_sockr, buffer, len, 0, reinterpret_cast<sockaddr*>(&addr), &fromLen);
339 if (strcmp(buffer + 4 + sizeof(HDR_8), "ERR"))
340 DecryptString(buffer, buffer, len, &m_ctxPass);
344 //---------------------------------------------------------------------------
345 int IA_CLIENT_PROT::NetSend(int n)
350 memset(buffer, 0, 2048);
355 msgLen = Prepare_CONN_SYN_8(buffer);
359 msgLen = Prepare_CONN_ACK_8(buffer);
363 msgLen = Prepare_ALIVE_ACK_8(buffer);
367 msgLen = Prepare_DISCONN_SYN_8(buffer);
371 msgLen = Prepare_DISCONN_ACK_8(buffer);
379 Send(buffer, msgLen);
383 //---------------------------------------------------------------------------
384 int IA_CLIENT_PROT::NetRecv()
388 if (Recv(buffer, sizeof(buffer)) < 0)
392 strncpy(packetName, buffer + 12, sizeof(packetName));
393 packetName[sizeof(packetName) - 1] = 0;
394 int pn = DeterminatePacketType(buffer + 12);
400 ret = Process_CONN_SYN_ACK_8(buffer);
404 ret = Process_ALIVE_SYN_8(buffer);
407 case DISCONN_SYN_ACK_N:
408 ret = Process_DISCONN_SYN_ACK_8(buffer);
412 ret = Process_FIN_8(buffer);
416 ret = Process_INFO_8(buffer);
420 ret = Process_ERROR(buffer);
428 //---------------------------------------------------------------------------
429 void IA_CLIENT_PROT::Start()
431 m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
433 //---------------------------------------------------------------------------
434 void IA_CLIENT_PROT::Stop()
436 if (m_thread.joinable())
437 m_thread.request_stop();
439 //---------------------------------------------------------------------------
440 void IA_CLIENT_PROT::Run(std::stop_token token)
442 while (!token.stop_requested())
449 if (m_action == IA_CONNECT)
454 m_phaseTime = GetTickCount();
456 if (m_reconnect && !m_firstConnect)
458 m_action = IA_CONNECT;
463 if (static_cast<int>(GetTickCount() - m_phaseTime)/1000 > m_aliveTimeout)
466 m_phaseTime = GetTickCount();
467 if (m_pStatusChangedCb != NULL)
468 m_pStatusChangedCb(0, m_statusChangedCbData);
471 if (m_action == IA_DISCONNECT)
474 NetSend(DISCONN_SYN_N);
476 m_phaseTime = GetTickCount();
482 if (static_cast<int>(GetTickCount() - m_phaseTime)/1000 > m_userTimeout)
485 m_phaseTime = GetTickCount();
486 if (m_pStatusChangedCb != NULL)
487 m_pStatusChangedCb(0, m_statusChangedCbData);
488 m_firstConnect = false;
491 if (m_action == IA_DISCONNECT)
494 NetSend(DISCONN_SYN_N);
496 m_phaseTime = GetTickCount();
502 if (static_cast<int>(GetTickCount() - m_phaseTime)/1000 > m_aliveTimeout)
505 m_phaseTime = GetTickCount();
506 if (m_pStatusChangedCb != NULL)
507 m_pStatusChangedCb(0, m_statusChangedCbData);
510 if (m_action == IA_CONNECT)
515 m_phaseTime = GetTickCount();
521 if (static_cast<int>(GetTickCount() - m_phaseTime)/1000 > m_aliveTimeout)
524 m_phaseTime = GetTickCount();
525 if (m_pStatusChangedCb != NULL)
526 m_pStatusChangedCb(0, m_statusChangedCbData);
529 if (m_action == IA_CONNECT)
534 m_phaseTime = GetTickCount();
539 std::this_thread::sleep_for(std::chrono::milliseconds(20));
542 //---------------------------------------------------------------------------
543 void IA_CLIENT_PROT::GetStat(LOADSTAT * ls)
545 memcpy(ls, &m_stat, sizeof(m_stat));
547 //---------------------------------------------------------------------------
548 void IA_CLIENT_PROT::SetServer(const std::string & sn, unsigned short p)
554 //---------------------------------------------------------------------------
555 void IA_CLIENT_PROT::SetLogin(const std::string & l)
559 //---------------------------------------------------------------------------
560 void IA_CLIENT_PROT::SetPassword(const std::string & p)
564 char keyL[IA_PASSWD_LEN];
565 memset(keyL, 0, IA_PASSWD_LEN);
566 strncpy(keyL, m_password.c_str(), IA_PASSWD_LEN);
567 Blowfish_Init(&m_ctxPass, keyL, IA_PASSWD_LEN);
569 //---------------------------------------------------------------------------
570 void IA_CLIENT_PROT::SetEnabledDirs(const bool * selectedDirs)
572 memcpy(m_selectedDirs, selectedDirs, sizeof(bool) * DIR_NUM);
574 //---------------------------------------------------------------------------
575 int IA_CLIENT_PROT::Connect()
577 m_action = IA_CONNECT;
580 //---------------------------------------------------------------------------
581 int IA_CLIENT_PROT::Disconnect()
583 m_firstConnect = true;
584 m_action = IA_DISCONNECT;
587 //---------------------------------------------------------------------------
588 int IA_CLIENT_PROT::GetStrError(std::string * error) const
590 int ret = m_codeError;
596 //---------------------------------------------------------------------------
597 int IA_CLIENT_PROT::Process_CONN_SYN_ACK_8(const void* buffer)
599 std::vector<std::string> dirNames;
600 m_connSynAck8 = static_cast<const CONN_SYN_ACK_8*>(buffer);
603 SwapBytes(m_connSynAck8->len);
604 SwapBytes(m_connSynAck8->rnd);
605 SwapBytes(m_connSynAck8->userTimeOut);
606 SwapBytes(m_connSynAck8->aliveDelay);
609 m_rnd = m_connSynAck8->rnd;
610 m_userTimeout = m_connSynAck8->userTimeOut;
611 m_aliveTimeout = m_connSynAck8->aliveDelay;
613 for (int i = 0; i < DIR_NUM; i++)
614 dirNames.push_back(reinterpret_cast<const char*>(m_connSynAck8->dirName[i]));
616 if (m_pDirNameCb != NULL)
617 m_pDirNameCb(dirNames, m_dirNameCbData);
621 m_phaseTime = GetTickCount();
623 return CONN_SYN_ACK_N;
625 //---------------------------------------------------------------------------
626 int IA_CLIENT_PROT::Process_ALIVE_SYN_8(const void* buffer)
628 m_aliveSyn8 = static_cast<const ALIVE_SYN_8*>(buffer);
631 SwapBytes(m_aliveSyn8->len);
632 SwapBytes(m_aliveSyn8->rnd);
633 SwapBytes(m_aliveSyn8->cash);
634 SwapBytes(m_aliveSyn8->status);
635 for (int i = 0; i < DIR_NUM; ++i)
637 SwapBytes(m_aliveSyn8->mu[i]);
638 SwapBytes(m_aliveSyn8->md[i]);
639 SwapBytes(m_aliveSyn8->su[i]);
640 SwapBytes(m_aliveSyn8->sd[i]);
644 m_rnd = m_aliveSyn8->rnd;
645 memcpy(&m_stat, m_aliveSyn8->mu, sizeof(m_stat));
647 if (m_pStatChangedCb != NULL)
648 m_pStatChangedCb(m_stat, m_statChangedCbData);
650 if (m_pStatusChangedCb != NULL)
651 m_pStatusChangedCb(1, m_statusChangedCbData);
652 NetSend(ALIVE_ACK_N);
653 m_phaseTime = GetTickCount();
657 //---------------------------------------------------------------------------
658 int IA_CLIENT_PROT::Process_DISCONN_SYN_ACK_8(const void* buffer)
660 m_disconnSynAck8 = static_cast<const DISCONN_SYN_ACK_8*>(buffer);
663 SwapBytes(m_disconnSynAck8->len);
664 SwapBytes(m_disconnSynAck8->rnd);
667 m_rnd = m_disconnSynAck8->rnd;
669 NetSend(DISCONN_ACK_N);
671 m_phaseTime = GetTickCount();
673 return DISCONN_SYN_ACK_N;
675 //---------------------------------------------------------------------------
676 int IA_CLIENT_PROT::Process_FIN_8(const void*)
679 m_phaseTime = GetTickCount();
680 if (m_pStatusChangedCb != NULL)
681 m_pStatusChangedCb(0, m_statusChangedCbData);
685 //---------------------------------------------------------------------------
686 int IA_CLIENT_PROT::Process_INFO_8(const void* buffer)
688 m_info = static_cast<const INFO_8*>(buffer);
691 SwapBytes(m_info->len);
692 SwapBytes(m_info->sendTime);
695 if (m_pInfoCb != NULL)
696 m_pInfoCb(reinterpret_cast<const char*>(m_info->text), m_info->infoType, m_info->showTime, m_info->sendTime, m_infoCbData);
699 //---------------------------------------------------------------------------
700 int IA_CLIENT_PROT::Process_ERROR(const void* buffer)
703 memcpy(&err, buffer, sizeof(err));
709 KOIToWin(reinterpret_cast<const char*>(err.text), &m_messageText);
710 if (m_pErrorCb != NULL)
711 m_pErrorCb(m_messageText, IA_SERVER_ERROR, m_errorCbData);
713 m_phaseTime = GetTickCount();
714 m_codeError = IA_SERVER_ERROR;
718 //---------------------------------------------------------------------------
719 int IA_CLIENT_PROT::Prepare_CONN_SYN_8(void* buffer)
721 m_connSyn8 = static_cast<CONN_SYN_8*>(buffer);
723 assert(sizeof(CONN_SYN_8) == Min8(sizeof(CONN_SYN_8)) && "CONN_SYN_8 is not aligned to 8 bytes");
725 m_connSyn8->len = sizeof(CONN_SYN_8);
728 SwapBytes(m_connSyn8->len);
731 strncpy(reinterpret_cast<char*>(m_connSyn8->type), "CONN_SYN", IA_MAX_TYPE_LEN);
732 strncpy(reinterpret_cast<char*>(m_connSyn8->login), m_login.c_str(), IA_LOGIN_LEN);
733 m_connSyn8->dirs = 0;
734 for (int i = 0; i < DIR_NUM; i++)
735 m_connSyn8->dirs |= (m_selectedDirs[i] << i);
736 return sizeof(CONN_SYN_8);
738 //---------------------------------------------------------------------------
739 int IA_CLIENT_PROT::Prepare_CONN_ACK_8(void* buffer)
741 m_connAck8 = static_cast<CONN_ACK_8*>(buffer);
743 assert(sizeof(CONN_ACK_8) == Min8(sizeof(CONN_ACK_8)) && "CONN_ACK_8 is not aligned to 8 bytes");
745 m_connAck8->len = sizeof(CONN_ACK_8);
746 strncpy(reinterpret_cast<char*>(m_connAck8->loginS), m_login.c_str(), IA_LOGIN_LEN);
747 strncpy(reinterpret_cast<char*>(m_connAck8->type), "CONN_ACK", IA_MAX_TYPE_LEN);
749 m_connAck8->rnd = m_rnd;
752 SwapBytes(m_connAck8->len);
753 SwapBytes(m_connAck8->rnd);
756 return sizeof(CONN_ACK_8);
758 //---------------------------------------------------------------------------
759 int IA_CLIENT_PROT::Prepare_ALIVE_ACK_8(void* buffer)
761 m_aliveAck8 = static_cast<ALIVE_ACK_8*>(buffer);
763 assert(Min8(sizeof(ALIVE_ACK_8)) == sizeof(ALIVE_ACK_8) && "ALIVE_ACK_8 is not aligned to 8 bytes");
765 m_aliveAck8->len = sizeof(ALIVE_ACK_8);
766 strncpy(reinterpret_cast<char*>(m_aliveAck8->loginS), m_login.c_str(), IA_LOGIN_LEN);
767 strncpy(reinterpret_cast<char*>(m_aliveAck8->type), "ALIVE_ACK", IA_MAX_TYPE_LEN);
768 m_aliveAck8->rnd = ++m_rnd;
771 SwapBytes(m_aliveAck8->len);
772 SwapBytes(m_aliveAck8->rnd);
775 return sizeof(ALIVE_ACK_8);
777 //---------------------------------------------------------------------------
778 int IA_CLIENT_PROT::Prepare_DISCONN_SYN_8(void* buffer)
780 m_disconnSyn8 = static_cast<DISCONN_SYN_8*>(buffer);
782 assert(Min8(sizeof(DISCONN_SYN_8)) == sizeof(DISCONN_SYN_8) && "DISCONN_SYN_8 is not aligned to 8 bytes");
784 m_disconnSyn8->len = sizeof(DISCONN_SYN_8);
787 SwapBytes(m_disconnSyn8->len);
790 strncpy(reinterpret_cast<char*>(m_disconnSyn8->loginS), m_login.c_str(), IA_LOGIN_LEN);
791 strncpy(reinterpret_cast<char*>(m_disconnSyn8->type), "DISCONN_SYN", IA_MAX_TYPE_LEN);
792 strncpy(reinterpret_cast<char*>(m_disconnSyn8->login), m_login.c_str(), IA_LOGIN_LEN);
793 return sizeof(DISCONN_SYN_8);
795 //---------------------------------------------------------------------------
796 int IA_CLIENT_PROT::Prepare_DISCONN_ACK_8(void* buffer)
798 m_disconnAck8 = static_cast<DISCONN_ACK_8*>(buffer);
800 assert(Min8(sizeof(DISCONN_ACK_8)) == sizeof(DISCONN_ACK_8) && "DISCONN_ACK_8 is not aligned to 8 bytes");
802 m_disconnAck8->len = Min8(sizeof(DISCONN_ACK_8));
803 m_disconnAck8->rnd = m_rnd + 1;
806 SwapBytes(m_disconnAck8->len);
807 SwapBytes(m_disconnAck8->rnd);
810 strncpy(reinterpret_cast<char*>(m_disconnAck8->loginS), m_login.c_str(), IA_LOGIN_LEN);
811 strncpy(reinterpret_cast<char*>(m_disconnAck8->type), "DISCONN_ACK", IA_MAX_TYPE_LEN);
812 return Min8(sizeof(DISCONN_ACK_8));
814 //---------------------------------------------------------------------------
815 void IA_CLIENT_PROT::SetStatusChangedCb(tpStatusChangedCb p, void * data)
817 m_pStatusChangedCb = p;
818 m_statusChangedCbData = data;
820 //---------------------------------------------------------------------------
821 void IA_CLIENT_PROT::SetStatChangedCb(tpStatChangedCb p, void * data)
823 m_pStatChangedCb = p;
824 m_statChangedCbData = data;
826 //---------------------------------------------------------------------------
827 void IA_CLIENT_PROT::SetInfoCb(tpCallBackInfoFn p, void * data)
832 //---------------------------------------------------------------------------
833 void IA_CLIENT_PROT::SetDirNameCb(tpCallBackDirNameFn p, void * data)
836 m_dirNameCbData = data;
838 //---------------------------------------------------------------------------
839 void IA_CLIENT_PROT::SetErrorCb(tpCallBackErrorFn p, void * data)
842 m_errorCbData = data;
844 //---------------------------------------------------------------------------