1 /// \file
2 ///
3 /// This file is part of RakNet Copyright 2003 Jenkins Software LLC
4 ///
5 /// Raknet is available under the terms of the GPLv3 license, see /usr/local/share/licenses/raknet-3.9.2_10,1/GPLv3.
6 
7 
8 #include "RakNetTypes.h"
9 #include "RakAssert.h"
10 #include <string.h>
11 #include <stdio.h>
12 
13 
14 #if   defined(_WIN32)
15 // IP_DONTFRAGMENT is different between winsock 1 and winsock 2.  Therefore, Winsock2.h must be linked againt Ws2_32.lib
16 // winsock.h must be linked against WSock32.lib.  If these two are mixed up the flag won't work correctly
17 #include <winsock2.h>
18 #else
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #endif
23 
24 #include <string.h> // strncasecmp
25 #include "Itoa.h"
26 #include "SocketLayer.h"
27 #include <stdlib.h>
28 
NonNumericHostString(const char * host)29 bool NonNumericHostString( const char *host )
30 {
31 	if ( host[ 0 ] >= '0' && host[ 0 ] <= '9' )
32 		return false;
33 
34 	if ( (host[ 0 ] == '-') && ( host[ 1 ] >= '0' && host[ 1 ] <= '9' ) )
35 		return false;
36 
37 	return true;
38 }
39 
SocketDescriptor()40 SocketDescriptor::SocketDescriptor() {port=0; hostAddress[0]=0; remotePortRakNetWasStartedOn_PS3=0;}
SocketDescriptor(unsigned short _port,const char * _hostAddress)41 SocketDescriptor::SocketDescriptor(unsigned short _port, const char *_hostAddress)
42 {
43 	remotePortRakNetWasStartedOn_PS3=0;
44 	port=_port;
45 	if (_hostAddress)
46 		strcpy(hostAddress, _hostAddress);
47 	else
48 		hostAddress[0]=0;
49 }
50 
51 // Defaults to not in peer to peer mode for NetworkIDs.  This only sends the localSystemAddress portion in the BitStream class
52 // This is what you want for client/server, where the server assigns all NetworkIDs and it is unnecessary to transmit the full structure.
53 // For peer to peer, this will transmit the systemAddress of the system that created the object in addition to localSystemAddress.  This allows
54 // Any system to create unique ids locally.
55 // All systems must use the same value for this variable.
56 //bool RAK_DLL_EXPORT NetworkID::peerToPeerMode=false;
57 
operator ==(const SystemAddress & right) const58 bool SystemAddress::operator==( const SystemAddress& right ) const
59 {
60 	return binaryAddress == right.binaryAddress && port == right.port;
61 }
62 
operator !=(const SystemAddress & right) const63 bool SystemAddress::operator!=( const SystemAddress& right ) const
64 {
65 	return binaryAddress != right.binaryAddress || port != right.port;
66 }
67 
operator >(const SystemAddress & right) const68 bool SystemAddress::operator>( const SystemAddress& right ) const
69 {
70 	return ( ( binaryAddress > right.binaryAddress ) || ( ( binaryAddress == right.binaryAddress ) && ( port > right.port ) ) );
71 }
72 
operator <(const SystemAddress & right) const73 bool SystemAddress::operator<( const SystemAddress& right ) const
74 {
75 	return ( ( binaryAddress < right.binaryAddress ) || ( ( binaryAddress == right.binaryAddress ) && ( port < right.port ) ) );
76 }
ToString(bool writePort) const77 const char *SystemAddress::ToString(bool writePort) const
78 {
79 	static unsigned char strIndex=0;
80 	static char str[8][30];
81 
82 	unsigned char lastStrIndex=strIndex;
83 	strIndex++;
84 	ToString(writePort, str[lastStrIndex&7]);
85 	return (char*) str[lastStrIndex&7];
86 }
ToString(bool writePort,char * dest) const87 void SystemAddress::ToString(bool writePort, char *dest) const
88 {
89 	if (*this==UNASSIGNED_SYSTEM_ADDRESS)
90 	{
91 		strcpy(dest, "UNASSIGNED_SYSTEM_ADDRESS");
92 		return;
93 	}
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 	in_addr in;
107 	in.s_addr = binaryAddress;
108 //	cellSysmoduleLoadModule(CELL_SYSMODULE_NETCTL);
109 //	sys_net_initialize_network();
110 	const char *ntoaStr = inet_ntoa( in );
111 	strcpy(dest, ntoaStr);
112 	if (writePort)
113 	{
114 		strcat(dest, ":");
115 		Itoa(port, dest+strlen(dest), 10);
116 	}
117 
118 }
SystemAddress()119 SystemAddress::SystemAddress() {*this=UNASSIGNED_SYSTEM_ADDRESS; systemIndex=(SystemIndex)-1;}
SystemAddress(const char * a,unsigned short b)120 SystemAddress::SystemAddress(const char *a, unsigned short b) {SetBinaryAddress(a); port=b; systemIndex=(SystemIndex)-1;};
SystemAddress(unsigned int a,unsigned short b)121 SystemAddress::SystemAddress(unsigned int a, unsigned short b) {binaryAddress=a; port=b; systemIndex=(SystemIndex)-1;};
122 #ifdef _MSC_VER
123 #pragma warning( disable : 4996 )  // The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _strnicmp. See online help for details.
124 #endif
SetBinaryAddress(const char * str)125 void SystemAddress::SetBinaryAddress(const char *str)
126 {
127 	if ( NonNumericHostString( str ) )
128 	{
129 
130 	#if defined(_WIN32)
131 		if (_strnicmp(str,"localhost", 9)==0)
132 	#else
133 		if (strncasecmp(str,"localhost", 9)==0)
134 	#endif
135 		{
136 			binaryAddress=inet_addr("127.0.0.1");
137 			if (str[9])
138 				port=(unsigned short) atoi(str+9);
139 			return;
140 		}
141 
142 		const char *ip = ( char* ) SocketLayer::DomainNameToIP( str );
143 		if (ip)
144 		{
145 			binaryAddress=inet_addr(ip);
146 		}
147 	}
148 	else
149 	{
150 		//#ifdef _XBOX
151 		//	binaryAddress=UNASSIGNED_SYSTEM_ADDRESS.binaryAddress;
152 		//#else
153 		// Split the string into the first part, and the : part
154 		int index, portIndex;
155 		char IPPart[22];
156 		char portPart[10];
157 		// Only write the valid parts, don't change existing if invalid
158 		//	binaryAddress=UNASSIGNED_SYSTEM_ADDRESS.binaryAddress;
159 		//	port=UNASSIGNED_SYSTEM_ADDRESS.port;
160 		for (index=0; str[index] && str[index]!=':' && index<22; index++)
161 		{
162 			IPPart[index]=str[index];
163 		}
164 		IPPart[index]=0;
165 		portPart[0]=0;
166 		if (str[index] && str[index+1])
167 		{
168 			index++;
169 			for (portIndex=0; portIndex<10 && str[index] && index < 22+10; index++, portIndex++)
170 				portPart[portIndex]=str[index];
171 			portPart[portIndex]=0;
172 		}
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 		if (IPPart[0])
187 			binaryAddress=inet_addr(IPPart);
188 
189 
190 
191 		if (portPart[0])
192 			port=(unsigned short) atoi(portPart);
193 		//#endif
194 	}
195 
196 }
197 
operator =(const NetworkID & input)198 NetworkID& NetworkID::operator = ( const NetworkID& input )
199 {
200 #if NETWORK_ID_SUPPORTS_PEER_TO_PEER==1
201 	systemAddress = input.systemAddress;
202 	guid = input.guid;
203 #endif
204 	localSystemAddress = input.localSystemAddress;
205 	return *this;
206 }
207 
operator ==(const NetworkID & right) const208 bool NetworkID::operator==( const NetworkID& right ) const
209 {
210 #if NETWORK_ID_SUPPORTS_PEER_TO_PEER==1
211 //	if (NetworkID::peerToPeerMode)
212 	{
213 		if (guid!=UNASSIGNED_RAKNET_GUID)
214 			return guid == right.guid && localSystemAddress == right.localSystemAddress;
215 		else
216 			return systemAddress == right.systemAddress && localSystemAddress == right.localSystemAddress;
217 	}
218 #else
219 //	else
220 		return localSystemAddress==right.localSystemAddress;
221 #endif
222 }
223 
operator !=(const NetworkID & right) const224 bool NetworkID::operator!=( const NetworkID& right ) const
225 {
226 #if NETWORK_ID_SUPPORTS_PEER_TO_PEER==1
227 //	if (NetworkID::peerToPeerMode)
228 	{
229 		if (guid!=UNASSIGNED_RAKNET_GUID)
230 			return guid != right.guid || localSystemAddress != right.localSystemAddress;
231 		else
232 			return systemAddress != right.systemAddress || localSystemAddress != right.localSystemAddress;
233 	}
234 #else
235 //	else
236 		return localSystemAddress!=right.localSystemAddress;
237 #endif
238 }
239 
operator >(const NetworkID & right) const240 bool NetworkID::operator>( const NetworkID& right ) const
241 {
242 #if NETWORK_ID_SUPPORTS_PEER_TO_PEER==1
243 //	if (NetworkID::peerToPeerMode)
244 	{
245 		if (guid!=UNASSIGNED_RAKNET_GUID)
246 			return ( ( guid > right.guid ) || ( ( guid == right.guid ) && ( localSystemAddress > right.localSystemAddress ) ) );
247 		else
248 			return ( ( systemAddress > right.systemAddress ) || ( ( systemAddress == right.systemAddress ) && ( localSystemAddress > right.localSystemAddress ) ) );
249 	}
250 #else
251 //	else
252 		return localSystemAddress>right.localSystemAddress;
253 #endif
254 }
255 
operator <(const NetworkID & right) const256 bool NetworkID::operator<( const NetworkID& right ) const
257 {
258 #if NETWORK_ID_SUPPORTS_PEER_TO_PEER==1
259 //	if (NetworkID::peerToPeerMode)
260 	{
261 		if (guid!=UNASSIGNED_RAKNET_GUID)
262 			return ( ( guid < right.guid ) || ( ( guid == right.guid ) && ( localSystemAddress < right.localSystemAddress ) ) );
263 		else
264 			return ( ( systemAddress < right.systemAddress ) || ( ( systemAddress == right.systemAddress ) && ( localSystemAddress < right.localSystemAddress ) ) );
265 	}
266 #else
267 	//else
268 		return localSystemAddress<right.localSystemAddress;
269 #endif
270 }
IsPeerToPeerMode(void)271 bool NetworkID::IsPeerToPeerMode(void)
272 {
273 #if NETWORK_ID_SUPPORTS_PEER_TO_PEER==1
274 	return true;
275 #else
276 	return false;
277 #endif
278 //	return peerToPeerMode;
279 }
SetPeerToPeerMode(bool isPeerToPeer)280 void NetworkID::SetPeerToPeerMode(bool isPeerToPeer)
281 {
282 	(void) isPeerToPeer;
283 
284 	// deprecated, define NETWORK_ID_SUPPORTS_PEER_TO_PEER instead for true, comment out for false
285 	// This is in RakNetDefines.h
286 	RakAssert(0);
287 
288 //	peerToPeerMode=isPeerToPeer;
289 }
operator ==(const RakNetGUID & right) const290 bool RakNetGUID::operator==( const RakNetGUID& right ) const
291 {
292 	return g==right.g;
293 }
operator !=(const RakNetGUID & right) const294 bool RakNetGUID::operator!=( const RakNetGUID& right ) const
295 {
296 	return g!=right.g;
297 }
operator >(const RakNetGUID & right) const298 bool RakNetGUID::operator > ( const RakNetGUID& right ) const
299 {
300 	return g > right.g;
301 }
operator <(const RakNetGUID & right) const302 bool RakNetGUID::operator < ( const RakNetGUID& right ) const
303 {
304 	return g < right.g;
305 }
ToString(void) const306 const char *RakNetGUID::ToString(void) const
307 {
308 	static unsigned char strIndex=0;
309 	static char str[8][22];
310 
311 	unsigned char lastStrIndex=strIndex;
312 	strIndex++;
313 	ToString(str[lastStrIndex&7]);
314 	return (char*) str[lastStrIndex&7];
315 }
ToString(char * dest) const316 void RakNetGUID::ToString(char *dest) const
317 {
318 	if (*this==UNASSIGNED_RAKNET_GUID)
319 		strcpy(dest, "UNASSIGNED_RAKNET_GUID");
320 
321 	//sprintf(dest, "%u.%u.%u.%u.%u.%u", g[0], g[1], g[2], g[3], g[4], g[5]);
322 	sprintf(dest, "%" PRINTF_64_BIT_MODIFIER "u", (long long unsigned int) g);
323 	// sprintf(dest, "%u.%u.%u.%u.%u.%u", g[0], g[1], g[2], g[3], g[4], g[5]);
324 }
FromString(const char * source)325 bool RakNetGUID::FromString(const char *source)
326 {
327 	if (source==0)
328 		return false;
329 
330 
331 
332 #if   defined(WIN32)
333 	g=_atoi64(source);
334 
335 
336 #else
337 	// Changed from g=strtoull(source,0,10); for android
338 	g=strtoull(source, (char **)NULL, 10);
339 #endif
340 	return true;
341 
342 }
343