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>
44 #include "stg/common.h"
47 //-------------------------------------------------------------------------
50 IP_MASK() : ip(0), mask(0) {}
51 IP_MASK(const IP_MASK & ipm) : ip(ipm.ip), mask(ipm.mask) {}
55 //-------------------------------------------------------------------------
58 friend std::ostream & operator<< (std::ostream & o, const USER_IPS & i);
59 //friend stringstream & operator<< (stringstream & s, const USER_IPS & i);
60 friend const USER_IPS StrToIPS(const std::string & ipsStr);
63 typedef std::vector<IP_MASK> ContainerType;
64 typedef ContainerType::size_type IndexType;
67 USER_IPS(const USER_IPS &);
68 USER_IPS & operator=(const USER_IPS &);
69 const IP_MASK & operator[](IndexType idx) const;
70 std::string GetIpStr() const;
71 bool IsIPInIPS(uint32_t ip) const;
72 bool OnlyOneIP() const;
75 void Add(const IP_MASK &im);
79 uint32_t CalcMask(unsigned int msk) const;
82 //-------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
91 USER_IPS::USER_IPS(const USER_IPS & i)
94 //-----------------------------------------------------------------------------
96 USER_IPS & USER_IPS::operator=(const USER_IPS & i)
101 //-----------------------------------------------------------------------------
103 const IP_MASK & USER_IPS::operator[](IndexType idx) const
107 //-----------------------------------------------------------------------------
109 std::string USER_IPS::GetIpStr() const
121 ContainerType::const_iterator it(ips.begin());
122 std::ostringstream s;
123 s << inet_ntostring(it->ip);
125 for (; it != ips.end(); ++it)
127 s << "," << inet_ntostring(it->ip);
131 //-----------------------------------------------------------------------------
133 size_t USER_IPS::Count() const
137 //-----------------------------------------------------------------------------
139 uint32_t USER_IPS::CalcMask(unsigned int msk) const
143 return htonl(0xFFffFFff << (32 - msk));
145 //-----------------------------------------------------------------------------
147 bool USER_IPS::IsIPInIPS(uint32_t ip) const
154 if (ips.front().ip == 0)
157 for (ContainerType::const_iterator it(ips.begin()); it != ips.end(); ++it)
159 uint32_t mask(CalcMask(it->mask));
160 if ((ip & mask) == (it->ip & mask))
165 //-----------------------------------------------------------------------------
167 bool USER_IPS::OnlyOneIP() const
169 if (ips.size() == 1 && ips.front().mask == 32 && ips.front().ip != 0)
174 //-----------------------------------------------------------------------------
176 bool USER_IPS::IsAnyIP() const
178 return !ips.empty() && ips.front().ip == 0;
180 //-----------------------------------------------------------------------------
182 void USER_IPS::Add(const IP_MASK &im)
186 //-----------------------------------------------------------------------------
188 void USER_IPS::Erase()
190 ips.erase(ips.begin(), ips.end());
192 //-----------------------------------------------------------------------------
194 std::ostream & operator<<(std::ostream & o, const USER_IPS & i)
196 return o << i.GetIpStr();
198 //-----------------------------------------------------------------------------
200 stringstream & operator<<(std::stringstream & s, const USER_IPS & i)
205 //-----------------------------------------------------------------------------
207 const USER_IPS StrToIPS(const std::string & ipsStr)
212 std::vector<std::string> ipMask;
218 if (ipsStr[0] == '*' && ipsStr.size() == 1)
222 ips.ips.push_back(im);
226 char * tmp = new char[ipsStr.size() + 1];
227 strcpy(tmp, ipsStr.c_str());
229 while ((paddr = strtok(pstr, ",")))
232 ipMask.push_back(paddr);
237 for (USER_IPS::IndexType i = 0; i < ipMask.size(); i++)
242 strcpy(str, ipMask[i].c_str());
243 strIp = strtok(str, "/");
248 strMask = strtok(NULL, "/");
250 im.ip = inet_addr(strIp);
251 if (im.ip == INADDR_NONE)
260 if (str2x(strMask, m) != 0)
271 if ((im.ip & ips.CalcMask(im.mask)) != im.ip)
276 ips.ips.push_back(im);
281 //-------------------------------------------------------------------------