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 <algorithm>
36 /////////////////////////
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
46 //-------------------------------------------------------------------------
49 IP_MASK() : ip(0), mask(0) {}
50 IP_MASK(const IP_MASK & ipm) : ip(ipm.ip), mask(ipm.mask) {}
54 //-------------------------------------------------------------------------
57 friend std::ostream & operator<< (ostream & o, const USER_IPS & i);
58 //friend stringstream & operator<< (stringstream & s, const USER_IPS & i);
59 friend const USER_IPS StrToIPS(const string & ipsStr) throw(string);
63 USER_IPS(const USER_IPS &);
64 USER_IPS & operator=(const USER_IPS &);
65 const IP_MASK & operator[](int idx) const;
66 std::string GetIpStr() const;
67 bool IsIPInIPS(uint32_t ip) const;
68 bool OnlyOneIP() const;
70 void Add(const IP_MASK &im);
74 uint32_t CalcMask(unsigned int msk) const;
75 std::vector<IP_MASK> ips;
77 //-------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
86 USER_IPS::USER_IPS(const USER_IPS & i)
89 //-----------------------------------------------------------------------------
91 USER_IPS & USER_IPS::operator=(const USER_IPS & i)
96 //-----------------------------------------------------------------------------
98 const IP_MASK & USER_IPS::operator[](int idx) const
102 //-----------------------------------------------------------------------------
104 std::string USER_IPS::GetIpStr() const
116 std::vector<IP_MASK>::const_iterator it(ips.begin());
118 s << inet_ntostring(it->ip);
120 for (; it != ips.end(); ++it)
122 s << "," << inet_ntostring(it->ip);
126 //-----------------------------------------------------------------------------
128 int USER_IPS::Count() const
132 //-----------------------------------------------------------------------------
134 bool USER_IPS::IsIPInIPS(uint32_t ip) const
141 if (ips.front().ip == 0)
144 for (std::vector<IP_MASK>::const_iterator it(ips.begin()); it != ips.end(); ++it)
146 uint32_t mask(CalcMask(it->mask));
147 if ((ip & mask) == (it->ip & mask))
152 //-----------------------------------------------------------------------------
154 bool USER_IPS::OnlyOneIP() const
156 if (ips.size() == 1 && ips.front().mask == 32)
161 //-----------------------------------------------------------------------------
163 uint32_t USER_IPS::CalcMask(unsigned int msk) const
167 return htonl(0xFFffFFff << (32 - msk));
169 //-----------------------------------------------------------------------------
171 void USER_IPS::Add(const IP_MASK &im)
175 //-----------------------------------------------------------------------------
177 void USER_IPS::Erase()
179 ips.erase(ips.begin(), ips.end());
181 //-----------------------------------------------------------------------------
183 std::ostream & operator<<(std::ostream & o, const USER_IPS & i)
185 return o << i.GetIpStr();
187 //-----------------------------------------------------------------------------
189 stringstream & operator<<(std::stringstream & s, const USER_IPS & i)
194 //-----------------------------------------------------------------------------
196 const USER_IPS StrToIPS(const std::string & ipsStr) throw(std::string)
201 std::vector<std::string> ipMask;
205 err = "Incorrect IP address.";
209 if (ipsStr[0] == '*' && ipsStr.size() == 1)
213 ips.ips.push_back(im);
217 char * str = new char[ipsStr.size() + 1];
218 strcpy(str, ipsStr.c_str());
220 while ((paddr = strtok(pstr, ",")))
223 ipMask.push_back(paddr);
228 for (unsigned int i = 0; i < ipMask.size(); i++)
233 strcpy(str, ipMask[i].c_str());
234 strIp = strtok(str, "/");
237 err = "Incorrect IP address " + ipsStr;
240 strMask = strtok(NULL, "/");
242 im.ip = inet_addr(strIp);
243 if (im.ip == INADDR_NONE)
245 err = "Incorrect IP address: " + std::string(strIp);
253 if (str2x(strMask, m) != 0)
255 err = "Incorrect mask: " + std::string(strMask);
262 err = "Incorrect mask: " + std::string(strMask);
266 if ((im.ip & ips.CalcMask(im.mask)) != im.ip)
268 err = "Address does'n match mask: " + std::string(strIp) + "/" + std::string(strMask);
272 ips.ips.push_back(im);
277 //-------------------------------------------------------------------------