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
NatTypeDetectionCommon.cpp
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
16 #include "slikenet/NatTypeDetectionCommon.h"
17
18 #if _RAKNET_SUPPORT_NatTypeDetectionServer==1 || _RAKNET_SUPPORT_NatTypeDetectionClient==1
19
20 #include "slikenet/SocketLayer.h"
21 #include "slikenet/SocketIncludes.h"
22 #include "slikenet/SocketDefines.h"
23
24 using namespace SLNet;
25
26 bool SLNet::CanConnect(NATTypeDetectionResult type1, NATTypeDetectionResult type2)
27 {
30 bool connectionGraph[NAT_TYPE_COUNT][NAT_TYPE_COUNT] =
31 {
32 // None, Full Cone, Address Restricted, Port Restricted, Symmetric, Unknown, InProgress, Supports_UPNP
33 {true, true, true, true, true, false, false, true}, // None
34 {true, true, true, true, true, false, false, true}, // Full Cone
35 {true, true, true, true, true, false, false, true}, // Address restricted
36 {true, true, true, true, false, false, false, true}, // Port restricted
37 {true, true, true, false, false, false, false, true}, // Symmetric
38 {false, false, false, false, false, false, false, false}, // Unknown
39 {false, false, false, false, false, false, false, false}, // InProgress
40 {true, true, true, true, true, false, false, true} // Supports_UPNP
41 };
42
43 return connectionGraph[(int) type1][(int) type2];
44 }
45
46 const char *SLNet::NATTypeDetectionResultToString(NATTypeDetectionResult type)
47 {
48 switch (type)
49 {
50 case NAT_TYPE_NONE:
51 return "None";
52 case NAT_TYPE_FULL_CONE:
53 return "Full cone";
54 case NAT_TYPE_ADDRESS_RESTRICTED:
55 return "Address restricted";
56 case NAT_TYPE_PORT_RESTRICTED:
57 return "Port restricted";
58 case NAT_TYPE_SYMMETRIC:
59 return "Symmetric";
60 case NAT_TYPE_UNKNOWN:
61 return "Unknown";
62 case NAT_TYPE_DETECTION_IN_PROGRESS:
63 return "In Progress";
64 case NAT_TYPE_SUPPORTS_UPNP:
65 return "Supports UPNP";
66 case NAT_TYPE_COUNT:
67 return "NAT_TYPE_COUNT";
68 }
69 return "Error, unknown enum in NATTypeDetectionResult";
70 }
71
72 // None and relaxed can connect to anything
73 // Moderate can connect to moderate or less
74 // Strict can connect to relaxed or less
75 const char *SLNet::NATTypeDetectionResultToStringFriendly(NATTypeDetectionResult type)
76 {
77 switch (type)
78 {
79 case NAT_TYPE_NONE:
80 return "Open";
81 case NAT_TYPE_FULL_CONE:
82 return "Relaxed";
83 case NAT_TYPE_ADDRESS_RESTRICTED:
84 return "Relaxed";
85 case NAT_TYPE_PORT_RESTRICTED:
86 return "Moderate";
87 case NAT_TYPE_SYMMETRIC:
88 return "Strict";
89 case NAT_TYPE_UNKNOWN:
90 return "Unknown";
91 case NAT_TYPE_DETECTION_IN_PROGRESS:
92 return "In Progress";
93 case NAT_TYPE_SUPPORTS_UPNP:
94 return "Supports UPNP";
95 case NAT_TYPE_COUNT:
96 return "NAT_TYPE_COUNT";
97 }
98 return "Error, unknown enum in NATTypeDetectionResult";
99 }
100
101
102 RakNetSocket2* SLNet::CreateNonblockingBoundSocket(const char *bindAddr
103 #ifdef __native_client__
104 ,_PP_Instance_ chromeInstance
105 #endif
106 , RNS2EventHandler *eventHandler
107 )
108 {
109 RakNetSocket2 *r2 = RakNetSocket2Allocator::AllocRNS2();
110 #if defined(__native_client__)
111 NativeClientBindParameters ncbp;
112 RNS2_NativeClient * nativeClientSocket = (RNS2_NativeClient*) r2;
113 ncbp.eventHandler=eventHandler;
114 ncbp.forceHostAddress=(char*) bindAddr;
115 ncbp.is_ipv6=false;
116 ncbp.nativeClientInstance=chromeInstance;
117 ncbp.port=0;
118 nativeClientSocket->Bind(&ncbp, _FILE_AND_LINE_);
119 #elif defined(WINDOWS_STORE_RT)
120 RakAssert("TODO" && 0);
121 #else
122 if (r2->IsBerkleySocket())
123 {
124 RNS2_BerkleyBindParameters bbp;
125 bbp.port=0;
126 bbp.hostAddress=(char*)bindAddr;
127 bbp.addressFamily=AF_INET;
128 bbp.type=SOCK_DGRAM;
129 bbp.protocol=0;
130 bbp.nonBlockingSocket=true;
131 bbp.setBroadcast=true;
132 bbp.setIPHdrIncl=false;
133 bbp.doNotFragment=false;
134 bbp.pollingThreadPriority=0;
135 bbp.eventHandler=eventHandler;
136 bbp.remotePortRakNetWasStartedOn_PS3_PS4_PSP2=0;
137 RNS2BindResult br = ((RNS2_Berkley*) r2)->Bind(&bbp, _FILE_AND_LINE_);
138
139 if (br==BR_FAILED_TO_BIND_SOCKET)
140 {
141 RakNetSocket2Allocator::DeallocRNS2(r2);
142 return 0;
143 }
144 else if (br==BR_FAILED_SEND_TEST)
145 {
146 RakNetSocket2Allocator::DeallocRNS2(r2);
147 return 0;
148 }
149 else
150 {
151 RakAssert(br==BR_SUCCESS);
152 }
153
154 ((RNS2_Berkley*) r2)->CreateRecvPollingThread(0);
155 }
156 else
157 {
158 RakAssert("TODO" && 0);
159 }
160 #endif
161
162 return r2;
163
164 /*
165 #ifdef __native_client__
166 RakNetSocket2 *s = SocketLayer::CreateBoundSocket( 0, 0, false, bindAddr, true, 0, AF_INET, chromeInstance );
167 #else
168 RakNetSocket2 *s = SocketLayer::CreateBoundSocket( 0, 0, false, bindAddr, true, 0, AF_INET, 0 );
169 #endif
170
171 #ifdef _WIN32
172 unsigned long nonblocking = 1;
173 s->IOCTLSocket( FIONBIO, &nonblocking );
174 #elif defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3) || defined(_PS4) || defined(SN_TARGET_PSP2)
175 int sock_opt=1;
176 s->SetSockOpt(SOL_SOCKET, SO_NBIO, ( char * ) & sock_opt, sizeof ( sock_opt ) );
177 #elif defined(__native_client__)
178 // Nop
179 #else
180 s->Fcntl( F_SETFL, O_NONBLOCK );
181 #endif
182 return s;
183 */
184 }
185
186 /*
187 int SLNet::NatTypeRecvFrom(char *data, RakNetSocket2* socket, SystemAddress &sender, RNS2EventHandler *eventHandler)
188 {
189 #if defined(__native_client__)
190 RakAssert("TODO" && 0);
191 #elif defined(WINDOWS_STORE_RT)
192 RakAssert("TODO" && 0);
193 #else
194 if (socket->IsBerkleySocket())
195 {
196 RNS2RecvStruct *recvFromStruct;
197 recvFromStruct=AllocRNS2RecvStruct(_FILE_AND_LINE_);
198 if (recvFromStruct != NULL)
199 {
200 recvFromStruct->socket=this;
201 socket->RecvFromBlocking(recvFromStruct);
202 }
203 if (recvFromStruct->bytesRead>0)
204 {
205 sender = recvFromStruct->systemAddress;
206 }
207 return recvFromStruct->bytesRead;
208 }
209 return 0;
210 #endif
211 }
212 */
213
214 #endif // #if _RAKNET_SUPPORT_NatTypeDetectionServer==1 || _RAKNET_SUPPORT_NatTypeDetectionClient==1