• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

NatPunchthroughClient.h

Go to the documentation of this file.
00001 
00008 
00009 #include "NativeFeatureIncludes.h"
00010 #if _RAKNET_SUPPORT_NatPunchthroughClient==1
00011 
00012 #ifndef __NAT_PUNCHTHROUGH_CLIENT_H
00013 #define __NAT_PUNCHTHROUGH_CLIENT_H
00014 
00015 #include "RakNetTypes.h"
00016 #include "Export.h"
00017 #include "PluginInterface2.h"
00018 #include "PacketPriority.h"
00019 #include "SocketIncludes.h"
00020 #include "DS_List.h"
00021 #include "RakString.h"
00022 
00023 // Trendnet TEW-632BRP sometimes starts at port 1024 and increments sequentially.
00024 // Zonnet zsr1134we. Replies go out on the net, but are always absorbed by the remote router??
00025 // Dlink ebr2310 to Trendnet ok
00026 // Trendnet TEW-652BRP to Trendnet 632BRP OK
00027 // Trendnet TEW-632BRP to Trendnet 632BRP OK
00028 // Buffalo WHR-HP-G54 OK
00029 // Netgear WGR614 ok
00030 
00031 class RakPeerInterface;
00032 struct Packet;
00033 #if _RAKNET_SUPPORT_PacketLogger==1
00034 class PacketLogger;
00035 #endif
00036 
00038 struct RAK_DLL_EXPORT PunchthroughConfiguration
00039 {
00043         PunchthroughConfiguration() {
00044                 TIME_BETWEEN_PUNCH_ATTEMPTS_INTERNAL=15;
00045                 TIME_BETWEEN_PUNCH_ATTEMPTS_EXTERNAL=50;
00046                 UDP_SENDS_PER_PORT_INTERNAL=2;
00047                 UDP_SENDS_PER_PORT_EXTERNAL=8;
00048                 INTERNAL_IP_WAIT_AFTER_ATTEMPTS=30;
00049                 MAXIMUM_NUMBER_OF_INTERNAL_IDS_TO_CHECK=5; 
00050                 MAX_PREDICTIVE_PORT_RANGE=2;
00051                 EXTERNAL_IP_WAIT_BETWEEN_PORTS=100;
00052                 EXTERNAL_IP_WAIT_AFTER_ALL_ATTEMPTS=EXTERNAL_IP_WAIT_BETWEEN_PORTS;
00053                 retryOnFailure=false;
00054         }
00055 
00057         RakNetTimeMS TIME_BETWEEN_PUNCH_ATTEMPTS_INTERNAL;
00058         RakNetTimeMS TIME_BETWEEN_PUNCH_ATTEMPTS_EXTERNAL;
00059 
00061         int UDP_SENDS_PER_PORT_INTERNAL;
00062         int UDP_SENDS_PER_PORT_EXTERNAL;
00063 
00065         int INTERNAL_IP_WAIT_AFTER_ATTEMPTS;
00066 
00068         int MAX_PREDICTIVE_PORT_RANGE;
00069 
00071         int EXTERNAL_IP_WAIT_BETWEEN_PORTS;
00072 
00074         int EXTERNAL_IP_WAIT_AFTER_ALL_ATTEMPTS;
00075 
00079         int MAXIMUM_NUMBER_OF_INTERNAL_IDS_TO_CHECK;
00080 
00083         bool retryOnFailure;
00084 };
00085 
00087 struct NatPunchthroughDebugInterface
00088 {
00089         NatPunchthroughDebugInterface() {}
00090         virtual ~NatPunchthroughDebugInterface() {}
00091         virtual void OnClientMessage(const char *msg)=0;
00092 };
00093 
00095 struct NatPunchthroughDebugInterface_Printf : public NatPunchthroughDebugInterface
00096 {
00097         virtual void OnClientMessage(const char *msg);
00098 };
00099 
00100 #if _RAKNET_SUPPORT_PacketLogger==1
00101 
00102 struct NatPunchthroughDebugInterface_PacketLogger : public NatPunchthroughDebugInterface
00103 {
00104         // Set to non-zero to write to the packetlogger!
00105         PacketLogger *pl;
00106 
00107         NatPunchthroughDebugInterface_PacketLogger() {pl=0;}
00108         ~NatPunchthroughDebugInterface_PacketLogger() {}
00109         virtual void OnClientMessage(const char *msg);
00110 };
00111 #endif
00112 
00120 class RAK_DLL_EXPORT NatPunchthroughClient : public PluginInterface2
00121 {
00122 public:
00123         NatPunchthroughClient();
00124         ~NatPunchthroughClient();
00125 
00127         bool OpenNAT(RakNetGUID destination, SystemAddress facilitator);
00128 
00131         PunchthroughConfiguration* GetPunchthroughConfiguration(void);
00132 
00135         void SetDebugInterface(NatPunchthroughDebugInterface *i);
00136 
00141         unsigned short GetUPNPExternalPort(void) const;
00142 
00146         unsigned short GetUPNPInternalPort(void) const;
00147 
00151         RakNet::RakString GetUPNPInternalAddress(void) const;
00152 
00154         virtual void Update(void);
00155 
00157         virtual PluginReceiveResult OnReceive(Packet *packet);
00158 
00160         virtual void OnNewConnection(SystemAddress systemAddress, RakNetGUID rakNetGUID, bool isIncoming);
00161 
00163         virtual void OnClosedConnection(SystemAddress systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
00164 
00165         virtual void OnAttach(void);
00166         virtual void OnDetach(void);
00167         virtual void OnRakPeerShutdown(void);
00168         void Clear(void);
00169 
00170 protected:
00171         unsigned short mostRecentNewExternalPort;
00172         void OnGetMostRecentPort(Packet *packet);
00173         void OnConnectAtTime(Packet *packet);
00174         unsigned int GetPendingOpenNATIndex(RakNetGUID destination, SystemAddress facilitator);
00175         void SendPunchthrough(RakNetGUID destination, SystemAddress facilitator);
00176         void SendTTL(SystemAddress sa);
00177         void SendOutOfBand(SystemAddress sa, MessageID oobId);
00178         void OnPunchthroughFailure(void);
00179         void OnReadyForNextPunchthrough(void);
00180         void PushFailure(void);
00181         bool RemoveFromFailureQueue(void);
00182         void PushSuccess(void);
00183         //void ProcessNextPunchthroughQueue(void);
00184 
00185         /*
00186         struct PendingOpenNAT
00187         {
00188                 RakNetGUID destination;
00189                 SystemAddress facilitator;
00190         };
00191         DataStructures::List<PendingOpenNAT> pendingOpenNAT;
00192         */
00193 
00194         struct SendPing
00195         {
00196                 RakNetTime nextActionTime;
00197                 SystemAddress targetAddress;
00198                 SystemAddress facilitator;
00199                 SystemAddress internalIds[MAXIMUM_NUMBER_OF_INTERNAL_IDS];
00200                 RakNetGUID targetGuid;
00201                 bool weAreSender;
00202                 int attemptCount;
00203                 int retryCount;
00204                 int punchingFixedPortAttempts; // only used for TestMode::PUNCHING_FIXED_PORT
00205                 uint16_t sessionId;
00206                 // Give priority to internal IP addresses because if we are on a LAN, we don't want to try to connect through the internet
00207                 enum TestMode
00208                 {
00209                         TESTING_INTERNAL_IPS,
00210                         WAITING_FOR_INTERNAL_IPS_RESPONSE,
00211                         TESTING_EXTERNAL_IPS_FROM_FACILITATOR_PORT,
00212                         TESTING_EXTERNAL_IPS_FROM_1024,
00213                         WAITING_AFTER_ALL_ATTEMPTS,
00214 
00215                         // The trendnet remaps the remote port to 1024.
00216                         // If you continue punching on a different port for the same IP it bans you and the communication becomes unidirectioal
00217                         PUNCHING_FIXED_PORT,
00218 
00219                         // try port 1024-1028
00220                 } testMode;
00221         } sp;
00222 
00223         PunchthroughConfiguration pc;
00224         NatPunchthroughDebugInterface *natPunchthroughDebugInterface;
00225 
00226         // The first time we fail a NAT attempt, we add it to failedAttemptList and try again, since sometimes trying again later fixes the problem
00227         // The second time we fail, we return ID_NAT_PUNCHTHROUGH_FAILED
00228         struct AddrAndGuid
00229         {
00230                 SystemAddress addr;
00231                 RakNetGUID guid;
00232         };
00233         DataStructures::List<AddrAndGuid> failedAttemptList;
00234 };
00235 
00236 #endif
00237 
00238 #endif // _RAKNET_SUPPORT_*

Generated on Thu Sep 30 2010 01:27:25 for RakNet by  doxygen 1.7.1