File: callid.c

package info (click to toggle)
openser 1.1.0-9etch1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 9,828 kB
  • ctags: 11,809
  • sloc: ansic: 120,528; sh: 5,249; yacc: 1,716; makefile: 1,261; php: 656; perl: 205; sql: 190
file content (164 lines) | stat: -rw-r--r-- 4,536 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * $Id: callid.c,v 1.1.1.1 2005/06/13 16:47:45 bogdan_iancu Exp $
 *
 * Fast Call-ID Generator
 *
 * Copyright (C) 2001-2003 FhG Fokus
 *
 * This file is part of openser, a free SIP server.
 *
 * openser 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
 *
 * openser 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
 *
 * History:
 * ----------
 *  2003-04-09  Created by janakj
 *  2003-10-24  updated to the new socket_info lists (andrei)
 */

#include <stdio.h>
#include <stdlib.h>
#include "../../dprint.h"
#include "../../pt.h"
#include "../../socket_info.h"
#include "callid.h"

#define CALLID_NR_LEN 20

/* Call-ID has the following form: <callid_nr>-<pid>@<ip>
 * callid_nr is initialized as a random number and continually
 * increases; -<pid>@<ip> is kept in callid_suffix
 */
#define CALLID_SUFFIX_LEN ( 1 /* - */                                            + \
			    5 /* pid */                                          + \
                           42 /* embedded v4inv6 address can be looong '128.' */ + \
	                    2 /* parenthesis [] */                              + \
                            1 /* ZT 0 */                                         + \
	                   16 /* one never knows ;-) */                            \
                          )

#define CID_SEP '-' /* the character which separates random from constant part */

static unsigned long callid_nr;
static char callid_buf[CALLID_NR_LEN + CALLID_SUFFIX_LEN];

str callid_prefix;
str callid_suffix;


/*
 * Initialize the Call-ID generator -- generates random prefix
 */
int init_callid(void)
{
	int rand_bits, i;

	     /* calculate the initial call-id */
	     /* how many bits and chars do we need to display the 
	      * whole ULONG number */
	callid_prefix.len = sizeof(unsigned long) * 2;
	callid_prefix.s = callid_buf;

	if (callid_prefix.len > CALLID_NR_LEN) {
		LOG(L_ERR, "ERROR: Too small callid buffer\n");
		return -1;
	}
	
	for(rand_bits = 1, i = RAND_MAX; i; i >>= 1, rand_bits++);  /* how long are the rand()s ? */
	i = callid_prefix.len * 4 / rand_bits; /* how many rands() fit in the ULONG ? */

	     /* now fill in the callid with as many random
	      * numbers as you can + 1 */
       	callid_nr = rand(); /* this is the + 1 */

	while(i--) {
		callid_nr <<= rand_bits;
		callid_nr |= rand();
	}

	i = snprintf(callid_prefix.s, callid_prefix.len + 1, "%0*lx", callid_prefix.len, callid_nr);
	if ((i == -1) || (i > callid_prefix.len)) {
		LOG(L_CRIT, "BUG: SORRY, callid calculation failed\n");
		return -2;
	}
	
	DBG("Call-ID initialization: '%.*s'\n", callid_prefix.len, callid_prefix.s);
	return 0;
}


/*
 * Child initialization -- generates suffix
 */
int child_init_callid(int rank) 
{
	struct socket_info *si;
	
	/* on tcp/tls bind_address is 0 so try to get the first address we listen
	 * on no matter the protocol */
	si=bind_address?bind_address:get_first_socket();
	if (si==0){
		LOG(L_CRIT, "BUG: child_init_callid: null socket list\n");
		return -1;
	}
	callid_suffix.s = callid_buf + callid_prefix.len;

	callid_suffix.len = snprintf(callid_suffix.s, CALLID_SUFFIX_LEN,
				     "%c%d@%.*s", CID_SEP, my_pid(), 
				     si->address_str.len,
				     si->address_str.s);
	if ((callid_suffix.len == -1) || (callid_suffix.len > CALLID_SUFFIX_LEN)) {
		LOG(L_ERR, "ERROR: child_init_callid: buffer too small\n");
		return -1;
	}

	DBG("DEBUG: callid: '%.*s'\n", callid_prefix.len + callid_suffix.len, callid_prefix.s);
	return 0;
}


/*
 * Increment a character in hex, return
 * carry flag
 */
static inline int inc_hexchar(char* _c)
{
	if (*_c == '9') {
		*_c = 'a';
		return 0;
	}

	if (*_c == 'f') {
		*_c = '0';
		return 1;
	}

	(*_c)++;
	return 0;
}


/*
 * Get a unique Call-ID
 */
void generate_callid(str* callid)
{
	int i;

	for(i = callid_prefix.len; i; i--) {
		if (!inc_hexchar(callid_prefix.s + i - 1)) break;
	}
	callid->s = callid_prefix.s;
	callid->len = callid_prefix.len + callid_suffix.len;
}