Exemplo n.º 1
0
bool ClientPuzzleManager::NonceTable::checkAdd(Nonce &theNonce)
{
   U32 nonce1 = readU32FromBuffer(theNonce.data);
   U32 nonce2 = readU32FromBuffer(theNonce.data + 4);

   U64 fullNonce = (U64(nonce1) << 32) | nonce2;

   U32 hashIndex = U32(fullNonce % mHashTableSize);
   for(Entry *walk = mHashTable[hashIndex]; walk; walk = walk->mHashNext)
      if(walk->mNonce == theNonce)
         return false;
   Entry *newEntry = (Entry *) mChunker.alloc(sizeof(Entry));
   newEntry->mNonce = theNonce;
   newEntry->mHashNext = mHashTable[hashIndex];
   mHashTable[hashIndex] = newEntry;
   return true;
}
Exemplo n.º 2
0
static void SocketToTNLAddress(const SOCKADDR *sockAddr, Address *address)
{
   if(sockAddr->sa_family == AF_INET)
   {
      address->transport = IPProtocol;
      address->port = htons(((SOCKADDR_IN *) sockAddr)->sin_port);
      U32 addr = htonl(((SOCKADDR_IN *) sockAddr)->sin_addr.s_addr);
      address->netNum[0] = addr;
      address->netNum[1] = 0;
      address->netNum[2] = 0;
      address->netNum[3] = 0;
   }
#ifndef NO_IPX_SUPPORT
   else if(sockAddr->sa_family == AF_IPX)
   {
      address->transport = IPXProtocol;
      address->port = htons(((SOCKADDR_IPX *) sockAddr)->sa_socket);
      address->netNum[0] = readU32FromBuffer((U8 *) ((SOCKADDR_IPX *) sockAddr)->sa_netnum);
      address->netNum[1] = readU32FromBuffer((U8 *) ((SOCKADDR_IPX *) sockAddr)->sa_nodenum);
      address->netNum[2] = readU16FromBuffer((U8 *) ((SOCKADDR_IPX *) sockAddr)->sa_nodenum+4);
      address->netNum[3] = 0;
   }
#endif
}
void AsymmetricKey::load(const ByteBuffer &theBuffer)
{
   mIsValid = false;

   crypto_key *theKey = (crypto_key *) malloc(sizeof(crypto_key));
   const U8 *bufferPtr = theBuffer.getBuffer();

   mHasPrivateKey = bufferPtr[0] == KeyTypePrivate;

   U32 bufferSize = theBuffer.getBufferSize();
   if(bufferSize < sizeof(U32) + 1)
      return;

   mKeySize = readU32FromBuffer(bufferPtr + 1);

   if( crypto_import(bufferPtr + sizeof(U32) + 1, bufferSize - sizeof(U32) - 1, theKey)
         != CRYPT_OK)
      return;

   mKeyData = theKey;

   if(mHasPrivateKey)
   {
      unsigned long bufferLen = sizeof(staticCryptoBuffer) - sizeof(U32) - 1;
      staticCryptoBuffer[0] = KeyTypePublic;

      writeU32ToBuffer(mKeySize, staticCryptoBuffer);

      if( crypto_export(staticCryptoBuffer + sizeof(U32) + 1, &bufferLen, PK_PUBLIC, theKey)
            != CRYPT_OK )
         return;

      bufferLen += sizeof(U32) + 1;

      mPublicKey = new ByteBuffer(staticCryptoBuffer, bufferLen);
      mPublicKey->takeOwnership();
      mPrivateKey = new ByteBuffer(theBuffer);
      mPrivateKey->takeOwnership();
   }
   else
   {
      mPublicKey = new ByteBuffer(theBuffer);
      mPublicKey->takeOwnership();
   }
   mIsValid = true;
}