From 02f215c550418f180d0d1b2694c99a7a8d4dc4b2 Mon Sep 17 00:00:00 2001
From: Maxim Mamontov <faust.madf@gmail.com>
Date: Wed, 31 Aug 2011 12:10:24 +0300
Subject: [PATCH 1/1] WaitPackets moved to common.lib

---
 projects/rscriptd/listener.cpp                | 28 ---------------
 projects/rscriptd/listener.h                  |  1 -
 .../authorization/inetaccess/inetaccess.cpp   | 28 ---------------
 .../authorization/inetaccess/inetaccess.h     |  2 --
 .../plugins/capture/cap_nf/cap_nf.cpp         | 34 ++-----------------
 .../stargazer/plugins/capture/cap_nf/cap_nf.h |  2 --
 .../plugins/capture/ether_linux/ether_cap.cpp | 28 ---------------
 .../plugins/capture/ether_linux/ether_cap.h   |  1 -
 .../stargazer/plugins/other/radius/radius.cpp | 30 +---------------
 .../stargazer/plugins/other/radius/radius.h   |  2 --
 .../stargazer/plugins/other/smux/utils.cpp    | 34 +++++--------------
 projects/stargazer/plugins/other/smux/utils.h |  1 -
 stglibs/common.lib/common.cpp                 | 25 ++++++++++++++
 stglibs/common.lib/common.h                   |  2 ++
 14 files changed, 40 insertions(+), 178 deletions(-)

diff --git a/projects/rscriptd/listener.cpp b/projects/rscriptd/listener.cpp
index 39e590b6..9e03988c 100644
--- a/projects/rscriptd/listener.cpp
+++ b/projects/rscriptd/listener.cpp
@@ -456,34 +456,6 @@ if (strncmp((char *)header.protoVer, "02", RS_PROTO_VER_LEN))
 return false;
 }
 //-----------------------------------------------------------------------------
-bool LISTENER::WaitPackets(int sd) const
-{
-fd_set rfds;
-FD_ZERO(&rfds);
-FD_SET(sd, &rfds);
-
-struct timeval tv;
-tv.tv_sec = 0;
-tv.tv_usec = 500000;
-
-int res = select(sd + 1, &rfds, NULL, NULL, &tv);
-if (res == -1) // Error
-    {
-    if (errno != EINTR)
-        {
-        printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
-        }
-    return false;
-    }
-
-if (res == 0) // Timeout
-    {
-    return false;
-    }
-
-return true;
-}
-//-----------------------------------------------------------------------------
 inline
 void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password)
 {
diff --git a/projects/rscriptd/listener.h b/projects/rscriptd/listener.h
index 1c88149a..f4d2856e 100644
--- a/projects/rscriptd/listener.h
+++ b/projects/rscriptd/listener.h
@@ -94,7 +94,6 @@ private:
     // Networking stuff
     bool                PrepareNet();
     bool                FinalizeNet();
-    bool                WaitPackets(int sd) const;
     bool                RecvPacket();
     // Parsing stuff
     bool                CheckHeader(const RS_PACKET_HEADER & header) const;
diff --git a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp
index 58d9259f..d74c42ae 100644
--- a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp
+++ b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp
@@ -1764,34 +1764,6 @@ ip2user.erase(it);
 return ret;
 }
 //-----------------------------------------------------------------------------
-bool AUTH_IA::WaitPackets(int sd) const
-{
-fd_set rfds;
-FD_ZERO(&rfds);
-FD_SET(sd, &rfds);
-
-struct timeval tv;
-tv.tv_sec = 0;
-tv.tv_usec = 500000;
-
-int res = select(sd + 1, &rfds, NULL, NULL, &tv);
-if (res == -1) // Error
-    {
-    if (errno != EINTR)
-        {
-        printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
-        }
-    return false;
-    }
-
-if (res == 0) // Timeout
-    {
-    return false;
-    }
-
-return true;
-}
-//-----------------------------------------------------------------------------
 inline
 void InitEncrypt(BLOWFISH_CTX * ctx, const string & password)
 {
diff --git a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.h b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.h
index a648ec04..ab0c20ee 100644
--- a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.h
+++ b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.h
@@ -283,8 +283,6 @@ private:
     int                 RealSendMessage7(const STG_MSG & msg, uint32_t ip, IA_USER & user);
     int                 RealSendMessage8(const STG_MSG & msg, uint32_t ip, IA_USER & user);
 
-    bool                WaitPackets(int sd) const;
-
     BLOWFISH_CTX        ctxS;        //for loginS
 
     mutable std::string errorStr;
diff --git a/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp b/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp
index 3d6c55b5..a572e8a1 100644
--- a/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp
+++ b/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp
@@ -248,7 +248,7 @@ socklen_t slen;
 cap->stoppedUDP = false;
 while (cap->runningUDP)
     {
-    if (!cap->WaitPackets(cap->sockUDP))
+    if (!WaitPackets(cap->sockUDP))
         {
         continue;
         }
@@ -291,7 +291,7 @@ socklen_t slen;
 cap->stoppedTCP = false;
 while (cap->runningTCP)
     {
-    if (!cap->WaitPackets(cap->sockTCP))
+    if (!WaitPackets(cap->sockTCP))
         {
         continue;
         }
@@ -312,7 +312,7 @@ while (cap->runningTCP)
         continue;
         }
 
-    if (!cap->WaitPackets(sd))
+    if (!WaitPackets(sd))
         {
         close(sd);
         continue;
@@ -385,31 +385,3 @@ for (int i = 0; i < packets; ++i)
     traffCnt->Process(ip);
     }
 }
-
-bool NF_CAP::WaitPackets(int sd) const
-{
-fd_set rfds;
-FD_ZERO(&rfds);
-FD_SET(sd, &rfds);
-
-struct timeval tv;
-tv.tv_sec = 0;
-tv.tv_usec = 500000;
-
-int res = select(sd + 1, &rfds, NULL, NULL, &tv);
-if (res == -1) // Error
-    {
-    if (errno != EINTR)
-        {
-        printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
-        }
-    return false;
-    }
-
-if (res == 0) // Timeout
-    {
-    return false;
-    }
-
-return true;
-}
diff --git a/projects/stargazer/plugins/capture/cap_nf/cap_nf.h b/projects/stargazer/plugins/capture/cap_nf/cap_nf.h
index ccdd6708..3b531353 100644
--- a/projects/stargazer/plugins/capture/cap_nf/cap_nf.h
+++ b/projects/stargazer/plugins/capture/cap_nf/cap_nf.h
@@ -128,8 +128,6 @@ private:
     bool OpenUDP();
     void CloseTCP() { close(sockTCP); }
     void CloseUDP() { close(sockUDP); }
-
-    bool WaitPackets(int sd) const;
 };
 
 extern "C" PLUGIN * GetPlugin();
diff --git a/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp b/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp
index bd075fee..d45b1755 100644
--- a/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp
+++ b/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp
@@ -219,31 +219,3 @@ if (-1 == res)
 
 return 0;
 }
-//-----------------------------------------------------------------------------
-bool ETHER_CAP::WaitPackets(int sd) const
-{
-fd_set rfds;
-FD_ZERO(&rfds);
-FD_SET(sd, &rfds);
-
-struct timeval tv;
-tv.tv_sec = 0;
-tv.tv_usec = 500000;
-
-int res = select(sd + 1, &rfds, NULL, NULL, &tv);
-if (res == -1) // Error
-    {
-    if (errno != EINTR)
-        {
-        printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
-        }
-    return false;
-    }
-
-if (res == 0) // Timeout
-    {
-    return false;
-    }
-
-return true;
-}
diff --git a/projects/stargazer/plugins/capture/ether_linux/ether_cap.h b/projects/stargazer/plugins/capture/ether_linux/ether_cap.h
index c03a6066..994fea8a 100644
--- a/projects/stargazer/plugins/capture/ether_linux/ether_cap.h
+++ b/projects/stargazer/plugins/capture/ether_linux/ether_cap.h
@@ -67,7 +67,6 @@ private:
     int                 EthCapOpen();
     int                 EthCapClose();
     int                 EthCapRead(void * buffer, int blen, char ** iface);
-    bool                WaitPackets(int sd) const;
 
     mutable std::string errorStr;
 
diff --git a/projects/stargazer/plugins/other/radius/radius.cpp b/projects/stargazer/plugins/other/radius/radius.cpp
index 1dfff791..8ea93c9d 100644
--- a/projects/stargazer/plugins/other/radius/radius.cpp
+++ b/projects/stargazer/plugins/other/radius/radius.cpp
@@ -295,7 +295,7 @@ rad->isRunning = true;
 
 while (rad->nonstop)
     {
-    if (!rad->WaitPackets(rad->sock))
+    if (!WaitPackets(rad->sock))
         {
         continue;
         }
@@ -588,34 +588,6 @@ bool RADIUS::IsAllowedService(const std::string & svc) const
 return CanAuthService(svc) || CanAcctService(svc);
 }
 //-----------------------------------------------------------------------------
-bool RADIUS::WaitPackets(int sd) const
-{
-fd_set rfds;
-FD_ZERO(&rfds);
-FD_SET(sd, &rfds);
-
-struct timeval tv;
-tv.tv_sec = 0;
-tv.tv_usec = 500000;
-
-int res = select(sd + 1, &rfds, NULL, NULL, &tv);
-if (res == -1) // Error
-    {
-    if (errno != EINTR)
-        {
-        printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
-        }
-    return false;
-    }
-
-if (res == 0) // Timeout
-    {
-    return false;
-    }
-
-return true;
-}
-//-----------------------------------------------------------------------------
 inline
 void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password)
 {
diff --git a/projects/stargazer/plugins/other/radius/radius.h b/projects/stargazer/plugins/other/radius/radius.h
index 2a60f117..43484907 100644
--- a/projects/stargazer/plugins/other/radius/radius.h
+++ b/projects/stargazer/plugins/other/radius/radius.h
@@ -125,8 +125,6 @@ private:
     bool                CanAcctService(const std::string & svc) const;
     bool                IsAllowedService(const std::string & svc) const;
 
-    bool                WaitPackets(int sd) const;
-
     void                PrintServices(const std::list<std::string> & svcs);
 
     struct Printer : public unary_function<std::string, void>
diff --git a/projects/stargazer/plugins/other/smux/utils.cpp b/projects/stargazer/plugins/other/smux/utils.cpp
index 3a3628f5..a67314cb 100644
--- a/projects/stargazer/plugins/other/smux/utils.cpp
+++ b/projects/stargazer/plugins/other/smux/utils.cpp
@@ -1,4 +1,3 @@
-#include <sys/select.h>
 #include <unistd.h> // write
 
 #include <cstring> // memset
@@ -15,30 +14,6 @@
 #include "pen.h"
 #include "utils.h"
 
-bool WaitPackets(int sd)
-{
-fd_set rfds;
-FD_ZERO(&rfds);
-FD_SET(sd, &rfds);
-
-struct timeval tv;
-tv.tv_sec = 0;
-tv.tv_usec = 500000;
-
-int res = select(sd + 1, &rfds, NULL, NULL, &tv);
-if (res == -1) // Error
-    {
-    if (errno != EINTR)
-        printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
-    return false;
-    }
-
-if (res == 0) // Timeout
-    return false;
-
-return true;
-}
-
 bool String2OI(const std::string & str, OBJECT_IDENTIFIER_t * oi)
 {
 size_t left = 0, pos = 0, arcPos = 0;
@@ -117,6 +92,8 @@ OCTET_STRING_fromString(&msg.choice.simple.password, "");
 char buffer[1024];
 error = der_encode_to_buffer(&asn_DEF_OpenPDU, &msg, buffer, sizeof(buffer));
 
+ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OpenPDU, &msg);
+
 if (error.encoded == -1)
     {
     printfd(__FILE__, "Could not encode OpenPDU (at %s)\n",
@@ -144,6 +121,8 @@ char buffer[1024];
 asn_enc_rval_t error;
 error = der_encode_to_buffer(&asn_DEF_ClosePDU, &msg, buffer, sizeof(buffer));
 
+ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_ClosePDU, &msg);
+
 if (error.encoded == -1)
     {
     printfd(__FILE__, "Could not encode ClosePDU (at %s)\n",
@@ -176,6 +155,8 @@ OBJECT_IDENTIFIER_set_arcs(&msg.subtree,
 char buffer[1024];
 error = der_encode_to_buffer(&asn_DEF_RReqPDU, &msg, buffer, sizeof(buffer));
 
+ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RReqPDU, &msg);
+
 if (error.encoded == -1)
     {
     printfd(__FILE__, "Could not encode RReqPDU (at %s)\n",
@@ -203,6 +184,7 @@ if (length < 1)
     return NULL;
 asn_dec_rval_t error;
 error = ber_decode(0, &asn_DEF_SMUX_PDUs, (void **)&pdus, buffer, length);
+
 if(error.code != RC_OK)
     {
     printfd(__FILE__, "Failed to decode PDUs at byte %ld\n",
@@ -253,6 +235,8 @@ char buffer[1024];
 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, &msg, buffer,
                              sizeof(buffer));
 
+ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_GetResponse_PDU, &msg);
+
 if (error.encoded == -1)
     {
     printfd(__FILE__, "Could not encode GetResponsePDU for error (at %s)\n",
diff --git a/projects/stargazer/plugins/other/smux/utils.h b/projects/stargazer/plugins/other/smux/utils.h
index 0814bfba..8007fee6 100644
--- a/projects/stargazer/plugins/other/smux/utils.h
+++ b/projects/stargazer/plugins/other/smux/utils.h
@@ -7,7 +7,6 @@
 #include "stg/SMUX-PDUs.h"
 #include "stg/GetResponse-PDU.h"
 
-bool WaitPackets(int sd);
 bool String2OI(const std::string & str, OBJECT_IDENTIFIER_t * oi);
 std::string OI2String(OBJECT_IDENTIFIER_t * oi);
 bool SendOpenPDU(int fd);
diff --git a/stglibs/common.lib/common.cpp b/stglibs/common.lib/common.cpp
index b9fb3bf9..3361adb3 100644
--- a/stglibs/common.lib/common.cpp
+++ b/stglibs/common.lib/common.cpp
@@ -64,6 +64,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <sys/select.h>
 #endif
 
 #include <iconv.h>
@@ -1032,3 +1033,27 @@ if (*val < min || *val > max)
 
 return 0;
 }
+
+bool WaitPackets(int sd)
+{
+fd_set rfds;
+FD_ZERO(&rfds);
+FD_SET(sd, &rfds);
+
+struct timeval tv;
+tv.tv_sec = 0;
+tv.tv_usec = 500000;
+
+int res = select(sd + 1, &rfds, NULL, NULL, &tv);
+if (res == -1) // Error
+    {
+    if (errno != EINTR)
+        printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
+    return false;
+    }
+
+if (res == 0) // Timeout
+    return false;
+
+return true;
+}
diff --git a/stglibs/common.lib/common.h b/stglibs/common.lib/common.h
index 19a8773e..ac091147 100644
--- a/stglibs/common.lib/common.h
+++ b/stglibs/common.lib/common.h
@@ -105,6 +105,8 @@ int ParseUnsignedInRange(const std::string & str, unsigned min,
                          unsigned max, unsigned * val);
 int ParseYesNo(const std::string & str, bool * val);
 
+bool WaitPackets(int sd);
+
 //-----------------------------------------------------------------------------
 template <typename varT>
 int str2x(const std::string & str, varT & x)
-- 
2.44.2