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 // ôÕÔ ÓÏÂÒÁÎÙ ÏÂÒÁÂÏÔÞÉËÉ ÒÁÚÎÙÈ ÐÁËÅÔÏ×
767 const int offset = LOGIN_LEN + 2 + 6; // LOGIN_LEN + sizeOfMagic + sizeOfVer;
769 IA_USER * iaUser = NULL;
771 CONN_SYN_6 * connSyn6;
772 CONN_SYN_7 * connSyn7;
773 CONN_SYN_8 * connSyn8;
775 CONN_ACK_6 * connAck;
776 ALIVE_ACK_6 * aliveAck;
777 DISCONN_SYN_6 * disconnSyn;
778 DISCONN_ACK_6 * disconnAck;
780 map<uint32_t, IA_USER>::iterator it;
781 it = ip2user.find(sip);
783 if (it == ip2user.end() || (*user)->GetID() != it->second.user->GetID())
785 // åÝÅ ÎÅ ÂÙÌÏ ÚÁÐÒÏÓÏ× Ó ÜÔÏÇÏ IP
786 printfd(__FILE__, "Add new user\n");
787 ip2user[sip].protoVer = protoVer;
788 ip2user[sip].user = *user;
789 ip2user[sip].port = sport;
790 #ifdef IA_PHASE_DEBUG
791 ip2user[sip].phase.SetLogFileName(stgSettings->GetLogFileName());
792 ip2user[sip].phase.SetUserLogin((*user)->GetLogin());
796 it = ip2user.find(sip); //TODO
797 if (it == ip2user.end())
799 printfd(__FILE__, "+++ ERROR +++\n");
804 iaUser = &(it->second);
806 if (iaUser->port != sport)
807 iaUser->port = sport;
809 if (iaUser->password != (*user)->property.password.Get())
811 InitEncrypt(&iaUser->ctx, (*user)->property.password.Get());
812 iaUser->password = (*user)->property.password.Get();
816 Decrypt(&iaUser->ctx, buff, buff, (dataLen - offset) / 8);
818 char packetName[IA_MAX_TYPE_LEN];
819 strncpy(packetName, buff + 4, IA_MAX_TYPE_LEN);
820 packetName[IA_MAX_TYPE_LEN - 1] = 0;
822 map<string, int>::iterator pi;
823 pi = packetTypes.find(packetName);
824 if (pi == packetTypes.end())
826 SendError(sip, sport, protoVer, "îÅÐÒÁ×ÉÌØÎÙÊ ÌÏÇÉÎ ÉÌÉ ÐÁÒÏÌØ!");
827 printfd(__FILE__, "Login or password is wrong!\n");
828 WriteServLog("User's connect failed. IP \'%s\'. Wrong login or password", inet_ntostring(sip).c_str());
836 if ((*user)->property.disabled.Get())
838 SendError(sip, sport, protoVer, "õÞÅÔÎÁÑ ÚÁÐÉÓØ ÚÁÂÌÏËÉÒÏ×ÁÎÁ");
842 if ((*user)->property.passive.Get())
844 SendError(sip, sport, protoVer, "õÞÅÔÎÁÑ ÚÁÐÉÓØ ÚÁÍÏÒÏÖÅÎÁ");
848 if ((*user)->GetAuthorized() && (*user)->GetCurrIP() != sip)
850 printfd(__FILE__, "Login %s alredy in use. IP \'%s\'\n", (*user)->GetLogin().c_str(), inet_ntostring(sip).c_str());
851 WriteServLog("Login %s alredy in use. IP \'%s\'", (*user)->GetLogin().c_str(), inet_ntostring(sip).c_str());
852 SendError(sip, sport, protoVer, "÷ÁÛ ÌÏÇÉÎ ÕÖÅ ÉÓÐÏÌØÚÕÅÔÓÑ!");
857 if (users->FindByIPIdx(sip, &u) == 0 && u->GetLogin() != (*user)->GetLogin())
859 printfd(__FILE__, "IP address alredy in use. IP \'%s\'", inet_ntostring(sip).c_str());
860 WriteServLog("IP address alredy in use. IP \'%s\'", inet_ntostring(sip).c_str());
861 SendError(sip, sport, protoVer, "÷ÁÛ IP ÁÄÒÅÓ ÕÖÅ ÉÓÐÏÌØÚÕÅÔÓÑ!");
865 // ôÅÐÅÒØ ÍÙ ÄÏÌÖÎÙ ÐÒÏ×ÅÒÉÔØ, ÍÏÖÅÔ ÌÉ ÐÏÌØÚÏ×ÁÔÅÌØ ÐÏÄËÌÀÞÉÔÓÑ Ó ÜÔÏÇÏ ÁÄÒÅÓÁ.
866 int ipFound = (*user)->property.ips.Get().IsIPInIPS(sip);
869 printfd(__FILE__, "User %s. IP address is incorrect. IP \'%s\'\n", (*user)->GetLogin().c_str(), inet_ntostring(sip).c_str());
870 WriteServLog("User %s. IP address is incorrect. IP \'%s\'", (*user)->GetLogin().c_str(), inet_ntostring(sip).c_str());
871 SendError(sip, sport, protoVer, "ðÏÌØÚÏ×ÁÔÅÌØ ÎÅ ÏÐÏÚÎÁÎ! ðÒÏ×ÅÒØÔÅ IP ÁÄÒÅÓ.");
883 connSyn6 = (CONN_SYN_6*)(buff - offset);
884 ret = Process_CONN_SYN_6(connSyn6, &(it->second), user, sip);
887 connSyn7 = (CONN_SYN_7*)(buff - offset);
888 ret = Process_CONN_SYN_7(connSyn7, &(it->second), user, sip);
891 connSyn8 = (CONN_SYN_8*)(buff - offset);
892 ret = Process_CONN_SYN_8(connSyn8, &(it->second), user, sip);
903 Send_CONN_SYN_ACK_6(iaUser, user, sip);
906 Send_CONN_SYN_ACK_7(iaUser, user, sip);
909 Send_CONN_SYN_ACK_8(iaUser, user, sip);
915 connAck = (CONN_ACK_6*)(buff - offset);
919 ret = Process_CONN_ACK_6(connAck, iaUser, user, sip);
922 ret = Process_CONN_ACK_7(connAck, iaUser, user, sip);
925 ret = Process_CONN_ACK_8((CONN_ACK_8*)(buff - offset), iaUser, user, sip);
931 SendError(sip, sport, protoVer, errorStr);
938 Send_ALIVE_SYN_6(iaUser, sip);
941 Send_ALIVE_SYN_7(iaUser, sip);
944 Send_ALIVE_SYN_8(iaUser, sip);
950 // ðÒÉÂÙÌ ÏÔ×ÅÔ Ó ÐÏÄÔ×ÅÒÖÄÅÎÉÅÍ ALIVE
952 aliveAck = (ALIVE_ACK_6*)(buff - offset);
956 ret = Process_ALIVE_ACK_6(aliveAck, iaUser, user, sip);
959 ret = Process_ALIVE_ACK_7(aliveAck, iaUser, user, sip);
962 ret = Process_ALIVE_ACK_8((ALIVE_ACK_8*)(buff - offset), iaUser, user, sip);
967 // úÁÐÒÏÓ ÎÁ ÏÔËÌÀÞÅÎÉÅ
970 disconnSyn = (DISCONN_SYN_6*)(buff - offset);
974 ret = Process_DISCONN_SYN_6(disconnSyn, iaUser, user, sip);
977 ret = Process_DISCONN_SYN_7(disconnSyn, iaUser, user, sip);
980 ret = Process_DISCONN_SYN_8((DISCONN_SYN_8*)(buff - offset), iaUser, user, sip);
990 Send_DISCONN_SYN_ACK_6(iaUser, sip);
993 Send_DISCONN_SYN_ACK_7(iaUser, sip);
996 Send_DISCONN_SYN_ACK_8(iaUser, sip);
1002 disconnAck = (DISCONN_ACK_6*)(buff - offset);
1007 ret = Process_DISCONN_ACK_6(disconnAck, iaUser, user, sip, it);
1010 ret = Process_DISCONN_ACK_7(disconnAck, iaUser, user, sip, it);
1013 ret = Process_DISCONN_ACK_8((DISCONN_ACK_8*)(buff - offset), iaUser, user, sip, it);
1020 Send_FIN_6(iaUser, sip, it);
1023 Send_FIN_7(iaUser, sip, it);
1026 Send_FIN_8(iaUser, sip, it);
1034 //-----------------------------------------------------------------------------
1035 void AUTH_IA::DelUser(user_iter u)
1037 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1039 uint32_t ip = u->GetCurrIP();
1044 map<uint32_t, IA_USER>::iterator it;
1045 it = ip2user.find(ip);
1046 if (it == ip2user.end())
1049 printfd(__FILE__, "Nothing to delete\n");
1053 if (it->second.user == u)
1055 printfd(__FILE__, "User removed!\n");
1056 it->second.user->Unauthorize(this);
1060 //-----------------------------------------------------------------------------
1061 int AUTH_IA::SendError(uint32_t ip, uint16_t port, int protoVer, const string & text)
1063 struct sockaddr_in sendAddr;
1070 memset(&err, 0, sizeof(ERR));
1072 sendAddr.sin_family = AF_INET;
1073 sendAddr.sin_port = htons(port);
1075 sendAddr.sin_addr.s_addr = ip;// IP ÐÏÌØÚÏ×ÁÔÅÌÑ
1078 strncpy((char*)err.type, "ERR", 16);
1079 strncpy((char*)err.text, text.c_str(), MAX_MSG_LEN);
1085 res = sendto(listenSocket, &err, sizeof(err), 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
1086 printfd(__FILE__, "SendError %d bytes sent\n", res);
1091 memset(&err8, 0, sizeof(ERR_8));
1093 sendAddr.sin_family = AF_INET;
1094 sendAddr.sin_port = htons(port);
1096 sendAddr.sin_addr.s_addr = ip;// IP ÐÏÌØÚÏ×ÁÔÅÌÑ
1099 strncpy((char*)err8.type, "ERR", 16);
1100 strncpy((char*)err8.text, text.c_str(), MAX_MSG_LEN);
1103 SwapBytes(err8.len);
1106 res = sendto(listenSocket, &err8, sizeof(err8), 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
1107 printfd(__FILE__, "SendError_8 %d bytes sent\n", res);
1113 //-----------------------------------------------------------------------------
1114 int AUTH_IA::Send(uint32_t ip, uint16_t port, const char * buffer, int len)
1116 struct sockaddr_in sendAddr;
1119 sendAddr.sin_family = AF_INET;
1120 sendAddr.sin_port = htons(port);
1121 sendAddr.sin_addr.s_addr = ip;
1123 res = sendto(listenSocket, buffer, len, 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
1125 static struct timeval tv;
1126 gettimeofday(&tv, NULL);
1130 //-----------------------------------------------------------------------------
1131 void AUTH_IA::InitEncrypt(BLOWFISH_CTX * ctx, const string & password)
1133 unsigned char keyL[PASSWD_LEN]; // ðÁÒÏÌØ ÄÌÑ ÛÉÆÒÏ×ËÉ
1134 memset(keyL, 0, PASSWD_LEN);
1135 strncpy((char *)keyL, password.c_str(), PASSWD_LEN);
1136 Blowfish_Init(ctx, keyL, PASSWD_LEN);
1138 //-----------------------------------------------------------------------------
1139 void AUTH_IA::Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
1141 // len8 - ÄÌÉÎÁ × 8-ÍÉ ÂÁÊÔÏ×ÙÈ ÂÌÏËÁÈ
1143 for (int i = 0; i < len8; i++)
1144 DecodeString(dst + i * 8, src + i * 8, ctx);
1146 //-----------------------------------------------------------------------------
1147 void AUTH_IA::Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
1149 // len8 - ÄÌÉÎÁ × 8-ÍÉ ÂÁÊÔÏ×ÙÈ ÂÌÏËÁÈ
1151 for (int i = 0; i < len8; i++)
1152 EncodeString(dst + i * 8, src + i * 8, ctx);
1154 //-----------------------------------------------------------------------------
1155 int AUTH_IA::SendMessage(const STG_MSG & msg, uint32_t ip) const
1157 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1159 printfd(__FILE__, "SendMessage userIP=%s\n", inet_ntostring(ip).c_str());
1161 map<uint32_t, IA_USER>::iterator it;
1162 it = ip2user.find(ip);
1163 if (it == ip2user.end())
1165 errorStr = "Unknown user.";
1168 it->second.messagesToSend.push_back(msg);
1171 //-----------------------------------------------------------------------------
1172 int AUTH_IA::RealSendMessage6(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1174 printfd(__FILE__, "RealSendMessage 6 user=%s\n", user.user->GetLogin().c_str());
1179 memset(&info, 0, sizeof(INFO_6));
1182 strncpy((char*)info.type, "INFO", 16);
1183 info.infoType = 'I';
1184 strncpy((char*)info.text, msg.text.c_str(), 235);
1187 size_t len = info.len;
1189 SwapBytes(info.len);
1192 memcpy(buffer, &info, sizeof(INFO_6));
1193 Encrypt(&user.ctx, buffer, buffer, len / 8);
1194 Send(ip, iaSettings.GetUserPort(), buffer, len);
1198 //-----------------------------------------------------------------------------
1199 int AUTH_IA::RealSendMessage7(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1201 printfd(__FILE__, "RealSendMessage 7 user=%s\n", user.user->GetLogin().c_str());
1206 memset(&info, 0, sizeof(INFO_7));
1209 strncpy((char*)info.type, "INFO_7", 16);
1210 info.infoType = msg.header.type;
1211 info.showTime = msg.header.showTime;
1213 info.sendTime = msg.header.creationTime;
1215 size_t len = info.len;
1217 SwapBytes(info.len);
1218 SwapBytes(info.sendTime);
1221 strncpy((char*)info.text, msg.text.c_str(), MAX_MSG_LEN - 1);
1222 info.text[MAX_MSG_LEN - 1] = 0;
1224 memcpy(buffer, &info, sizeof(INFO_7));
1226 Encrypt(&user.ctx, buffer, buffer, len / 8);
1227 Send(ip, iaSettings.GetUserPort(), buffer, len);
1231 //-----------------------------------------------------------------------------
1232 int AUTH_IA::RealSendMessage8(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1234 printfd(__FILE__, "RealSendMessage 8 user=%s\n", user.user->GetLogin().c_str());
1237 memset(buffer, 0, sizeof(buffer));
1241 memset(&info, 0, sizeof(INFO_8));
1244 strncpy((char*)info.type, "INFO_8", 16);
1245 info.infoType = msg.header.type;
1246 info.showTime = msg.header.showTime;
1247 info.sendTime = msg.header.creationTime;
1249 strncpy((char*)info.text, msg.text.c_str(), IA_MAX_MSG_LEN_8 - 1);
1250 info.text[IA_MAX_MSG_LEN_8 - 1] = 0;
1252 size_t len = info.len;
1254 SwapBytes(info.len);
1255 SwapBytes(info.sendTime);
1258 memcpy(buffer, &info, sizeof(INFO_8));
1260 Encrypt(&user.ctx, buffer, buffer, len / 8);
1261 Send(ip, user.port, buffer, len);
1265 //-----------------------------------------------------------------------------
1266 int AUTH_IA::Process_CONN_SYN_6(CONN_SYN_6 *, IA_USER * iaUser, user_iter *, uint32_t)
1268 if (!(iaUser->phase.GetPhase() == 1 || iaUser->phase.GetPhase() == 3))
1271 enabledDirs = 0xFFffFFff;
1273 iaUser->phase.SetPhase2();
1274 printfd(__FILE__, "Phase changed from %d to 2. Reason: CONN_SYN_6\n", iaUser->phase.GetPhase());
1277 //-----------------------------------------------------------------------------
1278 int AUTH_IA::Process_CONN_SYN_7(CONN_SYN_7 * connSyn, IA_USER * iaUser, user_iter * user, uint32_t sip)
1280 return Process_CONN_SYN_6(connSyn, iaUser, user, sip);
1282 //-----------------------------------------------------------------------------
1283 int AUTH_IA::Process_CONN_SYN_8(CONN_SYN_8 * connSyn, IA_USER * iaUser, user_iter * user, uint32_t sip)
1286 SwapBytes(connSyn->dirs);
1288 int ret = Process_CONN_SYN_6((CONN_SYN_6*)connSyn, iaUser, user, sip);
1289 enabledDirs = connSyn->dirs;
1292 //-----------------------------------------------------------------------------
1293 int AUTH_IA::Process_CONN_ACK_6(CONN_ACK_6 * connAck, IA_USER * iaUser, user_iter *, uint32_t sip)
1296 SwapBytes(connAck->len);
1297 SwapBytes(connAck->rnd);
1299 printfd( __FILE__, "CONN_ACK_6 %s\n", connAck->type);
1300 // ÕÓÔÁÎÏ×ÉÔØ ÎÏ×ÕÀ ÆÁÚÕ É ×ÒÅÍÑ É ÒÁÚÒÅÛÉÔØ ÉÎÅÔ
1301 if ((iaUser->phase.GetPhase() == 2) && (connAck->rnd == iaUser->rnd + 1))
1303 iaUser->phase.UpdateTime();
1305 iaUser->lastSendAlive = iaUser->phase.GetTime();
1306 if (iaUser->user->Authorize(sip, "", enabledDirs, this) == 0)
1308 iaUser->phase.SetPhase3();
1309 printfd(__FILE__, "Phase changed from 2 to 3. Reason: CONN_ACK_6\n");
1314 errorStr = iaUser->user->GetStrError();
1315 iaUser->phase.SetPhase1();
1316 printfd(__FILE__, "Phase changed from 2 to 1. Reason: failed to authorize user\n");
1320 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), connAck->rnd);
1323 //-----------------------------------------------------------------------------
1324 int AUTH_IA::Process_CONN_ACK_7(CONN_ACK_7 * connAck, IA_USER * iaUser, user_iter * user, uint32_t sip)
1326 return Process_CONN_ACK_6(connAck, iaUser, user, sip);
1328 //-----------------------------------------------------------------------------
1329 int AUTH_IA::Process_CONN_ACK_8(CONN_ACK_8 * connAck, IA_USER * iaUser, user_iter *, uint32_t sip)
1332 SwapBytes(connAck->len);
1333 SwapBytes(connAck->rnd);
1335 printfd( __FILE__, "CONN_ACK_8 %s\n", connAck->type);
1337 if ((iaUser->phase.GetPhase() == 2) && (connAck->rnd == iaUser->rnd + 1))
1339 iaUser->phase.UpdateTime();
1340 iaUser->lastSendAlive = iaUser->phase.GetTime();
1341 if (iaUser->user->Authorize(sip, "", enabledDirs, this) == 0)
1343 iaUser->phase.SetPhase3();
1344 printfd(__FILE__, "Phase changed from 2 to 3. Reason: CONN_ACK_8\n");
1349 errorStr = iaUser->user->GetStrError();
1350 iaUser->phase.SetPhase1();
1351 printfd(__FILE__, "Phase changed from 2 to 1. Reason: failed to authorize user\n");
1355 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), connAck->rnd);
1358 //-----------------------------------------------------------------------------
1359 int AUTH_IA::Process_ALIVE_ACK_6(ALIVE_ACK_6 * aliveAck, IA_USER * iaUser, user_iter *, uint32_t)
1362 SwapBytes(aliveAck->len);
1363 SwapBytes(aliveAck->rnd);
1365 printfd(__FILE__, "ALIVE_ACK_6\n");
1366 if ((iaUser->phase.GetPhase() == 3) && (aliveAck->rnd == iaUser->rnd + 1))
1368 iaUser->phase.UpdateTime();
1370 iaUser->aliveSent = false;
1375 //-----------------------------------------------------------------------------
1376 int AUTH_IA::Process_ALIVE_ACK_7(ALIVE_ACK_7 * aliveAck, IA_USER * iaUser, user_iter * user, uint32_t sip)
1378 return Process_ALIVE_ACK_6(aliveAck, iaUser, user, sip);
1380 //-----------------------------------------------------------------------------
1381 int AUTH_IA::Process_ALIVE_ACK_8(ALIVE_ACK_8 * aliveAck, IA_USER * iaUser, user_iter *, uint32_t)
1384 SwapBytes(aliveAck->len);
1385 SwapBytes(aliveAck->rnd);
1387 printfd(__FILE__, "ALIVE_ACK_8\n");
1388 if ((iaUser->phase.GetPhase() == 3) && (aliveAck->rnd == iaUser->rnd + 1))
1390 iaUser->phase.UpdateTime();
1392 iaUser->aliveSent = false;
1397 //-----------------------------------------------------------------------------
1398 int AUTH_IA::Process_DISCONN_SYN_6(DISCONN_SYN_6 *, IA_USER * iaUser, user_iter *, uint32_t)
1400 printfd(__FILE__, "DISCONN_SYN_6\n");
1401 if (iaUser->phase.GetPhase() != 3)
1403 printfd(__FILE__, "Invalid phase. Expected 3, actual %d\n", iaUser->phase.GetPhase());
1404 errorStr = "Incorrect request DISCONN_SYN";
1408 iaUser->phase.SetPhase4();
1409 printfd(__FILE__, "Phase changed from 3 to 4. Reason: DISCONN_SYN_6\n");
1413 //-----------------------------------------------------------------------------
1414 int AUTH_IA::Process_DISCONN_SYN_7(DISCONN_SYN_7 * disconnSyn, IA_USER * iaUser, user_iter * user, uint32_t sip)
1416 return Process_DISCONN_SYN_6(disconnSyn, iaUser, user, sip);
1418 //-----------------------------------------------------------------------------
1419 int AUTH_IA::Process_DISCONN_SYN_8(DISCONN_SYN_8 *, IA_USER * iaUser, user_iter *, uint32_t)
1421 if (iaUser->phase.GetPhase() != 3)
1423 errorStr = "Incorrect request DISCONN_SYN";
1424 printfd(__FILE__, "Invalid phase. Expected 3, actual %d\n", iaUser->phase.GetPhase());
1428 iaUser->phase.SetPhase4();
1429 printfd(__FILE__, "Phase changed from 3 to 4. Reason: DISCONN_SYN_6\n");
1433 //-----------------------------------------------------------------------------
1434 int AUTH_IA::Process_DISCONN_ACK_6(DISCONN_ACK_6 * disconnAck,
1438 map<uint32_t, IA_USER>::iterator)
1441 SwapBytes(disconnAck->len);
1442 SwapBytes(disconnAck->rnd);
1444 printfd(__FILE__, "DISCONN_ACK_6\n");
1445 if (!((iaUser->phase.GetPhase() == 4) && (disconnAck->rnd == iaUser->rnd + 1)))
1447 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), disconnAck->rnd);
1453 //-----------------------------------------------------------------------------
1454 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)
1456 return Process_DISCONN_ACK_6(disconnAck, iaUser, user, sip, it);
1458 //-----------------------------------------------------------------------------
1459 int AUTH_IA::Process_DISCONN_ACK_8(DISCONN_ACK_8 * disconnAck, IA_USER * iaUser, user_iter *, uint32_t, map<uint32_t, IA_USER>::iterator)
1462 SwapBytes(disconnAck->len);
1463 SwapBytes(disconnAck->rnd);
1465 printfd(__FILE__, "DISCONN_ACK_8\n");
1466 if (!((iaUser->phase.GetPhase() == 4) && (disconnAck->rnd == iaUser->rnd + 1)))
1468 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), disconnAck->rnd);
1474 //-----------------------------------------------------------------------------
1475 int AUTH_IA::Send_CONN_SYN_ACK_6(IA_USER * iaUser, user_iter *, uint32_t sip)
1477 //+++ Fill static data in connSynAck +++
1478 // TODO Move this code. It must be executed only once
1479 connSynAck6.len = Min8(sizeof(CONN_SYN_ACK_6));
1480 strcpy((char*)connSynAck6.type, "CONN_SYN_ACK");
1481 for (int j = 0; j < DIR_NUM; j++)
1483 strncpy((char*)connSynAck6.dirName[j],
1484 stgSettings->GetDirName(j).c_str(),
1487 connSynAck6.dirName[j][sizeof(string16) - 1] = 0;
1489 //--- Fill static data in connSynAck ---
1491 iaUser->rnd = random();
1492 connSynAck6.rnd = iaUser->rnd;
1494 connSynAck6.userTimeOut = iaSettings.GetUserTimeout();
1495 connSynAck6.aliveDelay = iaSettings.GetUserDelay();
1498 SwapBytes(connSynAck6.len);
1499 SwapBytes(connSynAck6.rnd);
1500 SwapBytes(connSynAck6.userTimeOut);
1501 SwapBytes(connSynAck6.aliveDelay);
1504 Encrypt(&iaUser->ctx, (char*)&connSynAck6, (char*)&connSynAck6, Min8(sizeof(CONN_SYN_ACK_6))/8);
1505 return Send(sip, iaSettings.GetUserPort(), (char*)&connSynAck6, Min8(sizeof(CONN_SYN_ACK_6)));;
1507 //-----------------------------------------------------------------------------
1508 int AUTH_IA::Send_CONN_SYN_ACK_7(IA_USER * iaUser, user_iter * user, uint32_t sip)
1510 return Send_CONN_SYN_ACK_6(iaUser, user, sip);
1512 //-----------------------------------------------------------------------------
1513 int AUTH_IA::Send_CONN_SYN_ACK_8(IA_USER * iaUser, user_iter *, uint32_t sip)
1515 strcpy((char*)connSynAck8.hdr.magic, IA_ID);
1516 connSynAck8.hdr.protoVer[0] = 0;
1517 connSynAck8.hdr.protoVer[1] = 8;
1519 //+++ Fill static data in connSynAck +++
1520 // TODO Move this code. It must be executed only once
1521 connSynAck8.len = Min8(sizeof(CONN_SYN_ACK_8));
1522 strcpy((char*)connSynAck8.type, "CONN_SYN_ACK");
1523 for (int j = 0; j < DIR_NUM; j++)
1525 strncpy((char*)connSynAck8.dirName[j],
1526 stgSettings->GetDirName(j).c_str(),
1529 connSynAck8.dirName[j][sizeof(string16) - 1] = 0;
1531 //--- Fill static data in connSynAck ---
1533 iaUser->rnd = random();
1534 connSynAck8.rnd = iaUser->rnd;
1536 connSynAck8.userTimeOut = iaSettings.GetUserTimeout();
1537 connSynAck8.aliveDelay = iaSettings.GetUserDelay();
1540 SwapBytes(connSynAck8.len);
1541 SwapBytes(connSynAck8.rnd);
1542 SwapBytes(connSynAck8.userTimeOut);
1543 SwapBytes(connSynAck8.aliveDelay);
1546 Encrypt(&iaUser->ctx, (char*)&connSynAck8, (char*)&connSynAck8, Min8(sizeof(CONN_SYN_ACK_8))/8);
1547 return Send(sip, iaUser->port, (char*)&connSynAck8, Min8(sizeof(CONN_SYN_ACK_8)));
1548 //return Send(sip, iaUser->port, (char*)&connSynAck8, 384);
1550 //-----------------------------------------------------------------------------
1551 int AUTH_IA::Send_ALIVE_SYN_6(IA_USER * iaUser, uint32_t sip)
1553 aliveSyn6.len = Min8(sizeof(ALIVE_SYN_6));
1554 aliveSyn6.rnd = iaUser->rnd = random();
1556 strcpy((char*)aliveSyn6.type, "ALIVE_SYN");
1558 for (int i = 0; i < DIR_NUM; i++)
1560 aliveSyn6.md[i] = iaUser->user->property.down.Get()[i];
1561 aliveSyn6.mu[i] = iaUser->user->property.up.Get()[i];
1563 aliveSyn6.sd[i] = iaUser->user->GetSessionDownload()[i];
1564 aliveSyn6.su[i] = iaUser->user->GetSessionUpload()[i];
1568 int dn = iaSettings.GetFreeMbShowType();
1569 const TARIFF * tf = iaUser->user->GetTariff();
1573 double p = tf->GetPriceWithTraffType(aliveSyn6.mu[dn],
1580 snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "---");
1584 double fmb = iaUser->user->property.freeMb;
1585 fmb = fmb < 0 ? 0 : fmb;
1586 snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "%.3f", fmb / p);
1591 if (freeMbNone == iaSettings.GetFreeMbShowType())
1593 aliveSyn6.freeMb[0] = 0;
1597 double fmb = iaUser->user->property.freeMb;
1598 fmb = fmb < 0 ? 0 : fmb;
1599 snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "C%.3f", fmb);
1604 if (iaUser->aliveSent)
1606 printfd(__FILE__, "========= ALIVE_ACK_6(7) TIMEOUT !!! %s =========\n", iaUser->user->GetLogin().c_str());
1608 iaUser->aliveSent = true;
1611 aliveSyn6.cash =(int64_t) (iaUser->user->property.cash.Get() * 1000.0);
1612 if (!stgSettings->GetShowFeeInCash())
1613 aliveSyn6.cash -= (int64_t)(tf->GetFee() * 1000.0);
1616 SwapBytes(aliveSyn6.len);
1617 SwapBytes(aliveSyn6.rnd);
1618 SwapBytes(aliveSyn6.cash);
1619 for (int i = 0; i < DIR_NUM; ++i)
1621 SwapBytes(aliveSyn6.mu[i]);
1622 SwapBytes(aliveSyn6.md[i]);
1623 SwapBytes(aliveSyn6.su[i]);
1624 SwapBytes(aliveSyn6.sd[i]);
1628 Encrypt(&(iaUser->ctx), (char*)&aliveSyn6, (char*)&aliveSyn6, Min8(sizeof(aliveSyn6))/8);
1629 return Send(sip, iaSettings.GetUserPort(), (char*)&aliveSyn6, Min8(sizeof(aliveSyn6)));
1631 //-----------------------------------------------------------------------------
1632 int AUTH_IA::Send_ALIVE_SYN_7(IA_USER * iaUser, uint32_t sip)
1634 return Send_ALIVE_SYN_6(iaUser, sip);
1636 //-----------------------------------------------------------------------------
1637 int AUTH_IA::Send_ALIVE_SYN_8(IA_USER * iaUser, uint32_t sip)
1639 strcpy((char*)aliveSyn8.hdr.magic, IA_ID);
1640 aliveSyn8.hdr.protoVer[0] = 0;
1641 aliveSyn8.hdr.protoVer[1] = 8;
1643 aliveSyn8.len = Min8(sizeof(ALIVE_SYN_8));
1644 aliveSyn8.rnd = iaUser->rnd = random();
1646 strcpy((char*)aliveSyn8.type, "ALIVE_SYN");
1648 for (int i = 0; i < DIR_NUM; i++)
1650 aliveSyn8.md[i] = iaUser->user->property.down.Get()[i];
1651 aliveSyn8.mu[i] = iaUser->user->property.up.Get()[i];
1653 aliveSyn8.sd[i] = iaUser->user->GetSessionDownload()[i];
1654 aliveSyn8.su[i] = iaUser->user->GetSessionUpload()[i];
1658 int dn = iaSettings.GetFreeMbShowType();
1662 const TARIFF * tf = iaUser->user->GetTariff();
1663 double p = tf->GetPriceWithTraffType(aliveSyn8.mu[dn],
1670 snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "---");
1674 double fmb = iaUser->user->property.freeMb;
1675 fmb = fmb < 0 ? 0 : fmb;
1676 snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "%.3f", fmb / p);
1681 if (freeMbNone == iaSettings.GetFreeMbShowType())
1683 aliveSyn8.freeMb[0] = 0;
1687 double fmb = iaUser->user->property.freeMb;
1688 fmb = fmb < 0 ? 0 : fmb;
1689 snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "C%.3f", fmb);
1694 if (iaUser->aliveSent)
1696 printfd(__FILE__, "========= ALIVE_ACK_8 TIMEOUT !!! =========\n");
1698 iaUser->aliveSent = true;
1701 const TARIFF * tf = iaUser->user->GetTariff();
1703 aliveSyn8.cash =(int64_t) (iaUser->user->property.cash.Get() * 1000.0);
1704 if (!stgSettings->GetShowFeeInCash())
1705 aliveSyn8.cash -= (int64_t)(tf->GetFee() * 1000.0);
1708 SwapBytes(aliveSyn8.len);
1709 SwapBytes(aliveSyn8.rnd);
1710 SwapBytes(aliveSyn8.cash);
1711 SwapBytes(aliveSyn8.status);
1712 for (int i = 0; i < DIR_NUM; ++i)
1714 SwapBytes(aliveSyn8.mu[i]);
1715 SwapBytes(aliveSyn8.md[i]);
1716 SwapBytes(aliveSyn8.su[i]);
1717 SwapBytes(aliveSyn8.sd[i]);
1721 Encrypt(&(iaUser->ctx), (char*)&aliveSyn8, (char*)&aliveSyn8, Min8(sizeof(aliveSyn8))/8);
1722 return Send(sip, iaUser->port, (char*)&aliveSyn8, Min8(sizeof(aliveSyn8)));
1724 //-----------------------------------------------------------------------------
1725 int AUTH_IA::Send_DISCONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip)
1727 disconnSynAck6.len = Min8(sizeof(DISCONN_SYN_ACK_6));
1728 strcpy((char*)disconnSynAck6.type, "DISCONN_SYN_ACK");
1729 disconnSynAck6.rnd = iaUser->rnd = random();
1732 SwapBytes(disconnSynAck6.len);
1733 SwapBytes(disconnSynAck6.rnd);
1736 Encrypt(&iaUser->ctx, (char*)&disconnSynAck6, (char*)&disconnSynAck6, Min8(sizeof(disconnSynAck6))/8);
1737 return Send(sip, iaSettings.GetUserPort(), (char*)&disconnSynAck6, Min8(sizeof(disconnSynAck6)));
1739 //-----------------------------------------------------------------------------
1740 int AUTH_IA::Send_DISCONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip)
1742 return Send_DISCONN_SYN_ACK_6(iaUser, sip);
1744 //-----------------------------------------------------------------------------
1745 int AUTH_IA::Send_DISCONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip)
1747 strcpy((char*)disconnSynAck8.hdr.magic, IA_ID);
1748 disconnSynAck8.hdr.protoVer[0] = 0;
1749 disconnSynAck8.hdr.protoVer[1] = 8;
1751 disconnSynAck8.len = Min8(sizeof(DISCONN_SYN_ACK_8));
1752 strcpy((char*)disconnSynAck8.type, "DISCONN_SYN_ACK");
1753 disconnSynAck8.rnd = iaUser->rnd = random();
1756 SwapBytes(disconnSynAck8.len);
1757 SwapBytes(disconnSynAck8.rnd);
1760 Encrypt(&iaUser->ctx, (char*)&disconnSynAck8, (char*)&disconnSynAck8, Min8(sizeof(disconnSynAck8))/8);
1761 return Send(sip, iaUser->port, (char*)&disconnSynAck8, Min8(sizeof(disconnSynAck8)));
1763 //-----------------------------------------------------------------------------
1764 int AUTH_IA::Send_FIN_6(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1766 fin6.len = Min8(sizeof(FIN_6));
1767 strcpy((char*)fin6.type, "FIN");
1768 strcpy((char*)fin6.ok, "OK");
1771 SwapBytes(fin6.len);
1774 Encrypt(&iaUser->ctx, (char*)&fin6, (char*)&fin6, Min8(sizeof(fin6))/8);
1776 iaUser->user->Unauthorize(this);
1778 int ret = Send(sip, iaSettings.GetUserPort(), (char*)&fin6, Min8(sizeof(fin6)));
1782 //-----------------------------------------------------------------------------
1783 int AUTH_IA::Send_FIN_7(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1785 return Send_FIN_6(iaUser, sip, it);
1787 //-----------------------------------------------------------------------------
1788 int AUTH_IA::Send_FIN_8(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1790 strcpy((char*)fin8.hdr.magic, IA_ID);
1791 fin8.hdr.protoVer[0] = 0;
1792 fin8.hdr.protoVer[1] = 8;
1794 fin8.len = Min8(sizeof(FIN_8));
1795 strcpy((char*)fin8.type, "FIN");
1796 strcpy((char*)fin8.ok, "OK");
1799 SwapBytes(fin8.len);
1802 Encrypt(&iaUser->ctx, (char*)&fin8, (char*)&fin8, Min8(sizeof(fin8))/8);
1804 iaUser->user->Unauthorize(this);
1806 int ret = Send(sip, iaUser->port, (char*)&fin8, Min8(sizeof(fin8)));
1810 //-----------------------------------------------------------------------------
1811 bool AUTH_IA::WaitPackets(int sd) const
1819 tv.tv_usec = 500000;
1821 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
1822 if (res == -1) // Error
1826 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
1831 if (res == 0) // Timeout