diff options
| author | Jelmer Vernooij <jelmer@samba.org> | 2008-04-02 17:12:26 +0200 | 
|---|---|---|
| committer | Jelmer Vernooij <jelmer@samba.org> | 2008-04-02 17:12:26 +0200 | 
| commit | 1a57b893378c31bd94182b161921a38bdacf8300 (patch) | |
| tree | 7c3808ce9fc3df246aebc285f76ea5e6de520ab0 | |
| parent | 0a14b8cb44e199e39e04e04d0be1b74b660e7447 (diff) | |
| parent | 5be87b2e736962dce2576012b7f1cf215f169f34 (diff) | |
Merge move of random_bytes().
| -rw-r--r-- | lib/misc.c | 65 | ||||
| -rw-r--r-- | unix.c | 67 | 
2 files changed, 67 insertions, 65 deletions
| @@ -391,71 +391,6 @@ signed int do_iconv( char *from_cs, char *to_cs, char *src, char *dst, size_t si  		return( outbuf - dst );  } -/* A pretty reliable random number generator. Tries to use the /dev/random -   devices first, and falls back to the random number generator from libc -   when it fails. Opens randomizer devices with O_NONBLOCK to make sure a -   lack of entropy won't halt BitlBee. */ -void random_bytes( unsigned char *buf, int count ) -{ -	static int use_dev = -1; -	 -	/* Actually this probing code isn't really necessary, is it? */ -	if( use_dev == -1 ) -	{ -		if( access( "/dev/random", R_OK ) == 0 || access( "/dev/urandom", R_OK ) == 0 ) -			use_dev = 1; -		else -		{ -			use_dev = 0; -			srand( ( getpid() << 16 ) ^ time( NULL ) ); -		} -	} -	 -	if( use_dev ) -	{ -		int fd; -		 -		/* At least on Linux, /dev/random can block if there's not -		   enough entropy. We really don't want that, so if it can't -		   give anything, use /dev/urandom instead. */ -		if( ( fd = open( "/dev/random", O_RDONLY | O_NONBLOCK ) ) >= 0 ) -			if( read( fd, buf, count ) == count ) -			{ -				close( fd ); -				return; -			} -		close( fd ); -		 -		/* urandom isn't supposed to block at all, but just to be -		   sure. If it blocks, we'll disable use_dev and use the libc -		   randomizer instead. */ -		if( ( fd = open( "/dev/urandom", O_RDONLY | O_NONBLOCK ) ) >= 0 ) -			if( read( fd, buf, count ) == count ) -			{ -				close( fd ); -				return; -			} -		close( fd ); -		 -		/* If /dev/random blocks once, we'll still try to use it -		   again next time. If /dev/urandom also fails for some -		   reason, stick with libc during this session. */ -		 -		use_dev = 0; -		srand( ( getpid() << 16 ) ^ time( NULL ) ); -	} -	 -	if( !use_dev ) -	{ -		int i; -		 -		/* Possibly the LSB of rand() isn't very random on some -		   platforms. Seems okay on at least Linux and OSX though. */ -		for( i = 0; i < count; i ++ ) -			buf[i] = rand() & 0xff; -	} -} -  int is_bool( char *value )  {  	if( *value == 0 ) @@ -224,3 +224,70 @@ double gettime()  	gettimeofday( time, 0 );  	return( (double) time->tv_sec + (double) time->tv_usec / 1000000 );  } + +/* A pretty reliable random number generator. Tries to use the /dev/random +   devices first, and falls back to the random number generator from libc +   when it fails. Opens randomizer devices with O_NONBLOCK to make sure a +   lack of entropy won't halt BitlBee. */ +void random_bytes( unsigned char *buf, int count ) +{ +	static int use_dev = -1; +	 +	/* Actually this probing code isn't really necessary, is it? */ +	if( use_dev == -1 ) +	{ +		if( access( "/dev/random", R_OK ) == 0 || access( "/dev/urandom", R_OK ) == 0 ) +			use_dev = 1; +		else +		{ +			use_dev = 0; +			srand( ( getpid() << 16 ) ^ time( NULL ) ); +		} +	} +	 +	if( use_dev ) +	{ +		int fd; +		 +		/* At least on Linux, /dev/random can block if there's not +		   enough entropy. We really don't want that, so if it can't +		   give anything, use /dev/urandom instead. */ +		if( ( fd = open( "/dev/random", O_RDONLY | O_NONBLOCK ) ) >= 0 ) +			if( read( fd, buf, count ) == count ) +			{ +				close( fd ); +				return; +			} +		close( fd ); +		 +		/* urandom isn't supposed to block at all, but just to be +		   sure. If it blocks, we'll disable use_dev and use the libc +		   randomizer instead. */ +		if( ( fd = open( "/dev/urandom", O_RDONLY | O_NONBLOCK ) ) >= 0 ) +			if( read( fd, buf, count ) == count ) +			{ +				close( fd ); +				return; +			} +		close( fd ); +		 +		/* If /dev/random blocks once, we'll still try to use it +		   again next time. If /dev/urandom also fails for some +		   reason, stick with libc during this session. */ +		 +		use_dev = 0; +		srand( ( getpid() << 16 ) ^ time( NULL ) ); +	} +	 +	if( !use_dev ) +	{ +		int i; +		 +		/* Possibly the LSB of rand() isn't very random on some +		   platforms. Seems okay on at least Linux and OSX though. */ +		for( i = 0; i < count; i ++ ) +			buf[i] = rand() & 0xff; +	} +} + + | 
