change random number generator

This commit is contained in:
wangyu- 2018-06-04 00:36:23 -05:00
parent 9fd6428525
commit d4ee2d314b
7 changed files with 131 additions and 55 deletions

View File

@ -8,7 +8,8 @@
#include "common.h" #include "common.h"
#include "log.h" #include "log.h"
#include <random>
#include <cmath>
int about_to_exit=0; 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<u64_t> dis64;
std::uniform_int_distribution<u32_t> dis32;
std::uniform_int_distribution<unsigned char> 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_fd_t()
{ {
random_number_fd=open("/dev/urandom",O_RDONLY); random_number_fd=open("/dev/urandom",O_RDONLY);
@ -45,8 +72,39 @@ struct random_fd_t
int get_fd() int get_fd()
{ {
return random_number_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 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; //u64_t ret;
int size=read(random_fd.get_fd(),&ret,sizeof(ret)); //int size=read(random_fd.get_fd(),&ret,sizeof(ret));
if(size!=sizeof(ret)) //if(size!=sizeof(ret))
{ //{
mylog(log_fatal,"get random number failed %d\n",size); // 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; //u32_t ret;
int size=read(random_fd.get_fd(),&ret,sizeof(ret)); //int size=read(random_fd.get_fd(),&ret,sizeof(ret));
if(size!=sizeof(ret)) //if(size!=sizeof(ret))
{ //{
mylog(log_fatal,"get random number failed %d\n",size); // mylog(log_fatal,"get random number failed %d\n",size);
myexit(-1); // myexit(-1);
} //}
return ret; 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; u32_t ret=0;
while(ret==0) while(ret==0)
{ {
ret=get_true_random_number(); ret=get_fake_random_number();
} }
return ret; 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) int set_timer_ms(int epollfd,int &timer_fd,u32_t timer_interval)

View File

@ -58,7 +58,7 @@ typedef int i32_t;
typedef unsigned short u16_t; typedef unsigned short u16_t;
typedef short i16_t; typedef short i16_t;
struct itimerspec { struct my_itimerspec {
struct timespec it_interval; /* Timer interval */ struct timespec it_interval; /* Timer interval */
struct timespec it_value; /* Initial expiration */ struct timespec it_value; /* Initial expiration */
}; };
@ -209,9 +209,9 @@ char * my_ntoa(u32_t ip);
void myexit(int a); void myexit(int a);
void init_random_number_fd(); void init_random_number_fd();
u64_t get_true_random_number_64(); u64_t get_fake_random_number_64();
u32_t get_true_random_number(); u32_t get_fake_random_number();
u32_t get_true_random_number_nz(); u32_t get_fake_random_number_nz();
u64_t ntoh64(u64_t a); u64_t ntoh64(u64_t a);
u64_t hton64(u64_t a); u64_t hton64(u64_t a);
bool larger_than_u16(uint16_t a,uint16_t b); bool larger_than_u16(uint16_t a,uint16_t b);
@ -231,7 +231,7 @@ void myexit(int a);
int add_iptables_rule(char *); int add_iptables_rule(char *);
int clear_iptables_rule(); 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 random_between(u32_t a,u32_t b);
int set_timer_ms(int epollfd,int &timer_fd,u32_t timer_interval); int set_timer_ms(int epollfd,int &timer_fd,u32_t timer_interval);

View File

@ -74,10 +74,10 @@ void conv_manager_t::clear()
} }
u32_t conv_manager_t::get_new_conv() 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()) while(conv_to_u64.find(conv)!=conv_to_u64.end())
{ {
conv=get_true_random_number_nz(); conv=get_fake_random_number_nz();
} }
return conv; return conv;
} }

View File

@ -179,7 +179,7 @@ public:
loop=0; loop=0;
cb=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; return 0;
} }

View File

@ -92,8 +92,11 @@ void sigint_cb(struct ev_loop *l, ev_signal *w, int revents)
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
unit_test();
struct ev_loop* loop=ev_default_loop(0); struct ev_loop* loop=ev_default_loop(0);
ev_signal signal_watcher_sigpipe; ev_signal signal_watcher_sigpipe;
ev_signal_init(&signal_watcher_sigpipe, sigpipe_cb, 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) 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) if(client_or_server==client_mode)

View File

@ -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); //mylog(log_info,"rand = %d\n",rand);
if (dest.cook&&random_drop != 0) { 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; return 0;
} }
} }
@ -330,6 +330,38 @@ int handle_command(char *s)
int unit_test() 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; int i,j,k;
void *code=fec_new(3,6); void *code=fec_new(3,6);
char arr[6][100]= char arr[6][100]=
@ -543,6 +575,7 @@ int unit_test()
} }
return 0; return 0;
} }

View File

@ -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) if (in_len > 65535||in_len<0)
return -1; return -1;
int iv_len=iv_min+rand()%(iv_max-iv_min); 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); memcpy(output+iv_len,input,in_len);
output[iv_len+in_len]=(uint8_t)iv_len; output[iv_len+in_len]=(uint8_t)iv_len;
@ -88,7 +88,7 @@ int do_obscure(char * data,int &len)
assert(len<buf_len); assert(len<buf_len);
int iv_len=random_between(iv_min,iv_max); int iv_len=random_between(iv_min,iv_max);
get_true_random_chars(data+len,iv_len); get_fake_random_chars(data+len,iv_len);
data[iv_len+len]=(uint8_t)iv_len; data[iv_len+len]=(uint8_t)iv_len;
for(int i=0,j=0;i<len;i++,j++) for(int i=0,j=0;i<len;i++,j++)
{ {