From d4ee2d314b43e9a53ecb7a8318f859db105df5b1 Mon Sep 17 00:00:00 2001 From: wangyu- Date: Mon, 4 Jun 2018 00:36:23 -0500 Subject: [PATCH] change random number generator --- common.cpp | 126 ++++++++++++++++++++++++++++++++----------------- common.h | 10 ++-- connection.cpp | 4 +- fec_manager.h | 2 +- main.cpp | 5 +- misc.cpp | 35 +++++++++++++- packet.cpp | 4 +- 7 files changed, 131 insertions(+), 55 deletions(-) diff --git a/common.cpp b/common.cpp index a118b14..72373a8 100644 --- a/common.cpp +++ b/common.cpp @@ -8,7 +8,8 @@ #include "common.h" #include "log.h" - +#include +#include int about_to_exit=0; @@ -28,9 +29,35 @@ int socket_buf_size=1024*1024; -struct random_fd_t +struct my_random_t { - int random_number_fd; + std::random_device rd; + std::mt19937 gen; + std::uniform_int_distribution dis64; + std::uniform_int_distribution dis32; + + std::uniform_int_distribution dis8; + + my_random_t() + { + std::mt19937 gen_tmp(rd()); + gen=gen_tmp; + gen.discard(700000); //magic + } + u64_t gen64() + { + return dis64(gen); + } + u32_t gen32() + { + return dis32(gen); + } + + unsigned char gen8() + { + return dis8(gen); + } + /*int random_number_fd; random_fd_t() { random_number_fd=open("/dev/urandom",O_RDONLY); @@ -45,8 +72,39 @@ struct random_fd_t int get_fd() { return random_number_fd; + }*/ +}my_random; + +void get_fake_random_chars(char * s,int len) +{ + char *p=s; + int left=len; + + while(left>=(int)sizeof(u64_t)) + { + *((u64_t*)p)=my_random.gen64(); //no endianess problem here , but may break strict-alias? + + p+=sizeof(u64_t); + left-=sizeof(u64_t); } -}random_fd; + if(left) + { + u64_t tmp=my_random.gen64(); + memcpy(p,&tmp,left); + } +} + +int random_between(u32_t a,u32_t b) +{ + if(a>b) + { + mylog(log_fatal,"min >max?? %d %d\n",a ,b); + myexit(1); + } + if(a==b)return a; + else return a+get_fake_random_number()%(b+1-a); +} + /* u64_t get_current_time()//ms { @@ -181,36 +239,36 @@ int clear_iptables_rule() -u64_t get_true_random_number_64() +u64_t get_fake_random_number_64() { - u64_t ret; - int size=read(random_fd.get_fd(),&ret,sizeof(ret)); - if(size!=sizeof(ret)) - { - mylog(log_fatal,"get random number failed %d\n",size); + //u64_t ret; + //int size=read(random_fd.get_fd(),&ret,sizeof(ret)); + //if(size!=sizeof(ret)) + //{ + // mylog(log_fatal,"get random number failed %d\n",size); - myexit(-1); - } + // myexit(-1); + //} - return ret; + return my_random.gen64(); } -u32_t get_true_random_number() +u32_t get_fake_random_number() { - u32_t ret; - int size=read(random_fd.get_fd(),&ret,sizeof(ret)); - if(size!=sizeof(ret)) - { - mylog(log_fatal,"get random number failed %d\n",size); - myexit(-1); - } - return ret; + //u32_t ret; + //int size=read(random_fd.get_fd(),&ret,sizeof(ret)); + //if(size!=sizeof(ret)) + //{ + // mylog(log_fatal,"get random number failed %d\n",size); + // myexit(-1); + //} + return my_random.gen32(); } -u32_t get_true_random_number_nz() //nz for non-zero +u32_t get_fake_random_number_nz() //nz for non-zero { u32_t ret=0; while(ret==0) { - ret=get_true_random_number(); + ret=get_fake_random_number(); } return ret; } @@ -446,26 +504,8 @@ bool larger_than_u16(uint16_t a,uint16_t b) } } -void get_true_random_chars(char * s,int len) -{ - int size=read(random_fd.get_fd(),s,len); - if(size!=len) - { - printf("get random number failed\n"); - exit(-1); - } -} -int random_between(u32_t a,u32_t b) -{ - if(a>b) - { - mylog(log_fatal,"min >max?? %d %d\n",a ,b); - myexit(1); - } - if(a==b)return a; - else return a+get_true_random_number()%(b+1-a); -} + /* int set_timer_ms(int epollfd,int &timer_fd,u32_t timer_interval) diff --git a/common.h b/common.h index 6fdc955..4495129 100644 --- a/common.h +++ b/common.h @@ -58,7 +58,7 @@ typedef int i32_t; typedef unsigned short u16_t; typedef short i16_t; -struct itimerspec { +struct my_itimerspec { struct timespec it_interval; /* Timer interval */ struct timespec it_value; /* Initial expiration */ }; @@ -209,9 +209,9 @@ char * my_ntoa(u32_t ip); void myexit(int a); void init_random_number_fd(); -u64_t get_true_random_number_64(); -u32_t get_true_random_number(); -u32_t get_true_random_number_nz(); +u64_t get_fake_random_number_64(); +u32_t get_fake_random_number(); +u32_t get_fake_random_number_nz(); u64_t ntoh64(u64_t a); u64_t hton64(u64_t a); bool larger_than_u16(uint16_t a,uint16_t b); @@ -231,7 +231,7 @@ void myexit(int a); int add_iptables_rule(char *); int clear_iptables_rule(); -void get_true_random_chars(char * s,int len); +void get_fake_random_chars(char * s,int len); int random_between(u32_t a,u32_t b); int set_timer_ms(int epollfd,int &timer_fd,u32_t timer_interval); diff --git a/connection.cpp b/connection.cpp index 2aa77a9..3c7fe18 100644 --- a/connection.cpp +++ b/connection.cpp @@ -74,10 +74,10 @@ void conv_manager_t::clear() } u32_t conv_manager_t::get_new_conv() { - u32_t conv=get_true_random_number_nz(); + u32_t conv=get_fake_random_number_nz(); while(conv_to_u64.find(conv)!=conv_to_u64.end()) { - conv=get_true_random_number_nz(); + conv=get_fake_random_number_nz(); } return conv; } diff --git a/fec_manager.h b/fec_manager.h index 63b216c..b61f28f 100644 --- a/fec_manager.h +++ b/fec_manager.h @@ -179,7 +179,7 @@ public: loop=0; cb=0; } - seq=(u32_t)get_true_random_number(); //TODO temp solution for a bug. + seq=(u32_t)get_fake_random_number(); //TODO temp solution for a bug. return 0; } diff --git a/main.cpp b/main.cpp index d1b6db0..b8d60b3 100644 --- a/main.cpp +++ b/main.cpp @@ -92,8 +92,11 @@ void sigint_cb(struct ev_loop *l, ev_signal *w, int revents) } + int main(int argc, char *argv[]) { + unit_test(); + struct ev_loop* loop=ev_default_loop(0); ev_signal signal_watcher_sigpipe; ev_signal_init(&signal_watcher_sigpipe, sigpipe_cb, SIGPIPE); @@ -142,7 +145,7 @@ int main(int argc, char *argv[]) if(strlen(tun_dev)==0) { - sprintf(tun_dev,"tun%u",get_true_random_number()%1000); + sprintf(tun_dev,"tun%u",get_fake_random_number()%1000); } if(client_or_server==client_mode) diff --git a/misc.cpp b/misc.cpp index 78e26fc..bcb2aef 100644 --- a/misc.cpp +++ b/misc.cpp @@ -242,7 +242,7 @@ int delay_send(my_time_t delay,const dest_t &dest,char *data,int len) //mylog(log_info,"rand = %d\n",rand); if (dest.cook&&random_drop != 0) { - if (get_true_random_number() % 10000 < (u32_t) random_drop) { + if (get_fake_random_number() % 10000 < (u32_t) random_drop) { return 0; } } @@ -330,6 +330,38 @@ int handle_command(char *s) int unit_test() { + + { + union test_t + { + u64_t u64; + char arry[8]; + }test111; + + assert((void*)&test111.u64==(void*)&test111.arry[0]); + //printf("%llx,%llx\n",&ttt.u64,&ttt.arry[0]); + + printf("%llx\n",get_fake_random_number_64()); + printf("%llx\n",get_fake_random_number_64()); + printf("%llx\n",get_fake_random_number_64()); + + printf("%x\n",get_fake_random_number()); + printf("%x\n",get_fake_random_number()); + printf("%x\n",get_fake_random_number()); + + char buf[10]; + get_fake_random_chars(buf,10); + for(int i=0;i<10;i++) + printf("<%d>",(int)buf[i]); + printf("\n"); + + get_fake_random_chars(buf,10); + for(int i=0;i<10;i++) + printf("<%d>",(int)buf[i]); + printf("\n"); + } + + int i,j,k; void *code=fec_new(3,6); char arr[6][100]= @@ -543,6 +575,7 @@ int unit_test() } + return 0; } diff --git a/packet.cpp b/packet.cpp index f8fc559..bf7f852 100644 --- a/packet.cpp +++ b/packet.cpp @@ -61,7 +61,7 @@ int do_obscure_old(const char * input, int in_len,char *output,int &out_len) if (in_len > 65535||in_len<0) return -1; int iv_len=iv_min+rand()%(iv_max-iv_min); - get_true_random_chars(output,iv_len); + get_fake_random_chars(output,iv_len); memcpy(output+iv_len,input,in_len); output[iv_len+in_len]=(uint8_t)iv_len; @@ -88,7 +88,7 @@ int do_obscure(char * data,int &len) assert(len