Ticket #11537: IPLimit.cpp

File IPLimit.cpp, 7.9 KB (added by zzweng_2001@…, 7 years ago)
Line 
1#include "IPLimit.h"
2
3#include <sys/socket.h>
4#include <netinet/in.h>
5#include <arpa/inet.h>
6#include <boost/date_time/posix_time/posix_time.hpp>
7#include <boost/thread/xtime.hpp>
8
9
10
11#include <stdlib.h>
12#include <stdio.h>
13#include "myLog.h"
14
15using namespace IPRateLimit;
16using boost::multi_index_container;
17using namespace boost::multi_index;
18using namespace boost::interprocess;
19
20//iprate_con* CIPLimit::con = NULL;
21//pthread_mutex_t* CIPLimit::mutex = NULL;
22//bip::managed_mapped_file* CIPLimit::seg = NULL;
23//int CIPLimit::maxCount = 0;
24
25uint32_t CIPLimit::IPStr2Int(const char* pStr)
26{
27 return ntohl(inet_addr(pStr));
28}
29void CIPLimit::IPInt2Str(uint32_t ip,char *pIP)
30{
31 uint32_t nip = htonl(ip);
32 struct in_addr addr;
33 addr.s_addr = nip;
34 strcpy(pIP,inet_ntoa(addr));
35}
36void CIPLimit::time2str(time_t t,std::string &str)
37{
38 struct tm t1;
39 localtime_r(&t,&t1);
40 char buf[64] = {0};
41 strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S",&t1);
42 str = buf;
43}
44
45int CIPLimit::init()
46{
47 //maxCount = count;
48 try
49 {
50 //m_seg = bip::managed_mapped_file(bip::open_or_create, shm_name, count*sizeof(iprate)*8);
51
52 //m_mutex = bip::named_mutex(bip::open_or_create,shm_name);
53
54 con = m_seg.find_or_construct<iprate_con>("iplimitcontainer")(
55 iprate_con::ctor_args_list(),
56 iprate_con::allocator_type(m_seg.get_segment_manager()));
57 if(!con) return -3;
58 //return MyProMutex::init(mutex_ipc_key,&mutex,bInitMutex);
59
60
61 }
62 catch(boost::interprocess::interprocess_exception& e)
63 {
64 MYLOG_ERROR(logger,"exception %s",e.what());
65 return -4;
66 }
67
68 return 0;
69}
70
71/*bool CIPLimit::lock()
72{
73 return MyProMutex::lock(mutex);
74}
75
76void CIPLimit::unlock()
77{
78 MyProMutex::unlock(mutex);
79}
80
81int CIPLimit::destroyMutex()
82{
83 return pthread_mutex_destroy(mutex);
84}*/
85
86
87CIPLimit::CIPLimit(const char *shm_name, const long count):con(NULL),m_mutex(bip::open_or_create,shm_name),m_seg(bip::open_or_create, shm_name, count*sizeof(iprate)*8),maxCount(count)
88{
89 //ctor
90
91
92}
93
94CIPLimit::~CIPLimit()
95{
96 //dtor
97}
98
99struct modify_data
100{
101 modify_data(const iprate &data):v(data) {}
102
103 void operator()(iprate& data)
104 {
105 data = v;
106 }
107private:
108iprate v;
109};
110
111int CIPLimit::getSecCountGreaterThan(int minNum,std::vector<iprate> &data)
112{
113try
114{
115 scoped_lock<bip::named_recursive_mutex> lock(m_mutex);
116
117 /*MyProMutex::CAutoRelease mtx(mutex);
118 if(mtx.bret == false)
119 return -1;*/
120
121 secCountIdx &idx = get<ESEC_COUNT>(*con);
122 secCountIdx::const_iterator iter = idx.lower_bound(minNum);
123 for(;iter != idx.end();iter++)
124 {
125 data.push_back(*iter);
126 }
127}
128catch(boost::interprocess::interprocess_exception& e)
129{
130 MYLOG_ERROR(logger,"free mem %d,con.size %d,exception %s",m_seg.get_free_memory(),con->size(),e.what());
131 return -2;
132}
133
134 return 0;
135}
136
137int CIPLimit::getMinCountGreaterThan(int minNum,std::vector<iprate> &data)
138{
139try
140{
141
142 scoped_lock<bip::named_recursive_mutex> lock(m_mutex);
143 /*MyProMutex::CAutoRelease mtx(mutex);
144 if(mtx.bret == false)
145 return -1;*/
146
147 minCountIdx &idx = get<EMIN_COUNT>(*con);
148 minCountIdx::const_iterator iter = idx.lower_bound(minNum);
149 for(;iter != idx.end();iter++)
150 {
151 data.push_back(*iter);
152 }
153}
154catch(boost::interprocess::interprocess_exception& e)
155{
156 MYLOG_ERROR(logger,"free mem %d,con.size %d,exception %s",m_seg.get_free_memory(),con->size(),e.what());
157 return -1;
158}
159 return 0;
160}
161
162int CIPLimit::getHourCountGreaterThan(int minNum,std::vector<iprate> &data)
163{
164try
165{
166
167 scoped_lock<bip::named_recursive_mutex> lock(m_mutex);
168 /*MyProMutex::CAutoRelease mtx(mutex);
169 if(mtx.bret == false)
170 return -1;*/
171
172 hourCountIdx &idx = get<EHOUR_COUNT>(*con);
173 hourCountIdx::const_iterator iter = idx.lower_bound(minNum);
174 for(;iter != idx.end();iter++)
175 {
176 data.push_back(*iter);
177 }
178}
179catch(boost::interprocess::interprocess_exception& e)
180{
181 MYLOG_ERROR(logger,"free mem %d,con.size %d,exception %s",m_seg.get_free_memory(),con->size(),e.what());
182 return -1;
183}
184
185 return 0;
186}
187
188int CIPLimit::getDayCountGreaterThan(int minNum,std::vector<iprate> &data)
189{
190try
191{
192 scoped_lock<bip::named_recursive_mutex> lock(m_mutex);
193 /*MyProMutex::CAutoRelease mtx(mutex);
194 if(mtx.bret == false)
195 return -1;*/
196
197 dayCountIdx &idx = get<EDAY_COUNT>(*con);
198 dayCountIdx::const_iterator iter = idx.lower_bound(minNum);
199 for(;iter != idx.end();iter++)
200 {
201 data.push_back(*iter);
202 }
203
204}
205catch(boost::interprocess::interprocess_exception& e)
206{
207 MYLOG_ERROR(logger,"free mem %d,con.size %d,exception %s",m_seg.get_free_memory(),con->size(),e.what());
208 return -1;
209}
210return 0;
211
212}
213
214int CIPLimit::getDataGreaterThan(int minNum,const ENUM_TYPE etype,std::vector<iprate> &data)
215{
216
217 if(etype == ESEC_COUNT)
218 {
219 return getSecCountGreaterThan(minNum,data);
220 }
221 else if(etype == EMIN_COUNT)
222 {
223 return getMinCountGreaterThan(minNum,data);
224 }
225 else if(etype == EHOUR_COUNT)
226 {
227 return getHourCountGreaterThan(minNum,data);
228
229 }
230 else if(etype == EDAY_COUNT)
231 {
232 return getDayCountGreaterThan(minNum,data);
233
234 }
235 else
236 {
237 return -1;
238 }
239
240}
241int CIPLimit::incCount(uint32_t ip,iprate &data)
242{
243try
244{
245 scoped_lock<bip::named_recursive_mutex> lock(m_mutex);
246 /* MyProMutex::CAutoRelease mtx(mutex);
247 if(mtx.bret == false)
248 {
249 printf("lock ret false\n");
250 return -1;
251 }*/
252
253 ipIdx &idx = get<EIP>(*con);
254 ipIdx::const_iterator iter = idx.find(ip);
255 //ipIdx::const_iterator iter = get<EIP>(*con).find(ip);
256 time_t t = time(NULL);
257 if(iter != idx.end())
258 {
259 data = (*iter);
260
261 //printf("---- %d,%d,%d,%d\n",data.countSec,data.countMin,data.countHour,data.countDay);
262
263 int diffSec = t - data.tmSec;
264 int diffMin = t - data.tmMin;
265 int diffHour = t - data.tmHour;
266 int diffDay = t - data.tmDay;
267
268 //printf("diff %d,%d,%d,%d\n",diffSec,diffMin,diffHour,diffDay);
269
270 if(diffSec <= 1)
271 {
272 ++data.countSec;
273 }
274 else
275 {
276 data.countSec = 1;
277 data.tmSec = t;
278 }
279
280 if(diffMin <= 60)
281 {
282 ++data.countMin;
283 }
284 else
285 {
286 data.countMin = 1;
287 data.tmMin = t;
288 }
289
290 if(diffHour <= 60*60)
291 {
292 ++data.countHour;
293 }
294 else
295 {
296 data.countHour = 1;
297 data.tmHour = t;
298 }
299
300 if(diffDay <= 60*60*24)
301 {
302 ++data.countDay;
303 }
304 else
305 {
306 data.countDay = 1;
307 data.tmDay = t;
308 }
309 //printf(">>%d,%d,%d,%d\n",data.countSec,data.countMin,data.countHour,data.countDay);
310
311 idx.modify(iter,modify_data(data));
312
313
314 }
315 else
316 {
317 iprate d;
318 d.ip = ip;
319 d.tmSec = t;
320 d.tmMin = t;
321 d.tmHour = t;
322 d.tmDay = t;
323
324 d.countSec = 1;
325 d.countMin = 1;
326 d.countHour = 1;
327 d.countDay = 1;
328
329 if((int)con->size() >= maxCount )
330 {
331 dayCountIdx &dayIdx = get<EDAY_COUNT>(*con);
332
333 dayCountIdx::const_iterator it = dayIdx.begin();
334 //printf("min %d,%d,%d,%d\n",it->countSec,it->countMin,it->countHour,it->countDay);
335
336 dayIdx.modify(it,modify_data(d));
337 }
338 else
339 {
340 con->insert(d);
341 }
342
343 data = d;
344
345 }
346}
347catch(boost::interprocess::interprocess_exception& e)
348{
349 MYLOG_ERROR(logger,"free mem %d,con.size %d,exception %s",m_seg.get_free_memory(),con->size(),e.what());
350
351 return -4;
352}
353
354 return 0;
355
356
357}
358