contrib/ldns/util.c
int
ldns_init_random(FILE *fd, unsigned int size)
{
	/* if fp is given, seed srandom with data from file
	   otherwise use /dev/urandom */
	FILE *rand_f;
	uint8_t *seed;
	size_t read = 0;
	unsigned int seed_i;
	struct timeval tv;

	/* we'll need at least sizeof(unsigned int) bytes for the
	   standard prng seed */
	if (size < (unsigned int) sizeof(seed_i)){
		size = (unsigned int) sizeof(seed_i);
	}

	seed = LDNS_XMALLOC(uint8_t, size);
        if(!seed) {
		return 1;
        }

	if (!fd) {
		if ((rand_f = fopen("/dev/urandom", "r")) == NULL) {
			/* no readable /dev/urandom, try /dev/random */
			if ((rand_f = fopen("/dev/random", "r")) == NULL) {
				/* no readable /dev/random either, and no entropy
				   source given. we'll have to improvise */
				for (read = 0; read < size; read++) {
					gettimeofday(&tv, NULL);
					seed[read] = (uint8_t) (tv.tv_usec % 256);
				}
			} else {
				read = fread(seed, 1, size, rand_f);
			}
		} else {
			read = fread(seed, 1, size, rand_f);
		}
	} else {
		rand_f = fd;
		read = fread(seed, 1, size, rand_f);
	}

	if (read < size) {
		LDNS_FREE(seed);
		return 1;
	} else {
#ifdef HAVE_SSL
		/* Seed the OpenSSL prng (most systems have it seeded
		   automatically, in that case this call just adds entropy */
		RAND_seed(seed, (int) size);
#else
		/* Seed the standard prng, only uses the first
		 * unsigned sizeof(unsiged int) bytes found in the entropy pool
		 */
		memcpy(&seed_i, seed, sizeof(seed_i));
		srandom(seed_i);
#endif
		LDNS_FREE(seed);
	}

	if (!fd) {
                if (rand_f) fclose(rand_f);
	}

	return 0;
}
/**
 * Init the random source
 * applications should call this if they need entropy data within ldns
 * If openSSL is available, it is automatically seeded from /dev/urandom
 * or /dev/random
 *
 * If you need more entropy, or have no openssl available, this function
 * MUST be called at the start of the program
 *
 * If openssl *is* available, this function just adds more entropy
 **/
Init the random source
applications should call this if they need entropy data within ldns
If openSSL is available, it is automatically seeded from /dev/urandom
or /dev/random

If you need more entropy, or have no openssl available, this function
MUST be called at the start of the program

If openssl *is* available, this function just adds more entropy
Init the random source applications should call this if they need entropy data within ldns If openSSL is available it is automatically seeded from / dev / urandom or / dev / random If you need more entropy or have no openssl available this function MUST be called at the start of the program If openssl * is * available this function just adds more entropy
int
ldns_init_random(FILE *fd, unsigned int size)
{
	FILE *rand_f;
	uint8_t *seed;
	size_t read = 0;
	unsigned int seed_i;
	struct timeval tv;
	if (size < (unsigned int) sizeof(seed_i)){
		size = (unsigned int) sizeof(seed_i);
	}
	seed = LDNS_XMALLOC(uint8_t, size);
        if(!seed) {
		return 1;
        }
	if (!fd) {
		if ((rand_f = fopen("/dev/urandom", "r")) == NULL) {
			if ((rand_f = fopen("/dev/random", "r")) == NULL) {
				for (read = 0; read < size; read++) {
					gettimeofday(&tv, NULL);
					seed[read] = (uint8_t) (tv.tv_usec % 256);
				}
			} else {
				read = fread(seed, 1, size, rand_f);
			}
		} else {
			read = fread(seed, 1, size, rand_f);
		}
	} else {
		rand_f = fd;
		read = fread(seed, 1, size, rand_f);
	}
	if (read < size) {
		LDNS_FREE(seed);
		return 1;
	} else {
#ifdef HAVE_SSL
		RAND_seed(seed, (int) size);
#else
		memcpy(&seed_i, seed, sizeof(seed_i));
		srandom(seed_i);
#endif
		LDNS_FREE(seed);
	}
	if (!fd) {
                if (rand_f) fclose(rand_f);
	}
	return 0;
}
int ldns_init_random ( FILE * fd , unsigned int size ) { FILE * rand_f ; uint8_t * seed ; size_t read = 0 ; unsigned int seed_i ; struct timeval tv ; if ( size < ( unsigned int ) sizeof ( seed_i ) ) { size = ( unsigned int ) sizeof ( seed_i ) ; } seed = LDNS_XMALLOC ( uint8_t , size ) ; if ( ! seed ) { return 1 ; } if ( ! fd ) { if ( ( rand_f = fopen ( " " , " " ) ) == NULL ) { if ( ( rand_f = fopen ( " " , " " ) ) == NULL ) { for ( read = 0 ; read < size ; read ++ ) { gettimeofday ( & tv , NULL ) ; seed [ read ] = ( uint8_t ) ( tv . tv_usec % 256 ) ; } } else { read = fread ( seed , 1 , size , rand_f ) ; } } else { read = fread ( seed , 1 , size , rand_f ) ; } } else { rand_f = fd ; read = fread ( seed , 1 , size , rand_f ) ; } if ( read < size ) { LDNS_FREE ( seed ) ; return 1 ; } else { #ifdef HAVE_SSL RAND_seed ( seed , ( int ) size ) ; #else memcpy ( & seed_i , seed , sizeof ( seed_i ) ) ; srandom ( seed_i ) ; #endif LDNS_FREE ( seed ) ; } if ( ! fd ) { if ( rand_f ) fclose ( rand_f ) ; } return 0 ; }
Init the random source
 applications should call this if they need entropy data within ldns
 If openSSL is available, it is automatically seeded from /dev/urandom
 or /dev/random
Init the random source applications should call this if they need entropy data within ldns If openSSL is available it is automatically seeded from / dev / urandom or / dev / random
/* if fp is given, seed srandom with data from file
	   otherwise use /dev/urandom */ /* we'll need at least sizeof(unsigned int) bytes for the
	   standard prng seed */ /* no readable /dev/urandom, try /dev/random */ /* no readable /dev/random either, and no entropy
				   source given. we'll have to improvise */ /* Seed the OpenSSL prng (most systems have it seeded
		   automatically, in that case this call just adds entropy */ /* Seed the standard prng, only uses the first
		 * unsigned sizeof(unsiged int) bytes found in the entropy pool
		 */