Module: sip-router
Branch: master
Commit: 7f839dab108423e47660e06459591efef41f81a3
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=7f839da…
Author: Marius Zbihlei <marius.zbihlei(a)1and1.ro>
Committer: Marius Zbihlei <marius.zbihlei(a)1and1.ro>
Date: Tue Apr 10 17:17:56 2012 +0300
Added new mode to sr_init that modifies the way the pseudo-random generator behaves
Now it can use a LFSR (Linear Feedback Shift Register) to generate pseudo random numbers
---
lib/srutils/sruid.c | 36 ++++++++++++++++++++++++++++++++++--
lib/srutils/sruid.h | 5 ++++-
modules_k/kex/kex_mod.c | 4 ++--
3 files changed, 40 insertions(+), 5 deletions(-)
diff --git a/lib/srutils/sruid.c b/lib/srutils/sruid.c
index 682a244..d9efec8 100644
--- a/lib/srutils/sruid.c
+++ b/lib/srutils/sruid.c
@@ -34,10 +34,38 @@
#include "sruid.h"
+/* starting polynomials */
+#define MASK_32 0xb4bcd35c
+#define MASK_31 0x7a5bc2e3
+
+unsigned int lfsr32, lfsr31;
+
+int shift_lfsr(unsigned int *lfsr, unsigned int mask){
+ int feedback;
+ feedback = *lfsr & 0x1;
+ *lfsr >>=1;
+ if (feedback == 1)
+ *lfsr ^= mask;
+ return *lfsr;
+}
+
+void init_lfsr(void){
+ lfsr32 = (unsigned int)time(NULL);
+ lfsr31 = (unsigned int)getpid();
+}
+
+/*
+ * returns a 32 bit random integer
+ *
+ */
+int get_random(){
+ return (shift_lfsr(&lfsr32, MASK_32) ^ shift_lfsr(&lfsr31, MASK_31)) &
0xffffffff;
+}
+
/**
*
*/
-int sruid_init(sruid_t *sid, char sep, char *cid)
+int sruid_init(sruid_t *sid, char sep, char *cid, int mode)
{
int i;
@@ -67,6 +95,7 @@ int sruid_init(sruid_t *sid, char sep, char *cid)
}
sid->out = sid->buf + i + 5;
sid->uid.s = sid->buf;
+ sid->mode = (sruid_mode_t)mode;
LM_DBG("root for sruid is [%.*s] (%u / %d)\n", i+5, sid->uid.s,
sid->counter, i+5);
return 0;
@@ -88,7 +117,10 @@ int sruid_next(sruid_t *sid)
if(sid->counter==0)
sid->counter=1;
- val = sid->counter;
+ if(sid->mode == SRUID_LFSR)
+ val = get_random();
+ else
+ val = sid->counter;
i = 0;
while(val!=0)
{
diff --git a/lib/srutils/sruid.h b/lib/srutils/sruid.h
index b7c49ee..16ac116 100644
--- a/lib/srutils/sruid.h
+++ b/lib/srutils/sruid.h
@@ -29,14 +29,17 @@
#define SRUID_SIZE 40
+typedef enum {SRUID_INC=0, SRUID_LFSR=1} sruid_mode_t;
+
typedef struct sruid {
char buf[SRUID_SIZE];
char *out;
str uid;
unsigned int counter;
+ sruid_mode_t mode;
} sruid_t;
-int sruid_init(sruid_t *sid, char sep, char *cid);
+int sruid_init(sruid_t *sid, char sep, char *cid, int mode);
int sruid_next(sruid_t *sid);
#endif
diff --git a/modules_k/kex/kex_mod.c b/modules_k/kex/kex_mod.c
index 82165f5..ad3cc1d 100644
--- a/modules_k/kex/kex_mod.c
+++ b/modules_k/kex/kex_mod.c
@@ -137,7 +137,7 @@ struct module_exports exports= {
*/
static int mod_init(void)
{
- if(sruid_init(&_kex_sruid, '-', NULL)<0)
+ if(sruid_init(&_kex_sruid, '-', NULL, 0)<0)
return -1;
if(init_mi_core()<0)
return -1;
@@ -158,7 +158,7 @@ static int mod_init(void)
static int child_init(int rank)
{
LM_DBG("rank is (%d)\n", rank);
- if(sruid_init(&_kex_sruid, '-', NULL)<0)
+ if(sruid_init(&_kex_sruid, '-', NULL, 0)<0)
return -1;
if (rank==PROC_INIT)
return pkg_proc_stats_init();