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 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
70 BASE_PLUGIN * GetPlugin()
72 return iac.GetPlugin();
74 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
77 AUTH_IA_SETTINGS::AUTH_IA_SETTINGS()
81 freeMbShowType(freeMbCash)
84 //-----------------------------------------------------------------------------
85 int AUTH_IA_SETTINGS::ParseIntInRange(const string & str, int min, int max, int * val)
87 if (str2x(str.c_str(), *val))
89 errorStr = "Incorrect value \'" + str + "\'.";
92 if (*val < min || *val > max)
94 errorStr = "Value \'" + str + "\' out of range.";
99 //-----------------------------------------------------------------------------
100 int AUTH_IA_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
104 vector<PARAM_VALUE>::const_iterator pvi;
105 ///////////////////////////
107 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
108 if (pvi == s.moduleParams.end())
110 errorStr = "Parameter \'Port\' not found.";
111 printfd(__FILE__, "Parameter 'Port' not found\n");
114 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
116 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
117 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
121 ///////////////////////////
122 pv.param = "UserDelay";
123 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
124 if (pvi == s.moduleParams.end())
126 errorStr = "Parameter \'UserDelay\' not found.";
127 printfd(__FILE__, "Parameter 'UserDelay' not found\n");
131 if (ParseIntInRange(pvi->value[0], 5, 600, &userDelay))
133 errorStr = "Cannot parse parameter \'UserDelay\': " + errorStr;
134 printfd(__FILE__, "Cannot parse parameter 'UserDelay'\n");
137 ///////////////////////////
138 pv.param = "UserTimeout";
139 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
140 if (pvi == s.moduleParams.end())
142 errorStr = "Parameter \'UserTimeout\' not found.";
143 printfd(__FILE__, "Parameter 'UserTimeout' not found\n");
147 if (ParseIntInRange(pvi->value[0], 15, 1200, &userTimeout))
149 errorStr = "Cannot parse parameter \'UserTimeout\': " + errorStr;
150 printfd(__FILE__, "Cannot parse parameter 'UserTimeout'\n");
153 /////////////////////////////////////////////////////////////
157 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
158 if (pvi == s.moduleParams.end())
160 errorStr = "Parameter \'FreeMb\' not found.";
161 printfd(__FILE__, "Parameter 'FreeMb' not found\n");
164 freeMbType = pvi->value[0];
166 if (strcasecmp(freeMbType.c_str(), "cash") == 0)
168 freeMbShowType = freeMbCash;
170 else if (strcasecmp(freeMbType.c_str(), "none") == 0)
172 freeMbShowType = freeMbNone;
174 else if (!str2x(freeMbType.c_str(), n))
176 if (n < 0 || n >= DIR_NUM)
178 errorStr = "Incorrect parameter \'" + freeMbType + "\'.";
179 printfd(__FILE__, "%s\n", errorStr.c_str());
182 freeMbShowType = (FREEMB)(freeMb0 + n);
186 errorStr = "Incorrect parameter \'" + freeMbType + "\'.";
187 printfd(__FILE__, "%s\n", errorStr.c_str());
190 /////////////////////////////////////////////////////////////
193 //-----------------------------------------------------------------------------
194 //-----------------------------------------------------------------------------
195 //-----------------------------------------------------------------------------
196 #ifdef IA_PHASE_DEBUG
201 gettimeofday(&phaseTime, NULL);
207 gettimeofday(&phaseTime, NULL);
210 //-----------------------------------------------------------------------------
211 IA_PHASE::~IA_PHASE()
213 #ifdef IA_PHASE_DEBUG
214 flog = fopen(log.c_str(), "at");
217 fprintf(flog, "IA %s D\n", login.c_str());
222 //-----------------------------------------------------------------------------
223 #ifdef IA_PHASE_DEBUG
224 void IA_PHASE::SetLogFileName(const string & logFileName)
226 log = logFileName + ".ia.log";
228 //-----------------------------------------------------------------------------
229 void IA_PHASE::SetUserLogin(const string & login)
231 IA_PHASE::login = login;
233 //-----------------------------------------------------------------------------
234 void IA_PHASE::WritePhaseChange(int newPhase)
237 gettimeofday(&newPhaseTime, NULL);
238 flog = fopen(log.c_str(), "at");
239 /*int64_t tn = newPhaseTime.GetSec()*1000000 + newPhaseTime.GetUSec();
240 int64_t to = phaseTime.GetSec()*1000000 + phaseTime.GetUSec();*/
243 string action = newPhase == phase ? "U" : "C";
244 double delta = newPhaseTime.GetSec() - phaseTime.GetSec();
245 delta += (newPhaseTime.GetUSec() - phaseTime.GetUSec()) * 1.0e-6;
246 fprintf(flog, "IA %s %s oldPhase = %d, newPhase = %d. dt = %.6f\n",
256 //-----------------------------------------------------------------------------
257 void IA_PHASE::SetPhase1()
259 #ifdef IA_PHASE_DEBUG
263 gettimeofday(&phaseTime, NULL);
265 //-----------------------------------------------------------------------------
266 void IA_PHASE::SetPhase2()
268 #ifdef IA_PHASE_DEBUG
272 gettimeofday(&phaseTime, NULL);
274 //-----------------------------------------------------------------------------
275 void IA_PHASE::SetPhase3()
277 #ifdef IA_PHASE_DEBUG
281 gettimeofday(&phaseTime, NULL);
283 //-----------------------------------------------------------------------------
284 void IA_PHASE::SetPhase4()
286 #ifdef IA_PHASE_DEBUG
290 gettimeofday(&phaseTime, NULL);
292 //-----------------------------------------------------------------------------
293 void IA_PHASE::SetPhase5()
295 #ifdef IA_PHASE_DEBUG
299 gettimeofday(&phaseTime, NULL);
301 //-----------------------------------------------------------------------------
302 int IA_PHASE::GetPhase() const
306 //-----------------------------------------------------------------------------
307 void IA_PHASE::UpdateTime()
309 #ifdef IA_PHASE_DEBUG
310 WritePhaseChange(phase);
312 gettimeofday(&phaseTime, NULL);
314 //-----------------------------------------------------------------------------
315 const UTIME & IA_PHASE::GetTime() const
319 //-----------------------------------------------------------------------------
320 //-----------------------------------------------------------------------------
321 //-----------------------------------------------------------------------------
325 isRunningRunTimeouter(false),
326 WriteServLog(GetStgLogger()),
327 enabledDirs(0xFFffFFff),
328 onDelUserNotifier(*this)
330 InitEncrypt(&ctxS, "pr7Hhen");
332 pthread_mutexattr_t attr;
333 pthread_mutexattr_init(&attr);
334 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
335 pthread_mutex_init(&mutex, &attr);
337 memset(&connSynAck6, 0, sizeof(CONN_SYN_ACK_6));
338 memset(&connSynAck8, 0, sizeof(CONN_SYN_ACK_8));
339 memset(&disconnSynAck6, 0, sizeof(DISCONN_SYN_ACK_6));
340 memset(&disconnSynAck8, 0, sizeof(DISCONN_SYN_ACK_8));
341 memset(&aliveSyn6, 0, sizeof(ALIVE_SYN_6));
342 memset(&aliveSyn8, 0, sizeof(ALIVE_SYN_8));
343 memset(&fin6, 0, sizeof(FIN_6));
344 memset(&fin8, 0, sizeof(FIN_8));
346 printfd(__FILE__, "sizeof(CONN_SYN_6) = %d %d\n", sizeof(CONN_SYN_6), Min8(sizeof(CONN_SYN_6)));
347 printfd(__FILE__, "sizeof(CONN_SYN_8) = %d %d\n", sizeof(CONN_SYN_8), Min8(sizeof(CONN_SYN_8)));
348 printfd(__FILE__, "sizeof(CONN_SYN_ACK_6) = %d %d\n", sizeof(CONN_SYN_ACK_6), Min8(sizeof(CONN_SYN_ACK_6)));
349 printfd(__FILE__, "sizeof(CONN_SYN_ACK_8) = %d %d\n", sizeof(CONN_SYN_ACK_8), Min8(sizeof(CONN_SYN_ACK_8)));
350 printfd(__FILE__, "sizeof(CONN_ACK_6) = %d %d\n", sizeof(CONN_ACK_6), Min8(sizeof(CONN_ACK_6)));
351 printfd(__FILE__, "sizeof(ALIVE_SYN_6) = %d %d\n", sizeof(ALIVE_SYN_6), Min8(sizeof(ALIVE_SYN_6)));
352 printfd(__FILE__, "sizeof(ALIVE_SYN_8) = %d %d\n", sizeof(ALIVE_SYN_8), Min8(sizeof(ALIVE_SYN_8)));
353 printfd(__FILE__, "sizeof(ALIVE_ACK_6) = %d %d\n", sizeof(ALIVE_ACK_6), Min8(sizeof(ALIVE_ACK_6)));
354 printfd(__FILE__, "sizeof(DISCONN_SYN_6) = %d %d\n", sizeof(DISCONN_SYN_6), Min8(sizeof(DISCONN_SYN_6)));
355 printfd(__FILE__, "sizeof(DISCONN_SYN_ACK_6) = %d %d\n", sizeof(DISCONN_SYN_ACK_6), Min8(sizeof(DISCONN_SYN_ACK_6)));
356 printfd(__FILE__, "sizeof(DISCONN_SYN_ACK_8) = %d %d\n", sizeof(DISCONN_SYN_ACK_8), Min8(sizeof(DISCONN_SYN_ACK_8)));
357 printfd(__FILE__, "sizeof(DISCONN_ACK_6) = %d %d\n", sizeof(DISCONN_ACK_6), Min8(sizeof(DISCONN_ACK_6)));
358 printfd(__FILE__, "sizeof(FIN_6) = %d %d\n", sizeof(FIN_6), Min8(sizeof(FIN_6)));
359 printfd(__FILE__, "sizeof(FIN_8) = %d %d\n", sizeof(FIN_8), Min8(sizeof(FIN_8)));
360 printfd(__FILE__, "sizeof(ERR) = %d %d\n", sizeof(ERR), Min8(sizeof(ERR)));
361 printfd(__FILE__, "sizeof(INFO_6) = %d %d\n", sizeof(INFO_6), Min8(sizeof(INFO_6)));
362 printfd(__FILE__, "sizeof(INFO_7) = %d %d\n", sizeof(INFO_7), Min8(sizeof(INFO_7)));
363 printfd(__FILE__, "sizeof(INFO_8) = %d %d\n", sizeof(INFO_8), Min8(sizeof(INFO_8)));
365 packetTypes["CONN_SYN"] = CONN_SYN_N;
366 packetTypes["CONN_SYN_ACK"] = CONN_SYN_ACK_N;
367 packetTypes["CONN_ACK"] = CONN_ACK_N;
368 packetTypes["ALIVE_SYN"] = ALIVE_SYN_N;
369 packetTypes["ALIVE_ACK"] = ALIVE_ACK_N;
370 packetTypes["DISCONN_SYN"] = DISCONN_SYN_N;
371 packetTypes["DISCONN_SYN_ACK"] = DISCONN_SYN_ACK_N;
372 packetTypes["DISCONN_ACK"] = DISCONN_ACK_N;
373 packetTypes["FIN"] = FIN_N;
374 packetTypes["ERR"] = ERROR_N;
376 //-----------------------------------------------------------------------------
379 pthread_mutex_destroy(&mutex);
381 //-----------------------------------------------------------------------------
384 users->AddNotifierUserDel(&onDelUserNotifier);
394 if (pthread_create(&recvThread, NULL, Run, this))
396 errorStr = "Cannot create thread.";
397 printfd(__FILE__, "Cannot create recv thread\n");
402 if (!isRunningRunTimeouter)
404 if (pthread_create(&timeouterThread, NULL, RunTimeouter, this))
406 errorStr = "Cannot create thread.";
407 printfd(__FILE__, "Cannot create timeouter thread\n");
414 //-----------------------------------------------------------------------------
425 UnauthorizeUser(this)
430 //5 seconds to thread stops itself
431 for (int i = 0; i < 25 && isRunningRun; i++)
436 //after 5 seconds waiting thread still running. now killing it
439 //TODO pthread_cancel()
440 if (pthread_kill(recvThread, SIGINT))
442 errorStr = "Cannot kill thread.";
443 printfd(__FILE__, "Cannot kill thread\n");
446 for (int i = 0; i < 25 && isRunningRun; ++i)
450 printfd(__FILE__, "Failed to stop recv thread\n");
454 pthread_join(recvThread, NULL);
456 printfd(__FILE__, "AUTH_IA killed Run\n");
462 if (isRunningRunTimeouter)
464 //5 seconds to thread stops itself
465 for (int i = 0; i < 25 && isRunningRunTimeouter; i++)
470 //after 5 seconds waiting thread still running. now killing it
471 if (isRunningRunTimeouter)
473 //TODO pthread_cancel()
474 if (pthread_kill(timeouterThread, SIGINT))
476 errorStr = "Cannot kill thread.";
479 for (int i = 0; i < 25 && isRunningRunTimeouter; ++i)
481 if (isRunningRunTimeouter)
483 printfd(__FILE__, "Failed to stop timeouter thread\n");
487 pthread_join(timeouterThread, NULL);
489 printfd(__FILE__, "AUTH_IA killed Timeouter\n");
492 printfd(__FILE__, "AUTH_IA::Stoped successfully.\n");
493 users->DelNotifierUserDel(&onDelUserNotifier);
496 //-----------------------------------------------------------------------------
497 void * AUTH_IA::Run(void * d)
499 AUTH_IA * ia = static_cast<AUTH_IA *>(d);
501 ia->isRunningRun = true;
505 time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
509 ia->RecvData(buffer, sizeof(buffer));
510 if ((touchTime + MONITOR_TIME_DELAY_SEC <= stgTime) && ia->stgSettings->GetMonitoring())
513 string monFile = ia->stgSettings->GetMonitorDir() + "/inetaccess_r";
514 TouchFile(monFile.c_str());
518 ia->isRunningRun = false;
521 //-----------------------------------------------------------------------------
522 void * AUTH_IA::RunTimeouter(void * d)
524 AUTH_IA * ia = static_cast<AUTH_IA *>(d);
526 ia->isRunningRunTimeouter = true;
529 string monFile = ia->stgSettings->GetMonitorDir() + "/inetaccess_t";
534 // TODO cahange counter to timer and MONITOR_TIME_DELAY_SEC
535 if (++a % (50*60) == 0 && ia->stgSettings->GetMonitoring())
537 TouchFile(monFile.c_str());
541 ia->isRunningRunTimeouter = false;
544 //-----------------------------------------------------------------------------
545 int AUTH_IA::ParseSettings()
547 int ret = iaSettings.ParseSettings(settings);
549 errorStr = iaSettings.GetStrError();
552 //-----------------------------------------------------------------------------
553 int AUTH_IA::PrepareNet()
555 struct sockaddr_in listenAddr;
557 listenSocket = socket(AF_INET, SOCK_DGRAM, 0);
559 if (listenSocket < 0)
561 errorStr = "Cannot create socket.";
565 listenAddr.sin_family = AF_INET;
566 listenAddr.sin_port = htons(iaSettings.GetUserPort());
567 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
569 if (bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr)) < 0)
571 errorStr = "AUTH_IA: Bind failed.";
576 if (getsockopt(listenSocket, SOL_SOCKET, SO_SNDBUF, &buffLen, sizeof(buffLen)) < 0)
579 errorStr = "Getsockopt failed. " + string(strerror(errno));
583 //WriteServLog("buffLen = %d", buffLen);
587 //-----------------------------------------------------------------------------
588 int AUTH_IA::FinalizeNet()
593 //-----------------------------------------------------------------------------
594 int AUTH_IA::RecvData(char * buffer, int bufferSize)
596 if (!WaitPackets(listenSocket)) // Timeout
601 struct sockaddr_in outerAddr;
602 socklen_t outerAddrLen(sizeof(outerAddr));
603 int dataLen = recvfrom(listenSocket, buffer, bufferSize, 0, (struct sockaddr *)&outerAddr, &outerAddrLen);
610 if (dataLen <= 0) // Error
614 printfd(__FILE__, "recvfrom res=%d, error: '%s'\n", dataLen, strerror(errno));
624 if (CheckHeader(buffer, &protoVer))
627 char login[PASSWD_LEN]; //TODO why PASSWD_LEN ?
628 memset(login, 0, PASSWD_LEN);
630 Decrypt(&ctxS, login, buffer + 8, PASSWD_LEN / 8);
632 uint32_t sip = *((uint32_t*)&outerAddr.sin_addr);
633 uint16_t sport = htons(outerAddr.sin_port);
636 if (users->FindByName(login, &user) == 0)
638 printfd(__FILE__, "User %s FOUND!\n", user->GetLogin().c_str());
639 PacketProcessor(buffer, dataLen, sip, sport, protoVer, &user);
643 WriteServLog("User\'s connect failed:: user \'%s\' not found. IP \'%s\'",
645 inet_ntostring(sip).c_str());
646 printfd(__FILE__, "User %s NOT found!\n", login);
647 SendError(sip, sport, protoVer, "îÅÐÒÁ×ÉÌØÎÙÊ ÌÏÇÉÎ ÉÌÉ ÐÁÒÏÌØ!");
653 //-----------------------------------------------------------------------------
654 int AUTH_IA::CheckHeader(const char * buffer, int * protoVer)
656 if (strncmp(IA_ID, buffer, strlen(IA_ID)) != 0)
658 //SendError(userIP, updateMsg);
659 printfd(__FILE__, "update needed - IA_ID\n");
660 //SendError(userIP, "Incorrect header!");
664 if (buffer[6] != 0) //proto[0] shoud be 0
666 printfd(__FILE__, "update needed - PROTO major: %d\n", buffer[6]);
667 //SendError(userIP, updateMsg);
674 //SendError(userIP, updateMsg);
675 printfd(__FILE__, "update needed - PROTO minor: %d\n", buffer[7]);
680 *protoVer = buffer[7];
684 //-----------------------------------------------------------------------------
685 int AUTH_IA::Timeouter()
687 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
689 map<uint32_t, IA_USER>::iterator it;
690 it = ip2user.begin();
693 while (it != ip2user.end())
697 static UTIME currTime;
698 gettimeofday(&currTime, NULL);
700 if ((it->second.phase.GetPhase() == 2)
701 && (currTime - it->second.phase.GetTime()) > iaSettings.GetUserDelay())
703 it->second.phase.SetPhase1();
704 printfd(__FILE__, "Phase changed from 2 to 1. Reason: timeout\n");
707 if (it->second.phase.GetPhase() == 3)
709 if (!it->second.messagesToSend.empty())
711 if (it->second.protoVer == 6)
712 RealSendMessage6(*it->second.messagesToSend.begin(), sip, it->second);
714 if (it->second.protoVer == 7)
715 RealSendMessage7(*it->second.messagesToSend.begin(), sip, it->second);
717 if (it->second.protoVer == 8)
718 RealSendMessage8(*it->second.messagesToSend.begin(), sip, it->second);
720 it->second.messagesToSend.erase(it->second.messagesToSend.begin());
723 if((currTime - it->second.lastSendAlive) > iaSettings.GetUserDelay())
725 switch (it->second.protoVer)
728 Send_ALIVE_SYN_6(&(it->second), sip);
731 Send_ALIVE_SYN_7(&(it->second), sip);
734 Send_ALIVE_SYN_8(&(it->second), sip);
738 gettimeofday(&it->second.lastSendAlive, NULL);
741 if ((currTime - it->second.phase.GetTime()) > iaSettings.GetUserTimeout())
743 it->second.user->Unauthorize(this);
749 if ((it->second.phase.GetPhase() == 4)
750 && ((currTime - it->second.phase.GetTime()) > iaSettings.GetUserDelay()))
752 it->second.phase.SetPhase3();
753 printfd(__FILE__, "Phase changed from 4 to 3. Reason: timeout\n");
761 //-----------------------------------------------------------------------------
762 int AUTH_IA::PacketProcessor(char * buff, int dataLen, uint32_t sip, uint16_t sport, int protoVer, user_iter * user)
764 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
765 // ôÕÔ ÓÏÂÒÁÎÙ ÏÂÒÁÂÏÔÞÉËÉ ÒÁÚÎÙÈ ÐÁËÅÔÏ×
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)->property.password.Get())
812 InitEncrypt(&iaUser->ctx, (*user)->property.password.Get());
813 iaUser->password = (*user)->property.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 packetLen = *(int*)buff;
839 if ((*user)->property.disabled.Get())
841 SendError(sip, sport, protoVer, "õÞÅÔÎÁÑ ÚÁÐÉÓØ ÚÁÂÌÏËÉÒÏ×ÁÎÁ");
845 if ((*user)->property.passive.Get())
847 SendError(sip, sport, protoVer, "õÞÅÔÎÁÑ ÚÁÐÉÓØ ÚÁÍÏÒÏÖÅÎÁ");
851 if ((*user)->GetAuthorized() && (*user)->GetCurrIP() != sip)
853 printfd(__FILE__, "Login %s alredy in use. IP \'%s\'\n", (*user)->GetLogin().c_str(), inet_ntostring(sip).c_str());
854 WriteServLog("Login %s alredy in use. IP \'%s\'", (*user)->GetLogin().c_str(), inet_ntostring(sip).c_str());
855 SendError(sip, sport, protoVer, "÷ÁÛ ÌÏÇÉÎ ÕÖÅ ÉÓÐÏÌØÚÕÅÔÓÑ!");
860 if (users->FindByIPIdx(sip, &u) == 0 && u->GetLogin() != (*user)->GetLogin())
862 printfd(__FILE__, "IP address alredy in use. IP \'%s\'", inet_ntostring(sip).c_str());
863 WriteServLog("IP address alredy in use. IP \'%s\'", inet_ntostring(sip).c_str());
864 SendError(sip, sport, protoVer, "÷ÁÛ IP ÁÄÒÅÓ ÕÖÅ ÉÓÐÏÌØÚÕÅÔÓÑ!");
868 // ôÅÐÅÒØ ÍÙ ÄÏÌÖÎÙ ÐÒÏ×ÅÒÉÔØ, ÍÏÖÅÔ ÌÉ ÐÏÌØÚÏ×ÁÔÅÌØ ÐÏÄËÌÀÞÉÔÓÑ Ó ÜÔÏÇÏ ÁÄÒÅÓÁ.
869 int ipFound = (*user)->property.ips.Get().IsIPInIPS(sip);
872 printfd(__FILE__, "User %s. IP address is incorrect. IP \'%s\'\n", (*user)->GetLogin().c_str(), inet_ntostring(sip).c_str());
873 WriteServLog("User %s. IP address is incorrect. IP \'%s\'", (*user)->GetLogin().c_str(), inet_ntostring(sip).c_str());
874 SendError(sip, sport, protoVer, "ðÏÌØÚÏ×ÁÔÅÌØ ÎÅ ÏÐÏÚÎÁÎ! ðÒÏ×ÅÒØÔÅ IP ÁÄÒÅÓ.");
886 connSyn6 = (CONN_SYN_6*)(buff - offset);
887 ret = Process_CONN_SYN_6(connSyn6, &(it->second), user, sip);
890 connSyn7 = (CONN_SYN_7*)(buff - offset);
891 ret = Process_CONN_SYN_7(connSyn7, &(it->second), user, sip);
894 connSyn8 = (CONN_SYN_8*)(buff - offset);
895 ret = Process_CONN_SYN_8(connSyn8, &(it->second), user, sip);
906 Send_CONN_SYN_ACK_6(iaUser, user, sip);
909 Send_CONN_SYN_ACK_7(iaUser, user, sip);
912 Send_CONN_SYN_ACK_8(iaUser, user, sip);
918 connAck = (CONN_ACK_6*)(buff - offset);
922 ret = Process_CONN_ACK_6(connAck, iaUser, user, sip);
925 ret = Process_CONN_ACK_7(connAck, iaUser, user, sip);
928 ret = Process_CONN_ACK_8((CONN_ACK_8*)(buff - offset), iaUser, user, sip);
934 SendError(sip, sport, protoVer, errorStr);
941 Send_ALIVE_SYN_6(iaUser, sip);
944 Send_ALIVE_SYN_7(iaUser, sip);
947 Send_ALIVE_SYN_8(iaUser, sip);
953 // ðÒÉÂÙÌ ÏÔ×ÅÔ Ó ÐÏÄÔ×ÅÒÖÄÅÎÉÅÍ ALIVE
955 aliveAck = (ALIVE_ACK_6*)(buff - offset);
959 ret = Process_ALIVE_ACK_6(aliveAck, iaUser, user, sip);
962 ret = Process_ALIVE_ACK_7(aliveAck, iaUser, user, sip);
965 ret = Process_ALIVE_ACK_8((ALIVE_ACK_8*)(buff - offset), iaUser, user, sip);
970 // úÁÐÒÏÓ ÎÁ ÏÔËÌÀÞÅÎÉÅ
973 disconnSyn = (DISCONN_SYN_6*)(buff - offset);
977 ret = Process_DISCONN_SYN_6(disconnSyn, iaUser, user, sip);
980 ret = Process_DISCONN_SYN_7(disconnSyn, iaUser, user, sip);
983 ret = Process_DISCONN_SYN_8((DISCONN_SYN_8*)(buff - offset), iaUser, user, sip);
993 Send_DISCONN_SYN_ACK_6(iaUser, sip);
996 Send_DISCONN_SYN_ACK_7(iaUser, sip);
999 Send_DISCONN_SYN_ACK_8(iaUser, sip);
1005 disconnAck = (DISCONN_ACK_6*)(buff - offset);
1010 ret = Process_DISCONN_ACK_6(disconnAck, iaUser, user, sip, it);
1013 ret = Process_DISCONN_ACK_7(disconnAck, iaUser, user, sip, it);
1016 ret = Process_DISCONN_ACK_8((DISCONN_ACK_8*)(buff - offset), iaUser, user, sip, it);
1023 Send_FIN_6(iaUser, sip, it);
1026 Send_FIN_7(iaUser, sip, it);
1029 Send_FIN_8(iaUser, sip, it);
1037 //-----------------------------------------------------------------------------
1038 void AUTH_IA::DelUser(user_iter u)
1040 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1042 uint32_t ip = u->GetCurrIP();
1047 map<uint32_t, IA_USER>::iterator it;
1048 it = ip2user.find(ip);
1049 if (it == ip2user.end())
1052 printfd(__FILE__, "Nothing to delete\n");
1056 if (it->second.user == u)
1058 printfd(__FILE__, "User removed!\n");
1059 it->second.user->Unauthorize(this);
1063 //-----------------------------------------------------------------------------
1064 int AUTH_IA::SendError(uint32_t ip, uint16_t port, int protoVer, const string & text)
1066 struct sockaddr_in sendAddr;
1073 memset(&err, 0, sizeof(ERR));
1075 sendAddr.sin_family = AF_INET;
1076 sendAddr.sin_port = htons(port);
1078 sendAddr.sin_addr.s_addr = ip;// IP ÐÏÌØÚÏ×ÁÔÅÌÑ
1081 strncpy((char*)err.type, "ERR", 16);
1082 strncpy((char*)err.text, text.c_str(), MAX_MSG_LEN);
1088 res = sendto(listenSocket, &err, sizeof(err), 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
1089 printfd(__FILE__, "SendError %d bytes sent\n", res);
1094 memset(&err8, 0, sizeof(ERR_8));
1096 sendAddr.sin_family = AF_INET;
1097 sendAddr.sin_port = htons(port);
1099 sendAddr.sin_addr.s_addr = ip;// IP ÐÏÌØÚÏ×ÁÔÅÌÑ
1102 strncpy((char*)err8.type, "ERR", 16);
1103 strncpy((char*)err8.text, text.c_str(), MAX_MSG_LEN);
1106 SwapBytes(err8.len);
1109 res = sendto(listenSocket, &err8, sizeof(err8), 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
1110 printfd(__FILE__, "SendError_8 %d bytes sent\n", res);
1116 //-----------------------------------------------------------------------------
1117 int AUTH_IA::Send(uint32_t ip, uint16_t port, const char * buffer, int len)
1119 struct sockaddr_in sendAddr;
1122 sendAddr.sin_family = AF_INET;
1123 sendAddr.sin_port = htons(port);
1124 sendAddr.sin_addr.s_addr = ip;
1126 res = sendto(listenSocket, buffer, len, 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
1128 static struct timeval tv;
1129 gettimeofday(&tv, NULL);
1133 //-----------------------------------------------------------------------------
1134 void AUTH_IA::InitEncrypt(BLOWFISH_CTX * ctx, const string & password)
1136 unsigned char keyL[PASSWD_LEN]; // ðÁÒÏÌØ ÄÌÑ ÛÉÆÒÏ×ËÉ
1137 memset(keyL, 0, PASSWD_LEN);
1138 strncpy((char *)keyL, password.c_str(), PASSWD_LEN);
1139 Blowfish_Init(ctx, keyL, PASSWD_LEN);
1141 //-----------------------------------------------------------------------------
1142 void AUTH_IA::Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
1144 // len8 - ÄÌÉÎÁ × 8-ÍÉ ÂÁÊÔÏ×ÙÈ ÂÌÏËÁÈ
1146 for (int i = 0; i < len8; i++)
1147 DecodeString(dst + i * 8, src + i * 8, ctx);
1149 //-----------------------------------------------------------------------------
1150 void AUTH_IA::Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
1152 // len8 - ÄÌÉÎÁ × 8-ÍÉ ÂÁÊÔÏ×ÙÈ ÂÌÏËÁÈ
1154 for (int i = 0; i < len8; i++)
1155 EncodeString(dst + i * 8, src + i * 8, ctx);
1157 //-----------------------------------------------------------------------------
1158 int AUTH_IA::SendMessage(const STG_MSG & msg, uint32_t ip) const
1160 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1162 printfd(__FILE__, "SendMessage userIP=%s\n", inet_ntostring(ip).c_str());
1164 map<uint32_t, IA_USER>::iterator it;
1165 it = ip2user.find(ip);
1166 if (it == ip2user.end())
1168 errorStr = "Unknown user.";
1171 it->second.messagesToSend.push_back(msg);
1174 //-----------------------------------------------------------------------------
1175 int AUTH_IA::RealSendMessage6(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1177 printfd(__FILE__, "RealSendMessage 6 user=%s\n", user.user->GetLogin().c_str());
1182 memset(&info, 0, sizeof(INFO_6));
1185 strncpy((char*)info.type, "INFO", 16);
1186 info.infoType = 'I';
1187 strncpy((char*)info.text, msg.text.c_str(), 235);
1190 size_t len = info.len;
1192 SwapBytes(info.len);
1195 memcpy(buffer, &info, sizeof(INFO_6));
1196 Encrypt(&user.ctx, buffer, buffer, len / 8);
1197 Send(ip, iaSettings.GetUserPort(), buffer, len);
1201 //-----------------------------------------------------------------------------
1202 int AUTH_IA::RealSendMessage7(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1204 printfd(__FILE__, "RealSendMessage 7 user=%s\n", user.user->GetLogin().c_str());
1209 memset(&info, 0, sizeof(INFO_7));
1212 strncpy((char*)info.type, "INFO_7", 16);
1213 info.infoType = msg.header.type;
1214 info.showTime = msg.header.showTime;
1216 info.sendTime = msg.header.creationTime;
1218 size_t len = info.len;
1220 SwapBytes(info.len);
1221 SwapBytes(info.sendTime);
1224 strncpy((char*)info.text, msg.text.c_str(), MAX_MSG_LEN - 1);
1225 info.text[MAX_MSG_LEN - 1] = 0;
1227 memcpy(buffer, &info, sizeof(INFO_7));
1229 Encrypt(&user.ctx, buffer, buffer, len / 8);
1230 Send(ip, iaSettings.GetUserPort(), buffer, len);
1234 //-----------------------------------------------------------------------------
1235 int AUTH_IA::RealSendMessage8(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1237 printfd(__FILE__, "RealSendMessage 8 user=%s\n", user.user->GetLogin().c_str());
1240 memset(buffer, 0, sizeof(buffer));
1244 memset(&info, 0, sizeof(INFO_8));
1247 strncpy((char*)info.type, "INFO_8", 16);
1248 info.infoType = msg.header.type;
1249 info.showTime = msg.header.showTime;
1250 info.sendTime = msg.header.creationTime;
1252 strncpy((char*)info.text, msg.text.c_str(), IA_MAX_MSG_LEN_8 - 1);
1253 info.text[IA_MAX_MSG_LEN_8 - 1] = 0;
1255 size_t len = info.len;
1257 SwapBytes(info.len);
1258 SwapBytes(info.sendTime);
1261 memcpy(buffer, &info, sizeof(INFO_8));
1263 Encrypt(&user.ctx, buffer, buffer, len / 8);
1264 Send(ip, user.port, buffer, len);
1268 //-----------------------------------------------------------------------------
1269 int AUTH_IA::Process_CONN_SYN_6(CONN_SYN_6 *, IA_USER * iaUser, user_iter *, uint32_t)
1271 if (!(iaUser->phase.GetPhase() == 1 || iaUser->phase.GetPhase() == 3))
1274 enabledDirs = 0xFFffFFff;
1276 iaUser->phase.SetPhase2();
1277 printfd(__FILE__, "Phase changed from %d to 2. Reason: CONN_SYN_6\n", iaUser->phase.GetPhase());
1280 //-----------------------------------------------------------------------------
1281 int AUTH_IA::Process_CONN_SYN_7(CONN_SYN_7 * connSyn, IA_USER * iaUser, user_iter * user, uint32_t sip)
1283 return Process_CONN_SYN_6(connSyn, iaUser, user, sip);
1285 //-----------------------------------------------------------------------------
1286 int AUTH_IA::Process_CONN_SYN_8(CONN_SYN_8 * connSyn, IA_USER * iaUser, user_iter * user, uint32_t sip)
1289 SwapBytes(connSyn->dirs);
1291 int ret = Process_CONN_SYN_6((CONN_SYN_6*)connSyn, iaUser, user, sip);
1292 enabledDirs = connSyn->dirs;
1295 //-----------------------------------------------------------------------------
1296 int AUTH_IA::Process_CONN_ACK_6(CONN_ACK_6 * connAck, IA_USER * iaUser, user_iter *, uint32_t sip)
1299 SwapBytes(connAck->len);
1300 SwapBytes(connAck->rnd);
1302 printfd( __FILE__, "CONN_ACK_6 %s\n", connAck->type);
1303 // ÕÓÔÁÎÏ×ÉÔØ ÎÏ×ÕÀ ÆÁÚÕ É ×ÒÅÍÑ É ÒÁÚÒÅÛÉÔØ ÉÎÅÔ
1304 if ((iaUser->phase.GetPhase() == 2) && (connAck->rnd == iaUser->rnd + 1))
1306 iaUser->phase.UpdateTime();
1308 iaUser->lastSendAlive = iaUser->phase.GetTime();
1309 if (iaUser->user->Authorize(sip, "", enabledDirs, this) == 0)
1311 iaUser->phase.SetPhase3();
1312 printfd(__FILE__, "Phase changed from 2 to 3. Reason: CONN_ACK_6\n");
1317 errorStr = iaUser->user->GetStrError();
1318 iaUser->phase.SetPhase1();
1319 printfd(__FILE__, "Phase changed from 2 to 1. Reason: failed to authorize user\n");
1323 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), connAck->rnd);
1326 //-----------------------------------------------------------------------------
1327 int AUTH_IA::Process_CONN_ACK_7(CONN_ACK_7 * connAck, IA_USER * iaUser, user_iter * user, uint32_t sip)
1329 return Process_CONN_ACK_6(connAck, iaUser, user, sip);
1331 //-----------------------------------------------------------------------------
1332 int AUTH_IA::Process_CONN_ACK_8(CONN_ACK_8 * connAck, IA_USER * iaUser, user_iter *, uint32_t sip)
1335 SwapBytes(connAck->len);
1336 SwapBytes(connAck->rnd);
1338 printfd( __FILE__, "CONN_ACK_8 %s\n", connAck->type);
1340 if ((iaUser->phase.GetPhase() == 2) && (connAck->rnd == iaUser->rnd + 1))
1342 iaUser->phase.UpdateTime();
1343 iaUser->lastSendAlive = iaUser->phase.GetTime();
1344 if (iaUser->user->Authorize(sip, "", enabledDirs, this) == 0)
1346 iaUser->phase.SetPhase3();
1347 printfd(__FILE__, "Phase changed from 2 to 3. Reason: CONN_ACK_8\n");
1352 errorStr = iaUser->user->GetStrError();
1353 iaUser->phase.SetPhase1();
1354 printfd(__FILE__, "Phase changed from 2 to 1. Reason: failed to authorize user\n");
1358 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), connAck->rnd);
1361 //-----------------------------------------------------------------------------
1362 int AUTH_IA::Process_ALIVE_ACK_6(ALIVE_ACK_6 * aliveAck, IA_USER * iaUser, user_iter *, uint32_t)
1365 SwapBytes(aliveAck->len);
1366 SwapBytes(aliveAck->rnd);
1368 printfd(__FILE__, "ALIVE_ACK_6\n");
1369 if ((iaUser->phase.GetPhase() == 3) && (aliveAck->rnd == iaUser->rnd + 1))
1371 iaUser->phase.UpdateTime();
1373 iaUser->aliveSent = false;
1378 //-----------------------------------------------------------------------------
1379 int AUTH_IA::Process_ALIVE_ACK_7(ALIVE_ACK_7 * aliveAck, IA_USER * iaUser, user_iter * user, uint32_t sip)
1381 return Process_ALIVE_ACK_6(aliveAck, iaUser, user, sip);
1383 //-----------------------------------------------------------------------------
1384 int AUTH_IA::Process_ALIVE_ACK_8(ALIVE_ACK_8 * aliveAck, IA_USER * iaUser, user_iter *, uint32_t)
1387 SwapBytes(aliveAck->len);
1388 SwapBytes(aliveAck->rnd);
1390 printfd(__FILE__, "ALIVE_ACK_8\n");
1391 if ((iaUser->phase.GetPhase() == 3) && (aliveAck->rnd == iaUser->rnd + 1))
1393 iaUser->phase.UpdateTime();
1395 iaUser->aliveSent = false;
1400 //-----------------------------------------------------------------------------
1401 int AUTH_IA::Process_DISCONN_SYN_6(DISCONN_SYN_6 *, IA_USER * iaUser, user_iter *, uint32_t)
1403 printfd(__FILE__, "DISCONN_SYN_6\n");
1404 if (iaUser->phase.GetPhase() != 3)
1406 printfd(__FILE__, "Invalid phase. Expected 3, actual %d\n", iaUser->phase.GetPhase());
1407 errorStr = "Incorrect request DISCONN_SYN";
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_SYN_7(DISCONN_SYN_7 * disconnSyn, IA_USER * iaUser, user_iter * user, uint32_t sip)
1419 return Process_DISCONN_SYN_6(disconnSyn, iaUser, user, sip);
1421 //-----------------------------------------------------------------------------
1422 int AUTH_IA::Process_DISCONN_SYN_8(DISCONN_SYN_8 *, IA_USER * iaUser, user_iter *, uint32_t)
1424 if (iaUser->phase.GetPhase() != 3)
1426 errorStr = "Incorrect request DISCONN_SYN";
1427 printfd(__FILE__, "Invalid phase. Expected 3, actual %d\n", iaUser->phase.GetPhase());
1431 iaUser->phase.SetPhase4();
1432 printfd(__FILE__, "Phase changed from 3 to 4. Reason: DISCONN_SYN_6\n");
1436 //-----------------------------------------------------------------------------
1437 int AUTH_IA::Process_DISCONN_ACK_6(DISCONN_ACK_6 * disconnAck,
1441 map<uint32_t, IA_USER>::iterator)
1444 SwapBytes(disconnAck->len);
1445 SwapBytes(disconnAck->rnd);
1447 printfd(__FILE__, "DISCONN_ACK_6\n");
1448 if (!((iaUser->phase.GetPhase() == 4) && (disconnAck->rnd == iaUser->rnd + 1)))
1450 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), disconnAck->rnd);
1456 //-----------------------------------------------------------------------------
1457 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)
1459 return Process_DISCONN_ACK_6(disconnAck, iaUser, user, sip, it);
1461 //-----------------------------------------------------------------------------
1462 int AUTH_IA::Process_DISCONN_ACK_8(DISCONN_ACK_8 * disconnAck, IA_USER * iaUser, user_iter *, uint32_t, map<uint32_t, IA_USER>::iterator)
1465 SwapBytes(disconnAck->len);
1466 SwapBytes(disconnAck->rnd);
1468 printfd(__FILE__, "DISCONN_ACK_8\n");
1469 if (!((iaUser->phase.GetPhase() == 4) && (disconnAck->rnd == iaUser->rnd + 1)))
1471 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), disconnAck->rnd);
1477 //-----------------------------------------------------------------------------
1478 int AUTH_IA::Send_CONN_SYN_ACK_6(IA_USER * iaUser, user_iter *, uint32_t sip)
1480 //+++ Fill static data in connSynAck +++
1481 // TODO Move this code. It must be executed only once
1482 connSynAck6.len = Min8(sizeof(CONN_SYN_ACK_6));
1483 strcpy((char*)connSynAck6.type, "CONN_SYN_ACK");
1484 for (int j = 0; j < DIR_NUM; j++)
1486 strncpy((char*)connSynAck6.dirName[j],
1487 stgSettings->GetDirName(j).c_str(),
1490 connSynAck6.dirName[j][sizeof(string16) - 1] = 0;
1492 //--- Fill static data in connSynAck ---
1494 iaUser->rnd = random();
1495 connSynAck6.rnd = iaUser->rnd;
1497 connSynAck6.userTimeOut = iaSettings.GetUserTimeout();
1498 connSynAck6.aliveDelay = iaSettings.GetUserDelay();
1501 SwapBytes(connSynAck6.len);
1502 SwapBytes(connSynAck6.rnd);
1503 SwapBytes(connSynAck6.userTimeOut);
1504 SwapBytes(connSynAck6.aliveDelay);
1507 Encrypt(&iaUser->ctx, (char*)&connSynAck6, (char*)&connSynAck6, Min8(sizeof(CONN_SYN_ACK_6))/8);
1508 return Send(sip, iaSettings.GetUserPort(), (char*)&connSynAck6, Min8(sizeof(CONN_SYN_ACK_6)));;
1510 //-----------------------------------------------------------------------------
1511 int AUTH_IA::Send_CONN_SYN_ACK_7(IA_USER * iaUser, user_iter * user, uint32_t sip)
1513 return Send_CONN_SYN_ACK_6(iaUser, user, sip);
1515 //-----------------------------------------------------------------------------
1516 int AUTH_IA::Send_CONN_SYN_ACK_8(IA_USER * iaUser, user_iter *, uint32_t sip)
1518 strcpy((char*)connSynAck8.hdr.magic, IA_ID);
1519 connSynAck8.hdr.protoVer[0] = 0;
1520 connSynAck8.hdr.protoVer[1] = 8;
1522 //+++ Fill static data in connSynAck +++
1523 // TODO Move this code. It must be executed only once
1524 connSynAck8.len = Min8(sizeof(CONN_SYN_ACK_8));
1525 strcpy((char*)connSynAck8.type, "CONN_SYN_ACK");
1526 for (int j = 0; j < DIR_NUM; j++)
1528 strncpy((char*)connSynAck8.dirName[j],
1529 stgSettings->GetDirName(j).c_str(),
1532 connSynAck8.dirName[j][sizeof(string16) - 1] = 0;
1534 //--- Fill static data in connSynAck ---
1536 iaUser->rnd = random();
1537 connSynAck8.rnd = iaUser->rnd;
1539 connSynAck8.userTimeOut = iaSettings.GetUserTimeout();
1540 connSynAck8.aliveDelay = iaSettings.GetUserDelay();
1543 SwapBytes(connSynAck8.len);
1544 SwapBytes(connSynAck8.rnd);
1545 SwapBytes(connSynAck8.userTimeOut);
1546 SwapBytes(connSynAck8.aliveDelay);
1549 Encrypt(&iaUser->ctx, (char*)&connSynAck8, (char*)&connSynAck8, Min8(sizeof(CONN_SYN_ACK_8))/8);
1550 return Send(sip, iaUser->port, (char*)&connSynAck8, Min8(sizeof(CONN_SYN_ACK_8)));
1551 //return Send(sip, iaUser->port, (char*)&connSynAck8, 384);
1553 //-----------------------------------------------------------------------------
1554 int AUTH_IA::Send_ALIVE_SYN_6(IA_USER * iaUser, uint32_t sip)
1556 aliveSyn6.len = Min8(sizeof(ALIVE_SYN_6));
1557 aliveSyn6.rnd = iaUser->rnd = random();
1559 strcpy((char*)aliveSyn6.type, "ALIVE_SYN");
1561 for (int i = 0; i < DIR_NUM; i++)
1563 aliveSyn6.md[i] = iaUser->user->property.down.Get()[i];
1564 aliveSyn6.mu[i] = iaUser->user->property.up.Get()[i];
1566 aliveSyn6.sd[i] = iaUser->user->GetSessionDownload()[i];
1567 aliveSyn6.su[i] = iaUser->user->GetSessionUpload()[i];
1571 int dn = iaSettings.GetFreeMbShowType();
1572 const TARIFF * tf = iaUser->user->GetTariff();
1576 double p = tf->GetPriceWithTraffType(aliveSyn6.mu[dn],
1583 snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "---");
1587 double fmb = iaUser->user->property.freeMb;
1588 fmb = fmb < 0 ? 0 : fmb;
1589 snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "%.3f", fmb / p);
1594 if (freeMbNone == iaSettings.GetFreeMbShowType())
1596 aliveSyn6.freeMb[0] = 0;
1600 double fmb = iaUser->user->property.freeMb;
1601 fmb = fmb < 0 ? 0 : fmb;
1602 snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "C%.3f", fmb);
1607 if (iaUser->aliveSent)
1609 printfd(__FILE__, "========= ALIVE_ACK_6(7) TIMEOUT !!! %s =========\n", iaUser->user->GetLogin().c_str());
1611 iaUser->aliveSent = true;
1614 aliveSyn6.cash =(int64_t) (iaUser->user->property.cash.Get() * 1000.0);
1615 if (!stgSettings->GetShowFeeInCash())
1616 aliveSyn6.cash -= (int64_t)(tf->GetFee() * 1000.0);
1619 SwapBytes(aliveSyn6.len);
1620 SwapBytes(aliveSyn6.rnd);
1621 SwapBytes(aliveSyn6.cash);
1622 for (int i = 0; i < DIR_NUM; ++i)
1624 SwapBytes(aliveSyn6.mu[i]);
1625 SwapBytes(aliveSyn6.md[i]);
1626 SwapBytes(aliveSyn6.su[i]);
1627 SwapBytes(aliveSyn6.sd[i]);
1631 Encrypt(&(iaUser->ctx), (char*)&aliveSyn6, (char*)&aliveSyn6, Min8(sizeof(aliveSyn6))/8);
1632 return Send(sip, iaSettings.GetUserPort(), (char*)&aliveSyn6, Min8(sizeof(aliveSyn6)));
1634 //-----------------------------------------------------------------------------
1635 int AUTH_IA::Send_ALIVE_SYN_7(IA_USER * iaUser, uint32_t sip)
1637 return Send_ALIVE_SYN_6(iaUser, sip);
1639 //-----------------------------------------------------------------------------
1640 int AUTH_IA::Send_ALIVE_SYN_8(IA_USER * iaUser, uint32_t sip)
1642 strcpy((char*)aliveSyn8.hdr.magic, IA_ID);
1643 aliveSyn8.hdr.protoVer[0] = 0;
1644 aliveSyn8.hdr.protoVer[1] = 8;
1646 aliveSyn8.len = Min8(sizeof(ALIVE_SYN_8));
1647 aliveSyn8.rnd = iaUser->rnd = random();
1649 strcpy((char*)aliveSyn8.type, "ALIVE_SYN");
1651 for (int i = 0; i < DIR_NUM; i++)
1653 aliveSyn8.md[i] = iaUser->user->property.down.Get()[i];
1654 aliveSyn8.mu[i] = iaUser->user->property.up.Get()[i];
1656 aliveSyn8.sd[i] = iaUser->user->GetSessionDownload()[i];
1657 aliveSyn8.su[i] = iaUser->user->GetSessionUpload()[i];
1661 int dn = iaSettings.GetFreeMbShowType();
1665 const TARIFF * tf = iaUser->user->GetTariff();
1666 double p = tf->GetPriceWithTraffType(aliveSyn8.mu[dn],
1673 snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "---");
1677 double fmb = iaUser->user->property.freeMb;
1678 fmb = fmb < 0 ? 0 : fmb;
1679 snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "%.3f", fmb / p);
1684 if (freeMbNone == iaSettings.GetFreeMbShowType())
1686 aliveSyn8.freeMb[0] = 0;
1690 double fmb = iaUser->user->property.freeMb;
1691 fmb = fmb < 0 ? 0 : fmb;
1692 snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "C%.3f", fmb);
1697 if (iaUser->aliveSent)
1699 printfd(__FILE__, "========= ALIVE_ACK_8 TIMEOUT !!! =========\n");
1701 iaUser->aliveSent = true;
1704 const TARIFF * tf = iaUser->user->GetTariff();
1706 aliveSyn8.cash =(int64_t) (iaUser->user->property.cash.Get() * 1000.0);
1707 if (!stgSettings->GetShowFeeInCash())
1708 aliveSyn8.cash -= (int64_t)(tf->GetFee() * 1000.0);
1711 SwapBytes(aliveSyn8.len);
1712 SwapBytes(aliveSyn8.rnd);
1713 SwapBytes(aliveSyn8.cash);
1714 SwapBytes(aliveSyn8.status);
1715 for (int i = 0; i < DIR_NUM; ++i)
1717 SwapBytes(aliveSyn8.mu[i]);
1718 SwapBytes(aliveSyn8.md[i]);
1719 SwapBytes(aliveSyn8.su[i]);
1720 SwapBytes(aliveSyn8.sd[i]);
1724 Encrypt(&(iaUser->ctx), (char*)&aliveSyn8, (char*)&aliveSyn8, Min8(sizeof(aliveSyn8))/8);
1725 return Send(sip, iaUser->port, (char*)&aliveSyn8, Min8(sizeof(aliveSyn8)));
1727 //-----------------------------------------------------------------------------
1728 int AUTH_IA::Send_DISCONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip)
1730 disconnSynAck6.len = Min8(sizeof(DISCONN_SYN_ACK_6));
1731 strcpy((char*)disconnSynAck6.type, "DISCONN_SYN_ACK");
1732 disconnSynAck6.rnd = iaUser->rnd = random();
1735 SwapBytes(disconnSynAck6.len);
1736 SwapBytes(disconnSynAck6.rnd);
1739 Encrypt(&iaUser->ctx, (char*)&disconnSynAck6, (char*)&disconnSynAck6, Min8(sizeof(disconnSynAck6))/8);
1740 return Send(sip, iaSettings.GetUserPort(), (char*)&disconnSynAck6, Min8(sizeof(disconnSynAck6)));
1742 //-----------------------------------------------------------------------------
1743 int AUTH_IA::Send_DISCONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip)
1745 return Send_DISCONN_SYN_ACK_6(iaUser, sip);
1747 //-----------------------------------------------------------------------------
1748 int AUTH_IA::Send_DISCONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip)
1750 strcpy((char*)disconnSynAck8.hdr.magic, IA_ID);
1751 disconnSynAck8.hdr.protoVer[0] = 0;
1752 disconnSynAck8.hdr.protoVer[1] = 8;
1754 disconnSynAck8.len = Min8(sizeof(DISCONN_SYN_ACK_8));
1755 strcpy((char*)disconnSynAck8.type, "DISCONN_SYN_ACK");
1756 disconnSynAck8.rnd = iaUser->rnd = random();
1759 SwapBytes(disconnSynAck8.len);
1760 SwapBytes(disconnSynAck8.rnd);
1763 Encrypt(&iaUser->ctx, (char*)&disconnSynAck8, (char*)&disconnSynAck8, Min8(sizeof(disconnSynAck8))/8);
1764 return Send(sip, iaUser->port, (char*)&disconnSynAck8, Min8(sizeof(disconnSynAck8)));
1766 //-----------------------------------------------------------------------------
1767 int AUTH_IA::Send_FIN_6(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1769 fin6.len = Min8(sizeof(FIN_6));
1770 strcpy((char*)fin6.type, "FIN");
1771 strcpy((char*)fin6.ok, "OK");
1774 SwapBytes(fin6.len);
1777 Encrypt(&iaUser->ctx, (char*)&fin6, (char*)&fin6, Min8(sizeof(fin6))/8);
1779 iaUser->user->Unauthorize(this);
1781 int ret = Send(sip, iaSettings.GetUserPort(), (char*)&fin6, Min8(sizeof(fin6)));
1785 //-----------------------------------------------------------------------------
1786 int AUTH_IA::Send_FIN_7(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1788 return Send_FIN_6(iaUser, sip, it);
1790 //-----------------------------------------------------------------------------
1791 int AUTH_IA::Send_FIN_8(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1793 strcpy((char*)fin8.hdr.magic, IA_ID);
1794 fin8.hdr.protoVer[0] = 0;
1795 fin8.hdr.protoVer[1] = 8;
1797 fin8.len = Min8(sizeof(FIN_8));
1798 strcpy((char*)fin8.type, "FIN");
1799 strcpy((char*)fin8.ok, "OK");
1802 SwapBytes(fin8.len);
1805 Encrypt(&iaUser->ctx, (char*)&fin8, (char*)&fin8, Min8(sizeof(fin8))/8);
1807 iaUser->user->Unauthorize(this);
1809 int ret = Send(sip, iaUser->port, (char*)&fin8, Min8(sizeof(fin8)));
1813 //-----------------------------------------------------------------------------
1814 bool AUTH_IA::WaitPackets(int sd) const
1822 tv.tv_usec = 500000;
1824 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
1825 if (res == -1) // Error
1829 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
1834 if (res == 0) // Timeout