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 $
31 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
49 //-------------------------------------------------------------------------
52 IP_MASK() : ip(0), mask(0) {}
53 IP_MASK(const IP_MASK & ipm) : ip(ipm.ip), mask(ipm.mask) {}
57 //-------------------------------------------------------------------------
60 friend std::ostream & operator<< (ostream & o, const USER_IPS & i);
61 //friend stringstream & operator<< (stringstream & s, const USER_IPS & i);
62 friend const USER_IPS StrToIPS(const string & ipsStr) throw(string);
66 USER_IPS(const USER_IPS &);
67 USER_IPS & operator=(const USER_IPS &);
68 const IP_MASK & operator[](int idx) const;
69 std::string GetIpStr() const;
70 bool IsIPInIPS(uint32_t ip) const;
71 bool OnlyOneIP() const;
73 void Add(const IP_MASK &im);
77 uint32_t CalcMask(unsigned int msk) const;
78 std::vector<IP_MASK> ips;
80 //-------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
89 USER_IPS::USER_IPS(const USER_IPS & i)
92 //-----------------------------------------------------------------------------
94 USER_IPS & USER_IPS::operator=(const USER_IPS & i)
99 //-----------------------------------------------------------------------------
101 const IP_MASK & USER_IPS::operator[](int idx) const
105 //-----------------------------------------------------------------------------
107 std::string USER_IPS::GetIpStr() const
119 std::vector<IP_MASK>::const_iterator it(ips.begin());
121 s << inet_ntostring(it->ip);
123 for (; it != ips.end(); ++it)
125 s << "," << inet_ntostring(it->ip);
129 //-----------------------------------------------------------------------------
131 int USER_IPS::Count() const
135 //-----------------------------------------------------------------------------
137 bool USER_IPS::IsIPInIPS(uint32_t ip) const
144 if (ips.front().ip == 0)
147 for (std::vector<IP_MASK>::const_iterator it(ips.begin()); it != ips.end(); ++it)
149 uint32_t mask(CalcMask(it->mask));
150 if ((ip & mask) == (it->ip & mask))
155 //-----------------------------------------------------------------------------
157 bool USER_IPS::OnlyOneIP() const
159 if (ips.size() == 1 && ips.front().mask == 32)
164 //-----------------------------------------------------------------------------
166 uint32_t USER_IPS::CalcMask(unsigned int msk) const
170 return htonl(0xFFffFFff << (32 - msk));
172 //-----------------------------------------------------------------------------
174 void USER_IPS::Add(const IP_MASK &im)
178 //-----------------------------------------------------------------------------
180 void USER_IPS::Erase()
182 ips.erase(ips.begin(), ips.end());
184 //-----------------------------------------------------------------------------
186 std::ostream & operator<<(std::ostream & o, const USER_IPS & i)
188 return o << i.GetIpStr();
190 //-----------------------------------------------------------------------------
192 stringstream & operator<<(std::stringstream & s, const USER_IPS & i)
197 //-----------------------------------------------------------------------------
199 const USER_IPS StrToIPS(const std::string & ipsStr) throw(std::string)
204 std::vector<std::string> ipMask;
211 if (ipsStr[0] == '*' && ipsStr.size() == 1)
215 ips.ips.push_back(im);
219 char * str = new char[ipsStr.size() + 1];
220 strcpy(str, ipsStr.c_str());
222 while ((paddr = strtok(pstr, ",")))
225 ipMask.push_back(paddr);
230 for (unsigned int i = 0; i < ipMask.size(); i++)
235 strcpy(str, ipMask[i].c_str());
236 strIp = strtok(str, "/");
239 err = "Incorrect IP address " + ipsStr;
242 strMask = strtok(NULL, "/");
244 im.ip = inet_addr(strIp);
245 if (im.ip == INADDR_NONE)
247 err = "Incorrect IP address: " + std::string(strIp);
255 if (str2x(strMask, m) != 0)
257 err = "Incorrect mask: " + std::string(strMask);
264 err = "Incorrect mask: " + std::string(strMask);
268 if ((im.ip & ips.CalcMask(im.mask)) != im.ip)
270 err = "Address does'n match mask: " + std::string(strIp) + "/" + std::string(strMask);
274 ips.ips.push_back(im);
279 //-------------------------------------------------------------------------