2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23 $Date: 2010/03/04 11:49:53 $
30 #include "stg/common.h"
39 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
46 //-------------------------------------------------------------------------
49 IP_MASK() : ip(0), mask(0) {}
53 //-------------------------------------------------------------------------
56 friend std::ostream & operator<< (std::ostream & o, const USER_IPS & i);
57 friend const USER_IPS StrToIPS(const std::string & ipsStr);
60 typedef std::vector<IP_MASK> ContainerType;
61 typedef ContainerType::size_type IndexType;
63 const IP_MASK & operator[](IndexType idx) const { return ips[idx]; }
64 std::string GetIpStr() const;
65 bool IsIPInIPS(uint32_t ip) const;
66 bool OnlyOneIP() const;
68 size_t Count() const { return ips.size(); }
69 void Add(const IP_MASK &im) { ips.push_back(im); }
72 uint32_t CalcMask(unsigned int msk) const;
75 //-------------------------------------------------------------------------
78 std::string USER_IPS::GetIpStr() const
86 ContainerType::const_iterator it(ips.begin());
87 std::string res = inet_ntostring(it->ip);
89 for (; it != ips.end(); ++it)
90 res += "," + inet_ntostring(it->ip);
93 //-----------------------------------------------------------------------------
95 uint32_t USER_IPS::CalcMask(unsigned int msk) const
99 return htonl(0xFFffFFff << (32 - msk));
101 //-----------------------------------------------------------------------------
103 bool USER_IPS::IsIPInIPS(uint32_t ip) const
108 if (ips.front().ip == 0)
111 for (ContainerType::const_iterator it(ips.begin()); it != ips.end(); ++it)
113 uint32_t mask(CalcMask(it->mask));
114 if ((ip & mask) == (it->ip & mask))
119 //-----------------------------------------------------------------------------
121 bool USER_IPS::OnlyOneIP() const
123 if (ips.size() == 1 && ips.front().mask == 32 && ips.front().ip != 0)
128 //-----------------------------------------------------------------------------
130 bool USER_IPS::IsAnyIP() const
132 return !ips.empty() && ips.front().ip == 0;
134 //-----------------------------------------------------------------------------
136 std::ostream & operator<<(std::ostream & o, const USER_IPS & i)
138 return o << i.GetIpStr();
140 //-----------------------------------------------------------------------------
142 const USER_IPS StrToIPS(const std::string & ipsStr)
145 std::vector<std::string> ipMask;
149 if (ipsStr[0] == '*' && ipsStr.size() == 1)
151 ips.ips.push_back(IP_MASK());
155 char * tmp = new char[ipsStr.size() + 1];
156 strcpy(tmp, ipsStr.c_str());
159 while ((paddr = strtok(pstr, ",")))
162 ipMask.push_back(paddr);
167 for (USER_IPS::IndexType i = 0; i < ipMask.size(); i++)
172 strcpy(str, ipMask[i].c_str());
173 strIp = strtok(str, "/");
176 strMask = strtok(NULL, "/");
180 im.ip = inet_addr(strIp);
181 if (im.ip == INADDR_NONE)
188 if (str2x(strMask, m) != 0)
195 if ((im.ip & ips.CalcMask(im.mask)) != im.ip)
198 ips.ips.push_back(im);
203 //-------------------------------------------------------------------------