X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/d1d9452a687184ac6b992fadc77e89c8a308e388..72797a0baa15ce75663a8dc662a132a6dd1a0de1:/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp?ds=inline diff --git a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp index 7894d491..01162875 100644 --- a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp +++ b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp @@ -18,56 +18,36 @@ * Author : Boris Mikhailenko */ -/* - $Revision: 1.79 $ - $Date: 2010/03/25 15:18:48 $ - $Author: faust $ - */ - #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif -#include -#include -#include // close +#include "inetaccess.h" +#include "stg/common.h" +#include "stg/locker.h" +#include "stg/tariff.h" +#include "stg/settings.h" + +#include #include #include #include // snprintf #include #include -#include - -#include "stg/common.h" -#include "stg/locker.h" -#include "stg/tariff.h" -#include "stg/user_property.h" -#include "stg/settings.h" -#include "stg/plugin_creator.h" -#include "inetaccess.h" -extern volatile time_t stgTime; +#include +#include +#include // close -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -namespace -{ -PLUGIN_CREATOR iac; +#define IA_PROTO_VER (6) -void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password); -void Decrypt(BLOWFISH_CTX * ctx, void * dst, const void * src, size_t len8); -void Encrypt(BLOWFISH_CTX * ctx, void * dst, const void * src, size_t len8); -} +extern volatile time_t stgTime; -extern "C" PLUGIN * GetPlugin(); -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -PLUGIN * GetPlugin() +extern "C" STG::Plugin* GetPlugin() { -return iac.GetPlugin(); + static AUTH_IA plugin; + return &plugin; } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- @@ -76,21 +56,20 @@ AUTH_IA_SETTINGS::AUTH_IA_SETTINGS() : userDelay(0), userTimeout(0), port(0), - errorStr(), freeMbShowType(freeMbCash), logProtocolErrors(false) { } //----------------------------------------------------------------------------- -int AUTH_IA_SETTINGS::ParseSettings(const MODULE_SETTINGS & s) +int AUTH_IA_SETTINGS::ParseSettings(const STG::ModuleSettings & s) { int p; -PARAM_VALUE pv; -std::vector::const_iterator pvi; +STG::ParamValue pv; +std::vector::const_iterator pvi; /////////////////////////// pv.param = "Port"; pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); -if (pvi == s.moduleParams.end()) +if (pvi == s.moduleParams.end() || pvi->value.empty()) { errorStr = "Parameter \'Port\' not found."; printfd(__FILE__, "Parameter 'Port' not found\n"); @@ -106,7 +85,7 @@ port = static_cast(p); /////////////////////////// pv.param = "UserDelay"; pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); -if (pvi == s.moduleParams.end()) +if (pvi == s.moduleParams.end() || pvi->value.empty()) { errorStr = "Parameter \'UserDelay\' not found."; printfd(__FILE__, "Parameter 'UserDelay' not found\n"); @@ -122,7 +101,7 @@ if (ParseIntInRange(pvi->value[0], 5, 600, &userDelay)) /////////////////////////// pv.param = "UserTimeout"; pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); -if (pvi == s.moduleParams.end()) +if (pvi == s.moduleParams.end() || pvi->value.empty()) { errorStr = "Parameter \'UserTimeout\' not found."; printfd(__FILE__, "Parameter 'UserTimeout' not found\n"); @@ -138,7 +117,7 @@ if (ParseIntInRange(pvi->value[0], 15, 1200, &userTimeout)) /////////////////////////// pv.param = "LogProtocolErrors"; pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); -if (pvi == s.moduleParams.end()) +if (pvi == s.moduleParams.end() || pvi->value.empty()) logProtocolErrors = false; else if (ParseYesNo(pvi->value[0], &logProtocolErrors)) { @@ -151,7 +130,7 @@ std::string freeMbType; int n = 0; pv.param = "FreeMb"; pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); -if (pvi == s.moduleParams.end()) +if (pvi == s.moduleParams.end() || pvi->value.empty()) { errorStr = "Parameter \'FreeMb\' not found."; printfd(__FILE__, "Parameter 'FreeMb' not found\n"); @@ -175,7 +154,7 @@ else if (!str2x(freeMbType.c_str(), n)) printfd(__FILE__, "%s\n", errorStr.c_str()); return -1; } - freeMbShowType = (FREEMB)(freeMb0 + n); + freeMbShowType = static_cast(n); } else { @@ -192,15 +171,13 @@ return 0; #ifdef IA_PHASE_DEBUG IA_PHASE::IA_PHASE() : phase(1), - phaseTime(), flog(NULL) { gettimeofday(&phaseTime, NULL); } #else IA_PHASE::IA_PHASE() - : phase(1), - phaseTime() + : phase(1) { gettimeofday(&phaseTime, NULL); } @@ -307,39 +284,16 @@ return phaseTime; //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- AUTH_IA::AUTH_IA() - : ctxS(), - errorStr(), - iaSettings(), - settings(), - nonstop(false), - isRunningRun(false), + : isRunningRun(false), isRunningRunTimeouter(false), users(NULL), stgSettings(NULL), - ip2user(), - recvThread(), - timeouterThread(), - mutex(), listenSocket(-1), - connSynAck6(), - connSynAck8(), - disconnSynAck6(), - disconnSynAck8(), - aliveSyn6(), - aliveSyn8(), - fin6(), - fin8(), - packetTypes(), enabledDirs(0xFFffFFff), onDelUserNotifier(*this), - logger(GetPluginLogger(GetStgLogger(), "auth_ia")) + logger(STG::PluginLogger::get("auth_ia")) { -InitEncrypt(&ctxS, "pr7Hhen"); - -pthread_mutexattr_t attr; -pthread_mutexattr_init(&attr); -pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); -pthread_mutex_init(&mutex, &attr); +InitContext("pr7Hhen", 7, &ctxS); memset(&connSynAck6, 0, sizeof(CONN_SYN_ACK_6)); memset(&connSynAck8, 0, sizeof(CONN_SYN_ACK_8)); @@ -383,40 +337,23 @@ packetTypes["ERR"] = ERROR_N; //----------------------------------------------------------------------------- AUTH_IA::~AUTH_IA() { -pthread_mutex_destroy(&mutex); } //----------------------------------------------------------------------------- int AUTH_IA::Start() { users->AddNotifierUserDel(&onDelUserNotifier); -nonstop = true; if (PrepareNet()) { return -1; } -if (!isRunningRun) - { - if (pthread_create(&recvThread, NULL, Run, this)) - { - errorStr = "Cannot create thread."; - printfd(__FILE__, "Cannot create recv thread\n"); - logger("Cannot create recv thread."); - return -1; - } - } +if (!m_thread.joinable()) + m_thread = std::jthread([this](auto token){ Run(std::move(token)); }); + +if (!m_timeouterThread.joinable()) + m_timeouterThread = std::jthread([this](auto token){ RunTimeouter(std::move(token)); }); -if (!isRunningRunTimeouter) - { - if (pthread_create(&timeouterThread, NULL, RunTimeouter, this)) - { - errorStr = "Cannot create thread."; - printfd(__FILE__, "Cannot create timeouter thread\n"); - logger("Cannot create timeouter thread."); - return -1; - } - } errorStr = ""; return 0; } @@ -426,7 +363,8 @@ int AUTH_IA::Stop() if (!IsRunning()) return 0; -nonstop = false; +m_thread.request_stop(); +m_timeouterThread.request_stop(); std::for_each( ip2user.begin(), @@ -458,6 +396,16 @@ if (isRunningRunTimeouter) users->DelNotifierUserDel(&onDelUserNotifier); +if (isRunningRun) + m_thread.detach(); +else + m_thread.join(); + +if (isRunningRunTimeouter) + m_timeouterThread.detach(); +else + m_timeouterThread.join(); + if (isRunningRun || isRunningRunTimeouter) return -1; @@ -465,61 +413,55 @@ printfd(__FILE__, "AUTH_IA::Stoped successfully.\n"); return 0; } //----------------------------------------------------------------------------- -void * AUTH_IA::Run(void * d) +void AUTH_IA::Run(std::stop_token token) { sigset_t signalSet; sigfillset(&signalSet); pthread_sigmask(SIG_BLOCK, &signalSet, NULL); -AUTH_IA * ia = static_cast(d); - -ia->isRunningRun = true; +isRunningRun = true; char buffer[512]; time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC; -while (ia->nonstop) +while (!token.stop_requested()) { - ia->RecvData(buffer, sizeof(buffer)); - if ((touchTime + MONITOR_TIME_DELAY_SEC <= stgTime) && ia->stgSettings->GetMonitoring()) + RecvData(buffer, sizeof(buffer)); + if ((touchTime + MONITOR_TIME_DELAY_SEC <= stgTime) && stgSettings->GetMonitoring()) { touchTime = stgTime; - std::string monFile = ia->stgSettings->GetMonitorDir() + "/inetaccess_r"; - TouchFile(monFile.c_str()); + std::string monFile = stgSettings->GetMonitorDir() + "/inetaccess_r"; + TouchFile(monFile); } } -ia->isRunningRun = false; -return NULL; +isRunningRun = false; } //----------------------------------------------------------------------------- -void * AUTH_IA::RunTimeouter(void * d) +void AUTH_IA::RunTimeouter(std::stop_token token) { sigset_t signalSet; sigfillset(&signalSet); pthread_sigmask(SIG_BLOCK, &signalSet, NULL); -AUTH_IA * ia = static_cast(d); - -ia->isRunningRunTimeouter = true; +isRunningRunTimeouter = true; int a = -1; -std::string monFile = ia->stgSettings->GetMonitorDir() + "/inetaccess_t"; -while (ia->nonstop) +std::string monFile = stgSettings->GetMonitorDir() + "/inetaccess_t"; +while (!token.stop_requested()) { struct timespec ts = {0, 20000000}; nanosleep(&ts, NULL); - ia->Timeouter(); + Timeouter(); // TODO change counter to timer and MONITOR_TIME_DELAY_SEC - if (++a % (50 * 60) == 0 && ia->stgSettings->GetMonitoring()) + if (++a % (50 * 60) == 0 && stgSettings->GetMonitoring()) { - TouchFile(monFile.c_str()); + TouchFile(monFile); } } -ia->isRunningRunTimeouter = false; -return NULL; +isRunningRunTimeouter = false; } //----------------------------------------------------------------------------- int AUTH_IA::ParseSettings() @@ -530,6 +472,22 @@ if (ret) return ret; } //----------------------------------------------------------------------------- +int AUTH_IA::Reload(const STG::ModuleSettings & ms) +{ +AUTH_IA_SETTINGS newIaSettings; +if (newIaSettings.ParseSettings(ms)) + { + printfd(__FILE__, "AUTH_IA::Reload() - Failed to reload InetAccess.\n"); + logger("AUTH_IA: Cannot reload InetAccess. Errors found."); + return -1; + } + +printfd(__FILE__, "AUTH_IA::Reload() - Reloaded InetAccess successfully.\n"); +logger("AUTH_IA: Reloaded InetAccess successfully."); +iaSettings = newIaSettings; +return 0; +} +//----------------------------------------------------------------------------- int AUTH_IA::PrepareNet() { struct sockaddr_in listenAddr; @@ -544,10 +502,10 @@ if (listenSocket < 0) } listenAddr.sin_family = AF_INET; -listenAddr.sin_port = htons(static_cast(iaSettings.GetUserPort())); +listenAddr.sin_port = htons(iaSettings.GetUserPort()); listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0"); -if (bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr)) < 0) +if (bind(listenSocket, reinterpret_cast(&listenAddr), sizeof(listenAddr)) < 0) { errorStr = "AUTH_IA: Bind failed."; logger("Cannot bind the socket: %s", strerror(errno)); @@ -572,7 +530,7 @@ if (!WaitPackets(listenSocket)) // Timeout struct sockaddr_in outerAddr; socklen_t outerAddrLen(sizeof(outerAddr)); -ssize_t dataLen = recvfrom(listenSocket, buffer, bufferSize, 0, (struct sockaddr *)&outerAddr, &outerAddrLen); +ssize_t dataLen = recvfrom(listenSocket, buffer, bufferSize, 0, reinterpret_cast(&outerAddr), &outerAddrLen); if (!dataLen) // EOF { @@ -603,42 +561,42 @@ if (CheckHeader(buffer, sip, &protoVer)) char login[PASSWD_LEN]; //TODO why PASSWD_LEN ? memset(login, 0, PASSWD_LEN); -Decrypt(&ctxS, login, buffer + 8, PASSWD_LEN / 8); +DecryptString(login, buffer + 8, PASSWD_LEN, &ctxS); -USER_PTR user; +UserPtr user; if (users->FindByName(login, &user)) { logger("User's connect failed: user '%s' not found. IP %s", login, inet_ntostring(sip).c_str()); printfd(__FILE__, "User '%s' NOT found!\n", login); - SendError(sip, sport, protoVer, "îÅÐÒÁ×ÉÌØÎÙÊ ÌÏÇÉÎ!"); + SendError(sip, sport, protoVer, IconvString("Неправильный логин.", "utf8", "koi8-ru")); return -1; } printfd(__FILE__, "User '%s' FOUND!\n", user->GetLogin().c_str()); -if (user->GetProperty().disabled.Get()) +if (user->GetProperties().disabled.Get()) { logger("Cannont authorize '%s', user is disabled.", login); - SendError(sip, sport, protoVer, "õÞÅÔÎÁÑ ÚÁÐÉÓØ ÚÁÂÌÏËÉÒÏ×ÁÎÁ"); + SendError(sip, sport, protoVer, IconvString("Учетная запись заблокирована.", "utf8", "koi8-ru")); return 0; } -if (user->GetProperty().passive.Get()) +if (user->GetProperties().passive.Get()) { logger("Cannont authorize '%s', user is passive.", login); - SendError(sip, sport, protoVer, "õÞÅÔÎÁÑ ÚÁÐÉÓØ ÚÁÍÏÒÏÖÅÎÁ"); + SendError(sip, sport, protoVer, IconvString("Учетная запись заморожена.", "utf8", "koi8-ru")); return 0; } -if (!user->GetProperty().ips.Get().IsIPInIPS(sip)) +if (!user->GetProperties().ips.Get().find(sip)) { printfd(__FILE__, "User %s. IP address is incorrect. IP %s\n", user->GetLogin().c_str(), inet_ntostring(sip).c_str()); logger("User %s. IP address is incorrect. IP %s", user->GetLogin().c_str(), inet_ntostring(sip).c_str()); - SendError(sip, sport, protoVer, "ðÏÌØÚÏ×ÁÔÅÌØ ÎÅ ÏÐÏÚÎÁÎ! ðÒÏ×ÅÒØÔÅ IP ÁÄÒÅÓ."); + SendError(sip, sport, protoVer, IconvString("Пользователь не опознан. Проверьте IP-адрес.", "utf8", "koi8-ru")); return 0; } @@ -649,11 +607,9 @@ int AUTH_IA::CheckHeader(const char * buffer, uint32_t sip, int * protoVer) { if (strncmp(IA_ID, buffer, strlen(IA_ID)) != 0) { - //SendError(userIP, updateMsg); printfd(__FILE__, "update needed - IA_ID\n"); if (iaSettings.LogProtocolErrors()) logger("IP: %s. Header: invalid packed signature.", inet_ntostring(sip).c_str()); - //SendError(userIP, "Incorrect header!"); return -1; } @@ -662,14 +618,12 @@ if (buffer[6] != 0) //proto[0] shoud be 0 printfd(__FILE__, "update needed - PROTO major: %d\n", buffer[6]); if (iaSettings.LogProtocolErrors()) logger("IP: %s. Header: invalid protocol major version: %d.", inet_ntostring(sip).c_str(), buffer[6]); - //SendError(userIP, updateMsg); return -1; } if (buffer[7] < 6) { // need update - //SendError(userIP, updateMsg); printfd(__FILE__, "update needed - PROTO minor: %d\n", buffer[7]); if (iaSettings.LogProtocolErrors()) logger("IP: %s. Header: invalid protocol minor version: %d.", inet_ntostring(sip).c_str(), buffer[7]); @@ -684,7 +638,7 @@ return 0; //----------------------------------------------------------------------------- int AUTH_IA::Timeouter() { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::lock_guard lock(m_mutex); std::map::iterator it; it = ip2user.begin(); @@ -700,7 +654,7 @@ while (it != ip2user.end()) && (currTime - it->second.phase.GetTime()) > iaSettings.GetUserDelay()) { if (iaSettings.LogProtocolErrors()) - 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()); + 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()); it->second.phase.SetPhase1(); printfd(__FILE__, "Phase changed from 2 to 1. Reason: timeout\n"); ip2user.erase(it++); @@ -744,7 +698,7 @@ while (it != ip2user.end()) if ((currTime - it->second.phase.GetTime()) > iaSettings.GetUserTimeout()) { if (iaSettings.LogProtocolErrors()) - 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()); + 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()); users->Unauthorize(it->second.user->GetLogin(), this); ip2user.erase(it++); continue; @@ -755,7 +709,7 @@ while (it != ip2user.end()) && ((currTime - it->second.phase.GetTime()) > iaSettings.GetUserDelay())) { if (iaSettings.LogProtocolErrors()) - 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()); + 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()); it->second.phase.SetPhase3(); printfd(__FILE__, "Phase changed from 4 to 3. Reason: timeout\n"); } @@ -766,17 +720,17 @@ while (it != ip2user.end()) return 0; } //----------------------------------------------------------------------------- -int AUTH_IA::PacketProcessor(void * buff, size_t dataLen, uint32_t sip, uint16_t sport, int protoVer, USER_PTR user) +int AUTH_IA::PacketProcessor(void * buff, size_t dataLen, uint32_t sip, uint16_t sport, int protoVer, UserPtr user) { std::string login(user->GetLogin()); const size_t offset = LOGIN_LEN + 2 + 6; // LOGIN_LEN + sizeOfMagic + sizeOfVer; -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::lock_guard lock(m_mutex); std::map::iterator it(ip2user.find(sip)); if (it == ip2user.end()) { - USER_PTR userPtr; + UserPtr userPtr; if (!users->FindByIPIdx(sip, &userPtr)) { if (userPtr->GetID() != user->GetID()) @@ -789,7 +743,7 @@ if (it == ip2user.end()) userPtr->GetLogin().c_str(), inet_ntostring(sip).c_str(), login.c_str()); - SendError(sip, sport, protoVer, "÷ÁÛ IP ÁÄÒÅÓ ÕÖÅ ÉÓÐÏÌØÚÕÅÔÓÑ!"); + SendError(sip, sport, protoVer, IconvString("IP-адрес уже сипользуется.", "utf8", "koi8-ru")); return 0; } } @@ -814,19 +768,20 @@ else if (user->GetID() != it->second.user->GetID()) it->second.user->GetLogin().c_str(), inet_ntostring(sip).c_str(), user->GetLogin().c_str()); - SendError(sip, sport, protoVer, "÷ÁÛ IP ÁÄÒÅÓ ÕÖÅ ÉÓÐÏÌØÚÕÅÔÓÑ!"); + SendError(sip, sport, protoVer, IconvString("IP-адрес уже используется.", "utf8", "koi8-ru")); return 0; } IA_USER * iaUser = &(it->second); -if (iaUser->password != user->GetProperty().password.Get()) +if (iaUser->password != user->GetProperties().password.Get()) { - InitEncrypt(&iaUser->ctx, user->GetProperty().password.Get()); - iaUser->password = user->GetProperty().password.Get(); + const std::string & password = user->GetProperties().password.Get(); + InitContext(password.c_str(), password.length(), &iaUser->ctx); + iaUser->password = user->GetProperties().password.Get(); } -Decrypt(&iaUser->ctx, static_cast(buff) + offset, static_cast(buff) + offset, (dataLen - offset) / 8); +DecryptString(static_cast(buff) + offset, static_cast(buff) + offset, (dataLen - offset), &iaUser->ctx); char packetName[IA_MAX_TYPE_LEN]; strncpy(packetName, static_cast(buff) + offset + 4, IA_MAX_TYPE_LEN); @@ -835,7 +790,7 @@ packetName[IA_MAX_TYPE_LEN - 1] = 0; std::map::iterator pi(packetTypes.find(packetName)); if (pi == packetTypes.end()) { - SendError(sip, sport, protoVer, "îÅÐÒÁ×ÉÌØÎÙÊ ÌÏÇÉÎ ÉÌÉ ÐÁÒÏÌØ!"); + SendError(sip, sport, protoVer, IconvString("Неправильный логин или пароль.", "utf8", "koi8-ru")); printfd(__FILE__, "Login or password is wrong!\n"); logger("User's connect failed. User: '%s', ip %s. Wrong login or password", login.c_str(), @@ -853,7 +808,7 @@ if (user->IsAuthorizedBy(this) && user->GetCurrIP() != sip) login.c_str(), inet_ntostring(user->GetCurrIP()).c_str(), inet_ntostring(sip).c_str()); - SendError(sip, sport, protoVer, "÷ÁÛ ÌÏÇÉÎ ÕÖÅ ÉÓÐÏÌØÚÕÅÔÓÑ!"); + SendError(sip, sport, protoVer, IconvString("Логин уже используется.", "utf8", "koi8-ru")); ip2user.erase(it); return 0; } @@ -948,7 +903,7 @@ switch (pi->second) return -1; } //----------------------------------------------------------------------------- -void AUTH_IA::DelUser(USER_PTR u) +void AUTH_IA::DelUser(UserPtr u) { uint32_t ip = u->GetCurrIP(); @@ -958,7 +913,7 @@ if (!ip) std::map::iterator it; -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::lock_guard lock(m_mutex); it = ip2user.find(ip); if (it == ip2user.end()) { @@ -991,14 +946,14 @@ switch (protoVer) sendAddr.sin_addr.s_addr = ip; err.len = 1; - strncpy((char*)err.type, "ERR", 16); - strncpy((char*)err.text, text.c_str(), MAX_MSG_LEN); + memcpy(err.type, "ERR", 3); + memcpy(err.text, text.c_str(), std::min(text.length(), MAX_MSG_LEN)); #ifdef ARCH_BE SwapBytes(err.len); #endif - res = sendto(listenSocket, &err, sizeof(err), 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr)); + res = sendto(listenSocket, &err, sizeof(err), 0, reinterpret_cast(&sendAddr), sizeof(sendAddr)); printfd(__FILE__, "SendError %d bytes sent\n", res); break; @@ -1011,14 +966,14 @@ switch (protoVer) sendAddr.sin_addr.s_addr = ip; err8.len = 256; - strncpy((char*)err8.type, "ERR", 16); - strncpy((char*)err8.text, text.c_str(), MAX_MSG_LEN); + memcpy(err8.type, "ERR", 3); + memcpy(err8.text, text.c_str(), std::min(text.length(), MAX_MSG_LEN)); #ifdef ARCH_BE SwapBytes(err8.len); #endif - res = sendto(listenSocket, &err8, sizeof(err8), 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr)); + res = sendto(listenSocket, &err8, sizeof(err8), 0, reinterpret_cast(&sendAddr), sizeof(sendAddr)); printfd(__FILE__, "SendError_8 %d bytes sent\n", res); break; } @@ -1026,7 +981,7 @@ switch (protoVer) return 0; } //----------------------------------------------------------------------------- -int AUTH_IA::Send(uint32_t ip, uint16_t port, const char * buffer, size_t len) +int AUTH_IA::Send(uint32_t ip, uint16_t port, const void* buffer, size_t len) { struct sockaddr_in sendAddr; @@ -1034,19 +989,19 @@ sendAddr.sin_family = AF_INET; sendAddr.sin_port = htons(port); sendAddr.sin_addr.s_addr = ip; -if (sendto(listenSocket, buffer, len, 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr)) == static_cast(len)) +if (sendto(listenSocket, buffer, len, 0, reinterpret_cast(&sendAddr), sizeof(sendAddr)) == static_cast(len)) return 0; return -1; } //----------------------------------------------------------------------------- -int AUTH_IA::SendMessage(const STG_MSG & msg, uint32_t ip) const +int AUTH_IA::SendMessage(const STG::Message & msg, uint32_t ip) const { printfd(__FILE__, "SendMessage userIP=%s\n", inet_ntostring(ip).c_str()); std::map::iterator it; -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::lock_guard lock(m_mutex); it = ip2user.find(ip); if (it == ip2user.end()) { @@ -1057,7 +1012,7 @@ it->second.messagesToSend.push_back(msg); return 0; } //----------------------------------------------------------------------------- -int AUTH_IA::RealSendMessage6(const STG_MSG & msg, uint32_t ip, IA_USER & user) +int AUTH_IA::RealSendMessage6(const STG::Message & msg, uint32_t ip, IA_USER & user) { printfd(__FILE__, "RealSendMessage 6 user=%s\n", user.login.c_str()); @@ -1065,9 +1020,9 @@ INFO_6 info; memset(&info, 0, sizeof(INFO_6)); info.len = 256; -strncpy((char*)info.type, "INFO", 16); +memcpy(info.type, "INFO", 4); info.infoType = 'I'; -strncpy((char*)info.text, msg.text.c_str(), 235); +memcpy(info.text, msg.text.c_str(), std::min(msg.text.length(), 235)); info.text[234] = 0; size_t len = info.len; @@ -1077,11 +1032,11 @@ SwapBytes(info.len); char buffer[256]; memcpy(buffer, &info, sizeof(INFO_6)); -Encrypt(&user.ctx, buffer, buffer, len / 8); +EncryptString(buffer, buffer, len, &user.ctx); return Send(ip, iaSettings.GetUserPort(), buffer, len); } //----------------------------------------------------------------------------- -int AUTH_IA::RealSendMessage7(const STG_MSG & msg, uint32_t ip, IA_USER & user) +int AUTH_IA::RealSendMessage7(const STG::Message & msg, uint32_t ip, IA_USER & user) { printfd(__FILE__, "RealSendMessage 7 user=%s\n", user.login.c_str()); @@ -1089,7 +1044,7 @@ INFO_7 info; memset(&info, 0, sizeof(INFO_7)); info.len = 264; -strncpy((char*)info.type, "INFO_7", 16); +memcpy(info.type, "INFO_7", 6); info.infoType = static_cast(msg.header.type); info.showTime = static_cast(msg.header.showTime); info.sendTime = msg.header.creationTime; @@ -1100,17 +1055,17 @@ SwapBytes(info.len); SwapBytes(info.sendTime); #endif -strncpy((char*)info.text, msg.text.c_str(), MAX_MSG_LEN - 1); +memcpy(info.text, msg.text.c_str(), std::min(msg.text.length(), MAX_MSG_LEN - 1)); info.text[MAX_MSG_LEN - 1] = 0; char buffer[300]; memcpy(buffer, &info, sizeof(INFO_7)); -Encrypt(&user.ctx, buffer, buffer, len / 8); +EncryptString(buffer, buffer, len, &user.ctx); return Send(ip, iaSettings.GetUserPort(), buffer, len); } //----------------------------------------------------------------------------- -int AUTH_IA::RealSendMessage8(const STG_MSG & msg, uint32_t ip, IA_USER & user) +int AUTH_IA::RealSendMessage8(const STG::Message & msg, uint32_t ip, IA_USER & user) { printfd(__FILE__, "RealSendMessage 8 user=%s\n", user.login.c_str()); @@ -1118,12 +1073,12 @@ INFO_8 info; memset(&info, 0, sizeof(INFO_8)); info.len = 1056; -strncpy((char*)info.type, "INFO_8", 16); +memcpy(info.type, "INFO_8", 6); info.infoType = static_cast(msg.header.type); info.showTime = static_cast(msg.header.showTime); info.sendTime = msg.header.creationTime; -strncpy((char*)info.text, msg.text.c_str(), IA_MAX_MSG_LEN_8 - 1); +memcpy(info.text, msg.text.c_str(), std::min(msg.text.length(), IA_MAX_MSG_LEN_8 - 1)); info.text[IA_MAX_MSG_LEN_8 - 1] = 0; size_t len = info.len; @@ -1135,7 +1090,7 @@ SwapBytes(info.sendTime); char buffer[1500]; memcpy(buffer, &info, sizeof(INFO_8)); -Encrypt(&user.ctx, buffer, buffer, len / 8); +EncryptString(buffer, buffer, len, &user.ctx); return Send(ip, user.port, buffer, len); } //----------------------------------------------------------------------------- @@ -1196,7 +1151,7 @@ if ((iaUser->phase.GetPhase() == 2) && (connAck->rnd == iaUser->rnd + 1)) return -1; } } -printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), connAck->rnd); +printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d, expected: %d\n", iaUser->phase.GetPhase(), connAck->rnd, iaUser->rnd + 1); if (iaSettings.LogProtocolErrors()) { if (iaUser->phase.GetPhase() != 2) @@ -1241,7 +1196,7 @@ if ((iaUser->phase.GetPhase() == 2) && (connAck->rnd == iaUser->rnd + 1)) return -1; } } -printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), connAck->rnd); +printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d, expected: %d\n", iaUser->phase.GetPhase(), connAck->rnd, iaUser->rnd + 1); if (iaSettings.LogProtocolErrors()) { if (iaUser->phase.GetPhase() != 2) @@ -1390,12 +1345,12 @@ int AUTH_IA::Send_CONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip) //+++ Fill static data in connSynAck +++ // TODO Move this code. It must be executed only once connSynAck6.len = Min8(sizeof(CONN_SYN_ACK_6)); -strcpy((char*)connSynAck6.type, "CONN_SYN_ACK"); +memcpy(connSynAck6.type, "CONN_SYN_ACK", 12); for (int j = 0; j < DIR_NUM; j++) { - strncpy((char*)connSynAck6.dirName[j], - stgSettings->GetDirName(j).c_str(), - sizeof(string16)); + memcpy(connSynAck6.dirName[j], + stgSettings->GetDirName(j).c_str(), + std::min(stgSettings->GetDirName(j).length(), sizeof(string16))); connSynAck6.dirName[j][sizeof(string16) - 1] = 0; } @@ -1404,8 +1359,10 @@ for (int j = 0; j < DIR_NUM; j++) iaUser->rnd = static_cast(random()); connSynAck6.rnd = iaUser->rnd; -connSynAck6.userTimeOut = iaSettings.GetUserTimeout(); -connSynAck6.aliveDelay = iaSettings.GetUserDelay(); +printfd(__FILE__, "Sending CONN_SYN_ACK with control number %d.\n", iaUser->rnd); + +connSynAck6.userTimeOut = iaSettings.GetUserTimeout().GetSec(); +connSynAck6.aliveDelay = iaSettings.GetUserDelay().GetSec(); #ifdef ARCH_BE SwapBytes(connSynAck6.len); @@ -1414,8 +1371,8 @@ SwapBytes(connSynAck6.userTimeOut); SwapBytes(connSynAck6.aliveDelay); #endif -Encrypt(&iaUser->ctx, (char*)&connSynAck6, (char*)&connSynAck6, Min8(sizeof(CONN_SYN_ACK_6))/8); -return Send(sip, iaSettings.GetUserPort(), (char*)&connSynAck6, Min8(sizeof(CONN_SYN_ACK_6)));; +EncryptString(&connSynAck6, &connSynAck6, Min8(sizeof(CONN_SYN_ACK_6)), &iaUser->ctx); +return Send(sip, iaSettings.GetUserPort(), &connSynAck6, Min8(sizeof(CONN_SYN_ACK_6)));; } //----------------------------------------------------------------------------- int AUTH_IA::Send_CONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip) @@ -1425,19 +1382,19 @@ return Send_CONN_SYN_ACK_6(iaUser, sip); //----------------------------------------------------------------------------- int AUTH_IA::Send_CONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip) { -strcpy((char*)connSynAck8.hdr.magic, IA_ID); +memcpy(connSynAck8.hdr.magic, IA_ID, strlen(IA_ID)); connSynAck8.hdr.protoVer[0] = 0; connSynAck8.hdr.protoVer[1] = 8; //+++ Fill static data in connSynAck +++ // TODO Move this code. It must be executed only once connSynAck8.len = Min8(sizeof(CONN_SYN_ACK_8)); -strcpy((char*)connSynAck8.type, "CONN_SYN_ACK"); +memcpy(connSynAck8.type, "CONN_SYN_ACK", 12); for (int j = 0; j < DIR_NUM; j++) { - strncpy((char*)connSynAck8.dirName[j], - stgSettings->GetDirName(j).c_str(), - sizeof(string16)); + memcpy(connSynAck8.dirName[j], + stgSettings->GetDirName(j).c_str(), + std::min(stgSettings->GetDirName(j).length(), sizeof(string16))); connSynAck8.dirName[j][sizeof(string16) - 1] = 0; } @@ -1446,8 +1403,10 @@ for (int j = 0; j < DIR_NUM; j++) iaUser->rnd = static_cast(random()); connSynAck8.rnd = iaUser->rnd; -connSynAck8.userTimeOut = iaSettings.GetUserTimeout(); -connSynAck8.aliveDelay = iaSettings.GetUserDelay(); +printfd(__FILE__, "Sending CONN_SYN_ACK with control number %d.\n", iaUser->rnd); + +connSynAck8.userTimeOut = iaSettings.GetUserTimeout().GetSec(); +connSynAck8.aliveDelay = iaSettings.GetUserDelay().GetSec(); #ifdef ARCH_BE SwapBytes(connSynAck8.len); @@ -1456,8 +1415,8 @@ SwapBytes(connSynAck8.userTimeOut); SwapBytes(connSynAck8.aliveDelay); #endif -Encrypt(&iaUser->ctx, (char*)&connSynAck8, (char*)&connSynAck8, Min8(sizeof(CONN_SYN_ACK_8))/8); -return Send(sip, iaUser->port, (char*)&connSynAck8, Min8(sizeof(CONN_SYN_ACK_8))); +EncryptString(&connSynAck8, &connSynAck8, Min8(sizeof(CONN_SYN_ACK_8)), &iaUser->ctx); +return Send(sip, iaUser->port, &connSynAck8, Min8(sizeof(CONN_SYN_ACK_8))); } //----------------------------------------------------------------------------- int AUTH_IA::Send_ALIVE_SYN_6(IA_USER * iaUser, uint32_t sip) @@ -1465,12 +1424,12 @@ int AUTH_IA::Send_ALIVE_SYN_6(IA_USER * iaUser, uint32_t sip) aliveSyn6.len = Min8(sizeof(ALIVE_SYN_6)); aliveSyn6.rnd = iaUser->rnd = static_cast(random()); -strcpy((char*)aliveSyn6.type, "ALIVE_SYN"); +memcpy(aliveSyn6.type, "ALIVE_SYN", 9); for (int i = 0; i < DIR_NUM; i++) { - aliveSyn6.md[i] = iaUser->user->GetProperty().down.Get()[i]; - aliveSyn6.mu[i] = iaUser->user->GetProperty().up.Get()[i]; + aliveSyn6.md[i] = iaUser->user->GetProperties().down.Get()[i]; + aliveSyn6.mu[i] = iaUser->user->GetProperties().up.Get()[i]; aliveSyn6.sd[i] = iaUser->user->GetSessionDownload()[i]; aliveSyn6.su[i] = iaUser->user->GetSessionUpload()[i]; @@ -1478,7 +1437,7 @@ for (int i = 0; i < DIR_NUM; i++) //TODO int dn = iaSettings.GetFreeMbShowType(); -const TARIFF * tf = iaUser->user->GetTariff(); +const auto tf = iaUser->user->GetTariff(); if (dn < DIR_NUM) { @@ -1489,13 +1448,13 @@ if (dn < DIR_NUM) p *= 1024 * 1024; if (std::fabs(p) < 1.0e-3) { - snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "---"); + memcpy(aliveSyn6.freeMb, "---", 3); } else { - double fmb = iaUser->user->GetProperty().freeMb; + double fmb = iaUser->user->GetProperties().freeMb; fmb = fmb < 0 ? 0 : fmb; - snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "%.3f", fmb / p); + snprintf(reinterpret_cast(aliveSyn6.freeMb), IA_FREEMB_LEN, "%.3f", fmb / p); } } else @@ -1506,9 +1465,9 @@ else } else { - double fmb = iaUser->user->GetProperty().freeMb; + double fmb = iaUser->user->GetProperties().freeMb; fmb = fmb < 0 ? 0 : fmb; - snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "C%.3f", fmb); + snprintf(reinterpret_cast(aliveSyn6.freeMb), IA_FREEMB_LEN, "C%.3f", fmb); } } @@ -1520,9 +1479,9 @@ if (iaUser->aliveSent) iaUser->aliveSent = true; #endif -aliveSyn6.cash =(int64_t) (iaUser->user->GetProperty().cash.Get() * 1000.0); +aliveSyn6.cash = static_cast(iaUser->user->GetProperties().cash.Get() * 1000.0); if (!stgSettings->GetShowFeeInCash()) - aliveSyn6.cash -= (int64_t)(tf->GetFee() * 1000.0); + aliveSyn6.cash -= static_cast((tf->GetFee() * 1000.0)); #ifdef ARCH_BE SwapBytes(aliveSyn6.len); @@ -1537,8 +1496,8 @@ for (int i = 0; i < DIR_NUM; ++i) } #endif -Encrypt(&(iaUser->ctx), (char*)&aliveSyn6, (char*)&aliveSyn6, Min8(sizeof(aliveSyn6))/8); -return Send(sip, iaSettings.GetUserPort(), (char*)&aliveSyn6, Min8(sizeof(aliveSyn6))); +EncryptString(&aliveSyn6, &aliveSyn6, Min8(sizeof(aliveSyn6)), &iaUser->ctx); +return Send(sip, iaSettings.GetUserPort(), &aliveSyn6, Min8(sizeof(aliveSyn6))); } //----------------------------------------------------------------------------- int AUTH_IA::Send_ALIVE_SYN_7(IA_USER * iaUser, uint32_t sip) @@ -1548,19 +1507,19 @@ return Send_ALIVE_SYN_6(iaUser, sip); //----------------------------------------------------------------------------- int AUTH_IA::Send_ALIVE_SYN_8(IA_USER * iaUser, uint32_t sip) { -strcpy((char*)aliveSyn8.hdr.magic, IA_ID); +memcpy(aliveSyn8.hdr.magic, IA_ID, strlen(IA_ID)); aliveSyn8.hdr.protoVer[0] = 0; aliveSyn8.hdr.protoVer[1] = 8; aliveSyn8.len = Min8(sizeof(ALIVE_SYN_8)); aliveSyn8.rnd = iaUser->rnd = static_cast(random()); -strcpy((char*)aliveSyn8.type, "ALIVE_SYN"); +memcpy(aliveSyn8.type, "ALIVE_SYN", 9); for (int i = 0; i < DIR_NUM; i++) { - aliveSyn8.md[i] = iaUser->user->GetProperty().down.Get()[i]; - aliveSyn8.mu[i] = iaUser->user->GetProperty().up.Get()[i]; + aliveSyn8.md[i] = iaUser->user->GetProperties().down.Get()[i]; + aliveSyn8.mu[i] = iaUser->user->GetProperties().up.Get()[i]; aliveSyn8.sd[i] = iaUser->user->GetSessionDownload()[i]; aliveSyn8.su[i] = iaUser->user->GetSessionUpload()[i]; @@ -1571,7 +1530,7 @@ int dn = iaSettings.GetFreeMbShowType(); if (dn < DIR_NUM) { - const TARIFF * tf = iaUser->user->GetTariff(); + const auto tf = iaUser->user->GetTariff(); double p = tf->GetPriceWithTraffType(aliveSyn8.mu[dn], aliveSyn8.md[dn], dn, @@ -1579,13 +1538,13 @@ if (dn < DIR_NUM) p *= 1024 * 1024; if (std::fabs(p) < 1.0e-3) { - snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "---"); + memcpy(aliveSyn8.freeMb, "---", 3); } else { - double fmb = iaUser->user->GetProperty().freeMb; + double fmb = iaUser->user->GetProperties().freeMb; fmb = fmb < 0 ? 0 : fmb; - snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "%.3f", fmb / p); + snprintf(reinterpret_cast(aliveSyn8.freeMb), IA_FREEMB_LEN, "%.3f", fmb / p); } } else @@ -1596,9 +1555,9 @@ else } else { - double fmb = iaUser->user->GetProperty().freeMb; + double fmb = iaUser->user->GetProperties().freeMb; fmb = fmb < 0 ? 0 : fmb; - snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "C%.3f", fmb); + snprintf(reinterpret_cast(aliveSyn8.freeMb), IA_FREEMB_LEN, "C%.3f", fmb); } } @@ -1610,11 +1569,11 @@ if (iaUser->aliveSent) iaUser->aliveSent = true; #endif -const TARIFF * tf = iaUser->user->GetTariff(); +const auto tf = iaUser->user->GetTariff(); -aliveSyn8.cash =(int64_t) (iaUser->user->GetProperty().cash.Get() * 1000.0); +aliveSyn8.cash = static_cast(iaUser->user->GetProperties().cash.Get() * 1000.0); if (!stgSettings->GetShowFeeInCash()) - aliveSyn8.cash -= (int64_t)(tf->GetFee() * 1000.0); + aliveSyn8.cash -= static_cast(tf->GetFee() * 1000.0); #ifdef ARCH_BE SwapBytes(aliveSyn8.len); @@ -1630,14 +1589,14 @@ for (int i = 0; i < DIR_NUM; ++i) } #endif -Encrypt(&(iaUser->ctx), (char*)&aliveSyn8, (char*)&aliveSyn8, Min8(sizeof(aliveSyn8))/8); -return Send(sip, iaUser->port, (char*)&aliveSyn8, Min8(sizeof(aliveSyn8))); +EncryptString(&aliveSyn8, &aliveSyn8, Min8(sizeof(aliveSyn8)), &iaUser->ctx); +return Send(sip, iaUser->port, &aliveSyn8, Min8(sizeof(aliveSyn8))); } //----------------------------------------------------------------------------- int AUTH_IA::Send_DISCONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip) { disconnSynAck6.len = Min8(sizeof(DISCONN_SYN_ACK_6)); -strcpy((char*)disconnSynAck6.type, "DISCONN_SYN_ACK"); +memcpy(disconnSynAck6.type, "DISCONN_SYN_ACK", 15); disconnSynAck6.rnd = iaUser->rnd = static_cast(random()); #ifdef ARCH_BE @@ -1645,8 +1604,8 @@ SwapBytes(disconnSynAck6.len); SwapBytes(disconnSynAck6.rnd); #endif -Encrypt(&iaUser->ctx, (char*)&disconnSynAck6, (char*)&disconnSynAck6, Min8(sizeof(disconnSynAck6))/8); -return Send(sip, iaSettings.GetUserPort(), (char*)&disconnSynAck6, Min8(sizeof(disconnSynAck6))); +EncryptString(&disconnSynAck6, &disconnSynAck6, Min8(sizeof(disconnSynAck6)), &iaUser->ctx); +return Send(sip, iaSettings.GetUserPort(), &disconnSynAck6, Min8(sizeof(disconnSynAck6))); } //----------------------------------------------------------------------------- int AUTH_IA::Send_DISCONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip) @@ -1656,12 +1615,12 @@ return Send_DISCONN_SYN_ACK_6(iaUser, sip); //----------------------------------------------------------------------------- int AUTH_IA::Send_DISCONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip) { -strcpy((char*)disconnSynAck8.hdr.magic, IA_ID); +memcpy(disconnSynAck8.hdr.magic, IA_ID, strlen(IA_ID)); disconnSynAck8.hdr.protoVer[0] = 0; disconnSynAck8.hdr.protoVer[1] = 8; disconnSynAck8.len = Min8(sizeof(DISCONN_SYN_ACK_8)); -strcpy((char*)disconnSynAck8.type, "DISCONN_SYN_ACK"); +memcpy(disconnSynAck8.type, "DISCONN_SYN_ACK", 15); disconnSynAck8.rnd = iaUser->rnd = static_cast(random()); #ifdef ARCH_BE @@ -1669,25 +1628,25 @@ SwapBytes(disconnSynAck8.len); SwapBytes(disconnSynAck8.rnd); #endif -Encrypt(&iaUser->ctx, (char*)&disconnSynAck8, (char*)&disconnSynAck8, Min8(sizeof(disconnSynAck8))/8); -return Send(sip, iaUser->port, (char*)&disconnSynAck8, Min8(sizeof(disconnSynAck8))); +EncryptString(&disconnSynAck8, &disconnSynAck8, Min8(sizeof(disconnSynAck8)), &iaUser->ctx); +return Send(sip, iaUser->port, &disconnSynAck8, Min8(sizeof(disconnSynAck8))); } //----------------------------------------------------------------------------- int AUTH_IA::Send_FIN_6(IA_USER * iaUser, uint32_t sip, std::map::iterator it) { fin6.len = Min8(sizeof(FIN_6)); -strcpy((char*)fin6.type, "FIN"); -strcpy((char*)fin6.ok, "OK"); +memcpy(fin6.type, "FIN", 3); +memcpy(fin6.ok, "OK", 2); #ifdef ARCH_BE SwapBytes(fin6.len); #endif -Encrypt(&iaUser->ctx, (char*)&fin6, (char*)&fin6, Min8(sizeof(fin6))/8); +EncryptString(&fin6, &fin6, Min8(sizeof(fin6)), &iaUser->ctx); users->Unauthorize(iaUser->login, this); -int res = Send(sip, iaSettings.GetUserPort(), (char*)&fin6, Min8(sizeof(fin6))); +int res = Send(sip, iaSettings.GetUserPort(), &fin6, Min8(sizeof(fin6))); ip2user.erase(it); @@ -1701,52 +1660,25 @@ return Send_FIN_6(iaUser, sip, it); //----------------------------------------------------------------------------- int AUTH_IA::Send_FIN_8(IA_USER * iaUser, uint32_t sip, std::map::iterator it) { -strcpy((char*)fin8.hdr.magic, IA_ID); +memcpy(fin8.hdr.magic, IA_ID, strlen(IA_ID)); fin8.hdr.protoVer[0] = 0; fin8.hdr.protoVer[1] = 8; fin8.len = Min8(sizeof(FIN_8)); -strcpy((char*)fin8.type, "FIN"); -strcpy((char*)fin8.ok, "OK"); +memcpy(fin8.type, "FIN", 3); +memcpy(fin8.ok, "OK", 2); #ifdef ARCH_BE SwapBytes(fin8.len); #endif -Encrypt(&iaUser->ctx, (char*)&fin8, (char*)&fin8, Min8(sizeof(fin8))/8); +EncryptString(&fin8, &fin8, Min8(sizeof(fin8)), &iaUser->ctx); users->Unauthorize(iaUser->login, this); -int res = Send(sip, iaUser->port, (char*)&fin8, Min8(sizeof(fin8))); +int res = Send(sip, iaUser->port, &fin8, Min8(sizeof(fin8))); ip2user.erase(it); return res; } -namespace -{ -//----------------------------------------------------------------------------- -inline -void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password) -{ -unsigned char keyL[PASSWD_LEN]; -memset(keyL, 0, PASSWD_LEN); -strncpy((char *)keyL, password.c_str(), PASSWD_LEN); -Blowfish_Init(ctx, keyL, PASSWD_LEN); -} -//----------------------------------------------------------------------------- -inline -void Decrypt(BLOWFISH_CTX * ctx, void * dst, const void * src, size_t len8) -{ -for (size_t i = 0; i < len8; i++) - DecodeString(static_cast(dst) + i * 8, static_cast(src) + i * 8, ctx); -} -//----------------------------------------------------------------------------- -inline -void Encrypt(BLOWFISH_CTX * ctx, void * dst, const void * src, size_t len8) -{ -for (size_t i = 0; i < len8; i++) - EncodeString(static_cast(dst) + i * 8, static_cast(src) + i * 8, ctx); -} -//----------------------------------------------------------------------------- -}