Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: rate limits OOB server queries
  4. //
  5. //=============================================================================//
  6. #ifndef SVIPRATELIMIT_H
  7. #define SVIPRATELIMIT_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "netadr.h"
  12. #include "sv_ipratelimit.h"
  13. #include "convar.h"
  14. #include "utlrbtree.h"
  15. class CIPRateLimit
  16. {
  17. public:
  18. CIPRateLimit(ConVar *maxSec, ConVar *maxWindow, ConVar *maxSecGlobal);
  19. ~CIPRateLimit();
  20. // updates an ip entry, return true if the ip is allowed, false otherwise
  21. bool CheckIP( netadr_t ip );
  22. private:
  23. bool CheckIPInternal( netadr_t ip );
  24. enum { MAX_TREE_SIZE = 32768, START_TREE_SIZE = 2048, FLUSH_TIMEOUT = 120 };
  25. typedef int ip_t;
  26. struct iprate_s
  27. {
  28. ip_t ip;
  29. long lastTime;
  30. int count;
  31. };
  32. static bool LessIP( const struct CIPRateLimit::iprate_s &lhs, const struct CIPRateLimit::iprate_s &rhs );
  33. CUtlRBTree< struct iprate_s, ip_t > m_IPTree;
  34. int m_iGlobalCount;
  35. long m_lLastTime;
  36. double m_flFlushTime;
  37. ConVar *m_maxSec;
  38. ConVar *m_maxWindow;
  39. ConVar *m_maxSecGlobal;
  40. };
  41. // returns false if this IP exceeds rate limits
  42. bool CheckConnectionLessRateLimits( netadr_t & adr );
  43. #endif // SVIPRATELIMIT_H