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 <sys/select.h>
34 #include <unistd.h> // usleep, close
38 #include <cstdio> // snprintf
42 #include "stg/common.h"
43 #include "stg/locker.h"
44 #include "stg/tariff.h"
45 #include "stg/user_property.h"
46 #include "stg/settings.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 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
78 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
85 return iac.GetPlugin();
87 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
90 AUTH_IA_SETTINGS::AUTH_IA_SETTINGS()
94 freeMbShowType(freeMbCash)
97 //-----------------------------------------------------------------------------
98 int AUTH_IA_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
102 vector<PARAM_VALUE>::const_iterator pvi;
103 ///////////////////////////
105 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
106 if (pvi == s.moduleParams.end())
108 errorStr = "Parameter \'Port\' not found.";
109 printfd(__FILE__, "Parameter 'Port' not found\n");
112 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
114 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
115 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
119 ///////////////////////////
120 pv.param = "UserDelay";
121 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
122 if (pvi == s.moduleParams.end())
124 errorStr = "Parameter \'UserDelay\' not found.";
125 printfd(__FILE__, "Parameter 'UserDelay' not found\n");
129 if (ParseIntInRange(pvi->value[0], 5, 600, &userDelay))
131 errorStr = "Cannot parse parameter \'UserDelay\': " + errorStr;
132 printfd(__FILE__, "Cannot parse parameter 'UserDelay'\n");
135 ///////////////////////////
136 pv.param = "UserTimeout";
137 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
138 if (pvi == s.moduleParams.end())
140 errorStr = "Parameter \'UserTimeout\' not found.";
141 printfd(__FILE__, "Parameter 'UserTimeout' not found\n");
145 if (ParseIntInRange(pvi->value[0], 15, 1200, &userTimeout))
147 errorStr = "Cannot parse parameter \'UserTimeout\': " + errorStr;
148 printfd(__FILE__, "Cannot parse parameter 'UserTimeout'\n");
151 /////////////////////////////////////////////////////////////
155 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
156 if (pvi == s.moduleParams.end())
158 errorStr = "Parameter \'FreeMb\' not found.";
159 printfd(__FILE__, "Parameter 'FreeMb' not found\n");
162 freeMbType = pvi->value[0];
164 if (strcasecmp(freeMbType.c_str(), "cash") == 0)
166 freeMbShowType = freeMbCash;
168 else if (strcasecmp(freeMbType.c_str(), "none") == 0)
170 freeMbShowType = freeMbNone;
172 else if (!str2x(freeMbType.c_str(), n))
174 if (n < 0 || n >= DIR_NUM)
176 errorStr = "Incorrect parameter \'" + freeMbType + "\'.";
177 printfd(__FILE__, "%s\n", errorStr.c_str());
180 freeMbShowType = (FREEMB)(freeMb0 + n);
184 errorStr = "Incorrect parameter \'" + freeMbType + "\'.";
185 printfd(__FILE__, "%s\n", errorStr.c_str());
188 /////////////////////////////////////////////////////////////
191 //-----------------------------------------------------------------------------
192 //-----------------------------------------------------------------------------
193 //-----------------------------------------------------------------------------
194 #ifdef IA_PHASE_DEBUG
199 gettimeofday(&phaseTime, NULL);
205 gettimeofday(&phaseTime, NULL);
208 //-----------------------------------------------------------------------------
209 IA_PHASE::~IA_PHASE()
211 #ifdef IA_PHASE_DEBUG
212 flog = fopen(log.c_str(), "at");
215 fprintf(flog, "IA %s D\n", login.c_str());
220 //-----------------------------------------------------------------------------
221 #ifdef IA_PHASE_DEBUG
222 void IA_PHASE::SetLogFileName(const string & logFileName)
224 log = logFileName + ".ia.log";
226 //-----------------------------------------------------------------------------
227 void IA_PHASE::SetUserLogin(const string & login)
229 IA_PHASE::login = login;
231 //-----------------------------------------------------------------------------
232 void IA_PHASE::WritePhaseChange(int newPhase)
235 gettimeofday(&newPhaseTime, NULL);
236 flog = fopen(log.c_str(), "at");
237 /*int64_t tn = newPhaseTime.GetSec()*1000000 + newPhaseTime.GetUSec();
238 int64_t to = phaseTime.GetSec()*1000000 + phaseTime.GetUSec();*/
241 string action = newPhase == phase ? "U" : "C";
242 double delta = newPhaseTime.GetSec() - phaseTime.GetSec();
243 delta += (newPhaseTime.GetUSec() - phaseTime.GetUSec()) * 1.0e-6;
244 fprintf(flog, "IA %s %s oldPhase = %d, newPhase = %d. dt = %.6f\n",
254 //-----------------------------------------------------------------------------
255 void IA_PHASE::SetPhase1()
257 #ifdef IA_PHASE_DEBUG
261 gettimeofday(&phaseTime, NULL);
263 //-----------------------------------------------------------------------------
264 void IA_PHASE::SetPhase2()
266 #ifdef IA_PHASE_DEBUG
270 gettimeofday(&phaseTime, NULL);
272 //-----------------------------------------------------------------------------
273 void IA_PHASE::SetPhase3()
275 #ifdef IA_PHASE_DEBUG
279 gettimeofday(&phaseTime, NULL);
281 //-----------------------------------------------------------------------------
282 void IA_PHASE::SetPhase4()
284 #ifdef IA_PHASE_DEBUG
288 gettimeofday(&phaseTime, NULL);
290 //-----------------------------------------------------------------------------
291 void IA_PHASE::SetPhase5()
293 #ifdef IA_PHASE_DEBUG
297 gettimeofday(&phaseTime, NULL);
299 //-----------------------------------------------------------------------------
300 int IA_PHASE::GetPhase() const
304 //-----------------------------------------------------------------------------
305 void IA_PHASE::UpdateTime()
307 #ifdef IA_PHASE_DEBUG
308 WritePhaseChange(phase);
310 gettimeofday(&phaseTime, NULL);
312 //-----------------------------------------------------------------------------
313 const UTIME & IA_PHASE::GetTime() const
317 //-----------------------------------------------------------------------------
318 //-----------------------------------------------------------------------------
319 //-----------------------------------------------------------------------------
323 isRunningRunTimeouter(false),
327 WriteServLog(GetStgLogger()),
328 enabledDirs(0xFFffFFff),
329 onDelUserNotifier(*this)
331 InitEncrypt(&ctxS, "pr7Hhen");
333 pthread_mutexattr_t attr;
334 pthread_mutexattr_init(&attr);
335 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
336 pthread_mutex_init(&mutex, &attr);
338 memset(&connSynAck6, 0, sizeof(CONN_SYN_ACK_6));
339 memset(&connSynAck8, 0, sizeof(CONN_SYN_ACK_8));
340 memset(&disconnSynAck6, 0, sizeof(DISCONN_SYN_ACK_6));
341 memset(&disconnSynAck8, 0, sizeof(DISCONN_SYN_ACK_8));
342 memset(&aliveSyn6, 0, sizeof(ALIVE_SYN_6));
343 memset(&aliveSyn8, 0, sizeof(ALIVE_SYN_8));
344 memset(&fin6, 0, sizeof(FIN_6));
345 memset(&fin8, 0, sizeof(FIN_8));
347 printfd(__FILE__, "sizeof(CONN_SYN_6) = %d %d\n", sizeof(CONN_SYN_6), Min8(sizeof(CONN_SYN_6)));
348 printfd(__FILE__, "sizeof(CONN_SYN_8) = %d %d\n", sizeof(CONN_SYN_8), Min8(sizeof(CONN_SYN_8)));
349 printfd(__FILE__, "sizeof(CONN_SYN_ACK_6) = %d %d\n", sizeof(CONN_SYN_ACK_6), Min8(sizeof(CONN_SYN_ACK_6)));
350 printfd(__FILE__, "sizeof(CONN_SYN_ACK_8) = %d %d\n", sizeof(CONN_SYN_ACK_8), Min8(sizeof(CONN_SYN_ACK_8)));
351 printfd(__FILE__, "sizeof(CONN_ACK_6) = %d %d\n", sizeof(CONN_ACK_6), Min8(sizeof(CONN_ACK_6)));
352 printfd(__FILE__, "sizeof(ALIVE_SYN_6) = %d %d\n", sizeof(ALIVE_SYN_6), Min8(sizeof(ALIVE_SYN_6)));
353 printfd(__FILE__, "sizeof(ALIVE_SYN_8) = %d %d\n", sizeof(ALIVE_SYN_8), Min8(sizeof(ALIVE_SYN_8)));
354 printfd(__FILE__, "sizeof(ALIVE_ACK_6) = %d %d\n", sizeof(ALIVE_ACK_6), Min8(sizeof(ALIVE_ACK_6)));
355 printfd(__FILE__, "sizeof(DISCONN_SYN_6) = %d %d\n", sizeof(DISCONN_SYN_6), Min8(sizeof(DISCONN_SYN_6)));
356 printfd(__FILE__, "sizeof(DISCONN_SYN_ACK_6) = %d %d\n", sizeof(DISCONN_SYN_ACK_6), Min8(sizeof(DISCONN_SYN_ACK_6)));
357 printfd(__FILE__, "sizeof(DISCONN_SYN_ACK_8) = %d %d\n", sizeof(DISCONN_SYN_ACK_8), Min8(sizeof(DISCONN_SYN_ACK_8)));
358 printfd(__FILE__, "sizeof(DISCONN_ACK_6) = %d %d\n", sizeof(DISCONN_ACK_6), Min8(sizeof(DISCONN_ACK_6)));
359 printfd(__FILE__, "sizeof(FIN_6) = %d %d\n", sizeof(FIN_6), Min8(sizeof(FIN_6)));
360 printfd(__FILE__, "sizeof(FIN_8) = %d %d\n", sizeof(FIN_8), Min8(sizeof(FIN_8)));
361 printfd(__FILE__, "sizeof(ERR) = %d %d\n", sizeof(ERR), Min8(sizeof(ERR)));
362 printfd(__FILE__, "sizeof(INFO_6) = %d %d\n", sizeof(INFO_6), Min8(sizeof(INFO_6)));
363 printfd(__FILE__, "sizeof(INFO_7) = %d %d\n", sizeof(INFO_7), Min8(sizeof(INFO_7)));
364 printfd(__FILE__, "sizeof(INFO_8) = %d %d\n", sizeof(INFO_8), Min8(sizeof(INFO_8)));
366 packetTypes["CONN_SYN"] = CONN_SYN_N;
367 packetTypes["CONN_SYN_ACK"] = CONN_SYN_ACK_N;
368 packetTypes["CONN_ACK"] = CONN_ACK_N;
369 packetTypes["ALIVE_SYN"] = ALIVE_SYN_N;
370 packetTypes["ALIVE_ACK"] = ALIVE_ACK_N;
371 packetTypes["DISCONN_SYN"] = DISCONN_SYN_N;
372 packetTypes["DISCONN_SYN_ACK"] = DISCONN_SYN_ACK_N;
373 packetTypes["DISCONN_ACK"] = DISCONN_ACK_N;
374 packetTypes["FIN"] = FIN_N;
375 packetTypes["ERR"] = ERROR_N;
377 //-----------------------------------------------------------------------------
380 pthread_mutex_destroy(&mutex);
382 //-----------------------------------------------------------------------------
385 users->AddNotifierUserDel(&onDelUserNotifier);
395 if (pthread_create(&recvThread, NULL, Run, this))
397 errorStr = "Cannot create thread.";
398 printfd(__FILE__, "Cannot create recv thread\n");
403 if (!isRunningRunTimeouter)
405 if (pthread_create(&timeouterThread, NULL, RunTimeouter, this))
407 errorStr = "Cannot create thread.";
408 printfd(__FILE__, "Cannot create timeouter thread\n");
415 //-----------------------------------------------------------------------------
426 UnauthorizeUser(this)
431 //5 seconds to thread stops itself
432 for (int i = 0; i < 25 && isRunningRun; i++)
437 //after 5 seconds waiting thread still running. now killing it
440 //TODO pthread_cancel()
441 if (pthread_kill(recvThread, SIGINT))
443 errorStr = "Cannot kill thread.";
444 printfd(__FILE__, "Cannot kill thread\n");
447 for (int i = 0; i < 25 && isRunningRun; ++i)
451 printfd(__FILE__, "Failed to stop recv thread\n");
455 pthread_join(recvThread, NULL);
457 printfd(__FILE__, "AUTH_IA killed Run\n");
463 if (isRunningRunTimeouter)
465 //5 seconds to thread stops itself
466 for (int i = 0; i < 25 && isRunningRunTimeouter; i++)
471 //after 5 seconds waiting thread still running. now killing it
472 if (isRunningRunTimeouter)
474 //TODO pthread_cancel()
475 if (pthread_kill(timeouterThread, SIGINT))
477 errorStr = "Cannot kill thread.";
480 for (int i = 0; i < 25 && isRunningRunTimeouter; ++i)
482 if (isRunningRunTimeouter)
484 printfd(__FILE__, "Failed to stop timeouter thread\n");
488 pthread_join(timeouterThread, NULL);
490 printfd(__FILE__, "AUTH_IA killed Timeouter\n");
493 printfd(__FILE__, "AUTH_IA::Stoped successfully.\n");
494 users->DelNotifierUserDel(&onDelUserNotifier);
497 //-----------------------------------------------------------------------------
498 void * AUTH_IA::Run(void * d)
500 AUTH_IA * ia = static_cast<AUTH_IA *>(d);
502 ia->isRunningRun = true;
506 time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
510 ia->RecvData(buffer, sizeof(buffer));
511 if ((touchTime + MONITOR_TIME_DELAY_SEC <= stgTime) && ia->stgSettings->GetMonitoring())
514 string monFile = ia->stgSettings->GetMonitorDir() + "/inetaccess_r";
515 TouchFile(monFile.c_str());
519 ia->isRunningRun = false;
522 //-----------------------------------------------------------------------------
523 void * AUTH_IA::RunTimeouter(void * d)
525 AUTH_IA * ia = static_cast<AUTH_IA *>(d);
527 ia->isRunningRunTimeouter = true;
530 string monFile = ia->stgSettings->GetMonitorDir() + "/inetaccess_t";
535 // TODO cahange counter to timer and MONITOR_TIME_DELAY_SEC
536 if (++a % (50*60) == 0 && ia->stgSettings->GetMonitoring())
538 TouchFile(monFile.c_str());
542 ia->isRunningRunTimeouter = false;
545 //-----------------------------------------------------------------------------
546 int AUTH_IA::ParseSettings()
548 int ret = iaSettings.ParseSettings(settings);
550 errorStr = iaSettings.GetStrError();
553 //-----------------------------------------------------------------------------
554 int AUTH_IA::PrepareNet()
556 struct sockaddr_in listenAddr;
558 listenSocket = socket(AF_INET, SOCK_DGRAM, 0);
560 if (listenSocket < 0)
562 errorStr = "Cannot create socket.";
566 listenAddr.sin_family = AF_INET;
567 listenAddr.sin_port = htons(iaSettings.GetUserPort());
568 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
570 if (bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr)) < 0)
572 errorStr = "AUTH_IA: Bind failed.";
577 if (getsockopt(listenSocket, SOL_SOCKET, SO_SNDBUF, &buffLen, sizeof(buffLen)) < 0)
580 errorStr = "Getsockopt failed. " + string(strerror(errno));
584 //WriteServLog("buffLen = %d", buffLen);
588 //-----------------------------------------------------------------------------
589 int AUTH_IA::FinalizeNet()
594 //-----------------------------------------------------------------------------
595 int AUTH_IA::RecvData(char * buffer, int bufferSize)
597 if (!WaitPackets(listenSocket)) // Timeout
602 struct sockaddr_in outerAddr;
603 socklen_t outerAddrLen(sizeof(outerAddr));
604 int dataLen = recvfrom(listenSocket, buffer, bufferSize, 0, (struct sockaddr *)&outerAddr, &outerAddrLen);
611 if (dataLen <= 0) // Error
615 printfd(__FILE__, "recvfrom res=%d, error: '%s'\n", dataLen, strerror(errno));
625 if (CheckHeader(buffer, &protoVer))
628 char login[PASSWD_LEN]; //TODO why PASSWD_LEN ?
629 memset(login, 0, PASSWD_LEN);
631 Decrypt(&ctxS, login, buffer + 8, PASSWD_LEN / 8);
633 uint32_t sip = *((uint32_t*)&outerAddr.sin_addr);
634 uint16_t sport = htons(outerAddr.sin_port);
637 if (users->FindByName(login, &user) == 0)
639 printfd(__FILE__, "User %s FOUND!\n", user->GetLogin().c_str());
640 PacketProcessor(buffer, dataLen, sip, sport, protoVer, &user);
644 WriteServLog("User\'s connect failed:: user \'%s\' not found. IP \'%s\'",
646 inet_ntostring(sip).c_str());
647 printfd(__FILE__, "User %s NOT found!\n", login);
648 SendError(sip, sport, protoVer, "îÅÐÒÁ×ÉÌØÎÙÊ ÌÏÇÉÎ ÉÌÉ ÐÁÒÏÌØ!");
654 //-----------------------------------------------------------------------------
655 int AUTH_IA::CheckHeader(const char * buffer, int * protoVer)
657 if (strncmp(IA_ID, buffer, strlen(IA_ID)) != 0)
659 //SendError(userIP, updateMsg);
660 printfd(__FILE__, "update needed - IA_ID\n");
661 //SendError(userIP, "Incorrect header!");
665 if (buffer[6] != 0) //proto[0] shoud be 0
667 printfd(__FILE__, "update needed - PROTO major: %d\n", buffer[6]);
668 //SendError(userIP, updateMsg);
675 //SendError(userIP, updateMsg);
676 printfd(__FILE__, "update needed - PROTO minor: %d\n", buffer[7]);
681 *protoVer = buffer[7];
685 //-----------------------------------------------------------------------------
686 int AUTH_IA::Timeouter()
688 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
690 map<uint32_t, IA_USER>::iterator it;
691 it = ip2user.begin();
694 while (it != ip2user.end())
698 static UTIME currTime;
699 gettimeofday(&currTime, NULL);
701 if ((it->second.phase.GetPhase() == 2)
702 && (currTime - it->second.phase.GetTime()) > iaSettings.GetUserDelay())
704 it->second.phase.SetPhase1();
705 printfd(__FILE__, "Phase changed from 2 to 1. Reason: timeout\n");
708 if (it->second.phase.GetPhase() == 3)
710 if (!it->second.messagesToSend.empty())
712 if (it->second.protoVer == 6)
713 RealSendMessage6(*it->second.messagesToSend.begin(), sip, it->second);
715 if (it->second.protoVer == 7)
716 RealSendMessage7(*it->second.messagesToSend.begin(), sip, it->second);
718 if (it->second.protoVer == 8)
719 RealSendMessage8(*it->second.messagesToSend.begin(), sip, it->second);
721 it->second.messagesToSend.erase(it->second.messagesToSend.begin());
724 if((currTime - it->second.lastSendAlive) > iaSettings.GetUserDelay())
726 switch (it->second.protoVer)
729 Send_ALIVE_SYN_6(&(it->second), sip);
732 Send_ALIVE_SYN_7(&(it->second), sip);
735 Send_ALIVE_SYN_8(&(it->second), sip);
739 gettimeofday(&it->second.lastSendAlive, NULL);
742 if ((currTime - it->second.phase.GetTime()) > iaSettings.GetUserTimeout())
744 it->second.user->Unauthorize(this);
750 if ((it->second.phase.GetPhase() == 4)
751 && ((currTime - it->second.phase.GetTime()) > iaSettings.GetUserDelay()))
753 it->second.phase.SetPhase3();
754 printfd(__FILE__, "Phase changed from 4 to 3. Reason: timeout\n");
762 //-----------------------------------------------------------------------------
763 int AUTH_IA::PacketProcessor(char * buff, int dataLen, uint32_t sip, uint16_t sport, int protoVer, USER_PTR * user)
765 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
766 // ôÕÔ ÓÏÂÒÁÎÙ ÏÂÒÁÂÏÔÞÉËÉ ÒÁÚÎÙÈ ÐÁËÅÔÏ×
768 const int offset = LOGIN_LEN + 2 + 6; // LOGIN_LEN + sizeOfMagic + sizeOfVer;
770 IA_USER * iaUser = NULL;
772 CONN_SYN_6 * connSyn6;
773 CONN_SYN_7 * connSyn7;
774 CONN_SYN_8 * connSyn8;
776 CONN_ACK_6 * connAck;
777 ALIVE_ACK_6 * aliveAck;
778 DISCONN_SYN_6 * disconnSyn;
779 DISCONN_ACK_6 * disconnAck;
781 map<uint32_t, IA_USER>::iterator it;
782 it = ip2user.find(sip);
784 if (it == ip2user.end() || (*user)->GetID() != it->second.user->GetID())
786 // åÝÅ ÎÅ ÂÙÌÏ ÚÁÐÒÏÓÏ× Ó ÜÔÏÇÏ IP
787 printfd(__FILE__, "Add new user\n");
788 ip2user[sip].protoVer = protoVer;
789 ip2user[sip].user = *user;
790 ip2user[sip].port = sport;
791 #ifdef IA_PHASE_DEBUG
792 ip2user[sip].phase.SetLogFileName(stgSettings->GetLogFileName());
793 ip2user[sip].phase.SetUserLogin((*user)->GetLogin());
797 it = ip2user.find(sip); //TODO
798 if (it == ip2user.end())
800 printfd(__FILE__, "+++ ERROR +++\n");
805 iaUser = &(it->second);
807 if (iaUser->port != sport)
808 iaUser->port = sport;
810 if (iaUser->password != (*user)->GetProperty().password.Get())
812 InitEncrypt(&iaUser->ctx, (*user)->GetProperty().password.Get());
813 iaUser->password = (*user)->GetProperty().password.Get();
817 Decrypt(&iaUser->ctx, buff, buff, (dataLen - offset) / 8);
819 char packetName[IA_MAX_TYPE_LEN];
820 strncpy(packetName, buff + 4, IA_MAX_TYPE_LEN);
821 packetName[IA_MAX_TYPE_LEN - 1] = 0;
823 map<string, int>::iterator pi;
824 pi = packetTypes.find(packetName);
825 if (pi == packetTypes.end())
827 SendError(sip, sport, protoVer, "îÅÐÒÁ×ÉÌØÎÙÊ ÌÏÇÉÎ ÉÌÉ ÐÁÒÏÌØ!");
828 printfd(__FILE__, "Login or password is wrong!\n");
829 WriteServLog("User's connect failed. IP \'%s\'. Wrong login or password", inet_ntostring(sip).c_str());
837 if ((*user)->GetProperty().disabled.Get())
839 SendError(sip, sport, protoVer, "õÞÅÔÎÁÑ ÚÁÐÉÓØ ÚÁÂÌÏËÉÒÏ×ÁÎÁ");
843 if ((*user)->GetProperty().passive.Get())
845 SendError(sip, sport, protoVer, "õÞÅÔÎÁÑ ÚÁÐÉÓØ ÚÁÍÏÒÏÖÅÎÁ");
849 if ((*user)->GetAuthorized() && (*user)->GetCurrIP() != sip)
851 printfd(__FILE__, "Login %s already in use. IP \'%s\'\n", (*user)->GetLogin().c_str(), inet_ntostring(sip).c_str());
852 WriteServLog("Login %s already in use. IP \'%s\'", (*user)->GetLogin().c_str(), inet_ntostring(sip).c_str());
853 SendError(sip, sport, protoVer, "÷ÁÛ ÌÏÇÉÎ ÕÖÅ ÉÓÐÏÌØÚÕÅÔÓÑ!");
858 if (users->FindByIPIdx(sip, &u) == 0 && u->GetLogin() != (*user)->GetLogin())
860 printfd(__FILE__, "IP address already in use. IP \'%s\'", inet_ntostring(sip).c_str());
861 WriteServLog("IP address already in use. IP \'%s\'", inet_ntostring(sip).c_str());
862 SendError(sip, sport, protoVer, "÷ÁÛ IP ÁÄÒÅÓ ÕÖÅ ÉÓÐÏÌØÚÕÅÔÓÑ!");
866 // ôÅÐÅÒØ ÍÙ ÄÏÌÖÎÙ ÐÒÏ×ÅÒÉÔØ, ÍÏÖÅÔ ÌÉ ÐÏÌØÚÏ×ÁÔÅÌØ ÐÏÄËÌÀÞÉÔÓÑ Ó ÜÔÏÇÏ ÁÄÒÅÓÁ.
867 int ipFound = (*user)->GetProperty().ips.Get().IsIPInIPS(sip);
870 printfd(__FILE__, "User %s. IP address is incorrect. IP \'%s\'\n", (*user)->GetLogin().c_str(), inet_ntostring(sip).c_str());
871 WriteServLog("User %s. IP address is incorrect. IP \'%s\'", (*user)->GetLogin().c_str(), inet_ntostring(sip).c_str());
872 SendError(sip, sport, protoVer, "ðÏÌØÚÏ×ÁÔÅÌØ ÎÅ ÏÐÏÚÎÁÎ! ðÒÏ×ÅÒØÔÅ IP ÁÄÒÅÓ.");
884 connSyn6 = (CONN_SYN_6*)(buff - offset);
885 ret = Process_CONN_SYN_6(connSyn6, &(it->second), sip);
888 connSyn7 = (CONN_SYN_7*)(buff - offset);
889 ret = Process_CONN_SYN_7(connSyn7, &(it->second), sip);
892 connSyn8 = (CONN_SYN_8*)(buff - offset);
893 ret = Process_CONN_SYN_8(connSyn8, &(it->second), sip);
904 Send_CONN_SYN_ACK_6(iaUser, sip);
907 Send_CONN_SYN_ACK_7(iaUser, sip);
910 Send_CONN_SYN_ACK_8(iaUser, sip);
916 connAck = (CONN_ACK_6*)(buff - offset);
920 ret = Process_CONN_ACK_6(connAck, iaUser, sip);
923 ret = Process_CONN_ACK_7(connAck, iaUser, sip);
926 ret = Process_CONN_ACK_8((CONN_ACK_8*)(buff - offset), iaUser, sip);
932 SendError(sip, sport, protoVer, errorStr);
939 Send_ALIVE_SYN_6(iaUser, sip);
942 Send_ALIVE_SYN_7(iaUser, sip);
945 Send_ALIVE_SYN_8(iaUser, sip);
951 // ðÒÉÂÙÌ ÏÔ×ÅÔ Ó ÐÏÄÔ×ÅÒÖÄÅÎÉÅÍ ALIVE
953 aliveAck = (ALIVE_ACK_6*)(buff - offset);
957 ret = Process_ALIVE_ACK_6(aliveAck, iaUser, sip);
960 ret = Process_ALIVE_ACK_7(aliveAck, iaUser, sip);
963 ret = Process_ALIVE_ACK_8((ALIVE_ACK_8*)(buff - offset), iaUser, sip);
968 // úÁÐÒÏÓ ÎÁ ÏÔËÌÀÞÅÎÉÅ
971 disconnSyn = (DISCONN_SYN_6*)(buff - offset);
975 ret = Process_DISCONN_SYN_6(disconnSyn, iaUser, sip);
978 ret = Process_DISCONN_SYN_7(disconnSyn, iaUser, sip);
981 ret = Process_DISCONN_SYN_8((DISCONN_SYN_8*)(buff - offset), iaUser, sip);
991 Send_DISCONN_SYN_ACK_6(iaUser, sip);
994 Send_DISCONN_SYN_ACK_7(iaUser, sip);
997 Send_DISCONN_SYN_ACK_8(iaUser, sip);
1003 disconnAck = (DISCONN_ACK_6*)(buff - offset);
1008 ret = Process_DISCONN_ACK_6(disconnAck, iaUser, sip, it);
1011 ret = Process_DISCONN_ACK_7(disconnAck, iaUser, sip, it);
1014 ret = Process_DISCONN_ACK_8((DISCONN_ACK_8*)(buff - offset), iaUser, sip, it);
1021 Send_FIN_6(iaUser, sip, it);
1024 Send_FIN_7(iaUser, sip, it);
1027 Send_FIN_8(iaUser, sip, it);
1035 //-----------------------------------------------------------------------------
1036 void AUTH_IA::DelUser(USER_PTR u)
1038 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1040 uint32_t ip = u->GetCurrIP();
1045 map<uint32_t, IA_USER>::iterator it;
1046 it = ip2user.find(ip);
1047 if (it == ip2user.end())
1050 printfd(__FILE__, "Nothing to delete\n");
1054 if (it->second.user == u)
1056 printfd(__FILE__, "User removed!\n");
1057 it->second.user->Unauthorize(this);
1061 //-----------------------------------------------------------------------------
1062 int AUTH_IA::SendError(uint32_t ip, uint16_t port, int protoVer, const string & text)
1064 struct sockaddr_in sendAddr;
1071 memset(&err, 0, sizeof(ERR));
1073 sendAddr.sin_family = AF_INET;
1074 sendAddr.sin_port = htons(port);
1076 sendAddr.sin_addr.s_addr = ip;// IP ÐÏÌØÚÏ×ÁÔÅÌÑ
1079 strncpy((char*)err.type, "ERR", 16);
1080 strncpy((char*)err.text, text.c_str(), MAX_MSG_LEN);
1086 res = sendto(listenSocket, &err, sizeof(err), 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
1087 printfd(__FILE__, "SendError %d bytes sent\n", res);
1092 memset(&err8, 0, sizeof(ERR_8));
1094 sendAddr.sin_family = AF_INET;
1095 sendAddr.sin_port = htons(port);
1097 sendAddr.sin_addr.s_addr = ip;// IP ÐÏÌØÚÏ×ÁÔÅÌÑ
1100 strncpy((char*)err8.type, "ERR", 16);
1101 strncpy((char*)err8.text, text.c_str(), MAX_MSG_LEN);
1104 SwapBytes(err8.len);
1107 res = sendto(listenSocket, &err8, sizeof(err8), 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
1108 printfd(__FILE__, "SendError_8 %d bytes sent\n", res);
1114 //-----------------------------------------------------------------------------
1115 int AUTH_IA::Send(uint32_t ip, uint16_t port, const char * buffer, int len)
1117 struct sockaddr_in sendAddr;
1120 sendAddr.sin_family = AF_INET;
1121 sendAddr.sin_port = htons(port);
1122 sendAddr.sin_addr.s_addr = ip;
1124 res = sendto(listenSocket, buffer, len, 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
1126 static struct timeval tv;
1127 gettimeofday(&tv, NULL);
1131 //-----------------------------------------------------------------------------
1132 int AUTH_IA::SendMessage(const STG_MSG & msg, uint32_t ip) const
1134 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1136 printfd(__FILE__, "SendMessage userIP=%s\n", inet_ntostring(ip).c_str());
1138 map<uint32_t, IA_USER>::iterator it;
1139 it = ip2user.find(ip);
1140 if (it == ip2user.end())
1142 errorStr = "Unknown user.";
1145 it->second.messagesToSend.push_back(msg);
1148 //-----------------------------------------------------------------------------
1149 int AUTH_IA::RealSendMessage6(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1151 printfd(__FILE__, "RealSendMessage 6 user=%s\n", user.user->GetLogin().c_str());
1156 memset(&info, 0, sizeof(INFO_6));
1159 strncpy((char*)info.type, "INFO", 16);
1160 info.infoType = 'I';
1161 strncpy((char*)info.text, msg.text.c_str(), 235);
1164 size_t len = info.len;
1166 SwapBytes(info.len);
1169 memcpy(buffer, &info, sizeof(INFO_6));
1170 Encrypt(&user.ctx, buffer, buffer, len / 8);
1171 Send(ip, iaSettings.GetUserPort(), buffer, len);
1175 //-----------------------------------------------------------------------------
1176 int AUTH_IA::RealSendMessage7(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1178 printfd(__FILE__, "RealSendMessage 7 user=%s\n", user.user->GetLogin().c_str());
1183 memset(&info, 0, sizeof(INFO_7));
1186 strncpy((char*)info.type, "INFO_7", 16);
1187 info.infoType = msg.header.type;
1188 info.showTime = msg.header.showTime;
1190 info.sendTime = msg.header.creationTime;
1192 size_t len = info.len;
1194 SwapBytes(info.len);
1195 SwapBytes(info.sendTime);
1198 strncpy((char*)info.text, msg.text.c_str(), MAX_MSG_LEN - 1);
1199 info.text[MAX_MSG_LEN - 1] = 0;
1201 memcpy(buffer, &info, sizeof(INFO_7));
1203 Encrypt(&user.ctx, buffer, buffer, len / 8);
1204 Send(ip, iaSettings.GetUserPort(), buffer, len);
1208 //-----------------------------------------------------------------------------
1209 int AUTH_IA::RealSendMessage8(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1211 printfd(__FILE__, "RealSendMessage 8 user=%s\n", user.user->GetLogin().c_str());
1214 memset(buffer, 0, sizeof(buffer));
1218 memset(&info, 0, sizeof(INFO_8));
1221 strncpy((char*)info.type, "INFO_8", 16);
1222 info.infoType = msg.header.type;
1223 info.showTime = msg.header.showTime;
1224 info.sendTime = msg.header.creationTime;
1226 strncpy((char*)info.text, msg.text.c_str(), IA_MAX_MSG_LEN_8 - 1);
1227 info.text[IA_MAX_MSG_LEN_8 - 1] = 0;
1229 size_t len = info.len;
1231 SwapBytes(info.len);
1232 SwapBytes(info.sendTime);
1235 memcpy(buffer, &info, sizeof(INFO_8));
1237 Encrypt(&user.ctx, buffer, buffer, len / 8);
1238 Send(ip, user.port, buffer, len);
1242 //-----------------------------------------------------------------------------
1243 int AUTH_IA::Process_CONN_SYN_6(CONN_SYN_6 *, IA_USER * iaUser, uint32_t)
1245 if (!(iaUser->phase.GetPhase() == 1 || iaUser->phase.GetPhase() == 3))
1248 enabledDirs = 0xFFffFFff;
1250 iaUser->phase.SetPhase2();
1251 printfd(__FILE__, "Phase changed from %d to 2. Reason: CONN_SYN_6\n", iaUser->phase.GetPhase());
1254 //-----------------------------------------------------------------------------
1255 int AUTH_IA::Process_CONN_SYN_7(CONN_SYN_7 * connSyn, IA_USER * iaUser, uint32_t sip)
1257 return Process_CONN_SYN_6(connSyn, iaUser, sip);
1259 //-----------------------------------------------------------------------------
1260 int AUTH_IA::Process_CONN_SYN_8(CONN_SYN_8 * connSyn, IA_USER * iaUser, uint32_t sip)
1263 SwapBytes(connSyn->dirs);
1265 int ret = Process_CONN_SYN_6((CONN_SYN_6*)connSyn, iaUser, sip);
1266 enabledDirs = connSyn->dirs;
1269 //-----------------------------------------------------------------------------
1270 int AUTH_IA::Process_CONN_ACK_6(CONN_ACK_6 * connAck, IA_USER * iaUser, uint32_t sip)
1273 SwapBytes(connAck->len);
1274 SwapBytes(connAck->rnd);
1276 printfd( __FILE__, "CONN_ACK_6 %s\n", connAck->type);
1277 // ÕÓÔÁÎÏ×ÉÔØ ÎÏ×ÕÀ ÆÁÚÕ É ×ÒÅÍÑ É ÒÁÚÒÅÛÉÔØ ÉÎÅÔ
1278 if ((iaUser->phase.GetPhase() == 2) && (connAck->rnd == iaUser->rnd + 1))
1280 iaUser->phase.UpdateTime();
1282 iaUser->lastSendAlive = iaUser->phase.GetTime();
1283 if (iaUser->user->Authorize(sip, enabledDirs, this) == 0)
1285 iaUser->phase.SetPhase3();
1286 printfd(__FILE__, "Phase changed from 2 to 3. Reason: CONN_ACK_6\n");
1291 errorStr = iaUser->user->GetStrError();
1292 iaUser->phase.SetPhase1();
1293 printfd(__FILE__, "Phase changed from 2 to 1. Reason: failed to authorize user\n");
1297 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), connAck->rnd);
1300 //-----------------------------------------------------------------------------
1301 int AUTH_IA::Process_CONN_ACK_7(CONN_ACK_7 * connAck, IA_USER * iaUser, uint32_t sip)
1303 return Process_CONN_ACK_6(connAck, iaUser, sip);
1305 //-----------------------------------------------------------------------------
1306 int AUTH_IA::Process_CONN_ACK_8(CONN_ACK_8 * connAck, IA_USER * iaUser, uint32_t sip)
1309 SwapBytes(connAck->len);
1310 SwapBytes(connAck->rnd);
1312 printfd( __FILE__, "CONN_ACK_8 %s\n", connAck->type);
1314 if ((iaUser->phase.GetPhase() == 2) && (connAck->rnd == iaUser->rnd + 1))
1316 iaUser->phase.UpdateTime();
1317 iaUser->lastSendAlive = iaUser->phase.GetTime();
1318 if (iaUser->user->Authorize(sip, enabledDirs, this) == 0)
1320 iaUser->phase.SetPhase3();
1321 printfd(__FILE__, "Phase changed from 2 to 3. Reason: CONN_ACK_8\n");
1326 errorStr = iaUser->user->GetStrError();
1327 iaUser->phase.SetPhase1();
1328 printfd(__FILE__, "Phase changed from 2 to 1. Reason: failed to authorize user\n");
1332 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), connAck->rnd);
1335 //-----------------------------------------------------------------------------
1336 int AUTH_IA::Process_ALIVE_ACK_6(ALIVE_ACK_6 * aliveAck, IA_USER * iaUser, uint32_t)
1339 SwapBytes(aliveAck->len);
1340 SwapBytes(aliveAck->rnd);
1342 printfd(__FILE__, "ALIVE_ACK_6\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_ALIVE_ACK_7(ALIVE_ACK_7 * aliveAck, IA_USER * iaUser, uint32_t sip)
1355 return Process_ALIVE_ACK_6(aliveAck, iaUser, sip);
1357 //-----------------------------------------------------------------------------
1358 int AUTH_IA::Process_ALIVE_ACK_8(ALIVE_ACK_8 * aliveAck, IA_USER * iaUser, uint32_t)
1361 SwapBytes(aliveAck->len);
1362 SwapBytes(aliveAck->rnd);
1364 printfd(__FILE__, "ALIVE_ACK_8\n");
1365 if ((iaUser->phase.GetPhase() == 3) && (aliveAck->rnd == iaUser->rnd + 1))
1367 iaUser->phase.UpdateTime();
1369 iaUser->aliveSent = false;
1374 //-----------------------------------------------------------------------------
1375 int AUTH_IA::Process_DISCONN_SYN_6(DISCONN_SYN_6 *, IA_USER * iaUser, uint32_t)
1377 printfd(__FILE__, "DISCONN_SYN_6\n");
1378 if (iaUser->phase.GetPhase() != 3)
1380 printfd(__FILE__, "Invalid phase. Expected 3, actual %d\n", iaUser->phase.GetPhase());
1381 errorStr = "Incorrect request DISCONN_SYN";
1385 iaUser->phase.SetPhase4();
1386 printfd(__FILE__, "Phase changed from 3 to 4. Reason: DISCONN_SYN_6\n");
1390 //-----------------------------------------------------------------------------
1391 int AUTH_IA::Process_DISCONN_SYN_7(DISCONN_SYN_7 * disconnSyn, IA_USER * iaUser, uint32_t sip)
1393 return Process_DISCONN_SYN_6(disconnSyn, iaUser, sip);
1395 //-----------------------------------------------------------------------------
1396 int AUTH_IA::Process_DISCONN_SYN_8(DISCONN_SYN_8 *, IA_USER * iaUser, uint32_t)
1398 if (iaUser->phase.GetPhase() != 3)
1400 errorStr = "Incorrect request DISCONN_SYN";
1401 printfd(__FILE__, "Invalid phase. Expected 3, actual %d\n", iaUser->phase.GetPhase());
1405 iaUser->phase.SetPhase4();
1406 printfd(__FILE__, "Phase changed from 3 to 4. Reason: DISCONN_SYN_6\n");
1410 //-----------------------------------------------------------------------------
1411 int AUTH_IA::Process_DISCONN_ACK_6(DISCONN_ACK_6 * disconnAck,
1414 map<uint32_t, IA_USER>::iterator)
1417 SwapBytes(disconnAck->len);
1418 SwapBytes(disconnAck->rnd);
1420 printfd(__FILE__, "DISCONN_ACK_6\n");
1421 if (!((iaUser->phase.GetPhase() == 4) && (disconnAck->rnd == iaUser->rnd + 1)))
1423 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), disconnAck->rnd);
1429 //-----------------------------------------------------------------------------
1430 int AUTH_IA::Process_DISCONN_ACK_7(DISCONN_ACK_7 * disconnAck, IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1432 return Process_DISCONN_ACK_6(disconnAck, iaUser, sip, it);
1434 //-----------------------------------------------------------------------------
1435 int AUTH_IA::Process_DISCONN_ACK_8(DISCONN_ACK_8 * disconnAck, IA_USER * iaUser, uint32_t, map<uint32_t, IA_USER>::iterator)
1438 SwapBytes(disconnAck->len);
1439 SwapBytes(disconnAck->rnd);
1441 printfd(__FILE__, "DISCONN_ACK_8\n");
1442 if (!((iaUser->phase.GetPhase() == 4) && (disconnAck->rnd == iaUser->rnd + 1)))
1444 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), disconnAck->rnd);
1450 //-----------------------------------------------------------------------------
1451 int AUTH_IA::Send_CONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip)
1453 //+++ Fill static data in connSynAck +++
1454 // TODO Move this code. It must be executed only once
1455 connSynAck6.len = Min8(sizeof(CONN_SYN_ACK_6));
1456 strcpy((char*)connSynAck6.type, "CONN_SYN_ACK");
1457 for (int j = 0; j < DIR_NUM; j++)
1459 strncpy((char*)connSynAck6.dirName[j],
1460 stgSettings->GetDirName(j).c_str(),
1463 connSynAck6.dirName[j][sizeof(string16) - 1] = 0;
1465 //--- Fill static data in connSynAck ---
1467 iaUser->rnd = random();
1468 connSynAck6.rnd = iaUser->rnd;
1470 connSynAck6.userTimeOut = iaSettings.GetUserTimeout();
1471 connSynAck6.aliveDelay = iaSettings.GetUserDelay();
1474 SwapBytes(connSynAck6.len);
1475 SwapBytes(connSynAck6.rnd);
1476 SwapBytes(connSynAck6.userTimeOut);
1477 SwapBytes(connSynAck6.aliveDelay);
1480 Encrypt(&iaUser->ctx, (char*)&connSynAck6, (char*)&connSynAck6, Min8(sizeof(CONN_SYN_ACK_6))/8);
1481 return Send(sip, iaSettings.GetUserPort(), (char*)&connSynAck6, Min8(sizeof(CONN_SYN_ACK_6)));;
1483 //-----------------------------------------------------------------------------
1484 int AUTH_IA::Send_CONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip)
1486 return Send_CONN_SYN_ACK_6(iaUser, sip);
1488 //-----------------------------------------------------------------------------
1489 int AUTH_IA::Send_CONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip)
1491 strcpy((char*)connSynAck8.hdr.magic, IA_ID);
1492 connSynAck8.hdr.protoVer[0] = 0;
1493 connSynAck8.hdr.protoVer[1] = 8;
1495 //+++ Fill static data in connSynAck +++
1496 // TODO Move this code. It must be executed only once
1497 connSynAck8.len = Min8(sizeof(CONN_SYN_ACK_8));
1498 strcpy((char*)connSynAck8.type, "CONN_SYN_ACK");
1499 for (int j = 0; j < DIR_NUM; j++)
1501 strncpy((char*)connSynAck8.dirName[j],
1502 stgSettings->GetDirName(j).c_str(),
1505 connSynAck8.dirName[j][sizeof(string16) - 1] = 0;
1507 //--- Fill static data in connSynAck ---
1509 iaUser->rnd = random();
1510 connSynAck8.rnd = iaUser->rnd;
1512 connSynAck8.userTimeOut = iaSettings.GetUserTimeout();
1513 connSynAck8.aliveDelay = iaSettings.GetUserDelay();
1516 SwapBytes(connSynAck8.len);
1517 SwapBytes(connSynAck8.rnd);
1518 SwapBytes(connSynAck8.userTimeOut);
1519 SwapBytes(connSynAck8.aliveDelay);
1522 Encrypt(&iaUser->ctx, (char*)&connSynAck8, (char*)&connSynAck8, Min8(sizeof(CONN_SYN_ACK_8))/8);
1523 return Send(sip, iaUser->port, (char*)&connSynAck8, Min8(sizeof(CONN_SYN_ACK_8)));
1524 //return Send(sip, iaUser->port, (char*)&connSynAck8, 384);
1526 //-----------------------------------------------------------------------------
1527 int AUTH_IA::Send_ALIVE_SYN_6(IA_USER * iaUser, uint32_t sip)
1529 aliveSyn6.len = Min8(sizeof(ALIVE_SYN_6));
1530 aliveSyn6.rnd = iaUser->rnd = random();
1532 strcpy((char*)aliveSyn6.type, "ALIVE_SYN");
1534 for (int i = 0; i < DIR_NUM; i++)
1536 aliveSyn6.md[i] = iaUser->user->GetProperty().down.Get()[i];
1537 aliveSyn6.mu[i] = iaUser->user->GetProperty().up.Get()[i];
1539 aliveSyn6.sd[i] = iaUser->user->GetSessionDownload()[i];
1540 aliveSyn6.su[i] = iaUser->user->GetSessionUpload()[i];
1544 int dn = iaSettings.GetFreeMbShowType();
1545 const TARIFF * tf = iaUser->user->GetTariff();
1549 double p = tf->GetPriceWithTraffType(aliveSyn6.mu[dn],
1556 snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "---");
1560 double fmb = iaUser->user->GetProperty().freeMb;
1561 fmb = fmb < 0 ? 0 : fmb;
1562 snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "%.3f", fmb / p);
1567 if (freeMbNone == iaSettings.GetFreeMbShowType())
1569 aliveSyn6.freeMb[0] = 0;
1573 double fmb = iaUser->user->GetProperty().freeMb;
1574 fmb = fmb < 0 ? 0 : fmb;
1575 snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "C%.3f", fmb);
1580 if (iaUser->aliveSent)
1582 printfd(__FILE__, "========= ALIVE_ACK_6(7) TIMEOUT !!! %s =========\n", iaUser->user->GetLogin().c_str());
1584 iaUser->aliveSent = true;
1587 aliveSyn6.cash =(int64_t) (iaUser->user->GetProperty().cash.Get() * 1000.0);
1588 if (!stgSettings->GetShowFeeInCash())
1589 aliveSyn6.cash -= (int64_t)(tf->GetFee() * 1000.0);
1592 SwapBytes(aliveSyn6.len);
1593 SwapBytes(aliveSyn6.rnd);
1594 SwapBytes(aliveSyn6.cash);
1595 for (int i = 0; i < DIR_NUM; ++i)
1597 SwapBytes(aliveSyn6.mu[i]);
1598 SwapBytes(aliveSyn6.md[i]);
1599 SwapBytes(aliveSyn6.su[i]);
1600 SwapBytes(aliveSyn6.sd[i]);
1604 Encrypt(&(iaUser->ctx), (char*)&aliveSyn6, (char*)&aliveSyn6, Min8(sizeof(aliveSyn6))/8);
1605 return Send(sip, iaSettings.GetUserPort(), (char*)&aliveSyn6, Min8(sizeof(aliveSyn6)));
1607 //-----------------------------------------------------------------------------
1608 int AUTH_IA::Send_ALIVE_SYN_7(IA_USER * iaUser, uint32_t sip)
1610 return Send_ALIVE_SYN_6(iaUser, sip);
1612 //-----------------------------------------------------------------------------
1613 int AUTH_IA::Send_ALIVE_SYN_8(IA_USER * iaUser, uint32_t sip)
1615 strcpy((char*)aliveSyn8.hdr.magic, IA_ID);
1616 aliveSyn8.hdr.protoVer[0] = 0;
1617 aliveSyn8.hdr.protoVer[1] = 8;
1619 aliveSyn8.len = Min8(sizeof(ALIVE_SYN_8));
1620 aliveSyn8.rnd = iaUser->rnd = random();
1622 strcpy((char*)aliveSyn8.type, "ALIVE_SYN");
1624 for (int i = 0; i < DIR_NUM; i++)
1626 aliveSyn8.md[i] = iaUser->user->GetProperty().down.Get()[i];
1627 aliveSyn8.mu[i] = iaUser->user->GetProperty().up.Get()[i];
1629 aliveSyn8.sd[i] = iaUser->user->GetSessionDownload()[i];
1630 aliveSyn8.su[i] = iaUser->user->GetSessionUpload()[i];
1634 int dn = iaSettings.GetFreeMbShowType();
1638 const TARIFF * tf = iaUser->user->GetTariff();
1639 double p = tf->GetPriceWithTraffType(aliveSyn8.mu[dn],
1646 snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "---");
1650 double fmb = iaUser->user->GetProperty().freeMb;
1651 fmb = fmb < 0 ? 0 : fmb;
1652 snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "%.3f", fmb / p);
1657 if (freeMbNone == iaSettings.GetFreeMbShowType())
1659 aliveSyn8.freeMb[0] = 0;
1663 double fmb = iaUser->user->GetProperty().freeMb;
1664 fmb = fmb < 0 ? 0 : fmb;
1665 snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "C%.3f", fmb);
1670 if (iaUser->aliveSent)
1672 printfd(__FILE__, "========= ALIVE_ACK_8 TIMEOUT !!! =========\n");
1674 iaUser->aliveSent = true;
1677 const TARIFF * tf = iaUser->user->GetTariff();
1679 aliveSyn8.cash =(int64_t) (iaUser->user->GetProperty().cash.Get() * 1000.0);
1680 if (!stgSettings->GetShowFeeInCash())
1681 aliveSyn8.cash -= (int64_t)(tf->GetFee() * 1000.0);
1684 SwapBytes(aliveSyn8.len);
1685 SwapBytes(aliveSyn8.rnd);
1686 SwapBytes(aliveSyn8.cash);
1687 SwapBytes(aliveSyn8.status);
1688 for (int i = 0; i < DIR_NUM; ++i)
1690 SwapBytes(aliveSyn8.mu[i]);
1691 SwapBytes(aliveSyn8.md[i]);
1692 SwapBytes(aliveSyn8.su[i]);
1693 SwapBytes(aliveSyn8.sd[i]);
1697 Encrypt(&(iaUser->ctx), (char*)&aliveSyn8, (char*)&aliveSyn8, Min8(sizeof(aliveSyn8))/8);
1698 return Send(sip, iaUser->port, (char*)&aliveSyn8, Min8(sizeof(aliveSyn8)));
1700 //-----------------------------------------------------------------------------
1701 int AUTH_IA::Send_DISCONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip)
1703 disconnSynAck6.len = Min8(sizeof(DISCONN_SYN_ACK_6));
1704 strcpy((char*)disconnSynAck6.type, "DISCONN_SYN_ACK");
1705 disconnSynAck6.rnd = iaUser->rnd = random();
1708 SwapBytes(disconnSynAck6.len);
1709 SwapBytes(disconnSynAck6.rnd);
1712 Encrypt(&iaUser->ctx, (char*)&disconnSynAck6, (char*)&disconnSynAck6, Min8(sizeof(disconnSynAck6))/8);
1713 return Send(sip, iaSettings.GetUserPort(), (char*)&disconnSynAck6, Min8(sizeof(disconnSynAck6)));
1715 //-----------------------------------------------------------------------------
1716 int AUTH_IA::Send_DISCONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip)
1718 return Send_DISCONN_SYN_ACK_6(iaUser, sip);
1720 //-----------------------------------------------------------------------------
1721 int AUTH_IA::Send_DISCONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip)
1723 strcpy((char*)disconnSynAck8.hdr.magic, IA_ID);
1724 disconnSynAck8.hdr.protoVer[0] = 0;
1725 disconnSynAck8.hdr.protoVer[1] = 8;
1727 disconnSynAck8.len = Min8(sizeof(DISCONN_SYN_ACK_8));
1728 strcpy((char*)disconnSynAck8.type, "DISCONN_SYN_ACK");
1729 disconnSynAck8.rnd = iaUser->rnd = random();
1732 SwapBytes(disconnSynAck8.len);
1733 SwapBytes(disconnSynAck8.rnd);
1736 Encrypt(&iaUser->ctx, (char*)&disconnSynAck8, (char*)&disconnSynAck8, Min8(sizeof(disconnSynAck8))/8);
1737 return Send(sip, iaUser->port, (char*)&disconnSynAck8, Min8(sizeof(disconnSynAck8)));
1739 //-----------------------------------------------------------------------------
1740 int AUTH_IA::Send_FIN_6(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1742 fin6.len = Min8(sizeof(FIN_6));
1743 strcpy((char*)fin6.type, "FIN");
1744 strcpy((char*)fin6.ok, "OK");
1747 SwapBytes(fin6.len);
1750 Encrypt(&iaUser->ctx, (char*)&fin6, (char*)&fin6, Min8(sizeof(fin6))/8);
1752 iaUser->user->Unauthorize(this);
1754 int ret = Send(sip, iaSettings.GetUserPort(), (char*)&fin6, Min8(sizeof(fin6)));
1758 //-----------------------------------------------------------------------------
1759 int AUTH_IA::Send_FIN_7(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1761 return Send_FIN_6(iaUser, sip, it);
1763 //-----------------------------------------------------------------------------
1764 int AUTH_IA::Send_FIN_8(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1766 strcpy((char*)fin8.hdr.magic, IA_ID);
1767 fin8.hdr.protoVer[0] = 0;
1768 fin8.hdr.protoVer[1] = 8;
1770 fin8.len = Min8(sizeof(FIN_8));
1771 strcpy((char*)fin8.type, "FIN");
1772 strcpy((char*)fin8.ok, "OK");
1775 SwapBytes(fin8.len);
1778 Encrypt(&iaUser->ctx, (char*)&fin8, (char*)&fin8, Min8(sizeof(fin8))/8);
1780 iaUser->user->Unauthorize(this);
1782 int ret = Send(sip, iaUser->port, (char*)&fin8, Min8(sizeof(fin8)));
1786 //-----------------------------------------------------------------------------
1787 bool AUTH_IA::WaitPackets(int sd) const
1795 tv.tv_usec = 500000;
1797 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
1798 if (res == -1) // Error
1802 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
1807 if (res == 0) // Timeout
1814 //-----------------------------------------------------------------------------
1816 void InitEncrypt(BLOWFISH_CTX * ctx, const string & password)
1818 unsigned char keyL[PASSWD_LEN]; // ðÁÒÏÌØ ÄÌÑ ÛÉÆÒÏ×ËÉ
1819 memset(keyL, 0, PASSWD_LEN);
1820 strncpy((char *)keyL, password.c_str(), PASSWD_LEN);
1821 Blowfish_Init(ctx, keyL, PASSWD_LEN);
1823 //-----------------------------------------------------------------------------
1825 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
1827 // len8 - ÄÌÉÎÁ × 8-ÍÉ ÂÁÊÔÏ×ÙÈ ÂÌÏËÁÈ
1829 for (int i = 0; i < len8; i++)
1830 DecodeString(dst + i * 8, src + i * 8, ctx);
1832 //-----------------------------------------------------------------------------
1834 void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
1836 // len8 - ÄÌÉÎÁ × 8-ÍÉ ÂÁÊÔÏ×ÙÈ ÂÌÏËÁÈ
1838 for (int i = 0; i < len8; i++)
1839 EncodeString(dst + i * 8, src + i * 8, ctx);