]> git.stg.codes - stg.git/blobdiff - projects/rscriptd/listener.cpp
Remove 'stg' prefixes from headers and add it to libraries
[stg.git] / projects / rscriptd / listener.cpp
index 87a85ebbab70cd6e7c27a7e9962f8dd754764cc4..47adc93b82aed9c29491f5f7532a077c415b9d45 100644 (file)
 #include <unistd.h>
 
 #include <csignal>
+#include <cerrno>
 #include <ctime>
 #include <cstring>
 #include <sstream>
 #include <algorithm>
 
+#include "stg/script_executer.h"
+#include "stg/locker.h"
+#include "stg/common.h"
 #include "listener.h"
-#include "script_executer.h"
-#include "stg_locker.h"
-#include "common.h"
+
+void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password);
+void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
 
 //-----------------------------------------------------------------------------
 LISTENER::LISTENER()
@@ -52,7 +56,7 @@ version = "rscriptd listener v.1.2";
 pthread_mutex_init(&mutex, NULL);
 }
 //-----------------------------------------------------------------------------
-void LISTENER::SetPassword(const string & p)
+void LISTENER::SetPassword(const std::string & p)
 {
 password = p;
 printfd(__FILE__, "Encryption initiated with password \'%s\'\n", password.c_str());
@@ -158,9 +162,9 @@ return false;
 //-----------------------------------------------------------------------------
 void * LISTENER::Run(void * d)
 {
-LISTENER * ia = static_cast<LISTENER *>(d);
+LISTENER * listener = static_cast<LISTENER *>(d);
 
-ia->Runner();
+listener->Runner();
 
 return NULL;
 }
@@ -179,9 +183,9 @@ receiverStopped = true;
 //-----------------------------------------------------------------------------
 void * LISTENER::RunProcessor(void * d)
 {
-LISTENER * ia = static_cast<LISTENER *>(d);
+LISTENER * listener = static_cast<LISTENER *>(d);
 
-ia->ProcessorRunner();
+listener->ProcessorRunner();
 
 return NULL;
 }
@@ -211,8 +215,6 @@ if (listenSocket < 0)
     return true;
     }
 
-printfd(__FILE__, "Port: %d\n", port);
-
 struct sockaddr_in listenAddr;
 listenAddr.sin_family = AF_INET;
 listenAddr.sin_port = htons(port);
@@ -246,7 +248,7 @@ RS_PACKET_HEADER packetHead;
 iov[0].iov_base = reinterpret_cast<char *>(&packetHead);
 iov[0].iov_len = sizeof(packetHead);
 iov[1].iov_base = buffer;
-iov[1].iov_len = sizeof(buffer);
+iov[1].iov_len = sizeof(buffer) - sizeof(packetHead);
 
 size_t dataLen = 0;
 while (dataLen < sizeof(buffer))
@@ -317,9 +319,9 @@ if (strncmp((char *)packetTail.magic, RS_ID, RS_MAGIC_LEN))
     }
 
 std::stringstream params;
-params << data.login << " "
+params << "\"" << data.login << "\" "
        << inet_ntostring(data.ip) << " "
-       << ntohl(data.id) << " "
+       << data.id << " "
        << (char *)packetTail.params;
 
 data.params = params.str();
@@ -329,9 +331,10 @@ return false;
 //-----------------------------------------------------------------------------
 void LISTENER::ProcessPending()
 {
-printfd(__FILE__, "Pending data size: %d\n", pending.size());
 std::list<PendingData>::iterator it(pending.begin());
-while (it != pending.end())
+size_t count = 0;
+printfd(__FILE__, "Pending: %d\n", pending.size());
+while (it != pending.end() && count < 256)
     {
     std::vector<AliveData>::iterator uit(
             std::lower_bound(
@@ -371,10 +374,11 @@ while (it != pending.end())
             users.erase(uit);
             }
         }
-
-    STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-    pending.erase(it++);
+    ++it;
+    ++count;
     }
+STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+pending.erase(pending.begin(), it);
 }
 //-----------------------------------------------------------------------------
 void LISTENER::ProcessTimeouts()
@@ -439,23 +443,6 @@ else
 return false;
 }
 //-----------------------------------------------------------------------------
-void LISTENER::InitEncrypt(BLOWFISH_CTX * ctx, const 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);
-}
-//-----------------------------------------------------------------------------
-void LISTENER::Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
-{
-if (dst != src)
-    memcpy(dst, src, len8 * 8);
-
-for (int i = 0; i < len8; i++)
-    Blowfish_Decrypt(ctx, (uint32_t *)(dst + i * 8), (uint32_t *)(dst + i * 8 + 4));
-}
-//-----------------------------------------------------------------------------
 bool LISTENER::CheckHeader(const RS_PACKET_HEADER & header) const
 {
 if (strncmp((char *)header.magic, RS_ID, RS_MAGIC_LEN))
@@ -497,3 +484,22 @@ if (res == 0) // Timeout
 return true;
 }
 //-----------------------------------------------------------------------------
+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, char * dst, const char * src, int len8)
+{
+if (dst != src)
+    memcpy(dst, src, len8 * 8);
+
+for (int i = 0; i < len8; i++)
+    Blowfish_Decrypt(ctx, (uint32_t *)(dst + i * 8), (uint32_t *)(dst + i * 8 + 4));
+}
+//-----------------------------------------------------------------------------