PageRenderTime 30ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/raknet/Router2.h

https://gitlab.com/computerphilly/openblox
C Header | 203 lines | 120 code | 32 blank | 51 comment | 1 complexity | 43b88e91afb1c8b08791e20fdca449db MD5 | raw file
  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 Router2 plugin. Allows you to connect to a system by routing packets through another system that is connected to both you and the destination. Useful for getting around NATs.
  12. ///
  13. #include "NativeFeatureIncludes.h"
  14. #if _RAKNET_SUPPORT_Router2==1 && _RAKNET_SUPPORT_UDPForwarder==1
  15. #ifndef __ROUTER_2_PLUGIN_H
  16. #define __ROUTER_2_PLUGIN_H
  17. #include "RakNetTypes.h"
  18. #include "PluginInterface2.h"
  19. #include "PacketPriority.h"
  20. #include "Export.h"
  21. #include "UDPForwarder.h"
  22. #include "MessageIdentifiers.h"
  23. #include "DS_List.h"
  24. #include "SimpleMutex.h"
  25. namespace RakNet
  26. {
  27. /// Forward declarations
  28. class RakPeerInterface;
  29. struct Router2DebugInterface
  30. {
  31. Router2DebugInterface() {}
  32. virtual ~Router2DebugInterface() {}
  33. virtual void ShowFailure(const char *message);
  34. virtual void ShowDiagnostic(const char *message);
  35. };
  36. /// \defgroup ROUTER_2_GROUP Router2
  37. /// \brief Part of the NAT punchthrough solution, allowing you to connect to systems by routing through a shared connection.
  38. /// \details Router2 routes datagrams between two systems that are not directly connected by using the bandwidth of a third system, to which the other two systems were connected
  39. /// It is of benefit when a fully connected mesh topology is desired, but could not be completely established due to routers and/or firewalls
  40. /// As the system address of a remote system will be the system address of the intermediary, it is necessary to use the RakNetGUID object to refer to systems, including with other plugins
  41. /// \ingroup PLUGINS_GROUP
  42. /// \ingroup ROUTER_2_GROUP
  43. /// \brief Class interface for the Router2 system
  44. /// \details
  45. class RAK_DLL_EXPORT Router2 : public PluginInterface2
  46. {
  47. public:
  48. // GetInstance() and DestroyInstance(instance*)
  49. STATIC_FACTORY_DECLARATIONS(Router2)
  50. Router2();
  51. virtual ~Router2();
  52. /// Sets the socket family to use, either IPV4 or IPV6
  53. /// \param[in] socketFamily For IPV4, use AF_INET (default). For IPV6, use AF_INET6. To autoselect, use AF_UNSPEC.
  54. void SetSocketFamily(unsigned short _socketFamily);
  55. /// \brief Query all connected systems to connect through them to a third system.
  56. /// System will return ID_ROUTER_2_FORWARDING_NO_PATH if unable to connect.
  57. /// Else you will get ID_ROUTER_2_FORWARDING_ESTABLISHED
  58. ///
  59. /// On ID_ROUTER_2_FORWARDING_ESTABLISHED, EstablishRouting as follows:
  60. ///
  61. /// RakNet::BitStream bs(packet->data, packet->length, false);
  62. /// bs.IgnoreBytes(sizeof(MessageID));
  63. /// RakNetGUID endpointGuid;
  64. /// bs.Read(endpointGuid);
  65. /// unsigned short sourceToDestPort;
  66. /// bs.Read(sourceToDestPort);
  67. /// char ipAddressString[32];
  68. /// packet->systemAddress.ToString(false, ipAddressString);
  69. /// rakPeerInterface->EstablishRouting(ipAddressString, sourceToDestPort, 0,0);
  70. ///
  71. /// \note The SystemAddress for a connection should not be used - always use RakNetGuid as the address can change at any time.
  72. /// When the address changes, you will get ID_ROUTER_2_REROUTED
  73. void EstablishRouting(RakNetGUID endpointGuid);
  74. /// Set the maximum number of bidirectional connections this system will support
  75. /// Defaults to 0
  76. void SetMaximumForwardingRequests(int max);
  77. /// For testing and debugging
  78. void SetDebugInterface(Router2DebugInterface *_debugInterface);
  79. /// Get the pointer passed to SetDebugInterface()
  80. Router2DebugInterface *GetDebugInterface(void) const;
  81. // --------------------------------------------------------------------------------------------
  82. // Packet handling functions
  83. // --------------------------------------------------------------------------------------------
  84. virtual PluginReceiveResult OnReceive(Packet *packet);
  85. virtual void Update(void);
  86. virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
  87. virtual void OnFailedConnectionAttempt(Packet *packet, PI2_FailedConnectionAttemptReason failedConnectionAttemptReason);
  88. virtual void OnRakPeerShutdown(void);
  89. enum Router2RequestStates
  90. {
  91. R2RS_REQUEST_STATE_QUERY_FORWARDING,
  92. REQUEST_STATE_REQUEST_FORWARDING,
  93. };
  94. struct ConnectionRequestSystem
  95. {
  96. RakNetGUID guid;
  97. int pingToEndpoint;
  98. unsigned short usedForwardingEntries;
  99. };
  100. struct ConnnectRequest
  101. {
  102. ConnnectRequest();
  103. ~ConnnectRequest();
  104. DataStructures::List<ConnectionRequestSystem> connectionRequestSystems;
  105. SimpleMutex connectionRequestSystemsMutex;
  106. Router2RequestStates requestState;
  107. RakNet::TimeMS pingTimeout;
  108. RakNetGUID endpointGuid;
  109. RakNetGUID lastRequestedForwardingSystem;
  110. bool returnConnectionLostOnFailure;
  111. unsigned int GetGuidIndex(RakNetGUID guid);
  112. };
  113. unsigned int GetConnectionRequestIndex(RakNetGUID endpointGuid);
  114. struct MiniPunchRequest
  115. {
  116. RakNetGUID endpointGuid;
  117. SystemAddress endpointAddress;
  118. bool gotReplyFromEndpoint;
  119. RakNetGUID sourceGuid;
  120. SystemAddress sourceAddress;
  121. bool gotReplyFromSource;
  122. RakNet::TimeMS timeout;
  123. RakNet::TimeMS nextAction;
  124. unsigned short forwardingPort;
  125. __UDPSOCKET__ forwardingSocket;
  126. };
  127. struct ForwardedConnection
  128. {
  129. RakNetGUID endpointGuid;
  130. RakNetGUID intermediaryGuid;
  131. SystemAddress intermediaryAddress;
  132. bool returnConnectionLostOnFailure;
  133. bool weInitiatedForwarding;
  134. };
  135. protected:
  136. bool UpdateForwarding(ConnnectRequest* connectionRequest);
  137. void RemoveConnectionRequest(unsigned int connectionRequestIndex);
  138. void RequestForwarding(ConnnectRequest* connectionRequest);
  139. void OnQueryForwarding(Packet *packet);
  140. void OnQueryForwardingReply(Packet *packet);
  141. void OnRequestForwarding(Packet *packet);
  142. void OnRerouted(Packet *packet);
  143. void OnMiniPunchReply(Packet *packet);
  144. void OnMiniPunchReplyBounce(Packet *packet);
  145. bool OnForwardingSuccess(Packet *packet);
  146. int GetLargestPingAmongConnectedSystems(void) const;
  147. void ReturnToUser(MessageID messageId, RakNetGUID endpointGuid, const SystemAddress &systemAddress, bool wasGeneratedLocally);
  148. bool ConnectInternal(RakNetGUID endpointGuid, bool returnConnectionLostOnFailure);
  149. UDPForwarder *udpForwarder;
  150. int maximumForwardingRequests;
  151. SimpleMutex connectionRequestsMutex, miniPunchesInProgressMutex, forwardedConnectionListMutex;
  152. DataStructures::List<ConnnectRequest*> connectionRequests;
  153. DataStructures::List<MiniPunchRequest> miniPunchesInProgress;
  154. // Forwarding we have initiated
  155. DataStructures::List<ForwardedConnection> forwardedConnectionList;
  156. void ClearConnectionRequests(void);
  157. void ClearMinipunches(void);
  158. void ClearForwardedConnections(void);
  159. void ClearAll(void);
  160. int ReturnFailureOnCannotForward(RakNetGUID sourceGuid, RakNetGUID endpointGuid);
  161. void SendFailureOnCannotForward(RakNetGUID sourceGuid, RakNetGUID endpointGuid);
  162. void SendForwardingSuccess(MessageID messageId, RakNetGUID sourceGuid, RakNetGUID endpointGuid, unsigned short sourceToDstPort);
  163. void SendOOBFromRakNetPort(OutOfBandIdentifiers oob, BitStream *extraData, SystemAddress sa);
  164. void SendOOBFromSpecifiedSocket(OutOfBandIdentifiers oob, SystemAddress sa, __UDPSOCKET__ socket);
  165. void SendOOBMessages(MiniPunchRequest *mpr);
  166. Router2DebugInterface *debugInterface;
  167. unsigned short socketFamily;
  168. };
  169. }
  170. #endif
  171. #endif // _RAKNET_SUPPORT_*