]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/configuration/sgconfig/rsconf.cpp
Use netinet/in.h instead of linux/in.h.
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / rsconf.cpp
index afcb688172bf59864082e3757ae15ed0441544ad..c06b99dae83fb0d64ae220e232b069d17f3cc8c5 100644 (file)
@@ -29,7 +29,6 @@
 
 #include <cerrno>
 #include <csignal>
 
 #include <cerrno>
 #include <csignal>
-#include <cstdio> // snprintf
 #include <cstring> // strerror
 
 #include <unistd.h> // close
 #include <cstring> // strerror
 
 #include <unistd.h> // close
@@ -60,9 +59,6 @@ enum
 //-----------------------------------------------------------------------------
 int CONFIGPROTO::Prepare()
 {
 //-----------------------------------------------------------------------------
 int CONFIGPROTO::Prepare()
 {
-int res;
-struct sockaddr_in listenAddr;
-
 sigset_t sigmask, oldmask;
 sigemptyset(&sigmask);
 sigaddset(&sigmask, SIGINT);
 sigset_t sigmask, oldmask;
 sigemptyset(&sigmask);
 sigaddset(&sigmask, SIGINT);
@@ -75,11 +71,12 @@ listenSocket = socket(PF_INET, SOCK_STREAM, 0);
 
 if (listenSocket < 0)
     {
 
 if (listenSocket < 0)
     {
-    errorStr = "Create NET_CONFIGURATOR socket failed.";
+    errorStr = "Create socket failed.";
     logger("Cannot create a socket: %s", strerror(errno));
     return -1;
     }
 
     logger("Cannot create a socket: %s", strerror(errno));
     return -1;
     }
 
+struct sockaddr_in listenAddr;
 listenAddr.sin_family = PF_INET;
 listenAddr.sin_port = htons(port);
 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
 listenAddr.sin_family = PF_INET;
 listenAddr.sin_port = htons(port);
 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
@@ -93,17 +90,14 @@ if (0 != setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &lng, 4))
     return -1;
     }
 
     return -1;
     }
 
-res = bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr));
-
-if (res == -1)
+if (bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr)) == -1)
     {
     errorStr = "Bind admin socket failed";
     logger("Cannot bind the socket: %s", strerror(errno));
     return -1;
     }
 
     {
     errorStr = "Bind admin socket failed";
     logger("Cannot bind the socket: %s", strerror(errno));
     return -1;
     }
 
-res = listen(listenSocket, 0);
-if (res == -1)
+if (listen(listenSocket, 0) == -1)
     {
     errorStr = "Listen admin socket failed";
     logger("Cannot listen the socket: %s", strerror(errno));
     {
     errorStr = "Listen admin socket failed";
     logger("Cannot listen the socket: %s", strerror(errno));
@@ -118,18 +112,18 @@ return 0;
 int CONFIGPROTO::Stop()
 {
 nonstop = false;
 int CONFIGPROTO::Stop()
 {
 nonstop = false;
+shutdown(listenSocket, SHUT_RDWR);
 close(listenSocket);
 //TODO: Idiotism
 close(listenSocket);
 //TODO: Idiotism
-int                 sock;
 struct sockaddr_in  addr;
 struct sockaddr_in  addr;
-socklen_t           addrLen;
 addr.sin_family = PF_INET;
 addr.sin_port = htons(port);
 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
 
 addr.sin_family = PF_INET;
 addr.sin_port = htons(port);
 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
 
-addrLen = sizeof(addr);
-sock = socket(PF_INET, SOCK_STREAM, 0);
+socklen_t addrLen = sizeof(addr);
+int sock = socket(PF_INET, SOCK_STREAM, 0);
 connect(sock, (sockaddr*)&addr, addrLen);
 connect(sock, (sockaddr*)&addr, addrLen);
+shutdown(sock, SHUT_RDWR);
 close(sock);
 //Idiotism end
 return 0;
 close(sock);
 //Idiotism end
 return 0;
@@ -137,8 +131,6 @@ return 0;
 //-----------------------------------------------------------------------------
 void CONFIGPROTO::Run()
 {
 //-----------------------------------------------------------------------------
 void CONFIGPROTO::Run()
 {
-state = confHdr;
-
 while (nonstop)
     {
     state = confHdr;
 while (nonstop)
     {
     state = confHdr;
@@ -164,6 +156,7 @@ while (nonstop)
         {
         if (RecvHdr(outerSocket) < 0)
             {
         {
         if (RecvHdr(outerSocket) < 0)
             {
+            shutdown(outerSocket, SHUT_RDWR);
             close(outerSocket);
             continue;
             }
             close(outerSocket);
             continue;
             }
@@ -171,11 +164,13 @@ while (nonstop)
             {
             if (SendHdrAnswer(outerSocket, ans_ok) < 0)
                 {
             {
             if (SendHdrAnswer(outerSocket, ans_ok) < 0)
                 {
+                shutdown(outerSocket, SHUT_RDWR);
                 close(outerSocket);
                 continue;
                 }
             if (RecvLogin(outerSocket) < 0)
                 {
                 close(outerSocket);
                 continue;
                 }
             if (RecvLogin(outerSocket) < 0)
                 {
+                shutdown(outerSocket, SHUT_RDWR);
                 close(outerSocket);
                 continue;
                 }
                 close(outerSocket);
                 continue;
                 }
@@ -183,11 +178,13 @@ while (nonstop)
                 {
                 if (SendLoginAnswer(outerSocket) < 0)
                     {
                 {
                 if (SendLoginAnswer(outerSocket) < 0)
                     {
+                    shutdown(outerSocket, SHUT_RDWR);
                     close(outerSocket);
                     continue;
                     }
                 if (RecvLoginS(outerSocket) < 0)
                     {
                     close(outerSocket);
                     continue;
                     }
                 if (RecvLoginS(outerSocket) < 0)
                     {
+                    shutdown(outerSocket, SHUT_RDWR);
                     close(outerSocket);
                     continue;
                     }
                     close(outerSocket);
                     continue;
                     }
@@ -195,11 +192,13 @@ while (nonstop)
                     {
                     if (SendLoginSAnswer(outerSocket, ans_ok) < 0)
                         {
                     {
                     if (SendLoginSAnswer(outerSocket, ans_ok) < 0)
                         {
+                        shutdown(outerSocket, SHUT_RDWR);
                         close(outerSocket);
                         continue;
                         }
                     if (RecvData(outerSocket) < 0)
                         {
                         close(outerSocket);
                         continue;
                         }
                     if (RecvData(outerSocket) < 0)
                         {
+                        shutdown(outerSocket, SHUT_RDWR);
                         close(outerSocket);
                         continue;
                         }
                         close(outerSocket);
                         continue;
                         }
@@ -209,6 +208,7 @@ while (nonstop)
                     {
                     if (SendLoginSAnswer(outerSocket, ans_err) < 0)
                         {
                     {
                     if (SendLoginSAnswer(outerSocket, ans_err) < 0)
                         {
+                        shutdown(outerSocket, SHUT_RDWR);
                         close(outerSocket);
                         continue;
                         }
                         close(outerSocket);
                         continue;
                         }
@@ -225,6 +225,7 @@ while (nonstop)
             WriteLogAccessFailed(adminIP);
             if (SendHdrAnswer(outerSocket, ans_err) < 0)
                 {
             WriteLogAccessFailed(adminIP);
             if (SendHdrAnswer(outerSocket, ans_err) < 0)
                 {
+                shutdown(outerSocket, SHUT_RDWR);
                 close(outerSocket);
                 continue;
                 }
                 close(outerSocket);
                 continue;
                 }
@@ -235,6 +236,7 @@ while (nonstop)
         WriteLogAccessFailed(adminIP);
         }
     printfd(__FILE__, "Successfull connection from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str());
         WriteLogAccessFailed(adminIP);
         }
     printfd(__FILE__, "Successfull connection from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str());
+    shutdown(outerSocket, SHUT_RDWR);
     close(outerSocket);
     }
 }
     close(outerSocket);
     }
 }
@@ -480,7 +482,7 @@ while (1)
         if (ParseCommand())
             return SendError(sock, "Bad command");
         else
         if (ParseCommand())
             return SendError(sock, "Bad command");
         else
-            return SendDataAnswer(sock, currParser->GetAnswer());
+            return SendDataAnswer(sock, GetDataAnswer());
         }
     }
 //return 0;
         }
     }
 //return 0;
@@ -495,13 +497,13 @@ BLOWFISH_CTX ctx;
 InitContext(adminPassword.c_str(), ADM_PASSWD_LEN, &ctx);
 
 std::string::size_type pos = 0;
 InitContext(adminPassword.c_str(), ADM_PASSWD_LEN, &ctx);
 
 std::string::size_type pos = 0;
-std::string::size_type length = answer.length();
+std::string::size_type length = answer.length() + 1;
 while (pos < length)
     {
     char buffer[1024];
     std::string::size_type chunkLength = std::min(length - pos, sizeof(buffer));
     EncryptString(buffer, answer.c_str() + pos, chunkLength, &ctx);
 while (pos < length)
     {
     char buffer[1024];
     std::string::size_type chunkLength = std::min(length - pos, sizeof(buffer));
     EncryptString(buffer, answer.c_str() + pos, chunkLength, &ctx);
-    if (send(sock, buffer, chunkLength, 0) < 0)
+    if (send(sock, buffer, (chunkLength & ~7) < chunkLength ? chunkLength + 8 : chunkLength, 0) < 0) // Need to send data adjusted to the 8-byte boundary.
         return -1;
     pos += chunkLength;
     }
         return -1;
     pos += chunkLength;
     }