forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsg_callbacks.c
96 lines (86 loc) · 2.63 KB
/
msg_callbacks.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
/*
* $Id$
*
* Copyright (C) 2010 Sippy Software, Inc., http://www.sippysoft.com
*
* This file is part of opensips, a free SIP server.
*
* opensips 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
*
* opensips 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
*
*/
#include "parser/msg_parser.h"
#include "mem/mem.h"
#include "msg_callbacks.h"
struct msg_callback {
cb_type_t cb_type;
cb_func_t cb_func;
void *cb_arg;
struct msg_callback *next;
};
int
msg_callback_add(struct sip_msg *msg, cb_type_t cb_type, cb_func_t cb_func, void *cb_arg)
{
struct msg_callback *msg_cb;
switch (cb_type) {
case REQ_PRE_FORWARD:
if (msg->first_line.type == SIP_REQUEST)
break;
LM_ERR("programmatic error - REQ_PRE_FORWARD can only be registered on requests!");
return (-1);
default:
break;
}
msg_cb = pkg_malloc(sizeof(*msg_cb));
if (msg_cb == NULL) {
LM_ERR("can't allocate memory\n");
return (-1);
}
msg_cb->cb_type = cb_type;
msg_cb->cb_func = cb_func;
msg_cb->cb_arg = cb_arg;
msg_cb->next = msg->msg_cb;
msg->msg_cb = msg_cb;
return 0;
}
void
msg_callback_process(struct sip_msg *msg, cb_type_t cb_type, void *core_arg)
{
struct msg_callback *msg_cb;
struct msg_callback *msg_cb_pre;
for (msg_cb = msg->msg_cb; msg_cb != NULL; msg_cb = msg_cb->next) {
if (msg_cb->cb_type != cb_type) {
continue;
}
/* Execute callback */
msg_cb->cb_func(msg, cb_type, msg_cb->cb_arg, core_arg);
}
if (cb_type != MSG_DESTROY)
return;
for (msg_cb_pre = msg->msg_cb; msg_cb_pre != NULL; msg_cb_pre = msg_cb) {
msg_cb = msg_cb_pre->next;
pkg_free(msg_cb_pre);
}
msg->msg_cb = NULL;
}
int
msg_callback_check(struct sip_msg *msg, cb_type_t cb_type, cb_func_t cb_func)
{
struct msg_callback *msg_cb;
for (msg_cb = msg->msg_cb; msg_cb != NULL; msg_cb = msg_cb->next) {
if (msg_cb->cb_func == cb_func && msg_cb->cb_type == cb_type)
return (1);
}
return (0);
}