Browse code

- Faster Call-ID generator - Increments directly the HEX representation of the random number

Jan Janak authored on 09/04/2003 11:48:42
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,157 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * Fast Call-ID Generator
5
+ *
6
+ * Copyright (C) 2001-2003 Fhg Fokus
7
+ *
8
+ * This file is part of ser, a free SIP server.
9
+ *
10
+ * ser 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
+ * For a license to use the ser software under conditions
16
+ * other than those described here, or to purchase support for this
17
+ * software, please contact iptel.org by e-mail at the following addresses:
18
+ *    info@iptel.org
19
+ *
20
+ * ser is distributed in the hope that it will be useful,
21
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
+ * GNU General Public License for more details.
24
+ *
25
+ * You should have received a copy of the GNU General Public License 
26
+ * along with this program; if not, write to the Free Software 
27
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
+ *
29
+ * History:
30
+ * ----------
31
+ * 2003-04-09 Created by janakj
32
+ */
33
+
34
+#include <stdio.h>
35
+#include <stdlib.h>
36
+#include "../../dprint.h"
37
+#include "../../pt.h"
38
+#include "callid.h"
39
+
40
+#define CALLID_NR_LEN 20
41
+
42
+/* Call-ID has the following form: <callid_nr>-<pid>@<ip>
43
+ * callid_nr is initialized as a random number and continually
44
+ * increases; -<pid>@<ip> is kept in callid_suffix
45
+ */
46
+#define CALLID_SUFFIX_LEN ( 1 /* - */                                            + \
47
+			    5 /* pid */                                          + \
48
+                           42 /* embedded v4inv6 address can be looong '128.' */ + \
49
+	                    2 /* parenthessis [] */                              + \
50
+                            1 /* ZT 0 */                                         + \
51
+	                   16 /* one never knows ;-) */                            \
52
+                          )
53
+
54
+#define CID_SEP '-' /* the character which separates random from constant part */
55
+
56
+static unsigned long callid_nr;
57
+static char callid_buf[CALLID_NR_LEN + CALLID_SUFFIX_LEN];
58
+
59
+str callid_prefix;
60
+str callid_suffix;
61
+
62
+
63
+/*
64
+ * Initialize the Call-ID generator -- generates random prefix
65
+ */
66
+int init_callid(void)
67
+{
68
+	int rand_bits, i;
69
+
70
+	     /* calculate the initial call-id */
71
+	     /* how many bits and chars do we need to display the 
72
+	      * whole ULONG number */
73
+	callid_prefix.len = sizeof(unsigned long) * 2;
74
+	callid_prefix.s = callid_buf;
75
+
76
+	if (callid_prefix.len > CALLID_NR_LEN) {
77
+		LOG(L_ERR, "ERROR: Too small callid buffer\n");
78
+		return -1;
79
+	}
80
+	
81
+	for(rand_bits = 0, i = RAND_MAX; i; i >>= 1, rand_bits++);  /* how long are the rand()s ? */
82
+	i = callid_prefix.len * 4 / rand_bits; /* how many rands() fit in the ULONG ? */
83
+
84
+	     /* now fill in the callid with as many random
85
+	      * numbers as you can + 1 */
86
+	callid_nr = rand(); /* this is the + 1 */
87
+	while(i--) {
88
+		callid_nr <<= rand_bits;
89
+		callid_nr |= rand();
90
+	}
91
+
92
+	i = snprintf(callid_prefix.s, callid_prefix.len, "%0*lx", callid_prefix.len, callid_nr);
93
+	if ((i == -1) || (i > callid_prefix.len)) {
94
+		LOG(L_CRIT, "BUG: SORRY, callid calculation failed\n");
95
+		return -2;
96
+	}
97
+	
98
+	DBG("Call-ID initialization: '%.*s'\n", callid_prefix.len, callid_prefix.s);
99
+	return 0;
100
+}
101
+
102
+
103
+/*
104
+ * Child initialization -- generates suffix
105
+ */
106
+int child_init_callid(int rank) 
107
+{
108
+	callid_suffix.s = callid_buf + callid_prefix.len;
109
+
110
+	callid_suffix.len = snprintf(callid_suffix.s, CALLID_SUFFIX_LEN,
111
+				     "%c%d@%.*s", CID_SEP, my_pid(), 
112
+				     sock_info[bind_idx].address_str.len,
113
+				     sock_info[bind_idx].address_str.s);
114
+	if ((callid_suffix.len == -1) || (callid_suffix.len > CALLID_SUFFIX_LEN)) {
115
+		LOG(L_ERR, "ERROR: child_init_callid: buffer too small\n");
116
+		return -1;
117
+	}
118
+
119
+	DBG("DEBUG: callid: '%.*s'\n", callid_prefix.len + callid_suffix.len, callid_prefix.s);
120
+	return 0;
121
+}
122
+
123
+
124
+/*
125
+ * Increment a character in hex, return
126
+ * carry flag
127
+ */
128
+static inline int inc_hexchar(char* _c)
129
+{
130
+	if (*_c == '9') {
131
+		*_c = 'a';
132
+		return 0;
133
+	}
134
+
135
+	if (*_c == 'f') {
136
+		*_c = '0';
137
+		return 1;
138
+	}
139
+
140
+	(*_c)++;
141
+	return 0;
142
+}
143
+
144
+
145
+/*
146
+ * Get a unique Call-ID
147
+ */
148
+void generate_callid(str* callid)
149
+{
150
+	int i;
151
+
152
+	for(i = callid_prefix.len; i; i--) {
153
+		if (!inc_hexchar(callid_prefix.s + i - 1)) break;
154
+	}
155
+	callid->s = callid_prefix.s;
156
+	callid->len = callid_prefix.len + callid_suffix.len;
157
+}
0 158
new file mode 100644
... ...
@@ -0,0 +1,58 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * Fast Call-ID Generator
5
+ *
6
+ * Copyright (C) 2001-2003 Fhg Fokus
7
+ *
8
+ * This file is part of ser, a free SIP server.
9
+ *
10
+ * ser 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
+ * For a license to use the ser software under conditions
16
+ * other than those described here, or to purchase support for this
17
+ * software, please contact iptel.org by e-mail at the following addresses:
18
+ *    info@iptel.org
19
+ *
20
+ * ser is distributed in the hope that it will be useful,
21
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
+ * GNU General Public License for more details.
24
+ *
25
+ * You should have received a copy of the GNU General Public License 
26
+ * along with this program; if not, write to the Free Software 
27
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
+ *
29
+ * History:
30
+ * ----------
31
+ * 2003-04-09 Created by janakj
32
+ */
33
+
34
+#ifndef CALLID_H
35
+#define CALLID_H
36
+
37
+#include "../../str.h"
38
+
39
+
40
+/*
41
+ * Initialize the Call-ID generator -- generates random prefix
42
+ */
43
+int init_callid(void);
44
+
45
+
46
+/*
47
+ * Child initialization -- generates suffix
48
+ */
49
+int child_init_callid(int rank);
50
+
51
+
52
+/*
53
+ * Get a unique Call-ID
54
+ */
55
+void generate_callid(str* callid);
56
+
57
+
58
+#endif /* CALLID_H */