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 $Date: 2010/03/25 15:18:48 $
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <unistd.h> // usleep, close
37 #include <cstdio> // snprintf
41 #include "stg/common.h"
42 #include "stg/locker.h"
43 #include "stg/tariff.h"
44 #include "stg/user_property.h"
45 #include "stg/settings.h"
46 #include "stg/plugin_creator.h"
47 #include "inetaccess.h"
49 extern volatile const time_t stgTime;
51 void InitEncrypt(BLOWFISH_CTX * ctx, const string & password);
52 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
53 void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
55 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
58 PLUGIN_CREATOR<AUTH_IA> iac;
59 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
64 return iac.GetPlugin();
66 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
69 AUTH_IA_SETTINGS::AUTH_IA_SETTINGS()
73 freeMbShowType(freeMbCash)
76 //-----------------------------------------------------------------------------
77 int AUTH_IA_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
81 vector<PARAM_VALUE>::const_iterator pvi;
82 ///////////////////////////
84 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
85 if (pvi == s.moduleParams.end())
87 errorStr = "Parameter \'Port\' not found.";
88 printfd(__FILE__, "Parameter 'Port' not found\n");
91 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
93 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
94 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
98 ///////////////////////////
99 pv.param = "UserDelay";
100 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
101 if (pvi == s.moduleParams.end())
103 errorStr = "Parameter \'UserDelay\' not found.";
104 printfd(__FILE__, "Parameter 'UserDelay' not found\n");
108 if (ParseIntInRange(pvi->value[0], 5, 600, &userDelay))
110 errorStr = "Cannot parse parameter \'UserDelay\': " + errorStr;
111 printfd(__FILE__, "Cannot parse parameter 'UserDelay'\n");
114 ///////////////////////////
115 pv.param = "UserTimeout";
116 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
117 if (pvi == s.moduleParams.end())
119 errorStr = "Parameter \'UserTimeout\' not found.";
120 printfd(__FILE__, "Parameter 'UserTimeout' not found\n");
124 if (ParseIntInRange(pvi->value[0], 15, 1200, &userTimeout))
126 errorStr = "Cannot parse parameter \'UserTimeout\': " + errorStr;
127 printfd(__FILE__, "Cannot parse parameter 'UserTimeout'\n");
130 /////////////////////////////////////////////////////////////
134 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
135 if (pvi == s.moduleParams.end())
137 errorStr = "Parameter \'FreeMb\' not found.";
138 printfd(__FILE__, "Parameter 'FreeMb' not found\n");
141 freeMbType = pvi->value[0];
143 if (strcasecmp(freeMbType.c_str(), "cash") == 0)
145 freeMbShowType = freeMbCash;
147 else if (strcasecmp(freeMbType.c_str(), "none") == 0)
149 freeMbShowType = freeMbNone;
151 else if (!str2x(freeMbType.c_str(), n))
153 if (n < 0 || n >= DIR_NUM)
155 errorStr = "Incorrect parameter \'" + freeMbType + "\'.";
156 printfd(__FILE__, "%s\n", errorStr.c_str());
159 freeMbShowType = (FREEMB)(freeMb0 + n);
163 errorStr = "Incorrect parameter \'" + freeMbType + "\'.";
164 printfd(__FILE__, "%s\n", errorStr.c_str());
167 /////////////////////////////////////////////////////////////
170 //-----------------------------------------------------------------------------
171 //-----------------------------------------------------------------------------
172 //-----------------------------------------------------------------------------
173 #ifdef IA_PHASE_DEBUG
178 gettimeofday(&phaseTime, NULL);
184 gettimeofday(&phaseTime, NULL);
187 //-----------------------------------------------------------------------------
188 IA_PHASE::~IA_PHASE()
190 #ifdef IA_PHASE_DEBUG
191 flog = fopen(log.c_str(), "at");
194 fprintf(flog, "IA %s D\n", login.c_str());
199 //-----------------------------------------------------------------------------
200 #ifdef IA_PHASE_DEBUG
201 void IA_PHASE::SetLogFileName(const string & logFileName)
203 log = logFileName + ".ia.log";
205 //-----------------------------------------------------------------------------
206 void IA_PHASE::SetUserLogin(const string & login)
208 IA_PHASE::login = login;
210 //-----------------------------------------------------------------------------
211 void IA_PHASE::WritePhaseChange(int newPhase)
214 gettimeofday(&newPhaseTime, NULL);
215 flog = fopen(log.c_str(), "at");
218 string action = newPhase == phase ? "U" : "C";
219 double delta = newPhaseTime.GetSec() - phaseTime.GetSec();
220 delta += (newPhaseTime.GetUSec() - phaseTime.GetUSec()) * 1.0e-6;
221 fprintf(flog, "IA %s %s oldPhase = %d, newPhase = %d. dt = %.6f\n",
231 //-----------------------------------------------------------------------------
232 void IA_PHASE::SetPhase1()
234 #ifdef IA_PHASE_DEBUG
238 gettimeofday(&phaseTime, NULL);
240 //-----------------------------------------------------------------------------
241 void IA_PHASE::SetPhase2()
243 #ifdef IA_PHASE_DEBUG
247 gettimeofday(&phaseTime, NULL);
249 //-----------------------------------------------------------------------------
250 void IA_PHASE::SetPhase3()
252 #ifdef IA_PHASE_DEBUG
256 gettimeofday(&phaseTime, NULL);
258 //-----------------------------------------------------------------------------
259 void IA_PHASE::SetPhase4()
261 #ifdef IA_PHASE_DEBUG
265 gettimeofday(&phaseTime, NULL);
267 //-----------------------------------------------------------------------------
268 void IA_PHASE::SetPhase5()
270 #ifdef IA_PHASE_DEBUG
274 gettimeofday(&phaseTime, NULL);
276 //-----------------------------------------------------------------------------
277 int IA_PHASE::GetPhase() const
281 //-----------------------------------------------------------------------------
282 void IA_PHASE::UpdateTime()
284 #ifdef IA_PHASE_DEBUG
285 WritePhaseChange(phase);
287 gettimeofday(&phaseTime, NULL);
289 //-----------------------------------------------------------------------------
290 const UTIME & IA_PHASE::GetTime() const
294 //-----------------------------------------------------------------------------
295 //-----------------------------------------------------------------------------
296 //-----------------------------------------------------------------------------
300 isRunningRunTimeouter(false),
304 WriteServLog(GetStgLogger()),
305 enabledDirs(0xFFffFFff),
306 onDelUserNotifier(*this)
308 InitEncrypt(&ctxS, "pr7Hhen");
310 pthread_mutexattr_t attr;
311 pthread_mutexattr_init(&attr);
312 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
313 pthread_mutex_init(&mutex, &attr);
315 memset(&connSynAck6, 0, sizeof(CONN_SYN_ACK_6));
316 memset(&connSynAck8, 0, sizeof(CONN_SYN_ACK_8));
317 memset(&disconnSynAck6, 0, sizeof(DISCONN_SYN_ACK_6));
318 memset(&disconnSynAck8, 0, sizeof(DISCONN_SYN_ACK_8));
319 memset(&aliveSyn6, 0, sizeof(ALIVE_SYN_6));
320 memset(&aliveSyn8, 0, sizeof(ALIVE_SYN_8));
321 memset(&fin6, 0, sizeof(FIN_6));
322 memset(&fin8, 0, sizeof(FIN_8));
324 printfd(__FILE__, "sizeof(CONN_SYN_6) = %d %d\n", sizeof(CONN_SYN_6), Min8(sizeof(CONN_SYN_6)));
325 printfd(__FILE__, "sizeof(CONN_SYN_8) = %d %d\n", sizeof(CONN_SYN_8), Min8(sizeof(CONN_SYN_8)));
326 printfd(__FILE__, "sizeof(CONN_SYN_ACK_6) = %d %d\n", sizeof(CONN_SYN_ACK_6), Min8(sizeof(CONN_SYN_ACK_6)));
327 printfd(__FILE__, "sizeof(CONN_SYN_ACK_8) = %d %d\n", sizeof(CONN_SYN_ACK_8), Min8(sizeof(CONN_SYN_ACK_8)));
328 printfd(__FILE__, "sizeof(CONN_ACK_6) = %d %d\n", sizeof(CONN_ACK_6), Min8(sizeof(CONN_ACK_6)));
329 printfd(__FILE__, "sizeof(ALIVE_SYN_6) = %d %d\n", sizeof(ALIVE_SYN_6), Min8(sizeof(ALIVE_SYN_6)));
330 printfd(__FILE__, "sizeof(ALIVE_SYN_8) = %d %d\n", sizeof(ALIVE_SYN_8), Min8(sizeof(ALIVE_SYN_8)));
331 printfd(__FILE__, "sizeof(ALIVE_ACK_6) = %d %d\n", sizeof(ALIVE_ACK_6), Min8(sizeof(ALIVE_ACK_6)));
332 printfd(__FILE__, "sizeof(DISCONN_SYN_6) = %d %d\n", sizeof(DISCONN_SYN_6), Min8(sizeof(DISCONN_SYN_6)));
333 printfd(__FILE__, "sizeof(DISCONN_SYN_ACK_6) = %d %d\n", sizeof(DISCONN_SYN_ACK_6), Min8(sizeof(DISCONN_SYN_ACK_6)));
334 printfd(__FILE__, "sizeof(DISCONN_SYN_ACK_8) = %d %d\n", sizeof(DISCONN_SYN_ACK_8), Min8(sizeof(DISCONN_SYN_ACK_8)));
335 printfd(__FILE__, "sizeof(DISCONN_ACK_6) = %d %d\n", sizeof(DISCONN_ACK_6), Min8(sizeof(DISCONN_ACK_6)));
336 printfd(__FILE__, "sizeof(FIN_6) = %d %d\n", sizeof(FIN_6), Min8(sizeof(FIN_6)));
337 printfd(__FILE__, "sizeof(FIN_8) = %d %d\n", sizeof(FIN_8), Min8(sizeof(FIN_8)));
338 printfd(__FILE__, "sizeof(ERR) = %d %d\n", sizeof(ERR), Min8(sizeof(ERR)));
339 printfd(__FILE__, "sizeof(INFO_6) = %d %d\n", sizeof(INFO_6), Min8(sizeof(INFO_6)));
340 printfd(__FILE__, "sizeof(INFO_7) = %d %d\n", sizeof(INFO_7), Min8(sizeof(INFO_7)));
341 printfd(__FILE__, "sizeof(INFO_8) = %d %d\n", sizeof(INFO_8), Min8(sizeof(INFO_8)));
343 packetTypes["CONN_SYN"] = CONN_SYN_N;
344 packetTypes["CONN_SYN_ACK"] = CONN_SYN_ACK_N;
345 packetTypes["CONN_ACK"] = CONN_ACK_N;
346 packetTypes["ALIVE_SYN"] = ALIVE_SYN_N;
347 packetTypes["ALIVE_ACK"] = ALIVE_ACK_N;
348 packetTypes["DISCONN_SYN"] = DISCONN_SYN_N;
349 packetTypes["DISCONN_SYN_ACK"] = DISCONN_SYN_ACK_N;
350 packetTypes["DISCONN_ACK"] = DISCONN_ACK_N;
351 packetTypes["FIN"] = FIN_N;
352 packetTypes["ERR"] = ERROR_N;
354 //-----------------------------------------------------------------------------
357 pthread_mutex_destroy(&mutex);
359 //-----------------------------------------------------------------------------
362 users->AddNotifierUserDel(&onDelUserNotifier);
372 if (pthread_create(&recvThread, NULL, Run, this))
374 errorStr = "Cannot create thread.";
375 printfd(__FILE__, "Cannot create recv thread\n");
380 if (!isRunningRunTimeouter)
382 if (pthread_create(&timeouterThread, NULL, RunTimeouter, this))
384 errorStr = "Cannot create thread.";
385 printfd(__FILE__, "Cannot create timeouter thread\n");
392 //-----------------------------------------------------------------------------
403 UnauthorizeUser(this)
408 //5 seconds to thread stops itself
409 for (int i = 0; i < 25 && isRunningRun; i++)
414 //after 5 seconds waiting thread still running. now killing it
417 if (pthread_kill(recvThread, SIGINT))
419 errorStr = "Cannot kill thread.";
420 printfd(__FILE__, "Cannot kill thread\n");
423 for (int i = 0; i < 25 && isRunningRun; ++i)
427 printfd(__FILE__, "Failed to stop recv thread\n");
431 pthread_join(recvThread, NULL);
433 printfd(__FILE__, "AUTH_IA killed Run\n");
439 if (isRunningRunTimeouter)
441 //5 seconds to thread stops itself
442 for (int i = 0; i < 25 && isRunningRunTimeouter; i++)
447 //after 5 seconds waiting thread still running. now killing it
448 if (isRunningRunTimeouter)
450 if (pthread_kill(timeouterThread, SIGINT))
452 errorStr = "Cannot kill thread.";
455 for (int i = 0; i < 25 && isRunningRunTimeouter; ++i)
457 if (isRunningRunTimeouter)
459 printfd(__FILE__, "Failed to stop timeouter thread\n");
463 pthread_join(timeouterThread, NULL);
465 printfd(__FILE__, "AUTH_IA killed Timeouter\n");
468 printfd(__FILE__, "AUTH_IA::Stoped successfully.\n");
469 users->DelNotifierUserDel(&onDelUserNotifier);
472 //-----------------------------------------------------------------------------
473 void * AUTH_IA::Run(void * d)
475 AUTH_IA * ia = static_cast<AUTH_IA *>(d);
477 ia->isRunningRun = true;
481 time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
485 ia->RecvData(buffer, sizeof(buffer));
486 if ((touchTime + MONITOR_TIME_DELAY_SEC <= stgTime) && ia->stgSettings->GetMonitoring())
489 string monFile = ia->stgSettings->GetMonitorDir() + "/inetaccess_r";
490 TouchFile(monFile.c_str());
494 ia->isRunningRun = false;
497 //-----------------------------------------------------------------------------
498 void * AUTH_IA::RunTimeouter(void * d)
500 AUTH_IA * ia = static_cast<AUTH_IA *>(d);
502 ia->isRunningRunTimeouter = true;
505 string monFile = ia->stgSettings->GetMonitorDir() + "/inetaccess_t";
510 // TODO change counter to timer and MONITOR_TIME_DELAY_SEC
511 if (++a % (50 * 60) == 0 && ia->stgSettings->GetMonitoring())
513 TouchFile(monFile.c_str());
517 ia->isRunningRunTimeouter = false;
520 //-----------------------------------------------------------------------------
521 int AUTH_IA::ParseSettings()
523 int ret = iaSettings.ParseSettings(settings);
525 errorStr = iaSettings.GetStrError();
528 //-----------------------------------------------------------------------------
529 int AUTH_IA::PrepareNet()
531 struct sockaddr_in listenAddr;
533 listenSocket = socket(AF_INET, SOCK_DGRAM, 0);
535 if (listenSocket < 0)
537 errorStr = "Cannot create socket.";
541 listenAddr.sin_family = AF_INET;
542 listenAddr.sin_port = htons(iaSettings.GetUserPort());
543 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
545 if (bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr)) < 0)
547 errorStr = "AUTH_IA: Bind failed.";
553 //-----------------------------------------------------------------------------
554 int AUTH_IA::FinalizeNet()
559 //-----------------------------------------------------------------------------
560 int AUTH_IA::RecvData(char * buffer, int bufferSize)
562 if (!WaitPackets(listenSocket)) // Timeout
567 struct sockaddr_in outerAddr;
568 socklen_t outerAddrLen(sizeof(outerAddr));
569 int dataLen = recvfrom(listenSocket, buffer, bufferSize, 0, (struct sockaddr *)&outerAddr, &outerAddrLen);
576 if (dataLen <= 0) // Error
580 printfd(__FILE__, "recvfrom res=%d, error: '%s'\n", dataLen, strerror(errno));
590 if (CheckHeader(buffer, &protoVer))
593 char login[PASSWD_LEN]; //TODO why PASSWD_LEN ?
594 memset(login, 0, PASSWD_LEN);
596 Decrypt(&ctxS, login, buffer + 8, PASSWD_LEN / 8);
598 uint32_t sip = outerAddr.sin_addr.s_addr;
599 uint16_t sport = htons(outerAddr.sin_port);
602 if (users->FindByName(login, &user) == 0)
604 WriteServLog("User's connect failed: user '%s' not found. IP %s",
606 inet_ntostring(sip).c_str());
607 printfd(__FILE__, "User %s NOT found!\n", login);
608 SendError(sip, sport, protoVer, "îÅÐÒÁ×ÉÌØÎÙÊ ÌÏÇÉÎ!");
611 printfd(__FILE__, "User '%s' FOUND!\n", user->GetLogin().c_str());
613 if (user->GetProperty().disabled.Get())
615 SendError(sip, sport, protoVer, "õÞÅÔÎÁÑ ÚÁÐÉÓØ ÚÁÂÌÏËÉÒÏ×ÁÎÁ");
619 if (user->GetProperty().passive.Get())
621 SendError(sip, sport, protoVer, "õÞÅÔÎÁÑ ÚÁÐÉÓØ ÚÁÍÏÒÏÖÅÎÁ");
625 if (!user->GetProperty().ips.Get().IsIPInIPS(sip))
627 printfd(__FILE__, "User %s. IP address is incorrect. IP %s\n",
628 user->GetLogin().c_str(), inet_ntostring(sip).c_str());
629 WriteServLog("User %s. IP address is incorrect. IP %s",
630 user->GetLogin().c_str(), inet_ntostring(sip).c_str());
631 SendError(sip, sport, protoVer, "ðÏÌØÚÏ×ÁÔÅÌØ ÎÅ ÏÐÏÚÎÁÎ! ðÒÏ×ÅÒØÔÅ IP ÁÄÒÅÓ.");
635 return PacketProcessor(buffer, dataLen, sip, sport, protoVer, user);
637 //-----------------------------------------------------------------------------
638 int AUTH_IA::CheckHeader(const char * buffer, int * protoVer)
640 if (strncmp(IA_ID, buffer, strlen(IA_ID)) != 0)
642 //SendError(userIP, updateMsg);
643 printfd(__FILE__, "update needed - IA_ID\n");
644 //SendError(userIP, "Incorrect header!");
648 if (buffer[6] != 0) //proto[0] shoud be 0
650 printfd(__FILE__, "update needed - PROTO major: %d\n", buffer[6]);
651 //SendError(userIP, updateMsg);
658 //SendError(userIP, updateMsg);
659 printfd(__FILE__, "update needed - PROTO minor: %d\n", buffer[7]);
664 *protoVer = buffer[7];
668 //-----------------------------------------------------------------------------
669 int AUTH_IA::Timeouter()
671 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
673 map<uint32_t, IA_USER>::iterator it;
674 it = ip2user.begin();
677 while (it != ip2user.end())
681 static UTIME currTime;
682 gettimeofday(&currTime, NULL);
684 if ((it->second.phase.GetPhase() == 2)
685 && (currTime - it->second.phase.GetTime()) > iaSettings.GetUserDelay())
687 it->second.phase.SetPhase1();
688 printfd(__FILE__, "Phase changed from 2 to 1. Reason: timeout\n");
693 if (it->second.phase.GetPhase() == 3)
695 if (!it->second.messagesToSend.empty())
697 if (it->second.protoVer == 6)
698 RealSendMessage6(*it->second.messagesToSend.begin(), sip, it->second);
700 if (it->second.protoVer == 7)
701 RealSendMessage7(*it->second.messagesToSend.begin(), sip, it->second);
703 if (it->second.protoVer == 8)
704 RealSendMessage8(*it->second.messagesToSend.begin(), sip, it->second);
706 it->second.messagesToSend.erase(it->second.messagesToSend.begin());
709 if((currTime - it->second.lastSendAlive) > iaSettings.GetUserDelay())
711 switch (it->second.protoVer)
714 Send_ALIVE_SYN_6(&(it->second), sip);
717 Send_ALIVE_SYN_7(&(it->second), sip);
720 Send_ALIVE_SYN_8(&(it->second), sip);
724 gettimeofday(&it->second.lastSendAlive, NULL);
727 if ((currTime - it->second.phase.GetTime()) > iaSettings.GetUserTimeout())
729 users->Unauthorize(it->second.user->GetLogin(), this);
735 if ((it->second.phase.GetPhase() == 4)
736 && ((currTime - it->second.phase.GetTime()) > iaSettings.GetUserDelay()))
738 it->second.phase.SetPhase3();
739 printfd(__FILE__, "Phase changed from 4 to 3. Reason: timeout\n");
747 //-----------------------------------------------------------------------------
748 int AUTH_IA::PacketProcessor(char * buff, int dataLen, uint32_t sip, uint16_t sport, int protoVer, USER_PTR user)
750 std::string login(user->GetLogin());
751 const int offset = LOGIN_LEN + 2 + 6; // LOGIN_LEN + sizeOfMagic + sizeOfVer;
753 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
754 map<uint32_t, IA_USER>::iterator it(ip2user.find(sip));
756 if (it == ip2user.end())
759 if (!users->FindByIPIdx(sip, &userPtr))
761 if (userPtr->GetID() != user->GetID())
763 printfd(__FILE__, "IP address already in use by user '%s'. IP %s, login: '%s'\n",
764 userPtr->GetLogin().c_str(),
765 inet_ntostring(sip).c_str(),
767 WriteServLog("IP address already in use by user '%s'. IP %s, login: '%s'",
768 userPtr->GetLogin().c_str(),
769 inet_ntostring(sip).c_str(),
771 SendError(sip, sport, protoVer, "÷ÁÛ IP ÁÄÒÅÓ ÕÖÅ ÉÓÐÏÌØÚÕÅÔÓÑ!");
776 printfd(__FILE__, "Add new user '%s' from ip %s\n",
777 login.c_str(), inet_ntostring(sip).c_str());
778 ip2user[sip].login = login;
779 ip2user[sip].user = user;
780 ip2user[sip].protoVer = protoVer;
781 ip2user[sip].port = sport;
782 #ifdef IA_PHASE_DEBUG
783 ip2user[sip].phase.SetLogFileName(stgSettings->GetLogFileName());
784 ip2user[sip].phase.SetUserLogin(login);
788 it = ip2user.find(sip); //TODO
789 if (it == ip2user.end())
791 printfd(__FILE__, "+++ ERROR +++\n");
795 else if (user->GetID() != it->second.user->GetID())
797 printfd(__FILE__, "IP address already in use by user '%s'. IP %s, login: '%s'\n",
798 it->second.user->GetLogin().c_str(),
799 inet_ntostring(sip).c_str(),
800 user->GetLogin().c_str());
801 WriteServLog("IP address already in use by user '%s'. IP %s, login: '%s'",
802 it->second.user->GetLogin().c_str(),
803 inet_ntostring(sip).c_str(),
804 user->GetLogin().c_str());
805 SendError(sip, sport, protoVer, "÷ÁÛ IP ÁÄÒÅÓ ÕÖÅ ÉÓÐÏÌØÚÕÅÔÓÑ!");
809 IA_USER * iaUser = &(it->second);
811 if (iaUser->port != sport)
812 iaUser->port = sport;
814 if (iaUser->password != user->GetProperty().password.Get())
816 InitEncrypt(&iaUser->ctx, user->GetProperty().password.Get());
817 iaUser->password = user->GetProperty().password.Get();
821 Decrypt(&iaUser->ctx, buff, buff, (dataLen - offset) / 8);
823 char packetName[IA_MAX_TYPE_LEN];
824 strncpy(packetName, buff + 4, IA_MAX_TYPE_LEN);
825 packetName[IA_MAX_TYPE_LEN - 1] = 0;
827 map<string, int>::iterator pi(packetTypes.find(packetName));
828 if (pi == packetTypes.end())
830 SendError(sip, sport, protoVer, "îÅÐÒÁ×ÉÌØÎÙÊ ÌÏÇÉÎ ÉÌÉ ÐÁÒÏÌØ!");
831 printfd(__FILE__, "Login or password is wrong!\n");
832 WriteServLog("User's connect failed. User: '%s', ip %s. Wrong login or password",
834 inet_ntostring(sip).c_str());
839 if (user->IsAuthorizedBy(this) && user->GetCurrIP() != sip)
841 printfd(__FILE__, "Login %s already in use from ip %s. IP %s\n",
842 login.c_str(), inet_ntostring(user->GetCurrIP()).c_str(),
843 inet_ntostring(sip).c_str());
844 WriteServLog("Login %s already in use from ip %s. IP %s",
846 inet_ntostring(user->GetCurrIP()).c_str(),
847 inet_ntostring(sip).c_str());
848 SendError(sip, sport, protoVer, "÷ÁÛ ÌÏÇÉÎ ÕÖÅ ÉÓÐÏÌØÚÕÅÔÓÑ!");
859 if (Process_CONN_SYN_6((CONN_SYN_6 *)(buff - offset), &(it->second), sip))
861 return Send_CONN_SYN_ACK_6(iaUser, sip);
863 if (Process_CONN_SYN_7((CONN_SYN_7 *)(buff - offset), &(it->second), sip))
865 return Send_CONN_SYN_ACK_7(iaUser, sip);
867 if (Process_CONN_SYN_8((CONN_SYN_8 *)(buff - offset), &(it->second), sip))
869 return Send_CONN_SYN_ACK_8(iaUser, sip);
877 if (Process_CONN_ACK_6((CONN_ACK_6 *)(buff - offset), iaUser, sip))
879 return Send_ALIVE_SYN_6(iaUser, sip);
881 if (Process_CONN_ACK_7((CONN_ACK_6 *)(buff - offset), iaUser, sip))
883 return Send_ALIVE_SYN_7(iaUser, sip);
885 if (Process_CONN_ACK_8((CONN_ACK_8 *)(buff - offset), iaUser, sip))
887 return Send_ALIVE_SYN_8(iaUser, sip);
895 return Process_ALIVE_ACK_6((ALIVE_ACK_6 *)(buff - offset), iaUser, sip);
897 return Process_ALIVE_ACK_7((ALIVE_ACK_6 *)(buff - offset), iaUser, sip);
899 return Process_ALIVE_ACK_8((ALIVE_ACK_8 *)(buff - offset), iaUser, sip);
907 if (Process_DISCONN_SYN_6((DISCONN_SYN_6 *)(buff - offset), iaUser, sip))
909 return Send_DISCONN_SYN_ACK_6(iaUser, sip);
911 if (Process_DISCONN_SYN_7((DISCONN_SYN_6 *)(buff - offset), iaUser, sip))
913 return Send_DISCONN_SYN_ACK_7(iaUser, sip);
915 if (Process_DISCONN_SYN_8((DISCONN_SYN_8 *)(buff - offset), iaUser, sip))
917 return Send_DISCONN_SYN_ACK_8(iaUser, sip);
925 if (Process_DISCONN_ACK_6((DISCONN_ACK_6 *)(buff - offset), iaUser, sip, it))
927 return Send_FIN_6(iaUser, sip, it);
929 if (Process_DISCONN_ACK_7((DISCONN_ACK_6 *)(buff - offset), iaUser, sip, it))
931 return Send_FIN_7(iaUser, sip, it);
933 if (Process_DISCONN_ACK_8((DISCONN_ACK_8 *)(buff - offset), iaUser, sip, it))
935 return Send_FIN_8(iaUser, sip, it);
942 //-----------------------------------------------------------------------------
943 void AUTH_IA::DelUser(USER_PTR u)
946 uint32_t ip = u->GetCurrIP();
951 map<uint32_t, IA_USER>::iterator it;
953 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
954 it = ip2user.find(ip);
955 if (it == ip2user.end())
958 printfd(__FILE__, "Nothing to delete\n");
962 if (it->second.user == u)
964 printfd(__FILE__, "User removed!\n");
965 users->Unauthorize(u->GetLogin(), this);
969 //-----------------------------------------------------------------------------
970 int AUTH_IA::SendError(uint32_t ip, uint16_t port, int protoVer, const string & text)
972 struct sockaddr_in sendAddr;
979 memset(&err, 0, sizeof(ERR));
981 sendAddr.sin_family = AF_INET;
982 sendAddr.sin_port = htons(port);
983 sendAddr.sin_addr.s_addr = ip;
986 strncpy((char*)err.type, "ERR", 16);
987 strncpy((char*)err.text, text.c_str(), MAX_MSG_LEN);
993 res = sendto(listenSocket, &err, sizeof(err), 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
994 printfd(__FILE__, "SendError %d bytes sent\n", res);
999 memset(&err8, 0, sizeof(ERR_8));
1001 sendAddr.sin_family = AF_INET;
1002 sendAddr.sin_port = htons(port);
1003 sendAddr.sin_addr.s_addr = ip;
1006 strncpy((char*)err8.type, "ERR", 16);
1007 strncpy((char*)err8.text, text.c_str(), MAX_MSG_LEN);
1010 SwapBytes(err8.len);
1013 res = sendto(listenSocket, &err8, sizeof(err8), 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
1014 printfd(__FILE__, "SendError_8 %d bytes sent\n", res);
1020 //-----------------------------------------------------------------------------
1021 int AUTH_IA::Send(uint32_t ip, uint16_t port, const char * buffer, int len)
1023 struct sockaddr_in sendAddr;
1025 sendAddr.sin_family = AF_INET;
1026 sendAddr.sin_port = htons(port);
1027 sendAddr.sin_addr.s_addr = ip;
1029 int res = sendto(listenSocket, buffer, len, 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
1031 static struct timeval tv;
1032 gettimeofday(&tv, NULL);
1039 //-----------------------------------------------------------------------------
1040 int AUTH_IA::SendMessage(const STG_MSG & msg, uint32_t ip) const
1042 printfd(__FILE__, "SendMessage userIP=%s\n", inet_ntostring(ip).c_str());
1044 map<uint32_t, IA_USER>::iterator it;
1046 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1047 it = ip2user.find(ip);
1048 if (it == ip2user.end())
1050 errorStr = "Unknown user.";
1053 it->second.messagesToSend.push_back(msg);
1056 //-----------------------------------------------------------------------------
1057 int AUTH_IA::RealSendMessage6(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1059 printfd(__FILE__, "RealSendMessage 6 user=%s\n", user.login.c_str());
1062 memset(&info, 0, sizeof(INFO_6));
1065 strncpy((char*)info.type, "INFO", 16);
1066 info.infoType = 'I';
1067 strncpy((char*)info.text, msg.text.c_str(), 235);
1070 size_t len = info.len;
1072 SwapBytes(info.len);
1076 memcpy(buffer, &info, sizeof(INFO_6));
1077 Encrypt(&user.ctx, buffer, buffer, len / 8);
1078 return Send(ip, iaSettings.GetUserPort(), buffer, len);
1080 //-----------------------------------------------------------------------------
1081 int AUTH_IA::RealSendMessage7(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1083 printfd(__FILE__, "RealSendMessage 7 user=%s\n", user.login.c_str());
1086 memset(&info, 0, sizeof(INFO_7));
1089 strncpy((char*)info.type, "INFO_7", 16);
1090 info.infoType = msg.header.type;
1091 info.showTime = msg.header.showTime;
1092 info.sendTime = msg.header.creationTime;
1094 size_t len = info.len;
1096 SwapBytes(info.len);
1097 SwapBytes(info.sendTime);
1100 strncpy((char*)info.text, msg.text.c_str(), MAX_MSG_LEN - 1);
1101 info.text[MAX_MSG_LEN - 1] = 0;
1104 memcpy(buffer, &info, sizeof(INFO_7));
1106 Encrypt(&user.ctx, buffer, buffer, len / 8);
1107 return Send(ip, iaSettings.GetUserPort(), buffer, len);
1109 //-----------------------------------------------------------------------------
1110 int AUTH_IA::RealSendMessage8(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1112 printfd(__FILE__, "RealSendMessage 8 user=%s\n", user.login.c_str());
1115 memset(&info, 0, sizeof(INFO_8));
1118 strncpy((char*)info.type, "INFO_8", 16);
1119 info.infoType = msg.header.type;
1120 info.showTime = msg.header.showTime;
1121 info.sendTime = msg.header.creationTime;
1123 strncpy((char*)info.text, msg.text.c_str(), IA_MAX_MSG_LEN_8 - 1);
1124 info.text[IA_MAX_MSG_LEN_8 - 1] = 0;
1126 size_t len = info.len;
1128 SwapBytes(info.len);
1129 SwapBytes(info.sendTime);
1133 memcpy(buffer, &info, sizeof(INFO_8));
1135 Encrypt(&user.ctx, buffer, buffer, len / 8);
1136 return Send(ip, user.port, buffer, len);
1138 //-----------------------------------------------------------------------------
1139 int AUTH_IA::Process_CONN_SYN_6(CONN_SYN_6 *, IA_USER * iaUser, uint32_t)
1141 if (!(iaUser->phase.GetPhase() == 1 || iaUser->phase.GetPhase() == 3))
1144 enabledDirs = 0xFFffFFff;
1146 iaUser->phase.SetPhase2();
1147 printfd(__FILE__, "Phase changed from %d to 2. Reason: CONN_SYN_6\n", iaUser->phase.GetPhase());
1150 //-----------------------------------------------------------------------------
1151 int AUTH_IA::Process_CONN_SYN_7(CONN_SYN_7 * connSyn, IA_USER * iaUser, uint32_t sip)
1153 return Process_CONN_SYN_6(connSyn, iaUser, sip);
1155 //-----------------------------------------------------------------------------
1156 int AUTH_IA::Process_CONN_SYN_8(CONN_SYN_8 * connSyn, IA_USER * iaUser, uint32_t sip)
1159 SwapBytes(connSyn->dirs);
1161 int ret = Process_CONN_SYN_6((CONN_SYN_6*)connSyn, iaUser, sip);
1162 enabledDirs = connSyn->dirs;
1165 //-----------------------------------------------------------------------------
1166 int AUTH_IA::Process_CONN_ACK_6(CONN_ACK_6 * connAck, IA_USER * iaUser, uint32_t sip)
1169 SwapBytes(connAck->len);
1170 SwapBytes(connAck->rnd);
1172 printfd( __FILE__, "CONN_ACK_6 %s\n", connAck->type);
1173 // ÕÓÔÁÎÏ×ÉÔØ ÎÏ×ÕÀ ÆÁÚÕ É ×ÒÅÍÑ É ÒÁÚÒÅÛÉÔØ ÉÎÅÔ
1174 if ((iaUser->phase.GetPhase() == 2) && (connAck->rnd == iaUser->rnd + 1))
1176 iaUser->phase.UpdateTime();
1178 iaUser->lastSendAlive = iaUser->phase.GetTime();
1179 if (users->Authorize(iaUser->login, sip, enabledDirs, this))
1181 iaUser->phase.SetPhase3();
1182 printfd(__FILE__, "Phase changed from 2 to 3. Reason: CONN_ACK_6\n");
1187 errorStr = iaUser->user->GetStrError();
1188 iaUser->phase.SetPhase1();
1190 printfd(__FILE__, "Phase changed from 2 to 1. Reason: failed to authorize user\n");
1194 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), connAck->rnd);
1197 //-----------------------------------------------------------------------------
1198 int AUTH_IA::Process_CONN_ACK_7(CONN_ACK_7 * connAck, IA_USER * iaUser, uint32_t sip)
1200 return Process_CONN_ACK_6(connAck, iaUser, sip);
1202 //-----------------------------------------------------------------------------
1203 int AUTH_IA::Process_CONN_ACK_8(CONN_ACK_8 * connAck, IA_USER * iaUser, uint32_t sip)
1206 SwapBytes(connAck->len);
1207 SwapBytes(connAck->rnd);
1209 printfd( __FILE__, "CONN_ACK_8 %s\n", connAck->type);
1211 if ((iaUser->phase.GetPhase() == 2) && (connAck->rnd == iaUser->rnd + 1))
1213 iaUser->phase.UpdateTime();
1214 iaUser->lastSendAlive = iaUser->phase.GetTime();
1215 if (users->Authorize(iaUser->login, sip, enabledDirs, this))
1217 iaUser->phase.SetPhase3();
1218 printfd(__FILE__, "Phase changed from 2 to 3. Reason: CONN_ACK_8\n");
1223 errorStr = iaUser->user->GetStrError();
1224 iaUser->phase.SetPhase1();
1226 printfd(__FILE__, "Phase changed from 2 to 1. Reason: failed to authorize user\n");
1230 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), connAck->rnd);
1233 //-----------------------------------------------------------------------------
1234 int AUTH_IA::Process_ALIVE_ACK_6(ALIVE_ACK_6 * aliveAck, IA_USER * iaUser, uint32_t)
1237 SwapBytes(aliveAck->len);
1238 SwapBytes(aliveAck->rnd);
1240 printfd(__FILE__, "ALIVE_ACK_6\n");
1241 if ((iaUser->phase.GetPhase() == 3) && (aliveAck->rnd == iaUser->rnd + 1))
1243 iaUser->phase.UpdateTime();
1245 iaUser->aliveSent = false;
1250 //-----------------------------------------------------------------------------
1251 int AUTH_IA::Process_ALIVE_ACK_7(ALIVE_ACK_7 * aliveAck, IA_USER * iaUser, uint32_t sip)
1253 return Process_ALIVE_ACK_6(aliveAck, iaUser, sip);
1255 //-----------------------------------------------------------------------------
1256 int AUTH_IA::Process_ALIVE_ACK_8(ALIVE_ACK_8 * aliveAck, IA_USER * iaUser, uint32_t)
1259 SwapBytes(aliveAck->len);
1260 SwapBytes(aliveAck->rnd);
1262 printfd(__FILE__, "ALIVE_ACK_8\n");
1263 if ((iaUser->phase.GetPhase() == 3) && (aliveAck->rnd == iaUser->rnd + 1))
1265 iaUser->phase.UpdateTime();
1267 iaUser->aliveSent = false;
1272 //-----------------------------------------------------------------------------
1273 int AUTH_IA::Process_DISCONN_SYN_6(DISCONN_SYN_6 *, IA_USER * iaUser, uint32_t)
1275 printfd(__FILE__, "DISCONN_SYN_6\n");
1276 if (iaUser->phase.GetPhase() != 3)
1278 printfd(__FILE__, "Invalid phase. Expected 3, actual %d\n", iaUser->phase.GetPhase());
1279 errorStr = "Incorrect request DISCONN_SYN";
1283 iaUser->phase.SetPhase4();
1284 printfd(__FILE__, "Phase changed from 3 to 4. Reason: DISCONN_SYN_6\n");
1288 //-----------------------------------------------------------------------------
1289 int AUTH_IA::Process_DISCONN_SYN_7(DISCONN_SYN_7 * disconnSyn, IA_USER * iaUser, uint32_t sip)
1291 return Process_DISCONN_SYN_6(disconnSyn, iaUser, sip);
1293 //-----------------------------------------------------------------------------
1294 int AUTH_IA::Process_DISCONN_SYN_8(DISCONN_SYN_8 *, IA_USER * iaUser, uint32_t)
1296 if (iaUser->phase.GetPhase() != 3)
1298 errorStr = "Incorrect request DISCONN_SYN";
1299 printfd(__FILE__, "Invalid phase. Expected 3, actual %d\n", iaUser->phase.GetPhase());
1303 iaUser->phase.SetPhase4();
1304 printfd(__FILE__, "Phase changed from 3 to 4. Reason: DISCONN_SYN_6\n");
1308 //-----------------------------------------------------------------------------
1309 int AUTH_IA::Process_DISCONN_ACK_6(DISCONN_ACK_6 * disconnAck,
1312 map<uint32_t, IA_USER>::iterator)
1315 SwapBytes(disconnAck->len);
1316 SwapBytes(disconnAck->rnd);
1318 printfd(__FILE__, "DISCONN_ACK_6\n");
1319 if (!((iaUser->phase.GetPhase() == 4) && (disconnAck->rnd == iaUser->rnd + 1)))
1321 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), disconnAck->rnd);
1327 //-----------------------------------------------------------------------------
1328 int AUTH_IA::Process_DISCONN_ACK_7(DISCONN_ACK_7 * disconnAck, IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1330 return Process_DISCONN_ACK_6(disconnAck, iaUser, sip, it);
1332 //-----------------------------------------------------------------------------
1333 int AUTH_IA::Process_DISCONN_ACK_8(DISCONN_ACK_8 * disconnAck, IA_USER * iaUser, uint32_t, map<uint32_t, IA_USER>::iterator)
1336 SwapBytes(disconnAck->len);
1337 SwapBytes(disconnAck->rnd);
1339 printfd(__FILE__, "DISCONN_ACK_8\n");
1340 if (!((iaUser->phase.GetPhase() == 4) && (disconnAck->rnd == iaUser->rnd + 1)))
1342 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), disconnAck->rnd);
1348 //-----------------------------------------------------------------------------
1349 int AUTH_IA::Send_CONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip)
1351 //+++ Fill static data in connSynAck +++
1352 // TODO Move this code. It must be executed only once
1353 connSynAck6.len = Min8(sizeof(CONN_SYN_ACK_6));
1354 strcpy((char*)connSynAck6.type, "CONN_SYN_ACK");
1355 for (int j = 0; j < DIR_NUM; j++)
1357 strncpy((char*)connSynAck6.dirName[j],
1358 stgSettings->GetDirName(j).c_str(),
1361 connSynAck6.dirName[j][sizeof(string16) - 1] = 0;
1363 //--- Fill static data in connSynAck ---
1365 iaUser->rnd = random();
1366 connSynAck6.rnd = iaUser->rnd;
1368 connSynAck6.userTimeOut = iaSettings.GetUserTimeout();
1369 connSynAck6.aliveDelay = iaSettings.GetUserDelay();
1372 SwapBytes(connSynAck6.len);
1373 SwapBytes(connSynAck6.rnd);
1374 SwapBytes(connSynAck6.userTimeOut);
1375 SwapBytes(connSynAck6.aliveDelay);
1378 Encrypt(&iaUser->ctx, (char*)&connSynAck6, (char*)&connSynAck6, Min8(sizeof(CONN_SYN_ACK_6))/8);
1379 return Send(sip, iaSettings.GetUserPort(), (char*)&connSynAck6, Min8(sizeof(CONN_SYN_ACK_6)));;
1381 //-----------------------------------------------------------------------------
1382 int AUTH_IA::Send_CONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip)
1384 return Send_CONN_SYN_ACK_6(iaUser, sip);
1386 //-----------------------------------------------------------------------------
1387 int AUTH_IA::Send_CONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip)
1389 strcpy((char*)connSynAck8.hdr.magic, IA_ID);
1390 connSynAck8.hdr.protoVer[0] = 0;
1391 connSynAck8.hdr.protoVer[1] = 8;
1393 //+++ Fill static data in connSynAck +++
1394 // TODO Move this code. It must be executed only once
1395 connSynAck8.len = Min8(sizeof(CONN_SYN_ACK_8));
1396 strcpy((char*)connSynAck8.type, "CONN_SYN_ACK");
1397 for (int j = 0; j < DIR_NUM; j++)
1399 strncpy((char*)connSynAck8.dirName[j],
1400 stgSettings->GetDirName(j).c_str(),
1403 connSynAck8.dirName[j][sizeof(string16) - 1] = 0;
1405 //--- Fill static data in connSynAck ---
1407 iaUser->rnd = random();
1408 connSynAck8.rnd = iaUser->rnd;
1410 connSynAck8.userTimeOut = iaSettings.GetUserTimeout();
1411 connSynAck8.aliveDelay = iaSettings.GetUserDelay();
1414 SwapBytes(connSynAck8.len);
1415 SwapBytes(connSynAck8.rnd);
1416 SwapBytes(connSynAck8.userTimeOut);
1417 SwapBytes(connSynAck8.aliveDelay);
1420 Encrypt(&iaUser->ctx, (char*)&connSynAck8, (char*)&connSynAck8, Min8(sizeof(CONN_SYN_ACK_8))/8);
1421 return Send(sip, iaUser->port, (char*)&connSynAck8, Min8(sizeof(CONN_SYN_ACK_8)));
1423 //-----------------------------------------------------------------------------
1424 int AUTH_IA::Send_ALIVE_SYN_6(IA_USER * iaUser, uint32_t sip)
1426 aliveSyn6.len = Min8(sizeof(ALIVE_SYN_6));
1427 aliveSyn6.rnd = iaUser->rnd = random();
1429 strcpy((char*)aliveSyn6.type, "ALIVE_SYN");
1431 for (int i = 0; i < DIR_NUM; i++)
1433 aliveSyn6.md[i] = iaUser->user->GetProperty().down.Get()[i];
1434 aliveSyn6.mu[i] = iaUser->user->GetProperty().up.Get()[i];
1436 aliveSyn6.sd[i] = iaUser->user->GetSessionDownload()[i];
1437 aliveSyn6.su[i] = iaUser->user->GetSessionUpload()[i];
1441 int dn = iaSettings.GetFreeMbShowType();
1442 const TARIFF * tf = iaUser->user->GetTariff();
1446 double p = tf->GetPriceWithTraffType(aliveSyn6.mu[dn],
1453 snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "---");
1457 double fmb = iaUser->user->GetProperty().freeMb;
1458 fmb = fmb < 0 ? 0 : fmb;
1459 snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "%.3f", fmb / p);
1464 if (freeMbNone == iaSettings.GetFreeMbShowType())
1466 aliveSyn6.freeMb[0] = 0;
1470 double fmb = iaUser->user->GetProperty().freeMb;
1471 fmb = fmb < 0 ? 0 : fmb;
1472 snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "C%.3f", fmb);
1477 if (iaUser->aliveSent)
1479 printfd(__FILE__, "========= ALIVE_ACK_6(7) TIMEOUT !!! %s =========\n", iaUser->login.c_str());
1481 iaUser->aliveSent = true;
1484 aliveSyn6.cash =(int64_t) (iaUser->user->GetProperty().cash.Get() * 1000.0);
1485 if (!stgSettings->GetShowFeeInCash())
1486 aliveSyn6.cash -= (int64_t)(tf->GetFee() * 1000.0);
1489 SwapBytes(aliveSyn6.len);
1490 SwapBytes(aliveSyn6.rnd);
1491 SwapBytes(aliveSyn6.cash);
1492 for (int i = 0; i < DIR_NUM; ++i)
1494 SwapBytes(aliveSyn6.mu[i]);
1495 SwapBytes(aliveSyn6.md[i]);
1496 SwapBytes(aliveSyn6.su[i]);
1497 SwapBytes(aliveSyn6.sd[i]);
1501 Encrypt(&(iaUser->ctx), (char*)&aliveSyn6, (char*)&aliveSyn6, Min8(sizeof(aliveSyn6))/8);
1502 return Send(sip, iaSettings.GetUserPort(), (char*)&aliveSyn6, Min8(sizeof(aliveSyn6)));
1504 //-----------------------------------------------------------------------------
1505 int AUTH_IA::Send_ALIVE_SYN_7(IA_USER * iaUser, uint32_t sip)
1507 return Send_ALIVE_SYN_6(iaUser, sip);
1509 //-----------------------------------------------------------------------------
1510 int AUTH_IA::Send_ALIVE_SYN_8(IA_USER * iaUser, uint32_t sip)
1512 strcpy((char*)aliveSyn8.hdr.magic, IA_ID);
1513 aliveSyn8.hdr.protoVer[0] = 0;
1514 aliveSyn8.hdr.protoVer[1] = 8;
1516 aliveSyn8.len = Min8(sizeof(ALIVE_SYN_8));
1517 aliveSyn8.rnd = iaUser->rnd = random();
1519 strcpy((char*)aliveSyn8.type, "ALIVE_SYN");
1521 for (int i = 0; i < DIR_NUM; i++)
1523 aliveSyn8.md[i] = iaUser->user->GetProperty().down.Get()[i];
1524 aliveSyn8.mu[i] = iaUser->user->GetProperty().up.Get()[i];
1526 aliveSyn8.sd[i] = iaUser->user->GetSessionDownload()[i];
1527 aliveSyn8.su[i] = iaUser->user->GetSessionUpload()[i];
1531 int dn = iaSettings.GetFreeMbShowType();
1535 const TARIFF * tf = iaUser->user->GetTariff();
1536 double p = tf->GetPriceWithTraffType(aliveSyn8.mu[dn],
1543 snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "---");
1547 double fmb = iaUser->user->GetProperty().freeMb;
1548 fmb = fmb < 0 ? 0 : fmb;
1549 snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "%.3f", fmb / p);
1554 if (freeMbNone == iaSettings.GetFreeMbShowType())
1556 aliveSyn8.freeMb[0] = 0;
1560 double fmb = iaUser->user->GetProperty().freeMb;
1561 fmb = fmb < 0 ? 0 : fmb;
1562 snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "C%.3f", fmb);
1567 if (iaUser->aliveSent)
1569 printfd(__FILE__, "========= ALIVE_ACK_8 TIMEOUT !!! =========\n");
1571 iaUser->aliveSent = true;
1574 const TARIFF * tf = iaUser->user->GetTariff();
1576 aliveSyn8.cash =(int64_t) (iaUser->user->GetProperty().cash.Get() * 1000.0);
1577 if (!stgSettings->GetShowFeeInCash())
1578 aliveSyn8.cash -= (int64_t)(tf->GetFee() * 1000.0);
1581 SwapBytes(aliveSyn8.len);
1582 SwapBytes(aliveSyn8.rnd);
1583 SwapBytes(aliveSyn8.cash);
1584 SwapBytes(aliveSyn8.status);
1585 for (int i = 0; i < DIR_NUM; ++i)
1587 SwapBytes(aliveSyn8.mu[i]);
1588 SwapBytes(aliveSyn8.md[i]);
1589 SwapBytes(aliveSyn8.su[i]);
1590 SwapBytes(aliveSyn8.sd[i]);
1594 Encrypt(&(iaUser->ctx), (char*)&aliveSyn8, (char*)&aliveSyn8, Min8(sizeof(aliveSyn8))/8);
1595 return Send(sip, iaUser->port, (char*)&aliveSyn8, Min8(sizeof(aliveSyn8)));
1597 //-----------------------------------------------------------------------------
1598 int AUTH_IA::Send_DISCONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip)
1600 disconnSynAck6.len = Min8(sizeof(DISCONN_SYN_ACK_6));
1601 strcpy((char*)disconnSynAck6.type, "DISCONN_SYN_ACK");
1602 disconnSynAck6.rnd = iaUser->rnd = random();
1605 SwapBytes(disconnSynAck6.len);
1606 SwapBytes(disconnSynAck6.rnd);
1609 Encrypt(&iaUser->ctx, (char*)&disconnSynAck6, (char*)&disconnSynAck6, Min8(sizeof(disconnSynAck6))/8);
1610 return Send(sip, iaSettings.GetUserPort(), (char*)&disconnSynAck6, Min8(sizeof(disconnSynAck6)));
1612 //-----------------------------------------------------------------------------
1613 int AUTH_IA::Send_DISCONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip)
1615 return Send_DISCONN_SYN_ACK_6(iaUser, sip);
1617 //-----------------------------------------------------------------------------
1618 int AUTH_IA::Send_DISCONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip)
1620 strcpy((char*)disconnSynAck8.hdr.magic, IA_ID);
1621 disconnSynAck8.hdr.protoVer[0] = 0;
1622 disconnSynAck8.hdr.protoVer[1] = 8;
1624 disconnSynAck8.len = Min8(sizeof(DISCONN_SYN_ACK_8));
1625 strcpy((char*)disconnSynAck8.type, "DISCONN_SYN_ACK");
1626 disconnSynAck8.rnd = iaUser->rnd = random();
1629 SwapBytes(disconnSynAck8.len);
1630 SwapBytes(disconnSynAck8.rnd);
1633 Encrypt(&iaUser->ctx, (char*)&disconnSynAck8, (char*)&disconnSynAck8, Min8(sizeof(disconnSynAck8))/8);
1634 return Send(sip, iaUser->port, (char*)&disconnSynAck8, Min8(sizeof(disconnSynAck8)));
1636 //-----------------------------------------------------------------------------
1637 int AUTH_IA::Send_FIN_6(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1639 fin6.len = Min8(sizeof(FIN_6));
1640 strcpy((char*)fin6.type, "FIN");
1641 strcpy((char*)fin6.ok, "OK");
1644 SwapBytes(fin6.len);
1647 Encrypt(&iaUser->ctx, (char*)&fin6, (char*)&fin6, Min8(sizeof(fin6))/8);
1649 users->Unauthorize(iaUser->login, this);
1652 return Send(sip, iaSettings.GetUserPort(), (char*)&fin6, Min8(sizeof(fin6)));
1654 //-----------------------------------------------------------------------------
1655 int AUTH_IA::Send_FIN_7(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1657 return Send_FIN_6(iaUser, sip, it);
1659 //-----------------------------------------------------------------------------
1660 int AUTH_IA::Send_FIN_8(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1662 strcpy((char*)fin8.hdr.magic, IA_ID);
1663 fin8.hdr.protoVer[0] = 0;
1664 fin8.hdr.protoVer[1] = 8;
1666 fin8.len = Min8(sizeof(FIN_8));
1667 strcpy((char*)fin8.type, "FIN");
1668 strcpy((char*)fin8.ok, "OK");
1671 SwapBytes(fin8.len);
1674 Encrypt(&iaUser->ctx, (char*)&fin8, (char*)&fin8, Min8(sizeof(fin8))/8);
1676 users->Unauthorize(iaUser->login, this);
1679 return Send(sip, iaUser->port, (char*)&fin8, Min8(sizeof(fin8)));
1681 //-----------------------------------------------------------------------------
1683 void InitEncrypt(BLOWFISH_CTX * ctx, const string & password)
1685 unsigned char keyL[PASSWD_LEN];
1686 memset(keyL, 0, PASSWD_LEN);
1687 strncpy((char *)keyL, password.c_str(), PASSWD_LEN);
1688 Blowfish_Init(ctx, keyL, PASSWD_LEN);
1690 //-----------------------------------------------------------------------------
1692 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
1694 for (int i = 0; i < len8; i++)
1695 DecodeString(dst + i * 8, src + i * 8, ctx);
1697 //-----------------------------------------------------------------------------
1699 void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
1701 for (int i = 0; i < len8; i++)
1702 EncodeString(dst + i * 8, src + i * 8, ctx);