#ifndef IPLIMIT_H #define IPLIMIT_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace IPRateLimit { using boost::multi_index_container; using namespace boost::multi_index; namespace bip=boost::interprocess; typedef bip::managed_mapped_file::allocator::type char_allocator; typedef bip::basic_string, char_allocator> shm_string; struct iprate { uint32_t ip; uint32_t countSec; uint32_t countMin; uint32_t countHour; uint32_t countDay; time_t tmSec; time_t tmMin; time_t tmHour; time_t tmDay; iprate() { ip = countSec = countMin = countHour = countDay = tmSec = tmMin = tmHour = tmDay = 0; } }; typedef multi_index_container< iprate,indexed_by< hashed_unique, ordered_non_unique, ordered_non_unique, ordered_non_unique, ordered_non_unique >, bip::allocator > iprate_con; typedef boost::multi_index::nth_index::type ipIdx; typedef boost::multi_index::nth_index::type secCountIdx; typedef boost::multi_index::nth_index::type minCountIdx; typedef boost::multi_index::nth_index::type hourCountIdx; typedef boost::multi_index::nth_index::type dayCountIdx; enum ENUM_TYPE { EIP=0,ESEC_COUNT=1,EMIN_COUNT=2,EHOUR_COUNT=3,EDAY_COUNT=4 }; class CIPLimit { public: CIPLimit(const char *shm_name,const long count); ~CIPLimit(); int init(); int incCount(uint32_t ip,iprate &data); int getDataGreaterThan(int minNum,const ENUM_TYPE etype,std::vector &data); /*bool lock(); void unlock(); int destroyMutex();*/ public: static uint32_t IPStr2Int(const char* pStr); static void IPInt2Str(uint32_t ip,char *pIP); static void time2str(time_t t,std::string &str); private: int getSecCountGreaterThan(int minNum,std::vector &data); int getMinCountGreaterThan(int minNum,std::vector &data); int getHourCountGreaterThan(int minNum,std::vector &data); int getDayCountGreaterThan(int minNum,std::vector &data); protected: private: iprate_con *con; bip::named_recursive_mutex m_mutex; bip::managed_mapped_file m_seg; int maxCount; }; } #endif // IPLIMIT_H