Пример #1
0
static int splitBucket(EHTFILE * file, char * key, unsigned position) {
	BucketRecord oldBkt, newBkt;
	char * oldPtr = oldBkt.buffer;
	char * newPtr = newBkt.buffer;
	char * ptr = file->bucket->buffer;
	unsigned newHashKey = hash(key);
	unsigned newHashPos = newHashKey % file->tableSize;
	int numDifferent = 0;
	int i;
	unsigned entrySize = sizeof(unsigned) + file->keyLength;

	oldBkt.length = 0;
	newBkt.length = 0;
	for ( i = 0; i < file->bucket->length; ++i ) {
		IndexOverlay * original = (IndexOverlay *)ptr;
		unsigned oldHashKey = hash(original->key);
		if ( oldHashKey != newHashKey )
		    ++numDifferent;
		if ( oldHashKey % file->tableSize == newHashPos ) {
		    IndexOverlay * newEntry = (IndexOverlay *)newPtr;
		    newEntry->link = original->link;
		    strcpy(newEntry->key, original->key);
		    newPtr += entrySize;
		    newBkt.length++;
		} else {
		    IndexOverlay * newEntry = (IndexOverlay *)oldPtr;
		    newEntry->link = original->link;
		    strcpy(newEntry->key, original->key);
		    oldPtr += entrySize;
		    oldBkt.length++;
		}
		ptr += entrySize;
	}
    if ( numDifferent == 0 ) {
        fprintf(stderr, "Unable to process this key!\n");
        exit(1);
	}
	if ( oldBkt.length > 0 ) {
	    IndexOverlay * newEntry = (IndexOverlay *)newPtr;
	    newEntry->link = position;
	    strcpy(newEntry->key, key);
	    newBkt.length++;
	    writeDirect(&oldBkt, file->bucketFile, file->bucketNumber);
	    file->bucketNumber = getDALength(file->bucketFile);
	    writeDirect(&newBkt, file->bucketFile, file->bucketNumber);
	    memcpy(file->bucket, &newBkt, sizeof(newBkt));
	    writeHeader(NULL, file->bucketFile);

	    /* update bucket index */
	    if ( file->cache->tablePos[newHashPos % 512] != newHashPos )
			file->cache->tablePos[newHashPos % 512] = newHashPos;
		file->cache->bucketPos[newHashPos % 512] = file->bucket;
		writeDirect(&(file->bucket), file->bucketIndex, newHashPos);
	    return 1;
	} else
	    return 0;
}
Пример #2
0
static void createIndex(EHTFILE * file, char * ehtFilename, char * bucketFilename) {
    unsigned recnum, bucket;
    BucketRecord rec;

    file->bucketIndex = openDirectAccessFile(ehtFilename, "w", sizeof(unsigned), sizeof(unsigned));
    file->bucketFile = openDirectAccessFile(bucketFilename, "w", sizeof(BucketRecord), 0);
    file->tableSize = 128;
    writeHeader(&(file->tableSize), file->bucketIndex);
    bucket = 0;
    for ( recnum = 0; recnum < file->tableSize; ++recnum )
        writeDirect(&bucket, file->bucketIndex, recnum);
    writeHeader(NULL, file->bucketIndex);

    rec.length = 0;
    writeDirect(&rec, file->bucketFile, 0);
    writeHeader(NULL, file->bucketFile);
}
Пример #3
0
static void doubleHashTable(EHTFILE * file) {
	unsigned i;

	if ( file->tableSize < 512 ) { /* update cache to reflect bucket index */
		for ( i = 0; i < file->tableSize; ++i ) {
		    file->cache->bucketPos[i + file->tableSize] = file->cache->bucketPos[i];
		    file->cache->tablePos[i + file->tableSize] = file->cache->tablePos[i];
		    writeDirect(&(file->cache->bucketPos[i]), file->bucketIndex, i + file->tableSize);
		}
	} else {
		unsigned bucketPos;
    	for ( i = 0; i < file->tableSize; ++i ) {
    		readDirect(&bucketPos, file->bucketIndex, i);
    		writeDirect(&bucketPos, file->bucketIndex, i + file->tableSize);
		}
	}
	file->tableSize *= 2;
	writeHeader(NULL, file->bucketIndex);
}
Пример #4
0
void MBC3Mapper::writeExRam(quint16 address, quint8 data) {
	if (m_ramEnable) {
		if (m_ramBank != -1) {
			if (ramSize())
			  writeDirect(address, data);
		} else {
			time(&m_lastTime);
			switch (m_clockRegister) {
			case 0x08: m_seconds = data; break;
			case 0x09: m_minutes = data; break;
			case 0x0a: m_hours = data; break;
			case 0x0b: m_days = data; break;
			case 0x0c: m_control = (m_control & 0x80) | data; break;
			}
		}
	}
}
Пример #5
0
int addEHTFile(EHTFILE * file, char * key, unsigned position) {
	int entrySize = file->keyLength + sizeof(unsigned);
	int bucketSize = sizeof(file->bucket->buffer) / entrySize;

	if ( readEHTFile(file, key) != 0 )
	    return 1;

	if ( file->bucket->length < bucketSize ) {
		IndexOverlay * indexEntry = (IndexOverlay *)(file->bucket->buffer + file->bucket->length * entrySize);
		indexEntry->link = position;
		strncpy(indexEntry->key, key, file->keyLength);
		file->bucket->length++;
		writeDirect(file->bucket, file->bucketFile, file->bucketNumber);
	} else
		while ( ! splitBucket(file, key, position) )
		    doubleHashTable(file);
}
Пример #6
0
int DTLSBio::sslWrite(const char* buf, int bufLen)
{
  /// Note:  For RTP and STUN packets, you must use writeDirect to comply with RFC 5764
  /// 1. Write unencrypted data using SSL_write
  /// 2. Read encrypted data from outbound BIO using BIO_read
  /// 3. Write encrypted data to external output (probably libnice) 
  /// Returns return value of external output
  ///
  int ret = 0;
  char readBuf[DTLS_BIO_BUFFER_LEN];
  ret = SSL_write(_pSSL, buf, bufLen);
  
  if (ret > 0)
  {
    //
    // We have written something, read it back as encrypted data
    //
    ret = BIO_read(_pOutBIO, readBuf, DTLS_BIO_BUFFER_LEN);
    
    if (ret > 0)
    {
      //
      // We have read the encrypted data, write it to our external output
      //
      ret = writeDirect(readBuf, ret);
    }
  }
  
  //
  // Assume we have written everything or nothing
  //
  if (ret > 0)
    return bufLen;
  
  return 0;
}
Пример #7
0
int DTLSBio::accept()
{
  //
  // Read from the external source
  //
  int ret = 0;
  char readBuf[DTLS_BIO_BUFFER_LEN];
  
  while (!SSL_is_init_finished(_pSSL))
  {
    ret = readDirect(readBuf, DTLS_BIO_BUFFER_LEN);
    if (ret > 0)
    {
      //
      // We have read request from the client
      // Write it to the input BIO
      //
      ret = BIO_write(_pInBIO, readBuf, ret);
      if (ret > 0)
      {
        //
        // Packets are written to the IN BIO.  call the handshake to process 
        // the request
        //
        SSL_do_handshake(_pSSL); 

        //
        // Read the response from the out BIO
        //

        ret = BIO_read(_pOutBIO, readBuf, DTLS_BIO_BUFFER_LEN);

        if (ret > 0)
        {
          //
          // We have read the encrypted data, write it to our external output
          //
          ret = writeDirect(readBuf, ret);

          if (ret <= 0)
          {
            OSS_LOG_ERROR("DTLSBio::accept - writeDirect returned " << ret);
            break;
          }
        }
        else
        {
          OSS_LOG_ERROR("DTLSBio::connect - BIO_read returned " << ret);
          break;
        }
      }
      else
      {
        OSS_LOG_ERROR("DTLSBio::accept - BIO_write returned " << ret);
        break;
      }
    }
    else
    {
      OSS_LOG_ERROR("DTLSBio::accept - readDirect returned " << ret);
      
      if (ret == 0)
      {
        //
        // No data returned by socket but is not an error.  Retry
        //
        continue;
      }
      else
      {
        break;
      }
    }
  }
  
  if (SSL_is_init_finished(_pSSL))
    return 1;
  else
    return 0;
}
Пример #8
0
int DTLSBio::connect()
{
  /// call the client handshake
  /// 1.  Call SSL_do_handshake to start the handshake procedure
  /// 2.  Read the ClientHello data from the output BIO and send to external output
  /// 3.  Read the ServerHelloDone from external input and write it to the input BIO
  /// 4.  Call SSL_do_handshake again to process ServerHelloDone
  /// 5.  Repeat 2-4 to complete Certificate exchange
  ///
  if (SSL_is_init_finished(_pSSL))
  {
    OSS_LOG_ERROR("DTLSBio::connect - SSL Handshake is already completed.");
    return 0;
  }
  
  //
  // Start the handshake
  //
  SSL_do_handshake(_pSSL);
  int ret = 0;
  char readBuf[DTLS_BIO_BUFFER_LEN];
  int resendCount = 0;
  while (!SSL_is_init_finished(_pSSL))
  {
    int pending = BIO_ctrl_pending(_pOutBIO);
    
    if (pending > 0)
    {
      ret = BIO_read(_pOutBIO, readBuf, DTLS_BIO_BUFFER_LEN);

      if (ret > 0)
      {
        //
        // We have read the encrypted data, write it to our external output
        //
        ret = writeDirect(readBuf, ret);

        if (ret <= 0)
        {
          OSS_LOG_ERROR("DTLSBio::connect - writeDirect returned " << ret);
          break;
        }
      }
      else
      {
        OSS_LOG_ERROR("DTLSBio::connect - BIO_read returned " << ret);
        break;
      }
    }

    //
    // Read the response from the server
    //
    
    ret = readDirect(readBuf, DTLS_BIO_BUFFER_LEN);

    if (ret > 0)
    {
      //
      // We have read the response from the server
      // Write it to the input BIO
      //
      ret = BIO_write(_pInBIO, readBuf, ret);
      if (ret > 0)
      {
        //
        // Packets are written to the IN BIO.  call the handshake again to process 
        // the response
        //
        SSL_do_handshake(_pSSL); 
      }
      else
      {
        OSS_LOG_ERROR("DTLSBio::connect - BIO_write returned " << ret);
        break;
      }
    }
    else
    {
      OSS_LOG_ERROR("DTLSBio::connect - readDirect returned " << ret);
      
      if (ret < 0)
      {
        //
        // This is an error.  Abort immediately
        //
        break;
      }
      
      //
      // We are not able to read any response.  We will try to retransmit
      //
      struct timeval timeout;
      if (DTLSv1_get_timeout(_pSSL, &timeout))
      {       
        OSS::UInt64 timeout_value = timeout.tv_sec*1000 + timeout.tv_usec/1000;
        if (timeout_value > 0)
        {
          if (resendCount > MAX_RETRY_COUNT)
          {
            break;
          }
          else
          {
            continue;
          }
        }
        else
        {
          ++resendCount;
          DTLSv1_handle_timeout(_pSSL);
          continue;
        }
      }
      else
      {
        break;
      }
    }
    
    resendCount = 0;
  }
  
  if (SSL_is_init_finished(_pSSL))
    return 1;
  else
    return 0;
}