/** Do the initialization - especially create a random seed and get your own PeerID
 * Must be called first.
 * Gets the name of this peer (for example "Ulrich Norbisrath's peer") and the public key */
F2FPeer * f2fInit( const F2FString myName, const F2FString myPublicKey )
{
#define MAXBUFFERSIZE 1024
	char buffer[MAXBUFFERSIZE+1]; // Buffer for concatenation of name and key
	buffer [MAXBUFFERSIZE]=0; // Make sure this is 0 terminated
	F2FSize buffersize;
	F2FWord32 seeds[ MT_STATE_SIZE ];

	// First create the seed for the mersenne twister
	srand( time(NULL) );  // Initialize the poor randomizer of C itself
	// copy name and publickey in buffer (this copy should be secure)
	strncpy( buffer, myName, MAXBUFFERSIZE );
	buffersize = strlen( buffer );
	strncpy( buffer + buffersize, myPublicKey, MAXBUFFERSIZE - buffersize );
	buffersize = strlen( buffer );
	if (buffersize < 4) // not enough data to create seed
	{
		globalError = F2FErrNotEnoughSeed;
	    return NULL;
	}
	// iterate over seed
	int seedCell;
	for (seedCell = 0; seedCell < MT_STATE_SIZE; ++seedCell)
	{
		// Xor a random number with a random 4 byte pair out of th ebuffer
		seeds[ seedCell ] = rand() ^ *( (F2FWord32 *) (buffer + ( rand()%(buffersize-3) ) ) );
	}
	mts_seedfull( &randomnessState, seeds ); // activate the seed
	/* Create a new peer with random uid */
	F2FPeer *newpeer = f2fPeerListNew( mts_lrand( &randomnessState ), mts_lrand( &randomnessState ) );
	if (newpeer == NULL)
	{
		globalError = F2FErrListFull;
		return NULL;
	}
	/* initialize buffers */
	sendBuffer.size = 0;
	/* Init was successfull */
	initWasCalled = 1;
	myself = newpeer;
	myself->activeprovider = F2FProviderMyself;
	return newpeer;
}
Beispiel #2
0
/*
 * Initialize a Mersenne Twist RNG from a 624-int seed.
 *
 * See mts_seedfull for full commentary.
 */
void mt_seedfull(
    uint32_t		seeds[MT_STATE_SIZE])
    {
    mts_seedfull(&mt_default_state, seeds);
    }
Beispiel #3
0
/*
 * Initialize a Mersenne Twist RNG from a 624-long seed.
 *
 * See mts_seedfull for full commentary.
 */
void mt_seedfull(
    unsigned long	seeds[MT_STATE_SIZE])
    {
    mts_seedfull(&mt_default_state, seeds);
    }