21 #define CAT_NEUTER_EXPORT  
   64 #ifdef USE_THREADED_SEND 
   69 #define CAT_AUDIT_PRINTF(...) printf(__VA_ARGS__) 
   71 #define CAT_AUDIT_PRINTF(...) 
   80 #define REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE 8 
   82 #if !defined ( __APPLE__ ) && !defined ( __APPLE_CC__ ) 
  118 using namespace SLNet;
 
  135 static const unsigned char OFFLINE_MESSAGE_DATA_ID[16]={0x00,0xFF,0xFF,0x00,0xFE,0xFE,0xFE,0xFE,0xFD,0xFD,0xFD,0xFD,0x12,0x34,0x56,0x78};
 
  137 struct PacketFollowedByData
 
  140     unsigned char data[1];
 
  159     p = 
new ((
void*)p) 
Packet;
 
  176     p = 
new ((
void*)p) 
Packet;
 
  194 #if LIBCAT_SECURITY==1 
  196     CAT_AUDIT_PRINTF(
"AUDIT: Initializing RakPeer security flags: using_security = false, server_handshake = null, cookie_jar = null\n");
 
  197     _using_security = 
false;
 
  198     _server_handshake = 0;
 
  207     trackFrequencyTable = 
false;
 
  208     maximumIncomingConnections = 0;
 
  209     maximumNumberOfPeers = 0;
 
  211     remoteSystemList = 0;
 
  212     activeSystemList = 0;
 
  213     activeSystemListSize=0;
 
  214     remoteSystemLookup=0;
 
  215     bytesSentPerSecond = bytesReceivedPerSecond = 0;
 
  217     isMainLoopThreadActive = 
false;
 
  218     incomingDatagramEventHandler=0;
 
  225 #if defined(GET_TIME_SPIKE_LIMIT) && GET_TIME_SPIKE_LIMIT>0 
  226     occasionalPing = 
true;
 
  228     occasionalPing = 
false;
 
  230     allowInternalRouting=
false;
 
  233     allowConnectionResponseIPMigration = 
false;
 
  235     incomingPasswordLength=0;
 
  236     splitMessageProgressInterval=0;
 
  238     unreliableTimeout=1000;
 
  242     userUpdateThreadPtr=0;
 
  243     userUpdateThreadData=0;
 
  247     defaultTimeoutTime=30000;
 
  249     defaultTimeoutTime=10000;
 
  255     _extraPingVariance=0;
 
  261     packetAllocationPoolMutex.Lock();
 
  263     packetAllocationPoolMutex.Unlock();
 
  311     quitAndDataEvents.InitEvent();
 
  312     limitConnectionFrequencyFromTheSameIP=
false;
 
  332 #if LIBCAT_SECURITY==1 
  334     CAT_AUDIT_PRINTF(
"AUDIT: Deleting RakPeer security objects, handshake = %x, cookie jar = %x\n", _server_handshake, _cookie_jar);
 
  385     if (threadPriority==-99999)
 
  411     RakAssert(socketDescriptors && socketDescriptorCount>=1);
 
  413     if (socketDescriptors==0 || socketDescriptorCount<1)
 
  421     if ( maxConnections <= 0 )
 
  429     for (i=0; i<socketDescriptorCount; i++)
 
  476         #if defined(__native_client__) 
  477         NativeClientBindParameters ncbp;
 
  478         RNS2_NativeClient * nativeClientSocket = (RNS2_NativeClient*) r2;
 
  480         ncbp.forceHostAddress=(
char*) socketDescriptors[i].hostAddress;
 
  481         ncbp.is_ipv6=socketDescriptors[i].
socketFamily==AF_INET6;
 
  483         ncbp.port=socketDescriptors[i].
port;
 
  485         #elif defined(WINDOWS_STORE_RT) 
  487         ((RNS2_WindowsStore8*) r2)->SetRecvEventHandler(
this);
 
  488         br = ((RNS2_WindowsStore8*) r2)->Bind(ref 
new Platform::String());
 
  500             bbp.
hostAddress=(
char*) socketDescriptors[i].hostAddress;
 
  515                 socketDescriptors[i].socketFamily!=AF_INET ||
 
  576 #if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) 
  577     for (i=0; i<socketDescriptorCount; i++)
 
  589 #if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) 
  641         for (i=0; i < (
unsigned int) maximumNumberOfPeers*REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE; i++)
 
  643             remoteSystemLookup[i]=0;
 
  663 #if RAKPEER_USER_THREADED!=1 
  676                     if ( errorCode != 0 )
 
  682 #endif // RAKPEER_USER_THREADED!=1 
  721 #if RAKPEER_USER_THREADED!=1 
  725 #endif // RAKPEER_USER_THREADED!=1 
  738 #ifdef USE_THREADED_SEND 
  739     SLNet::SendToThread::AddRef();
 
  764 #if LIBCAT_SECURITY==1 
  769     _require_client_public_key = bRequireClientKey;
 
  771     if (_server_handshake)
 
  773         CAT_AUDIT_PRINTF(
"AUDIT: Deleting old server_handshake %x\n", _server_handshake);
 
  782     _server_handshake = SLNet::OP_NEW<cat::ServerEasyHandshake>(
_FILE_AND_LINE_);
 
  785     CAT_AUDIT_PRINTF(
"AUDIT: Created new server_handshake %x\n", _server_handshake);
 
  789     if (_server_handshake->Initialize(public_key, private_key))
 
  791         CAT_AUDIT_PRINTF(
"AUDIT: Successfully initialized, filling cookie jar with goodies, storing public key and setting using security flag to true\n");
 
  793         _server_handshake->FillCookieJar(_cookie_jar);
 
  795         memcpy(my_public_key, public_key, 
sizeof(my_public_key));
 
  797         _using_security = 
true;
 
  801     CAT_AUDIT_PRINTF(
"AUDIT: Failure to initialize so deleting server handshake and cookie jar; also setting using_security flag = false\n");
 
  807     _using_security = 
false;
 
  812     (void) bRequireClientKey;
 
  825 #if LIBCAT_SECURITY==1 
  826     CAT_AUDIT_PRINTF(
"AUDIT: DisableSecurity() called, so deleting _server_handshake %x and cookie_jar %x\n", _server_handshake, _cookie_jar);
 
  832     _using_security = 
false;
 
  925     return (
unsigned short) addresses.
Size();
 
  943     if (passwordDataLength > 255)
 
  944         passwordDataLength=255;
 
  947         passwordDataLength=0;
 
  951     if (passwordDataLength>0)
 
  968     if (*passwordDataLength>0)
 
 1000     if (passwordDataLength>255)
 
 1001         passwordDataLength=255;
 
 1003     if (passwordData==0)
 
 1004         passwordDataLength=0;
 
 1017     return SendConnectionRequest( host, remotePort, passwordData, passwordDataLength, publicKey, connectionSocketIndex, 0, sendConnectionAttemptCount, timeBetweenSendConnectionAttemptsMS, timeoutTime);
 
 1024     if ( host == 0 || 
endThreads || socket == 0 )
 
 1027     if (passwordDataLength>255)
 
 1028         passwordDataLength=255;
 
 1030     if (passwordData==0)
 
 1031         passwordDataLength=0;
 
 1033     return SendConnectionRequest( host, remotePort, passwordData, passwordDataLength, publicKey, 0, 0, sendConnectionAttemptCount, timeBetweenSendConnectionAttemptsMS, timeoutTime, socket );
 
 1051     if ( blockDuration > 0 )
 
 1053         for ( i = 0; i < systemListSize; i++ )
 
 1061         startWaitingTime = time;
 
 1062         while ( time - startWaitingTime < blockDuration )
 
 1065             for (j=0; j < systemListSize; j++)
 
 1076             if ( anyActive==
false )
 
 1100 #if RAKPEER_USER_THREADED!=1 
 1102 #if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) 
 1143 #if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) 
 1154 #endif // RAKPEER_USER_THREADED!=1 
 1159     for ( i = 0; i < systemListSize; i++ )
 
 1216 #ifdef USE_THREADED_SEND 
 1217     SLNet::SendToThread::Deref();
 
 1243     if (numberOfSystems==0)
 
 1248         if (numberOfSystems)
 
 1259         for (i=0; i < *numberOfSystems && i < addresses.
Size(); i++)
 
 1260             remoteSystems[i]=addresses[i];
 
 1265         *numberOfSystems=(
unsigned short) addresses.
Size();
 
 1317     if ( data == 0 || length < 0 )
 
 1323     if ( broadcast == 
false && systemIdentifier.
IsUndefined())
 
 1327     if (forceReceiptNumber!=0)
 
 1328         usedSendReceipt=forceReceiptNumber;
 
 1346         return usedSendReceipt;
 
 1351     return usedSendReceipt;
 
 1356     if ( data == 0 || length < 0 )
 
 1360     memcpy(packet->
data, data, length);
 
 1382     if ( broadcast == 
false && systemIdentifier.
IsUndefined() )
 
 1386     if (forceReceiptNumber!=0)
 
 1387         usedSendReceipt=forceReceiptNumber;
 
 1403         return usedSendReceipt;
 
 1411     return usedSendReceipt;
 
 1440     if ( data == 0 || lengths == 0 )
 
 1446     if (numParameters==0)
 
 1452     if ( broadcast == 
false && systemIdentifier.
IsUndefined() )
 
 1456     if (forceReceiptNumber!=0)
 
 1457         usedSendReceipt=forceReceiptNumber;
 
 1463     return usedSendReceipt;
 
 1549             offset = 
sizeof(
unsigned char);
 
 1662     CloseConnectionInternal(target, sendDisconnectionNotification, 
false, orderingChannel, disconnectionNotificationPriority);
 
 1694 #if LIBCAT_SECURITY==1 
 1858     if ( IP == 0 || IP[ 0 ] == 0 || strlen( IP ) > 15 )
 
 1868         if ( strcmp( IP, 
banList[ index ]->IP ) == 0 )
 
 1871             if (milliseconds==0)
 
 1874                 banList[ index ]->timeout=time+milliseconds;
 
 1884     if (milliseconds==0)
 
 1887         banStruct->
timeout=time+milliseconds;
 
 1888     strcpy_s( banStruct->
IP, 16, IP );
 
 1907     if ( IP == 0 || IP[ 0 ] == 0 || strlen( IP ) > 15 )
 
 1917         if ( strcmp( IP, 
banList[ index ]->IP ) == 0 )
 
 1975     unsigned banListIndex, characterIndex;
 
 1979     if ( IP == 0 || IP[ 0 ] == 0 || strlen( IP ) > 15 )
 
 1993         if (
banList[ banListIndex ]->timeout>0 && 
banList[ banListIndex ]->timeout<time)
 
 1996             temp = 
banList[ banListIndex ];
 
 2008                 if ( 
banList[ banListIndex ]->IP[ characterIndex ] == IP[ characterIndex ] )
 
 2012                     if ( IP[ characterIndex ] == 0 )
 
 2025                     if ( 
banList[ banListIndex ]->IP[ characterIndex ] == 0 || IP[ characterIndex ] == 0 )
 
 2032                     if ( 
banList[ banListIndex ]->IP[ characterIndex ] == 
'*' )
 
 2079 bool RakPeer::Ping( 
const char* host, 
unsigned short remotePort, 
bool onlyReplyOnAcceptingConnections, 
unsigned connectionSocketIndex )
 
 2091     if ( onlyReplyOnAcceptingConnections )
 
 2117     bsp.
data = (
char*) bitStream.GetData() ;
 
 2118     bsp.
length = bitStream.GetNumberOfBytesUsed();
 
 2143     if ( remoteSystem == 0 )
 
 2155         return sum / quantity;
 
 2171     if ( remoteSystem == 0 )
 
 2193     if ( remoteSystem == 0 )
 
 2220     if (remoteSystem == 0)
 
 2229     int counter, lowestPingSoFar;
 
 2232     lowestPingSoFar = 65535;
 
 2234     clockDifferential = 0;
 
 2248     return clockDifferential;
 
 2265     if ( data && length > 0 )
 
 2301         if (remoteSystem==0)
 
 2328     ipList[index]=systemAddress;
 
 2362     return inactiveExternalId;
 
 2376     if (sockets.
Size()>0)
 
 2377         return sockets[socketIndex]->GetBoundAddress();
 
 2411         return (
unsigned int) -1;
 
 2414         return (
unsigned int) -1;
 
 2431     return (
unsigned int) -1;
 
 2466 #if LIBCAT_SECURITY == 1 
 2470     char *copy_source = 0;
 
 2491         for (
int ii = 0; ii < cat::EasyHandshake::PUBLIC_KEY_BYTES; ++ii)
 
 2493             if (copy_source[ii] != 0)
 
 2495                 memcpy(client_public_key, copy_source, cat::EasyHandshake::PUBLIC_KEY_BYTES);
 
 2503     (void) client_public_key;
 
 2533         if ( remoteSystem != 0 )
 
 2550         if ( remoteSystem != 0 )
 
 2606     static char str[128];
 
 2622     if (ip==0 || ip[0]==0)
 
 2626     if (strcmp(ip, 
"127.0.0.1")==0 || strcmp(ip, 
"localhost")==0)
 
 2631     for (i=0; i < num; i++)
 
 2667 bool RakPeer::AdvertiseSystem( 
const char *host, 
unsigned short remotePort, 
const char *data, 
int dataLength, 
unsigned connectionSocketIndex )
 
 2715 void RakPeer::SendTTL( 
const char* host, 
unsigned short remotePort, 
int ttl, 
unsigned connectionSocketIndex )
 
 2717 #if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) 
 2722     if (
socketList[realIndex]->IsBerkleySocket())
 
 2725         bsp.
data = (
char*) fakeData;
 
 2747     if (isNotThreadsafe)
 
 2780     if (isNotThreadsafe)
 
 2934             remoteSystemList[i].reliabilityLayer.ApplyNetworkSimulator(packetloss, minExtraPing, extraPingVariance);
 
 2937     _packetloss=packetloss;
 
 2938     _minExtraPing=minExtraPing;
 
 2939     _extraPingVariance=extraPingVariance;
 
 2956     return _packetloss>0 || _minExtraPing>0 || _extraPingVariance>0;
 
 2986     if (host==0  || host[0]==0)
 
 3004         bitStream.
Write(data, dataLength);
 
 3040         systemStats=&staticStatistics;
 
 3046         bool firstWrite=
false;
 
 3055                 if (firstWrite==
false)
 
 3061                     (*systemStats)+=rnsTemp;
 
 3133     if (calledFromNetworkThread)
 
 3176 #if LIBCAT_SECURITY==1 
 3177 bool RakPeer::GenerateConnectionRequestChallenge(RequestedConnectionStruct *rcs,
PublicKey *publicKey)
 
 3181     rcs->client_handshake = 0;
 
 3184     if (!publicKey) 
return true;
 
 3193         CAT_OBJCLR(rcs->remote_public_key);
 
 3194         rcs->client_handshake = SLNet::OP_NEW<cat::ClientEasyHandshake>(
_FILE_AND_LINE_);
 
 3206         rcs->client_handshake = SLNet::OP_NEW<cat::ClientEasyHandshake>(
_FILE_AND_LINE_);
 
 3207         memcpy(rcs->remote_public_key, publicKey->
remoteServerPublicKey, cat::EasyHandshake::PUBLIC_KEY_BYTES);
 
 3211             !rcs->client_handshake->GenerateChallenge(rcs->handshakeChallenge))
 
 3213             CAT_AUDIT_PRINTF(
"AUDIT: Failure initializing new client_handshake object with identity for this RequestedConnectionStruct\n");
 
 3215             rcs->client_handshake=0;
 
 3219         CAT_AUDIT_PRINTF(
"AUDIT: Success initializing new client handshake object with identity for this RequestedConnectionStruct -- pre-generated challenge\n");
 
 3228         rcs->client_handshake = SLNet::OP_NEW<cat::ClientEasyHandshake>(
_FILE_AND_LINE_);
 
 3229         memcpy(rcs->remote_public_key, publicKey->
remoteServerPublicKey, cat::EasyHandshake::PUBLIC_KEY_BYTES);
 
 3232             !rcs->client_handshake->GenerateChallenge(rcs->handshakeChallenge))
 
 3234             CAT_AUDIT_PRINTF(
"AUDIT: Failure initializing new client_handshake object for this RequestedConnectionStruct\n");
 
 3236             rcs->client_handshake=0;
 
 3240         CAT_AUDIT_PRINTF(
"AUDIT: Success initializing new client handshake object for this RequestedConnectionStruct -- pre-generated challenge\n");
 
 3279 #if LIBCAT_SECURITY==1 
 3281     if (!GenerateConnectionRequestChallenge(rcs,publicKey))
 
 3306 ConnectionAttemptResult RakPeer::SendConnectionRequest( 
const char* host, 
unsigned short remotePort, 
const char *passwordData, 
int passwordDataLength, 
PublicKey *publicKey, 
unsigned connectionSocketIndex, 
unsigned int extraData, 
unsigned sendConnectionAttemptCount, 
unsigned timeBetweenSendConnectionAttemptsMS, 
SLNet::TimeMS timeoutTime, 
RakNetSocket2* socket )
 
 3334 #if LIBCAT_SECURITY==1 
 3335     if (!GenerateConnectionRequestChallenge(rcs,publicKey))
 
 3380     if (calledFromNetworkThread)
 
 3383         if (index!=(
unsigned int) -1)
 
 3394         int deadConnectionIndex=-1;
 
 3403                 else if (deadConnectionIndex==-1)
 
 3404                     deadConnectionIndex=i;
 
 3408         if (deadConnectionIndex!=-1 && onlyActive==
false)
 
 3438     bs.
Read(incomingTimestamp);
 
 3439     unsigned char doSecurity;
 
 3440     bs.
Read(doSecurity);
 
 3442 #if LIBCAT_SECURITY==1 
 3443     unsigned char doClientKey;
 
 3444     if (_using_security)
 
 3447         if (doSecurity != 1 || !remoteSystem->
reliabilityLayer.GetAuthenticatedEncryption())
 
 3451         unsigned char proof[cat::EasyHandshake::PROOF_BYTES];
 
 3453         if (!remoteSystem->
reliabilityLayer.GetAuthenticatedEncryption()->ValidateProof(proof, 
sizeof(proof)))
 
 3459         CAT_OBJCLR(remoteSystem->client_public_key);
 
 3461         bs.
Read(doClientKey);
 
 3464         if (doClientKey == 1)
 
 3467             unsigned char ident[cat::EasyHandshake::IDENTITY_BYTES];
 
 3471             if (_require_client_public_key)
 
 3474                 if (!_server_handshake->VerifyInitiatorIdentity(remoteSystem->answer, ident, remoteSystem->client_public_key))
 
 3478                     bitStream.
Write((
unsigned char)2); 
 
 3490             if (_require_client_public_key)
 
 3494                 bitStream.
Write((
unsigned char)1); 
 
 3501 #endif // LIBCAT_SECURITY 
 3531     bitStream.
Write(systemIndex);
 
 3534     bitStream.
Write(incomingTimestamp);
 
 3545     if (performImmediate)
 
 3562     unsigned int numberOfIncomingConnections;
 
 3563     numberOfIncomingConnections = 0;
 
 3572             numberOfIncomingConnections++;
 
 3575     return numberOfIncomingConnections;
 
 3582     unsigned i,j,assignedIndex;
 
 3602                     *thisIPConnectedRecently=
true;
 
 3613     *thisIPConnectedRecently=
false;
 
 3623             remoteSystem->
guid=guid;
 
 3626             if (incomingMTU > remoteSystem->
MTUSize)
 
 3627                 remoteSystem->
MTUSize=incomingMTU;
 
 3641                 bindingAddress.
ToString(
true,str,static_cast<size_t>(256));
 
 3644                 unsigned int ipListIndex, foundIndex=(
unsigned int)-1;
 
 3653                         foundIndex=ipListIndex;
 
 3741             if ((
int) indexLoopupCheck!=(
int) assignedIndex)
 
 3743                 RakAssert((
int) indexLoopupCheck==(
int) assignedIndex);
 
 3747             return remoteSystem;
 
 3766     timeBS.
Read(encodedTimestamp);
 
 3770     timeBS.
Write(encodedTimestamp);
 
 3779     if ( remoteSystem == 0 )
 
 3839         rsi->
index=remoteSystemListIndex;
 
 3845         while (cur->
next!=0)
 
 3852         rsi->
index=remoteSystemListIndex;
 
 3906     return (
unsigned int) -1;
 
 3912     if (remoteSystemIndex==(
unsigned int)-1)
 
 3938             activeSystemListSize--;
 
 3942     RakAssert(
"activeSystemList invalid, entry not found in RemoveFromActiveSystemList. Ensure that AddToActiveSystemList and RemoveFromActiveSystemList are called by the same thread." && 0);
 
 4046         return SLNet::OP_NEW<RNS2RecvStruct>(file,line);
 
 4095     if (performImmediate)
 
 4126     if (sendDisconnectionNotification)
 
 4132         if (performImmediate)
 
 4135             if (index!=(
unsigned int) -1)
 
 4167             bcs->
priority=disconnectionNotificationPriority;
 
 4212     unsigned int totalLength=0;
 
 4213     unsigned int lengthOffset;
 
 4215     for (i=0; i < numParameters; i++)
 
 4218             totalLength+=lengths[i];
 
 4223     char *dataAggregate;
 
 4225     if (dataAggregate==0)
 
 4230     for (i=0, lengthOffset=0; i < numParameters; i++)
 
 4234             memcpy(dataAggregate+lengthOffset, data[i], lengths[i]);
 
 4235             lengthOffset+=lengths[i];
 
 4251     bcs->
data = dataAggregate;
 
 4273     unsigned sendListSize;
 
 4274     bool callerDataAllocationUsed;
 
 4275     unsigned int remoteSystemIndex, sendListIndex; 
 
 4277     callerDataAllocationUsed=
false;
 
 4286         remoteSystemIndex=(
unsigned int) -1;
 
 4289     if (broadcast==
false)
 
 4291         if (remoteSystemIndex==(
unsigned int) -1)
 
 4300             sendList=(
unsigned *)alloca(
sizeof(
unsigned));
 
 4310             sendList[0]=remoteSystemIndex;
 
 4326             if (remoteSystemIndex!=(
unsigned int) -1 && idx==remoteSystemIndex)
 
 4330                 sendList[sendListSize++]=idx;
 
 4334     if (sendListSize==0)
 
 4336         #if !defined(USE_ALLOCA) 
 4343     for (sendListIndex=0; sendListIndex < sendListSize; sendListIndex++)
 
 4346         bool useData = useCallerDataAllocation && callerDataAllocationUsed==
false && sendListIndex+1==sendListSize;
 
 4349             callerDataAllocationUsed=
true;
 
 4362 #if !defined(USE_ALLOCA) 
 4367     return callerDataAllocationUsed;
 
 4382     if (time > sendPingTime)
 
 4383         ping = time - sendPingTime;
 
 4395         remoteSystem->
lowestPing = (
unsigned short) ping;
 
 4428     for (i=0; i < freeQueue.
Size(); i++)
 
 4430 #if LIBCAT_SECURITY==1 
 4431         CAT_AUDIT_PRINTF(
"AUDIT: In ClearRequestedConnectionList(), Deleting freeQueue index %i client_handshake %x\n", i, freeQueue[i]->client_handshake);
 
 4446     unsigned char buff6[6];
 
 4488     for (j=0; j < 8; j++)
 
 4495         unsigned int diff4Bits = (
unsigned int) (diff & 15);
 
 4498         ((
char*)&g)[j] ^= diff4Bits;
 
 4504     gettimeofday(&tv, NULL);
 
 4505     return tv.tv_usec + tv.tv_sec * 1000000;
 
 4533     systemAddress.
ToString(
false, str1,static_cast<size_t>(64));
 
 4537             rakPeer->
pluginListNTS[i]->OnDirectSocketReceive(data, length*8, systemAddress);
 
 4569         *isOfflineMessage=
true;
 
 4610         *isOfflineMessage=
false;
 
 4613     if (*isOfflineMessage)
 
 4616             rakPeer->
pluginListNTS[i]->OnDirectSocketReceive(data, length*8, systemAddress);
 
 4628                 inBitStream.
Read(sendPingTime);
 
 4631                 inBitStream.
Read(remoteGuid);
 
 4635                 outBitStream.
Write(sendPingTime);
 
 4656                 packet->
data[0]=data[0];
 
 4658                 packet->
guid=remoteGuid;
 
 4678             bsOut.
Write(pingMS);
 
 4723                 rakPeer->
pluginListNTS[i]->OnDirectSocketReceive(data, length*8, systemAddress);
 
 4729             bsIn.
Read(serverGuid);
 
 4730             unsigned char serverHasSecurity;
 
 4733             bsIn.
Read(serverHasSecurity);
 
 4735             if (serverHasSecurity)
 
 4743             if (serverHasSecurity)
 
 4744                 bsOut.
Write(cookie);
 
 4753                     if (serverHasSecurity)
 
 4755 #if LIBCAT_SECURITY==1 
 4756                         unsigned char public_key[cat::EasyHandshake::PUBLIC_KEY_BYTES];
 
 4761                             memcpy(rcs->remote_public_key, public_key, cat::EasyHandshake::PUBLIC_KEY_BYTES);
 
 4762                             if (!rcs->client_handshake->Initialize(public_key) ||
 
 4763                                 !rcs->client_handshake->GenerateChallenge(rcs->handshakeChallenge))
 
 4765                                 CAT_AUDIT_PRINTF(
"AUDIT: Server passed a bad public key with PKM_ACCEPT_ANY_PUBLIC_KEY");
 
 4771                         if (cat::SecureEqual(public_key,
 
 4772                             rcs->remote_public_key,
 
 4773                             cat::EasyHandshake::PUBLIC_KEY_BYTES)==
false)
 
 4776                             CAT_AUDIT_PRINTF(
"AUDIT: Expected public key does not match what was sent by server -- Reporting back ID_PUBLIC_KEY_MISMATCH to user\n");
 
 4780                             packet->
bitSize = ( 
sizeof( char ) * 8);
 
 4782                             packet->
guid=serverGuid;
 
 4787                         if (rcs->client_handshake==0)
 
 4791                             bsOut.
Write((
unsigned char)0);
 
 4796                             bsOut.
Write((
unsigned char)1);
 
 4799                             bsOut.
WriteAlignedBytes((
const unsigned char*) rcs->handshakeChallenge,cat::EasyHandshake::CHALLENGE_BYTES);
 
 4801 #else // LIBCAT_SECURITY 
 4803                         bsOut.
Write((
unsigned char)0);
 
 4804 #endif // LIBCAT_SECURITY 
 4809 #if LIBCAT_SECURITY==1 
 4810                         if (rcs->client_handshake!=0)
 
 4813                             CAT_AUDIT_PRINTF(
"AUDIT: Security disabled by server but we expected security (indicated by client_handshake not null) so failing!\n");
 
 4817                             packet->
bitSize = ( 
sizeof( char ) * 8);
 
 4819                             packet->
guid=serverGuid;
 
 4823 #endif // LIBCAT_SECURITY 
 4857                 rakPeer->
pluginListNTS[i]->OnDirectSocketReceive(data, length*8, systemAddress);
 
 4865             bool b = bs.
Read(bindingAddress);
 
 4870             bool doSecurity=
false;
 
 4871             b=bs.
Read(doSecurity);
 
 4874 #if LIBCAT_SECURITY==1 
 4875             char answer[cat::EasyHandshake::ANSWER_BYTES];
 
 4876             CAT_AUDIT_PRINTF(
"AUDIT: Got ID_OPEN_CONNECTION_REPLY_2 and given doSecurity=%i\n", (
int)doSecurity);
 
 4882             cat::ClientEasyHandshake *client_handshake=0;
 
 4883 #endif // LIBCAT_SECURITY 
 4895 #if LIBCAT_SECURITY==1 
 4896                     CAT_AUDIT_PRINTF(
"AUDIT: System address matches an entry in the requestedConnectionQueue and doSecurity=%i\n", (
int)doSecurity);
 
 4899                         if (rcs->client_handshake==0)
 
 4901                             CAT_AUDIT_PRINTF(
"AUDIT: Server wants security but we didn't set a public key -- Reporting back ID_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY to user\n");
 
 4906                             packet->
data[ 1 ] = 0; 
 
 4907                             packet->
bitSize = ( 
sizeof( char ) * 8);
 
 4914                         CAT_AUDIT_PRINTF(
"AUDIT: Looks good, preparing to send challenge to server! client_handshake = %x\n", client_handshake);
 
 4917 #endif // LIBCAT_SECURITY 
 4924                     bool thisIPConnectedRecently=
false;
 
 4926                     if (remoteSystem==0)
 
 4940                     if (thisIPConnectedRecently==
false)
 
 4946 #if LIBCAT_SECURITY==1 
 4947                             cat::u8 ident[cat::EasyHandshake::IDENTITY_BYTES];
 
 4948                             bool doIdentity = 
false;
 
 4950                             if (rcs->client_handshake)
 
 4955                                     if (!rcs->client_handshake->ProcessAnswerWithIdentity(answer, ident, remoteSystem->
reliabilityLayer.GetAuthenticatedEncryption()))
 
 4965                                     if (!rcs->client_handshake->ProcessAnswer(answer, remoteSystem->
reliabilityLayer.GetAuthenticatedEncryption()))
 
 4974                                 rcs->client_handshake=0;
 
 4976 #endif // LIBCAT_SECURITY 
 4988 #if LIBCAT_SECURITY==1 
 4989                             temp.
Write((
unsigned char)(doSecurity ? 1 : 0));
 
 4993                                 unsigned char proof[32];
 
 4994                                 remoteSystem->
reliabilityLayer.GetAuthenticatedEncryption()->GenerateProof(proof, 
sizeof(proof));
 
 4997                                 temp.
Write((
unsigned char)(doIdentity ? 1 : 0));
 
 5005                             temp.
Write((
unsigned char)0);
 
 5006 #endif // LIBCAT_SECURITY 
 5018                             packet->
bitSize = ( 
sizeof( char ) * 8);
 
 5036 #if LIBCAT_SECURITY==1 
 5037                     CAT_AUDIT_PRINTF(
"AUDIT: Deleting client_handshake object %x and rcs->client_handshake object %x\n", client_handshake, rcs->client_handshake);
 
 5040 #endif // LIBCAT_SECURITY 
 5065             if ((
unsigned char)(data)[0] == (
MessageID)ID_INCOMPATIBLE_PROTOCOL_VERSION)
 
 5072             bool connectionAttemptCancelled=
false;
 
 5079                     connectionAttemptCancelled=
true;
 
 5082 #if LIBCAT_SECURITY==1 
 5083                     CAT_AUDIT_PRINTF(
"AUDIT: Connection attempt canceled so deleting rcs->client_handshake object %x\n", rcs->client_handshake);
 
 5085 #endif // LIBCAT_SECURITY 
 5093             if (connectionAttemptCancelled)
 
 5097                 packet->
data[ 0 ] = data[0]; 
 
 5098                 packet->
bitSize = ( 
sizeof( char ) * 8);
 
 5137                 rakPeer->
pluginListNTS[i]->OnDirectSocketReceive(data, length*8, systemAddress);
 
 5143 #if LIBCAT_SECURITY==1 
 5144             if (rakPeer->_using_security)
 
 5146                 bsOut.
Write((
unsigned char) 1); 
 
 5149                 CAT_AUDIT_PRINTF(
"AUDIT: Writing cookie %i to %i:%i\n", cookie, systemAddress);
 
 5150                 bsOut.
Write(cookie);
 
 5152                 bsOut.
WriteAlignedBytes((
const unsigned char *) rakPeer->my_public_key,
sizeof(rakPeer->my_public_key));
 
 5155 #endif // LIBCAT_SECURITY 
 5156                 bsOut.
Write((
unsigned char) 0);  
 
 5183             bool requiresSecurityOfThisClient=
false;
 
 5184 #if LIBCAT_SECURITY==1 
 5185             char remoteHandshakeChallenge[cat::EasyHandshake::CHALLENGE_BYTES];
 
 5187             if (rakPeer->_using_security)
 
 5189                 systemAddress.
ToString(
false, str1, static_cast<size_t>(64));
 
 5194                 CAT_AUDIT_PRINTF(
"AUDIT: Got cookie %i from %i:%i\n", cookie, systemAddress);
 
 5195                 if (rakPeer->_cookie_jar->Verify(&systemAddress.
address,
sizeof(systemAddress.
address), cookie)==
false)
 
 5201                 unsigned char clientWroteChallenge;
 
 5202                 bs.
Read(clientWroteChallenge);
 
 5204                 if (requiresSecurityOfThisClient==
true && clientWroteChallenge==0)
 
 5210                 if (clientWroteChallenge)
 
 5212                     bs.
ReadAlignedBytes((
unsigned char*) remoteHandshakeChallenge, cat::EasyHandshake::CHALLENGE_BYTES);
 
 5214                     printf(
"AUDIT: RECV CHALLENGE ");
 
 5215                     for (
int ii = 0; ii < 
sizeof(remoteHandshakeChallenge); ++ii)
 
 5217                         printf(
"%02x", (cat::u8)remoteHandshakeChallenge[ii]);
 
 5223 #endif // LIBCAT_SECURITY 
 5225             bs.
Read(bindingAddress);
 
 5231             bool IPAddrInUse = rssFromSA != 0 && rssFromSA->
isActive;
 
 5233             bool GUIDInUse = rssFromGuid != 0 && rssFromGuid->
isActive;
 
 5242             if (IPAddrInUse & GUIDInUse)
 
 5258             else if (IPAddrInUse==
false && GUIDInUse==
true)
 
 5263             else if (IPAddrInUse==
true && GUIDInUse==
false)
 
 5278             bsAnswer.
Write(systemAddress);
 
 5279             bsAnswer.
Write(mtu);
 
 5280             bsAnswer.
Write(requiresSecurityOfThisClient);
 
 5286 #if LIBCAT_SECURITY==1 
 5287                 if (requiresSecurityOfThisClient)
 
 5289                     CAT_AUDIT_PRINTF(
"AUDIT: Resending public key and answer from packetloss.  Sending ID_OPEN_CONNECTION_REPLY_2\n");
 
 5290                     bsAnswer.
WriteAlignedBytes((
const unsigned char *) rssFromSA->answer,
sizeof(rssFromSA->answer));
 
 5292 #endif // LIBCAT_SECURITY 
 5306             else if (outcome!=0)
 
 5340             bool thisIPConnectedRecently=
false;
 
 5343             if (thisIPConnectedRecently==
true)
 
 5361 #if LIBCAT_SECURITY==1 
 5362             if (requiresSecurityOfThisClient)
 
 5364                 CAT_AUDIT_PRINTF(
"AUDIT: Writing public key.  Sending ID_OPEN_CONNECTION_REPLY_2\n");
 
 5365                 if (rakPeer->_server_handshake->ProcessChallenge(remoteHandshakeChallenge, rssFromSA->answer, rssFromSA->
reliabilityLayer.GetAuthenticatedEncryption() ))
 
 5379                 bsAnswer.
WriteAlignedBytes((
const unsigned char *) rssFromSA->answer,
sizeof(rssFromSA->answer));
 
 5381 #endif // LIBCAT_SECURITY 
 5404 #if LIBCAT_SECURITY==1 
 5406     printf(
"AUDIT: RECV ");
 
 5407     for (
int ii = 0; ii < length; ++ii)
 
 5409         printf(
"%02x", (cat::u8)data[ii]);
 
 5413 #endif // LIBCAT_SECURITY 
 5416     bool isOfflineMessage;
 
 5431         if ( isOfflineMessage==
false)
 
 5435                 rakNetSocket, &
rnr, timeRead, updateBitStream);
 
 5483         if (
socketList[i]->GetUserConnectionSocketIndex()==userIndex)
 
 5486     RakAssert(
"GetRakNetSocketFromUserConnectionSocketIndex failed" && 0);
 
 5487     return (
unsigned int) -1;
 
 5543     unsigned int activeSystemListIndex;
 
 5550     unsigned int byteSize;
 
 5551     unsigned char *data;
 
 5554     bool callerDataAllocationUsed;
 
 5561     #if   defined(WINDOWS_STORE_RT) 
 5562     #elif defined(_WIN32) 
 5569                 len = ((RNS2_Windows*)
socketList[0])->GetSocketLayerOverride()->RakNetRecvFrom(dataOut,&sender,
true);
 
 5604             if ( callerDataAllocationUsed==
false )
 
 5672         bool condition1, condition2;
 
 5673         unsigned requestedConnectionQueueIndex=0;
 
 5685                 if (condition1 || condition2)
 
 5698                         packet->
bitSize = ( 
sizeof(  char ) * 8);
 
 5703 #if LIBCAT_SECURITY==1 
 5704                     CAT_AUDIT_PRINTF(
"AUDIT: Connection attempt FAILED so deleting rcs->client_handshake object %x\n", rcs->client_handshake);
 
 5749                         socketToUse = rcs->
socket;
 
 5752 #if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) 
 5774                         if (sendToEnd-sendToStart>100)
 
 5788 #if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) 
 5793                     requestedConnectionQueueIndex++;
 
 5797                 requestedConnectionQueueIndex++;
 
 5805     for ( activeSystemListIndex = 0; activeSystemListIndex < 
activeSystemListSize; ++activeSystemListIndex )
 
 5914             while ( bitSize > 0 )
 
 5939                         RAKNET_DEBUG_PRINTF(
"Temporarily banning %i:%i for sending nonsense data\n", systemAddress);
 
 5943                         systemAddress.
ToString(
false, str1, static_cast<size_t>(64));
 
 5970                             bs.
Read(incomingTimestamp);
 
 5992                             inBitStream.
Read(bsSystemAddress);
 
 5997                             inBitStream.
Read(sendPingTime);
 
 5998                             inBitStream.
Read(sendPongTime);
 
 6043                         inBitStream.
Read(sendPingTime);
 
 6044                         inBitStream.
Read(sendPongTime);
 
 6055                         inBitStream.
Read(sendPingTime);
 
 6059                         outBitStream.
Write(sendPingTime);
 
 6105                             bool allowConnection, alreadyConnected;
 
 6110                                 allowConnection=
true;
 
 6112                                 allowConnection=
false;
 
 6115                                 alreadyConnected=
true;
 
 6117                                 alreadyConnected=
false;
 
 6119                             if ( allowConnection )
 
 6128                                 inBitStream.
Read(externalID);
 
 6129                                 inBitStream.
Read(systemIndex);
 
 6134                                 inBitStream.
Read(sendPingTime);
 
 6135                                 inBitStream.
Read(sendPongTime);
 
 6159                                 packet->
bitSize = byteSize * 8;
 
 6168                                 outBitStream.
Write(systemAddress);
 
 6171                                 outBitStream.
Write(sendPongTime);
 
 6176                                 if (alreadyConnected==
false)
 
 6311         + cat::AuthenticatedEncryption::OVERHEAD_BYTES
 
 6317     bool running = 
true;
 
 6392     for (
unsigned int i=0; i < pluginList.
Size(); i++)
 
 6394         switch (packet->
data[0])
 
 6448 #if  !defined(WINDOWS_STORE_RT) 
 6453     int startingIdx = 0;
 
 6456         int lowestIdx = startingIdx;
 
 6464         if (startingIdx != lowestIdx)
 
 6468             ipList[lowestIdx] = temp;