MessageFilter.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. /// \file
  11. /// \brief Message filter plugin. Assigns systems to FilterSets. Each FilterSet limits what messages are allowed. This is a security related plugin.
  12. ///
  13. #include "NativeFeatureIncludes.h"
  14. #if _RAKNET_SUPPORT_MessageFilter==1
  15. #ifndef __MESSAGE_FILTER_PLUGIN_H
  16. #define __MESSAGE_FILTER_PLUGIN_H
  17. #include "RakNetTypes.h"
  18. #include "PluginInterface2.h"
  19. #include "DS_OrderedList.h"
  20. #include "DS_Hash.h"
  21. #include "Export.h"
  22. /// MessageIdentifier (ID_*) values shoudln't go higher than this. Change it if you do.
  23. #define MESSAGE_FILTER_MAX_MESSAGE_ID 256
  24. namespace RakNet
  25. {
  26. /// Forward declarations
  27. class RakPeerInterface;
  28. /// \internal Has to be public so some of the shittier compilers can use it.
  29. int RAK_DLL_EXPORT MessageFilterStrComp( char *const &key,char *const &data );
  30. /// \internal Has to be public so some of the shittier compilers can use it.
  31. struct FilterSet
  32. {
  33. bool banOnFilterTimeExceed;
  34. bool kickOnDisallowedMessage;
  35. bool banOnDisallowedMessage;
  36. RakNet::TimeMS disallowedMessageBanTimeMS;
  37. RakNet::TimeMS timeExceedBanTimeMS;
  38. RakNet::TimeMS maxMemberTimeMS;
  39. void (*invalidMessageCallback)(RakPeerInterface *peer, AddressOrGUID systemAddress, int filterSetID, void *userData, unsigned char messageID);
  40. void *disallowedCallbackUserData;
  41. void (*timeoutCallback)(RakPeerInterface *peer, AddressOrGUID systemAddress, int filterSetID, void *userData);
  42. void *timeoutUserData;
  43. int filterSetID;
  44. bool allowedIDs[MESSAGE_FILTER_MAX_MESSAGE_ID];
  45. DataStructures::OrderedList<RakNet::RakString,RakNet::RakString> allowedRPC4;
  46. };
  47. /// \internal Has to be public so some of the shittier compilers can use it.
  48. int RAK_DLL_EXPORT FilterSetComp( const int &key, FilterSet * const &data );
  49. /// \internal Has to be public so some of the shittier compilers can use it.
  50. struct FilteredSystem
  51. {
  52. FilterSet *filter;
  53. RakNet::TimeMS timeEnteredThisSet;
  54. };
  55. /// \defgroup MESSAGEFILTER_GROUP MessageFilter
  56. /// \brief Remote incoming packets from unauthorized systems
  57. /// \details
  58. /// \ingroup PLUGINS_GROUP
  59. /// \brief Assigns systems to FilterSets. Each FilterSet limits what kinds of messages are allowed.
  60. /// \details The MessageFilter plugin is used for security where you limit what systems can send what kind of messages.<BR>
  61. /// You implicitly define FilterSets, and add allowed message IDs to these FilterSets.<BR>
  62. /// You then add systems to these filters, such that those systems are limited to sending what the filters allows.<BR>
  63. /// You can automatically assign systems to a filter.<BR>
  64. /// You can automatically kick and possibly ban users that stay in a filter too long, or send the wrong message.<BR>
  65. /// Each system is a member of either zero or one filters.<BR>
  66. /// Add this plugin before any plugin you wish to filter (most likely just add this plugin before any other).
  67. /// \ingroup MESSAGEFILTER_GROUP
  68. class RAK_DLL_EXPORT MessageFilter : public PluginInterface2
  69. {
  70. public:
  71. // GetInstance() and DestroyInstance(instance*)
  72. STATIC_FACTORY_DECLARATIONS(MessageFilter)
  73. MessageFilter();
  74. virtual ~MessageFilter();
  75. // --------------------------------------------------------------------------------------------
  76. // User functions
  77. // --------------------------------------------------------------------------------------------
  78. /// Automatically add all new systems to a particular filter
  79. /// Defaults to -1
  80. /// \param[in] filterSetID Which filter to add new systems to. <0 for do not add.
  81. void SetAutoAddNewConnectionsToFilter(int filterSetID);
  82. /// Allow a range of message IDs
  83. /// Always allowed by default: ID_CONNECTION_REQUEST_ACCEPTED through ID_DOWNLOAD_PROGRESS
  84. /// Usually you specify a range to make it easier to add new enumerations without having to constantly refer back to this function.
  85. /// \param[in] allow True to allow this message ID, false to disallow. By default, all messageIDs except the noted types are disallowed. This includes messages from other plugins!
  86. /// \param[in] messageIDStart The first ID_* message to allow in the range. Inclusive.
  87. /// \param[in] messageIDEnd The last ID_* message to allow in the range. Inclusive.
  88. /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings.
  89. void SetAllowMessageID(bool allow, int messageIDStart, int messageIDEnd,int filterSetID);
  90. /// Allow a specific RPC4 call
  91. /// \pre MessageFilter must be attached before RPC4
  92. /// \param[in] uniqueID Identifier passed to RegisterFunction()
  93. /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings.
  94. void SetAllowRPC4(bool allow, const char* uniqueID, int filterSetID);
  95. /// What action to take on a disallowed message. You can kick or not. You can add them to the ban list for some time
  96. /// By default no action is taken. The message is simply ignored.
  97. /// param[in] 0 for permanent ban, >0 for ban time in milliseconds.
  98. /// \param[in] kickOnDisallowed kick the system that sent a disallowed message.
  99. /// \param[in] banOnDisallowed ban the system that sent a disallowed message. See \a banTimeMS for the ban duration
  100. /// \param[in] banTimeMS Passed to the milliseconds parameter of RakPeer::AddToBanList.
  101. /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings.
  102. void SetActionOnDisallowedMessage(bool kickOnDisallowed, bool banOnDisallowed, RakNet::TimeMS banTimeMS, int filterSetID);
  103. /// Set a user callback to be called on an invalid message for a particular filterSet
  104. /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings.
  105. /// \param[in] userData A pointer passed with the callback
  106. /// \param[in] invalidMessageCallback A pointer to a C function to be called back with the specified parameters.
  107. void SetDisallowedMessageCallback(int filterSetID, void *userData, void (*invalidMessageCallback)(RakPeerInterface *peer, AddressOrGUID addressOrGUID, int filterSetID, void *userData, unsigned char messageID));
  108. /// Set a user callback to be called when a user is disconnected due to SetFilterMaxTime
  109. /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings.
  110. /// \param[in] userData A pointer passed with the callback
  111. /// \param[in] invalidMessageCallback A pointer to a C function to be called back with the specified parameters.
  112. void SetTimeoutCallback(int filterSetID, void *userData, void (*invalidMessageCallback)(RakPeerInterface *peer, AddressOrGUID addressOrGUID, int filterSetID, void *userData));
  113. /// Limit how long a connection can stay in a particular filterSetID. After this time, the connection is kicked and possibly banned.
  114. /// By default there is no limit to how long a connection can stay in a particular filter set.
  115. /// \param[in] allowedTimeMS How many milliseconds to allow a connection to stay in this filter set.
  116. /// \param[in] banOnExceed True or false to ban the system, or not, when \a allowedTimeMS is exceeded
  117. /// \param[in] banTimeMS Passed to the milliseconds parameter of RakPeer::AddToBanList.
  118. /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings.
  119. void SetFilterMaxTime(int allowedTimeMS, bool banOnExceed, RakNet::TimeMS banTimeMS, int filterSetID);
  120. /// Get the filterSetID a system is using. Returns -1 for none.
  121. /// \param[in] addressOrGUID The system we are referring to
  122. int GetSystemFilterSet(AddressOrGUID addressOrGUID);
  123. /// Assign a system to a filter set.
  124. /// Systems are automatically added to filter sets (or not) based on SetAutoAddNewConnectionsToFilter()
  125. /// This function is used to change the filter set a system is using, to add it to a new filter set, or to remove it from all existin filter sets.
  126. /// \param[in] addressOrGUID The system we are referring to
  127. /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings. If -1, the system will be removed from all filter sets.
  128. void SetSystemFilterSet(AddressOrGUID addressOrGUID, int filterSetID);
  129. /// Returns the number of systems subscribed to a particular filter set
  130. /// Using anything other than -1 for \a filterSetID is slow, so you should store the returned value.
  131. /// \param[in] filterSetID The filter set to limit to. Use -1 for none (just returns the total number of filter systems in that case).
  132. unsigned GetSystemCount(int filterSetID) const;
  133. /// Returns the total number of filter sets.
  134. /// \return The total number of filter sets.
  135. unsigned GetFilterSetCount(void) const;
  136. /// Returns the ID of a filter set, by index
  137. /// \param[in] An index between 0 and GetFilterSetCount()-1 inclusive
  138. int GetFilterSetIDByIndex(unsigned index);
  139. /// Delete a FilterSet. All systems formerly subscribed to this filter are now unrestricted.
  140. /// \param[in] filterSetID The ID of the filter set to delete.
  141. void DeleteFilterSet(int filterSetID);
  142. // --------------------------------------------------------------------------------------------
  143. // Packet handling functions
  144. // --------------------------------------------------------------------------------------------
  145. virtual void Update(void);
  146. virtual PluginReceiveResult OnReceive(Packet *packet);
  147. virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming);
  148. virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
  149. protected:
  150. void Clear(void);
  151. void DeallocateFilterSet(FilterSet *filterSet);
  152. FilterSet* GetFilterSetByID(int filterSetID);
  153. void OnInvalidMessage(FilterSet *filterSet, AddressOrGUID systemAddress, unsigned char messageID);
  154. DataStructures::OrderedList<int, FilterSet*, FilterSetComp> filterList;
  155. // Change to guid
  156. DataStructures::Hash<AddressOrGUID, FilteredSystem, 2048, AddressOrGUID::ToInteger> systemList;
  157. int autoAddNewConnectionsToFilter;
  158. RakNet::Time whenLastTimeoutCheck;
  159. };
  160. } // namespace RakNet
  161. #endif
  162. #endif // _RAKNET_SUPPORT_*
粤ICP备19079148号