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>
37 #include "inetaccess.h"
40 extern volatile const time_t stgTime;
42 void InitEncrypt(BLOWFISH_CTX * ctx, const string & password);
43 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
44 void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
46 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
74 BASE_PLUGIN * GetPlugin()
76 return iac.GetPlugin();
78 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
81 AUTH_IA_SETTINGS::AUTH_IA_SETTINGS()
85 freeMbShowType(freeMbCash)
88 //-----------------------------------------------------------------------------
89 int AUTH_IA_SETTINGS::ParseIntInRange(const string & str, int min, int max, int * val)
91 if (str2x(str.c_str(), *val))
93 errorStr = "Incorrect value \'" + str + "\'.";
96 if (*val < min || *val > max)
98 errorStr = "Value \'" + str + "\' out of range.";
103 //-----------------------------------------------------------------------------
104 int AUTH_IA_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
108 vector<PARAM_VALUE>::const_iterator pvi;
109 ///////////////////////////
111 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
112 if (pvi == s.moduleParams.end())
114 errorStr = "Parameter \'Port\' not found.";
115 printfd(__FILE__, "Parameter 'Port' not found\n");
118 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
120 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
121 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
125 ///////////////////////////
126 pv.param = "UserDelay";
127 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
128 if (pvi == s.moduleParams.end())
130 errorStr = "Parameter \'UserDelay\' not found.";
131 printfd(__FILE__, "Parameter 'UserDelay' not found\n");
135 if (ParseIntInRange(pvi->value[0], 5, 600, &userDelay))
137 errorStr = "Cannot parse parameter \'UserDelay\': " + errorStr;
138 printfd(__FILE__, "Cannot parse parameter 'UserDelay'\n");
141 ///////////////////////////
142 pv.param = "UserTimeout";
143 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
144 if (pvi == s.moduleParams.end())
146 errorStr = "Parameter \'UserTimeout\' not found.";
147 printfd(__FILE__, "Parameter 'UserTimeout' not found\n");
151 if (ParseIntInRange(pvi->value[0], 15, 1200, &userTimeout))
153 errorStr = "Cannot parse parameter \'UserTimeout\': " + errorStr;
154 printfd(__FILE__, "Cannot parse parameter 'UserTimeout'\n");
157 /////////////////////////////////////////////////////////////
161 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
162 if (pvi == s.moduleParams.end())
164 errorStr = "Parameter \'FreeMb\' not found.";
165 printfd(__FILE__, "Parameter 'FreeMb' not found\n");
168 freeMbType = pvi->value[0];
170 if (strcasecmp(freeMbType.c_str(), "cash") == 0)
172 freeMbShowType = freeMbCash;
174 else if (strcasecmp(freeMbType.c_str(), "none") == 0)
176 freeMbShowType = freeMbNone;
178 else if (!str2x(freeMbType.c_str(), n))
180 if (n < 0 || n >= DIR_NUM)
182 errorStr = "Incorrect parameter \'" + freeMbType + "\'.";
183 printfd(__FILE__, "%s\n", errorStr.c_str());
186 freeMbShowType = (FREEMB)(freeMb0 + n);
190 errorStr = "Incorrect parameter \'" + freeMbType + "\'.";
191 printfd(__FILE__, "%s\n", errorStr.c_str());
194 /////////////////////////////////////////////////////////////
197 //-----------------------------------------------------------------------------
198 //-----------------------------------------------------------------------------
199 //-----------------------------------------------------------------------------
200 #ifdef IA_PHASE_DEBUG
205 gettimeofday(&phaseTime, NULL);
211 gettimeofday(&phaseTime, NULL);
214 //-----------------------------------------------------------------------------
215 IA_PHASE::~IA_PHASE()
217 #ifdef IA_PHASE_DEBUG
218 flog = fopen(log.c_str(), "at");
221 fprintf(flog, "IA %s D\n", login.c_str());
226 //-----------------------------------------------------------------------------
227 #ifdef IA_PHASE_DEBUG
228 void IA_PHASE::SetLogFileName(const string & logFileName)
230 log = logFileName + ".ia.log";
232 //-----------------------------------------------------------------------------
233 void IA_PHASE::SetUserLogin(const string & login)
235 IA_PHASE::login = login;
237 //-----------------------------------------------------------------------------
238 void IA_PHASE::WritePhaseChange(int newPhase)
241 gettimeofday(&newPhaseTime, NULL);
242 flog = fopen(log.c_str(), "at");
243 /*int64_t tn = newPhaseTime.GetSec()*1000000 + newPhaseTime.GetUSec();
244 int64_t to = phaseTime.GetSec()*1000000 + phaseTime.GetUSec();*/
247 string action = newPhase == phase ? "U" : "C";
248 double delta = newPhaseTime.GetSec() - phaseTime.GetSec();
249 delta += (newPhaseTime.GetUSec() - phaseTime.GetUSec()) * 1.0e-6;
250 fprintf(flog, "IA %s %s oldPhase = %d, newPhase = %d. dt = %.6f\n",
260 //-----------------------------------------------------------------------------
261 void IA_PHASE::SetPhase1()
263 #ifdef IA_PHASE_DEBUG
267 gettimeofday(&phaseTime, NULL);
269 //-----------------------------------------------------------------------------
270 void IA_PHASE::SetPhase2()
272 #ifdef IA_PHASE_DEBUG
276 gettimeofday(&phaseTime, NULL);
278 //-----------------------------------------------------------------------------
279 void IA_PHASE::SetPhase3()
281 #ifdef IA_PHASE_DEBUG
285 gettimeofday(&phaseTime, NULL);
287 //-----------------------------------------------------------------------------
288 void IA_PHASE::SetPhase4()
290 #ifdef IA_PHASE_DEBUG
294 gettimeofday(&phaseTime, NULL);
296 //-----------------------------------------------------------------------------
297 void IA_PHASE::SetPhase5()
299 #ifdef IA_PHASE_DEBUG
303 gettimeofday(&phaseTime, NULL);
305 //-----------------------------------------------------------------------------
306 int IA_PHASE::GetPhase() const
310 //-----------------------------------------------------------------------------
311 void IA_PHASE::UpdateTime()
313 #ifdef IA_PHASE_DEBUG
314 WritePhaseChange(phase);
316 gettimeofday(&phaseTime, NULL);
318 //-----------------------------------------------------------------------------
319 const UTIME & IA_PHASE::GetTime() const
323 //-----------------------------------------------------------------------------
324 //-----------------------------------------------------------------------------
325 //-----------------------------------------------------------------------------
329 isRunningRunTimeouter(false),
333 WriteServLog(GetStgLogger()),
334 enabledDirs(0xFFffFFff),
335 onDelUserNotifier(*this)
337 InitEncrypt(&ctxS, "pr7Hhen");
339 pthread_mutexattr_t attr;
340 pthread_mutexattr_init(&attr);
341 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
342 pthread_mutex_init(&mutex, &attr);
344 memset(&connSynAck6, 0, sizeof(CONN_SYN_ACK_6));
345 memset(&connSynAck8, 0, sizeof(CONN_SYN_ACK_8));
346 memset(&disconnSynAck6, 0, sizeof(DISCONN_SYN_ACK_6));
347 memset(&disconnSynAck8, 0, sizeof(DISCONN_SYN_ACK_8));
348 memset(&aliveSyn6, 0, sizeof(ALIVE_SYN_6));
349 memset(&aliveSyn8, 0, sizeof(ALIVE_SYN_8));
350 memset(&fin6, 0, sizeof(FIN_6));
351 memset(&fin8, 0, sizeof(FIN_8));
353 printfd(__FILE__, "sizeof(CONN_SYN_6) = %d %d\n", sizeof(CONN_SYN_6), Min8(sizeof(CONN_SYN_6)));
354 printfd(__FILE__, "sizeof(CONN_SYN_8) = %d %d\n", sizeof(CONN_SYN_8), Min8(sizeof(CONN_SYN_8)));
355 printfd(__FILE__, "sizeof(CONN_SYN_ACK_6) = %d %d\n", sizeof(CONN_SYN_ACK_6), Min8(sizeof(CONN_SYN_ACK_6)));
356 printfd(__FILE__, "sizeof(CONN_SYN_ACK_8) = %d %d\n", sizeof(CONN_SYN_ACK_8), Min8(sizeof(CONN_SYN_ACK_8)));
357 printfd(__FILE__, "sizeof(CONN_ACK_6) = %d %d\n", sizeof(CONN_ACK_6), Min8(sizeof(CONN_ACK_6)));
358 printfd(__FILE__, "sizeof(ALIVE_SYN_6) = %d %d\n", sizeof(ALIVE_SYN_6), Min8(sizeof(ALIVE_SYN_6)));
359 printfd(__FILE__, "sizeof(ALIVE_SYN_8) = %d %d\n", sizeof(ALIVE_SYN_8), Min8(sizeof(ALIVE_SYN_8)));
360 printfd(__FILE__, "sizeof(ALIVE_ACK_6) = %d %d\n", sizeof(ALIVE_ACK_6), Min8(sizeof(ALIVE_ACK_6)));
361 printfd(__FILE__, "sizeof(DISCONN_SYN_6) = %d %d\n", sizeof(DISCONN_SYN_6), Min8(sizeof(DISCONN_SYN_6)));
362 printfd(__FILE__, "sizeof(DISCONN_SYN_ACK_6) = %d %d\n", sizeof(DISCONN_SYN_ACK_6), Min8(sizeof(DISCONN_SYN_ACK_6)));
363 printfd(__FILE__, "sizeof(DISCONN_SYN_ACK_8) = %d %d\n", sizeof(DISCONN_SYN_ACK_8), Min8(sizeof(DISCONN_SYN_ACK_8)));
364 printfd(__FILE__, "sizeof(DISCONN_ACK_6) = %d %d\n", sizeof(DISCONN_ACK_6), Min8(sizeof(DISCONN_ACK_6)));
365 printfd(__FILE__, "sizeof(FIN_6) = %d %d\n", sizeof(FIN_6), Min8(sizeof(FIN_6)));
366 printfd(__FILE__, "sizeof(FIN_8) = %d %d\n", sizeof(FIN_8), Min8(sizeof(FIN_8)));
367 printfd(__FILE__, "sizeof(ERR) = %d %d\n", sizeof(ERR), Min8(sizeof(ERR)));
368 printfd(__FILE__, "sizeof(INFO_6) = %d %d\n", sizeof(INFO_6), Min8(sizeof(INFO_6)));
369 printfd(__FILE__, "sizeof(INFO_7) = %d %d\n", sizeof(INFO_7), Min8(sizeof(INFO_7)));
370 printfd(__FILE__, "sizeof(INFO_8) = %d %d\n", sizeof(INFO_8), Min8(sizeof(INFO_8)));
372 packetTypes["CONN_SYN"] = CONN_SYN_N;
373 packetTypes["CONN_SYN_ACK"] = CONN_SYN_ACK_N;
374 packetTypes["CONN_ACK"] = CONN_ACK_N;
375 packetTypes["ALIVE_SYN"] = ALIVE_SYN_N;
376 packetTypes["ALIVE_ACK"] = ALIVE_ACK_N;
377 packetTypes["DISCONN_SYN"] = DISCONN_SYN_N;
378 packetTypes["DISCONN_SYN_ACK"] = DISCONN_SYN_ACK_N;
379 packetTypes["DISCONN_ACK"] = DISCONN_ACK_N;
380 packetTypes["FIN"] = FIN_N;
381 packetTypes["ERR"] = ERROR_N;
383 //-----------------------------------------------------------------------------
386 pthread_mutex_destroy(&mutex);
388 //-----------------------------------------------------------------------------
391 users->AddNotifierUserDel(&onDelUserNotifier);
401 if (pthread_create(&recvThread, NULL, Run, this))
403 errorStr = "Cannot create thread.";
404 printfd(__FILE__, "Cannot create recv thread\n");
409 if (!isRunningRunTimeouter)
411 if (pthread_create(&timeouterThread, NULL, RunTimeouter, this))
413 errorStr = "Cannot create thread.";
414 printfd(__FILE__, "Cannot create timeouter thread\n");
421 //-----------------------------------------------------------------------------
432 UnauthorizeUser(this)
437 //5 seconds to thread stops itself
438 for (int i = 0; i < 25 && isRunningRun; i++)
443 //after 5 seconds waiting thread still running. now killing it
446 //TODO pthread_cancel()
447 if (pthread_kill(recvThread, SIGINT))
449 errorStr = "Cannot kill thread.";
450 printfd(__FILE__, "Cannot kill thread\n");
453 for (int i = 0; i < 25 && isRunningRun; ++i)
457 printfd(__FILE__, "Failed to stop recv thread\n");
461 pthread_join(recvThread, NULL);
463 printfd(__FILE__, "AUTH_IA killed Run\n");
469 if (isRunningRunTimeouter)
471 //5 seconds to thread stops itself
472 for (int i = 0; i < 25 && isRunningRunTimeouter; i++)
477 //after 5 seconds waiting thread still running. now killing it
478 if (isRunningRunTimeouter)
480 //TODO pthread_cancel()
481 if (pthread_kill(timeouterThread, SIGINT))
483 errorStr = "Cannot kill thread.";
486 for (int i = 0; i < 25 && isRunningRunTimeouter; ++i)
488 if (isRunningRunTimeouter)
490 printfd(__FILE__, "Failed to stop timeouter thread\n");
494 pthread_join(timeouterThread, NULL);
496 printfd(__FILE__, "AUTH_IA killed Timeouter\n");
499 printfd(__FILE__, "AUTH_IA::Stoped successfully.\n");
500 users->DelNotifierUserDel(&onDelUserNotifier);
503 //-----------------------------------------------------------------------------
504 void * AUTH_IA::Run(void * d)
506 AUTH_IA * ia = static_cast<AUTH_IA *>(d);
508 ia->isRunningRun = true;
512 time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
516 ia->RecvData(buffer, sizeof(buffer));
517 if ((touchTime + MONITOR_TIME_DELAY_SEC <= stgTime) && ia->stgSettings->GetMonitoring())
520 string monFile = ia->stgSettings->GetMonitorDir() + "/inetaccess_r";
521 TouchFile(monFile.c_str());
525 ia->isRunningRun = false;
528 //-----------------------------------------------------------------------------
529 void * AUTH_IA::RunTimeouter(void * d)
531 AUTH_IA * ia = static_cast<AUTH_IA *>(d);
533 ia->isRunningRunTimeouter = true;
536 string monFile = ia->stgSettings->GetMonitorDir() + "/inetaccess_t";
541 // TODO cahange counter to timer and MONITOR_TIME_DELAY_SEC
542 if (++a % (50*60) == 0 && ia->stgSettings->GetMonitoring())
544 TouchFile(monFile.c_str());
548 ia->isRunningRunTimeouter = false;
551 //-----------------------------------------------------------------------------
552 int AUTH_IA::ParseSettings()
554 int ret = iaSettings.ParseSettings(settings);
556 errorStr = iaSettings.GetStrError();
559 //-----------------------------------------------------------------------------
560 int AUTH_IA::PrepareNet()
562 struct sockaddr_in listenAddr;
564 listenSocket = socket(AF_INET, SOCK_DGRAM, 0);
566 if (listenSocket < 0)
568 errorStr = "Cannot create socket.";
572 listenAddr.sin_family = AF_INET;
573 listenAddr.sin_port = htons(iaSettings.GetUserPort());
574 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
576 if (bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr)) < 0)
578 errorStr = "AUTH_IA: Bind failed.";
583 if (getsockopt(listenSocket, SOL_SOCKET, SO_SNDBUF, &buffLen, sizeof(buffLen)) < 0)
586 errorStr = "Getsockopt failed. " + string(strerror(errno));
590 //WriteServLog("buffLen = %d", buffLen);
594 //-----------------------------------------------------------------------------
595 int AUTH_IA::FinalizeNet()
600 //-----------------------------------------------------------------------------
601 int AUTH_IA::RecvData(char * buffer, int bufferSize)
603 if (!WaitPackets(listenSocket)) // Timeout
608 struct sockaddr_in outerAddr;
609 socklen_t outerAddrLen(sizeof(outerAddr));
610 int dataLen = recvfrom(listenSocket, buffer, bufferSize, 0, (struct sockaddr *)&outerAddr, &outerAddrLen);
617 if (dataLen <= 0) // Error
621 printfd(__FILE__, "recvfrom res=%d, error: '%s'\n", dataLen, strerror(errno));
631 if (CheckHeader(buffer, &protoVer))
634 char login[PASSWD_LEN]; //TODO why PASSWD_LEN ?
635 memset(login, 0, PASSWD_LEN);
637 Decrypt(&ctxS, login, buffer + 8, PASSWD_LEN / 8);
639 uint32_t sip = *((uint32_t*)&outerAddr.sin_addr);
640 uint16_t sport = htons(outerAddr.sin_port);
643 if (users->FindByName(login, &user) == 0)
645 printfd(__FILE__, "User %s FOUND!\n", user->GetLogin().c_str());
646 PacketProcessor(buffer, dataLen, sip, sport, protoVer, &user);
650 WriteServLog("User\'s connect failed:: user \'%s\' not found. IP \'%s\'",
652 inet_ntostring(sip).c_str());
653 printfd(__FILE__, "User %s NOT found!\n", login);
654 SendError(sip, sport, protoVer, "îÅÐÒÁ×ÉÌØÎÙÊ ÌÏÇÉÎ ÉÌÉ ÐÁÒÏÌØ!");
660 //-----------------------------------------------------------------------------
661 int AUTH_IA::CheckHeader(const char * buffer, int * protoVer)
663 if (strncmp(IA_ID, buffer, strlen(IA_ID)) != 0)
665 //SendError(userIP, updateMsg);
666 printfd(__FILE__, "update needed - IA_ID\n");
667 //SendError(userIP, "Incorrect header!");
671 if (buffer[6] != 0) //proto[0] shoud be 0
673 printfd(__FILE__, "update needed - PROTO major: %d\n", buffer[6]);
674 //SendError(userIP, updateMsg);
681 //SendError(userIP, updateMsg);
682 printfd(__FILE__, "update needed - PROTO minor: %d\n", buffer[7]);
687 *protoVer = buffer[7];
691 //-----------------------------------------------------------------------------
692 int AUTH_IA::Timeouter()
694 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
696 map<uint32_t, IA_USER>::iterator it;
697 it = ip2user.begin();
700 while (it != ip2user.end())
704 static UTIME currTime;
705 gettimeofday(&currTime, NULL);
707 if ((it->second.phase.GetPhase() == 2)
708 && (currTime - it->second.phase.GetTime()) > iaSettings.GetUserDelay())
710 it->second.phase.SetPhase1();
711 printfd(__FILE__, "Phase changed from 2 to 1. Reason: timeout\n");
714 if (it->second.phase.GetPhase() == 3)
716 if (!it->second.messagesToSend.empty())
718 if (it->second.protoVer == 6)
719 RealSendMessage6(*it->second.messagesToSend.begin(), sip, it->second);
721 if (it->second.protoVer == 7)
722 RealSendMessage7(*it->second.messagesToSend.begin(), sip, it->second);
724 if (it->second.protoVer == 8)
725 RealSendMessage8(*it->second.messagesToSend.begin(), sip, it->second);
727 it->second.messagesToSend.erase(it->second.messagesToSend.begin());
730 if((currTime - it->second.lastSendAlive) > iaSettings.GetUserDelay())
732 switch (it->second.protoVer)
735 Send_ALIVE_SYN_6(&(it->second), sip);
738 Send_ALIVE_SYN_7(&(it->second), sip);
741 Send_ALIVE_SYN_8(&(it->second), sip);
745 gettimeofday(&it->second.lastSendAlive, NULL);
748 if ((currTime - it->second.phase.GetTime()) > iaSettings.GetUserTimeout())
750 it->second.user->Unauthorize(this);
756 if ((it->second.phase.GetPhase() == 4)
757 && ((currTime - it->second.phase.GetTime()) > iaSettings.GetUserDelay()))
759 it->second.phase.SetPhase3();
760 printfd(__FILE__, "Phase changed from 4 to 3. Reason: timeout\n");
768 //-----------------------------------------------------------------------------
769 int AUTH_IA::PacketProcessor(char * buff, int dataLen, uint32_t sip, uint16_t sport, int protoVer, user_iter * user)
771 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
772 // ôÕÔ ÓÏÂÒÁÎÙ ÏÂÒÁÂÏÔÞÉËÉ ÒÁÚÎÙÈ ÐÁËÅÔÏ×
774 const int offset = LOGIN_LEN + 2 + 6; // LOGIN_LEN + sizeOfMagic + sizeOfVer;
776 IA_USER * iaUser = NULL;
778 CONN_SYN_6 * connSyn6;
779 CONN_SYN_7 * connSyn7;
780 CONN_SYN_8 * connSyn8;
782 CONN_ACK_6 * connAck;
783 ALIVE_ACK_6 * aliveAck;
784 DISCONN_SYN_6 * disconnSyn;
785 DISCONN_ACK_6 * disconnAck;
787 map<uint32_t, IA_USER>::iterator it;
788 it = ip2user.find(sip);
790 if (it == ip2user.end() || (*user)->GetID() != it->second.user->GetID())
792 // åÝÅ ÎÅ ÂÙÌÏ ÚÁÐÒÏÓÏ× Ó ÜÔÏÇÏ IP
793 printfd(__FILE__, "Add new user\n");
794 ip2user[sip].protoVer = protoVer;
795 ip2user[sip].user = *user;
796 ip2user[sip].port = sport;
797 #ifdef IA_PHASE_DEBUG
798 ip2user[sip].phase.SetLogFileName(stgSettings->GetLogFileName());
799 ip2user[sip].phase.SetUserLogin((*user)->GetLogin());
803 it = ip2user.find(sip); //TODO
804 if (it == ip2user.end())
806 printfd(__FILE__, "+++ ERROR +++\n");
811 iaUser = &(it->second);
813 if (iaUser->port != sport)
814 iaUser->port = sport;
816 if (iaUser->password != (*user)->property.password.Get())
818 InitEncrypt(&iaUser->ctx, (*user)->property.password.Get());
819 iaUser->password = (*user)->property.password.Get();
823 Decrypt(&iaUser->ctx, buff, buff, (dataLen - offset) / 8);
825 char packetName[IA_MAX_TYPE_LEN];
826 strncpy(packetName, buff + 4, IA_MAX_TYPE_LEN);
827 packetName[IA_MAX_TYPE_LEN - 1] = 0;
829 map<string, int>::iterator pi;
830 pi = packetTypes.find(packetName);
831 if (pi == packetTypes.end())
833 SendError(sip, sport, protoVer, "îÅÐÒÁ×ÉÌØÎÙÊ ÌÏÇÉÎ ÉÌÉ ÐÁÒÏÌØ!");
834 printfd(__FILE__, "Login or password is wrong!\n");
835 WriteServLog("User's connect failed. IP \'%s\'. Wrong login or password", inet_ntostring(sip).c_str());
843 if ((*user)->property.disabled.Get())
845 SendError(sip, sport, protoVer, "õÞÅÔÎÁÑ ÚÁÐÉÓØ ÚÁÂÌÏËÉÒÏ×ÁÎÁ");
849 if ((*user)->property.passive.Get())
851 SendError(sip, sport, protoVer, "õÞÅÔÎÁÑ ÚÁÐÉÓØ ÚÁÍÏÒÏÖÅÎÁ");
855 if ((*user)->GetAuthorized() && (*user)->GetCurrIP() != sip)
857 printfd(__FILE__, "Login %s alredy in use. IP \'%s\'\n", (*user)->GetLogin().c_str(), inet_ntostring(sip).c_str());
858 WriteServLog("Login %s alredy in use. IP \'%s\'", (*user)->GetLogin().c_str(), inet_ntostring(sip).c_str());
859 SendError(sip, sport, protoVer, "÷ÁÛ ÌÏÇÉÎ ÕÖÅ ÉÓÐÏÌØÚÕÅÔÓÑ!");
864 if (users->FindByIPIdx(sip, &u) == 0 && u->GetLogin() != (*user)->GetLogin())
866 printfd(__FILE__, "IP address alredy in use. IP \'%s\'", inet_ntostring(sip).c_str());
867 WriteServLog("IP address alredy in use. IP \'%s\'", inet_ntostring(sip).c_str());
868 SendError(sip, sport, protoVer, "÷ÁÛ IP ÁÄÒÅÓ ÕÖÅ ÉÓÐÏÌØÚÕÅÔÓÑ!");
872 // ôÅÐÅÒØ ÍÙ ÄÏÌÖÎÙ ÐÒÏ×ÅÒÉÔØ, ÍÏÖÅÔ ÌÉ ÐÏÌØÚÏ×ÁÔÅÌØ ÐÏÄËÌÀÞÉÔÓÑ Ó ÜÔÏÇÏ ÁÄÒÅÓÁ.
873 int ipFound = (*user)->property.ips.Get().IsIPInIPS(sip);
876 printfd(__FILE__, "User %s. IP address is incorrect. IP \'%s\'\n", (*user)->GetLogin().c_str(), inet_ntostring(sip).c_str());
877 WriteServLog("User %s. IP address is incorrect. IP \'%s\'", (*user)->GetLogin().c_str(), inet_ntostring(sip).c_str());
878 SendError(sip, sport, protoVer, "ðÏÌØÚÏ×ÁÔÅÌØ ÎÅ ÏÐÏÚÎÁÎ! ðÒÏ×ÅÒØÔÅ IP ÁÄÒÅÓ.");
890 connSyn6 = (CONN_SYN_6*)(buff - offset);
891 ret = Process_CONN_SYN_6(connSyn6, &(it->second), user, sip);
894 connSyn7 = (CONN_SYN_7*)(buff - offset);
895 ret = Process_CONN_SYN_7(connSyn7, &(it->second), user, sip);
898 connSyn8 = (CONN_SYN_8*)(buff - offset);
899 ret = Process_CONN_SYN_8(connSyn8, &(it->second), user, sip);
910 Send_CONN_SYN_ACK_6(iaUser, user, sip);
913 Send_CONN_SYN_ACK_7(iaUser, user, sip);
916 Send_CONN_SYN_ACK_8(iaUser, user, sip);
922 connAck = (CONN_ACK_6*)(buff - offset);
926 ret = Process_CONN_ACK_6(connAck, iaUser, user, sip);
929 ret = Process_CONN_ACK_7(connAck, iaUser, user, sip);
932 ret = Process_CONN_ACK_8((CONN_ACK_8*)(buff - offset), iaUser, user, sip);
938 SendError(sip, sport, protoVer, errorStr);
945 Send_ALIVE_SYN_6(iaUser, sip);
948 Send_ALIVE_SYN_7(iaUser, sip);
951 Send_ALIVE_SYN_8(iaUser, sip);
957 // ðÒÉÂÙÌ ÏÔ×ÅÔ Ó ÐÏÄÔ×ÅÒÖÄÅÎÉÅÍ ALIVE
959 aliveAck = (ALIVE_ACK_6*)(buff - offset);
963 ret = Process_ALIVE_ACK_6(aliveAck, iaUser, user, sip);
966 ret = Process_ALIVE_ACK_7(aliveAck, iaUser, user, sip);
969 ret = Process_ALIVE_ACK_8((ALIVE_ACK_8*)(buff - offset), iaUser, user, sip);
974 // úÁÐÒÏÓ ÎÁ ÏÔËÌÀÞÅÎÉÅ
977 disconnSyn = (DISCONN_SYN_6*)(buff - offset);
981 ret = Process_DISCONN_SYN_6(disconnSyn, iaUser, user, sip);
984 ret = Process_DISCONN_SYN_7(disconnSyn, iaUser, user, sip);
987 ret = Process_DISCONN_SYN_8((DISCONN_SYN_8*)(buff - offset), iaUser, user, sip);
997 Send_DISCONN_SYN_ACK_6(iaUser, sip);
1000 Send_DISCONN_SYN_ACK_7(iaUser, sip);
1003 Send_DISCONN_SYN_ACK_8(iaUser, sip);
1009 disconnAck = (DISCONN_ACK_6*)(buff - offset);
1014 ret = Process_DISCONN_ACK_6(disconnAck, iaUser, user, sip, it);
1017 ret = Process_DISCONN_ACK_7(disconnAck, iaUser, user, sip, it);
1020 ret = Process_DISCONN_ACK_8((DISCONN_ACK_8*)(buff - offset), iaUser, user, sip, it);
1027 Send_FIN_6(iaUser, sip, it);
1030 Send_FIN_7(iaUser, sip, it);
1033 Send_FIN_8(iaUser, sip, it);
1041 //-----------------------------------------------------------------------------
1042 void AUTH_IA::DelUser(user_iter u)
1044 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1046 uint32_t ip = u->GetCurrIP();
1051 map<uint32_t, IA_USER>::iterator it;
1052 it = ip2user.find(ip);
1053 if (it == ip2user.end())
1056 printfd(__FILE__, "Nothing to delete\n");
1060 if (it->second.user == u)
1062 printfd(__FILE__, "User removed!\n");
1063 it->second.user->Unauthorize(this);
1067 //-----------------------------------------------------------------------------
1068 int AUTH_IA::SendError(uint32_t ip, uint16_t port, int protoVer, const string & text)
1070 struct sockaddr_in sendAddr;
1077 memset(&err, 0, sizeof(ERR));
1079 sendAddr.sin_family = AF_INET;
1080 sendAddr.sin_port = htons(port);
1082 sendAddr.sin_addr.s_addr = ip;// IP ÐÏÌØÚÏ×ÁÔÅÌÑ
1085 strncpy((char*)err.type, "ERR", 16);
1086 strncpy((char*)err.text, text.c_str(), MAX_MSG_LEN);
1092 res = sendto(listenSocket, &err, sizeof(err), 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
1093 printfd(__FILE__, "SendError %d bytes sent\n", res);
1098 memset(&err8, 0, sizeof(ERR_8));
1100 sendAddr.sin_family = AF_INET;
1101 sendAddr.sin_port = htons(port);
1103 sendAddr.sin_addr.s_addr = ip;// IP ÐÏÌØÚÏ×ÁÔÅÌÑ
1106 strncpy((char*)err8.type, "ERR", 16);
1107 strncpy((char*)err8.text, text.c_str(), MAX_MSG_LEN);
1110 SwapBytes(err8.len);
1113 res = sendto(listenSocket, &err8, sizeof(err8), 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
1114 printfd(__FILE__, "SendError_8 %d bytes sent\n", res);
1120 //-----------------------------------------------------------------------------
1121 int AUTH_IA::Send(uint32_t ip, uint16_t port, const char * buffer, int len)
1123 struct sockaddr_in sendAddr;
1126 sendAddr.sin_family = AF_INET;
1127 sendAddr.sin_port = htons(port);
1128 sendAddr.sin_addr.s_addr = ip;
1130 res = sendto(listenSocket, buffer, len, 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
1132 static struct timeval tv;
1133 gettimeofday(&tv, NULL);
1137 //-----------------------------------------------------------------------------
1138 int AUTH_IA::SendMessage(const STG_MSG & msg, uint32_t ip) const
1140 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1142 printfd(__FILE__, "SendMessage userIP=%s\n", inet_ntostring(ip).c_str());
1144 map<uint32_t, IA_USER>::iterator it;
1145 it = ip2user.find(ip);
1146 if (it == ip2user.end())
1148 errorStr = "Unknown user.";
1151 it->second.messagesToSend.push_back(msg);
1154 //-----------------------------------------------------------------------------
1155 int AUTH_IA::RealSendMessage6(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1157 printfd(__FILE__, "RealSendMessage 6 user=%s\n", user.user->GetLogin().c_str());
1162 memset(&info, 0, sizeof(INFO_6));
1165 strncpy((char*)info.type, "INFO", 16);
1166 info.infoType = 'I';
1167 strncpy((char*)info.text, msg.text.c_str(), 235);
1170 size_t len = info.len;
1172 SwapBytes(info.len);
1175 memcpy(buffer, &info, sizeof(INFO_6));
1176 Encrypt(&user.ctx, buffer, buffer, len / 8);
1177 Send(ip, iaSettings.GetUserPort(), buffer, len);
1181 //-----------------------------------------------------------------------------
1182 int AUTH_IA::RealSendMessage7(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1184 printfd(__FILE__, "RealSendMessage 7 user=%s\n", user.user->GetLogin().c_str());
1189 memset(&info, 0, sizeof(INFO_7));
1192 strncpy((char*)info.type, "INFO_7", 16);
1193 info.infoType = msg.header.type;
1194 info.showTime = msg.header.showTime;
1196 info.sendTime = msg.header.creationTime;
1198 size_t len = info.len;
1200 SwapBytes(info.len);
1201 SwapBytes(info.sendTime);
1204 strncpy((char*)info.text, msg.text.c_str(), MAX_MSG_LEN - 1);
1205 info.text[MAX_MSG_LEN - 1] = 0;
1207 memcpy(buffer, &info, sizeof(INFO_7));
1209 Encrypt(&user.ctx, buffer, buffer, len / 8);
1210 Send(ip, iaSettings.GetUserPort(), buffer, len);
1214 //-----------------------------------------------------------------------------
1215 int AUTH_IA::RealSendMessage8(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1217 printfd(__FILE__, "RealSendMessage 8 user=%s\n", user.user->GetLogin().c_str());
1220 memset(buffer, 0, sizeof(buffer));
1224 memset(&info, 0, sizeof(INFO_8));
1227 strncpy((char*)info.type, "INFO_8", 16);
1228 info.infoType = msg.header.type;
1229 info.showTime = msg.header.showTime;
1230 info.sendTime = msg.header.creationTime;
1232 strncpy((char*)info.text, msg.text.c_str(), IA_MAX_MSG_LEN_8 - 1);
1233 info.text[IA_MAX_MSG_LEN_8 - 1] = 0;
1235 size_t len = info.len;
1237 SwapBytes(info.len);
1238 SwapBytes(info.sendTime);
1241 memcpy(buffer, &info, sizeof(INFO_8));
1243 Encrypt(&user.ctx, buffer, buffer, len / 8);
1244 Send(ip, user.port, buffer, len);
1248 //-----------------------------------------------------------------------------
1249 int AUTH_IA::Process_CONN_SYN_6(CONN_SYN_6 *, IA_USER * iaUser, user_iter *, uint32_t)
1251 if (!(iaUser->phase.GetPhase() == 1 || iaUser->phase.GetPhase() == 3))
1254 enabledDirs = 0xFFffFFff;
1256 iaUser->phase.SetPhase2();
1257 printfd(__FILE__, "Phase changed from %d to 2. Reason: CONN_SYN_6\n", iaUser->phase.GetPhase());
1260 //-----------------------------------------------------------------------------
1261 int AUTH_IA::Process_CONN_SYN_7(CONN_SYN_7 * connSyn, IA_USER * iaUser, user_iter * user, uint32_t sip)
1263 return Process_CONN_SYN_6(connSyn, iaUser, user, sip);
1265 //-----------------------------------------------------------------------------
1266 int AUTH_IA::Process_CONN_SYN_8(CONN_SYN_8 * connSyn, IA_USER * iaUser, user_iter * user, uint32_t sip)
1269 SwapBytes(connSyn->dirs);
1271 int ret = Process_CONN_SYN_6((CONN_SYN_6*)connSyn, iaUser, user, sip);
1272 enabledDirs = connSyn->dirs;
1275 //-----------------------------------------------------------------------------
1276 int AUTH_IA::Process_CONN_ACK_6(CONN_ACK_6 * connAck, IA_USER * iaUser, user_iter *, uint32_t sip)
1279 SwapBytes(connAck->len);
1280 SwapBytes(connAck->rnd);
1282 printfd( __FILE__, "CONN_ACK_6 %s\n", connAck->type);
1283 // ÕÓÔÁÎÏ×ÉÔØ ÎÏ×ÕÀ ÆÁÚÕ É ×ÒÅÍÑ É ÒÁÚÒÅÛÉÔØ ÉÎÅÔ
1284 if ((iaUser->phase.GetPhase() == 2) && (connAck->rnd == iaUser->rnd + 1))
1286 iaUser->phase.UpdateTime();
1288 iaUser->lastSendAlive = iaUser->phase.GetTime();
1289 if (iaUser->user->Authorize(sip, "", enabledDirs, this) == 0)
1291 iaUser->phase.SetPhase3();
1292 printfd(__FILE__, "Phase changed from 2 to 3. Reason: CONN_ACK_6\n");
1297 errorStr = iaUser->user->GetStrError();
1298 iaUser->phase.SetPhase1();
1299 printfd(__FILE__, "Phase changed from 2 to 1. Reason: failed to authorize user\n");
1303 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), connAck->rnd);
1306 //-----------------------------------------------------------------------------
1307 int AUTH_IA::Process_CONN_ACK_7(CONN_ACK_7 * connAck, IA_USER * iaUser, user_iter * user, uint32_t sip)
1309 return Process_CONN_ACK_6(connAck, iaUser, user, sip);
1311 //-----------------------------------------------------------------------------
1312 int AUTH_IA::Process_CONN_ACK_8(CONN_ACK_8 * connAck, IA_USER * iaUser, user_iter *, uint32_t sip)
1315 SwapBytes(connAck->len);
1316 SwapBytes(connAck->rnd);
1318 printfd( __FILE__, "CONN_ACK_8 %s\n", connAck->type);
1320 if ((iaUser->phase.GetPhase() == 2) && (connAck->rnd == iaUser->rnd + 1))
1322 iaUser->phase.UpdateTime();
1323 iaUser->lastSendAlive = iaUser->phase.GetTime();
1324 if (iaUser->user->Authorize(sip, "", enabledDirs, this) == 0)
1326 iaUser->phase.SetPhase3();
1327 printfd(__FILE__, "Phase changed from 2 to 3. Reason: CONN_ACK_8\n");
1332 errorStr = iaUser->user->GetStrError();
1333 iaUser->phase.SetPhase1();
1334 printfd(__FILE__, "Phase changed from 2 to 1. Reason: failed to authorize user\n");
1338 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), connAck->rnd);
1341 //-----------------------------------------------------------------------------
1342 int AUTH_IA::Process_ALIVE_ACK_6(ALIVE_ACK_6 * aliveAck, IA_USER * iaUser, user_iter *, uint32_t)
1345 SwapBytes(aliveAck->len);
1346 SwapBytes(aliveAck->rnd);
1348 printfd(__FILE__, "ALIVE_ACK_6\n");
1349 if ((iaUser->phase.GetPhase() == 3) && (aliveAck->rnd == iaUser->rnd + 1))
1351 iaUser->phase.UpdateTime();
1353 iaUser->aliveSent = false;
1358 //-----------------------------------------------------------------------------
1359 int AUTH_IA::Process_ALIVE_ACK_7(ALIVE_ACK_7 * aliveAck, IA_USER * iaUser, user_iter * user, uint32_t sip)
1361 return Process_ALIVE_ACK_6(aliveAck, iaUser, user, sip);
1363 //-----------------------------------------------------------------------------
1364 int AUTH_IA::Process_ALIVE_ACK_8(ALIVE_ACK_8 * aliveAck, IA_USER * iaUser, user_iter *, uint32_t)
1367 SwapBytes(aliveAck->len);
1368 SwapBytes(aliveAck->rnd);
1370 printfd(__FILE__, "ALIVE_ACK_8\n");
1371 if ((iaUser->phase.GetPhase() == 3) && (aliveAck->rnd == iaUser->rnd + 1))
1373 iaUser->phase.UpdateTime();
1375 iaUser->aliveSent = false;
1380 //-----------------------------------------------------------------------------
1381 int AUTH_IA::Process_DISCONN_SYN_6(DISCONN_SYN_6 *, IA_USER * iaUser, user_iter *, uint32_t)
1383 printfd(__FILE__, "DISCONN_SYN_6\n");
1384 if (iaUser->phase.GetPhase() != 3)
1386 printfd(__FILE__, "Invalid phase. Expected 3, actual %d\n", iaUser->phase.GetPhase());
1387 errorStr = "Incorrect request DISCONN_SYN";
1391 iaUser->phase.SetPhase4();
1392 printfd(__FILE__, "Phase changed from 3 to 4. Reason: DISCONN_SYN_6\n");
1396 //-----------------------------------------------------------------------------
1397 int AUTH_IA::Process_DISCONN_SYN_7(DISCONN_SYN_7 * disconnSyn, IA_USER * iaUser, user_iter * user, uint32_t sip)
1399 return Process_DISCONN_SYN_6(disconnSyn, iaUser, user, sip);
1401 //-----------------------------------------------------------------------------
1402 int AUTH_IA::Process_DISCONN_SYN_8(DISCONN_SYN_8 *, IA_USER * iaUser, user_iter *, uint32_t)
1404 if (iaUser->phase.GetPhase() != 3)
1406 errorStr = "Incorrect request DISCONN_SYN";
1407 printfd(__FILE__, "Invalid phase. Expected 3, actual %d\n", iaUser->phase.GetPhase());
1411 iaUser->phase.SetPhase4();
1412 printfd(__FILE__, "Phase changed from 3 to 4. Reason: DISCONN_SYN_6\n");
1416 //-----------------------------------------------------------------------------
1417 int AUTH_IA::Process_DISCONN_ACK_6(DISCONN_ACK_6 * disconnAck,
1421 map<uint32_t, IA_USER>::iterator)
1424 SwapBytes(disconnAck->len);
1425 SwapBytes(disconnAck->rnd);
1427 printfd(__FILE__, "DISCONN_ACK_6\n");
1428 if (!((iaUser->phase.GetPhase() == 4) && (disconnAck->rnd == iaUser->rnd + 1)))
1430 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), disconnAck->rnd);
1436 //-----------------------------------------------------------------------------
1437 int AUTH_IA::Process_DISCONN_ACK_7(DISCONN_ACK_7 * disconnAck, IA_USER * iaUser, user_iter * user, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1439 return Process_DISCONN_ACK_6(disconnAck, iaUser, user, sip, it);
1441 //-----------------------------------------------------------------------------
1442 int AUTH_IA::Process_DISCONN_ACK_8(DISCONN_ACK_8 * disconnAck, IA_USER * iaUser, user_iter *, uint32_t, map<uint32_t, IA_USER>::iterator)
1445 SwapBytes(disconnAck->len);
1446 SwapBytes(disconnAck->rnd);
1448 printfd(__FILE__, "DISCONN_ACK_8\n");
1449 if (!((iaUser->phase.GetPhase() == 4) && (disconnAck->rnd == iaUser->rnd + 1)))
1451 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), disconnAck->rnd);
1457 //-----------------------------------------------------------------------------
1458 int AUTH_IA::Send_CONN_SYN_ACK_6(IA_USER * iaUser, user_iter *, uint32_t sip)
1460 //+++ Fill static data in connSynAck +++
1461 // TODO Move this code. It must be executed only once
1462 connSynAck6.len = Min8(sizeof(CONN_SYN_ACK_6));
1463 strcpy((char*)connSynAck6.type, "CONN_SYN_ACK");
1464 for (int j = 0; j < DIR_NUM; j++)
1466 strncpy((char*)connSynAck6.dirName[j],
1467 stgSettings->GetDirName(j).c_str(),
1470 connSynAck6.dirName[j][sizeof(string16) - 1] = 0;
1472 //--- Fill static data in connSynAck ---
1474 iaUser->rnd = random();
1475 connSynAck6.rnd = iaUser->rnd;
1477 connSynAck6.userTimeOut = iaSettings.GetUserTimeout();
1478 connSynAck6.aliveDelay = iaSettings.GetUserDelay();
1481 SwapBytes(connSynAck6.len);
1482 SwapBytes(connSynAck6.rnd);
1483 SwapBytes(connSynAck6.userTimeOut);
1484 SwapBytes(connSynAck6.aliveDelay);
1487 Encrypt(&iaUser->ctx, (char*)&connSynAck6, (char*)&connSynAck6, Min8(sizeof(CONN_SYN_ACK_6))/8);
1488 return Send(sip, iaSettings.GetUserPort(), (char*)&connSynAck6, Min8(sizeof(CONN_SYN_ACK_6)));;
1490 //-----------------------------------------------------------------------------
1491 int AUTH_IA::Send_CONN_SYN_ACK_7(IA_USER * iaUser, user_iter * user, uint32_t sip)
1493 return Send_CONN_SYN_ACK_6(iaUser, user, sip);
1495 //-----------------------------------------------------------------------------
1496 int AUTH_IA::Send_CONN_SYN_ACK_8(IA_USER * iaUser, user_iter *, uint32_t sip)
1498 strcpy((char*)connSynAck8.hdr.magic, IA_ID);
1499 connSynAck8.hdr.protoVer[0] = 0;
1500 connSynAck8.hdr.protoVer[1] = 8;
1502 //+++ Fill static data in connSynAck +++
1503 // TODO Move this code. It must be executed only once
1504 connSynAck8.len = Min8(sizeof(CONN_SYN_ACK_8));
1505 strcpy((char*)connSynAck8.type, "CONN_SYN_ACK");
1506 for (int j = 0; j < DIR_NUM; j++)
1508 strncpy((char*)connSynAck8.dirName[j],
1509 stgSettings->GetDirName(j).c_str(),
1512 connSynAck8.dirName[j][sizeof(string16) - 1] = 0;
1514 //--- Fill static data in connSynAck ---
1516 iaUser->rnd = random();
1517 connSynAck8.rnd = iaUser->rnd;
1519 connSynAck8.userTimeOut = iaSettings.GetUserTimeout();
1520 connSynAck8.aliveDelay = iaSettings.GetUserDelay();
1523 SwapBytes(connSynAck8.len);
1524 SwapBytes(connSynAck8.rnd);
1525 SwapBytes(connSynAck8.userTimeOut);
1526 SwapBytes(connSynAck8.aliveDelay);
1529 Encrypt(&iaUser->ctx, (char*)&connSynAck8, (char*)&connSynAck8, Min8(sizeof(CONN_SYN_ACK_8))/8);
1530 return Send(sip, iaUser->port, (char*)&connSynAck8, Min8(sizeof(CONN_SYN_ACK_8)));
1531 //return Send(sip, iaUser->port, (char*)&connSynAck8, 384);
1533 //-----------------------------------------------------------------------------
1534 int AUTH_IA::Send_ALIVE_SYN_6(IA_USER * iaUser, uint32_t sip)
1536 aliveSyn6.len = Min8(sizeof(ALIVE_SYN_6));
1537 aliveSyn6.rnd = iaUser->rnd = random();
1539 strcpy((char*)aliveSyn6.type, "ALIVE_SYN");
1541 for (int i = 0; i < DIR_NUM; i++)
1543 aliveSyn6.md[i] = iaUser->user->property.down.Get()[i];
1544 aliveSyn6.mu[i] = iaUser->user->property.up.Get()[i];
1546 aliveSyn6.sd[i] = iaUser->user->GetSessionDownload()[i];
1547 aliveSyn6.su[i] = iaUser->user->GetSessionUpload()[i];
1551 int dn = iaSettings.GetFreeMbShowType();
1552 const TARIFF * tf = iaUser->user->GetTariff();
1556 double p = tf->GetPriceWithTraffType(aliveSyn6.mu[dn],
1563 snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "---");
1567 double fmb = iaUser->user->property.freeMb;
1568 fmb = fmb < 0 ? 0 : fmb;
1569 snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "%.3f", fmb / p);
1574 if (freeMbNone == iaSettings.GetFreeMbShowType())
1576 aliveSyn6.freeMb[0] = 0;
1580 double fmb = iaUser->user->property.freeMb;
1581 fmb = fmb < 0 ? 0 : fmb;
1582 snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "C%.3f", fmb);
1587 if (iaUser->aliveSent)
1589 printfd(__FILE__, "========= ALIVE_ACK_6(7) TIMEOUT !!! %s =========\n", iaUser->user->GetLogin().c_str());
1591 iaUser->aliveSent = true;
1594 aliveSyn6.cash =(int64_t) (iaUser->user->property.cash.Get() * 1000.0);
1595 if (!stgSettings->GetShowFeeInCash())
1596 aliveSyn6.cash -= (int64_t)(tf->GetFee() * 1000.0);
1599 SwapBytes(aliveSyn6.len);
1600 SwapBytes(aliveSyn6.rnd);
1601 SwapBytes(aliveSyn6.cash);
1602 for (int i = 0; i < DIR_NUM; ++i)
1604 SwapBytes(aliveSyn6.mu[i]);
1605 SwapBytes(aliveSyn6.md[i]);
1606 SwapBytes(aliveSyn6.su[i]);
1607 SwapBytes(aliveSyn6.sd[i]);
1611 Encrypt(&(iaUser->ctx), (char*)&aliveSyn6, (char*)&aliveSyn6, Min8(sizeof(aliveSyn6))/8);
1612 return Send(sip, iaSettings.GetUserPort(), (char*)&aliveSyn6, Min8(sizeof(aliveSyn6)));
1614 //-----------------------------------------------------------------------------
1615 int AUTH_IA::Send_ALIVE_SYN_7(IA_USER * iaUser, uint32_t sip)
1617 return Send_ALIVE_SYN_6(iaUser, sip);
1619 //-----------------------------------------------------------------------------
1620 int AUTH_IA::Send_ALIVE_SYN_8(IA_USER * iaUser, uint32_t sip)
1622 strcpy((char*)aliveSyn8.hdr.magic, IA_ID);
1623 aliveSyn8.hdr.protoVer[0] = 0;
1624 aliveSyn8.hdr.protoVer[1] = 8;
1626 aliveSyn8.len = Min8(sizeof(ALIVE_SYN_8));
1627 aliveSyn8.rnd = iaUser->rnd = random();
1629 strcpy((char*)aliveSyn8.type, "ALIVE_SYN");
1631 for (int i = 0; i < DIR_NUM; i++)
1633 aliveSyn8.md[i] = iaUser->user->property.down.Get()[i];
1634 aliveSyn8.mu[i] = iaUser->user->property.up.Get()[i];
1636 aliveSyn8.sd[i] = iaUser->user->GetSessionDownload()[i];
1637 aliveSyn8.su[i] = iaUser->user->GetSessionUpload()[i];
1641 int dn = iaSettings.GetFreeMbShowType();
1645 const TARIFF * tf = iaUser->user->GetTariff();
1646 double p = tf->GetPriceWithTraffType(aliveSyn8.mu[dn],
1653 snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "---");
1657 double fmb = iaUser->user->property.freeMb;
1658 fmb = fmb < 0 ? 0 : fmb;
1659 snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "%.3f", fmb / p);
1664 if (freeMbNone == iaSettings.GetFreeMbShowType())
1666 aliveSyn8.freeMb[0] = 0;
1670 double fmb = iaUser->user->property.freeMb;
1671 fmb = fmb < 0 ? 0 : fmb;
1672 snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "C%.3f", fmb);
1677 if (iaUser->aliveSent)
1679 printfd(__FILE__, "========= ALIVE_ACK_8 TIMEOUT !!! =========\n");
1681 iaUser->aliveSent = true;
1684 const TARIFF * tf = iaUser->user->GetTariff();
1686 aliveSyn8.cash =(int64_t) (iaUser->user->property.cash.Get() * 1000.0);
1687 if (!stgSettings->GetShowFeeInCash())
1688 aliveSyn8.cash -= (int64_t)(tf->GetFee() * 1000.0);
1691 SwapBytes(aliveSyn8.len);
1692 SwapBytes(aliveSyn8.rnd);
1693 SwapBytes(aliveSyn8.cash);
1694 SwapBytes(aliveSyn8.status);
1695 for (int i = 0; i < DIR_NUM; ++i)
1697 SwapBytes(aliveSyn8.mu[i]);
1698 SwapBytes(aliveSyn8.md[i]);
1699 SwapBytes(aliveSyn8.su[i]);
1700 SwapBytes(aliveSyn8.sd[i]);
1704 Encrypt(&(iaUser->ctx), (char*)&aliveSyn8, (char*)&aliveSyn8, Min8(sizeof(aliveSyn8))/8);
1705 return Send(sip, iaUser->port, (char*)&aliveSyn8, Min8(sizeof(aliveSyn8)));
1707 //-----------------------------------------------------------------------------
1708 int AUTH_IA::Send_DISCONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip)
1710 disconnSynAck6.len = Min8(sizeof(DISCONN_SYN_ACK_6));
1711 strcpy((char*)disconnSynAck6.type, "DISCONN_SYN_ACK");
1712 disconnSynAck6.rnd = iaUser->rnd = random();
1715 SwapBytes(disconnSynAck6.len);
1716 SwapBytes(disconnSynAck6.rnd);
1719 Encrypt(&iaUser->ctx, (char*)&disconnSynAck6, (char*)&disconnSynAck6, Min8(sizeof(disconnSynAck6))/8);
1720 return Send(sip, iaSettings.GetUserPort(), (char*)&disconnSynAck6, Min8(sizeof(disconnSynAck6)));
1722 //-----------------------------------------------------------------------------
1723 int AUTH_IA::Send_DISCONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip)
1725 return Send_DISCONN_SYN_ACK_6(iaUser, sip);
1727 //-----------------------------------------------------------------------------
1728 int AUTH_IA::Send_DISCONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip)
1730 strcpy((char*)disconnSynAck8.hdr.magic, IA_ID);
1731 disconnSynAck8.hdr.protoVer[0] = 0;
1732 disconnSynAck8.hdr.protoVer[1] = 8;
1734 disconnSynAck8.len = Min8(sizeof(DISCONN_SYN_ACK_8));
1735 strcpy((char*)disconnSynAck8.type, "DISCONN_SYN_ACK");
1736 disconnSynAck8.rnd = iaUser->rnd = random();
1739 SwapBytes(disconnSynAck8.len);
1740 SwapBytes(disconnSynAck8.rnd);
1743 Encrypt(&iaUser->ctx, (char*)&disconnSynAck8, (char*)&disconnSynAck8, Min8(sizeof(disconnSynAck8))/8);
1744 return Send(sip, iaUser->port, (char*)&disconnSynAck8, Min8(sizeof(disconnSynAck8)));
1746 //-----------------------------------------------------------------------------
1747 int AUTH_IA::Send_FIN_6(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1749 fin6.len = Min8(sizeof(FIN_6));
1750 strcpy((char*)fin6.type, "FIN");
1751 strcpy((char*)fin6.ok, "OK");
1754 SwapBytes(fin6.len);
1757 Encrypt(&iaUser->ctx, (char*)&fin6, (char*)&fin6, Min8(sizeof(fin6))/8);
1759 iaUser->user->Unauthorize(this);
1761 int ret = Send(sip, iaSettings.GetUserPort(), (char*)&fin6, Min8(sizeof(fin6)));
1765 //-----------------------------------------------------------------------------
1766 int AUTH_IA::Send_FIN_7(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1768 return Send_FIN_6(iaUser, sip, it);
1770 //-----------------------------------------------------------------------------
1771 int AUTH_IA::Send_FIN_8(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1773 strcpy((char*)fin8.hdr.magic, IA_ID);
1774 fin8.hdr.protoVer[0] = 0;
1775 fin8.hdr.protoVer[1] = 8;
1777 fin8.len = Min8(sizeof(FIN_8));
1778 strcpy((char*)fin8.type, "FIN");
1779 strcpy((char*)fin8.ok, "OK");
1782 SwapBytes(fin8.len);
1785 Encrypt(&iaUser->ctx, (char*)&fin8, (char*)&fin8, Min8(sizeof(fin8))/8);
1787 iaUser->user->Unauthorize(this);
1789 int ret = Send(sip, iaUser->port, (char*)&fin8, Min8(sizeof(fin8)));
1793 //-----------------------------------------------------------------------------
1794 bool AUTH_IA::WaitPackets(int sd) const
1802 tv.tv_usec = 500000;
1804 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
1805 if (res == -1) // Error
1809 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
1814 if (res == 0) // Timeout
1821 //-----------------------------------------------------------------------------
1823 void InitEncrypt(BLOWFISH_CTX * ctx, const string & password)
1825 unsigned char keyL[PASSWD_LEN]; // ðÁÒÏÌØ ÄÌÑ ÛÉÆÒÏ×ËÉ
1826 memset(keyL, 0, PASSWD_LEN);
1827 strncpy((char *)keyL, password.c_str(), PASSWD_LEN);
1828 Blowfish_Init(ctx, keyL, PASSWD_LEN);
1830 //-----------------------------------------------------------------------------
1832 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
1834 // len8 - ÄÌÉÎÁ × 8-ÍÉ ÂÁÊÔÏ×ÙÈ ÂÌÏËÁÈ
1836 for (int i = 0; i < len8; i++)
1837 DecodeString(dst + i * 8, src + i * 8, ctx);
1839 //-----------------------------------------------------------------------------
1841 void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
1843 // len8 - ÄÌÉÎÁ × 8-ÍÉ ÂÁÊÔÏ×ÙÈ ÂÌÏËÁÈ
1845 for (int i = 0; i < len8; i++)
1846 EncodeString(dst + i * 8, src + i * 8, ctx);