/src/kamailio/src/core/nonsip_hooks.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2006 iptelorg GmbH |
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 | | * \file |
23 | | * \brief Kamailio core :: non-sip callbacks, called whenever a message with protocol != SIP/2.0 |
24 | | * is received (the message must have at least a sip like first line or |
25 | | * else they will be dropped before this callbacks are called |
26 | | * |
27 | | * \ingroup core |
28 | | * Module: \ref core |
29 | | */ |
30 | | |
31 | | #include "nonsip_hooks.h" |
32 | | #include "mem/mem.h" |
33 | | |
34 | | static struct nonsip_hook *nonsip_hooks; |
35 | | static unsigned int nonsip_max_hooks = MAX_NONSIP_HOOKS; |
36 | | static int last_hook_idx = 0; |
37 | | |
38 | | |
39 | | int init_nonsip_hooks() |
40 | 0 | { |
41 | 0 | nonsip_hooks = pkg_malloc(nonsip_max_hooks * sizeof(struct nonsip_hook)); |
42 | 0 | if(nonsip_hooks == 0) { |
43 | 0 | PKG_MEM_ERROR; |
44 | 0 | goto error; |
45 | 0 | } |
46 | 0 | memset(nonsip_hooks, 0, nonsip_max_hooks * sizeof(struct nonsip_hook)); |
47 | 0 | return 0; |
48 | 0 | error: |
49 | 0 | PKG_MEM_ERROR; |
50 | 0 | return -1; |
51 | 0 | } |
52 | | |
53 | | |
54 | | void destroy_nonsip_hooks() |
55 | 0 | { |
56 | 0 | int r; |
57 | |
|
58 | 0 | if(nonsip_hooks) { |
59 | 0 | for(r = 0; r < last_hook_idx; r++) { |
60 | 0 | if(nonsip_hooks[r].destroy) |
61 | 0 | nonsip_hooks[r].destroy(); |
62 | 0 | } |
63 | 0 | pkg_free(nonsip_hooks); |
64 | 0 | nonsip_hooks = 0; |
65 | 0 | } |
66 | 0 | } |
67 | | |
68 | | |
69 | | /* allocates a new hook |
70 | | * returns 0 on success and -1 on error */ |
71 | | int register_nonsip_msg_hook(struct nonsip_hook *h) |
72 | 0 | { |
73 | 0 | struct nonsip_hook *tmp; |
74 | 0 | int new_max_hooks; |
75 | |
|
76 | 0 | if(nonsip_max_hooks == 0) |
77 | 0 | goto error; |
78 | 0 | if(last_hook_idx >= nonsip_max_hooks) { |
79 | 0 | new_max_hooks = 2 * nonsip_max_hooks; |
80 | 0 | tmp = pkg_realloc( |
81 | 0 | nonsip_hooks, new_max_hooks * sizeof(struct nonsip_hook)); |
82 | 0 | if(tmp == 0) { |
83 | 0 | goto error; |
84 | 0 | } |
85 | 0 | nonsip_hooks = tmp; |
86 | | /* init the new chunk */ |
87 | 0 | memset(&nonsip_hooks[last_hook_idx + 1], 0, |
88 | 0 | (new_max_hooks - nonsip_max_hooks - 1) |
89 | 0 | * sizeof(struct nonsip_hook)); |
90 | 0 | nonsip_max_hooks = new_max_hooks; |
91 | 0 | } |
92 | 0 | nonsip_hooks[last_hook_idx] = *h; |
93 | 0 | last_hook_idx++; |
94 | 0 | return 0; |
95 | 0 | error: |
96 | 0 | return -1; |
97 | 0 | } |
98 | | |
99 | | |
100 | | int nonsip_msg_run_hooks(struct sip_msg *msg) |
101 | 0 | { |
102 | 0 | int r; |
103 | 0 | int ret; |
104 | |
|
105 | 0 | ret = NONSIP_MSG_DROP; /* default, if no hook installed, drop */ |
106 | 0 | for(r = 0; r < last_hook_idx; r++) { |
107 | 0 | ret = nonsip_hooks[r].on_nonsip_req(msg); |
108 | 0 | if(ret != NONSIP_MSG_PASS) |
109 | 0 | break; |
110 | 0 | } |
111 | 0 | return ret; |
112 | 0 | } |