SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NatPunchthroughClient.h
Go to the documentation of this file.
1 /*
2 * Original work: 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 * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7 * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8 *
9 *
10 * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13 * license found in the license.txt file in the root directory of this source tree.
14 */
15
19
20 #include "NativeFeatureIncludes.h"
21 #if _RAKNET_SUPPORT_NatPunchthroughClient==1
22
23 #ifndef __NAT_PUNCHTHROUGH_CLIENT_H
24 #define __NAT_PUNCHTHROUGH_CLIENT_H
25
26 #include "types.h"
27 #include "Export.h"
28 #include "PluginInterface2.h"
29 #include "PacketPriority.h"
30 #include "SocketIncludes.h"
31 #include "DS_List.h"
32 #include "string.h"
33 #include "DS_Queue.h"
34
35 // Trendnet TEW-632BRP sometimes starts at port 1024 and increments sequentially.
36 // Zonnet zsr1134we. Replies go out on the net, but are always absorbed by the remote router??
37 // Dlink ebr2310 to Trendnet ok
38 // Trendnet TEW-652BRP to Trendnet 632BRP OK
39 // Trendnet TEW-632BRP to Trendnet 632BRP OK
40 // Buffalo WHR-HP-G54 OK
41 // Netgear WGR614 ok
42
43 namespace SLNet
44 {
46 class RakPeerInterface;
47 struct Packet;
48 #if _RAKNET_SUPPORT_PacketLogger==1
49 class PacketLogger;
50 #endif
51
53 struct RAK_DLL_EXPORT PunchthroughConfiguration
54 {
58 PunchthroughConfiguration() {
59 TIME_BETWEEN_PUNCH_ATTEMPTS_INTERNAL=15;
60 TIME_BETWEEN_PUNCH_ATTEMPTS_EXTERNAL=50;
61 UDP_SENDS_PER_PORT_INTERNAL=2;
62 UDP_SENDS_PER_PORT_EXTERNAL=8;
63 INTERNAL_IP_WAIT_AFTER_ATTEMPTS=30;
64 MAXIMUM_NUMBER_OF_INTERNAL_IDS_TO_CHECK=5;
65 MAX_PREDICTIVE_PORT_RANGE=2;
66 EXTERNAL_IP_WAIT_BETWEEN_PORTS=200;
67 EXTERNAL_IP_WAIT_AFTER_FIRST_TTL=100;
68 EXTERNAL_IP_WAIT_AFTER_ALL_ATTEMPTS=EXTERNAL_IP_WAIT_BETWEEN_PORTS;
69 retryOnFailure=false;
70 }
71
73 SLNet::Time TIME_BETWEEN_PUNCH_ATTEMPTS_INTERNAL;
74 SLNet::Time TIME_BETWEEN_PUNCH_ATTEMPTS_EXTERNAL;
75
77 int UDP_SENDS_PER_PORT_INTERNAL;
78 int UDP_SENDS_PER_PORT_EXTERNAL;
79
81 int INTERNAL_IP_WAIT_AFTER_ATTEMPTS;
82
84 int MAX_PREDICTIVE_PORT_RANGE;
85
87 int EXTERNAL_IP_WAIT_AFTER_FIRST_TTL;
88
90 int EXTERNAL_IP_WAIT_BETWEEN_PORTS;
91
93 int EXTERNAL_IP_WAIT_AFTER_ALL_ATTEMPTS;
94
98 int MAXIMUM_NUMBER_OF_INTERNAL_IDS_TO_CHECK;
99
102 bool retryOnFailure;
103 };
104
106 struct RAK_DLL_EXPORT NatPunchthroughDebugInterface
107 {
108 NatPunchthroughDebugInterface() {}
109 virtual ~NatPunchthroughDebugInterface() {}
110 virtual void OnClientMessage(const char *msg)=0;
111 };
112
114 struct RAK_DLL_EXPORT NatPunchthroughDebugInterface_Printf : public NatPunchthroughDebugInterface
115 {
116 virtual void OnClientMessage(const char *msg);
117 };
118
119 #if _RAKNET_SUPPORT_PacketLogger==1
120
121 struct RAK_DLL_EXPORT NatPunchthroughDebugInterface_PacketLogger : public NatPunchthroughDebugInterface
122 {
123 // Set to non-zero to write to the packetlogger!
124 PacketLogger *pl;
125
126 NatPunchthroughDebugInterface_PacketLogger() {pl=0;}
127 ~NatPunchthroughDebugInterface_PacketLogger() {}
128 virtual void OnClientMessage(const char *msg);
129 };
130 #endif
131
139 class RAK_DLL_EXPORT NatPunchthroughClient : public PluginInterface2
140 {
141 public:
142
143 // GetInstance() and DestroyInstance(instance*)
144 STATIC_FACTORY_DECLARATIONS(NatPunchthroughClient)
145
146 NatPunchthroughClient();
147 ~NatPunchthroughClient();
148
153 void FindRouterPortStride(const SystemAddress &facilitator);
154
162 bool OpenNAT(RakNetGUID destination, const SystemAddress &facilitator);
163
164 /*
172 bool OpenNATGroup(DataStructures::List<RakNetGUID> destinationSystems, const SystemAddress &facilitator);
173 */
174
177 PunchthroughConfiguration* GetPunchthroughConfiguration(void);
178
181 void SetDebugInterface(NatPunchthroughDebugInterface *i);
182
184 void GetUPNPPortMappings(char *externalPort, char *internalPort, const SystemAddress &natPunchthroughServerAddress);
185
187 virtual void Update(void);
188
190 virtual PluginReceiveResult OnReceive(Packet *packet);
191
193 virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming);
194
196 virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
197
198 virtual void OnAttach(void);
199 virtual void OnDetach(void);
200 virtual void OnRakPeerShutdown(void);
201 void Clear(void);
202
203 struct SendPing
204 {
205 SLNet::Time nextActionTime;
206 SystemAddress targetAddress;
207 SystemAddress facilitator;
208 SystemAddress internalIds[MAXIMUM_NUMBER_OF_INTERNAL_IDS];
209 RakNetGUID targetGuid;
210 bool weAreSender;
211 int attemptCount;
212 int retryCount;
213 int punchingFixedPortAttempts; // only used for TestMode::PUNCHING_FIXED_PORT
214 uint16_t sessionId;
215 bool sentTTL;
216 // Give priority to internal IP addresses because if we are on a LAN, we don't want to try to connect through the internet
217 enum TestMode
218 {
219 TESTING_INTERNAL_IPS,
220 WAITING_FOR_INTERNAL_IPS_RESPONSE,
221 //SEND_WITH_TTL,
222 TESTING_EXTERNAL_IPS_FACILITATOR_PORT_TO_FACILITATOR_PORT,
223 TESTING_EXTERNAL_IPS_1024_TO_FACILITATOR_PORT,
224 TESTING_EXTERNAL_IPS_FACILITATOR_PORT_TO_1024,
225 TESTING_EXTERNAL_IPS_1024_TO_1024,
226 WAITING_AFTER_ALL_ATTEMPTS,
227
228 // The trendnet remaps the remote port to 1024.
229 // If you continue punching on a different port for the same IP it bans you and the communication becomes unidirectioal
230 PUNCHING_FIXED_PORT,
231
232 // try port 1024-1028
233 } testMode;
234 } sp;
235
236 protected:
237 unsigned short mostRecentExternalPort;
238 //void OnNatGroupPunchthroughRequest(Packet *packet);
239 void OnFailureNotification(Packet *packet);
240 //void OnNatGroupPunchthroughReply(Packet *packet);
241 void OnGetMostRecentPort(Packet *packet);
242 void OnConnectAtTime(Packet *packet);
243 unsigned int GetPendingOpenNATIndex(RakNetGUID destination, const SystemAddress &facilitator);
244 void SendPunchthrough(RakNetGUID destination, const SystemAddress &facilitator);
245 void QueueOpenNAT(RakNetGUID destination, const SystemAddress &facilitator);
246 void SendQueuedOpenNAT(void);
247 void SendTTL(const SystemAddress &sa);
248 void SendOutOfBand(SystemAddress sa, MessageID oobId);
249 void OnPunchthroughFailure(void);
250 void OnReadyForNextPunchthrough(void);
251 void PushFailure(void);
252 bool RemoveFromFailureQueue(void);
253 void PushSuccess(void);
254
255 PunchthroughConfiguration pc;
256 NatPunchthroughDebugInterface *natPunchthroughDebugInterface;
257
258 // The first time we fail a NAT attempt, we add it to failedAttemptList and try again, since sometimes trying again later fixes the problem
259 // The second time we fail, we return ID_NAT_PUNCHTHROUGH_FAILED
260 struct AddrAndGuid
261 {
262 SystemAddress addr;
263 RakNetGUID guid;
264 };
265 DataStructures::List<AddrAndGuid> failedAttemptList;
266
267 struct DSTAndFac
268 {
269 RakNetGUID destination;
270 SystemAddress facilitator;
271 };
272 DataStructures::Queue<DSTAndFac> queuedOpenNat;
273
274 void IncrementExternalAttemptCount(SLNet::Time time, SLNet::Time delta);
275 unsigned short portStride;
276 enum
277 {
278 HAS_PORT_STRIDE,
279 UNKNOWN_PORT_STRIDE,
280 CALCULATING_PORT_STRIDE,
281 INCAPABLE_PORT_STRIDE
282 } hasPortStride;
283 SLNet::Time portStrideCalTimeout;
284
285 /*
286 struct TimeAndGuid
287 {
288 SLNet::Time time;
289 RakNetGUID guid;
290 };
291 DataStructures::List<TimeAndGuid> groupRequestsInProgress;
292
293 struct GroupPunchRequest
294 {
295 SystemAddress facilitator;
296 DataStructures::List<RakNetGUID> pendingList;
297 DataStructures::List<RakNetGUID> passedListGuid;
298 DataStructures::List<SystemAddress> passedListAddress;
299 DataStructures::List<RakNetGUID> failedList;
300 DataStructures::List<RakNetGUID> ignoredList;
301 };
302 DataStructures::List<GroupPunchRequest*> groupPunchRequests;
303 void UpdateGroupPunchOnNatResult(SystemAddress facilitator, RakNetGUID targetSystem, SystemAddress targetSystemAddress, int result); // 0=failed, 1=success, 2=ignore
304 */
305 };
306
307 } // namespace SLNet
308
309 #endif
310
311 #endif // _RAKNET_SUPPORT_*