]> git.stg.codes - stg.git/blob - include/stg/user_ips.h
Remove redindand ctors from USER_IPS.
[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 uint32_t ip;
52 uint32_t mask;
53 };
54 //-------------------------------------------------------------------------
55 class USER_IPS
56 {
57     friend std::ostream & operator<< (std::ostream & o, const USER_IPS & i);
58     friend const USER_IPS StrToIPS(const std::string & ipsStr);
59
60 public:
61     typedef std::vector<IP_MASK> ContainerType;
62     typedef ContainerType::size_type IndexType;
63
64     const IP_MASK & operator[](IndexType idx) const { return ips[idx]; }
65     std::string GetIpStr() const;
66     bool IsIPInIPS(uint32_t ip) const;
67     bool OnlyOneIP() const;
68     bool IsAnyIP() const;
69     size_t Count() const { return ips.size(); }
70     void Add(const IP_MASK &im) { ips.push_back(im); }
71
72 private:
73     uint32_t CalcMask(unsigned int msk) const;
74     ContainerType ips;
75 };
76 //-------------------------------------------------------------------------
77
78 //-----------------------------------------------------------------------------
79 inline
80 std::string USER_IPS::GetIpStr() const
81 {
82 if (ips.empty())
83     {
84     return "";
85     }
86
87 if (ips[0].ip == 0)
88     {
89     return "*";
90     }
91
92 ContainerType::const_iterator it(ips.begin());
93 std::ostringstream s;
94 s << inet_ntostring(it->ip);
95 ++it;
96 for (; it != ips.end(); ++it)
97     {
98     s << "," << inet_ntostring(it->ip);
99     }
100 return s.str();
101 }
102 //-----------------------------------------------------------------------------
103 inline
104 uint32_t USER_IPS::CalcMask(unsigned int msk) const
105 {
106 if (msk > 32)
107     return 0;
108 return htonl(0xFFffFFff << (32 - msk));
109 }
110 //-----------------------------------------------------------------------------
111 inline
112 bool USER_IPS::IsIPInIPS(uint32_t ip) const
113 {
114 if (ips.empty())
115     {
116     return false;
117     }
118
119 if (ips.front().ip == 0)
120     return true;
121
122 for (ContainerType::const_iterator it(ips.begin()); it != ips.end(); ++it)
123     {
124     uint32_t mask(CalcMask(it->mask));
125     if ((ip & mask) == (it->ip & mask))
126         return true;
127     }
128 return false;
129 }
130 //-----------------------------------------------------------------------------
131 inline
132 bool USER_IPS::OnlyOneIP() const
133 {
134 if (ips.size() == 1 && ips.front().mask == 32 && ips.front().ip != 0)
135     return true;
136
137 return false;
138 }
139 //-----------------------------------------------------------------------------
140 inline
141 bool USER_IPS::IsAnyIP() const
142 {
143     return !ips.empty() && ips.front().ip == 0;
144 }
145 //-----------------------------------------------------------------------------
146 inline
147 std::ostream & operator<<(std::ostream & o, const USER_IPS & i)
148 {
149 return o << i.GetIpStr();
150 }
151 //-----------------------------------------------------------------------------
152 inline
153 const USER_IPS StrToIPS(const std::string & ipsStr)
154 {
155 USER_IPS ips;
156 char * paddr;
157 IP_MASK im;
158 std::vector<std::string> ipMask;
159 if (ipsStr.empty())
160     {
161     return ips;
162     }
163
164 if (ipsStr[0] == '*' && ipsStr.size() == 1)
165     {
166     im.ip = 0;
167     im.mask = 0;
168     ips.ips.push_back(im);
169     return ips;
170     }
171
172 char * tmp = new char[ipsStr.size() + 1];
173 strcpy(tmp, ipsStr.c_str());
174 char * pstr = tmp;
175 while ((paddr = strtok(pstr, ",")))
176     {
177     pstr = NULL;
178     ipMask.push_back(paddr);
179     }
180
181 delete[] tmp;
182
183 for (USER_IPS::IndexType i = 0; i < ipMask.size(); i++)
184     {
185     char str[128];
186     char * strIp;
187     char * strMask;
188     strcpy(str, ipMask[i].c_str());
189     strIp = strtok(str, "/");
190     if (strIp == NULL)
191         {
192         return ips;
193         }
194     strMask = strtok(NULL, "/");
195
196     im.ip = inet_addr(strIp);
197     if (im.ip == INADDR_NONE)
198         {
199         return ips;
200         }
201
202     im.mask = 32;
203     if (strMask != NULL)
204         {
205         int m = 0;
206         if (str2x(strMask, m) != 0)
207             {
208             return ips;
209             }
210         im.mask = m;
211
212         if (im.mask > 32)
213             {
214             return ips;
215             }
216
217         if ((im.ip & ips.CalcMask(im.mask)) != im.ip)
218             {
219             return ips;
220             }
221         }
222     ips.ips.push_back(im);
223     }
224
225 return ips;
226 }
227 //-------------------------------------------------------------------------
228 #endif //USER_IPS_H