Module: sip-router Branch: master Commit: 0d544f04bb17b671341f8bff5c51cea1dba4dd35 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=0d544f04...
Author: Daniel-Constantin Mierla miconda@gmail.com Committer: Daniel-Constantin Mierla miconda@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 @@ +/* + * $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