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 //---------------------------------------------------------------------------
40 #include <arpa/inet.h>
50 #include "stg/common.h"
54 #define IA_CONNECT (1)
55 #define IA_DISCONNECT (2)
57 #define IA_DEBUGPROTO 1
60 //---------------------------------------------------------------------------
61 //---------------------------------------------------------------------------
62 //---------------------------------------------------------------------------
67 long long res = ms * 1000000;
68 struct timespec ts = {res / 1000000000, res % 1000000000};
71 //---------------------------------------------------------------------------
72 void * RunL(void * data)
75 sigfillset(&signalSet);
76 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
78 IA_CLIENT_PROT * c = (IA_CLIENT_PROT *)data;
87 while (c->GetNonstop())
93 //---------------------------------------------------------------------------
97 gettimeofday(&tv, NULL);
98 return tv.tv_sec*1000 + tv.tv_usec/1000;
101 //---------------------------------------------------------------------------
102 unsigned long WINAPI RunW(void * data)
104 IA_CLIENT_PROT * c = (IA_CLIENT_PROT *)data;
105 while (c->GetNonstop())
109 //---------------------------------------------------------------------------
115 bool HostNameToIP(const std::string & hostName, uint32_t & ip)
117 ip = inet_addr(hostName.c_str());
118 if (ip == INADDR_NONE)
120 hostent * phe = gethostbyname(hostName.c_str());
123 ip = *((uint32_t *)phe->h_addr_list[0]);
136 //---------------------------------------------------------------------------
137 //---------------------------------------------------------------------------
138 //---------------------------------------------------------------------------
139 IA_CLIENT_PROT::IA_CLIENT_PROT(const std::string & sn, unsigned short p,
140 const std::string & ln, uint16_t lp)
146 isNetPrepared(false),
160 pStatusChangedCb(NULL),
161 pStatChangedCb(NULL),
165 statusChangedCbData(NULL),
166 statChangedCbData(NULL),
176 disconnSynAck8(NULL),
180 memset(&stat, 0, sizeof(stat));
183 WSAStartup(MAKEWORD(2, 0), &wsaData);
186 packetTypes["CONN_SYN"] = CONN_SYN_N;
187 packetTypes["CONN_SYN_ACK"] = CONN_SYN_ACK_N;
188 packetTypes["CONN_ACK"] = CONN_ACK_N;
189 packetTypes["ALIVE_SYN"] = ALIVE_SYN_N;
190 packetTypes["ALIVE_ACK"] = ALIVE_ACK_N;
191 packetTypes["DISCONN_SYN"] = DISCONN_SYN_N;
192 packetTypes["DISCONN_SYN_ACK"] = DISCONN_SYN_ACK_N;
193 packetTypes["DISCONN_ACK"] = DISCONN_ACK_N;
194 packetTypes["FIN"] = FIN_N;
195 packetTypes["ERR"] = ERROR_N;
196 packetTypes["INFO"] = INFO_N;
197 packetTypes["INFO_7"] = INFO_7_N;
198 packetTypes["INFO_8"] = INFO_8_N;
200 unsigned char key[IA_PASSWD_LEN];
201 memset(key, 0, IA_PASSWD_LEN);
202 strncpy((char *)key, "pr7Hhen", 8);
203 Blowfish_Init(&ctxHdr, key, IA_PASSWD_LEN);
205 memset(key, 0, IA_PASSWD_LEN);
206 Blowfish_Init(&ctxPass, key, IA_PASSWD_LEN);
208 for (size_t i = 0; i < DIR_NUM; ++i)
210 selectedDirs[i] = false;
213 servAddr.sin_family = AF_INET;
214 servAddr.sin_port = htons(port);
215 servAddr.sin_addr.s_addr = ip;
217 //---------------------------------------------------------------------------
218 void IA_CLIENT_PROT::PrepareNet()
220 /*struct hostent * phe;
223 ip = inet_addr(serverName.c_str());
224 if (ip == INADDR_NONE)
226 phe = gethostbyname(serverName.c_str());
229 ip = *((unsigned long*)phe->h_addr_list[0]);
233 strError = string("Unknown host ") + "\'" + serverName + "\'";
234 codeError = IA_GETHOSTBYNAME_ERROR;
235 if (pErrorCb != NULL)
236 pErrorCb(strError, IA_GETHOSTBYNAME_ERROR, errorCbData);
240 if (!HostNameToIP(serverName, ip))
243 strError = std::string("Unknown host ") + "\'" + serverName + "\'";
244 codeError = IA_GETHOSTBYNAME_ERROR;
245 if (pErrorCb != NULL)
246 pErrorCb(strError, IA_GETHOSTBYNAME_ERROR, errorCbData);
256 sockr = socket(AF_INET, SOCK_DGRAM, 0);
258 struct sockaddr_in localAddrR;
259 localAddrR.sin_family = AF_INET;
262 localAddrR.sin_port = htons(localPort);
264 localAddrR.sin_port = htons(port);
266 if (!localName.empty())
268 if (!HostNameToIP(localName, localIP))
270 strError = std::string("Unknown host ") + "\'" + serverName + "\'";
271 codeError = IA_GETHOSTBYNAME_ERROR;
272 if (pErrorCb != NULL)
273 pErrorCb(strError, IA_GETHOSTBYNAME_ERROR, errorCbData);
274 localIP = INADDR_ANY;
279 localIP = INADDR_ANY;
282 localAddrR.sin_addr.s_addr = localIP;
284 servAddr.sin_family = AF_INET;
285 servAddr.sin_port = htons(port);
286 servAddr.sin_addr.s_addr = ip;
288 int res = bind(sockr, (struct sockaddr*)&localAddrR, sizeof(localAddrR));
291 strError = "bind error";
292 codeError = IA_BIND_ERROR;
293 if (pErrorCb != NULL)
294 pErrorCb(strError, IA_BIND_ERROR, errorCbData);
299 unsigned long arg = 1;
300 ioctlsocket(sockr, FIONBIO, &arg);
302 if (0 != fcntl(sockr, F_SETFL, O_NONBLOCK))
304 strError = "fcntl error";
305 codeError = IA_FCNTL_ERROR;
306 if (pErrorCb != NULL)
307 pErrorCb(strError, IA_FCNTL_ERROR, errorCbData);
312 //---------------------------------------------------------------------------
313 IA_CLIENT_PROT::~IA_CLIENT_PROT()
322 //---------------------------------------------------------------------------
323 int IA_CLIENT_PROT::DeterminatePacketType(const char * buffer)
325 std::map<std::string, int>::iterator pi;
326 pi = packetTypes.find(buffer);
327 if (pi == packetTypes.end())
336 //---------------------------------------------------------------------------
337 void IA_CLIENT_PROT::FillHdr8(char * buffer, unsigned long)
339 strncpy(buffer, IA_ID, 6);
340 buffer[IA_MAGIC_LEN] = 0;
341 buffer[IA_MAGIC_LEN + 1] = IA_PROTO_VER;
342 strncpy(buffer + sizeof(HDR_8), login.c_str(), IA_LOGIN_LEN);
344 //---------------------------------------------------------------------------
345 int IA_CLIENT_PROT::Send(char * buffer, int len)
350 isNetPrepared = true;
353 int db = sizeof(HDR_8);
354 EncryptString(buffer + db, buffer + db, IA_LOGIN_LEN, &ctxHdr);
357 int encLen = (len - sizeof(HDR_8) - IA_LOGIN_LEN);
358 EncryptString(buffer + db, buffer + db, encLen, &ctxPass);
360 return sendto(sockr, buffer, len, 0, (struct sockaddr*)&servAddr, sizeof(servAddr));
362 //---------------------------------------------------------------------------
363 int IA_CLIENT_PROT::Recv(char * buffer, int len)
371 struct sockaddr_in addr;
372 fromLen = sizeof(addr);
373 int res = recvfrom(sockr, buffer, len, 0, (struct sockaddr*)&addr, &fromLen);
378 if (strcmp(buffer + 4 + sizeof(HDR_8), "ERR"))
379 DecryptString(buffer, buffer, len, &ctxPass);
383 //---------------------------------------------------------------------------
384 int IA_CLIENT_PROT::NetSend(int n)
389 memset(buffer, 0, 2048);
394 msgLen = Prepare_CONN_SYN_8(buffer);
398 msgLen = Prepare_CONN_ACK_8(buffer);
402 msgLen = Prepare_ALIVE_ACK_8(buffer);
406 msgLen = Prepare_DISCONN_SYN_8(buffer);
410 msgLen = Prepare_DISCONN_ACK_8(buffer);
418 Send(buffer, msgLen);
422 //---------------------------------------------------------------------------
423 int IA_CLIENT_PROT::NetRecv()
427 if (Recv(buffer, sizeof(buffer)) < 0)
431 strncpy(packetName, buffer + 12, sizeof(packetName));
432 packetName[sizeof(packetName) - 1] = 0;
433 int pn = DeterminatePacketType(buffer + 12);
439 ret = Process_CONN_SYN_ACK_8(buffer);
443 ret = Process_ALIVE_SYN_8(buffer);
446 case DISCONN_SYN_ACK_N:
447 ret = Process_DISCONN_SYN_ACK_8(buffer);
451 ret = Process_FIN_8(buffer);
455 ret = Process_INFO_8(buffer);
459 ret = Process_ERROR(buffer);
467 //---------------------------------------------------------------------------
468 void IA_CLIENT_PROT::Start()
473 CreateThread(NULL, 16384, RunW, this, 0, &pt);
475 pthread_create(&thread, NULL, RunL, this);
478 //---------------------------------------------------------------------------
479 void IA_CLIENT_PROT::Stop()
483 //---------------------------------------------------------------------------
484 void IA_CLIENT_PROT::Run()
491 if (action == IA_CONNECT)
496 phaseTime = GetTickCount();
498 if (reconnect && !firstConnect)
505 if ((int)(GetTickCount() - phaseTime)/1000 > aliveTimeout)
508 phaseTime = GetTickCount();
509 if (pStatusChangedCb != NULL)
510 pStatusChangedCb(0, statusChangedCbData);
513 if (action == IA_DISCONNECT)
516 NetSend(DISCONN_SYN_N);
518 phaseTime = GetTickCount();
524 if ((int)(GetTickCount() - phaseTime)/1000 > userTimeout)
527 phaseTime = GetTickCount();
528 if (pStatusChangedCb != NULL)
529 pStatusChangedCb(0, statusChangedCbData);
530 firstConnect = false;
533 if (action == IA_DISCONNECT)
536 NetSend(DISCONN_SYN_N);
538 phaseTime = GetTickCount();
544 if ((int)(GetTickCount() - phaseTime)/1000 > aliveTimeout)
547 phaseTime = GetTickCount();
548 if (pStatusChangedCb != NULL)
549 pStatusChangedCb(0, statusChangedCbData);
552 if (action == IA_CONNECT)
557 phaseTime = GetTickCount();
563 if ((int)(GetTickCount() - phaseTime)/1000 > aliveTimeout)
566 phaseTime = GetTickCount();
567 if (pStatusChangedCb != NULL)
568 pStatusChangedCb(0, statusChangedCbData);
571 if (action == IA_CONNECT)
576 phaseTime = GetTickCount();
584 //---------------------------------------------------------------------------
585 void IA_CLIENT_PROT::GetStat(LOADSTAT * ls)
587 memcpy(ls, &stat, sizeof(stat));
589 //---------------------------------------------------------------------------
590 void IA_CLIENT_PROT::SetServer(const std::string & sn, unsigned short p)
596 //---------------------------------------------------------------------------
597 void IA_CLIENT_PROT::SetLogin(const std::string & l)
601 //---------------------------------------------------------------------------
602 void IA_CLIENT_PROT::SetPassword(const std::string & p)
606 unsigned char keyL[IA_PASSWD_LEN];
607 memset(keyL, 0, IA_PASSWD_LEN);
608 strncpy((char *)keyL, password.c_str(), IA_PASSWD_LEN);
609 Blowfish_Init(&ctxPass, keyL, IA_PASSWD_LEN);
611 //---------------------------------------------------------------------------
612 void IA_CLIENT_PROT::SetEnabledDirs(const bool * selectedDirs)
614 memcpy(IA_CLIENT_PROT::selectedDirs, selectedDirs, sizeof(bool) * DIR_NUM);
616 //---------------------------------------------------------------------------
617 int IA_CLIENT_PROT::Connect()
622 //---------------------------------------------------------------------------
623 int IA_CLIENT_PROT::Disconnect()
626 action = IA_DISCONNECT;
629 //---------------------------------------------------------------------------
630 int IA_CLIENT_PROT::GetStrError(std::string * error) const
638 //---------------------------------------------------------------------------
639 int IA_CLIENT_PROT::Process_CONN_SYN_ACK_8(const char * buffer)
641 std::vector<std::string> dirNames;
642 connSynAck8 = (CONN_SYN_ACK_8*)buffer;
645 SwapBytes(connSynAck8->len);
646 SwapBytes(connSynAck8->rnd);
647 SwapBytes(connSynAck8->userTimeOut);
648 SwapBytes(connSynAck8->aliveDelay);
651 rnd = connSynAck8->rnd;
652 userTimeout = connSynAck8->userTimeOut;
653 aliveTimeout = connSynAck8->aliveDelay;
655 for (int i = 0; i < DIR_NUM; i++)
657 dirNames.push_back((const char*)connSynAck8->dirName[i]);
660 if (pDirNameCb != NULL)
661 pDirNameCb(dirNames, dirNameCbData);
665 phaseTime = GetTickCount();
667 return CONN_SYN_ACK_N;
669 //---------------------------------------------------------------------------
670 int IA_CLIENT_PROT::Process_ALIVE_SYN_8(const char * buffer)
672 aliveSyn8 = (ALIVE_SYN_8*)buffer;
675 SwapBytes(aliveSyn8->len);
676 SwapBytes(aliveSyn8->rnd);
677 SwapBytes(aliveSyn8->cash);
678 SwapBytes(aliveSyn8->status);
679 for (int i = 0; i < DIR_NUM; ++i)
681 SwapBytes(aliveSyn8->mu[i]);
682 SwapBytes(aliveSyn8->md[i]);
683 SwapBytes(aliveSyn8->su[i]);
684 SwapBytes(aliveSyn8->sd[i]);
688 rnd = aliveSyn8->rnd;
689 memcpy(&stat, (char*)aliveSyn8->mu, sizeof(stat));
691 if (pStatChangedCb != NULL)
692 pStatChangedCb(stat, statChangedCbData);
694 if (pStatusChangedCb != NULL)
695 pStatusChangedCb(1, statusChangedCbData);
696 NetSend(ALIVE_ACK_N);
697 phaseTime = GetTickCount();
701 //---------------------------------------------------------------------------
702 int IA_CLIENT_PROT::Process_DISCONN_SYN_ACK_8(const char * buffer)
704 disconnSynAck8 = (DISCONN_SYN_ACK_8*)buffer;
707 SwapBytes(disconnSynAck8->len);
708 SwapBytes(disconnSynAck8->rnd);
711 rnd = disconnSynAck8->rnd;
713 NetSend(DISCONN_ACK_N);
715 phaseTime = GetTickCount();
717 return DISCONN_SYN_ACK_N;
719 //---------------------------------------------------------------------------
720 int IA_CLIENT_PROT::Process_FIN_8(const char *)
723 phaseTime = GetTickCount();
724 if (pStatusChangedCb != NULL)
725 pStatusChangedCb(0, statusChangedCbData);
729 //---------------------------------------------------------------------------
730 int IA_CLIENT_PROT::Process_INFO_8(const char * buffer)
732 info = (INFO_8*)buffer;
735 SwapBytes(info->len);
736 SwapBytes(info->sendTime);
740 pInfoCb((char*)info->text, info->infoType, info->showTime, info->sendTime, infoCbData);
743 //---------------------------------------------------------------------------
744 int IA_CLIENT_PROT::Process_ERROR(const char * buffer)
747 memcpy(&err, buffer, sizeof(err));
753 KOIToWin((const char*)err.text, &messageText);
754 if (pErrorCb != NULL)
755 pErrorCb(messageText, IA_SERVER_ERROR, errorCbData);
757 phaseTime = GetTickCount();
758 codeError = IA_SERVER_ERROR;
762 //---------------------------------------------------------------------------
763 int IA_CLIENT_PROT::Prepare_CONN_SYN_8(char * buffer)
765 connSyn8 = (CONN_SYN_8*)buffer;
767 assert(sizeof(CONN_SYN_8) == Min8(sizeof(CONN_SYN_8)) && "CONN_SYN_8 is not aligned to 8 bytes");
769 connSyn8->len = sizeof(CONN_SYN_8);
772 SwapBytes(connSyn8->len);
775 strncpy((char*)connSyn8->type, "CONN_SYN", IA_MAX_TYPE_LEN);
776 strncpy((char*)connSyn8->login, login.c_str(), IA_LOGIN_LEN);
778 for (int i = 0; i < DIR_NUM; i++)
780 connSyn8->dirs |= (selectedDirs[i] << i);
782 return sizeof(CONN_SYN_8);
784 //---------------------------------------------------------------------------
785 int IA_CLIENT_PROT::Prepare_CONN_ACK_8(char * buffer)
787 connAck8 = (CONN_ACK_8*)buffer;
789 assert(sizeof(CONN_ACK_8) == Min8(sizeof(CONN_ACK_8)) && "CONN_ACK_8 is not aligned to 8 bytes");
791 connAck8->len = sizeof(CONN_ACK_8);
792 strncpy((char*)connAck8->loginS, login.c_str(), IA_LOGIN_LEN);
793 strncpy((char*)connAck8->type, "CONN_ACK", IA_MAX_TYPE_LEN);
798 SwapBytes(connAck8->len);
799 SwapBytes(connAck8->rnd);
802 return sizeof(CONN_ACK_8);
804 //---------------------------------------------------------------------------
805 int IA_CLIENT_PROT::Prepare_ALIVE_ACK_8(char * buffer)
807 aliveAck8 = (ALIVE_ACK_8*)buffer;
809 assert(Min8(sizeof(ALIVE_ACK_8)) == sizeof(ALIVE_ACK_8) && "ALIVE_ACK_8 is not aligned to 8 bytes");
811 aliveAck8 = (ALIVE_ACK_8*)buffer;
812 aliveAck8->len = sizeof(ALIVE_ACK_8);
813 strncpy((char*)aliveAck8->loginS, login.c_str(), IA_LOGIN_LEN);
814 strncpy((char*)aliveAck8->type, "ALIVE_ACK", IA_MAX_TYPE_LEN);
815 aliveAck8->rnd = ++rnd;
818 SwapBytes(aliveAck8->len);
819 SwapBytes(aliveAck8->rnd);
822 return sizeof(ALIVE_ACK_8);
824 //---------------------------------------------------------------------------
825 int IA_CLIENT_PROT::Prepare_DISCONN_SYN_8(char * buffer)
827 disconnSyn8 = (DISCONN_SYN_8*)buffer;
829 assert(Min8(sizeof(DISCONN_SYN_8)) == sizeof(DISCONN_SYN_8) && "DISCONN_SYN_8 is not aligned to 8 bytes");
831 disconnSyn8->len = sizeof(DISCONN_SYN_8);
834 SwapBytes(disconnSyn8->len);
837 strncpy((char*)disconnSyn8->loginS, login.c_str(), IA_LOGIN_LEN);
838 strncpy((char*)disconnSyn8->type, "DISCONN_SYN", IA_MAX_TYPE_LEN);
839 strncpy((char*)disconnSyn8->login, login.c_str(), IA_LOGIN_LEN);
840 return sizeof(DISCONN_SYN_8);
842 //---------------------------------------------------------------------------
843 int IA_CLIENT_PROT::Prepare_DISCONN_ACK_8(char * buffer)
845 disconnAck8 = (DISCONN_ACK_8*)buffer;
847 assert(Min8(sizeof(DISCONN_ACK_8)) == sizeof(DISCONN_ACK_8) && "DISCONN_ACK_8 is not aligned to 8 bytes");
849 disconnAck8->len = Min8(sizeof(DISCONN_ACK_8));
850 disconnAck8->rnd = rnd + 1;
853 SwapBytes(disconnAck8->len);
854 SwapBytes(disconnAck8->rnd);
857 strncpy((char*)disconnAck8->loginS, login.c_str(), IA_LOGIN_LEN);
858 strncpy((char*)disconnAck8->type, "DISCONN_ACK", IA_MAX_TYPE_LEN);
859 return Min8(sizeof(DISCONN_ACK_8));
861 //---------------------------------------------------------------------------
862 void IA_CLIENT_PROT::SetStatusChangedCb(tpStatusChangedCb p, void * data)
864 pStatusChangedCb = p;
865 statusChangedCbData = data;
867 //---------------------------------------------------------------------------
868 void IA_CLIENT_PROT::SetStatChangedCb(tpStatChangedCb p, void * data)
871 statChangedCbData = data;
873 //---------------------------------------------------------------------------
874 void IA_CLIENT_PROT::SetInfoCb(tpCallBackInfoFn p, void * data)
879 //---------------------------------------------------------------------------
880 void IA_CLIENT_PROT::SetDirNameCb(tpCallBackDirNameFn p, void * data)
883 dirNameCbData = data;
885 //---------------------------------------------------------------------------
886 void IA_CLIENT_PROT::SetErrorCb(tpCallBackErrorFn p, void * data)
891 //---------------------------------------------------------------------------