-
Notifications
You must be signed in to change notification settings - Fork 909
/
dlg_cb.c
275 lines (227 loc) · 6.21 KB
/
dlg_cb.c
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* $Id$
*
* Copyright (C) 2006 Voice Sistem SRL
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio 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
*
* Kamailio 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:
* --------
* 2006-04-14 initial version (bogdan)
* 2008-04-04 added direction reporting in dlg callbacks (bogdan)
* 2008-04-14 DLGCB_CREATED may be registered before the module
* initialization (bogdan)
* 2008-04-15 added new type of callback to be triggered when dialogs are
* loaded from DB (bogdan)
*/
#include "../../core/mem/shm_mem.h"
#include "../../core/dprint.h"
#include "dlg_hash.h"
#include "dlg_cb.h"
static struct dlg_head_cbl *create_cbs = 0;
static struct dlg_head_cbl *load_cbs = 0;
static struct dlg_cb_params params = {NULL, NULL, DLG_DIR_NONE, NULL, NULL};
#define POINTER_CLOSED_MARKER ((void *)(-1))
static void run_load_callback(struct dlg_callback *cb);
static struct dlg_head_cbl *init_dlg_callback(void)
{
struct dlg_head_cbl *new_cbs;
new_cbs = (struct dlg_head_cbl *)shm_malloc(sizeof(struct dlg_head_cbl));
if(new_cbs == 0) {
LM_ERR("no more shm mem\n");
return 0;
}
new_cbs->first = 0;
new_cbs->types = 0;
return new_cbs;
}
void destroy_dlg_callbacks_list(struct dlg_callback *cb)
{
struct dlg_callback *cb_t;
while(cb) {
cb_t = cb;
cb = cb->next;
if(cb_t->callback_param_free && cb_t->param) {
cb_t->callback_param_free(cb_t->param);
cb_t->param = NULL;
}
shm_free(cb_t);
}
}
void destroy_dlg_callbacks(unsigned int types)
{
if(types & DLGCB_CREATED) {
if(create_cbs && create_cbs != POINTER_CLOSED_MARKER) {
destroy_dlg_callbacks_list(create_cbs->first);
shm_free(create_cbs);
create_cbs = POINTER_CLOSED_MARKER;
}
}
if(types & DLGCB_LOADED) {
if(load_cbs && load_cbs != POINTER_CLOSED_MARKER) {
destroy_dlg_callbacks_list(load_cbs->first);
shm_free(load_cbs);
load_cbs = POINTER_CLOSED_MARKER;
}
}
}
int register_dlgcb_nodlg(struct dlg_cell *dlg, int types, dialog_cb f,
void *param, param_free_cb ff)
{
if(!dlg) {
LM_ERR("Can't find dialog to register callback\n");
return -1;
}
int ret = register_dlgcb(dlg, types, f, param, ff);
unref_dlg(dlg, 1);
return ret;
}
int register_dlgcb(struct dlg_cell *dlg, int types, dialog_cb f, void *param,
param_free_cb ff)
{
struct dlg_callback *cb;
if(types & DLGCB_LOADED) {
if(types != DLGCB_LOADED) {
LM_CRIT("DLGCB_LOADED type must be registered alone!\n");
return -1;
}
} else if(types & DLGCB_CREATED) {
if(types != DLGCB_CREATED) {
LM_CRIT("DLGCB_CREATED type must be registered alone!\n");
return -1;
}
} else {
if(dlg == 0) {
LM_CRIT("non-DLGCB_CREATED type "
"must be registered to a dialog (dlg missing)!\n");
return -1;
}
}
cb = (struct dlg_callback *)shm_malloc(sizeof(struct dlg_callback));
if(cb == 0) {
LM_ERR("no more shm mem\n");
return -1;
}
cb->types = types;
cb->callback = f;
cb->param = param;
cb->callback_param_free = ff;
if(types == DLGCB_CREATED) {
if(load_cbs == POINTER_CLOSED_MARKER) {
LM_CRIT("DLGCB_CREATED type registered after shutdown!?!\n");
goto error;
}
if(create_cbs == 0) {
/* not initialized yet */
if((create_cbs = init_dlg_callback()) == NULL) {
LM_ERR("no more shm mem\n");
goto error;
}
}
cb->next = create_cbs->first;
create_cbs->first = cb;
create_cbs->types |= types;
} else if(types == DLGCB_LOADED) {
if(load_cbs == POINTER_CLOSED_MARKER) {
/* run the callback on the spot */
run_load_callback(cb);
destroy_dlg_callbacks_list(cb);
return 0;
}
if(load_cbs == 0) {
/* not initialized yet */
if((load_cbs = init_dlg_callback()) == NULL) {
LM_ERR("no more shm mem\n");
goto error;
}
}
cb->next = load_cbs->first;
load_cbs->first = cb;
load_cbs->types |= types;
} else {
cb->next = dlg->cbs.first;
dlg->cbs.first = cb;
dlg->cbs.types |= types;
}
return 0;
error:
shm_free(cb);
return -1;
}
static void run_load_callback(struct dlg_callback *cb)
{
struct dlg_cell *dlg;
unsigned int i;
params.req = NULL;
params.rpl = NULL;
params.direction = DLG_DIR_NONE;
params.param = &cb->param;
for(i = 0; i < d_table->size; i++) {
for(dlg = d_table->entries[i].first; dlg; dlg = dlg->next)
cb->callback(dlg, DLGCB_LOADED, ¶ms);
}
return;
}
void run_load_callbacks(void)
{
struct dlg_callback *cb;
if(load_cbs && load_cbs != POINTER_CLOSED_MARKER) {
for(cb = load_cbs->first; cb; cb = cb->next)
run_load_callback(cb);
}
return;
}
void run_create_callbacks(struct dlg_cell *dlg, struct sip_msg *msg)
{
struct dlg_callback *cb;
LM_DBG("Running DLG_CREATED callbacks\n");
if(create_cbs == NULL || create_cbs == POINTER_CLOSED_MARKER
|| create_cbs->first == NULL)
return;
params.req = msg;
params.rpl = NULL;
/* initial request goes DOWNSTREAM all the time */
params.direction = DLG_DIR_DOWNSTREAM;
/* avoid garbage due static structure */
params.param = NULL;
params.dlg_data = NULL;
for(cb = create_cbs->first; cb; cb = cb->next) {
LM_DBG("dialog=%p\n", dlg);
params.param = &cb->param;
cb->callback(dlg, DLGCB_CREATED, ¶ms);
}
return;
}
void run_dlg_callbacks(int type, struct dlg_cell *dlg, struct sip_msg *req,
struct sip_msg *rpl, unsigned int dir, void *dlg_data)
{
struct dlg_callback *cb;
params.req = req;
params.rpl = rpl;
params.direction = dir;
params.dlg_data = dlg_data;
if(dlg->cbs.first == 0 || ((dlg->cbs.types) & type) == 0)
return;
for(cb = dlg->cbs.first; cb; cb = cb->next) {
if((cb->types) & type) {
LM_DBG("dialog=%p, type=%d\n", dlg, type);
params.param = &cb->param;
cb->callback(dlg, type, ¶ms);
}
}
return;
}