[sr-dev] git:master: lib/srutils: reinit root part of sruid when counter overflows

Daniel-Constantin Mierla miconda at gmail.com
Wed Apr 11 13:27:31 CEST 2012


Module: sip-router
Branch: master
Commit: 5b8b34dc40d1e93507d82cedbc19d43a0c78c5a1
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=5b8b34dc40d1e93507d82cedbc19d43a0c78c5a1

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Wed Apr 11 13:21:34 2012 +0200

lib/srutils: reinit root part of sruid when counter overflows

- avoid in this way very unlikely UIDs overlapping (when a pid has to
  give more than 2^32 UIDs)

---

 lib/srutils/sruid.c |   44 +++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 43 insertions(+), 1 deletions(-)

diff --git a/lib/srutils/sruid.c b/lib/srutils/sruid.c
index d9efec8..4b3d9a0 100644
--- a/lib/srutils/sruid.c
+++ b/lib/srutils/sruid.c
@@ -104,6 +104,41 @@ int sruid_init(sruid_t *sid, char sep, char *cid, int mode)
 /**
  *
  */
+int sruid_reinit(sruid_t *sid, int mode)
+{
+	int i;
+	char sep;
+
+	if(sid==NULL)
+		return -1;
+
+	sep = sid->buf[4];
+	sid->buf[5] = '\0';
+
+	if(server_id!=0)
+		i = snprintf(sid->buf+5, SRUID_SIZE - 5 /*so far*/ - 8 /* extra int */,
+			"%x%c%x%c%x%c", (unsigned int)server_id, sep,
+			(unsigned int)time(NULL), sep, (unsigned int)my_pid(), sep);
+	else
+		i = snprintf(sid->buf+5, SRUID_SIZE - 5 /*so far*/ - 8 /* extra int */,
+			"%x%c%x%c",
+			(unsigned int)time(NULL), sep, (unsigned int)my_pid(), sep);
+	if(i<=0 || i>SRUID_SIZE-13)
+	{
+		LM_ERR("could not re-initialize sruid struct - output len: %d\n", i);
+		return -1;
+	}
+	sid->out = sid->buf + i + 5;
+	sid->uid.s = sid->buf;
+	sid->mode = (sruid_mode_t)mode;
+	LM_DBG("re-init root for sruid is [%.*s] (%u / %d)\n", i+5, sid->uid.s,
+			sid->counter, i+5);
+	return 0;
+}
+
+/**
+ *
+ */
 int sruid_next(sruid_t *sid)
 {
 	unsigned short digit;
@@ -114,8 +149,15 @@ int sruid_next(sruid_t *sid)
 		return -1;
 
 	sid->counter++;
-	if(sid->counter==0)
+	if(sid->counter==0) {
+		if(sid->mode == SRUID_INC)
+		{
+			/* counter overflow - re-init to have new timestamp */
+			if(sruid_reinit(sid, SRUID_INC)<0)
+				return -1;
+		}
 		sid->counter=1;
+	}
 
 	if(sid->mode == SRUID_LFSR)
 		val = get_random();




More information about the sr-dev mailing list