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");
216 /*int64_t tn = newPhaseTime.GetSec()*1000000 + newPhaseTime.GetUSec();
217 int64_t to = phaseTime.GetSec()*1000000 + phaseTime.GetUSec();*/
220 string action = newPhase == phase ? "U" : "C";
221 double delta = newPhaseTime.GetSec() - phaseTime.GetSec();
222 delta += (newPhaseTime.GetUSec() - phaseTime.GetUSec()) * 1.0e-6;
223 fprintf(flog, "IA %s %s oldPhase = %d, newPhase = %d. dt = %.6f\n",
233 //-----------------------------------------------------------------------------
234 void IA_PHASE::SetPhase1()
236 #ifdef IA_PHASE_DEBUG
240 gettimeofday(&phaseTime, NULL);
242 //-----------------------------------------------------------------------------
243 void IA_PHASE::SetPhase2()
245 #ifdef IA_PHASE_DEBUG
249 gettimeofday(&phaseTime, NULL);
251 //-----------------------------------------------------------------------------
252 void IA_PHASE::SetPhase3()
254 #ifdef IA_PHASE_DEBUG
258 gettimeofday(&phaseTime, NULL);
260 //-----------------------------------------------------------------------------
261 void IA_PHASE::SetPhase4()
263 #ifdef IA_PHASE_DEBUG
267 gettimeofday(&phaseTime, NULL);
269 //-----------------------------------------------------------------------------
270 void IA_PHASE::SetPhase5()
272 #ifdef IA_PHASE_DEBUG
276 gettimeofday(&phaseTime, NULL);
278 //-----------------------------------------------------------------------------
279 int IA_PHASE::GetPhase() const
283 //-----------------------------------------------------------------------------
284 void IA_PHASE::UpdateTime()
286 #ifdef IA_PHASE_DEBUG
287 WritePhaseChange(phase);
289 gettimeofday(&phaseTime, NULL);
291 //-----------------------------------------------------------------------------
292 const UTIME & IA_PHASE::GetTime() const
296 //-----------------------------------------------------------------------------
297 //-----------------------------------------------------------------------------
298 //-----------------------------------------------------------------------------
302 isRunningRunTimeouter(false),
306 WriteServLog(GetStgLogger()),
307 enabledDirs(0xFFffFFff),
308 onDelUserNotifier(*this)
310 InitEncrypt(&ctxS, "pr7Hhen");
312 pthread_mutexattr_t attr;
313 pthread_mutexattr_init(&attr);
314 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
315 pthread_mutex_init(&mutex, &attr);
317 memset(&connSynAck6, 0, sizeof(CONN_SYN_ACK_6));
318 memset(&connSynAck8, 0, sizeof(CONN_SYN_ACK_8));
319 memset(&disconnSynAck6, 0, sizeof(DISCONN_SYN_ACK_6));
320 memset(&disconnSynAck8, 0, sizeof(DISCONN_SYN_ACK_8));
321 memset(&aliveSyn6, 0, sizeof(ALIVE_SYN_6));
322 memset(&aliveSyn8, 0, sizeof(ALIVE_SYN_8));
323 memset(&fin6, 0, sizeof(FIN_6));
324 memset(&fin8, 0, sizeof(FIN_8));
326 printfd(__FILE__, "sizeof(CONN_SYN_6) = %d %d\n", sizeof(CONN_SYN_6), Min8(sizeof(CONN_SYN_6)));
327 printfd(__FILE__, "sizeof(CONN_SYN_8) = %d %d\n", sizeof(CONN_SYN_8), Min8(sizeof(CONN_SYN_8)));
328 printfd(__FILE__, "sizeof(CONN_SYN_ACK_6) = %d %d\n", sizeof(CONN_SYN_ACK_6), Min8(sizeof(CONN_SYN_ACK_6)));
329 printfd(__FILE__, "sizeof(CONN_SYN_ACK_8) = %d %d\n", sizeof(CONN_SYN_ACK_8), Min8(sizeof(CONN_SYN_ACK_8)));
330 printfd(__FILE__, "sizeof(CONN_ACK_6) = %d %d\n", sizeof(CONN_ACK_6), Min8(sizeof(CONN_ACK_6)));
331 printfd(__FILE__, "sizeof(ALIVE_SYN_6) = %d %d\n", sizeof(ALIVE_SYN_6), Min8(sizeof(ALIVE_SYN_6)));
332 printfd(__FILE__, "sizeof(ALIVE_SYN_8) = %d %d\n", sizeof(ALIVE_SYN_8), Min8(sizeof(ALIVE_SYN_8)));
333 printfd(__FILE__, "sizeof(ALIVE_ACK_6) = %d %d\n", sizeof(ALIVE_ACK_6), Min8(sizeof(ALIVE_ACK_6)));
334 printfd(__FILE__, "sizeof(DISCONN_SYN_6) = %d %d\n", sizeof(DISCONN_SYN_6), Min8(sizeof(DISCONN_SYN_6)));
335 printfd(__FILE__, "sizeof(DISCONN_SYN_ACK_6) = %d %d\n", sizeof(DISCONN_SYN_ACK_6), Min8(sizeof(DISCONN_SYN_ACK_6)));
336 printfd(__FILE__, "sizeof(DISCONN_SYN_ACK_8) = %d %d\n", sizeof(DISCONN_SYN_ACK_8), Min8(sizeof(DISCONN_SYN_ACK_8)));
337 printfd(__FILE__, "sizeof(DISCONN_ACK_6) = %d %d\n", sizeof(DISCONN_ACK_6), Min8(sizeof(DISCONN_ACK_6)));
338 printfd(__FILE__, "sizeof(FIN_6) = %d %d\n", sizeof(FIN_6), Min8(sizeof(FIN_6)));
339 printfd(__FILE__, "sizeof(FIN_8) = %d %d\n", sizeof(FIN_8), Min8(sizeof(FIN_8)));
340 printfd(__FILE__, "sizeof(ERR) = %d %d\n", sizeof(ERR), Min8(sizeof(ERR)));
341 printfd(__FILE__, "sizeof(INFO_6) = %d %d\n", sizeof(INFO_6), Min8(sizeof(INFO_6)));
342 printfd(__FILE__, "sizeof(INFO_7) = %d %d\n", sizeof(INFO_7), Min8(sizeof(INFO_7)));
343 printfd(__FILE__, "sizeof(INFO_8) = %d %d\n", sizeof(INFO_8), Min8(sizeof(INFO_8)));
345 packetTypes["CONN_SYN"] = CONN_SYN_N;
346 packetTypes["CONN_SYN_ACK"] = CONN_SYN_ACK_N;
347 packetTypes["CONN_ACK"] = CONN_ACK_N;
348 packetTypes["ALIVE_SYN"] = ALIVE_SYN_N;
349 packetTypes["ALIVE_ACK"] = ALIVE_ACK_N;
350 packetTypes["DISCONN_SYN"] = DISCONN_SYN_N;
351 packetTypes["DISCONN_SYN_ACK"] = DISCONN_SYN_ACK_N;
352 packetTypes["DISCONN_ACK"] = DISCONN_ACK_N;
353 packetTypes["FIN"] = FIN_N;
354 packetTypes["ERR"] = ERROR_N;
356 //-----------------------------------------------------------------------------
359 pthread_mutex_destroy(&mutex);
361 //-----------------------------------------------------------------------------
364 users->AddNotifierUserDel(&onDelUserNotifier);
374 if (pthread_create(&recvThread, NULL, Run, this))
376 errorStr = "Cannot create thread.";
377 printfd(__FILE__, "Cannot create recv thread\n");
382 if (!isRunningRunTimeouter)
384 if (pthread_create(&timeouterThread, NULL, RunTimeouter, this))
386 errorStr = "Cannot create thread.";
387 printfd(__FILE__, "Cannot create timeouter thread\n");
394 //-----------------------------------------------------------------------------
405 UnauthorizeUser(this)
410 //5 seconds to thread stops itself
411 for (int i = 0; i < 25 && isRunningRun; i++)
416 //after 5 seconds waiting thread still running. now killing it
419 //TODO pthread_cancel()
420 if (pthread_kill(recvThread, SIGINT))
422 errorStr = "Cannot kill thread.";
423 printfd(__FILE__, "Cannot kill thread\n");
426 for (int i = 0; i < 25 && isRunningRun; ++i)
430 printfd(__FILE__, "Failed to stop recv thread\n");
434 pthread_join(recvThread, NULL);
436 printfd(__FILE__, "AUTH_IA killed Run\n");
442 if (isRunningRunTimeouter)
444 //5 seconds to thread stops itself
445 for (int i = 0; i < 25 && isRunningRunTimeouter; i++)
450 //after 5 seconds waiting thread still running. now killing it
451 if (isRunningRunTimeouter)
453 //TODO pthread_cancel()
454 if (pthread_kill(timeouterThread, SIGINT))
456 errorStr = "Cannot kill thread.";
459 for (int i = 0; i < 25 && isRunningRunTimeouter; ++i)
461 if (isRunningRunTimeouter)
463 printfd(__FILE__, "Failed to stop timeouter thread\n");
467 pthread_join(timeouterThread, NULL);
469 printfd(__FILE__, "AUTH_IA killed Timeouter\n");
472 printfd(__FILE__, "AUTH_IA::Stoped successfully.\n");
473 users->DelNotifierUserDel(&onDelUserNotifier);
476 //-----------------------------------------------------------------------------
477 void * AUTH_IA::Run(void * d)
479 AUTH_IA * ia = static_cast<AUTH_IA *>(d);
481 ia->isRunningRun = true;
485 time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
489 ia->RecvData(buffer, sizeof(buffer));
490 if ((touchTime + MONITOR_TIME_DELAY_SEC <= stgTime) && ia->stgSettings->GetMonitoring())
493 string monFile = ia->stgSettings->GetMonitorDir() + "/inetaccess_r";
494 TouchFile(monFile.c_str());
498 ia->isRunningRun = false;
501 //-----------------------------------------------------------------------------
502 void * AUTH_IA::RunTimeouter(void * d)
504 AUTH_IA * ia = static_cast<AUTH_IA *>(d);
506 ia->isRunningRunTimeouter = true;
509 string monFile = ia->stgSettings->GetMonitorDir() + "/inetaccess_t";
514 // TODO change counter to timer and MONITOR_TIME_DELAY_SEC
515 if (++a % (50 * 60) == 0 && ia->stgSettings->GetMonitoring())
517 TouchFile(monFile.c_str());
521 ia->isRunningRunTimeouter = false;
524 //-----------------------------------------------------------------------------
525 int AUTH_IA::ParseSettings()
527 int ret = iaSettings.ParseSettings(settings);
529 errorStr = iaSettings.GetStrError();
532 //-----------------------------------------------------------------------------
533 int AUTH_IA::PrepareNet()
535 struct sockaddr_in listenAddr;
537 listenSocket = socket(AF_INET, SOCK_DGRAM, 0);
539 if (listenSocket < 0)
541 errorStr = "Cannot create socket.";
545 listenAddr.sin_family = AF_INET;
546 listenAddr.sin_port = htons(iaSettings.GetUserPort());
547 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
549 if (bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr)) < 0)
551 errorStr = "AUTH_IA: Bind failed.";
557 //-----------------------------------------------------------------------------
558 int AUTH_IA::FinalizeNet()
563 //-----------------------------------------------------------------------------
564 int AUTH_IA::RecvData(char * buffer, int bufferSize)
566 if (!WaitPackets(listenSocket)) // Timeout
571 struct sockaddr_in outerAddr;
572 socklen_t outerAddrLen(sizeof(outerAddr));
573 int dataLen = recvfrom(listenSocket, buffer, bufferSize, 0, (struct sockaddr *)&outerAddr, &outerAddrLen);
580 if (dataLen <= 0) // Error
584 printfd(__FILE__, "recvfrom res=%d, error: '%s'\n", dataLen, strerror(errno));
594 if (CheckHeader(buffer, &protoVer))
597 char login[PASSWD_LEN]; //TODO why PASSWD_LEN ?
598 memset(login, 0, PASSWD_LEN);
600 Decrypt(&ctxS, login, buffer + 8, PASSWD_LEN / 8);
602 uint32_t sip = outerAddr.sin_addr.s_addr;
603 uint16_t sport = htons(outerAddr.sin_port);
606 if (users->FindByName(login, &user) == 0)
608 printfd(__FILE__, "User %s FOUND!\n", user->GetLogin().c_str());
609 PacketProcessor(buffer, dataLen, sip, sport, protoVer, user);
613 WriteServLog("User\'s connect failed:: user \'%s\' not found. IP \'%s\'",
615 inet_ntostring(sip).c_str());
616 printfd(__FILE__, "User %s NOT found!\n", login);
617 SendError(sip, sport, protoVer, "îÅÐÒÁ×ÉÌØÎÙÊ ÌÏÇÉÎ ÉÌÉ ÐÁÒÏÌØ!");
622 //-----------------------------------------------------------------------------
623 int AUTH_IA::CheckHeader(const char * buffer, int * protoVer)
625 if (strncmp(IA_ID, buffer, strlen(IA_ID)) != 0)
627 //SendError(userIP, updateMsg);
628 printfd(__FILE__, "update needed - IA_ID\n");
629 //SendError(userIP, "Incorrect header!");
633 if (buffer[6] != 0) //proto[0] shoud be 0
635 printfd(__FILE__, "update needed - PROTO major: %d\n", buffer[6]);
636 //SendError(userIP, updateMsg);
643 //SendError(userIP, updateMsg);
644 printfd(__FILE__, "update needed - PROTO minor: %d\n", buffer[7]);
649 *protoVer = buffer[7];
653 //-----------------------------------------------------------------------------
654 int AUTH_IA::Timeouter()
656 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
658 map<uint32_t, IA_USER>::iterator it;
659 it = ip2user.begin();
662 while (it != ip2user.end())
666 static UTIME currTime;
667 gettimeofday(&currTime, NULL);
669 if ((it->second.phase.GetPhase() == 2)
670 && (currTime - it->second.phase.GetTime()) > iaSettings.GetUserDelay())
672 it->second.phase.SetPhase1();
673 printfd(__FILE__, "Phase changed from 2 to 1. Reason: timeout\n");
678 if (it->second.phase.GetPhase() == 3)
680 if (!it->second.messagesToSend.empty())
682 if (it->second.protoVer == 6)
683 RealSendMessage6(*it->second.messagesToSend.begin(), sip, it->second);
685 if (it->second.protoVer == 7)
686 RealSendMessage7(*it->second.messagesToSend.begin(), sip, it->second);
688 if (it->second.protoVer == 8)
689 RealSendMessage8(*it->second.messagesToSend.begin(), sip, it->second);
691 it->second.messagesToSend.erase(it->second.messagesToSend.begin());
694 if((currTime - it->second.lastSendAlive) > iaSettings.GetUserDelay())
696 switch (it->second.protoVer)
699 Send_ALIVE_SYN_6(&(it->second), sip);
702 Send_ALIVE_SYN_7(&(it->second), sip);
705 Send_ALIVE_SYN_8(&(it->second), sip);
709 gettimeofday(&it->second.lastSendAlive, NULL);
712 if ((currTime - it->second.phase.GetTime()) > iaSettings.GetUserTimeout())
714 users->Unauthorize(it->second.user->GetLogin(), this);
720 if ((it->second.phase.GetPhase() == 4)
721 && ((currTime - it->second.phase.GetTime()) > iaSettings.GetUserDelay()))
723 it->second.phase.SetPhase3();
724 printfd(__FILE__, "Phase changed from 4 to 3. Reason: timeout\n");
732 //-----------------------------------------------------------------------------
733 int AUTH_IA::PacketProcessor(char * buff, int dataLen, uint32_t sip, uint16_t sport, int protoVer, USER_PTR user)
735 std::string login(user->GetLogin());
736 // ôÕÔ ÓÏÂÒÁÎÙ ÏÂÒÁÂÏÔÞÉËÉ ÒÁÚÎÙÈ ÐÁËÅÔÏ×
737 const int offset = LOGIN_LEN + 2 + 6; // LOGIN_LEN + sizeOfMagic + sizeOfVer;
739 IA_USER * iaUser = NULL;
741 CONN_SYN_6 * connSyn6;
742 CONN_SYN_7 * connSyn7;
743 CONN_SYN_8 * connSyn8;
745 CONN_ACK_6 * connAck;
746 ALIVE_ACK_6 * aliveAck;
747 DISCONN_SYN_6 * disconnSyn;
748 DISCONN_ACK_6 * disconnAck;
750 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
751 map<uint32_t, IA_USER>::iterator it(ip2user.find(sip));
753 if (it == ip2user.end())
755 // åÝÅ ÎÅ ÂÙÌÏ ÚÁÐÒÏÓÏ× Ó ÜÔÏÇÏ IP
756 printfd(__FILE__, "Add new user\n");
757 ip2user[sip].login = user->GetLogin();
758 ip2user[sip].user = user;
759 ip2user[sip].protoVer = protoVer;
760 ip2user[sip].port = sport;
761 #ifdef IA_PHASE_DEBUG
762 ip2user[sip].phase.SetLogFileName(stgSettings->GetLogFileName());
763 ip2user[sip].phase.SetUserLogin(login);
767 it = ip2user.find(sip); //TODO
768 if (it == ip2user.end())
770 printfd(__FILE__, "+++ ERROR +++\n");
774 else if (user->GetID() != it->second.user->GetID())
776 printfd(__FILE__, "IP address already in use by user '%s'. IP %s, login: '%s'\n",
777 it->second.user->GetLogin().c_str(),
778 inet_ntostring(sip).c_str(),
779 user->GetLogin().c_str());
780 WriteServLog("IP address already in use by user '%s'. IP %s, login: '%s'",
781 it->second.user->GetLogin().c_str(),
782 inet_ntostring(sip).c_str(),
783 user->GetLogin().c_str());
784 SendError(sip, sport, protoVer, "÷ÁÛ IP ÁÄÒÅÓ ÕÖÅ ÉÓÐÏÌØÚÕÅÔÓÑ!");
788 iaUser = &(it->second);
790 if (iaUser->port != sport)
791 iaUser->port = sport;
793 if (iaUser->password != user->GetProperty().password.Get())
795 InitEncrypt(&iaUser->ctx, user->GetProperty().password.Get());
796 iaUser->password = user->GetProperty().password.Get();
800 Decrypt(&iaUser->ctx, buff, buff, (dataLen - offset) / 8);
802 char packetName[IA_MAX_TYPE_LEN];
803 strncpy(packetName, buff + 4, IA_MAX_TYPE_LEN);
804 packetName[IA_MAX_TYPE_LEN - 1] = 0;
806 map<string, int>::iterator pi(packetTypes.find(packetName));
807 if (pi == packetTypes.end())
809 SendError(sip, sport, protoVer, "îÅÐÒÁ×ÉÌØÎÙÊ ÌÏÇÉÎ ÉÌÉ ÐÁÒÏÌØ!");
810 printfd(__FILE__, "Login or password is wrong!\n");
811 WriteServLog("User's connect failed. IP \'%s\'. Wrong login or password", inet_ntostring(sip).c_str());
815 if (user->GetProperty().disabled.Get())
817 SendError(sip, sport, protoVer, "õÞÅÔÎÁÑ ÚÁÐÉÓØ ÚÁÂÌÏËÉÒÏ×ÁÎÁ");
821 if (user->GetProperty().passive.Get())
823 SendError(sip, sport, protoVer, "õÞÅÔÎÁÑ ÚÁÐÉÓØ ÚÁÍÏÒÏÖÅÎÁ");
827 if (user->IsAuthorizedBy(this) && user->GetCurrIP() != sip)
829 printfd(__FILE__, "Login %s already in use from ip %s. IP %s\n",
830 login.c_str(), inet_ntostring(user->GetCurrIP()).c_str(),
831 inet_ntostring(sip).c_str());
832 WriteServLog("Login %s already in use from ip %s. IP %s",
834 inet_ntostring(user->GetCurrIP()).c_str(),
835 inet_ntostring(sip).c_str());
836 SendError(sip, sport, protoVer, "÷ÁÛ ÌÏÇÉÎ ÕÖÅ ÉÓÐÏÌØÚÕÅÔÓÑ!");
840 // ôÅÐÅÒØ ÍÙ ÄÏÌÖÎÙ ÐÒÏ×ÅÒÉÔØ, ÍÏÖÅÔ ÌÉ ÐÏÌØÚÏ×ÁÔÅÌØ ÐÏÄËÌÀÞÉÔÓÑ Ó ÜÔÏÇÏ ÁÄÒÅÓÁ.
841 int ipFound = user->GetProperty().ips.Get().IsIPInIPS(sip);
844 printfd(__FILE__, "User %s. IP address is incorrect. IP %s\n",
845 login.c_str(), inet_ntostring(sip).c_str());
846 WriteServLog("User %s. IP address is incorrect. IP %s",
847 login.c_str(), inet_ntostring(sip).c_str());
848 SendError(sip, sport, protoVer, "ðÏÌØÚÏ×ÁÔÅÌØ ÎÅ ÏÐÏÚÎÁÎ! ðÒÏ×ÅÒØÔÅ IP ÁÄÒÅÓ.");
860 connSyn6 = (CONN_SYN_6*)(buff - offset);
861 ret = Process_CONN_SYN_6(connSyn6, &(it->second), sip);
864 connSyn7 = (CONN_SYN_7*)(buff - offset);
865 ret = Process_CONN_SYN_7(connSyn7, &(it->second), sip);
868 connSyn8 = (CONN_SYN_8*)(buff - offset);
869 ret = Process_CONN_SYN_8(connSyn8, &(it->second), sip);
880 Send_CONN_SYN_ACK_6(iaUser, sip);
883 Send_CONN_SYN_ACK_7(iaUser, sip);
886 Send_CONN_SYN_ACK_8(iaUser, sip);
892 connAck = (CONN_ACK_6*)(buff - offset);
896 ret = Process_CONN_ACK_6(connAck, iaUser, sip);
899 ret = Process_CONN_ACK_7(connAck, iaUser, sip);
902 ret = Process_CONN_ACK_8((CONN_ACK_8*)(buff - offset), iaUser, sip);
908 SendError(sip, sport, protoVer, errorStr);
915 Send_ALIVE_SYN_6(iaUser, sip);
918 Send_ALIVE_SYN_7(iaUser, sip);
921 Send_ALIVE_SYN_8(iaUser, sip);
927 // ðÒÉÂÙÌ ÏÔ×ÅÔ Ó ÐÏÄÔ×ÅÒÖÄÅÎÉÅÍ ALIVE
929 aliveAck = (ALIVE_ACK_6*)(buff - offset);
933 ret = Process_ALIVE_ACK_6(aliveAck, iaUser, sip);
936 ret = Process_ALIVE_ACK_7(aliveAck, iaUser, sip);
939 ret = Process_ALIVE_ACK_8((ALIVE_ACK_8*)(buff - offset), iaUser, sip);
944 // úÁÐÒÏÓ ÎÁ ÏÔËÌÀÞÅÎÉÅ
947 disconnSyn = (DISCONN_SYN_6*)(buff - offset);
951 ret = Process_DISCONN_SYN_6(disconnSyn, iaUser, sip);
954 ret = Process_DISCONN_SYN_7(disconnSyn, iaUser, sip);
957 ret = Process_DISCONN_SYN_8((DISCONN_SYN_8*)(buff - offset), iaUser, sip);
967 Send_DISCONN_SYN_ACK_6(iaUser, sip);
970 Send_DISCONN_SYN_ACK_7(iaUser, sip);
973 Send_DISCONN_SYN_ACK_8(iaUser, sip);
979 disconnAck = (DISCONN_ACK_6*)(buff - offset);
984 ret = Process_DISCONN_ACK_6(disconnAck, iaUser, sip, it);
987 ret = Process_DISCONN_ACK_7(disconnAck, iaUser, sip, it);
990 ret = Process_DISCONN_ACK_8((DISCONN_ACK_8*)(buff - offset), iaUser, sip, it);
997 Send_FIN_6(iaUser, sip, it);
1000 Send_FIN_7(iaUser, sip, it);
1003 Send_FIN_8(iaUser, sip, it);
1011 //-----------------------------------------------------------------------------
1012 void AUTH_IA::DelUser(USER_PTR u)
1014 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1016 uint32_t ip = u->GetCurrIP();
1021 map<uint32_t, IA_USER>::iterator it;
1022 it = ip2user.find(ip);
1023 if (it == ip2user.end())
1026 printfd(__FILE__, "Nothing to delete\n");
1030 if (it->second.user == u)
1032 printfd(__FILE__, "User removed!\n");
1033 users->Unauthorize(u->GetLogin(), this);
1037 //-----------------------------------------------------------------------------
1038 int AUTH_IA::SendError(uint32_t ip, uint16_t port, int protoVer, const string & text)
1040 struct sockaddr_in sendAddr;
1047 memset(&err, 0, sizeof(ERR));
1049 sendAddr.sin_family = AF_INET;
1050 sendAddr.sin_port = htons(port);
1052 sendAddr.sin_addr.s_addr = ip;// IP ÐÏÌØÚÏ×ÁÔÅÌÑ
1055 strncpy((char*)err.type, "ERR", 16);
1056 strncpy((char*)err.text, text.c_str(), MAX_MSG_LEN);
1062 res = sendto(listenSocket, &err, sizeof(err), 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
1063 printfd(__FILE__, "SendError %d bytes sent\n", res);
1068 memset(&err8, 0, sizeof(ERR_8));
1070 sendAddr.sin_family = AF_INET;
1071 sendAddr.sin_port = htons(port);
1073 sendAddr.sin_addr.s_addr = ip;// IP ÐÏÌØÚÏ×ÁÔÅÌÑ
1076 strncpy((char*)err8.type, "ERR", 16);
1077 strncpy((char*)err8.text, text.c_str(), MAX_MSG_LEN);
1080 SwapBytes(err8.len);
1083 res = sendto(listenSocket, &err8, sizeof(err8), 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
1084 printfd(__FILE__, "SendError_8 %d bytes sent\n", res);
1090 //-----------------------------------------------------------------------------
1091 int AUTH_IA::Send(uint32_t ip, uint16_t port, const char * buffer, int len)
1093 struct sockaddr_in sendAddr;
1096 sendAddr.sin_family = AF_INET;
1097 sendAddr.sin_port = htons(port);
1098 sendAddr.sin_addr.s_addr = ip;
1100 res = sendto(listenSocket, buffer, len, 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
1102 static struct timeval tv;
1103 gettimeofday(&tv, NULL);
1107 //-----------------------------------------------------------------------------
1108 int AUTH_IA::SendMessage(const STG_MSG & msg, uint32_t ip) const
1110 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1112 printfd(__FILE__, "SendMessage userIP=%s\n", inet_ntostring(ip).c_str());
1114 map<uint32_t, IA_USER>::iterator it;
1115 it = ip2user.find(ip);
1116 if (it == ip2user.end())
1118 errorStr = "Unknown user.";
1121 it->second.messagesToSend.push_back(msg);
1124 //-----------------------------------------------------------------------------
1125 int AUTH_IA::RealSendMessage6(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1127 printfd(__FILE__, "RealSendMessage 6 user=%s\n", user.login.c_str());
1132 memset(&info, 0, sizeof(INFO_6));
1135 strncpy((char*)info.type, "INFO", 16);
1136 info.infoType = 'I';
1137 strncpy((char*)info.text, msg.text.c_str(), 235);
1140 size_t len = info.len;
1142 SwapBytes(info.len);
1145 memcpy(buffer, &info, sizeof(INFO_6));
1146 Encrypt(&user.ctx, buffer, buffer, len / 8);
1147 Send(ip, iaSettings.GetUserPort(), buffer, len);
1151 //-----------------------------------------------------------------------------
1152 int AUTH_IA::RealSendMessage7(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1154 printfd(__FILE__, "RealSendMessage 7 user=%s\n", user.login.c_str());
1159 memset(&info, 0, sizeof(INFO_7));
1162 strncpy((char*)info.type, "INFO_7", 16);
1163 info.infoType = msg.header.type;
1164 info.showTime = msg.header.showTime;
1166 info.sendTime = msg.header.creationTime;
1168 size_t len = info.len;
1170 SwapBytes(info.len);
1171 SwapBytes(info.sendTime);
1174 strncpy((char*)info.text, msg.text.c_str(), MAX_MSG_LEN - 1);
1175 info.text[MAX_MSG_LEN - 1] = 0;
1177 memcpy(buffer, &info, sizeof(INFO_7));
1179 Encrypt(&user.ctx, buffer, buffer, len / 8);
1180 Send(ip, iaSettings.GetUserPort(), buffer, len);
1184 //-----------------------------------------------------------------------------
1185 int AUTH_IA::RealSendMessage8(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1187 printfd(__FILE__, "RealSendMessage 8 user=%s\n", user.login.c_str());
1190 memset(buffer, 0, sizeof(buffer));
1194 memset(&info, 0, sizeof(INFO_8));
1197 strncpy((char*)info.type, "INFO_8", 16);
1198 info.infoType = msg.header.type;
1199 info.showTime = msg.header.showTime;
1200 info.sendTime = msg.header.creationTime;
1202 strncpy((char*)info.text, msg.text.c_str(), IA_MAX_MSG_LEN_8 - 1);
1203 info.text[IA_MAX_MSG_LEN_8 - 1] = 0;
1205 size_t len = info.len;
1207 SwapBytes(info.len);
1208 SwapBytes(info.sendTime);
1211 memcpy(buffer, &info, sizeof(INFO_8));
1213 Encrypt(&user.ctx, buffer, buffer, len / 8);
1214 Send(ip, user.port, buffer, len);
1218 //-----------------------------------------------------------------------------
1219 int AUTH_IA::Process_CONN_SYN_6(CONN_SYN_6 *, IA_USER * iaUser, uint32_t)
1221 if (!(iaUser->phase.GetPhase() == 1 || iaUser->phase.GetPhase() == 3))
1224 enabledDirs = 0xFFffFFff;
1226 iaUser->phase.SetPhase2();
1227 printfd(__FILE__, "Phase changed from %d to 2. Reason: CONN_SYN_6\n", iaUser->phase.GetPhase());
1230 //-----------------------------------------------------------------------------
1231 int AUTH_IA::Process_CONN_SYN_7(CONN_SYN_7 * connSyn, IA_USER * iaUser, uint32_t sip)
1233 return Process_CONN_SYN_6(connSyn, iaUser, sip);
1235 //-----------------------------------------------------------------------------
1236 int AUTH_IA::Process_CONN_SYN_8(CONN_SYN_8 * connSyn, IA_USER * iaUser, uint32_t sip)
1239 SwapBytes(connSyn->dirs);
1241 int ret = Process_CONN_SYN_6((CONN_SYN_6*)connSyn, iaUser, sip);
1242 enabledDirs = connSyn->dirs;
1245 //-----------------------------------------------------------------------------
1246 int AUTH_IA::Process_CONN_ACK_6(CONN_ACK_6 * connAck, IA_USER * iaUser, uint32_t sip)
1249 SwapBytes(connAck->len);
1250 SwapBytes(connAck->rnd);
1252 printfd( __FILE__, "CONN_ACK_6 %s\n", connAck->type);
1253 // ÕÓÔÁÎÏ×ÉÔØ ÎÏ×ÕÀ ÆÁÚÕ É ×ÒÅÍÑ É ÒÁÚÒÅÛÉÔØ ÉÎÅÔ
1254 if ((iaUser->phase.GetPhase() == 2) && (connAck->rnd == iaUser->rnd + 1))
1256 iaUser->phase.UpdateTime();
1258 iaUser->lastSendAlive = iaUser->phase.GetTime();
1259 if (users->Authorize(iaUser->login, sip, enabledDirs, this))
1261 iaUser->phase.SetPhase3();
1262 printfd(__FILE__, "Phase changed from 2 to 3. Reason: CONN_ACK_6\n");
1267 errorStr = iaUser->user->GetStrError();
1268 iaUser->phase.SetPhase1();
1270 printfd(__FILE__, "Phase changed from 2 to 1. Reason: failed to authorize user\n");
1274 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), connAck->rnd);
1277 //-----------------------------------------------------------------------------
1278 int AUTH_IA::Process_CONN_ACK_7(CONN_ACK_7 * connAck, IA_USER * iaUser, uint32_t sip)
1280 return Process_CONN_ACK_6(connAck, iaUser, sip);
1282 //-----------------------------------------------------------------------------
1283 int AUTH_IA::Process_CONN_ACK_8(CONN_ACK_8 * connAck, IA_USER * iaUser, uint32_t sip)
1286 SwapBytes(connAck->len);
1287 SwapBytes(connAck->rnd);
1289 printfd( __FILE__, "CONN_ACK_8 %s\n", connAck->type);
1291 if ((iaUser->phase.GetPhase() == 2) && (connAck->rnd == iaUser->rnd + 1))
1293 iaUser->phase.UpdateTime();
1294 iaUser->lastSendAlive = iaUser->phase.GetTime();
1295 if (users->Authorize(iaUser->login, sip, enabledDirs, this))
1297 iaUser->phase.SetPhase3();
1298 printfd(__FILE__, "Phase changed from 2 to 3. Reason: CONN_ACK_8\n");
1303 errorStr = iaUser->user->GetStrError();
1304 iaUser->phase.SetPhase1();
1306 printfd(__FILE__, "Phase changed from 2 to 1. Reason: failed to authorize user\n");
1310 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), connAck->rnd);
1313 //-----------------------------------------------------------------------------
1314 int AUTH_IA::Process_ALIVE_ACK_6(ALIVE_ACK_6 * aliveAck, IA_USER * iaUser, uint32_t)
1317 SwapBytes(aliveAck->len);
1318 SwapBytes(aliveAck->rnd);
1320 printfd(__FILE__, "ALIVE_ACK_6\n");
1321 if ((iaUser->phase.GetPhase() == 3) && (aliveAck->rnd == iaUser->rnd + 1))
1323 iaUser->phase.UpdateTime();
1325 iaUser->aliveSent = false;
1330 //-----------------------------------------------------------------------------
1331 int AUTH_IA::Process_ALIVE_ACK_7(ALIVE_ACK_7 * aliveAck, IA_USER * iaUser, uint32_t sip)
1333 return Process_ALIVE_ACK_6(aliveAck, iaUser, sip);
1335 //-----------------------------------------------------------------------------
1336 int AUTH_IA::Process_ALIVE_ACK_8(ALIVE_ACK_8 * aliveAck, IA_USER * iaUser, uint32_t)
1339 SwapBytes(aliveAck->len);
1340 SwapBytes(aliveAck->rnd);
1342 printfd(__FILE__, "ALIVE_ACK_8\n");
1343 if ((iaUser->phase.GetPhase() == 3) && (aliveAck->rnd == iaUser->rnd + 1))
1345 iaUser->phase.UpdateTime();
1347 iaUser->aliveSent = false;
1352 //-----------------------------------------------------------------------------
1353 int AUTH_IA::Process_DISCONN_SYN_6(DISCONN_SYN_6 *, IA_USER * iaUser, uint32_t)
1355 printfd(__FILE__, "DISCONN_SYN_6\n");
1356 if (iaUser->phase.GetPhase() != 3)
1358 printfd(__FILE__, "Invalid phase. Expected 3, actual %d\n", iaUser->phase.GetPhase());
1359 errorStr = "Incorrect request DISCONN_SYN";
1363 iaUser->phase.SetPhase4();
1364 printfd(__FILE__, "Phase changed from 3 to 4. Reason: DISCONN_SYN_6\n");
1368 //-----------------------------------------------------------------------------
1369 int AUTH_IA::Process_DISCONN_SYN_7(DISCONN_SYN_7 * disconnSyn, IA_USER * iaUser, uint32_t sip)
1371 return Process_DISCONN_SYN_6(disconnSyn, iaUser, sip);
1373 //-----------------------------------------------------------------------------
1374 int AUTH_IA::Process_DISCONN_SYN_8(DISCONN_SYN_8 *, IA_USER * iaUser, uint32_t)
1376 if (iaUser->phase.GetPhase() != 3)
1378 errorStr = "Incorrect request DISCONN_SYN";
1379 printfd(__FILE__, "Invalid phase. Expected 3, actual %d\n", iaUser->phase.GetPhase());
1383 iaUser->phase.SetPhase4();
1384 printfd(__FILE__, "Phase changed from 3 to 4. Reason: DISCONN_SYN_6\n");
1388 //-----------------------------------------------------------------------------
1389 int AUTH_IA::Process_DISCONN_ACK_6(DISCONN_ACK_6 * disconnAck,
1392 map<uint32_t, IA_USER>::iterator)
1395 SwapBytes(disconnAck->len);
1396 SwapBytes(disconnAck->rnd);
1398 printfd(__FILE__, "DISCONN_ACK_6\n");
1399 if (!((iaUser->phase.GetPhase() == 4) && (disconnAck->rnd == iaUser->rnd + 1)))
1401 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), disconnAck->rnd);
1407 //-----------------------------------------------------------------------------
1408 int AUTH_IA::Process_DISCONN_ACK_7(DISCONN_ACK_7 * disconnAck, IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1410 return Process_DISCONN_ACK_6(disconnAck, iaUser, sip, it);
1412 //-----------------------------------------------------------------------------
1413 int AUTH_IA::Process_DISCONN_ACK_8(DISCONN_ACK_8 * disconnAck, IA_USER * iaUser, uint32_t, map<uint32_t, IA_USER>::iterator)
1416 SwapBytes(disconnAck->len);
1417 SwapBytes(disconnAck->rnd);
1419 printfd(__FILE__, "DISCONN_ACK_8\n");
1420 if (!((iaUser->phase.GetPhase() == 4) && (disconnAck->rnd == iaUser->rnd + 1)))
1422 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), disconnAck->rnd);
1428 //-----------------------------------------------------------------------------
1429 int AUTH_IA::Send_CONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip)
1431 //+++ Fill static data in connSynAck +++
1432 // TODO Move this code. It must be executed only once
1433 connSynAck6.len = Min8(sizeof(CONN_SYN_ACK_6));
1434 strcpy((char*)connSynAck6.type, "CONN_SYN_ACK");
1435 for (int j = 0; j < DIR_NUM; j++)
1437 strncpy((char*)connSynAck6.dirName[j],
1438 stgSettings->GetDirName(j).c_str(),
1441 connSynAck6.dirName[j][sizeof(string16) - 1] = 0;
1443 //--- Fill static data in connSynAck ---
1445 iaUser->rnd = random();
1446 connSynAck6.rnd = iaUser->rnd;
1448 connSynAck6.userTimeOut = iaSettings.GetUserTimeout();
1449 connSynAck6.aliveDelay = iaSettings.GetUserDelay();
1452 SwapBytes(connSynAck6.len);
1453 SwapBytes(connSynAck6.rnd);
1454 SwapBytes(connSynAck6.userTimeOut);
1455 SwapBytes(connSynAck6.aliveDelay);
1458 Encrypt(&iaUser->ctx, (char*)&connSynAck6, (char*)&connSynAck6, Min8(sizeof(CONN_SYN_ACK_6))/8);
1459 return Send(sip, iaSettings.GetUserPort(), (char*)&connSynAck6, Min8(sizeof(CONN_SYN_ACK_6)));;
1461 //-----------------------------------------------------------------------------
1462 int AUTH_IA::Send_CONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip)
1464 return Send_CONN_SYN_ACK_6(iaUser, sip);
1466 //-----------------------------------------------------------------------------
1467 int AUTH_IA::Send_CONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip)
1469 strcpy((char*)connSynAck8.hdr.magic, IA_ID);
1470 connSynAck8.hdr.protoVer[0] = 0;
1471 connSynAck8.hdr.protoVer[1] = 8;
1473 //+++ Fill static data in connSynAck +++
1474 // TODO Move this code. It must be executed only once
1475 connSynAck8.len = Min8(sizeof(CONN_SYN_ACK_8));
1476 strcpy((char*)connSynAck8.type, "CONN_SYN_ACK");
1477 for (int j = 0; j < DIR_NUM; j++)
1479 strncpy((char*)connSynAck8.dirName[j],
1480 stgSettings->GetDirName(j).c_str(),
1483 connSynAck8.dirName[j][sizeof(string16) - 1] = 0;
1485 //--- Fill static data in connSynAck ---
1487 iaUser->rnd = random();
1488 connSynAck8.rnd = iaUser->rnd;
1490 connSynAck8.userTimeOut = iaSettings.GetUserTimeout();
1491 connSynAck8.aliveDelay = iaSettings.GetUserDelay();
1494 SwapBytes(connSynAck8.len);
1495 SwapBytes(connSynAck8.rnd);
1496 SwapBytes(connSynAck8.userTimeOut);
1497 SwapBytes(connSynAck8.aliveDelay);
1500 Encrypt(&iaUser->ctx, (char*)&connSynAck8, (char*)&connSynAck8, Min8(sizeof(CONN_SYN_ACK_8))/8);
1501 return Send(sip, iaUser->port, (char*)&connSynAck8, Min8(sizeof(CONN_SYN_ACK_8)));
1502 //return Send(sip, iaUser->port, (char*)&connSynAck8, 384);
1504 //-----------------------------------------------------------------------------
1505 int AUTH_IA::Send_ALIVE_SYN_6(IA_USER * iaUser, uint32_t sip)
1507 aliveSyn6.len = Min8(sizeof(ALIVE_SYN_6));
1508 aliveSyn6.rnd = iaUser->rnd = random();
1510 strcpy((char*)aliveSyn6.type, "ALIVE_SYN");
1512 for (int i = 0; i < DIR_NUM; i++)
1514 aliveSyn6.md[i] = iaUser->user->GetProperty().down.Get()[i];
1515 aliveSyn6.mu[i] = iaUser->user->GetProperty().up.Get()[i];
1517 aliveSyn6.sd[i] = iaUser->user->GetSessionDownload()[i];
1518 aliveSyn6.su[i] = iaUser->user->GetSessionUpload()[i];
1522 int dn = iaSettings.GetFreeMbShowType();
1523 const TARIFF * tf = iaUser->user->GetTariff();
1527 double p = tf->GetPriceWithTraffType(aliveSyn6.mu[dn],
1534 snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "---");
1538 double fmb = iaUser->user->GetProperty().freeMb;
1539 fmb = fmb < 0 ? 0 : fmb;
1540 snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "%.3f", fmb / p);
1545 if (freeMbNone == iaSettings.GetFreeMbShowType())
1547 aliveSyn6.freeMb[0] = 0;
1551 double fmb = iaUser->user->GetProperty().freeMb;
1552 fmb = fmb < 0 ? 0 : fmb;
1553 snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "C%.3f", fmb);
1558 if (iaUser->aliveSent)
1560 printfd(__FILE__, "========= ALIVE_ACK_6(7) TIMEOUT !!! %s =========\n", iaUser->login.c_str());
1562 iaUser->aliveSent = true;
1565 aliveSyn6.cash =(int64_t) (iaUser->user->GetProperty().cash.Get() * 1000.0);
1566 if (!stgSettings->GetShowFeeInCash())
1567 aliveSyn6.cash -= (int64_t)(tf->GetFee() * 1000.0);
1570 SwapBytes(aliveSyn6.len);
1571 SwapBytes(aliveSyn6.rnd);
1572 SwapBytes(aliveSyn6.cash);
1573 for (int i = 0; i < DIR_NUM; ++i)
1575 SwapBytes(aliveSyn6.mu[i]);
1576 SwapBytes(aliveSyn6.md[i]);
1577 SwapBytes(aliveSyn6.su[i]);
1578 SwapBytes(aliveSyn6.sd[i]);
1582 Encrypt(&(iaUser->ctx), (char*)&aliveSyn6, (char*)&aliveSyn6, Min8(sizeof(aliveSyn6))/8);
1583 return Send(sip, iaSettings.GetUserPort(), (char*)&aliveSyn6, Min8(sizeof(aliveSyn6)));
1585 //-----------------------------------------------------------------------------
1586 int AUTH_IA::Send_ALIVE_SYN_7(IA_USER * iaUser, uint32_t sip)
1588 return Send_ALIVE_SYN_6(iaUser, sip);
1590 //-----------------------------------------------------------------------------
1591 int AUTH_IA::Send_ALIVE_SYN_8(IA_USER * iaUser, uint32_t sip)
1593 strcpy((char*)aliveSyn8.hdr.magic, IA_ID);
1594 aliveSyn8.hdr.protoVer[0] = 0;
1595 aliveSyn8.hdr.protoVer[1] = 8;
1597 aliveSyn8.len = Min8(sizeof(ALIVE_SYN_8));
1598 aliveSyn8.rnd = iaUser->rnd = random();
1600 strcpy((char*)aliveSyn8.type, "ALIVE_SYN");
1602 for (int i = 0; i < DIR_NUM; i++)
1604 aliveSyn8.md[i] = iaUser->user->GetProperty().down.Get()[i];
1605 aliveSyn8.mu[i] = iaUser->user->GetProperty().up.Get()[i];
1607 aliveSyn8.sd[i] = iaUser->user->GetSessionDownload()[i];
1608 aliveSyn8.su[i] = iaUser->user->GetSessionUpload()[i];
1612 int dn = iaSettings.GetFreeMbShowType();
1616 const TARIFF * tf = iaUser->user->GetTariff();
1617 double p = tf->GetPriceWithTraffType(aliveSyn8.mu[dn],
1624 snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "---");
1628 double fmb = iaUser->user->GetProperty().freeMb;
1629 fmb = fmb < 0 ? 0 : fmb;
1630 snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "%.3f", fmb / p);
1635 if (freeMbNone == iaSettings.GetFreeMbShowType())
1637 aliveSyn8.freeMb[0] = 0;
1641 double fmb = iaUser->user->GetProperty().freeMb;
1642 fmb = fmb < 0 ? 0 : fmb;
1643 snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "C%.3f", fmb);
1648 if (iaUser->aliveSent)
1650 printfd(__FILE__, "========= ALIVE_ACK_8 TIMEOUT !!! =========\n");
1652 iaUser->aliveSent = true;
1655 const TARIFF * tf = iaUser->user->GetTariff();
1657 aliveSyn8.cash =(int64_t) (iaUser->user->GetProperty().cash.Get() * 1000.0);
1658 if (!stgSettings->GetShowFeeInCash())
1659 aliveSyn8.cash -= (int64_t)(tf->GetFee() * 1000.0);
1662 SwapBytes(aliveSyn8.len);
1663 SwapBytes(aliveSyn8.rnd);
1664 SwapBytes(aliveSyn8.cash);
1665 SwapBytes(aliveSyn8.status);
1666 for (int i = 0; i < DIR_NUM; ++i)
1668 SwapBytes(aliveSyn8.mu[i]);
1669 SwapBytes(aliveSyn8.md[i]);
1670 SwapBytes(aliveSyn8.su[i]);
1671 SwapBytes(aliveSyn8.sd[i]);
1675 Encrypt(&(iaUser->ctx), (char*)&aliveSyn8, (char*)&aliveSyn8, Min8(sizeof(aliveSyn8))/8);
1676 return Send(sip, iaUser->port, (char*)&aliveSyn8, Min8(sizeof(aliveSyn8)));
1678 //-----------------------------------------------------------------------------
1679 int AUTH_IA::Send_DISCONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip)
1681 disconnSynAck6.len = Min8(sizeof(DISCONN_SYN_ACK_6));
1682 strcpy((char*)disconnSynAck6.type, "DISCONN_SYN_ACK");
1683 disconnSynAck6.rnd = iaUser->rnd = random();
1686 SwapBytes(disconnSynAck6.len);
1687 SwapBytes(disconnSynAck6.rnd);
1690 Encrypt(&iaUser->ctx, (char*)&disconnSynAck6, (char*)&disconnSynAck6, Min8(sizeof(disconnSynAck6))/8);
1691 return Send(sip, iaSettings.GetUserPort(), (char*)&disconnSynAck6, Min8(sizeof(disconnSynAck6)));
1693 //-----------------------------------------------------------------------------
1694 int AUTH_IA::Send_DISCONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip)
1696 return Send_DISCONN_SYN_ACK_6(iaUser, sip);
1698 //-----------------------------------------------------------------------------
1699 int AUTH_IA::Send_DISCONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip)
1701 strcpy((char*)disconnSynAck8.hdr.magic, IA_ID);
1702 disconnSynAck8.hdr.protoVer[0] = 0;
1703 disconnSynAck8.hdr.protoVer[1] = 8;
1705 disconnSynAck8.len = Min8(sizeof(DISCONN_SYN_ACK_8));
1706 strcpy((char*)disconnSynAck8.type, "DISCONN_SYN_ACK");
1707 disconnSynAck8.rnd = iaUser->rnd = random();
1710 SwapBytes(disconnSynAck8.len);
1711 SwapBytes(disconnSynAck8.rnd);
1714 Encrypt(&iaUser->ctx, (char*)&disconnSynAck8, (char*)&disconnSynAck8, Min8(sizeof(disconnSynAck8))/8);
1715 return Send(sip, iaUser->port, (char*)&disconnSynAck8, Min8(sizeof(disconnSynAck8)));
1717 //-----------------------------------------------------------------------------
1718 int AUTH_IA::Send_FIN_6(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1720 fin6.len = Min8(sizeof(FIN_6));
1721 strcpy((char*)fin6.type, "FIN");
1722 strcpy((char*)fin6.ok, "OK");
1725 SwapBytes(fin6.len);
1728 Encrypt(&iaUser->ctx, (char*)&fin6, (char*)&fin6, Min8(sizeof(fin6))/8);
1730 users->Unauthorize(iaUser->login, this);
1732 int ret = Send(sip, iaSettings.GetUserPort(), (char*)&fin6, Min8(sizeof(fin6)));
1736 //-----------------------------------------------------------------------------
1737 int AUTH_IA::Send_FIN_7(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1739 return Send_FIN_6(iaUser, sip, it);
1741 //-----------------------------------------------------------------------------
1742 int AUTH_IA::Send_FIN_8(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1744 strcpy((char*)fin8.hdr.magic, IA_ID);
1745 fin8.hdr.protoVer[0] = 0;
1746 fin8.hdr.protoVer[1] = 8;
1748 fin8.len = Min8(sizeof(FIN_8));
1749 strcpy((char*)fin8.type, "FIN");
1750 strcpy((char*)fin8.ok, "OK");
1753 SwapBytes(fin8.len);
1756 Encrypt(&iaUser->ctx, (char*)&fin8, (char*)&fin8, Min8(sizeof(fin8))/8);
1758 users->Unauthorize(iaUser->login, this);
1760 int ret = Send(sip, iaUser->port, (char*)&fin8, Min8(sizeof(fin8)));
1764 //-----------------------------------------------------------------------------
1766 void InitEncrypt(BLOWFISH_CTX * ctx, const string & password)
1768 unsigned char keyL[PASSWD_LEN]; // ðÁÒÏÌØ ÄÌÑ ÛÉÆÒÏ×ËÉ
1769 memset(keyL, 0, PASSWD_LEN);
1770 strncpy((char *)keyL, password.c_str(), PASSWD_LEN);
1771 Blowfish_Init(ctx, keyL, PASSWD_LEN);
1773 //-----------------------------------------------------------------------------
1775 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
1777 // len8 - ÄÌÉÎÁ × 8-ÍÉ ÂÁÊÔÏ×ÙÈ ÂÌÏËÁÈ
1779 for (int i = 0; i < len8; i++)
1780 DecodeString(dst + i * 8, src + i * 8, ctx);
1782 //-----------------------------------------------------------------------------
1784 void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
1786 // len8 - ÄÌÉÎÁ × 8-ÍÉ ÂÁÊÔÏ×ÙÈ ÂÌÏËÁÈ
1788 for (int i = 0; i < len8; i++)
1789 EncodeString(dst + i * 8, src + i * 8, ctx);