File: script_cb.c

package info (click to toggle)
kamailio 4.2.0-2+deb8u3
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 56,276 kB
  • sloc: ansic: 552,836; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (198 lines) | stat: -rw-r--r-- 4,977 bytes parent folder | download | duplicates (2)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * $Id$
 *
 * Script callbacks -- they add the ability to register callback
 * functions which are always called when script for request
 * processing is entered or left
 *
 *
 * Copyright (C) 2001-2003 FhG Fokus
 *
 * This file is part of ser, a free SIP server.
 *
 * ser 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
 *
 * For a license to use the ser software under conditions
 * other than those described here, or to purchase support for this
 * software, please contact iptel.org by e-mail at the following addresses:
 *    info@iptel.org
 *
 * ser 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * History:
 * --------
 *  2003-03-29  cleaning pkg allocation introduced (jiri)
 *  2003-03-19  replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
 *  2005-02-13  script callbacks devided into request and reply types (bogdan)
 *  2009-06-01  Added pre- and post-script callback support for all types
 *		of route blocks. (Miklos)
 */

/*!
 * \file
 * \brief SIP-router core :: 
 * \ingroup core
 * Module: \ref core
 */


#include <stdlib.h>
#include "script_cb.h"
#include "dprint.h"
#include "error.h"
#include "mem/mem.h"

/* Number of cb types = last cb type */
#define SCRIPT_CB_NUM	(MAX_CB_TYPE-1)

static struct script_cb *pre_script_cb[SCRIPT_CB_NUM];
static struct script_cb *post_script_cb[SCRIPT_CB_NUM];

/* Add a script callback to the beginning of the linked list.
 * Returns -1 on error
 */
static inline int add_callback( struct script_cb **list,
	cb_function f, void *param)
{
	struct script_cb *new_cb;

	new_cb=pkg_malloc(sizeof(struct script_cb));
	if (new_cb==0) {
		LM_CRIT("out of memory\n");
		return -1;
	}
	new_cb->cbf = f;
	new_cb->param = param;
	/* link at the beginning of the list */
	new_cb->next = *list;
	*list = new_cb;
	return 0;
}

/* Register pre- or post-script callbacks.
 * Returns -1 on error, 0 on success
 */
int register_script_cb( cb_function f, unsigned int flags, void *param )
{
	struct script_cb	**cb_array;
	int	i;

	/* type checkings */
	if ( (flags&((1<<SCRIPT_CB_NUM)-1))==0 ) {
		LOG(L_BUG, "register_script_cb: callback flag not specified\n");
		return -1;
	}
	if ( (flags&(~(PRE_SCRIPT_CB|POST_SCRIPT_CB))) >= 1<<SCRIPT_CB_NUM ) {
		LOG(L_BUG, "register_script_cb: unsupported callback flags: %u\n",
			flags);
		return -1;
	}
	if ( (flags&(PRE_SCRIPT_CB|POST_SCRIPT_CB))==0 ||
	(flags&PRE_SCRIPT_CB && flags&POST_SCRIPT_CB) ) {
		LOG(L_BUG, "register_script_cb: callback POST or PRE type must "
			"be exactly one\n");
		return -1;
	}

	if (flags&PRE_SCRIPT_CB)
		cb_array = pre_script_cb;
	else
		cb_array = post_script_cb;

	/* Add the callback to the lists.
	 * (as many times as many flags are set)
	 */
	for (i=0; i<SCRIPT_CB_NUM; i++) {
		if ((flags&(1<<i)) == 0)
			continue;
		if (add_callback(&cb_array[i], f, param) < 0)
			goto add_error;
	}
	return 0;

add_error:
	LM_ERR("failed to add callback\n");
	return -1;
}

int init_script_cb()
{
	memset(pre_script_cb, 0, SCRIPT_CB_NUM * sizeof(struct script_cb *));
	memset(post_script_cb, 0, SCRIPT_CB_NUM * sizeof(struct script_cb *));
	return 0;
}

static inline void destroy_cb_list(struct script_cb **list)
{
	struct script_cb *foo;

	while( *list ) {
		foo = *list;
		*list = (*list)->next;
		pkg_free( foo );
	}
}

void destroy_script_cb()
{
	int	i;

	for (i=0; i<SCRIPT_CB_NUM; i++) {
		destroy_cb_list(&pre_script_cb[i]);
		destroy_cb_list(&post_script_cb[i]);
	}
}

/* Execute pre-script callbacks of a given type.
 * Returns 0 on error, 1 on success
 */
int exec_pre_script_cb( struct sip_msg *msg, enum script_cb_type type)
{
	struct script_cb	*cb;
	unsigned int	flags;

#ifdef EXTRA_DEBUG
	if (type > SCRIPT_CB_NUM) {
		LOG(L_BUG, "Uknown callback type\n");
		abort();
	}
#endif
	flags = PRE_SCRIPT_CB | (1<<(type-1));
	for (cb=pre_script_cb[type-1]; cb ; cb=cb->next ) {
		/* stop on error */
		if (cb->cbf(msg, flags, cb->param)==0)
			return 0;
	}
	return 1;
}

/* Execute post-script callbacks of a given type.
 * Always returns 1, success.
 */
int exec_post_script_cb( struct sip_msg *msg, enum script_cb_type type)
{
	struct script_cb	*cb;
	unsigned int	flags;

#ifdef EXTRA_DEBUG
	if (type > SCRIPT_CB_NUM) {
		LOG(L_BUG, "exec_pre_script_cb: Uknown callback type\n");
		abort();
	}
#endif
	flags = POST_SCRIPT_CB | (1<<(type-1));
	for (cb=post_script_cb[type-1]; cb ; cb=cb->next){
		cb->cbf(msg, flags, cb->param);
	}
	return 1;
}