]> git.stg.codes - stg.git/blob - include/stg/user_ips.h
Massive refactoring.
[stg.git] / include / stg / user_ips.h
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21  /*
22  $Revision: 1.22 $
23  $Date: 2010/03/04 11:49:53 $
24  $Author: faust $
25  */
26
27 #ifndef USER_IPS_H
28 #define USER_IPS_H
29
30 #ifdef FREE_BSD
31 #include <sys/types.h>
32 #endif
33
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37
38 #include <cstring>
39 #include <vector>
40 #include <string>
41 #include <iostream>
42 #include <sstream>
43
44 #include "stg/common.h"
45 #include "os_int.h"
46
47 //-------------------------------------------------------------------------
48 struct IP_MASK
49 {
50 IP_MASK() : ip(0), mask(0) {}
51 IP_MASK(const IP_MASK & ipm) : ip(ipm.ip), mask(ipm.mask)  {}
52 uint32_t ip;
53 uint32_t mask;
54 };
55 //-------------------------------------------------------------------------
56 class USER_IPS
57 {
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);
61
62 public:
63     typedef std::vector<IP_MASK> ContainerType;
64     typedef ContainerType::size_type IndexType;
65
66     USER_IPS();
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;
73     int  Count() const;
74     void Add(const IP_MASK &im);
75     void Erase();
76
77 private:
78     uint32_t CalcMask(unsigned int msk) const;
79     ContainerType ips;
80 };
81 //-------------------------------------------------------------------------
82
83 //-----------------------------------------------------------------------------
84 inline
85 USER_IPS::USER_IPS()
86     : ips()
87 {}
88 //-----------------------------------------------------------------------------
89 inline
90 USER_IPS::USER_IPS(const USER_IPS & i)
91     : ips(i.ips)
92 {}
93 //-----------------------------------------------------------------------------
94 inline
95 USER_IPS & USER_IPS::operator=(const USER_IPS & i)
96 {
97 ips = i.ips;
98 return *this;
99 }
100 //-----------------------------------------------------------------------------
101 inline
102 const IP_MASK & USER_IPS::operator[](IndexType idx) const
103 {
104 return ips[idx];
105 }
106 //-----------------------------------------------------------------------------
107 inline
108 std::string USER_IPS::GetIpStr() const
109 {
110 if (ips.empty())
111     {
112     return "";
113     }
114
115 if (ips[0].ip == 0)
116     {
117     return "*";
118     }
119
120 ContainerType::const_iterator it(ips.begin());
121 std::ostringstream s;
122 s << inet_ntostring(it->ip);
123 ++it;
124 for (; it != ips.end(); ++it)
125     {
126     s << "," << inet_ntostring(it->ip);
127     }
128 return s.str();
129 }
130 //-----------------------------------------------------------------------------
131 inline
132 int USER_IPS::Count() const
133 {
134 return static_cast<int>(ips.size());
135 }
136 //-----------------------------------------------------------------------------
137 inline
138 uint32_t USER_IPS::CalcMask(unsigned int msk) const
139 {
140 if (msk > 32)
141     return 0;
142 return htonl(0xFFffFFff << (32 - msk));
143 }
144 //-----------------------------------------------------------------------------
145 inline
146 bool USER_IPS::IsIPInIPS(uint32_t ip) const
147 {
148 if (ips.empty())
149     {
150     return false;
151     }
152
153 if (ips.front().ip == 0)
154     return true;
155
156 for (ContainerType::const_iterator it(ips.begin()); it != ips.end(); ++it)
157     {
158     uint32_t mask(CalcMask(it->mask));
159     if ((ip & mask) == (it->ip & mask))
160         return true;
161     }
162 return false;
163 }
164 //-----------------------------------------------------------------------------
165 inline
166 bool USER_IPS::OnlyOneIP() const
167 {
168 if (ips.size() == 1 && ips.front().mask == 32)
169     return true;
170
171 return false;
172 }
173 //-----------------------------------------------------------------------------
174 inline
175 void USER_IPS::Add(const IP_MASK &im)
176 {
177 ips.push_back(im);
178 }
179 //-----------------------------------------------------------------------------
180 inline
181 void USER_IPS::Erase()
182 {
183 ips.erase(ips.begin(), ips.end());
184 }
185 //-----------------------------------------------------------------------------
186 inline
187 std::ostream & operator<<(std::ostream & o, const USER_IPS & i)
188 {
189 return o << i.GetIpStr();
190 }
191 //-----------------------------------------------------------------------------
192 /*inline
193 stringstream & operator<<(std::stringstream & s, const USER_IPS & i)
194 {
195 s << i.GetIpStr();
196 return s;
197 }*/
198 //-----------------------------------------------------------------------------
199 inline
200 const USER_IPS StrToIPS(const std::string & ipsStr)
201 {
202 USER_IPS ips;
203 char * paddr;
204 IP_MASK im;
205 std::vector<std::string> ipMask;
206 if (ipsStr.empty())
207     {
208     return ips;
209     }
210
211 if (ipsStr[0] == '*' && ipsStr.size() == 1)
212     {
213     im.ip = 0;
214     im.mask = 0;
215     ips.ips.push_back(im);
216     return ips;
217     }
218
219 char * tmp = new char[ipsStr.size() + 1];
220 strcpy(tmp, ipsStr.c_str());
221 char * pstr = tmp;
222 while ((paddr = strtok(pstr, ",")))
223     {
224     pstr = NULL;
225     ipMask.push_back(paddr);
226     }
227
228 delete[] tmp;
229
230 for (USER_IPS::IndexType i = 0; i < ipMask.size(); i++)
231     {
232     char str[128];
233     char * strIp;
234     char * strMask;
235     strcpy(str, ipMask[i].c_str());
236     strIp = strtok(str, "/");
237     if (strIp == NULL)
238         {
239         return ips;
240         }
241     strMask = strtok(NULL, "/");
242
243     im.ip = inet_addr(strIp);
244     if (im.ip == INADDR_NONE)
245         {
246         return ips;
247         }
248
249     im.mask = 32;
250     if (strMask != NULL)
251         {
252         int m = 0;
253         if (str2x(strMask, m) != 0)
254             {
255             return ips;
256             }
257         im.mask = m;
258
259         if (im.mask > 32)
260             {
261             return ips;
262             }
263
264         if ((im.ip & ips.CalcMask(im.mask)) != im.ip)
265             {
266             return ips;
267             }
268         }
269     ips.ips.push_back(im);
270     }
271
272 return ips;
273 }
274 //-------------------------------------------------------------------------
275 #endif //USER_IPS_H