git.stg.codes
/
stg.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Removed rsconf.cpp.
[stg.git]
/
include
/
stg
/
user_ips.h
diff --git
a/include/stg/user_ips.h
b/include/stg/user_ips.h
index 17bb59be1bc9f1e09cb7cf3727d2fec1db7eae76..e55f99fa7fc114460681fcebe87c8639ef2ee259 100644
(file)
--- a/
include/stg/user_ips.h
+++ b/
include/stg/user_ips.h
@@
-44,8
+44,6
@@
#include "stg/common.h"
#include "os_int.h"
#include "stg/common.h"
#include "os_int.h"
-using namespace std;
-
//-------------------------------------------------------------------------
struct IP_MASK
{
//-------------------------------------------------------------------------
struct IP_MASK
{
@@
-57,25
+55,29
@@
uint32_t mask;
//-------------------------------------------------------------------------
class USER_IPS
{
//-------------------------------------------------------------------------
class USER_IPS
{
- friend std::ostream & operator<< (ostream & o, const USER_IPS & i);
+ friend std::ostream & operator<< (
std::
ostream & o, const USER_IPS & i);
//friend stringstream & operator<< (stringstream & s, const USER_IPS & i);
//friend stringstream & operator<< (stringstream & s, const USER_IPS & i);
- friend const USER_IPS StrToIPS(const st
ring & ipsStr) throw(string
);
+ friend const USER_IPS StrToIPS(const st
d::string & ipsStr
);
public:
public:
+ typedef std::vector<IP_MASK> ContainerType;
+ typedef ContainerType::size_type IndexType;
+
USER_IPS();
USER_IPS(const USER_IPS &);
USER_IPS & operator=(const USER_IPS &);
USER_IPS();
USER_IPS(const USER_IPS &);
USER_IPS & operator=(const USER_IPS &);
- const IP_MASK & operator[](
int
idx) const;
+ const IP_MASK & operator[](
IndexType
idx) const;
std::string GetIpStr() const;
bool IsIPInIPS(uint32_t ip) const;
bool OnlyOneIP() const;
std::string GetIpStr() const;
bool IsIPInIPS(uint32_t ip) const;
bool OnlyOneIP() const;
- int Count() const;
+ bool IsAnyIP() const;
+ size_t Count() const;
void Add(const IP_MASK &im);
void Erase();
private:
uint32_t CalcMask(unsigned int msk) const;
void Add(const IP_MASK &im);
void Erase();
private:
uint32_t CalcMask(unsigned int msk) const;
-
std::vector<IP_MASK>
ips;
+
ContainerType
ips;
};
//-------------------------------------------------------------------------
};
//-------------------------------------------------------------------------
@@
-98,7
+100,7
@@
return *this;
}
//-----------------------------------------------------------------------------
inline
}
//-----------------------------------------------------------------------------
inline
-const IP_MASK & USER_IPS::operator[](
int
idx) const
+const IP_MASK & USER_IPS::operator[](
IndexType
idx) const
{
return ips[idx];
}
{
return ips[idx];
}
@@
-116,8
+118,8
@@
if (ips[0].ip == 0)
return "*";
}
return "*";
}
-
std::vector<IP_MASK>
::const_iterator it(ips.begin());
-std::stringstream s;
+
ContainerType
::const_iterator it(ips.begin());
+std::
o
stringstream s;
s << inet_ntostring(it->ip);
++it;
for (; it != ips.end(); ++it)
s << inet_ntostring(it->ip);
++it;
for (; it != ips.end(); ++it)
@@
-128,12
+130,20
@@
return s.str();
}
//-----------------------------------------------------------------------------
inline
}
//-----------------------------------------------------------------------------
inline
-
in
t USER_IPS::Count() const
+
size_
t USER_IPS::Count() const
{
return ips.size();
}
//-----------------------------------------------------------------------------
inline
{
return ips.size();
}
//-----------------------------------------------------------------------------
inline
+uint32_t USER_IPS::CalcMask(unsigned int msk) const
+{
+if (msk > 32)
+ return 0;
+return htonl(0xFFffFFff << (32 - msk));
+}
+//-----------------------------------------------------------------------------
+inline
bool USER_IPS::IsIPInIPS(uint32_t ip) const
{
if (ips.empty())
bool USER_IPS::IsIPInIPS(uint32_t ip) const
{
if (ips.empty())
@@
-144,7
+154,7
@@
if (ips.empty())
if (ips.front().ip == 0)
return true;
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))
{
uint32_t mask(CalcMask(it->mask));
if ((ip & mask) == (it->ip & mask))
@@
-163,11
+173,9
@@
return false;
}
//-----------------------------------------------------------------------------
inline
}
//-----------------------------------------------------------------------------
inline
-
uint32_t USER_IPS::CalcMask(unsigned int msk
) const
+
bool USER_IPS::IsAnyIP(
) const
{
{
-if (msk > 32)
- return 0;
-return htonl(0xFFffFFff << (32 - msk));
+ return !ips.empty() && ips.front().ip == 0;
}
//-----------------------------------------------------------------------------
inline
}
//-----------------------------------------------------------------------------
inline
@@
-196,13
+204,12
@@
return s;
}*/
//-----------------------------------------------------------------------------
inline
}*/
//-----------------------------------------------------------------------------
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;
{
USER_IPS ips;
char * paddr;
IP_MASK im;
std::vector<std::string> ipMask;
-std::string err;
if (ipsStr.empty())
{
return ips;
if (ipsStr.empty())
{
return ips;
@@
-216,18
+223,18
@@
if (ipsStr[0] == '*' && ipsStr.size() == 1)
return ips;
}
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
;
while ((paddr = strtok(pstr, ",")))
{
pstr = NULL;
ipMask.push_back(paddr);
}
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;
{
char str[128];
char * strIp;
@@
-236,7
+243,6
@@
for (unsigned int i = 0; i < ipMask.size(); i++)
strIp = strtok(str, "/");
if (strIp == NULL)
{
strIp = strtok(str, "/");
if (strIp == NULL)
{
- err = "Incorrect IP address " + ipsStr;
return ips;
}
strMask = strtok(NULL, "/");
return ips;
}
strMask = strtok(NULL, "/");
@@
-244,7
+250,6
@@
for (unsigned int i = 0; i < ipMask.size(); i++)
im.ip = inet_addr(strIp);
if (im.ip == INADDR_NONE)
{
im.ip = inet_addr(strIp);
if (im.ip == INADDR_NONE)
{
- err = "Incorrect IP address: " + std::string(strIp);
return ips;
}
return ips;
}
@@
-254,20
+259,17
@@
for (unsigned int i = 0; i < ipMask.size(); i++)
int m = 0;
if (str2x(strMask, m) != 0)
{
int m = 0;
if (str2x(strMask, m) != 0)
{
- err = "Incorrect mask: " + std::string(strMask);
return ips;
}
im.mask = m;
if (im.mask > 32)
{
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)
{
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;
}
}
return ips;
}
}