sruid.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * $Id$
  3. *
  4. * sruid - unique id generator
  5. *
  6. * Copyright (C) 2012 Daniel-Constantin Mierla (asipto.com)
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <time.h>
  29. #include "../../dprint.h"
  30. #include "../../globals.h"
  31. #include "../../pt.h"
  32. #include "sruid.h"
  33. /* starting polynomials */
  34. #define MASK_32 0xb4bcd35c
  35. #define MASK_31 0x7a5bc2e3
  36. unsigned int lfsr32, lfsr31;
  37. int shift_lfsr(unsigned int *lfsr, unsigned int mask){
  38. int feedback;
  39. feedback = *lfsr & 0x1;
  40. *lfsr >>=1;
  41. if (feedback == 1)
  42. *lfsr ^= mask;
  43. return *lfsr;
  44. }
  45. void init_lfsr(void){
  46. lfsr32 = (unsigned int)time(NULL);
  47. lfsr31 = (unsigned int)getpid();
  48. }
  49. /*
  50. * returns a 32 bit random integer
  51. *
  52. */
  53. int get_random(){
  54. return (shift_lfsr(&lfsr32, MASK_32) ^ shift_lfsr(&lfsr31, MASK_31)) & 0xffffffff;
  55. }
  56. /**
  57. *
  58. */
  59. int sruid_init(sruid_t *sid, char sep, char *cid, int mode)
  60. {
  61. int i;
  62. if(sid==NULL)
  63. return -1;
  64. memset(sid, 0, sizeof(sruid_t));
  65. memcpy(sid->buf, "srid", 4);
  66. if(cid!=NULL)
  67. {
  68. for(i=0; i<4 && cid[i]!='\0'; i++)
  69. sid->buf[i] = cid[i];
  70. }
  71. sid->buf[4] = sep;
  72. if(server_id!=0)
  73. i = snprintf(sid->buf+5, SRUID_SIZE - 5 /*so far*/ - 8 /* extra int */,
  74. "%x%c%x%c%x%c", (unsigned int)server_id, sep,
  75. (unsigned int)time(NULL), sep, (unsigned int)my_pid(), sep);
  76. else
  77. i = snprintf(sid->buf+5, SRUID_SIZE - 5 /*so far*/ - 8 /* extra int */,
  78. "%x%c%x%c",
  79. (unsigned int)time(NULL), sep, (unsigned int)my_pid(), sep);
  80. if(i<=0 || i>SRUID_SIZE-13)
  81. {
  82. LM_ERR("could not initialize sruid struct - output len: %d\n", i);
  83. return -1;
  84. }
  85. sid->out = sid->buf + i + 5;
  86. sid->uid.s = sid->buf;
  87. sid->mode = (sruid_mode_t)mode;
  88. sid->pid = my_pid();
  89. LM_DBG("root for sruid is [%.*s] (%u / %d)\n", i+5, sid->uid.s,
  90. sid->counter, i+5);
  91. return 0;
  92. }
  93. /**
  94. *
  95. */
  96. int sruid_reinit(sruid_t *sid, int mode)
  97. {
  98. int i;
  99. char sep;
  100. if(sid==NULL)
  101. return -1;
  102. sep = sid->buf[4];
  103. sid->buf[5] = '\0';
  104. if(server_id!=0)
  105. i = snprintf(sid->buf+5, SRUID_SIZE - 5 /*so far*/ - 8 /* extra int */,
  106. "%x%c%x%c%x%c", (unsigned int)server_id, sep,
  107. (unsigned int)time(NULL), sep, (unsigned int)my_pid(), sep);
  108. else
  109. i = snprintf(sid->buf+5, SRUID_SIZE - 5 /*so far*/ - 8 /* extra int */,
  110. "%x%c%x%c",
  111. (unsigned int)time(NULL), sep, (unsigned int)my_pid(), sep);
  112. if(i<=0 || i>SRUID_SIZE-13)
  113. {
  114. LM_ERR("could not re-initialize sruid struct - output len: %d\n", i);
  115. return -1;
  116. }
  117. sid->out = sid->buf + i + 5;
  118. sid->uid.s = sid->buf;
  119. sid->mode = (sruid_mode_t)mode;
  120. sid->pid = my_pid();
  121. LM_DBG("re-init root for sruid is [%.*s] (%u / %d)\n", i+5, sid->uid.s,
  122. sid->counter, i+5);
  123. return 0;
  124. }
  125. /**
  126. *
  127. */
  128. int sruid_next(sruid_t *sid)
  129. {
  130. unsigned short digit;
  131. int i;
  132. unsigned int val;
  133. if(sid==NULL)
  134. return -1;
  135. sid->counter++;
  136. if(sid->counter==0) {
  137. if(sid->mode == SRUID_INC)
  138. {
  139. /* counter overflow - re-init to have new timestamp */
  140. if(sruid_reinit(sid, SRUID_INC)<0)
  141. return -1;
  142. }
  143. sid->counter=1;
  144. }
  145. if(sid->mode == SRUID_LFSR)
  146. val = get_random();
  147. else
  148. val = sid->counter;
  149. i = 0;
  150. while(val!=0)
  151. {
  152. digit = val & 0x0f;
  153. sid->out[i++] = (digit >= 10) ? digit + 'a' - 10 : digit + '0';
  154. val >>= 4;
  155. }
  156. sid->out[i] = '\0';
  157. sid->uid.len = sid->out + i - sid->buf;
  158. LM_DBG("new sruid is [%.*s] (%u / %d)\n", sid->uid.len, sid->uid.s,
  159. sid->counter, sid->uid.len);
  160. return 0;
  161. }
  162. /**
  163. *
  164. */
  165. int sruid_next_safe(sruid_t *sid)
  166. {
  167. if(unlikely(sid->pid!=my_pid())) sruid_reinit(sid, sid->mode);
  168. return sruid_next(sid);
  169. }