diff -urp ctorrent-dnh3.3.2.orig/bitfield.cpp ctorrent-dnh3.3.2/bitfield.cpp --- ctorrent-dnh3.3.2.orig/bitfield.cpp 2008-06-15 03:00:19.000000000 +0300 +++ ctorrent-dnh3.3.2/bitfield.cpp 2008-10-10 20:57:51.502803509 +0300 @@ -14,9 +14,7 @@ #include #include -#ifndef HAVE_RANDOM #include "compat.h" -#endif const unsigned char BIT_HEX[] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; @@ -273,10 +271,10 @@ size_t BitField::Random() const { size_t idx; - if( _isfull() ) idx = random() % nbits; + if( _isfull() ) idx = get_random64() % nbits; else{ size_t i; - i = random() % nset + 1; + i = get_random64() % nset + 1; for(idx = 0; idx < nbits && i; idx++) if( _isset(idx) ) i--; idx--; diff -urp ctorrent-dnh3.3.2.orig/btcontent.cpp ctorrent-dnh3.3.2/btcontent.cpp --- ctorrent-dnh3.3.2.orig/btcontent.cpp 2008-06-15 03:00:19.000000000 +0300 +++ ctorrent-dnh3.3.2/btcontent.cpp 2008-10-10 20:58:23.575804166 +0300 @@ -39,10 +39,7 @@ #include "ctcs.h" #include "console.h" #include "bttime.h" - -#ifndef HAVE_RANDOM #include "compat.h" -#endif #define meta_str(keylist,pstr,pint) decode_query(b,flen,(keylist),(pstr),(pint),(int64_t*) 0,QUERY_STR) #define meta_int(keylist,pint) decode_query(b,flen,(keylist),(const char**) 0,(pint),(int64_t*) 0,QUERY_INT) @@ -496,7 +493,7 @@ int btContent::InitialFromMI(const char char *dptr = (char *)m_shake_buffer + 48; char *eptr = dptr + PEER_ID_LEN; while (*sptr) *dptr++ = *sptr++; - while (dptr < eptr) *dptr++ = (unsigned char)random(); + while (dptr < eptr) *dptr++ = (unsigned char)get_random64(); } if( arg_announce ){ diff -urp ctorrent-dnh3.3.2.orig/btrequest.cpp ctorrent-dnh3.3.2/btrequest.cpp --- ctorrent-dnh3.3.2.orig/btrequest.cpp 2008-06-15 03:00:19.000000000 +0300 +++ ctorrent-dnh3.3.2/btrequest.cpp 2008-10-10 20:56:58.309802496 +0300 @@ -5,10 +5,7 @@ #include "btcontent.h" #include "btconfig.h" #include "console.h" - -#ifndef HAVE_RANDOM #include "compat.h" -#endif static void _empty_slice_list(PSLICE *ps_head) @@ -138,7 +135,7 @@ int RequestQueue::CopyShuffle(const Requ u->next = rq_head; } - shuffle = (random()&0x07)+2; + shuffle = (get_random64()&0x07)+2; if( shuffle > len/2 ) shuffle = len/2; for( ; shuffle; shuffle-- ){ prev = u; @@ -148,7 +145,7 @@ int RequestQueue::CopyShuffle(const Requ for( ; ps; ps = psnext ){ psnext = ps->next; if( !i-- ){ - rndbits = random(); + rndbits = get_random64(); i = sizeof(rndbits) - 3; // insure an extra bit } if( (rndbits>>=1)&01 ){ // beginning or end of list diff -urp ctorrent-dnh3.3.2.orig/compat.c ctorrent-dnh3.3.2/compat.c --- ctorrent-dnh3.3.2.orig/compat.c 2008-06-15 03:00:19.000000000 +0300 +++ ctorrent-dnh3.3.2/compat.c 2008-10-10 20:56:11.683803128 +0300 @@ -8,6 +8,9 @@ #include #endif +#include +#include + #include "compat.h" #ifndef HAVE_CLOCK_GETTIME @@ -120,39 +123,83 @@ int strcasecmp(const char *s1, const cha } #endif - -#ifndef HAVE_RANDOM -long random(void) +#define R(a,b) (((a) << (b)) | ((a) >> (32 - (b)))) +static void salsa20(void * __out, void *__in) { - long result; - unsigned long maxlong = 0, i = RAND_MAX; - - maxlong--; - maxlong /= 2; - - result = (long)rand(); - while( i < maxlong ){ - result = (result * 2UL*(RAND_MAX+1UL)) | rand(); - i *= 2UL*(RAND_MAX+1UL); + int i; + uint32_t x[16]; + uint32_t *in = __in; + uint32_t *out = __out; + + for (i = 0; i < 16; ++i) x[i] = in[i]; + + for (i = 20;i > 0;i -= 2) { + x[ 4] ^= R(x[ 0]+x[12], 7); + x[ 8] ^= R(x[ 4]+x[ 0], 9); + x[12] ^= R(x[ 8]+x[ 4],13); + x[ 0] ^= R(x[12]+x[ 8],18); + x[ 9] ^= R(x[ 5]+x[ 1], 7); + x[13] ^= R(x[ 9]+x[ 5], 9); + x[ 1] ^= R(x[13]+x[ 9],13); + x[ 5] ^= R(x[ 1]+x[13],18); + x[14] ^= R(x[10]+x[ 6], 7); + x[ 2] ^= R(x[14]+x[10], 9); + x[ 6] ^= R(x[ 2]+x[14],13); + x[10] ^= R(x[ 6]+x[ 2],18); + x[ 3] ^= R(x[15]+x[11], 7); + x[ 7] ^= R(x[ 3]+x[15], 9); + x[11] ^= R(x[ 7]+x[ 3],13); + x[15] ^= R(x[11]+x[ 7],18); + x[ 1] ^= R(x[ 0]+x[ 3], 7); + x[ 2] ^= R(x[ 1]+x[ 0], 9); + x[ 3] ^= R(x[ 2]+x[ 1],13); + x[ 0] ^= R(x[ 3]+x[ 2],18); + x[ 6] ^= R(x[ 5]+x[ 4], 7); + x[ 7] ^= R(x[ 6]+x[ 5], 9); + x[ 4] ^= R(x[ 7]+x[ 6],13); + x[ 5] ^= R(x[ 4]+x[ 7],18); + x[11] ^= R(x[10]+x[ 9], 7); + x[ 8] ^= R(x[11]+x[10], 9); + x[ 9] ^= R(x[ 8]+x[11],13); + x[10] ^= R(x[ 9]+x[ 8],18); + x[12] ^= R(x[15]+x[14], 7); + x[13] ^= R(x[12]+x[15], 9); + x[14] ^= R(x[13]+x[12],13); + x[15] ^= R(x[14]+x[13],18); + } + for (i = 0;i < 16;++i) out[i] = x[i] + in[i]; +} + +uint64_t get_random64(void) +{ + static uint64_t salsa20_out[8]; + static uint64_t salsa20_in[8]; + static int salsa20_nrints; + static int salsa20_rndinit; + + if (!salsa20_rndinit) { + FILE *frandom; + + salsa20_rndinit = 1; + frandom = fopen("/dev/urandom", "rb"); + if (frandom == NULL) { + _exit(1); + } + setbuf(frandom, NULL); + if (fread(salsa20_in, 1, sizeof(salsa20_in), frandom) != sizeof(salsa20_in)) { + _exit(1); + } + fclose(frandom); } - return (result < 0) ? -result : result; -} -void srandom(unsigned long seed) -{ - unsigned useed; - - if( sizeof(seed) > sizeof(useed) ){ - unsigned long mask = 0xffff; - int i = 2; - while( i < sizeof(useed) ){ - mask = (mask << 16) | 0xffff; - i += 2; - } - useed = (unsigned)(seed & mask); - }else useed = seed; + if (salsa20_nrints == 0) { + if (!++salsa20_in[0]) if (!++salsa20_in[1]) if (!++salsa20_in[2]) if (!++salsa20_in[3]) + if (!++salsa20_in[4]) if (!++salsa20_in[5]) if (!++salsa20_in[6]) ++salsa20_in[7]; + salsa20(salsa20_out, salsa20_in); + salsa20_nrints = 8; + } - srand(useed); + return salsa20_out[--salsa20_nrints]; } -#endif + diff -urp ctorrent-dnh3.3.2.orig/compat.h ctorrent-dnh3.3.2/compat.h --- ctorrent-dnh3.3.2.orig/compat.h 2008-06-15 03:00:19.000000000 +0300 +++ ctorrent-dnh3.3.2/compat.h 2008-10-10 20:55:29.210802909 +0300 @@ -6,7 +6,7 @@ extern "C" { #endif - +#include #if !defined(HAVE_SYS_TIME_H) || defined(TIME_WITH_SYS_TIME) #include #endif @@ -51,12 +51,7 @@ int strncasecmp(const char *s1, const ch int strcasecmp(const char *s1, const char *s2); #endif - -#ifndef HAVE_RANDOM -long random(void); -void srandom(unsigned long seed); -#endif - +uint64_t get_random64(void); #ifdef __cplusplus } Only in ctorrent-dnh3.3.2: compat.h.bak Only in ctorrent-dnh3.3.2: compat.h~ Only in ctorrent-dnh3.3.2: compat.o Only in ctorrent-dnh3.3.2: config.h Only in ctorrent-dnh3.3.2: config.h.in Only in ctorrent-dnh3.3.2: config.log Only in ctorrent-dnh3.3.2: config.status Only in ctorrent-dnh3.3.2: configure Only in ctorrent-dnh3.3.2: configure.ac Only in ctorrent-dnh3.3.2: connect_nonb.cpp Only in ctorrent-dnh3.3.2: connect_nonb.h Only in ctorrent-dnh3.3.2: connect_nonb.o Only in ctorrent-dnh3.3.2: console.cpp Only in ctorrent-dnh3.3.2: console.h Only in ctorrent-dnh3.3.2: console.o Only in ctorrent-dnh3.3.2: ctcs.cpp Only in ctorrent-dnh3.3.2: ctcs.h Only in ctorrent-dnh3.3.2: ctcs.o Only in ctorrent-dnh3.3.2: ctorrent diff -urp ctorrent-dnh3.3.2.orig/ctorrent.cpp ctorrent-dnh3.3.2/ctorrent.cpp --- ctorrent-dnh3.3.2.orig/ctorrent.cpp 2008-06-15 03:00:19.000000000 +0300 +++ ctorrent-dnh3.3.2/ctorrent.cpp 2008-10-10 21:03:48.744803432 +0300 @@ -21,12 +21,8 @@ #include "tracker.h" #include "ctcs.h" #include "console.h" - #include "./config.h" - -#ifndef HAVE_RANDOM #include "compat.h" -#endif #ifndef WINDOWS #include "sigint.h" @@ -48,15 +44,7 @@ int APIENTRY WinMain(HINSTANCE hInstance void Random_init() { - unsigned long seed; -#ifdef HAVE_GETTIMEOFDAY - struct timeval tv; - gettimeofday(&tv,(struct timezone*) 0); - seed = tv.tv_usec + tv.tv_sec + getpid(); -#else - seed = (unsigned long)time((time_t *)0); -#endif - srandom(seed); + (void)get_random64(); } int main(int argc, char **argv) Only in ctorrent-dnh3.3.2: ctorrent.cpp.bak Only in ctorrent-dnh3.3.2: ctorrent.cpp~ Only in ctorrent-dnh3.3.2: ctorrent.h Only in ctorrent-dnh3.3.2: ctorrent.o Only in ctorrent-dnh3.3.2: def.h Only in ctorrent-dnh3.3.2: depcomp Only in ctorrent-dnh3.3.2: downloader.cpp Only in ctorrent-dnh3.3.2: downloader.h Only in ctorrent-dnh3.3.2: downloader.o Only in ctorrent-dnh3.3.2: httpencode.cpp Only in ctorrent-dnh3.3.2: httpencode.h Only in ctorrent-dnh3.3.2: httpencode.o Only in ctorrent-dnh3.3.2: install-sh Only in ctorrent-dnh3.3.2: iplist.cpp Only in ctorrent-dnh3.3.2: iplist.h Only in ctorrent-dnh3.3.2: iplist.o Only in ctorrent-dnh3.3.2: missing Only in ctorrent-dnh3.3.2: mkinstalldirs Only in ctorrent-dnh3.3.2: msgencode.h Only in ctorrent-dnh3.3.2: peer.cpp Only in ctorrent-dnh3.3.2: peer.h Only in ctorrent-dnh3.3.2: peer.o diff -urp ctorrent-dnh3.3.2.orig/peerlist.cpp ctorrent-dnh3.3.2/peerlist.cpp --- ctorrent-dnh3.3.2.orig/peerlist.cpp 2008-06-15 03:00:19.000000000 +0300 +++ ctorrent-dnh3.3.2/peerlist.cpp 2008-10-10 20:58:48.688804487 +0300 @@ -22,11 +22,7 @@ #include "ctcs.h" #include "bttime.h" #include "console.h" - -#if !defined(HAVE_CLOCK_GETTIME) || !defined(HAVE_SNPRINTF) || \ - !defined(HAVE_RANDOM) #include "compat.h" -#endif #define MIN_UNCHOKES 3 #define MIN_OPT_CYCLE 3 @@ -1231,7 +1227,7 @@ int PeerList::UnChokeCheck(btPeer* peer, peer_array[m_max_unchoke] = loster; else { if( !r-- ){ - rndbits = random(); + rndbits = get_random64(); r = 15; } // if loser is empty and current is not, loser gets 75% chance. Only in ctorrent-dnh3.3.2: peerlist.cpp.bak Only in ctorrent-dnh3.3.2: peerlist.cpp~ Only in ctorrent-dnh3.3.2: peerlist.h Only in ctorrent-dnh3.3.2: peerlist.o Only in ctorrent-dnh3.3.2: rate.cpp Only in ctorrent-dnh3.3.2: rate.h Only in ctorrent-dnh3.3.2: rate.o Only in ctorrent-dnh3.3.2: rnd.c Only in ctorrent-dnh3.3.2: rnd.c~ Only in ctorrent-dnh3.3.2: setnonblock.cpp Only in ctorrent-dnh3.3.2: setnonblock.h Only in ctorrent-dnh3.3.2: setnonblock.o Only in ctorrent-dnh3.3.2: sha1.c Only in ctorrent-dnh3.3.2: sha1.h Only in ctorrent-dnh3.3.2: sha1.o Only in ctorrent-dnh3.3.2: sigint.cpp Only in ctorrent-dnh3.3.2: sigint.h Only in ctorrent-dnh3.3.2: sigint.o Only in ctorrent-dnh3.3.2: stamp-h.in Only in ctorrent-dnh3.3.2: stamp-h1 Only in ctorrent-dnh3.3.2: tags diff -urp ctorrent-dnh3.3.2.orig/tracker.cpp ctorrent-dnh3.3.2/tracker.cpp --- ctorrent-dnh3.3.2.orig/tracker.cpp 2008-06-15 03:00:19.000000000 +0300 +++ ctorrent-dnh3.3.2/tracker.cpp 2008-10-10 21:02:33.014804683 +0300 @@ -18,10 +18,7 @@ #include "ctcs.h" #include "console.h" #include "bttime.h" - -#if !defined(HAVE_SNPRINTF) || !defined(HAVE_RANDOM) #include "compat.h" -#endif btTracker Tracker; @@ -325,7 +322,7 @@ int btTracker::Initial() char chars[37] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for(int i=0; i<8; i++) - m_key[i] = chars[random()%36]; + m_key[i] = chars[get_random64()%36]; m_key[8] = 0; /* get local ip address */ Only in ctorrent-dnh3.3.2: tracker.cpp.bak Only in ctorrent-dnh3.3.2: tracker.cpp~ Only in ctorrent-dnh3.3.2: tracker.h Only in ctorrent-dnh3.3.2: tracker.o Only in ctorrent-dnh3.3.2: version.m4