]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp
Replace deprecated usleep with POSIX-compliant nanosleep
[stg.git] / projects / stargazer / plugins / authorization / inetaccess / inetaccess.cpp
index f1f20dc343a5a920727368f60dc4781eb2a980a7..4986080eb82a1b4c212e3a2a7b6bbef069bf6320 100644 (file)
@@ -30,7 +30,7 @@
 
 #include <sys/types.h>
 #include <sys/socket.h>
-#include <unistd.h> // usleep, close
+#include <unistd.h> // close
 
 #include <csignal>
 #include <cstdlib>
@@ -70,6 +70,7 @@ AUTH_IA_SETTINGS::AUTH_IA_SETTINGS()
     : userDelay(0),
       userTimeout(0),
       port(0),
+      errorStr(),
       freeMbShowType(freeMbCash)
 {
 }
@@ -173,13 +174,15 @@ 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)
+    : phase(1),
+      phaseTime()
 {
 gettimeofday(&phaseTime, NULL);
 }
@@ -295,12 +298,29 @@ return phaseTime;
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 AUTH_IA::AUTH_IA()
-    : nonstop(false),
+    : ctxS(),
+      errorStr(),
+      iaSettings(),
+      settings(),
+      nonstop(false),
       isRunningRun(false),
       isRunningRunTimeouter(false),
       users(NULL),
       stgSettings(NULL),
+      ip2user(),
+      recvThread(),
+      timeouterThread(),
+      mutex(),
       listenSocket(-1),
+      connSynAck6(),
+      connSynAck8(),
+      disconnSynAck6(),
+      disconnSynAck8(),
+      aliveSyn6(),
+      aliveSyn8(),
+      fin6(),
+      fin8(),
+      packetTypes(),
       WriteServLog(GetStgLogger()),
       enabledDirs(0xFFffFFff),
       onDelUserNotifier(*this)
@@ -408,7 +428,8 @@ if (isRunningRun)
     //5 seconds to thread stops itself
     for (int i = 0; i < 25 && isRunningRun; i++)
         {
-        usleep(200000);
+        struct timespec ts = {0, 200000000};
+        nanosleep(&ts, NULL);
         }
 
     //after 5 seconds waiting thread still running. now killing it
@@ -421,7 +442,10 @@ if (isRunningRun)
             return -1;
             }
         for (int i = 0; i < 25 && isRunningRun; ++i)
-            usleep(200000);
+            {
+            struct timespec ts = {0, 200000000};
+            nanosleep(&ts, NULL);
+            }
         if (isRunningRun)
             {
             printfd(__FILE__, "Failed to stop recv thread\n");
@@ -441,7 +465,8 @@ if (isRunningRunTimeouter)
     //5 seconds to thread stops itself
     for (int i = 0; i < 25 && isRunningRunTimeouter; i++)
         {
-        usleep(200000);
+        struct timespec ts = {0, 200000000};
+        nanosleep(&ts, NULL);
         }
 
     //after 5 seconds waiting thread still running. now killing it
@@ -453,7 +478,10 @@ if (isRunningRunTimeouter)
             return -1;
             }
         for (int i = 0; i < 25 && isRunningRunTimeouter; ++i)
-            usleep(200000);
+            {
+            struct timespec ts = {0, 200000000};
+            nanosleep(&ts, NULL);
+            }
         if (isRunningRunTimeouter)
             {
             printfd(__FILE__, "Failed to stop timeouter thread\n");
@@ -505,7 +533,8 @@ int a = -1;
 string monFile = ia->stgSettings->GetMonitorDir() + "/inetaccess_t";
 while (ia->nonstop)
     {
-    usleep(20000);
+    struct timespec ts = {0, 20000000};
+    nanosleep(&ts, NULL);
     ia->Timeouter();
     // TODO change counter to timer and MONITOR_TIME_DELAY_SEC
     if (++a % (50 * 60) == 0 && ia->stgSettings->GetMonitoring())
@@ -599,13 +628,14 @@ uint32_t sip = outerAddr.sin_addr.s_addr;
 uint16_t sport = htons(outerAddr.sin_port);
 
 USER_PTR user;
-if (users->FindByName(login, &user) == 0)
+if (users->FindByName(login, &user))
     {
     WriteServLog("User's connect failed: user '%s' not found. IP %s",
                  login,
                  inet_ntostring(sip).c_str());
-    printfd(__FILE__, "User %s NOT found!\n", login);
+    printfd(__FILE__, "User '%s' NOT found!\n", login);
     SendError(sip, sport, protoVer, "îÅÐÒÁ×ÉÌØÎÙÊ ÌÏÇÉÎ!");
+    return -1;
     }
 
 printfd(__FILE__, "User '%s' FOUND!\n", user->GetLogin().c_str());
@@ -775,22 +805,13 @@ if (it == ip2user.end())
 
     printfd(__FILE__, "Add new user '%s' from ip %s\n",
             login.c_str(), inet_ntostring(sip).c_str());
-    ip2user[sip].login = login;
-    ip2user[sip].user = user;
-    ip2user[sip].protoVer = protoVer;
-    ip2user[sip].port = sport;
+    std::pair<std::map<uint32_t, IA_USER>::iterator, bool> res;
+    res = ip2user.insert(std::make_pair(sip, IA_USER(login, user, sport, protoVer)));
+    it = res.first;
     #ifdef IA_PHASE_DEBUG
-    ip2user[sip].phase.SetLogFileName(stgSettings->GetLogFileName());
-    ip2user[sip].phase.SetUserLogin(login);
+    it->second.phase.SetLogFileName(stgSettings->GetLogFileName());
+    it->second.phase.SetUserLogin(login);
     #endif
-
-
-    it = ip2user.find(sip); //TODO
-    if (it == ip2user.end())
-        {
-        printfd(__FILE__, "+++ ERROR +++\n");
-        return -1;
-        }
     }
 else if (user->GetID() != it->second.user->GetID())
     {
@@ -808,9 +829,6 @@ else if (user->GetID() != it->second.user->GetID())
 
 IA_USER * iaUser = &(it->second);
 
-if (iaUser->port != sport)
-    iaUser->port = sport;
-
 if (iaUser->password != user->GetProperty().password.Get())
     {
     InitEncrypt(&iaUser->ctx, user->GetProperty().password.Get());
@@ -1167,7 +1185,7 @@ SwapBytes(connAck->len);
 SwapBytes(connAck->rnd);
 #endif
 printfd( __FILE__, "CONN_ACK_6 %s\n", connAck->type);
-// ÕÓÔÁÎÏ×ÉÔØ ÎÏ×ÕÀ ÆÁÚÕ É ×ÒÅÍÑ É ÒÁÚÒÅÛÉÔØ ÉÎÅÔ
+
 if ((iaUser->phase.GetPhase() == 2) && (connAck->rnd == iaUser->rnd + 1))
     {
     iaUser->phase.UpdateTime();
@@ -1644,9 +1662,12 @@ SwapBytes(fin6.len);
 Encrypt(&iaUser->ctx, (char*)&fin6, (char*)&fin6, Min8(sizeof(fin6))/8);
 
 users->Unauthorize(iaUser->login, this);
+
+int res = Send(sip, iaSettings.GetUserPort(), (char*)&fin6, Min8(sizeof(fin6)));
+
 ip2user.erase(it);
 
-return Send(sip, iaSettings.GetUserPort(), (char*)&fin6, Min8(sizeof(fin6)));
+return res;
 }
 //-----------------------------------------------------------------------------
 int AUTH_IA::Send_FIN_7(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
@@ -1671,9 +1692,12 @@ SwapBytes(fin8.len);
 Encrypt(&iaUser->ctx, (char*)&fin8, (char*)&fin8, Min8(sizeof(fin8))/8);
 
 users->Unauthorize(iaUser->login, this);
+
+int res = Send(sip, iaUser->port, (char*)&fin8, Min8(sizeof(fin8)));
+
 ip2user.erase(it);
 
-return Send(sip, iaUser->port, (char*)&fin8, Min8(sizeof(fin8)));
+return res;
 }
 //-----------------------------------------------------------------------------
 inline