MessageFilter.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. #include "NativeFeatureIncludes.h"
  11. #if _RAKNET_SUPPORT_MessageFilter==1
  12. #include "MessageFilter.h"
  13. #include "RakAssert.h"
  14. #include "GetTime.h"
  15. #include "MessageIdentifiers.h"
  16. #include "RakAssert.h"
  17. #include "RakPeerInterface.h"
  18. #include "PacketizedTCP.h"
  19. #include "BitStream.h"
  20. #ifdef _MSC_VER
  21. #pragma warning( push )
  22. #endif
  23. using namespace RakNet;
  24. int RakNet::MessageFilterStrComp( char *const &key,char *const &data )
  25. {
  26. return strcmp(key,data);
  27. }
  28. int RakNet::FilterSetComp( const int &key, FilterSet * const &data )
  29. {
  30. if (key < data->filterSetID)
  31. return -1;
  32. else if (key==data->filterSetID)
  33. return 0;
  34. else
  35. return 1;
  36. }
  37. STATIC_FACTORY_DEFINITIONS(MessageFilter,MessageFilter);
  38. MessageFilter::MessageFilter()
  39. {
  40. whenLastTimeoutCheck=RakNet::GetTime();
  41. }
  42. MessageFilter::~MessageFilter()
  43. {
  44. Clear();
  45. }
  46. void MessageFilter::SetAutoAddNewConnectionsToFilter(int filterSetID)
  47. {
  48. autoAddNewConnectionsToFilter=filterSetID;
  49. }
  50. void MessageFilter::SetAllowMessageID(bool allow, int messageIDStart, int messageIDEnd,int filterSetID)
  51. {
  52. RakAssert(messageIDStart <= messageIDEnd);
  53. FilterSet *filterSet = GetFilterSetByID(filterSetID);
  54. int i;
  55. for (i=messageIDStart; i <= messageIDEnd; ++i)
  56. filterSet->allowedIDs[i]=allow;
  57. }
  58. void MessageFilter::SetAllowRPC4(bool allow, const char* uniqueID, int filterSetID)
  59. {
  60. FilterSet *filterSet = GetFilterSetByID(filterSetID);
  61. bool objectExists;
  62. unsigned int idx = filterSet->allowedRPC4.GetIndexFromKey(uniqueID, &objectExists);
  63. if (allow)
  64. {
  65. if (objectExists==false)
  66. {
  67. filterSet->allowedRPC4.InsertAtIndex(uniqueID, idx, _FILE_AND_LINE_);
  68. filterSet->allowedIDs[ID_RPC_PLUGIN]=true;
  69. }
  70. }
  71. else
  72. {
  73. if (objectExists==true)
  74. {
  75. filterSet->allowedRPC4.RemoveAtIndex(idx);
  76. if (filterSet->allowedRPC4.Size()==0)
  77. {
  78. filterSet->allowedIDs[ID_RPC_PLUGIN]=false;
  79. }
  80. }
  81. }
  82. }
  83. void MessageFilter::SetActionOnDisallowedMessage(bool kickOnDisallowed, bool banOnDisallowed, RakNet::TimeMS banTimeMS, int filterSetID)
  84. {
  85. FilterSet *filterSet = GetFilterSetByID(filterSetID);
  86. filterSet->kickOnDisallowedMessage=kickOnDisallowed;
  87. filterSet->disallowedMessageBanTimeMS=banTimeMS;
  88. filterSet->banOnDisallowedMessage=banOnDisallowed;
  89. }
  90. void MessageFilter::SetDisallowedMessageCallback(int filterSetID, void *userData, void (*invalidMessageCallback)(RakPeerInterface *peer, AddressOrGUID systemAddress, int filterSetID, void *userData, unsigned char messageID))
  91. {
  92. FilterSet *filterSet = GetFilterSetByID(filterSetID);
  93. filterSet->invalidMessageCallback=invalidMessageCallback;
  94. filterSet->disallowedCallbackUserData=userData;
  95. }
  96. void MessageFilter::SetTimeoutCallback(int filterSetID, void *userData, void (*invalidMessageCallback)(RakPeerInterface *peer, AddressOrGUID systemAddress, int filterSetID, void *userData))
  97. {
  98. FilterSet *filterSet = GetFilterSetByID(filterSetID);
  99. filterSet->timeoutCallback=invalidMessageCallback;
  100. filterSet->timeoutUserData=userData;
  101. }
  102. void MessageFilter::SetFilterMaxTime(int allowedTimeMS, bool banOnExceed, RakNet::TimeMS banTimeMS, int filterSetID)
  103. {
  104. FilterSet *filterSet = GetFilterSetByID(filterSetID);
  105. filterSet->maxMemberTimeMS=allowedTimeMS;
  106. filterSet->banOnFilterTimeExceed=banOnExceed;
  107. filterSet->timeExceedBanTimeMS=banTimeMS;
  108. }
  109. int MessageFilter::GetSystemFilterSet(AddressOrGUID systemAddress)
  110. {
  111. // bool objectExists;
  112. // unsigned index = systemList.GetIndexFromKey(systemAddress, &objectExists);
  113. // if (objectExists==false)
  114. // return -1;
  115. // else
  116. // return systemList[index].filter->filterSetID;
  117. DataStructures::HashIndex index = systemList.GetIndexOf(systemAddress);
  118. if (index.IsInvalid())
  119. return -1;
  120. else
  121. return systemList.ItemAtIndex(index).filter->filterSetID;
  122. }
  123. void MessageFilter::SetSystemFilterSet(AddressOrGUID addressOrGUID, int filterSetID)
  124. {
  125. // Allocate this filter set if it doesn't exist.
  126. RakAssert(addressOrGUID.IsUndefined()==false);
  127. // bool objectExists;
  128. // unsigned index = systemList.GetIndexFromKey(addressOrGUID, &objectExists);
  129. // if (objectExists==false)
  130. DataStructures::HashIndex index = systemList.GetIndexOf(addressOrGUID);
  131. if (index.IsInvalid())
  132. {
  133. if (filterSetID<0)
  134. return;
  135. FilteredSystem filteredSystem;
  136. filteredSystem.filter = GetFilterSetByID(filterSetID);
  137. // filteredSystem.addressOrGUID=addressOrGUID;
  138. filteredSystem.timeEnteredThisSet=RakNet::GetTimeMS();
  139. // systemList.Insert(addressOrGUID, filteredSystem, true, _FILE_AND_LINE_);
  140. systemList.Push(addressOrGUID,filteredSystem,_FILE_AND_LINE_);
  141. }
  142. else
  143. {
  144. if (filterSetID>=0)
  145. {
  146. FilterSet *filterSet = GetFilterSetByID(filterSetID);
  147. systemList.ItemAtIndex(index).timeEnteredThisSet=RakNet::GetTimeMS();
  148. systemList.ItemAtIndex(index).filter=filterSet;
  149. }
  150. else
  151. {
  152. systemList.RemoveAtIndex(index, _FILE_AND_LINE_);
  153. }
  154. }
  155. }
  156. unsigned MessageFilter::GetSystemCount(int filterSetID) const
  157. {
  158. if (filterSetID==-1)
  159. {
  160. return systemList.Size();
  161. }
  162. else
  163. {
  164. unsigned i;
  165. unsigned count=0;
  166. DataStructures::List< FilteredSystem > itemList;
  167. DataStructures::List< AddressOrGUID > keyList;
  168. systemList.GetAsList(itemList, keyList, _FILE_AND_LINE_);
  169. for (i=0; i < itemList.Size(); i++)
  170. if (itemList[i].filter->filterSetID==filterSetID)
  171. ++count;
  172. return count;
  173. }
  174. }
  175. unsigned MessageFilter::GetFilterSetCount(void) const
  176. {
  177. return filterList.Size();
  178. }
  179. int MessageFilter::GetFilterSetIDByIndex(unsigned index)
  180. {
  181. return filterList[index]->filterSetID;
  182. }
  183. void MessageFilter::DeleteFilterSet(int filterSetID)
  184. {
  185. FilterSet *filterSet;
  186. bool objectExists;
  187. unsigned i,index;
  188. index = filterList.GetIndexFromKey(filterSetID, &objectExists);
  189. if (objectExists)
  190. {
  191. filterSet=filterList[index];
  192. DeallocateFilterSet(filterSet);
  193. filterList.RemoveAtIndex(index);
  194. DataStructures::List< FilteredSystem > itemList;
  195. DataStructures::List< AddressOrGUID > keyList;
  196. systemList.GetAsList(itemList, keyList, _FILE_AND_LINE_);
  197. for (i=0; i < itemList.Size(); i++)
  198. {
  199. if (itemList[i].filter==filterSet)
  200. {
  201. systemList.Remove(keyList[i], _FILE_AND_LINE_);
  202. }
  203. }
  204. /*
  205. // Don't reference this pointer any longer
  206. i=0;
  207. while (i < systemList.Size())
  208. {
  209. if (systemList[i].filter==filterSet)
  210. systemList.RemoveAtIndex(i);
  211. else
  212. ++i;
  213. }
  214. */
  215. }
  216. }
  217. void MessageFilter::Clear(void)
  218. {
  219. unsigned i;
  220. systemList.Clear(_FILE_AND_LINE_);
  221. for (i=0; i < filterList.Size(); i++)
  222. DeallocateFilterSet(filterList[i]);
  223. filterList.Clear(false, _FILE_AND_LINE_);
  224. }
  225. void MessageFilter::DeallocateFilterSet(FilterSet* filterSet)
  226. {
  227. RakNet::OP_DELETE(filterSet, _FILE_AND_LINE_);
  228. }
  229. FilterSet* MessageFilter::GetFilterSetByID(int filterSetID)
  230. {
  231. RakAssert(filterSetID>=0);
  232. bool objectExists;
  233. unsigned index;
  234. index = filterList.GetIndexFromKey(filterSetID, &objectExists);
  235. if (objectExists)
  236. return filterList[index];
  237. else
  238. {
  239. FilterSet *newFilterSet = RakNet::OP_NEW<FilterSet>( _FILE_AND_LINE_ );
  240. memset(newFilterSet->allowedIDs, 0, MESSAGE_FILTER_MAX_MESSAGE_ID * sizeof(bool));
  241. newFilterSet->banOnFilterTimeExceed=false;
  242. newFilterSet->kickOnDisallowedMessage=false;
  243. newFilterSet->banOnDisallowedMessage=false;
  244. newFilterSet->disallowedMessageBanTimeMS=0;
  245. newFilterSet->timeExceedBanTimeMS=0;
  246. newFilterSet->maxMemberTimeMS=0;
  247. newFilterSet->filterSetID=filterSetID;
  248. newFilterSet->invalidMessageCallback=0;
  249. newFilterSet->timeoutCallback=0;
  250. newFilterSet->timeoutUserData=0;
  251. filterList.Insert(filterSetID, newFilterSet, true, _FILE_AND_LINE_);
  252. return newFilterSet;
  253. }
  254. }
  255. void MessageFilter::OnInvalidMessage(FilterSet *filterSet, AddressOrGUID systemAddress, unsigned char messageID)
  256. {
  257. if (filterSet->invalidMessageCallback)
  258. filterSet->invalidMessageCallback(rakPeerInterface, systemAddress, filterSet->filterSetID, filterSet->disallowedCallbackUserData, messageID);
  259. if (filterSet->banOnDisallowedMessage && rakPeerInterface)
  260. {
  261. char str1[64];
  262. systemAddress.systemAddress.ToString(false, str1);
  263. rakPeerInterface->AddToBanList(str1, filterSet->disallowedMessageBanTimeMS);
  264. }
  265. if (filterSet->kickOnDisallowedMessage)
  266. {
  267. if (rakPeerInterface)
  268. rakPeerInterface->CloseConnection(systemAddress, true, 0);
  269. #if _RAKNET_SUPPORT_PacketizedTCP==1 && _RAKNET_SUPPORT_TCPInterface==1
  270. else
  271. tcpInterface->CloseConnection(systemAddress.systemAddress);
  272. #endif
  273. }
  274. }
  275. void MessageFilter::Update(void)
  276. {
  277. // Update all timers for all systems. If those systems' filter sets are expired, take the appropriate action.
  278. RakNet::Time curTime = RakNet::GetTime();
  279. if (GreaterThan(curTime - 1000, whenLastTimeoutCheck))
  280. {
  281. DataStructures::List< FilteredSystem > itemList;
  282. DataStructures::List< AddressOrGUID > keyList;
  283. systemList.GetAsList(itemList, keyList, _FILE_AND_LINE_);
  284. unsigned int index;
  285. for (index=0; index < itemList.Size(); index++)
  286. {
  287. if (itemList[index].filter &&
  288. itemList[index].filter->maxMemberTimeMS>0 &&
  289. curTime-itemList[index].timeEnteredThisSet >= itemList[index].filter->maxMemberTimeMS)
  290. {
  291. if (itemList[index].filter->timeoutCallback)
  292. itemList[index].filter->timeoutCallback(rakPeerInterface, keyList[index], itemList[index].filter->filterSetID, itemList[index].filter->timeoutUserData);
  293. if (itemList[index].filter->banOnFilterTimeExceed && rakPeerInterface)
  294. {
  295. char str1[64];
  296. keyList[index].ToString(false, str1);
  297. rakPeerInterface->AddToBanList(str1, itemList[index].filter->timeExceedBanTimeMS);
  298. }
  299. if (rakPeerInterface)
  300. rakPeerInterface->CloseConnection(keyList[index], true, 0);
  301. #if _RAKNET_SUPPORT_PacketizedTCP==1 && _RAKNET_SUPPORT_TCPInterface==1
  302. else
  303. tcpInterface->CloseConnection(keyList[index].systemAddress);
  304. #endif
  305. systemList.Remove(keyList[index], _FILE_AND_LINE_);
  306. }
  307. }
  308. whenLastTimeoutCheck=curTime+1000;
  309. }
  310. }
  311. void MessageFilter::OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming)
  312. {
  313. (void) systemAddress;
  314. (void) rakNetGUID;
  315. (void) isIncoming;
  316. AddressOrGUID aog;
  317. aog.rakNetGuid=rakNetGUID;
  318. aog.systemAddress=systemAddress;
  319. // New system, automatically assign to filter set if appropriate
  320. if (autoAddNewConnectionsToFilter>=0 && systemList.HasData(aog)==false)
  321. SetSystemFilterSet(aog, autoAddNewConnectionsToFilter);
  322. }
  323. void MessageFilter::OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason )
  324. {
  325. (void) rakNetGUID;
  326. (void) lostConnectionReason;
  327. AddressOrGUID aog;
  328. aog.rakNetGuid=rakNetGUID;
  329. aog.systemAddress=systemAddress;
  330. // Lost system, remove from the list
  331. systemList.Remove(aog, _FILE_AND_LINE_);
  332. }
  333. PluginReceiveResult MessageFilter::OnReceive(Packet *packet)
  334. {
  335. DataStructures::HashIndex index;
  336. unsigned char messageId;
  337. switch (packet->data[0])
  338. {
  339. case ID_NEW_INCOMING_CONNECTION:
  340. case ID_CONNECTION_REQUEST_ACCEPTED:
  341. case ID_CONNECTION_LOST:
  342. case ID_DISCONNECTION_NOTIFICATION:
  343. case ID_CONNECTION_ATTEMPT_FAILED:
  344. case ID_NO_FREE_INCOMING_CONNECTIONS:
  345. case ID_IP_RECENTLY_CONNECTED:
  346. case ID_CONNECTION_BANNED:
  347. case ID_INVALID_PASSWORD:
  348. case ID_UNCONNECTED_PONG:
  349. case ID_ALREADY_CONNECTED:
  350. case ID_ADVERTISE_SYSTEM:
  351. case ID_REMOTE_DISCONNECTION_NOTIFICATION:
  352. case ID_REMOTE_CONNECTION_LOST:
  353. case ID_REMOTE_NEW_INCOMING_CONNECTION:
  354. case ID_DOWNLOAD_PROGRESS:
  355. break;
  356. default:
  357. if (packet->data[0]==ID_TIMESTAMP)
  358. {
  359. if (packet->length<sizeof(MessageID) + sizeof(RakNet::TimeMS))
  360. return RR_STOP_PROCESSING_AND_DEALLOCATE; // Invalid message
  361. messageId=packet->data[sizeof(MessageID) + sizeof(RakNet::TimeMS)];
  362. }
  363. else
  364. messageId=packet->data[0];
  365. // If this system is filtered, check if this message is allowed. If not allowed, return RR_STOP_PROCESSING_AND_DEALLOCATE
  366. // index = systemList.GetIndexFromKey(packet->addressOrGUID, &objectExists);
  367. index = systemList.GetIndexOf(packet);
  368. if (index.IsInvalid())
  369. break;
  370. if (systemList.ItemAtIndex(index).filter->allowedIDs[messageId]==false)
  371. {
  372. OnInvalidMessage(systemList.ItemAtIndex(index).filter, packet, packet->data[0]);
  373. return RR_STOP_PROCESSING_AND_DEALLOCATE;
  374. }
  375. if (packet->data[0]==ID_RPC_PLUGIN)
  376. {
  377. RakNet::BitStream bsIn(packet->data,packet->length,false);
  378. bsIn.IgnoreBytes(2);
  379. RakNet::RakString functionName;
  380. bsIn.ReadCompressed(functionName);
  381. if (systemList.ItemAtIndex(index).filter->allowedRPC4.HasData(functionName)==false)
  382. {
  383. OnInvalidMessage(systemList.ItemAtIndex(index).filter, packet, packet->data[0]);
  384. return RR_STOP_PROCESSING_AND_DEALLOCATE;
  385. }
  386. }
  387. break;
  388. }
  389. return RR_CONTINUE_PROCESSING;
  390. }
  391. #ifdef _MSC_VER
  392. #pragma warning( pop )
  393. #endif
  394. #endif // _RAKNET_SUPPORT_*
粤ICP备19079148号