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 //---------------------------------------------------------------------------
45 #include <arpa/inet.h>
49 #include "stg/common.h"
53 #define IA_CONNECT (1)
54 #define IA_DISCONNECT (2)
56 #define IA_DEBUGPROTO 1
59 //---------------------------------------------------------------------------
60 //---------------------------------------------------------------------------
61 //---------------------------------------------------------------------------
64 void * RunL(void * data)
67 IA_CLIENT_PROT * c = (IA_CLIENT_PROT *)data;
76 while (c->GetNonstop())
82 //---------------------------------------------------------------------------
85 long long res = ms * 1000000;
86 struct timespec ts = {res / 1000000000, res % 1000000000};
89 //---------------------------------------------------------------------------
93 gettimeofday(&tv, NULL);
94 return tv.tv_sec*1000 + tv.tv_usec/1000;
97 //---------------------------------------------------------------------------
98 unsigned long WINAPI RunW(void * data)
100 IA_CLIENT_PROT * c = (IA_CLIENT_PROT *)data;
101 while (c->GetNonstop())
105 //---------------------------------------------------------------------------
107 //---------------------------------------------------------------------------
108 //---------------------------------------------------------------------------
109 //---------------------------------------------------------------------------
110 IA_CLIENT_PROT::IA_CLIENT_PROT(const string & sn, unsigned short p, uint16_t localPort)
116 isNetPrepared(false),
121 localPort(localPort),
129 pStatusChangedCb(NULL),
130 pStatChangedCb(NULL),
134 statusChangedCbData(NULL),
135 statChangedCbData(NULL),
145 disconnSynAck8(NULL),
149 memset(&stat, 0, sizeof(stat));
152 WSAStartup(MAKEWORD(2, 0), &wsaData);
155 packetTypes["CONN_SYN"] = CONN_SYN_N;
156 packetTypes["CONN_SYN_ACK"] = CONN_SYN_ACK_N;
157 packetTypes["CONN_ACK"] = CONN_ACK_N;
158 packetTypes["ALIVE_SYN"] = ALIVE_SYN_N;
159 packetTypes["ALIVE_ACK"] = ALIVE_ACK_N;
160 packetTypes["DISCONN_SYN"] = DISCONN_SYN_N;
161 packetTypes["DISCONN_SYN_ACK"] = DISCONN_SYN_ACK_N;
162 packetTypes["DISCONN_ACK"] = DISCONN_ACK_N;
163 packetTypes["FIN"] = FIN_N;
164 packetTypes["ERR"] = ERROR_N;
165 packetTypes["INFO"] = INFO_N;
166 packetTypes["INFO_7"] = INFO_7_N;
167 packetTypes["INFO_8"] = INFO_8_N;
169 unsigned char key[IA_PASSWD_LEN];
170 memset(key, 0, IA_PASSWD_LEN);
171 strncpy((char *)key, "pr7Hhen", 8);
172 Blowfish_Init(&ctxHdr, key, IA_PASSWD_LEN);
174 memset(key, 0, IA_PASSWD_LEN);
175 Blowfish_Init(&ctxPass, key, IA_PASSWD_LEN);
177 for (size_t i = 0; i < DIR_NUM; ++i)
179 selectedDirs[i] = false;
182 servAddr.sin_family = AF_INET;
183 servAddr.sin_port = htons(port);
184 servAddr.sin_addr.s_addr = ip;
186 //---------------------------------------------------------------------------
187 void IA_CLIENT_PROT::PrepareNet()
189 struct hostent * phe;
192 ip = inet_addr(serverName.c_str());
193 if (ip == INADDR_NONE)
195 phe = gethostbyname(serverName.c_str());
198 ip = *((unsigned long*)phe->h_addr_list[0]);
202 strError = string("Unknown host ") + "\'" + serverName + "\'";
203 codeError = IA_GETHOSTBYNAME_ERROR;
204 if (pErrorCb != NULL)
205 pErrorCb(strError, IA_GETHOSTBYNAME_ERROR, errorCbData);
215 sockr = socket(AF_INET, SOCK_DGRAM, 0);
217 struct sockaddr_in localAddrR;
218 localAddrR.sin_family = AF_INET;
221 localAddrR.sin_port = htons(localPort);
223 localAddrR.sin_port = htons(port);
224 localAddrR.sin_addr.s_addr = inet_addr("0.0.0.0");
226 servAddr.sin_family = AF_INET;
227 servAddr.sin_port = htons(port);
228 servAddr.sin_addr.s_addr = ip;
230 int res = bind(sockr, (struct sockaddr*)&localAddrR, sizeof(localAddrR));
233 strError = "bind error";
234 codeError = IA_BIND_ERROR;
235 if (pErrorCb != NULL)
236 pErrorCb(strError, IA_BIND_ERROR, errorCbData);
241 unsigned long arg = 1;
242 res = ioctlsocket(sockr, FIONBIO, &arg);
244 if (0 != fcntl(sockr, F_SETFL, O_NONBLOCK))
246 strError = "fcntl error";
247 codeError = IA_FCNTL_ERROR;
248 if (pErrorCb != NULL)
249 pErrorCb(strError, IA_FCNTL_ERROR, errorCbData);
254 //---------------------------------------------------------------------------
255 IA_CLIENT_PROT::~IA_CLIENT_PROT()
264 //---------------------------------------------------------------------------
265 int IA_CLIENT_PROT::DeterminatePacketType(const char * buffer)
267 map<string, int>::iterator pi;
268 pi = packetTypes.find(buffer);
269 if (pi == packetTypes.end())
278 //---------------------------------------------------------------------------
279 void IA_CLIENT_PROT::FillHdr8(char * buffer, unsigned long)
281 strncpy(buffer, IA_ID, 6);
282 buffer[IA_MAGIC_LEN] = 0;
283 buffer[IA_MAGIC_LEN + 1] = IA_PROTO_VER;
284 strncpy(buffer + sizeof(HDR_8), login.c_str(), IA_LOGIN_LEN);
286 //---------------------------------------------------------------------------
287 int IA_CLIENT_PROT::Send(char * buffer, int len)
292 isNetPrepared = true;
295 int db = sizeof(HDR_8);
296 for (int i = 0; i < IA_LOGIN_LEN/8; i++)
298 Blowfish_Encrypt(&ctxHdr, (uint32_t*)(buffer + db + i*8), (uint32_t*)(buffer + db + i*8 + 4));
302 int encLen = (len - sizeof(HDR_8) - IA_LOGIN_LEN)/8;
303 for (int i = 0; i < encLen; i++)
305 Blowfish_Encrypt(&ctxPass, (uint32_t*)(buffer + db), (uint32_t*)(buffer + db + 4));
309 return sendto(sockr, buffer, len, 0, (struct sockaddr*)&servAddr, sizeof(servAddr));
311 //---------------------------------------------------------------------------
312 int IA_CLIENT_PROT::Recv(char * buffer, int len)
320 struct sockaddr_in addr;
321 fromLen = sizeof(addr);
322 int res = recvfrom(sockr, buffer, len, 0, (struct sockaddr*)&addr, &fromLen);
327 if (strcmp(buffer + 4 + sizeof(HDR_8), "ERR"))
329 for (int i = 0; i < len/8; i++)
330 Blowfish_Decrypt(&ctxPass, (uint32_t*)(buffer + i*8), (uint32_t*)(buffer + i*8 + 4));
335 //---------------------------------------------------------------------------
336 int IA_CLIENT_PROT::NetSend(int n)
341 memset(buffer, 0, 2048);
346 msgLen = Prepare_CONN_SYN_8(buffer);
350 msgLen = Prepare_CONN_ACK_8(buffer);
354 msgLen = Prepare_ALIVE_ACK_8(buffer);
358 msgLen = Prepare_DISCONN_SYN_8(buffer);
362 msgLen = Prepare_DISCONN_ACK_8(buffer);
370 Send(buffer, msgLen);
374 //---------------------------------------------------------------------------
375 int IA_CLIENT_PROT::NetRecv()
379 if (Recv(buffer, sizeof(buffer)) < 0)
383 strncpy(packetName, buffer + 12, sizeof(packetName));
384 packetName[sizeof(packetName) - 1] = 0;
385 int pn = DeterminatePacketType(buffer + 12);
391 ret = Process_CONN_SYN_ACK_8(buffer);
395 ret = Process_ALIVE_SYN_8(buffer);
398 case DISCONN_SYN_ACK_N:
399 ret = Process_DISCONN_SYN_ACK_8(buffer);
403 ret = Process_FIN_8(buffer);
407 ret = Process_INFO_8(buffer);
411 ret = Process_ERROR(buffer);
419 //---------------------------------------------------------------------------
420 void IA_CLIENT_PROT::Start()
425 CreateThread(NULL, 16384, RunW, this, 0, &pt);
427 pthread_create(&thread, NULL, RunL, this);
430 //---------------------------------------------------------------------------
431 void IA_CLIENT_PROT::Stop()
435 //---------------------------------------------------------------------------
436 void IA_CLIENT_PROT::Run()
443 if (action == IA_CONNECT)
448 phaseTime = GetTickCount();
450 if (reconnect && !firstConnect)
457 if ((int)(GetTickCount() - phaseTime)/1000 > aliveTimeout)
460 phaseTime = GetTickCount();
461 if (pStatusChangedCb != NULL)
462 pStatusChangedCb(0, statusChangedCbData);
465 if (action == IA_DISCONNECT)
468 NetSend(DISCONN_SYN_N);
470 phaseTime = GetTickCount();
476 if ((int)(GetTickCount() - phaseTime)/1000 > userTimeout)
479 phaseTime = GetTickCount();
480 if (pStatusChangedCb != NULL)
481 pStatusChangedCb(0, statusChangedCbData);
482 firstConnect = false;
485 if (action == IA_DISCONNECT)
488 NetSend(DISCONN_SYN_N);
490 phaseTime = GetTickCount();
496 if ((int)(GetTickCount() - phaseTime)/1000 > aliveTimeout)
499 phaseTime = GetTickCount();
500 if (pStatusChangedCb != NULL)
501 pStatusChangedCb(0, statusChangedCbData);
504 if (action == IA_CONNECT)
509 phaseTime = GetTickCount();
515 if ((int)(GetTickCount() - phaseTime)/1000 > aliveTimeout)
518 phaseTime = GetTickCount();
519 if (pStatusChangedCb != NULL)
520 pStatusChangedCb(0, statusChangedCbData);
523 if (action == IA_CONNECT)
528 phaseTime = GetTickCount();
536 //---------------------------------------------------------------------------
537 void IA_CLIENT_PROT::GetStat(LOADSTAT * ls)
539 memcpy(ls, &stat, sizeof(stat));
541 //---------------------------------------------------------------------------
542 void IA_CLIENT_PROT::SetServer(const string & sn, unsigned short p)
548 //---------------------------------------------------------------------------
549 void IA_CLIENT_PROT::SetLogin(const string & l)
553 //---------------------------------------------------------------------------
554 void IA_CLIENT_PROT::SetPassword(const string & p)
558 unsigned char keyL[IA_PASSWD_LEN];
559 memset(keyL, 0, IA_PASSWD_LEN);
560 strncpy((char *)keyL, password.c_str(), IA_PASSWD_LEN);
561 Blowfish_Init(&ctxPass, keyL, IA_PASSWD_LEN);
563 //---------------------------------------------------------------------------
564 void IA_CLIENT_PROT::SetEnabledDirs(const bool * selectedDirs)
566 memcpy(IA_CLIENT_PROT::selectedDirs, selectedDirs, sizeof(bool) * DIR_NUM);
568 //---------------------------------------------------------------------------
569 int IA_CLIENT_PROT::Connect()
574 //---------------------------------------------------------------------------
575 int IA_CLIENT_PROT::Disconnect()
578 action = IA_DISCONNECT;
581 //---------------------------------------------------------------------------
582 int IA_CLIENT_PROT::GetStrError(string * error) const
590 //---------------------------------------------------------------------------
591 int IA_CLIENT_PROT::Process_CONN_SYN_ACK_8(const char * buffer)
593 vector<string> dirNames;
594 connSynAck8 = (CONN_SYN_ACK_8*)buffer;
597 SwapBytes(connSynAck8->len);
598 SwapBytes(connSynAck8->rnd);
599 SwapBytes(connSynAck8->userTimeOut);
600 SwapBytes(connSynAck8->aliveDelay);
603 rnd = connSynAck8->rnd;
604 userTimeout = connSynAck8->userTimeOut;
605 aliveTimeout = connSynAck8->aliveDelay;
607 for (int i = 0; i < DIR_NUM; i++)
609 dirNames.push_back((const char*)connSynAck8->dirName[i]);
612 if (pDirNameCb != NULL)
613 pDirNameCb(dirNames, dirNameCbData);
617 phaseTime = GetTickCount();
619 return CONN_SYN_ACK_N;
621 //---------------------------------------------------------------------------
622 int IA_CLIENT_PROT::Process_ALIVE_SYN_8(const char * buffer)
624 aliveSyn8 = (ALIVE_SYN_8*)buffer;
627 SwapBytes(aliveSyn8->len);
628 SwapBytes(aliveSyn8->rnd);
629 SwapBytes(aliveSyn8->cash);
630 SwapBytes(aliveSyn8->status);
631 for (int i = 0; i < DIR_NUM; ++i)
633 SwapBytes(aliveSyn8->mu[i]);
634 SwapBytes(aliveSyn8->md[i]);
635 SwapBytes(aliveSyn8->su[i]);
636 SwapBytes(aliveSyn8->sd[i]);
640 rnd = aliveSyn8->rnd;
641 memcpy(&stat, (char*)aliveSyn8->mu, sizeof(stat));
643 if (pStatChangedCb != NULL)
644 pStatChangedCb(stat, statChangedCbData);
646 if (pStatusChangedCb != NULL)
647 pStatusChangedCb(1, statusChangedCbData);
648 NetSend(ALIVE_ACK_N);
649 phaseTime = GetTickCount();
653 //---------------------------------------------------------------------------
654 int IA_CLIENT_PROT::Process_DISCONN_SYN_ACK_8(const char * buffer)
656 disconnSynAck8 = (DISCONN_SYN_ACK_8*)buffer;
659 SwapBytes(disconnSynAck8->len);
660 SwapBytes(disconnSynAck8->rnd);
663 rnd = disconnSynAck8->rnd;
665 NetSend(DISCONN_ACK_N);
667 phaseTime = GetTickCount();
669 return DISCONN_SYN_ACK_N;
671 //---------------------------------------------------------------------------
672 int IA_CLIENT_PROT::Process_FIN_8(const char *)
675 phaseTime = GetTickCount();
676 if (pStatusChangedCb != NULL)
677 pStatusChangedCb(0, statusChangedCbData);
681 //---------------------------------------------------------------------------
682 int IA_CLIENT_PROT::Process_INFO_8(const char * buffer)
684 info = (INFO_8*)buffer;
687 SwapBytes(info->len);
688 SwapBytes(info->sendTime);
692 pInfoCb((char*)info->text, info->infoType, info->showTime, info->sendTime, infoCbData);
695 //---------------------------------------------------------------------------
696 int IA_CLIENT_PROT::Process_ERROR(const char * buffer)
699 memcpy(&err, buffer, sizeof(err));
705 KOIToWin((const char*)err.text, &messageText);
706 if (pErrorCb != NULL)
707 pErrorCb(messageText, IA_SERVER_ERROR, errorCbData);
709 phaseTime = GetTickCount();
710 codeError = IA_SERVER_ERROR;
714 //---------------------------------------------------------------------------
715 int IA_CLIENT_PROT::Prepare_CONN_SYN_8(char * buffer)
717 connSyn8 = (CONN_SYN_8*)buffer;
720 SwapBytes(connSyn8->len);
723 assert(sizeof(CONN_SYN_8) == Min8(sizeof(CONN_SYN_8)) && "CONN_SYN_8 is not aligned to 8 bytes");
725 connSyn8->len = sizeof(CONN_SYN_8);
726 strncpy((char*)connSyn8->type, "CONN_SYN", IA_MAX_TYPE_LEN);
727 strncpy((char*)connSyn8->login, login.c_str(), IA_LOGIN_LEN);
729 for (int i = 0; i < DIR_NUM; i++)
731 connSyn8->dirs |= (selectedDirs[i] << i);
733 return connSyn8->len;
735 //---------------------------------------------------------------------------
736 int IA_CLIENT_PROT::Prepare_CONN_ACK_8(char * buffer)
738 connAck8 = (CONN_ACK_8*)buffer;
741 SwapBytes(connAck8->len);
742 SwapBytes(connAck8->rnd);
745 assert(sizeof(CONN_ACK_8) == Min8(sizeof(CONN_ACK_8)) && "CONN_ACK_8 is not aligned to 8 bytes");
747 connAck8->len = sizeof(CONN_ACK_8);
748 strncpy((char*)connAck8->loginS, login.c_str(), IA_LOGIN_LEN);
749 strncpy((char*)connAck8->type, "CONN_ACK", IA_MAX_TYPE_LEN);
753 return connAck8->len;
755 //---------------------------------------------------------------------------
756 int IA_CLIENT_PROT::Prepare_ALIVE_ACK_8(char * buffer)
758 aliveAck8 = (ALIVE_ACK_8*)buffer;
761 SwapBytes(aliveAck8->len);
762 SwapBytes(aliveAck8->rnd);
765 assert(Min8(sizeof(ALIVE_ACK_8)) == sizeof(ALIVE_ACK_8) && "ALIVE_ACK_8 is not aligned to 8 bytes");
767 aliveAck8 = (ALIVE_ACK_8*)buffer;
768 aliveAck8->len = sizeof(ALIVE_ACK_8);
769 strncpy((char*)aliveAck8->loginS, login.c_str(), IA_LOGIN_LEN);
770 strncpy((char*)aliveAck8->type, "ALIVE_ACK", IA_MAX_TYPE_LEN);
771 aliveAck8->rnd = ++rnd;
772 return aliveAck8->len;
774 //---------------------------------------------------------------------------
775 int IA_CLIENT_PROT::Prepare_DISCONN_SYN_8(char * buffer)
777 disconnSyn8 = (DISCONN_SYN_8*)buffer;
780 SwapBytes(disconnSyn8->len);
783 assert(Min8(sizeof(DISCONN_SYN_8)) == sizeof(DISCONN_SYN_8) && "DISCONN_SYN_8 is not aligned to 8 bytes");
785 disconnSyn8->len = sizeof(DISCONN_SYN_8);
786 strncpy((char*)disconnSyn8->loginS, login.c_str(), IA_LOGIN_LEN);
787 strncpy((char*)disconnSyn8->type, "DISCONN_SYN", IA_MAX_TYPE_LEN);
788 strncpy((char*)disconnSyn8->login, login.c_str(), IA_LOGIN_LEN);
789 return disconnSyn8->len;
791 //---------------------------------------------------------------------------
792 int IA_CLIENT_PROT::Prepare_DISCONN_ACK_8(char * buffer)
794 disconnAck8 = (DISCONN_ACK_8*)buffer;
797 SwapBytes(disconnAck8->len);
798 SwapBytes(disconnAck8->rnd);
801 assert(Min8(sizeof(DISCONN_ACK_8)) == sizeof(DISCONN_ACK_8) && "DISCONN_ACK_8 is not aligned to 8 bytes");
803 disconnAck8->len = Min8(sizeof(DISCONN_ACK_8));
804 disconnAck8->rnd = rnd + 1;
805 strncpy((char*)disconnAck8->loginS, login.c_str(), IA_LOGIN_LEN);
806 strncpy((char*)disconnAck8->type, "DISCONN_ACK", IA_MAX_TYPE_LEN);
807 return disconnAck8->len;
809 //---------------------------------------------------------------------------
810 void IA_CLIENT_PROT::SetStatusChangedCb(tpStatusChangedCb p, void * data)
812 pStatusChangedCb = p;
813 statusChangedCbData = data;
815 //---------------------------------------------------------------------------
816 void IA_CLIENT_PROT::SetStatChangedCb(tpStatChangedCb p, void * data)
819 statChangedCbData = data;
821 //---------------------------------------------------------------------------
822 void IA_CLIENT_PROT::SetInfoCb(tpCallBackInfoFn p, void * data)
827 //---------------------------------------------------------------------------
828 void IA_CLIENT_PROT::SetDirNameCb(tpCallBackDirNameFn p, void * data)
831 dirNameCbData = data;
833 //---------------------------------------------------------------------------
834 void IA_CLIENT_PROT::SetErrorCb(tpCallBackErrorFn p, void * data)
839 //---------------------------------------------------------------------------