]> git.stg.codes - stg.git/blobdiff - include/stg/user_ips.h
Minor changes.
[stg.git] / include / stg / user_ips.h
index 17bb59be1bc9f1e09cb7cf3727d2fec1db7eae76..bb920957e57e91baf5740322a440eb854975df69 100644 (file)
 #ifndef USER_IPS_H
 #define USER_IPS_H
 
-#ifdef FREE_BSD
-#include <sys/types.h>
-#endif
-
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#include "stg/common.h"
+#include "os_int.h"
 
 #include <cstring>
 #include <vector>
 #include <string>
 #include <iostream>
-#include <sstream>
 
-#include "stg/common.h"
-#include "os_int.h"
+#ifdef FREE_BSD
+#include <sys/types.h>
+#endif
 
-using namespace std;
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
 
 //-------------------------------------------------------------------------
 struct IP_MASK
 {
 IP_MASK() : ip(0), mask(0) {}
-IP_MASK(const IP_MASK & ipm) : ip(ipm.ip), mask(ipm.mask)  {}
 uint32_t ip;
 uint32_t mask;
 };
 //-------------------------------------------------------------------------
 class USER_IPS
 {
-    friend std::ostream & operator<< (ostream & o, const USER_IPS & i);
-    //friend stringstream & operator<< (stringstream & s, const USER_IPS & i);
-    friend const USER_IPS StrToIPS(const string & ipsStr) throw(string);
+    friend std::ostream & operator<< (std::ostream & o, const USER_IPS & i);
+    friend const USER_IPS StrToIPS(const std::string & ipsStr);
 
 public:
-    USER_IPS();
-    USER_IPS(const USER_IPS &);
-    USER_IPS & operator=(const USER_IPS &);
-    const IP_MASK & operator[](int idx) const;
+    typedef std::vector<IP_MASK> ContainerType;
+    typedef ContainerType::size_type IndexType;
+
+    const IP_MASK & operator[](IndexType idx) const { return ips[idx]; }
     std::string GetIpStr() const;
     bool IsIPInIPS(uint32_t ip) const;
     bool OnlyOneIP() const;
-    int  Count() const;
-    void Add(const IP_MASK &im);
-    void Erase();
+    bool IsAnyIP() const;
+    size_t Count() const { return ips.size(); }
+    void Add(const IP_MASK &im) { ips.push_back(im); }
 
 private:
     uint32_t CalcMask(unsigned int msk) const;
-    std::vector<IP_MASK> ips;
+    ContainerType ips;
 };
 //-------------------------------------------------------------------------
 
-//-----------------------------------------------------------------------------
-inline
-USER_IPS::USER_IPS()
-    : ips()
-{}
-//-----------------------------------------------------------------------------
-inline
-USER_IPS::USER_IPS(const USER_IPS & i)
-    : ips(i.ips)
-{}
-//-----------------------------------------------------------------------------
-inline
-USER_IPS & USER_IPS::operator=(const USER_IPS & i)
-{
-ips = i.ips;
-return *this;
-}
-//-----------------------------------------------------------------------------
-inline
-const IP_MASK & USER_IPS::operator[](int idx) const
-{
-return ips[idx];
-}
-//-----------------------------------------------------------------------------
 inline
 std::string USER_IPS::GetIpStr() const
 {
 if (ips.empty())
-    {
     return "";
-    }
 
 if (ips[0].ip == 0)
-    {
     return "*";
-    }
 
-std::vector<IP_MASK>::const_iterator it(ips.begin());
-std::stringstream s;
-s << inet_ntostring(it->ip);
+ContainerType::const_iterator it(ips.begin());
+std::string res = inet_ntostring(it->ip);
 ++it;
 for (; it != ips.end(); ++it)
-    {
-    s << "," << inet_ntostring(it->ip);
-    }
-return s.str();
+    res += "," + inet_ntostring(it->ip);
+return res;
 }
 //-----------------------------------------------------------------------------
 inline
-int USER_IPS::Count() const
+uint32_t USER_IPS::CalcMask(unsigned int msk) const
 {
-return ips.size();
+if (msk > 32)
+    return 0;
+return htonl(0xFFffFFff << (32 - msk));
 }
 //-----------------------------------------------------------------------------
 inline
 bool USER_IPS::IsIPInIPS(uint32_t ip) const
 {
 if (ips.empty())
-    {
     return false;
-    }
 
 if (ips.front().ip == 0)
     return true;
 
-for (std::vector<IP_MASK>::const_iterator it(ips.begin()); it != ips.end(); ++it)
+for (ContainerType::const_iterator it(ips.begin()); it != ips.end(); ++it)
     {
     uint32_t mask(CalcMask(it->mask));
     if ((ip & mask) == (it->ip & mask))
@@ -156,30 +120,16 @@ return false;
 inline
 bool USER_IPS::OnlyOneIP() const
 {
-if (ips.size() == 1 && ips.front().mask == 32)
+if (ips.size() == 1 && ips.front().mask == 32 && ips.front().ip != 0)
     return true;
 
 return false;
 }
 //-----------------------------------------------------------------------------
 inline
-uint32_t USER_IPS::CalcMask(unsigned int msk) const
-{
-if (msk > 32)
-    return 0;
-return htonl(0xFFffFFff << (32 - msk));
-}
-//-----------------------------------------------------------------------------
-inline
-void USER_IPS::Add(const IP_MASK &im)
-{
-ips.push_back(im);
-}
-//-----------------------------------------------------------------------------
-inline
-void USER_IPS::Erase()
+bool USER_IPS::IsAnyIP() const
 {
-ips.erase(ips.begin(), ips.end());
+    return !ips.empty() && ips.front().ip == 0;
 }
 //-----------------------------------------------------------------------------
 inline
@@ -188,46 +138,33 @@ std::ostream & operator<<(std::ostream & o, const USER_IPS & i)
 return o << i.GetIpStr();
 }
 //-----------------------------------------------------------------------------
-/*inline
-stringstream & operator<<(std::stringstream & s, const USER_IPS & i)
-{
-s << i.GetIpStr();
-return s;
-}*/
-//-----------------------------------------------------------------------------
 inline
-const USER_IPS StrToIPS(const std::string & ipsStr) throw(std::string)
+const USER_IPS StrToIPS(const std::string & ipsStr)
 {
 USER_IPS ips;
-char * paddr;
-IP_MASK im;
 std::vector<std::string> ipMask;
-std::string err;
 if (ipsStr.empty())
-    {
     return ips;
-    }
 
 if (ipsStr[0] == '*' && ipsStr.size() == 1)
     {
-    im.ip = 0;
-    im.mask = 0;
-    ips.ips.push_back(im);
+    ips.ips.push_back(IP_MASK());
     return ips;
     }
 
-char * str = new char[ipsStr.size() + 1];
-strcpy(str, ipsStr.c_str());
-char * pstr = str;
+char * tmp = new char[ipsStr.size() + 1];
+strcpy(tmp, ipsStr.c_str());
+char * pstr = tmp;
+char * paddr = NULL;
 while ((paddr = strtok(pstr, ",")))
     {
     pstr = NULL;
     ipMask.push_back(paddr);
     }
 
-delete[] str;
+delete[] tmp;
 
-for (unsigned int i = 0; i < ipMask.size(); i++)
+for (USER_IPS::IndexType i = 0; i < ipMask.size(); i++)
     {
     char str[128];
     char * strIp;
@@ -235,41 +172,28 @@ for (unsigned int i = 0; i < ipMask.size(); i++)
     strcpy(str, ipMask[i].c_str());
     strIp = strtok(str, "/");
     if (strIp == NULL)
-        {
-        err = "Incorrect IP address " + ipsStr;
         return ips;
-        }
     strMask = strtok(NULL, "/");
 
+    IP_MASK im;
+
     im.ip = inet_addr(strIp);
     if (im.ip == INADDR_NONE)
-        {
-        err = "Incorrect IP address: " + std::string(strIp);
         return ips;
-        }
 
     im.mask = 32;
     if (strMask != NULL)
         {
         int m = 0;
         if (str2x(strMask, m) != 0)
-            {
-            err = "Incorrect mask: " + std::string(strMask);
             return ips;
-            }
         im.mask = m;
 
         if (im.mask > 32)
-            {
-            err = "Incorrect mask: " + std::string(strMask);
             return ips;
-            }
 
         if ((im.ip & ips.CalcMask(im.mask)) != im.ip)
-            {
-            err = "Address does'n match mask: " + std::string(strIp) + "/" + std::string(strMask);
             return ips;
-            }
         }
     ips.ips.push_back(im);
     }