Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
ratelimiter.h
Go to the documentation of this file.
1 #ifndef RATE_TRACKER_H
2 #define RATE_TRACKER_H
3 
4 #include "socketaddress.h"
5 #include "fasthash.h"
6 
7 struct RateTracker
8 {
9  uint64_t count; // how many packets since firstEntryTime
10  time_t firstEntryTime; // what time the first entry came in at
11  time_t lastEntryTime; // what time the last entry came in at (may not be needed
12  time_t penaltyTime; // what time he's allowed out of penalty (0 if no penalty)
13 };
14 
16 {
17  uint64_t addrbytes[2];
18  bool operator==(const RateTrackerAddress& other)
19  {
20  return ((other.addrbytes[0] == addrbytes[0]) && (other.addrbytes[1] == addrbytes[1]));
21  }
22 };
23 
24 inline size_t FastHash_Hash(const RateTrackerAddress& addr)
25 {
26  size_t result;
27 
28  if (sizeof(size_t) >= 8)
29  {
30  result = addr.addrbytes[0] ^ addr.addrbytes[1];
31  }
32  else
33  {
34  uint32_t* pTmp = (uint32_t*)(addr.addrbytes);
35  result = pTmp[0] ^ pTmp[1] ^ pTmp[2] ^ pTmp[3];
36  }
37  return result;
38 }
39 
40 
42 {
43 protected:
44 
45  virtual time_t get_time();
46  uint64_t get_rate(const RateTracker* pRT);
47 
49 
51 
52  pthread_mutex_t _mutex;
53 
54  bool RateCheckImpl(const CSocketAddress& addr);
55 
56 
57 public:
58  static const uint64_t MAX_RATE = 3600; // 60/minute normalized to an hourly rate
59  static const uint64_t MIN_COUNT_FOR_CONSIDERATION = 60;
60  static const time_t RESET_INTERVAL_SECONDS = 120; // if he ever exceeds the hourly rate within a two minute interval, he gets penalized
61  static const time_t PENALTY_TIME_SECONDS = 3600; // one hour penalty
62 
63  bool RateCheck(const CSocketAddress& addr);
64 
65  RateLimiter(size_t tablesize, bool isUsingLock);
66  virtual ~RateLimiter();
67 };
68 
69 #endif
FastHashDynamic< RateTrackerAddress, RateTracker > _table
Definition: ratelimiter.h:48
bool _isUsingLock
Definition: ratelimiter.h:50
size_t FastHash_Hash(const RateTrackerAddress &addr)
Definition: ratelimiter.h:24
uint64_t addrbytes[2]
Definition: ratelimiter.h:17
bool operator==(const RateTrackerAddress &other)
Definition: ratelimiter.h:18
time_t penaltyTime
Definition: ratelimiter.h:12
uint64_t count
Definition: ratelimiter.h:9
pthread_mutex_t _mutex
Definition: ratelimiter.h:52
time_t firstEntryTime
Definition: ratelimiter.h:10
time_t lastEntryTime
Definition: ratelimiter.h:11