| 1 | #ifndef IPLIMIT_H
|
|---|
| 2 | #define IPLIMIT_H
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 | #include <boost/config.hpp>
|
|---|
| 7 | #include <algorithm>
|
|---|
| 8 | #include <boost/interprocess/allocators/allocator.hpp>
|
|---|
| 9 | #include <boost/interprocess/containers/string.hpp>
|
|---|
| 10 | #include <boost/interprocess/managed_mapped_file.hpp>
|
|---|
| 11 | #include <boost/interprocess/managed_shared_memory.hpp>
|
|---|
| 12 | #include <boost/interprocess/sync/scoped_lock.hpp>
|
|---|
| 13 | #include <boost/multi_index_container.hpp>
|
|---|
| 14 | #include <boost/multi_index/ordered_index.hpp>
|
|---|
| 15 |
|
|---|
| 16 | #include <boost/multi_index/hashed_index.hpp>
|
|---|
| 17 | #include <boost/multi_index/member.hpp>
|
|---|
| 18 | #include <boost/multi_index/composite_key.hpp>
|
|---|
| 19 | #include <boost/multi_index/identity.hpp>
|
|---|
| 20 | #include <iterator>
|
|---|
| 21 | #include <string>
|
|---|
| 22 | #include <iostream>
|
|---|
| 23 | #include <boost/interprocess/sync/scoped_lock.hpp>
|
|---|
| 24 | #include <boost/interprocess/sync/named_recursive_mutex.hpp>
|
|---|
| 25 |
|
|---|
| 26 | #include <vector>
|
|---|
| 27 | #include <pthread.h>
|
|---|
| 28 |
|
|---|
| 29 | namespace IPRateLimit
|
|---|
| 30 | {
|
|---|
| 31 |
|
|---|
| 32 | using boost::multi_index_container;
|
|---|
| 33 | using namespace boost::multi_index;
|
|---|
| 34 | namespace bip=boost::interprocess;
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 | typedef bip::managed_mapped_file::allocator<char>::type char_allocator;
|
|---|
| 38 | typedef bip::basic_string<char, std::char_traits<char>, char_allocator> shm_string;
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 | struct iprate
|
|---|
| 44 | {
|
|---|
| 45 | uint32_t ip;
|
|---|
| 46 | uint32_t countSec;
|
|---|
| 47 | uint32_t countMin;
|
|---|
| 48 | uint32_t countHour;
|
|---|
| 49 | uint32_t countDay;
|
|---|
| 50 | time_t tmSec;
|
|---|
| 51 | time_t tmMin;
|
|---|
| 52 | time_t tmHour;
|
|---|
| 53 | time_t tmDay;
|
|---|
| 54 | iprate()
|
|---|
| 55 | {
|
|---|
| 56 | ip = countSec = countMin = countHour = countDay = tmSec = tmMin = tmHour = tmDay = 0;
|
|---|
| 57 | }
|
|---|
| 58 | };
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 | typedef multi_index_container<
|
|---|
| 62 | iprate,indexed_by<
|
|---|
| 63 | hashed_unique<BOOST_MULTI_INDEX_MEMBER(iprate,uint32_t,ip)>,
|
|---|
| 64 | ordered_non_unique<BOOST_MULTI_INDEX_MEMBER(iprate,uint32_t,countSec)>,
|
|---|
| 65 | ordered_non_unique<BOOST_MULTI_INDEX_MEMBER(iprate,uint32_t,countMin)>,
|
|---|
| 66 | ordered_non_unique<BOOST_MULTI_INDEX_MEMBER(iprate,uint32_t,countHour)>,
|
|---|
| 67 | ordered_non_unique<BOOST_MULTI_INDEX_MEMBER(iprate,uint32_t,countDay)>
|
|---|
| 68 | >,
|
|---|
| 69 | bip::allocator<iprate,bip::managed_mapped_file::segment_manager>
|
|---|
| 70 | > iprate_con;
|
|---|
| 71 |
|
|---|
| 72 | typedef boost::multi_index::nth_index<iprate_con,0>::type ipIdx;
|
|---|
| 73 | typedef boost::multi_index::nth_index<iprate_con,1>::type secCountIdx;
|
|---|
| 74 | typedef boost::multi_index::nth_index<iprate_con,2>::type minCountIdx;
|
|---|
| 75 | typedef boost::multi_index::nth_index<iprate_con,3>::type hourCountIdx;
|
|---|
| 76 | typedef boost::multi_index::nth_index<iprate_con,4>::type dayCountIdx;
|
|---|
| 77 |
|
|---|
| 78 | enum ENUM_TYPE
|
|---|
| 79 | {
|
|---|
| 80 | EIP=0,ESEC_COUNT=1,EMIN_COUNT=2,EHOUR_COUNT=3,EDAY_COUNT=4
|
|---|
| 81 | };
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 | class CIPLimit
|
|---|
| 85 | {
|
|---|
| 86 | public:
|
|---|
| 87 | CIPLimit(const char *shm_name,const long count);
|
|---|
| 88 | ~CIPLimit();
|
|---|
| 89 | int init();
|
|---|
| 90 | int incCount(uint32_t ip,iprate &data);
|
|---|
| 91 | int getDataGreaterThan(int minNum,const ENUM_TYPE etype,std::vector<iprate> &data);
|
|---|
| 92 |
|
|---|
| 93 | /*bool lock();
|
|---|
| 94 | void unlock();
|
|---|
| 95 | int destroyMutex();*/
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 | public:
|
|---|
| 100 | static uint32_t IPStr2Int(const char* pStr);
|
|---|
| 101 | static void IPInt2Str(uint32_t ip,char *pIP);
|
|---|
| 102 | static void time2str(time_t t,std::string &str);
|
|---|
| 103 |
|
|---|
| 104 | private:
|
|---|
| 105 | int getSecCountGreaterThan(int minNum,std::vector<iprate> &data);
|
|---|
| 106 | int getMinCountGreaterThan(int minNum,std::vector<iprate> &data);
|
|---|
| 107 | int getHourCountGreaterThan(int minNum,std::vector<iprate> &data);
|
|---|
| 108 | int getDayCountGreaterThan(int minNum,std::vector<iprate> &data);
|
|---|
| 109 | protected:
|
|---|
| 110 | private:
|
|---|
| 111 |
|
|---|
| 112 | iprate_con *con;
|
|---|
| 113 | bip::named_recursive_mutex m_mutex;
|
|---|
| 114 | bip::managed_mapped_file m_seg;
|
|---|
| 115 | int maxCount;
|
|---|
| 116 | };
|
|---|
| 117 | }
|
|---|
| 118 | #endif // IPLIMIT_H
|
|---|