[sr-dev] git:master: lib/srutils: added small api for internal unique id generator

Marius Zbihlei marius.zbihlei at 1and1.ro
Tue Apr 10 13:33:36 CEST 2012


Salut Daniel,

Am testat acest sruid si ma gandeam la o simpla modificare ... as vrea 
sa generez numere intr-adevar pseudorandom, asa ca am folosit un LFSR 
(Linear Feedback Shift Register) pentru a genera sruid-uri de genul :

new sruid is [srid.1.4f84198d.42f4.fbcd] (94 / 25)
new sruid is [srid.1.4f84198d.42f4.30d3] (95 / 25)
new sruid is [srid.1.4f84198d.42f4.ddd4] (96 / 25)
new sruid is [srid.1.4f84198d.42f4.2b5f] (97 / 25)
new sruid is [srid.1.4f84198d.42f4.66b6] (98 / 25)
new sruid is [srid.1.4f84198d.42f4.3b53] (99 / 25)
new sruid is [srid.1.4f84198d.42f4.589c] (100 / 25)

Codul e ff putin si optim (cateva linii de shift-uri pe biti si un XOR). 
Ce parerea ai , commit ?! sau e un motiv pt care doresti uid-uri sub forma ?

new sruid is [srid.1.4f8416f0.42c3.f5] (95 / 23)
new sruid is [srid.1.4f8416f0.42c3.06] (96 / 23)
new sruid is [srid.1.4f8416f0.42c3.16] (97 / 23)
new sruid is [srid.1.4f8416f0.42c3.26] (98 / 23)
new sruid is [srid.1.4f8416f0.42c3.36] (99 / 23)
new sruid is [srid.1.4f8416f0.42c3.46] (100 / 23)

Salutari,

M.
> Module: sip-router
> Branch: master
> Commit: 0d544f04bb17b671341f8bff5c51cea1dba4dd35
> URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=0d544f04bb17b671341f8bff5c51cea1dba4dd35
>
> Author: Daniel-Constantin Mierla<miconda at gmail.com>
> Committer: Daniel-Constantin Mierla<miconda at gmail.com>
> Date:   Tue Apr 10 12:18:35 2012 +0200
>
> lib/srutils: added small api for internal unique id generator
>
> - uses prefix, server id, timestamp, pid and a counter for an unique
>    string
>
> ---
>
>   lib/srutils/sruid.c |  105 +++++++++++++++++++++++++++++++++++++++++++++++++++
>   lib/srutils/sruid.h |   42 ++++++++++++++++++++
>   2 files changed, 147 insertions(+), 0 deletions(-)
>
> diff --git a/lib/srutils/sruid.c b/lib/srutils/sruid.c
> new file mode 100644
> index 0000000..682a244
> --- /dev/null
> +++ b/lib/srutils/sruid.c
> @@ -0,0 +1,105 @@
> +/*new sruid is [srid.1.4f84198d.42f4.fbcd] (94 / 25)
> new sruid is [srid.1.4f84198d.42f4.30d3] (95 / 25)
> new sruid is [srid.1.4f84198d.42f4.ddd4] (96 / 25)
> new sruid is [srid.1.4f84198d.42f4.2b5f] (97 / 25)
> new sruid is [srid.1.4f84198d.42f4.66b6] (98 / 25)
> new sruid is [srid.1.4f84198d.42f4.3b53] (99 / 25)
> new sruid is [srid.1.4f84198d.42f4.589c]
> + * $Id$
> + *
> + * sruid - unique id generator
> + *
> + * Copyright (C) 2012 Daniel-Constantin Mierla (asipto.com)
> + *
> + * This file is part of Kamailio, a free SIP server.
> + *
> + * Kamailio is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version
> + *
> + * Kamailio is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program; if not, write to the Free Software
> + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
> + */
> +
> +#include<stdio.h>
> +#include<unistd.h>
> +#include<stdlib.h>
> +#include<string.h>
> +#include<time.h>
> +
> +#include "../../dprint.h"
> +#include "../../globals.h"
> +#include "../../pt.h"
> +
> +#include "sruid.h"
> +
> +/**
> + *
> + */
> +int sruid_init(sruid_t *sid, char sep, char *cid)
> +{
> +	int i;
> +
> +	if(sid==NULL)
> +		return -1;
> +	memset(sid, 0, sizeof(sruid_t));
> +	memcpy(sid->buf, "srid", 4);
> +	if(cid!=NULL)
> +	{
> +		for(i=0; i<4&&  cid[i]!='\0'; i++)
> +			sid->buf[i] = cid[i];
> +	}
> +	sid->buf[4] = sep;
> +
> +	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 initialize sruid struct - output len: %d\n", i);
> +		return -1;
> +	}
> +	sid->out = sid->buf + i + 5;
> +	sid->uid.s = sid->buf;
> +	LM_DBG("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;
> +	int i;
> +	unsigned int val;
> +
> +	if(sid==NULL)
> +		return -1;
> +
> +	sid->counter++;
> +	if(sid->counter==0)
> +		sid->counter=1;
> +
> +	val = sid->counter;
> +	i = 0;
> +	while(val!=0)
> +	{
> +		digit =  val&  0x0f;
> +		sid->out[i++] = (digit>= 10) ? digit + 'a' - 10 : digit + '0';
> +		val>>= 4;
> +	}
> +	sid->out[i] = '\0';
> +	sid->uid.len = sid->out + i - sid->buf;
> +	LM_DBG("new sruid is [%.*s] (%u / %d)\n", sid->uid.len, sid->uid.s,
> +			sid->counter, sid->uid.len);
> +	return 0;
> +}
> +
> diff --git a/lib/srutils/sruid.h b/lib/srutils/sruid.h
> new file mode 100644
> index 0000000..b7c49ee
> --- /dev/null
> +++ b/lib/srutils/sruid.h
> @@ -0,0 +1,42 @@
> +/*
> + * $Id$
> + *
> + * sruid - unique id generator
> + *
> + * Copyright (C) 2012 Daniel-Constantin Mierla (asipto.com)
> + *
> + * This file is part of Kamailio, a free SIP server.
> + *
> + * Kamailio is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version
> + *
> + * Kamailio is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program; if not, write to the Free Software
> + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
> + */
> +
> +#ifndef _SRUID_H_
> +#define _SRUID_H_
> +
> +#include "../../str.h"
> +
> +#define SRUID_SIZE	40
> +
> +typedef struct sruid {
> +	char buf[SRUID_SIZE];
> +	char *out;
> +	str uid;
> +	unsigned int counter;
> +} sruid_t;
> +
> +int sruid_init(sruid_t *sid, char sep, char *cid);
> +int sruid_next(sruid_t *sid);
> +
> +#endif
>
>
> _______________________________________________
> sr-dev mailing list
> sr-dev at lists.sip-router.org
> http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-dev
>


-- 
Zbihlei Marius

Head of
Linux Development Services Romania

1&1 Internet Development srl    Tel KA: 754-9152
Str Mircea Eliade 18            Tel RO: +40-31-223-9152
Sect 1, Bucuresti               mailto: marius.zbihlei at 1and1.ro
71295, Romania




More information about the sr-dev mailing list