Browse code

siputils: remove ring 180/183 conversion functions (GH #2989)

- remove ring 180/183 conversion functions (GH #2989)
- they are not working correctly anymore, and can easily be replaced
by textops/htable or reply routes

Henning Westerholt authored on 10/01/2022 13:44:51
Showing 4 changed files
... ...
@@ -20,12 +20,6 @@
20 20
 	handling and URI handling.
21 21
 	</para>
22 22
 	<para>
23
-	It offers some functions related to handle ringing. In a parallel forking
24
-	scenario you get several 183s with SDP. You don't want that your customers
25
-	hear more than one ringtone or answer machine in parallel on the phone.
26
-	So its necessary to drop the 183 in this cases and send a 180 instead.
27
-	</para>
28
-	<para>
29 23
 	This module also provides a function to answer OPTIONS requests which
30 24
 	are directed to the server itself. This means an OPTIONS request
31 25
 	which has the address of the server in the request URI, and no
... ...
@@ -72,26 +66,6 @@
72 66
 
73 67
 	<section>
74 68
 		<title>Parameters</title>
75
-		<section id="siputils.p.ring_timeout">
76
-			<title><varname>ring_timeout</varname> (int)</title>
77
-			<para>
78
-			Timeout value in seconds, define how long the call-id is stored in the internal list kept for replacing 183 messages  with 180.
79
-			A reasonable value is <quote>30</quote>.
80
-			</para>
81
-			<para>
82
-			<emphasis>
83
-				Default value is <quote>0</quote>. This means functionality is disabled.
84
-			</emphasis>
85
-			</para>
86
-			<example>
87
-			<title>Set <varname>ring_timeout</varname> parameter</title>
88
-				<programlisting format="linespecific">
89
-...
90
-modparam("siputils", "ring_timeout", 30)
91
-...
92
-				</programlisting>
93
-			</example>
94
-		</section>
95 69
 		<section id="siputils.p.options_accept">
96 70
 		<title><varname>options_accept</varname> (string)</title>
97 71
 		<para>
... ...
@@ -274,38 +248,6 @@ modparam("auth", "rpid_avp", "$avp(myrpid)")
274 248
 
275 249
 	<section>
276 250
 	<title>Functions</title>
277
-		<section id="siputils.f.ring_insert_callid">
278
-			<title>
279
-				<function moreinfo="none">ring_insert_callid()</function>
280
-			</title>
281
-			<para>
282
-			Inserting the call-id in the internal list, which is checked when
283
-			further replies arrive. Any 183 reply that is received during the
284
-			timeout value will be converted to a 180 message with removed SDP body.
285
-			Please note that you need to set a positive ring_timeout value in order
286
-			to use this function.
287
-			</para>
288
-			<para>
289
-			Note: the change of sip reply can be done only in cases when it is not
290
-			parsed too much before config execution, otherwise it fails. For
291
-			example, setting global parameter "log_prefix" can cause this function
292
-			to fail changing the replies.
293
-			</para>
294
-			<para>
295
-			The function returns TRUE on success, and FALSE during processing failures.
296
-			</para>
297
-			<para>
298
-			This function can be used from REQUEST_ROUTE and FAILURE_ROUTE.
299
-			</para>
300
-			<example>
301
-				<title><function>ring_insert_callid()</function> usage</title>
302
-				<programlisting format="linespecific">
303
-...
304
-ring_insert_callid();
305
-...
306
-				</programlisting>
307
-			</example>
308
-	</section>
309 251
 	<section id="siputils.f.options_reply">
310 252
 		<title>
311 253
 		<function moreinfo="none">options_reply()</function>
312 254
deleted file mode 100644
... ...
@@ -1,388 +0,0 @@
1
-/*
2
- * Copyright (C) 2008-2009 1&1 Internet AG
3
- *
4
- * This file is part of Kamailio, a free SIP server.
5
- *
6
- * Kamailio is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License as published by
8
- * the Free Software Foundation; either version 2 of the License, or
9
- * (at your option) any later version
10
- *
11
- * Kamailio is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
- * GNU General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU General Public License
17
- * along with this program; if not, write to the Free Software
18
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19
- *
20
- */
21
-
22
-
23
-/**
24
- * \file
25
- * \brief SIP-utils :: Only allow one 183 message per call-id
26
- * \ingroup siputils
27
- * - Module; \ref siputils
28
- *
29
- * \section ring_utils UTILS :: Ringing functionality
30
- *
31
- * In a parallel forking scenario you may get several 183s with SDP. You don't want
32
- * that your customers hear more than one ringtone or answer machine in parallel
33
- * on the phone. So its necessary to drop the 183 in these cases and send a 180 instead.
34
- */
35
-
36
-
37
-#include <stdio.h>
38
-#include <string.h>
39
-#include <stdlib.h>
40
-#include <assert.h>
41
-
42
-#include "../../core/parser/msg_parser.h"
43
-#include "../../core/dprint.h"
44
-#include "../../core/error.h"
45
-#include "../../core/ut.h"
46
-#include "../../core/mem/mem.h"
47
-#include "../../core/mem/shm_mem.h"
48
-#include "../../core/timer.h"
49
-#include "../../core/locking.h"
50
-#include "../../core/crypto/md5.h"
51
-
52
-#include "config.h"
53
-#include "ring.h"
54
-
55
-
56
-/*! list of calls for ringing functionality */
57
-struct ring_record_t {
58
-	struct ring_record_t *next;
59
-	unsigned int time; /*!< timeout value */
60
-	char callid[MAXCALLIDLEN+1]; /*!< callid of this call */
61
-};
62
-
63
-/*! hashtable for ringing records */
64
-struct hashtable_entry_t {
65
-	struct ring_record_t *head;
66
-	struct ring_record_t *tail;
67
-};
68
-
69
-typedef struct hashtable_entry_t hashtable_t[HASHTABLESIZE];
70
-
71
-/*! global hashtable */
72
-static hashtable_t *hashtable = NULL;
73
-
74
-static void insert(str callid);
75
-
76
-static int contains(str callid);
77
-
78
-
79
-/*!
80
- * \brief  Inserts callid of message into hashtable
81
- *
82
- * Inserts callid of message into hashtable. Any 183 messages with
83
- * this callid that occur in the next ring_timeout seconds, will be
84
- * converted to 180.
85
- * \param msg SIP message
86
- * \param unused1 unused
87
- * \param unused2 unused
88
- * \return 1 on success, -1 otherwise
89
- */
90
-int ring_insert_callid(struct sip_msg *msg, char *unused1, char *unused2)
91
-{
92
-	/* could fail, eg if already parsed don't care about result */
93
-	if(parse_headers(msg, HDR_CALLID_F, 0)<0) {
94
-		LM_ERR("failed to parse headers\n");
95
-		return -1;
96
-	}
97
-
98
-	if (msg->callid) {
99
-		lock_get(ring_lock);
100
-		if (!contains(msg->callid->body)) insert(msg->callid->body);
101
-		lock_release(ring_lock);
102
-	} else {
103
-		LM_ERR("no callid\n");
104
-		return -1;
105
-	}
106
-
107
-	return 1;
108
-}
109
-
110
-
111
-/*!
112
- * \brief Initialize the ring hashtable in shared memory
113
- */
114
-void ring_init_hashtable(void)
115
-{
116
-	int i;
117
-
118
-	hashtable = shm_malloc(sizeof(hashtable_t));
119
-	assert(hashtable);
120
-	for (i=0; i<HASHTABLESIZE; i++) {
121
-		(*hashtable)[i].head = NULL;
122
-		(*hashtable)[i].tail = NULL;
123
-	}
124
-}
125
-
126
-
127
-/*!
128
- * \brief Destroy the ring hashtable
129
- */
130
-void ring_destroy_hashtable(void)
131
-{
132
-	int i;
133
-
134
-	if (hashtable) {
135
-		for (i=0; i<HASHTABLESIZE; i++) {
136
-			while ((*hashtable)[i].head) {
137
-				struct ring_record_t* rr = (*hashtable)[i].head;
138
-				(*hashtable)[i].head = rr->next;
139
-				shm_free(rr);
140
-			}
141
-			(*hashtable)[i].tail = NULL;
142
-		}
143
-
144
-		shm_free(hashtable);
145
-	}
146
-}
147
-
148
-
149
-/*!
150
- * \brief Hash helper function
151
- * \param buf hashed buffer
152
- * \param len length of buffer
153
- * \return hash value, can be 0
154
- */
155
-static unsigned int hash(char *buf, int len)
156
-{
157
-	int i;
158
-	unsigned int retval = 0;
159
-	MD5_CTX md5context;
160
-	char digest[16];
161
-
162
-	MD5Init(&md5context);
163
-	MD5Update(&md5context, buf, len);
164
-	MD5Final(digest, &md5context);
165
-
166
-	for (i=0; i<16; i++) {
167
-		retval ^= ((unsigned int)((unsigned char)buf[i])) << i;
168
-	}
169
-
170
-	return retval;
171
-}
172
-
173
-
174
-/*!
175
- * \brief Expire entries on the hashtable
176
- * \param index array index that should expired
177
- */
178
-static void remove_timeout(unsigned int index)
179
-{
180
-	int ring_timeout = cfg_get(siputils, siputils_cfg, ring_timeout);
181
-	if(ring_timeout == 0){
182
-		LM_ERR("Could not get timeout from cfg. This will expire all entries");
183
-	}
184
-	while ((*hashtable)[index].head && ((*hashtable)[index].head)->time + ring_timeout < get_ticks()) {
185
-		struct ring_record_t* rr = (*hashtable)[index].head;
186
-		(*hashtable)[index].head = rr->next;
187
-		if ((*hashtable)[index].head == NULL) (*hashtable)[index].tail = NULL;
188
-		LM_DBG("deleting ticks=%d %s\n", get_ticks(), rr->callid);
189
-		shm_free(rr);
190
-	}
191
-}
192
-
193
-
194
-/*!
195
- * \brief Insert a new entry on the hashtable
196
- * \param callid Call-ID string
197
- */
198
-static void insert(str callid)
199
-{
200
-	unsigned int index = hash(callid.s, callid.len) & HASHTABLEMASK;
201
-	struct ring_record_t* rr;
202
-
203
-	remove_timeout(index);
204
-	rr = shm_malloc(sizeof(struct ring_record_t));
205
-	assert(rr);
206
-
207
-	rr->next = NULL;
208
-	rr->time = get_ticks();
209
-	strncpy(rr->callid, callid.s, MIN(callid.len, MAXCALLIDLEN));
210
-	rr->callid[MIN(callid.len, MAXCALLIDLEN)] = 0;
211
-
212
-	if ((*hashtable)[index].tail) {
213
-		(*hashtable)[index].tail->next = rr;
214
-		(*hashtable)[index].tail = rr;
215
-	}
216
-	else {
217
-		(*hashtable)[index].head = rr;
218
-		(*hashtable)[index].tail = rr;
219
-	}
220
-
221
-	LM_DBG("inserting at %d %.*s ticks=%d\n", index, callid.len, callid.s, rr->time);
222
-}
223
-
224
-
225
-/*!
226
- * \brief Helper functions that checks if the hash table contains the callid
227
- * \param callid Call-ID that is searched
228
- * \return 1 when callid could be found, 0 when not found
229
- */
230
-static int contains(str callid)
231
-{
232
-	unsigned int index = hash(callid.s, callid.len) & HASHTABLEMASK;
233
-	struct ring_record_t* rr;
234
-
235
-	remove_timeout(index);
236
-
237
-	rr = (*hashtable)[index].head;
238
-	while (rr) {
239
-		if (strncmp(rr->callid, callid.s, callid.len) == 0) return 1;
240
-		rr = rr->next;
241
-	}
242
-	return 0;
243
-}
244
-
245
-
246
-/*!
247
- * \brief Convert a 183  to a 180 message.
248
- * \param msg SIP message
249
- */
250
-static int conv183(struct sip_msg *msg)
251
-{
252
-	/* content-length and content-type headers are removed */
253
-	char *del1_start = strstr(msg->buf, "Content-Length:");
254
-	char *del2_start = strstr(msg->buf, "Content-Type:");
255
-	char *del1_end;
256
-	char *del2_end;
257
-	char *eoh;
258
-	char *chunk1_start;
259
-	int chunk1_len;
260
-	char *chunk1_dst;
261
-	char *chunk2_start;
262
-	int chunk2_len;
263
-	char *chunk2_dst;
264
-	char *chunk3_start;
265
-	int chunk3_len;
266
-	char *chunk3_dst;
267
-
268
-	if (del1_start>del2_start) {
269
-		char *tmp = del1_start;
270
-		del1_start = del2_start;
271
-		del2_start = tmp;
272
-	}
273
-
274
-	del1_end = NULL;
275
-	if (del1_start) {
276
-		del1_end = strstr(del1_start, "\r\n");
277
-		if (del1_end) del1_end+=2;
278
-	}
279
-	del2_end = NULL;
280
-	if (del2_start) {
281
-		del2_end = strstr(del2_start, "\r\n");
282
-		if (del2_end) del2_end+=2;
283
-	}
284
-
285
-	/* 180 message does not need session description */
286
-	eoh = strstr(msg->buf, "\r\n\r\n");
287
-	if (eoh) eoh+=2;
288
-
289
-	if ((!del1_start) || (!del2_start) || (!del1_end) || (!del2_end) || (!eoh)) {
290
-		LM_ERR("got invalid 183 message\n");
291
-		return -1;
292
-	}
293
-
294
-	/*
295
-	 * if message is parsed further than first deletion, offsets of parsed strings would
296
-	 * not be correct any more. In that case do not convert! If this error is reported,
297
-	 * check if other pre script callbacks are installed before the one of this module.
298
-	 */
299
-	if (msg->unparsed>del1_start) {
300
-		LM_ERR("183 message got parsed too far!\n");
301
-		return -1;
302
-	}
303
-
304
-	/* setting new status */
305
-	msg->first_line.u.reply.statuscode=180;
306
-	msg->first_line.u.reply.status.s[2]='0';
307
-	// don't change length of reason string
308
-	strncpy(msg->first_line.u.reply.reason.s, "Ringing                                           ", msg->first_line.u.reply.reason.len);
309
-
310
-	/* calculate addresses of chunks to be moved */
311
-	chunk1_start = del1_end;
312
-	chunk1_len     = (int)(long)(del2_start-del1_end);
313
-	chunk1_dst   = del1_start;
314
-
315
-	chunk2_start = del2_end;
316
-	chunk2_len     = (int)(long)(eoh-del2_end);
317
-	chunk2_dst   = chunk1_dst+chunk1_len;
318
-
319
-	chunk3_start = "Content-Length: 0\r\n\r\n";
320
-	chunk3_len     = strlen(chunk3_start);
321
-	chunk3_dst   = chunk2_dst+chunk2_len;
322
-
323
-	// move chunks
324
-	memmove(chunk1_dst, chunk1_start, chunk1_len);
325
-	memmove(chunk2_dst, chunk2_start, chunk2_len);
326
-	memmove(chunk3_dst, chunk3_start, chunk3_len);
327
-
328
-	/* terminate string with zero */
329
-	*(chunk3_dst+chunk3_len)='\0';
330
-
331
-	/* update message length */
332
-	msg->len = strlen(msg->buf);
333
-
334
-	return 0;
335
-}
336
-
337
-
338
-/*!
339
- * \brief Callback function that does the work inside the server.
340
- * \param msg SIP message
341
- * \param flags unused
342
- * \param bar unused
343
- * \return 1 on success, -1 on failure
344
- */
345
-int ring_filter(struct sip_msg *msg, unsigned int flags, void *bar)
346
-{
347
-	int contains_callid;
348
-
349
-	if (msg->first_line.type == SIP_REPLY && msg->first_line.u.reply.statuscode == 183) {
350
-		/* could fail, eg if already parsed, don't care about result */
351
-		if(parse_headers(msg, HDR_CALLID_F, 0)<0) {
352
-			LM_ERR("headers parsing failed\n");
353
-			return -1;
354
-		}
355
-
356
-		if (msg->callid) {
357
-			lock_get(ring_lock);
358
-			contains_callid=contains(msg->callid->body);
359
-			lock_release(ring_lock);
360
-
361
-			if (contains_callid) {
362
-				LM_DBG("converting 183 to 180 for %.*s\n", msg->callid->body.len, msg->callid->body.s);
363
-				if (conv183(msg)!=0) return -1;
364
-			}
365
-		} else {
366
-			LM_ERR("no callid\n");
367
-			return -1;
368
-		}
369
-	}
370
-
371
-	return 1;
372
-}
373
-
374
-
375
-/*!
376
- * \brief Fixup function for the ring_insert_callid function
377
- * \param param unused
378
- * \param param_no unused
379
- * \return 0
380
- */
381
-int ring_fixup(void ** param, int param_no) {
382
-	int ring_timeout = cfg_get(siputils, siputils_cfg, ring_timeout);
383
-	if (ring_timeout == 0) {
384
-		LM_ERR("ring_insert_callid functionality deactivated, you need to set a positive ring_timeout\n");
385
-		return -1;
386
-	}
387
-	return 0;
388
-}
389 0
deleted file mode 100644
... ...
@@ -1,87 +0,0 @@
1
-/*
2
- * Copyright (C) 2008-2009 1&1 Internet AG
3
- *
4
- * This file is part of Kamailio, a free SIP server.
5
- *
6
- * Kamailio is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License as published by
8
- * the Free Software Foundation; either version 2 of the License, or
9
- * (at your option) any later version
10
- *
11
- * Kamailio is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
- * GNU General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU General Public License
17
- * along with this program; if not, write to the Free Software
18
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19
- *
20
- */
21
-
22
-/**
23
- * \file
24
- * \brief SIP-utils :: Module with several utiltity functions related to SIP messages handling
25
- * \ingroup siputils
26
- * - Module; \ref siputils
27
- */
28
-
29
-#ifndef RING_H
30
-#define RING_H
31
-
32
-
33
-#define HASHTABLEBITS 13
34
-#define HASHTABLESIZE (((unsigned int)1) << HASHTABLEBITS)
35
-#define HASHTABLEMASK (HASHTABLESIZE - 1)
36
-#define MAXCALLIDLEN 255
37
-
38
-
39
-extern gen_lock_t *ring_lock;
40
-
41
-
42
-/*!
43
- * \brief  Inserts callid of message into hashtable
44
- *
45
- * Inserts callid of message into hashtable. Any 183 messages with
46
- * this callid that occur in the next ring_timeout seconds, will be
47
- * converted to 180.
48
- * \param msg SIP message
49
- * \param unused1 unused
50
- * \param unused2 unused
51
- * \return 1 on success, -1 otherwise
52
- */
53
-int ring_insert_callid(struct sip_msg *msg, char *unused1, char *unused2);
54
-
55
-
56
-/*!
57
- * \brief Initialize the ring hashtable in shared memory
58
- */
59
-void ring_init_hashtable(void);
60
-
61
-
62
-/*!
63
- * \brief Destroy the ring hashtable
64
- */
65
-void ring_destroy_hashtable(void);
66
-
67
-
68
-/*!
69
- * \brief Callback function that does the work inside the server.
70
- * \param msg SIP message
71
- * \param flags unused
72
- * \param bar unused
73
- * \return 1 on success, -1 on failure
74
- */
75
-int ring_filter(struct sip_msg *msg, unsigned int flags, void *bar);
76
-
77
-
78
-/*!
79
- * \brief Fixup function for the ring_insert_callid function
80
- * \param param unused
81
- * \param param_no unused
82
- * \return 0
83
- */
84
-int ring_fixup(void ** param, int param_no);
85
-
86
-
87
-#endif
... ...
@@ -34,12 +34,6 @@
34 34
  *  This module implement various functions and checks related to
35 35
  *  SIP message handling and URI handling.
36 36
  *
37
- *  It offers some functions related to handle ringing. In a
38
- *  parallel forking scenario you get several 183s with SDP. You
39
- *  don't want that your customers hear more than one ringtone or
40
- *  answer machine in parallel on the phone. So its necessary to
41
- *  drop the 183 in this cases and send a 180 instead.
42
- *
43 37
  *  This module provides a function to answer OPTIONS requests
44 38
  *  which are directed to the server itself. This means an OPTIONS
45 39
  *  request which has the address of the server in the request
... ...
@@ -68,7 +62,6 @@
68 62
 #include "../../core/parser/parse_uri.h"
69 63
 #include "../../core/parser/parse_date.h"
70 64
 
71
-#include "ring.h"
72 65
 #include "options.h"
73 66
 
74 67
 #include "checks.h"
... ...
@@ -96,8 +89,6 @@ str rpid_suffix = {DEF_RPID_SUFFIX, sizeof(DEF_RPID_SUFFIX) - 1};
96 89
 /*! Definition of AVP containing rpid value */
97 90
 char* rpid_avp_param = DEF_RPID_AVP;
98 91
 
99
-gen_lock_t *ring_lock = NULL;
100
-unsigned int ring_timeout = 0;
101 92
 /* for options functionality */
102 93
 str opt_accept = str_init(ACPT_DEF);
103 94
 str opt_accept_enc = str_init(ACPT_ENC_DEF);
... ...
@@ -128,8 +119,6 @@ static int fixup_option(void** param, int param_no);
128 119
 char *contact_flds_separator = DEFAULT_SEPARATOR;
129 120
 
130 121
 static cmd_export_t cmds[]={
131
-	{"ring_insert_callid", (cmd_function)ring_insert_callid, 0, ring_fixup,
132
-		0, REQUEST_ROUTE|FAILURE_ROUTE},
133 122
 	{"options_reply",      (cmd_function)opt_reply,         0, 0,
134 123
 		0, REQUEST_ROUTE},
135 124
 	{"is_user",            (cmd_function)is_user,           1, fixup_spve_null,
... ...
@@ -220,7 +209,6 @@ static cmd_export_t cmds[]={
220 209
 };
221 210
 
222 211
 static param_export_t params[] = {
223
-	{"ring_timeout",            INT_PARAM, &default_siputils_cfg.ring_timeout},
224 212
 	{"options_accept",          PARAM_STR, &opt_accept},
225 213
 	{"options_accept_encoding", PARAM_STR, &opt_accept_enc},
226 214
 	{"options_accept_language", PARAM_STR, &opt_accept_lang},
... ...
@@ -256,21 +244,6 @@ struct module_exports exports= {
256 244
 
257 245
 static int mod_init(void)
258 246
 {
259
-	if(default_siputils_cfg.ring_timeout > 0) {
260
-		ring_init_hashtable();
261
-
262
-		ring_lock = lock_alloc();
263
-		assert(ring_lock);
264
-		if (lock_init(ring_lock) == 0) {
265
-			LM_CRIT("cannot initialize lock.\n");
266
-			return -1;
267
-		}
268
-		if (register_script_cb(ring_filter, PRE_SCRIPT_CB|ONREPLY_CB, 0) != 0) {
269
-			LM_ERR("could not insert callback");
270
-			return -1;
271
-		}
272
-	}
273
-
274 247
 	/* bind the SL API */
275 248
 	if (sl_load_api(&opt_slb)!=0) {
276 249
 		LM_ERR("cannot bind to SL API\n");
... ...
@@ -293,13 +266,7 @@ static int mod_init(void)
293 266
 
294 267
 static void mod_destroy(void)
295 268
 {
296
-	if (ring_lock) {
297
-		lock_destroy(ring_lock);
298
-		lock_dealloc((void *)ring_lock);
299
-		ring_lock = NULL;
300
-	}
301 269
 
302
-	ring_destroy_hashtable();
303 270
 }
304 271
 
305 272