Changeset 1a57b893
- Timestamp:
- 2008-04-02T15:12:26Z (17 years ago)
- Branches:
- master
- Children:
- e46e077
- Parents:
- 0a14b8c (diff), 5be87b2 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/misc.c
r0a14b8c r1a57b893 392 392 } 393 393 394 /* A pretty reliable random number generator. Tries to use the /dev/random395 devices first, and falls back to the random number generator from libc396 when it fails. Opens randomizer devices with O_NONBLOCK to make sure a397 lack of entropy won't halt BitlBee. */398 void random_bytes( unsigned char *buf, int count )399 {400 static int use_dev = -1;401 402 /* Actually this probing code isn't really necessary, is it? */403 if( use_dev == -1 )404 {405 if( access( "/dev/random", R_OK ) == 0 || access( "/dev/urandom", R_OK ) == 0 )406 use_dev = 1;407 else408 {409 use_dev = 0;410 srand( ( getpid() << 16 ) ^ time( NULL ) );411 }412 }413 414 if( use_dev )415 {416 int fd;417 418 /* At least on Linux, /dev/random can block if there's not419 enough entropy. We really don't want that, so if it can't420 give anything, use /dev/urandom instead. */421 if( ( fd = open( "/dev/random", O_RDONLY | O_NONBLOCK ) ) >= 0 )422 if( read( fd, buf, count ) == count )423 {424 close( fd );425 return;426 }427 close( fd );428 429 /* urandom isn't supposed to block at all, but just to be430 sure. If it blocks, we'll disable use_dev and use the libc431 randomizer instead. */432 if( ( fd = open( "/dev/urandom", O_RDONLY | O_NONBLOCK ) ) >= 0 )433 if( read( fd, buf, count ) == count )434 {435 close( fd );436 return;437 }438 close( fd );439 440 /* If /dev/random blocks once, we'll still try to use it441 again next time. If /dev/urandom also fails for some442 reason, stick with libc during this session. */443 444 use_dev = 0;445 srand( ( getpid() << 16 ) ^ time( NULL ) );446 }447 448 if( !use_dev )449 {450 int i;451 452 /* Possibly the LSB of rand() isn't very random on some453 platforms. Seems okay on at least Linux and OSX though. */454 for( i = 0; i < count; i ++ )455 buf[i] = rand() & 0xff;456 }457 }458 459 394 int is_bool( char *value ) 460 395 { -
unix.c
r0a14b8c r1a57b893 225 225 return( (double) time->tv_sec + (double) time->tv_usec / 1000000 ); 226 226 } 227 228 /* A pretty reliable random number generator. Tries to use the /dev/random 229 devices first, and falls back to the random number generator from libc 230 when it fails. Opens randomizer devices with O_NONBLOCK to make sure a 231 lack of entropy won't halt BitlBee. */ 232 void random_bytes( unsigned char *buf, int count ) 233 { 234 static int use_dev = -1; 235 236 /* Actually this probing code isn't really necessary, is it? */ 237 if( use_dev == -1 ) 238 { 239 if( access( "/dev/random", R_OK ) == 0 || access( "/dev/urandom", R_OK ) == 0 ) 240 use_dev = 1; 241 else 242 { 243 use_dev = 0; 244 srand( ( getpid() << 16 ) ^ time( NULL ) ); 245 } 246 } 247 248 if( use_dev ) 249 { 250 int fd; 251 252 /* At least on Linux, /dev/random can block if there's not 253 enough entropy. We really don't want that, so if it can't 254 give anything, use /dev/urandom instead. */ 255 if( ( fd = open( "/dev/random", O_RDONLY | O_NONBLOCK ) ) >= 0 ) 256 if( read( fd, buf, count ) == count ) 257 { 258 close( fd ); 259 return; 260 } 261 close( fd ); 262 263 /* urandom isn't supposed to block at all, but just to be 264 sure. If it blocks, we'll disable use_dev and use the libc 265 randomizer instead. */ 266 if( ( fd = open( "/dev/urandom", O_RDONLY | O_NONBLOCK ) ) >= 0 ) 267 if( read( fd, buf, count ) == count ) 268 { 269 close( fd ); 270 return; 271 } 272 close( fd ); 273 274 /* If /dev/random blocks once, we'll still try to use it 275 again next time. If /dev/urandom also fails for some 276 reason, stick with libc during this session. */ 277 278 use_dev = 0; 279 srand( ( getpid() << 16 ) ^ time( NULL ) ); 280 } 281 282 if( !use_dev ) 283 { 284 int i; 285 286 /* Possibly the LSB of rand() isn't very random on some 287 platforms. Seems okay on at least Linux and OSX though. */ 288 for( i = 0; i < count; i ++ ) 289 buf[i] = rand() & 0xff; 290 } 291 } 292 293
Note: See TracChangeset
for help on using the changeset viewer.