Browse code

core: sruid - use specific prefix for global variables

- declare static functions used only local
- functions to append a value to sruid

Daniel-Constantin Mierla authored on 31/03/2021 21:55:51
Showing 2 changed files
... ...
@@ -40,31 +40,38 @@
40 40
 #include "sruid.h"
41 41
 
42 42
 /* starting polynomials */
43
-#define MASK_32 0xb4bcd35c
44
-#define MASK_31 0x7a5bc2e3
43
+#define SRUID_MASK_32 0xb4bcd35c
44
+#define SRUID_MASK_31 0x7a5bc2e3
45 45
 
46
-unsigned int lfsr32, lfsr31;
46
+static unsigned int sruid_lfsr32 = 0;
47
+static unsigned int sruid_lfsr31 = 0;
47 48
 
48
-int shift_lfsr(unsigned int *lfsr, unsigned int mask){
49
+static int sruid_shift_lfsr(unsigned int *lfsr, unsigned int mask)
50
+{
49 51
 	int feedback;
52
+
50 53
 	feedback = *lfsr & 0x1;
51
-	*lfsr >>=1;
52
-	if (feedback == 1)
54
+	*lfsr >>= 1;
55
+	if (feedback == 1) {
53 56
 		*lfsr ^= mask;
57
+	}
58
+
54 59
 	return *lfsr;
55 60
 }
56 61
 
57
-void init_lfsr(void){
58
-	lfsr32 = (unsigned int)time(NULL);
59
-	lfsr31 = (unsigned int)getpid();
62
+static void sruid_init_lfsr(void)
63
+{
64
+	sruid_lfsr32 = (unsigned int)time(NULL);
65
+	sruid_lfsr31 = (unsigned int)getpid();
60 66
 }
61 67
 
62
-/* 
68
+/**
63 69
  * returns a 32 bit random integer
64
- * 
65
- */ 
66
-int get_random(){
67
-	return (shift_lfsr(&lfsr32, MASK_32) ^ shift_lfsr(&lfsr31, MASK_31)) & 0xffffffff;
70
+ *
71
+ */
72
+static int sruid_get_random(){
73
+	return (sruid_shift_lfsr(&sruid_lfsr32, SRUID_MASK_32)
74
+				^ sruid_shift_lfsr(&sruid_lfsr31, SRUID_MASK_31)) & 0xffffffff;
68 75
 }
69 76
 
70 77
 /**
... ...
@@ -74,8 +81,9 @@ int sruid_init(sruid_t *sid, char sep, char *cid, int mode)
74 81
 {
75 82
 	int i;
76 83
 
77
-	if(sid==NULL)
84
+	if(sid==NULL) {
78 85
 		return -1;
86
+	}
79 87
 	memset(sid, 0, sizeof(sruid_t));
80 88
 	memcpy(sid->buf, "srid", 4);
81 89
 	if(cid!=NULL)
... ...
@@ -101,6 +109,9 @@ int sruid_init(sruid_t *sid, char sep, char *cid, int mode)
101 109
 	sid->out = sid->buf + i + 5;
102 110
 	sid->uid.s = sid->buf;
103 111
 	sid->mode = (sruid_mode_t)mode;
112
+	if(sid->mode == SRUID_LFSR) {
113
+		sruid_init_lfsr();
114
+	}
104 115
 	sid->pid = my_pid();
105 116
 	LM_DBG("root for sruid is [%.*s] (%u / %d)\n", i+5, sid->uid.s,
106 117
 			sid->counter, i+5);
... ...
@@ -115,20 +126,22 @@ int sruid_reinit(sruid_t *sid, int mode)
115 126
 	int i;
116 127
 	char sep;
117 128
 
118
-	if(sid==NULL)
129
+	if(sid==NULL) {
119 130
 		return -1;
131
+	}
120 132
 
121 133
 	sep = sid->buf[4];
122 134
 	sid->buf[5] = '\0';
123 135
 
124
-	if(server_id!=0)
136
+	if(server_id!=0) {
125 137
 		i = snprintf(sid->buf+5, SRUID_SIZE - 5 /*so far*/ - 8 /* extra int */,
126 138
 			"%x%c%x%c%x%c", (unsigned int)server_id, sep,
127 139
 			(unsigned int)time(NULL), sep, (unsigned int)my_pid(), sep);
128
-	else
140
+	} else {
129 141
 		i = snprintf(sid->buf+5, SRUID_SIZE - 5 /*so far*/ - 8 /* extra int */,
130 142
 			"%x%c%x%c",
131 143
 			(unsigned int)time(NULL), sep, (unsigned int)my_pid(), sep);
144
+	}
132 145
 	if(i<=0 || i>SRUID_SIZE-13)
133 146
 	{
134 147
 		LM_ERR("could not re-initialize sruid struct - output len: %d\n", i);
... ...
@@ -137,6 +150,9 @@ int sruid_reinit(sruid_t *sid, int mode)
137 150
 	sid->out = sid->buf + i + 5;
138 151
 	sid->uid.s = sid->buf;
139 152
 	sid->mode = (sruid_mode_t)mode;
153
+	if(sid->mode == SRUID_LFSR) {
154
+		sruid_init_lfsr();
155
+	}
140 156
 	sid->pid = my_pid();
141 157
 	LM_DBG("re-init root for sruid is [%.*s] (%u / %d)\n", i+5, sid->uid.s,
142 158
 			sid->counter, i+5);
... ...
@@ -146,19 +162,25 @@ int sruid_reinit(sruid_t *sid, int mode)
146 162
 /**
147 163
  *
148 164
  */
149
-int sruid_next(sruid_t *sid)
165
+int sruid_nextx(sruid_t *sid, str *x)
150 166
 {
151 167
 	unsigned short digit;
152 168
 	int i;
153 169
 	unsigned int val;
154 170
 
155
-	if(sid==NULL)
171
+	if(sid==NULL) {
156 172
 		return -1;
173
+	}
174
+	if(x!=NULL && x->len>0) {
175
+		if(sid->out - sid->buf + 1 + x->len >= SRUID_SIZE) {
176
+			LM_ERR("not enough space for x value\n");
177
+			return -1;
178
+		}
179
+	}
157 180
 
158 181
 	sid->counter++;
159 182
 	if(sid->counter==0) {
160
-		if(sid->mode == SRUID_INC)
161
-		{
183
+		if(sid->mode == SRUID_INC) {
162 184
 			/* counter overflow - re-init to have new timestamp */
163 185
 			if(sruid_reinit(sid, SRUID_INC)<0)
164 186
 				return -1;
... ...
@@ -166,17 +188,22 @@ int sruid_next(sruid_t *sid)
166 188
 		sid->counter=1;
167 189
 	}
168 190
 
169
-	if(sid->mode == SRUID_LFSR)
170
-		val = get_random();
171
-	else
191
+	if(sid->mode == SRUID_LFSR) {
192
+		val = sruid_get_random();
193
+	} else {
172 194
 		val = sid->counter;
195
+	}
173 196
 	i = 0;
174
-	while(val!=0)
175
-	{
197
+	while(val!=0) {
176 198
 		digit =  val & 0x0f;
177 199
 		sid->out[i++] = (digit >= 10) ? digit + 'a' - 10 : digit + '0';
178 200
 		val >>= 4;
179 201
 	}
202
+	if(x!=NULL && x->len>0) {
203
+		sid->out[i++] = sid->buf[4]; /* sep */
204
+		memcpy(sid->out + i, x->s, x->len);
205
+		i += x->len;
206
+	}
180 207
 	sid->out[i] = '\0';
181 208
 	sid->uid.len = sid->out + i - sid->buf;
182 209
 	LM_DBG("new sruid is [%.*s] (%u / %d)\n", sid->uid.len, sid->uid.s,
... ...
@@ -184,11 +211,28 @@ int sruid_next(sruid_t *sid)
184 211
 	return 0;
185 212
 }
186 213
 
214
+/**
215
+ *
216
+ */
217
+int sruid_next(sruid_t *sid)
218
+{
219
+	return sruid_nextx(sid, NULL);
220
+}
221
+
222
+/**
223
+ *
224
+ */
225
+int sruid_nextx_safe(sruid_t *sid, str *x)
226
+{
227
+	if(unlikely(sid->pid!=my_pid())) sruid_reinit(sid, sid->mode);
228
+	return sruid_nextx(sid, x);
229
+}
230
+
187 231
 /**
188 232
  *
189 233
  */
190 234
 int sruid_next_safe(sruid_t *sid)
191 235
 {
192 236
 	if(unlikely(sid->pid!=my_pid())) sruid_reinit(sid, sid->mode);
193
-	return sruid_next(sid);
237
+	return sruid_nextx(sid, NULL);
194 238
 }
... ...
@@ -31,7 +31,7 @@
31 31
 
32 32
 #include "../../core/str.h"
33 33
 
34
-#define SRUID_SIZE	40
34
+#define SRUID_SIZE	64
35 35
 
36 36
 typedef enum {SRUID_INC=0, SRUID_LFSR=1} sruid_mode_t;
37 37
 
... ...
@@ -48,4 +48,7 @@ int sruid_init(sruid_t *sid, char sep, char *cid, int mode);
48 48
 int sruid_next(sruid_t *sid);
49 49
 int sruid_next_safe(sruid_t *sid);
50 50
 
51
+int sruid_nextx(sruid_t *sid, str *x);
52
+int sruid_nextx_safe(sruid_t *sid, str *x);
53
+
51 54
 #endif