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>
25 #include "inetaccess.h"
27 #include "stg/common.h"
28 #include "stg/locker.h"
29 #include "stg/tariff.h"
30 #include "stg/settings.h"
35 #include <cstdio> // snprintf
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <unistd.h> // close
43 #define IA_PROTO_VER (6)
45 extern volatile time_t stgTime;
47 extern "C" STG::Plugin* GetPlugin()
49 static AUTH_IA plugin;
52 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
55 AUTH_IA_SETTINGS::AUTH_IA_SETTINGS()
59 freeMbShowType(freeMbCash),
60 logProtocolErrors(false)
63 //-----------------------------------------------------------------------------
64 int AUTH_IA_SETTINGS::ParseSettings(const STG::ModuleSettings & s)
68 std::vector<STG::ParamValue>::const_iterator pvi;
69 ///////////////////////////
71 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
72 if (pvi == s.moduleParams.end() || pvi->value.empty())
74 errorStr = "Parameter \'Port\' not found.";
75 printfd(__FILE__, "Parameter 'Port' not found\n");
78 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
80 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
81 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
84 port = static_cast<uint16_t>(p);
85 ///////////////////////////
86 pv.param = "UserDelay";
87 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
88 if (pvi == s.moduleParams.end() || pvi->value.empty())
90 errorStr = "Parameter \'UserDelay\' not found.";
91 printfd(__FILE__, "Parameter 'UserDelay' not found\n");
95 if (ParseIntInRange(pvi->value[0], 5, 600, &userDelay))
97 errorStr = "Cannot parse parameter \'UserDelay\': " + errorStr;
98 printfd(__FILE__, "Cannot parse parameter 'UserDelay'\n");
101 ///////////////////////////
102 pv.param = "UserTimeout";
103 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
104 if (pvi == s.moduleParams.end() || pvi->value.empty())
106 errorStr = "Parameter \'UserTimeout\' not found.";
107 printfd(__FILE__, "Parameter 'UserTimeout' not found\n");
111 if (ParseIntInRange(pvi->value[0], 15, 1200, &userTimeout))
113 errorStr = "Cannot parse parameter \'UserTimeout\': " + errorStr;
114 printfd(__FILE__, "Cannot parse parameter 'UserTimeout'\n");
117 ///////////////////////////
118 pv.param = "LogProtocolErrors";
119 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
120 if (pvi == s.moduleParams.end() || pvi->value.empty())
121 logProtocolErrors = false;
122 else if (ParseYesNo(pvi->value[0], &logProtocolErrors))
124 errorStr = "Cannot parse parameter \'LogProtocolErrors\': " + errorStr;
125 printfd(__FILE__, "Cannot parse parameter 'LogProtocolErrors'\n");
128 /////////////////////////////////////////////////////////////
129 std::string freeMbType;
132 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
133 if (pvi == s.moduleParams.end() || pvi->value.empty())
135 errorStr = "Parameter \'FreeMb\' not found.";
136 printfd(__FILE__, "Parameter 'FreeMb' not found\n");
139 freeMbType = pvi->value[0];
141 if (strcasecmp(freeMbType.c_str(), "cash") == 0)
143 freeMbShowType = freeMbCash;
145 else if (strcasecmp(freeMbType.c_str(), "none") == 0)
147 freeMbShowType = freeMbNone;
149 else if (!str2x(freeMbType.c_str(), n))
151 if (n < 0 || n >= DIR_NUM)
153 errorStr = "Incorrect parameter \'" + freeMbType + "\'.";
154 printfd(__FILE__, "%s\n", errorStr.c_str());
157 freeMbShowType = static_cast<FREEMB>(n);
161 errorStr = "Incorrect parameter \'" + freeMbType + "\'.";
162 printfd(__FILE__, "%s\n", errorStr.c_str());
165 /////////////////////////////////////////////////////////////
168 //-----------------------------------------------------------------------------
169 //-----------------------------------------------------------------------------
170 //-----------------------------------------------------------------------------
171 #ifdef IA_PHASE_DEBUG
176 gettimeofday(&phaseTime, NULL);
182 gettimeofday(&phaseTime, NULL);
185 //-----------------------------------------------------------------------------
186 IA_PHASE::~IA_PHASE()
188 #ifdef IA_PHASE_DEBUG
189 flog = fopen(log.c_str(), "at");
192 fprintf(flog, "IA %s D\n", login.c_str());
197 //-----------------------------------------------------------------------------
198 #ifdef IA_PHASE_DEBUG
199 void IA_PHASE::SetLogFileName(const string & logFileName)
201 log = logFileName + ".ia.log";
203 //-----------------------------------------------------------------------------
204 void IA_PHASE::SetUserLogin(const string & login)
206 IA_PHASE::login = login;
208 //-----------------------------------------------------------------------------
209 void IA_PHASE::WritePhaseChange(int newPhase)
212 gettimeofday(&newPhaseTime, NULL);
213 flog = fopen(log.c_str(), "at");
216 string action = newPhase == phase ? "U" : "C";
217 double delta = newPhaseTime.GetSec() - phaseTime.GetSec();
218 delta += (newPhaseTime.GetUSec() - phaseTime.GetUSec()) * 1.0e-6;
219 fprintf(flog, "IA %s %s oldPhase = %d, newPhase = %d. dt = %.6f\n",
229 //-----------------------------------------------------------------------------
230 void IA_PHASE::SetPhase1()
232 #ifdef IA_PHASE_DEBUG
236 gettimeofday(&phaseTime, NULL);
238 //-----------------------------------------------------------------------------
239 void IA_PHASE::SetPhase2()
241 #ifdef IA_PHASE_DEBUG
245 gettimeofday(&phaseTime, NULL);
247 //-----------------------------------------------------------------------------
248 void IA_PHASE::SetPhase3()
250 #ifdef IA_PHASE_DEBUG
254 gettimeofday(&phaseTime, NULL);
256 //-----------------------------------------------------------------------------
257 void IA_PHASE::SetPhase4()
259 #ifdef IA_PHASE_DEBUG
263 gettimeofday(&phaseTime, NULL);
265 //-----------------------------------------------------------------------------
266 int IA_PHASE::GetPhase() const
270 //-----------------------------------------------------------------------------
271 void IA_PHASE::UpdateTime()
273 #ifdef IA_PHASE_DEBUG
274 WritePhaseChange(phase);
276 gettimeofday(&phaseTime, NULL);
278 //-----------------------------------------------------------------------------
279 const UTIME & IA_PHASE::GetTime() const
283 //-----------------------------------------------------------------------------
284 //-----------------------------------------------------------------------------
285 //-----------------------------------------------------------------------------
287 : isRunningRun(false),
288 isRunningRunTimeouter(false),
292 enabledDirs(0xFFffFFff),
293 logger(STG::PluginLogger::get("auth_ia"))
295 InitContext("pr7Hhen", 7, &ctxS);
297 memset(&connSynAck6, 0, sizeof(CONN_SYN_ACK_6));
298 memset(&connSynAck8, 0, sizeof(CONN_SYN_ACK_8));
299 memset(&disconnSynAck6, 0, sizeof(DISCONN_SYN_ACK_6));
300 memset(&disconnSynAck8, 0, sizeof(DISCONN_SYN_ACK_8));
301 memset(&aliveSyn6, 0, sizeof(ALIVE_SYN_6));
302 memset(&aliveSyn8, 0, sizeof(ALIVE_SYN_8));
303 memset(&fin6, 0, sizeof(FIN_6));
304 memset(&fin8, 0, sizeof(FIN_8));
306 printfd(__FILE__, "sizeof(CONN_SYN_6) = %d %d\n", sizeof(CONN_SYN_6), Min8(sizeof(CONN_SYN_6)));
307 printfd(__FILE__, "sizeof(CONN_SYN_8) = %d %d\n", sizeof(CONN_SYN_8), Min8(sizeof(CONN_SYN_8)));
308 printfd(__FILE__, "sizeof(CONN_SYN_ACK_6) = %d %d\n", sizeof(CONN_SYN_ACK_6), Min8(sizeof(CONN_SYN_ACK_6)));
309 printfd(__FILE__, "sizeof(CONN_SYN_ACK_8) = %d %d\n", sizeof(CONN_SYN_ACK_8), Min8(sizeof(CONN_SYN_ACK_8)));
310 printfd(__FILE__, "sizeof(CONN_ACK_6) = %d %d\n", sizeof(CONN_ACK_6), Min8(sizeof(CONN_ACK_6)));
311 printfd(__FILE__, "sizeof(ALIVE_SYN_6) = %d %d\n", sizeof(ALIVE_SYN_6), Min8(sizeof(ALIVE_SYN_6)));
312 printfd(__FILE__, "sizeof(ALIVE_SYN_8) = %d %d\n", sizeof(ALIVE_SYN_8), Min8(sizeof(ALIVE_SYN_8)));
313 printfd(__FILE__, "sizeof(ALIVE_ACK_6) = %d %d\n", sizeof(ALIVE_ACK_6), Min8(sizeof(ALIVE_ACK_6)));
314 printfd(__FILE__, "sizeof(DISCONN_SYN_6) = %d %d\n", sizeof(DISCONN_SYN_6), Min8(sizeof(DISCONN_SYN_6)));
315 printfd(__FILE__, "sizeof(DISCONN_SYN_ACK_6) = %d %d\n", sizeof(DISCONN_SYN_ACK_6), Min8(sizeof(DISCONN_SYN_ACK_6)));
316 printfd(__FILE__, "sizeof(DISCONN_SYN_ACK_8) = %d %d\n", sizeof(DISCONN_SYN_ACK_8), Min8(sizeof(DISCONN_SYN_ACK_8)));
317 printfd(__FILE__, "sizeof(DISCONN_ACK_6) = %d %d\n", sizeof(DISCONN_ACK_6), Min8(sizeof(DISCONN_ACK_6)));
318 printfd(__FILE__, "sizeof(FIN_6) = %d %d\n", sizeof(FIN_6), Min8(sizeof(FIN_6)));
319 printfd(__FILE__, "sizeof(FIN_8) = %d %d\n", sizeof(FIN_8), Min8(sizeof(FIN_8)));
320 printfd(__FILE__, "sizeof(ERR) = %d %d\n", sizeof(ERR), Min8(sizeof(ERR)));
321 printfd(__FILE__, "sizeof(INFO_6) = %d %d\n", sizeof(INFO_6), Min8(sizeof(INFO_6)));
322 printfd(__FILE__, "sizeof(INFO_7) = %d %d\n", sizeof(INFO_7), Min8(sizeof(INFO_7)));
323 printfd(__FILE__, "sizeof(INFO_8) = %d %d\n", sizeof(INFO_8), Min8(sizeof(INFO_8)));
325 packetTypes["CONN_SYN"] = CONN_SYN_N;
326 packetTypes["CONN_SYN_ACK"] = CONN_SYN_ACK_N;
327 packetTypes["CONN_ACK"] = CONN_ACK_N;
328 packetTypes["ALIVE_SYN"] = ALIVE_SYN_N;
329 packetTypes["ALIVE_ACK"] = ALIVE_ACK_N;
330 packetTypes["DISCONN_SYN"] = DISCONN_SYN_N;
331 packetTypes["DISCONN_SYN_ACK"] = DISCONN_SYN_ACK_N;
332 packetTypes["DISCONN_ACK"] = DISCONN_ACK_N;
333 packetTypes["FIN"] = FIN_N;
334 packetTypes["ERR"] = ERROR_N;
336 //-----------------------------------------------------------------------------
340 //-----------------------------------------------------------------------------
343 m_onDelUserConn = users->onDel([this](auto user){ DelUser(user); });
348 if (!m_thread.joinable())
349 m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
351 if (!m_timeouterThread.joinable())
352 m_timeouterThread = std::jthread([this](auto token){ RunTimeouter(std::move(token)); });
357 //-----------------------------------------------------------------------------
363 m_thread.request_stop();
364 m_timeouterThread.request_stop();
369 UnauthorizeUser(this)
374 //5 seconds to thread stops itself
375 for (int i = 0; i < 25 && isRunningRun; i++)
377 struct timespec ts = {0, 200000000};
378 nanosleep(&ts, NULL);
384 if (isRunningRunTimeouter)
386 //5 seconds to thread stops itself
387 for (int i = 0; i < 25 && isRunningRunTimeouter; i++)
389 struct timespec ts = {0, 200000000};
390 nanosleep(&ts, NULL);
394 m_onDelUserConn.disconnect();
401 if (isRunningRunTimeouter)
402 m_timeouterThread.detach();
404 m_timeouterThread.join();
406 if (isRunningRun || isRunningRunTimeouter)
409 printfd(__FILE__, "AUTH_IA::Stoped successfully.\n");
412 //-----------------------------------------------------------------------------
413 void AUTH_IA::Run(std::stop_token token)
416 sigfillset(&signalSet);
417 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
423 time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
425 while (!token.stop_requested())
427 RecvData(buffer, sizeof(buffer));
428 if ((touchTime + MONITOR_TIME_DELAY_SEC <= stgTime) && stgSettings->GetMonitoring())
431 std::string monFile = stgSettings->GetMonitorDir() + "/inetaccess_r";
436 isRunningRun = false;
438 //-----------------------------------------------------------------------------
439 void AUTH_IA::RunTimeouter(std::stop_token token)
442 sigfillset(&signalSet);
443 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
445 isRunningRunTimeouter = true;
448 std::string monFile = stgSettings->GetMonitorDir() + "/inetaccess_t";
449 while (!token.stop_requested())
451 struct timespec ts = {0, 20000000};
452 nanosleep(&ts, NULL);
454 // TODO change counter to timer and MONITOR_TIME_DELAY_SEC
455 if (++a % (50 * 60) == 0 && stgSettings->GetMonitoring())
461 isRunningRunTimeouter = false;
463 //-----------------------------------------------------------------------------
464 int AUTH_IA::ParseSettings()
466 int ret = iaSettings.ParseSettings(settings);
468 errorStr = iaSettings.GetStrError();
471 //-----------------------------------------------------------------------------
472 int AUTH_IA::Reload(const STG::ModuleSettings & ms)
474 AUTH_IA_SETTINGS newIaSettings;
475 if (newIaSettings.ParseSettings(ms))
477 printfd(__FILE__, "AUTH_IA::Reload() - Failed to reload InetAccess.\n");
478 logger("AUTH_IA: Cannot reload InetAccess. Errors found.");
482 printfd(__FILE__, "AUTH_IA::Reload() - Reloaded InetAccess successfully.\n");
483 logger("AUTH_IA: Reloaded InetAccess successfully.");
484 iaSettings = newIaSettings;
487 //-----------------------------------------------------------------------------
488 int AUTH_IA::PrepareNet()
490 struct sockaddr_in listenAddr;
492 listenSocket = socket(AF_INET, SOCK_DGRAM, 0);
494 if (listenSocket < 0)
496 errorStr = "Cannot create socket.";
497 logger("Cannot create a socket: %s", strerror(errno));
501 listenAddr.sin_family = AF_INET;
502 listenAddr.sin_port = htons(iaSettings.GetUserPort());
503 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
505 if (bind(listenSocket, reinterpret_cast<sockaddr*>(&listenAddr), sizeof(listenAddr)) < 0)
507 errorStr = "AUTH_IA: Bind failed.";
508 logger("Cannot bind the socket: %s", strerror(errno));
514 //-----------------------------------------------------------------------------
515 int AUTH_IA::FinalizeNet()
520 //-----------------------------------------------------------------------------
521 int AUTH_IA::RecvData(char * buffer, int bufferSize)
523 if (!WaitPackets(listenSocket)) // Timeout
528 struct sockaddr_in outerAddr;
529 socklen_t outerAddrLen(sizeof(outerAddr));
530 ssize_t dataLen = recvfrom(listenSocket, buffer, bufferSize, 0, reinterpret_cast<sockaddr *>(&outerAddr), &outerAddrLen);
537 if (dataLen <= 0) // Error
541 printfd(__FILE__, "recvfrom res=%d, error: '%s'\n", dataLen, strerror(errno));
542 logger("recvfrom error: %s", strerror(errno));
551 uint32_t sip = outerAddr.sin_addr.s_addr;
552 uint16_t sport = htons(outerAddr.sin_port);
555 if (CheckHeader(buffer, sip, &protoVer))
558 char login[PASSWD_LEN]; //TODO why PASSWD_LEN ?
559 memset(login, 0, PASSWD_LEN);
561 DecryptString(login, buffer + 8, PASSWD_LEN, &ctxS);
564 if (users->FindByName(login, &user))
566 logger("User's connect failed: user '%s' not found. IP %s",
568 inet_ntostring(sip).c_str());
569 printfd(__FILE__, "User '%s' NOT found!\n", login);
570 SendError(sip, sport, protoVer, IconvString("Неправильный логин.", "utf8", "koi8-ru"));
574 printfd(__FILE__, "User '%s' FOUND!\n", user->GetLogin().c_str());
576 if (user->GetProperties().disabled.Get())
578 logger("Cannont authorize '%s', user is disabled.", login);
579 SendError(sip, sport, protoVer, IconvString("Учетная запись заблокирована.", "utf8", "koi8-ru"));
583 if (user->GetProperties().passive.Get())
585 logger("Cannont authorize '%s', user is passive.", login);
586 SendError(sip, sport, protoVer, IconvString("Учетная запись заморожена.", "utf8", "koi8-ru"));
590 if (!user->GetProperties().ips.Get().find(sip))
592 printfd(__FILE__, "User %s. IP address is incorrect. IP %s\n",
593 user->GetLogin().c_str(), inet_ntostring(sip).c_str());
594 logger("User %s. IP address is incorrect. IP %s",
595 user->GetLogin().c_str(), inet_ntostring(sip).c_str());
596 SendError(sip, sport, protoVer, IconvString("Пользователь не опознан. Проверьте IP-адрес.", "utf8", "koi8-ru"));
600 return PacketProcessor(buffer, dataLen, sip, sport, protoVer, user);
602 //-----------------------------------------------------------------------------
603 int AUTH_IA::CheckHeader(const char * buffer, uint32_t sip, int * protoVer)
605 if (strncmp(IA_ID, buffer, strlen(IA_ID)) != 0)
607 printfd(__FILE__, "update needed - IA_ID\n");
608 if (iaSettings.LogProtocolErrors())
609 logger("IP: %s. Header: invalid packed signature.", inet_ntostring(sip).c_str());
613 if (buffer[6] != 0) //proto[0] shoud be 0
615 printfd(__FILE__, "update needed - PROTO major: %d\n", buffer[6]);
616 if (iaSettings.LogProtocolErrors())
617 logger("IP: %s. Header: invalid protocol major version: %d.", inet_ntostring(sip).c_str(), buffer[6]);
624 printfd(__FILE__, "update needed - PROTO minor: %d\n", buffer[7]);
625 if (iaSettings.LogProtocolErrors())
626 logger("IP: %s. Header: invalid protocol minor version: %d.", inet_ntostring(sip).c_str(), buffer[7]);
631 *protoVer = buffer[7];
635 //-----------------------------------------------------------------------------
636 int AUTH_IA::Timeouter()
638 std::lock_guard lock(m_mutex);
640 std::map<uint32_t, IA_USER>::iterator it;
641 it = ip2user.begin();
643 while (it != ip2user.end())
645 uint32_t sip = it->first;
647 static UTIME currTime;
648 gettimeofday(&currTime, NULL);
650 if ((it->second.phase.GetPhase() == 2)
651 && (currTime - it->second.phase.GetTime()) > iaSettings.GetUserDelay())
653 if (iaSettings.LogProtocolErrors())
654 logger("User '%s'. Protocol version: %d. Phase 2: connect request timeout (%f > %d).", it->second.login.c_str(), it->second.protoVer, (currTime - it->second.phase.GetTime()).AsDouble(), iaSettings.GetUserDelay().GetSec());
655 it->second.phase.SetPhase1();
656 printfd(__FILE__, "Phase changed from 2 to 1. Reason: timeout\n");
661 if (it->second.phase.GetPhase() == 3)
663 if (!it->second.messagesToSend.empty())
665 if (it->second.protoVer == 6)
666 RealSendMessage6(*it->second.messagesToSend.begin(), sip, it->second);
668 if (it->second.protoVer == 7)
669 RealSendMessage7(*it->second.messagesToSend.begin(), sip, it->second);
671 if (it->second.protoVer == 8)
672 RealSendMessage8(*it->second.messagesToSend.begin(), sip, it->second);
674 it->second.messagesToSend.erase(it->second.messagesToSend.begin());
677 if((currTime - it->second.lastSendAlive) > iaSettings.GetUserDelay())
679 switch (it->second.protoVer)
682 Send_ALIVE_SYN_6(&(it->second), sip);
685 Send_ALIVE_SYN_7(&(it->second), sip);
688 Send_ALIVE_SYN_8(&(it->second), sip);
692 gettimeofday(&it->second.lastSendAlive, NULL);
695 if ((currTime - it->second.phase.GetTime()) > iaSettings.GetUserTimeout())
697 if (iaSettings.LogProtocolErrors())
698 logger("User '%s'. Protocol version: %d. Phase 3: alive timeout (%f > %d).", it->second.login.c_str(), it->second.protoVer, (currTime - it->second.phase.GetTime()).AsDouble(), iaSettings.GetUserTimeout().GetSec());
699 users->Unauthorize(it->second.user->GetLogin(), this);
705 if ((it->second.phase.GetPhase() == 4)
706 && ((currTime - it->second.phase.GetTime()) > iaSettings.GetUserDelay()))
708 if (iaSettings.LogProtocolErrors())
709 logger("User '%s'. Protocol version: %d. Phase 4: disconnect request timeout (%f > %d).", it->second.login.c_str(), it->second.protoVer, (currTime - it->second.phase.GetTime()).AsDouble(), iaSettings.GetUserDelay().GetSec());
710 it->second.phase.SetPhase3();
711 printfd(__FILE__, "Phase changed from 4 to 3. Reason: timeout\n");
719 //-----------------------------------------------------------------------------
720 int AUTH_IA::PacketProcessor(void * buff, size_t dataLen, uint32_t sip, uint16_t sport, int protoVer, UserPtr user)
722 std::string login(user->GetLogin());
723 const size_t offset = LOGIN_LEN + 2 + 6; // LOGIN_LEN + sizeOfMagic + sizeOfVer;
725 std::lock_guard lock(m_mutex);
726 std::map<uint32_t, IA_USER>::iterator it(ip2user.find(sip));
728 if (it == ip2user.end())
731 if (!users->FindByIPIdx(sip, &userPtr))
733 if (userPtr->GetID() != user->GetID())
735 printfd(__FILE__, "IP address already in use by user '%s'. IP %s, login: '%s'\n",
736 userPtr->GetLogin().c_str(),
737 inet_ntostring(sip).c_str(),
739 logger("IP address is already in use by user '%s'. IP %s, login: '%s'",
740 userPtr->GetLogin().c_str(),
741 inet_ntostring(sip).c_str(),
743 SendError(sip, sport, protoVer, IconvString("IP-адрес уже сипользуется.", "utf8", "koi8-ru"));
748 printfd(__FILE__, "Add new user '%s' from ip %s\n",
749 login.c_str(), inet_ntostring(sip).c_str());
750 std::pair<std::map<uint32_t, IA_USER>::iterator, bool> res;
751 res = ip2user.insert(std::make_pair(sip, IA_USER(login, user, sport, protoVer)));
753 #ifdef IA_PHASE_DEBUG
754 it->second.phase.SetLogFileName(stgSettings->GetLogFileName());
755 it->second.phase.SetUserLogin(login);
758 else if (user->GetID() != it->second.user->GetID())
760 printfd(__FILE__, "IP address already in use by user '%s'. IP %s, login: '%s'\n",
761 it->second.user->GetLogin().c_str(),
762 inet_ntostring(sip).c_str(),
763 user->GetLogin().c_str());
764 logger("IP address is already in use by user '%s'. IP %s, login: '%s'",
765 it->second.user->GetLogin().c_str(),
766 inet_ntostring(sip).c_str(),
767 user->GetLogin().c_str());
768 SendError(sip, sport, protoVer, IconvString("IP-адрес уже используется.", "utf8", "koi8-ru"));
772 IA_USER * iaUser = &(it->second);
774 if (iaUser->password != user->GetProperties().password.Get())
776 const std::string & password = user->GetProperties().password.Get();
777 InitContext(password.c_str(), password.length(), &iaUser->ctx);
778 iaUser->password = user->GetProperties().password.Get();
781 DecryptString(static_cast<char *>(buff) + offset, static_cast<char *>(buff) + offset, (dataLen - offset), &iaUser->ctx);
783 char packetName[IA_MAX_TYPE_LEN];
784 strncpy(packetName, static_cast<char *>(buff) + offset + 4, IA_MAX_TYPE_LEN);
785 packetName[IA_MAX_TYPE_LEN - 1] = 0;
787 std::map<std::string, int>::iterator pi(packetTypes.find(packetName));
788 if (pi == packetTypes.end())
790 SendError(sip, sport, protoVer, IconvString("Неправильный логин или пароль.", "utf8", "koi8-ru"));
791 printfd(__FILE__, "Login or password is wrong!\n");
792 logger("User's connect failed. User: '%s', ip %s. Wrong login or password",
794 inet_ntostring(sip).c_str());
799 if (user->IsAuthorizedBy(this) && user->GetCurrIP() != sip)
801 printfd(__FILE__, "Login %s already in use from ip %s. IP %s\n",
802 login.c_str(), inet_ntostring(user->GetCurrIP()).c_str(),
803 inet_ntostring(sip).c_str());
804 logger("Login '%s' is already in use from ip %s. IP %s",
806 inet_ntostring(user->GetCurrIP()).c_str(),
807 inet_ntostring(sip).c_str());
808 SendError(sip, sport, protoVer, IconvString("Логин уже используется.", "utf8", "koi8-ru"));
819 if (Process_CONN_SYN_6(static_cast<CONN_SYN_6 *>(buff), &(it->second), sip))
821 return Send_CONN_SYN_ACK_6(iaUser, sip);
823 if (Process_CONN_SYN_7(static_cast<CONN_SYN_7 *>(buff), &(it->second), sip))
825 return Send_CONN_SYN_ACK_7(iaUser, sip);
827 if (Process_CONN_SYN_8(static_cast<CONN_SYN_8 *>(buff), &(it->second), sip))
829 return Send_CONN_SYN_ACK_8(iaUser, sip);
837 if (Process_CONN_ACK_6(static_cast<CONN_ACK_6 *>(buff), iaUser, sip))
839 return Send_ALIVE_SYN_6(iaUser, sip);
841 if (Process_CONN_ACK_7(static_cast<CONN_ACK_6 *>(buff), iaUser, sip))
843 return Send_ALIVE_SYN_7(iaUser, sip);
845 if (Process_CONN_ACK_8(static_cast<CONN_ACK_8 *>(buff), iaUser, sip))
847 return Send_ALIVE_SYN_8(iaUser, sip);
855 return Process_ALIVE_ACK_6(static_cast<ALIVE_ACK_6 *>(buff), iaUser, sip);
857 return Process_ALIVE_ACK_7(static_cast<ALIVE_ACK_6 *>(buff), iaUser, sip);
859 return Process_ALIVE_ACK_8(static_cast<ALIVE_ACK_8 *>(buff), iaUser, sip);
867 if (Process_DISCONN_SYN_6(static_cast<DISCONN_SYN_6 *>(buff), iaUser, sip))
869 return Send_DISCONN_SYN_ACK_6(iaUser, sip);
871 if (Process_DISCONN_SYN_7(static_cast<DISCONN_SYN_6 *>(buff), iaUser, sip))
873 return Send_DISCONN_SYN_ACK_7(iaUser, sip);
875 if (Process_DISCONN_SYN_8(static_cast<DISCONN_SYN_8 *>(buff), iaUser, sip))
877 return Send_DISCONN_SYN_ACK_8(iaUser, sip);
885 if (Process_DISCONN_ACK_6(static_cast<DISCONN_ACK_6 *>(buff), iaUser, sip, it))
887 return Send_FIN_6(iaUser, sip, it);
889 if (Process_DISCONN_ACK_7(static_cast<DISCONN_ACK_6 *>(buff), iaUser, sip, it))
891 return Send_FIN_7(iaUser, sip, it);
893 if (Process_DISCONN_ACK_8(static_cast<DISCONN_ACK_8 *>(buff), iaUser, sip, it))
895 return Send_FIN_8(iaUser, sip, it);
902 //-----------------------------------------------------------------------------
903 void AUTH_IA::DelUser(UserPtr u)
906 uint32_t ip = u->GetCurrIP();
911 std::map<uint32_t, IA_USER>::iterator it;
913 std::lock_guard lock(m_mutex);
914 it = ip2user.find(ip);
915 if (it == ip2user.end())
918 printfd(__FILE__, "Nothing to delete\n");
922 if (it->second.user == u)
924 printfd(__FILE__, "User removed!\n");
925 users->Unauthorize(u->GetLogin(), this);
929 //-----------------------------------------------------------------------------
930 int AUTH_IA::SendError(uint32_t ip, uint16_t port, int protoVer, const std::string & text)
932 struct sockaddr_in sendAddr;
939 memset(&err, 0, sizeof(ERR));
941 sendAddr.sin_family = AF_INET;
942 sendAddr.sin_port = htons(port);
943 sendAddr.sin_addr.s_addr = ip;
946 memcpy(err.type, "ERR", 3);
947 memcpy(err.text, text.c_str(), std::min<size_t>(text.length(), MAX_MSG_LEN));
953 res = sendto(listenSocket, &err, sizeof(err), 0, reinterpret_cast<sockaddr*>(&sendAddr), sizeof(sendAddr));
954 printfd(__FILE__, "SendError %d bytes sent\n", res);
959 memset(&err8, 0, sizeof(ERR_8));
961 sendAddr.sin_family = AF_INET;
962 sendAddr.sin_port = htons(port);
963 sendAddr.sin_addr.s_addr = ip;
966 memcpy(err8.type, "ERR", 3);
967 memcpy(err8.text, text.c_str(), std::min<size_t>(text.length(), MAX_MSG_LEN));
973 res = sendto(listenSocket, &err8, sizeof(err8), 0, reinterpret_cast<sockaddr*>(&sendAddr), sizeof(sendAddr));
974 printfd(__FILE__, "SendError_8 %d bytes sent\n", res);
980 //-----------------------------------------------------------------------------
981 int AUTH_IA::Send(uint32_t ip, uint16_t port, const void* buffer, size_t len)
983 struct sockaddr_in sendAddr;
985 sendAddr.sin_family = AF_INET;
986 sendAddr.sin_port = htons(port);
987 sendAddr.sin_addr.s_addr = ip;
989 if (sendto(listenSocket, buffer, len, 0, reinterpret_cast<sockaddr*>(&sendAddr), sizeof(sendAddr)) == static_cast<ssize_t>(len))
994 //-----------------------------------------------------------------------------
995 int AUTH_IA::SendMessage(const STG::Message & msg, uint32_t ip) const
997 printfd(__FILE__, "SendMessage userIP=%s\n", inet_ntostring(ip).c_str());
999 std::map<uint32_t, IA_USER>::iterator it;
1001 std::lock_guard lock(m_mutex);
1002 it = ip2user.find(ip);
1003 if (it == ip2user.end())
1005 errorStr = "Unknown user.";
1008 it->second.messagesToSend.push_back(msg);
1011 //-----------------------------------------------------------------------------
1012 int AUTH_IA::RealSendMessage6(const STG::Message & msg, uint32_t ip, IA_USER & user)
1014 printfd(__FILE__, "RealSendMessage 6 user=%s\n", user.login.c_str());
1017 memset(&info, 0, sizeof(INFO_6));
1020 memcpy(info.type, "INFO", 4);
1021 info.infoType = 'I';
1022 memcpy(info.text, msg.text.c_str(), std::min<size_t>(msg.text.length(), 235));
1025 size_t len = info.len;
1027 SwapBytes(info.len);
1031 memcpy(buffer, &info, sizeof(INFO_6));
1032 EncryptString(buffer, buffer, len, &user.ctx);
1033 return Send(ip, iaSettings.GetUserPort(), buffer, len);
1035 //-----------------------------------------------------------------------------
1036 int AUTH_IA::RealSendMessage7(const STG::Message & msg, uint32_t ip, IA_USER & user)
1038 printfd(__FILE__, "RealSendMessage 7 user=%s\n", user.login.c_str());
1041 memset(&info, 0, sizeof(INFO_7));
1044 memcpy(info.type, "INFO_7", 6);
1045 info.infoType = static_cast<int8_t>(msg.header.type);
1046 info.showTime = static_cast<int8_t>(msg.header.showTime);
1047 info.sendTime = msg.header.creationTime;
1049 size_t len = info.len;
1051 SwapBytes(info.len);
1052 SwapBytes(info.sendTime);
1055 memcpy(info.text, msg.text.c_str(), std::min<size_t>(msg.text.length(), MAX_MSG_LEN - 1));
1056 info.text[MAX_MSG_LEN - 1] = 0;
1059 memcpy(buffer, &info, sizeof(INFO_7));
1061 EncryptString(buffer, buffer, len, &user.ctx);
1062 return Send(ip, iaSettings.GetUserPort(), buffer, len);
1064 //-----------------------------------------------------------------------------
1065 int AUTH_IA::RealSendMessage8(const STG::Message & msg, uint32_t ip, IA_USER & user)
1067 printfd(__FILE__, "RealSendMessage 8 user=%s\n", user.login.c_str());
1070 memset(&info, 0, sizeof(INFO_8));
1073 memcpy(info.type, "INFO_8", 6);
1074 info.infoType = static_cast<int8_t>(msg.header.type);
1075 info.showTime = static_cast<int8_t>(msg.header.showTime);
1076 info.sendTime = msg.header.creationTime;
1078 memcpy(info.text, msg.text.c_str(), std::min<size_t>(msg.text.length(), IA_MAX_MSG_LEN_8 - 1));
1079 info.text[IA_MAX_MSG_LEN_8 - 1] = 0;
1081 size_t len = info.len;
1083 SwapBytes(info.len);
1084 SwapBytes(info.sendTime);
1088 memcpy(buffer, &info, sizeof(INFO_8));
1090 EncryptString(buffer, buffer, len, &user.ctx);
1091 return Send(ip, user.port, buffer, len);
1093 //-----------------------------------------------------------------------------
1094 int AUTH_IA::Process_CONN_SYN_6(CONN_SYN_6 *, IA_USER * iaUser, uint32_t)
1096 if (!(iaUser->phase.GetPhase() == 1 || iaUser->phase.GetPhase() == 3))
1099 enabledDirs = 0xFFffFFff;
1101 iaUser->phase.SetPhase2();
1102 printfd(__FILE__, "Phase changed from %d to 2. Reason: CONN_SYN_6\n", iaUser->phase.GetPhase());
1105 //-----------------------------------------------------------------------------
1106 int AUTH_IA::Process_CONN_SYN_7(CONN_SYN_7 * connSyn, IA_USER * iaUser, uint32_t sip)
1108 return Process_CONN_SYN_6(connSyn, iaUser, sip);
1110 //-----------------------------------------------------------------------------
1111 int AUTH_IA::Process_CONN_SYN_8(CONN_SYN_8 * connSyn, IA_USER * iaUser, uint32_t sip)
1114 SwapBytes(connSyn->dirs);
1116 int ret = Process_CONN_SYN_6(reinterpret_cast<CONN_SYN_6 *>(connSyn), iaUser, sip);
1117 enabledDirs = connSyn->dirs;
1120 //-----------------------------------------------------------------------------
1121 int AUTH_IA::Process_CONN_ACK_6(CONN_ACK_6 * connAck, IA_USER * iaUser, uint32_t sip)
1124 SwapBytes(connAck->len);
1125 SwapBytes(connAck->rnd);
1127 printfd( __FILE__, "CONN_ACK_6 %s\n", connAck->type);
1129 if ((iaUser->phase.GetPhase() == 2) && (connAck->rnd == iaUser->rnd + 1))
1131 iaUser->phase.UpdateTime();
1133 iaUser->lastSendAlive = iaUser->phase.GetTime();
1134 if (users->Authorize(iaUser->login, sip, enabledDirs, this))
1136 iaUser->phase.SetPhase3();
1137 printfd(__FILE__, "Phase changed from 2 to 3. Reason: CONN_ACK_6\n");
1142 errorStr = iaUser->user->GetStrError();
1143 if (iaSettings.LogProtocolErrors())
1144 logger("IP: %s. User '%s'. Protocol version: %d. CONN_ACK: phase 2, authorization error ('%s').", inet_ntostring(sip).c_str(), iaUser->login.c_str(), iaUser->protoVer, errorStr.c_str());
1145 iaUser->phase.SetPhase1();
1147 printfd(__FILE__, "Phase changed from 2 to 1. Reason: failed to authorize user\n");
1151 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d, expected: %d\n", iaUser->phase.GetPhase(), connAck->rnd, iaUser->rnd + 1);
1152 if (iaSettings.LogProtocolErrors())
1154 if (iaUser->phase.GetPhase() != 2)
1155 logger("IP: %s. User '%s'. Protocol version: %d. CONN_ACK: invalid phase, expected 2, got %d.", inet_ntostring(sip).c_str(), iaUser->login.c_str(), iaUser->protoVer, iaUser->phase.GetPhase());
1156 if (connAck->rnd != iaUser->rnd + 1)
1157 logger("IP: %s. User '%s'. Protocol version: %d. CONN_ACK: invalid control number, expected %d, got %d.", inet_ntostring(sip).c_str(), iaUser->login.c_str(), iaUser->protoVer, (iaUser->rnd + 1), connAck->rnd);
1161 //-----------------------------------------------------------------------------
1162 int AUTH_IA::Process_CONN_ACK_7(CONN_ACK_7 * connAck, IA_USER * iaUser, uint32_t sip)
1164 return Process_CONN_ACK_6(connAck, iaUser, sip);
1166 //-----------------------------------------------------------------------------
1167 int AUTH_IA::Process_CONN_ACK_8(CONN_ACK_8 * connAck, IA_USER * iaUser, uint32_t sip)
1170 SwapBytes(connAck->len);
1171 SwapBytes(connAck->rnd);
1173 printfd( __FILE__, "CONN_ACK_8 %s\n", connAck->type);
1175 if ((iaUser->phase.GetPhase() == 2) && (connAck->rnd == iaUser->rnd + 1))
1177 iaUser->phase.UpdateTime();
1178 iaUser->lastSendAlive = iaUser->phase.GetTime();
1179 if (users->Authorize(iaUser->login, sip, enabledDirs, this))
1181 iaUser->phase.SetPhase3();
1182 printfd(__FILE__, "Phase changed from 2 to 3. Reason: CONN_ACK_8\n");
1187 errorStr = iaUser->user->GetStrError();
1188 if (iaSettings.LogProtocolErrors())
1189 logger("IP: %s. User '%s'. Protocol version: %d. CONN_ACK: phase 2, authorization error ('%s').", inet_ntostring(sip).c_str(), iaUser->login.c_str(), iaUser->protoVer, errorStr.c_str());
1190 iaUser->phase.SetPhase1();
1192 printfd(__FILE__, "Phase changed from 2 to 1. Reason: failed to authorize user\n");
1196 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d, expected: %d\n", iaUser->phase.GetPhase(), connAck->rnd, iaUser->rnd + 1);
1197 if (iaSettings.LogProtocolErrors())
1199 if (iaUser->phase.GetPhase() != 2)
1200 logger("IP: %s. User '%s'. Protocol version: %d. CONN_ACK: invalid phase, expected 2, got %d.", inet_ntostring(sip).c_str(), iaUser->login.c_str(), iaUser->protoVer, iaUser->phase.GetPhase());
1201 if (connAck->rnd != iaUser->rnd + 1)
1202 logger("IP: %s. User '%s'. Protocol version: %d. CONN_ACK: invalid control number, expected %d, got %d.", inet_ntostring(sip).c_str(), iaUser->login.c_str(), iaUser->protoVer, (iaUser->rnd + 1), connAck->rnd);
1206 //-----------------------------------------------------------------------------
1207 int AUTH_IA::Process_ALIVE_ACK_6(ALIVE_ACK_6 * aliveAck, IA_USER * iaUser, uint32_t)
1210 SwapBytes(aliveAck->len);
1211 SwapBytes(aliveAck->rnd);
1213 printfd(__FILE__, "ALIVE_ACK_6\n");
1214 if ((iaUser->phase.GetPhase() == 3) && (aliveAck->rnd == iaUser->rnd + 1))
1216 iaUser->phase.UpdateTime();
1218 iaUser->aliveSent = false;
1223 //-----------------------------------------------------------------------------
1224 int AUTH_IA::Process_ALIVE_ACK_7(ALIVE_ACK_7 * aliveAck, IA_USER * iaUser, uint32_t sip)
1226 return Process_ALIVE_ACK_6(aliveAck, iaUser, sip);
1228 //-----------------------------------------------------------------------------
1229 int AUTH_IA::Process_ALIVE_ACK_8(ALIVE_ACK_8 * aliveAck, IA_USER * iaUser, uint32_t)
1232 SwapBytes(aliveAck->len);
1233 SwapBytes(aliveAck->rnd);
1235 printfd(__FILE__, "ALIVE_ACK_8\n");
1236 if ((iaUser->phase.GetPhase() == 3) && (aliveAck->rnd == iaUser->rnd + 1))
1238 iaUser->phase.UpdateTime();
1240 iaUser->aliveSent = false;
1245 //-----------------------------------------------------------------------------
1246 int AUTH_IA::Process_DISCONN_SYN_6(DISCONN_SYN_6 *, IA_USER * iaUser, uint32_t sip)
1248 printfd(__FILE__, "DISCONN_SYN_6\n");
1249 if (iaUser->phase.GetPhase() != 3)
1251 if (iaSettings.LogProtocolErrors())
1252 logger("IP: %s. User '%s'. Protocol version: %d. DISCONN_SYN: invalid phase, expected 3, got %d.", inet_ntostring(sip).c_str(), iaUser->login.c_str(), iaUser->protoVer, iaUser->phase.GetPhase());
1253 printfd(__FILE__, "Invalid phase. Expected 3, actual %d\n", iaUser->phase.GetPhase());
1254 errorStr = "Incorrect request DISCONN_SYN";
1258 iaUser->phase.SetPhase4();
1259 printfd(__FILE__, "Phase changed from 3 to 4. Reason: DISCONN_SYN_6\n");
1263 //-----------------------------------------------------------------------------
1264 int AUTH_IA::Process_DISCONN_SYN_7(DISCONN_SYN_7 * disconnSyn, IA_USER * iaUser, uint32_t sip)
1266 return Process_DISCONN_SYN_6(disconnSyn, iaUser, sip);
1268 //-----------------------------------------------------------------------------
1269 int AUTH_IA::Process_DISCONN_SYN_8(DISCONN_SYN_8 *, IA_USER * iaUser, uint32_t sip)
1271 if (iaUser->phase.GetPhase() != 3)
1273 if (iaSettings.LogProtocolErrors())
1274 logger("IP: %s. User '%s'. Protocol version: %d. DISCONN_SYN: invalid phase, expected 3, got %d.", inet_ntostring(sip).c_str(), iaUser->login.c_str(), iaUser->protoVer, iaUser->phase.GetPhase());
1275 errorStr = "Incorrect request DISCONN_SYN";
1276 printfd(__FILE__, "Invalid phase. Expected 3, actual %d\n", iaUser->phase.GetPhase());
1280 iaUser->phase.SetPhase4();
1281 printfd(__FILE__, "Phase changed from 3 to 4. Reason: DISCONN_SYN_6\n");
1285 //-----------------------------------------------------------------------------
1286 int AUTH_IA::Process_DISCONN_ACK_6(DISCONN_ACK_6 * disconnAck,
1289 std::map<uint32_t, IA_USER>::iterator)
1292 SwapBytes(disconnAck->len);
1293 SwapBytes(disconnAck->rnd);
1295 printfd(__FILE__, "DISCONN_ACK_6\n");
1296 if (!((iaUser->phase.GetPhase() == 4) && (disconnAck->rnd == iaUser->rnd + 1)))
1298 if (iaSettings.LogProtocolErrors())
1300 if (iaUser->phase.GetPhase() != 4)
1301 logger("IP: %s. User '%s'. Protocol version: %d. DISCONN_ACK: invalid phase, expected 4, got %d.", inet_ntostring(sip).c_str(), iaUser->login.c_str(), iaUser->protoVer, iaUser->phase.GetPhase());
1302 if (disconnAck->rnd != iaUser->rnd + 1)
1303 logger("IP: %s. User '%s'. Protocol version: %d. DISCONN_ACK: invalid control number, expected %d, got %d.", inet_ntostring(sip).c_str(), iaUser->login.c_str(), iaUser->protoVer, (iaUser->rnd + 1), disconnAck->rnd);
1305 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), disconnAck->rnd);
1311 //-----------------------------------------------------------------------------
1312 int AUTH_IA::Process_DISCONN_ACK_7(DISCONN_ACK_7 * disconnAck, IA_USER * iaUser, uint32_t sip, std::map<uint32_t, IA_USER>::iterator it)
1314 return Process_DISCONN_ACK_6(disconnAck, iaUser, sip, it);
1316 //-----------------------------------------------------------------------------
1317 int AUTH_IA::Process_DISCONN_ACK_8(DISCONN_ACK_8 * disconnAck, IA_USER * iaUser, uint32_t sip, std::map<uint32_t, IA_USER>::iterator)
1320 SwapBytes(disconnAck->len);
1321 SwapBytes(disconnAck->rnd);
1323 printfd(__FILE__, "DISCONN_ACK_8\n");
1324 if (!((iaUser->phase.GetPhase() == 4) && (disconnAck->rnd == iaUser->rnd + 1)))
1326 if (iaSettings.LogProtocolErrors())
1328 if (iaUser->phase.GetPhase() != 4)
1329 logger("IP: %s. User '%s'. Protocol version: %d. DISCONN_ACK: invalid phase, expected 4, got %d.", inet_ntostring(sip).c_str(), iaUser->login.c_str(), iaUser->protoVer, iaUser->phase.GetPhase());
1330 if (disconnAck->rnd != iaUser->rnd + 1)
1331 logger("IP: %s. User '%s'. Protocol version: %d. DISCONN_ACK: invalid control number, expected %d, got %d.", inet_ntostring(sip).c_str(), iaUser->login.c_str(), iaUser->protoVer, (iaUser->rnd + 1), disconnAck->rnd);
1333 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), disconnAck->rnd);
1339 //-----------------------------------------------------------------------------
1340 int AUTH_IA::Send_CONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip)
1342 //+++ Fill static data in connSynAck +++
1343 // TODO Move this code. It must be executed only once
1344 connSynAck6.len = Min8(sizeof(CONN_SYN_ACK_6));
1345 memcpy(connSynAck6.type, "CONN_SYN_ACK", 12);
1346 for (int j = 0; j < DIR_NUM; j++)
1348 memcpy(connSynAck6.dirName[j],
1349 stgSettings->GetDirName(j).c_str(),
1350 std::min(stgSettings->GetDirName(j).length(), sizeof(string16)));
1352 connSynAck6.dirName[j][sizeof(string16) - 1] = 0;
1354 //--- Fill static data in connSynAck ---
1356 iaUser->rnd = static_cast<uint32_t>(random());
1357 connSynAck6.rnd = iaUser->rnd;
1359 printfd(__FILE__, "Sending CONN_SYN_ACK with control number %d.\n", iaUser->rnd);
1361 connSynAck6.userTimeOut = iaSettings.GetUserTimeout().GetSec();
1362 connSynAck6.aliveDelay = iaSettings.GetUserDelay().GetSec();
1365 SwapBytes(connSynAck6.len);
1366 SwapBytes(connSynAck6.rnd);
1367 SwapBytes(connSynAck6.userTimeOut);
1368 SwapBytes(connSynAck6.aliveDelay);
1371 EncryptString(&connSynAck6, &connSynAck6, Min8(sizeof(CONN_SYN_ACK_6)), &iaUser->ctx);
1372 return Send(sip, iaSettings.GetUserPort(), &connSynAck6, Min8(sizeof(CONN_SYN_ACK_6)));;
1374 //-----------------------------------------------------------------------------
1375 int AUTH_IA::Send_CONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip)
1377 return Send_CONN_SYN_ACK_6(iaUser, sip);
1379 //-----------------------------------------------------------------------------
1380 int AUTH_IA::Send_CONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip)
1382 memcpy(connSynAck8.hdr.magic, IA_ID, strlen(IA_ID));
1383 connSynAck8.hdr.protoVer[0] = 0;
1384 connSynAck8.hdr.protoVer[1] = 8;
1386 //+++ Fill static data in connSynAck +++
1387 // TODO Move this code. It must be executed only once
1388 connSynAck8.len = Min8(sizeof(CONN_SYN_ACK_8));
1389 memcpy(connSynAck8.type, "CONN_SYN_ACK", 12);
1390 for (int j = 0; j < DIR_NUM; j++)
1392 memcpy(connSynAck8.dirName[j],
1393 stgSettings->GetDirName(j).c_str(),
1394 std::min(stgSettings->GetDirName(j).length(), sizeof(string16)));
1396 connSynAck8.dirName[j][sizeof(string16) - 1] = 0;
1398 //--- Fill static data in connSynAck ---
1400 iaUser->rnd = static_cast<uint32_t>(random());
1401 connSynAck8.rnd = iaUser->rnd;
1403 printfd(__FILE__, "Sending CONN_SYN_ACK with control number %d.\n", iaUser->rnd);
1405 connSynAck8.userTimeOut = iaSettings.GetUserTimeout().GetSec();
1406 connSynAck8.aliveDelay = iaSettings.GetUserDelay().GetSec();
1409 SwapBytes(connSynAck8.len);
1410 SwapBytes(connSynAck8.rnd);
1411 SwapBytes(connSynAck8.userTimeOut);
1412 SwapBytes(connSynAck8.aliveDelay);
1415 EncryptString(&connSynAck8, &connSynAck8, Min8(sizeof(CONN_SYN_ACK_8)), &iaUser->ctx);
1416 return Send(sip, iaUser->port, &connSynAck8, Min8(sizeof(CONN_SYN_ACK_8)));
1418 //-----------------------------------------------------------------------------
1419 int AUTH_IA::Send_ALIVE_SYN_6(IA_USER * iaUser, uint32_t sip)
1421 aliveSyn6.len = Min8(sizeof(ALIVE_SYN_6));
1422 aliveSyn6.rnd = iaUser->rnd = static_cast<uint32_t>(random());
1424 memcpy(aliveSyn6.type, "ALIVE_SYN", 9);
1426 for (int i = 0; i < DIR_NUM; i++)
1428 aliveSyn6.md[i] = iaUser->user->GetProperties().down.Get()[i];
1429 aliveSyn6.mu[i] = iaUser->user->GetProperties().up.Get()[i];
1431 aliveSyn6.sd[i] = iaUser->user->GetSessionDownload()[i];
1432 aliveSyn6.su[i] = iaUser->user->GetSessionUpload()[i];
1436 int dn = iaSettings.GetFreeMbShowType();
1437 const auto tf = iaUser->user->GetTariff();
1441 double p = tf->GetPriceWithTraffType(aliveSyn6.mu[dn],
1446 if (std::fabs(p) < 1.0e-3)
1448 memcpy(aliveSyn6.freeMb, "---", 3);
1452 double fmb = iaUser->user->GetProperties().freeMb;
1453 fmb = fmb < 0 ? 0 : fmb;
1454 snprintf(reinterpret_cast<char*>(aliveSyn6.freeMb), IA_FREEMB_LEN, "%.3f", fmb / p);
1459 if (freeMbNone == iaSettings.GetFreeMbShowType())
1461 aliveSyn6.freeMb[0] = 0;
1465 double fmb = iaUser->user->GetProperties().freeMb;
1466 fmb = fmb < 0 ? 0 : fmb;
1467 snprintf(reinterpret_cast<char*>(aliveSyn6.freeMb), IA_FREEMB_LEN, "C%.3f", fmb);
1472 if (iaUser->aliveSent)
1474 printfd(__FILE__, "========= ALIVE_ACK_6(7) TIMEOUT !!! %s =========\n", iaUser->login.c_str());
1476 iaUser->aliveSent = true;
1479 aliveSyn6.cash = static_cast<int64_t>(iaUser->user->GetProperties().cash.Get() * 1000.0);
1480 if (!stgSettings->GetShowFeeInCash())
1481 aliveSyn6.cash -= static_cast<int64_t>((tf->GetFee() * 1000.0));
1484 SwapBytes(aliveSyn6.len);
1485 SwapBytes(aliveSyn6.rnd);
1486 SwapBytes(aliveSyn6.cash);
1487 for (int i = 0; i < DIR_NUM; ++i)
1489 SwapBytes(aliveSyn6.mu[i]);
1490 SwapBytes(aliveSyn6.md[i]);
1491 SwapBytes(aliveSyn6.su[i]);
1492 SwapBytes(aliveSyn6.sd[i]);
1496 EncryptString(&aliveSyn6, &aliveSyn6, Min8(sizeof(aliveSyn6)), &iaUser->ctx);
1497 return Send(sip, iaSettings.GetUserPort(), &aliveSyn6, Min8(sizeof(aliveSyn6)));
1499 //-----------------------------------------------------------------------------
1500 int AUTH_IA::Send_ALIVE_SYN_7(IA_USER * iaUser, uint32_t sip)
1502 return Send_ALIVE_SYN_6(iaUser, sip);
1504 //-----------------------------------------------------------------------------
1505 int AUTH_IA::Send_ALIVE_SYN_8(IA_USER * iaUser, uint32_t sip)
1507 memcpy(aliveSyn8.hdr.magic, IA_ID, strlen(IA_ID));
1508 aliveSyn8.hdr.protoVer[0] = 0;
1509 aliveSyn8.hdr.protoVer[1] = 8;
1511 aliveSyn8.len = Min8(sizeof(ALIVE_SYN_8));
1512 aliveSyn8.rnd = iaUser->rnd = static_cast<uint32_t>(random());
1514 memcpy(aliveSyn8.type, "ALIVE_SYN", 9);
1516 for (int i = 0; i < DIR_NUM; i++)
1518 aliveSyn8.md[i] = iaUser->user->GetProperties().down.Get()[i];
1519 aliveSyn8.mu[i] = iaUser->user->GetProperties().up.Get()[i];
1521 aliveSyn8.sd[i] = iaUser->user->GetSessionDownload()[i];
1522 aliveSyn8.su[i] = iaUser->user->GetSessionUpload()[i];
1526 int dn = iaSettings.GetFreeMbShowType();
1530 const auto tf = iaUser->user->GetTariff();
1531 double p = tf->GetPriceWithTraffType(aliveSyn8.mu[dn],
1536 if (std::fabs(p) < 1.0e-3)
1538 memcpy(aliveSyn8.freeMb, "---", 3);
1542 double fmb = iaUser->user->GetProperties().freeMb;
1543 fmb = fmb < 0 ? 0 : fmb;
1544 snprintf(reinterpret_cast<char*>(aliveSyn8.freeMb), IA_FREEMB_LEN, "%.3f", fmb / p);
1549 if (freeMbNone == iaSettings.GetFreeMbShowType())
1551 aliveSyn8.freeMb[0] = 0;
1555 double fmb = iaUser->user->GetProperties().freeMb;
1556 fmb = fmb < 0 ? 0 : fmb;
1557 snprintf(reinterpret_cast<char*>(aliveSyn8.freeMb), IA_FREEMB_LEN, "C%.3f", fmb);
1562 if (iaUser->aliveSent)
1564 printfd(__FILE__, "========= ALIVE_ACK_8 TIMEOUT !!! =========\n");
1566 iaUser->aliveSent = true;
1569 const auto tf = iaUser->user->GetTariff();
1571 aliveSyn8.cash = static_cast<int64_t>(iaUser->user->GetProperties().cash.Get() * 1000.0);
1572 if (!stgSettings->GetShowFeeInCash())
1573 aliveSyn8.cash -= static_cast<int64_t>(tf->GetFee() * 1000.0);
1576 SwapBytes(aliveSyn8.len);
1577 SwapBytes(aliveSyn8.rnd);
1578 SwapBytes(aliveSyn8.cash);
1579 SwapBytes(aliveSyn8.status);
1580 for (int i = 0; i < DIR_NUM; ++i)
1582 SwapBytes(aliveSyn8.mu[i]);
1583 SwapBytes(aliveSyn8.md[i]);
1584 SwapBytes(aliveSyn8.su[i]);
1585 SwapBytes(aliveSyn8.sd[i]);
1589 EncryptString(&aliveSyn8, &aliveSyn8, Min8(sizeof(aliveSyn8)), &iaUser->ctx);
1590 return Send(sip, iaUser->port, &aliveSyn8, Min8(sizeof(aliveSyn8)));
1592 //-----------------------------------------------------------------------------
1593 int AUTH_IA::Send_DISCONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip)
1595 disconnSynAck6.len = Min8(sizeof(DISCONN_SYN_ACK_6));
1596 memcpy(disconnSynAck6.type, "DISCONN_SYN_ACK", 15);
1597 disconnSynAck6.rnd = iaUser->rnd = static_cast<uint32_t>(random());
1600 SwapBytes(disconnSynAck6.len);
1601 SwapBytes(disconnSynAck6.rnd);
1604 EncryptString(&disconnSynAck6, &disconnSynAck6, Min8(sizeof(disconnSynAck6)), &iaUser->ctx);
1605 return Send(sip, iaSettings.GetUserPort(), &disconnSynAck6, Min8(sizeof(disconnSynAck6)));
1607 //-----------------------------------------------------------------------------
1608 int AUTH_IA::Send_DISCONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip)
1610 return Send_DISCONN_SYN_ACK_6(iaUser, sip);
1612 //-----------------------------------------------------------------------------
1613 int AUTH_IA::Send_DISCONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip)
1615 memcpy(disconnSynAck8.hdr.magic, IA_ID, strlen(IA_ID));
1616 disconnSynAck8.hdr.protoVer[0] = 0;
1617 disconnSynAck8.hdr.protoVer[1] = 8;
1619 disconnSynAck8.len = Min8(sizeof(DISCONN_SYN_ACK_8));
1620 memcpy(disconnSynAck8.type, "DISCONN_SYN_ACK", 15);
1621 disconnSynAck8.rnd = iaUser->rnd = static_cast<uint32_t>(random());
1624 SwapBytes(disconnSynAck8.len);
1625 SwapBytes(disconnSynAck8.rnd);
1628 EncryptString(&disconnSynAck8, &disconnSynAck8, Min8(sizeof(disconnSynAck8)), &iaUser->ctx);
1629 return Send(sip, iaUser->port, &disconnSynAck8, Min8(sizeof(disconnSynAck8)));
1631 //-----------------------------------------------------------------------------
1632 int AUTH_IA::Send_FIN_6(IA_USER * iaUser, uint32_t sip, std::map<uint32_t, IA_USER>::iterator it)
1634 fin6.len = Min8(sizeof(FIN_6));
1635 memcpy(fin6.type, "FIN", 3);
1636 memcpy(fin6.ok, "OK", 2);
1639 SwapBytes(fin6.len);
1642 EncryptString(&fin6, &fin6, Min8(sizeof(fin6)), &iaUser->ctx);
1644 users->Unauthorize(iaUser->login, this);
1646 int res = Send(sip, iaSettings.GetUserPort(), &fin6, Min8(sizeof(fin6)));
1652 //-----------------------------------------------------------------------------
1653 int AUTH_IA::Send_FIN_7(IA_USER * iaUser, uint32_t sip, std::map<uint32_t, IA_USER>::iterator it)
1655 return Send_FIN_6(iaUser, sip, it);
1657 //-----------------------------------------------------------------------------
1658 int AUTH_IA::Send_FIN_8(IA_USER * iaUser, uint32_t sip, std::map<uint32_t, IA_USER>::iterator it)
1660 memcpy(fin8.hdr.magic, IA_ID, strlen(IA_ID));
1661 fin8.hdr.protoVer[0] = 0;
1662 fin8.hdr.protoVer[1] = 8;
1664 fin8.len = Min8(sizeof(FIN_8));
1665 memcpy(fin8.type, "FIN", 3);
1666 memcpy(fin8.ok, "OK", 2);
1669 SwapBytes(fin8.len);
1672 EncryptString(&fin8, &fin8, Min8(sizeof(fin8)), &iaUser->ctx);
1674 users->Unauthorize(iaUser->login, this);
1676 int res = Send(sip, iaUser->port, &fin8, Min8(sizeof(fin8)));