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 
init_nonsip_hooks()40 int init_nonsip_hooks()
41 {
42 	nonsip_hooks=pkg_malloc(nonsip_max_hooks*
43 									sizeof(struct nonsip_hook));
44 	if (nonsip_hooks==0){
45 		PKG_MEM_ERROR;
46 		goto error;
47 	}
48 	memset(nonsip_hooks, 0, nonsip_max_hooks*sizeof(struct nonsip_hook));
49 	return 0;
50 error:
51 	PKG_MEM_ERROR;
52 	return -1;
53 }
54 
55 
56 
destroy_nonsip_hooks()57 void destroy_nonsip_hooks()
58 {
59 	int r;
60 
61 	if (nonsip_hooks){
62 		for (r=0; r<last_hook_idx; r++){
63 			if (nonsip_hooks[r].destroy)
64 				nonsip_hooks[r].destroy();
65 		}
66 		pkg_free(nonsip_hooks);
67 		nonsip_hooks=0;
68 	}
69 }
70 
71 
72 
73 /* allocates a new hook
74  * returns 0 on success and -1 on error */
register_nonsip_msg_hook(struct nonsip_hook * h)75 int register_nonsip_msg_hook(struct nonsip_hook *h)
76 {
77 	struct nonsip_hook* tmp;
78 	int new_max_hooks;
79 
80 	if (nonsip_max_hooks==0)
81 		goto error;
82 	if (last_hook_idx >= nonsip_max_hooks){
83 		new_max_hooks=2*nonsip_max_hooks;
84 		tmp=pkg_realloc(nonsip_hooks,
85 				new_max_hooks*sizeof(struct nonsip_hook));
86 		if (tmp==0){
87 			goto error;
88 		}
89 		nonsip_hooks=tmp;
90 		/* init the new chunk */
91 		memset(&nonsip_hooks[last_hook_idx+1], 0,
92 					(new_max_hooks-nonsip_max_hooks-1)*
93 						sizeof(struct nonsip_hook));
94 		nonsip_max_hooks=new_max_hooks;
95 	}
96 	nonsip_hooks[last_hook_idx]=*h;
97 	last_hook_idx++;
98 	return 0;
99 error:
100 	return -1;
101 }
102 
103 
104 
nonsip_msg_run_hooks(struct sip_msg * msg)105 int nonsip_msg_run_hooks(struct sip_msg* msg)
106 {
107 	int r;
108 	int ret;
109 
110 	ret=NONSIP_MSG_DROP; /* default, if no hook installed, drop */
111 	for (r=0; r<last_hook_idx; r++){
112 		ret=nonsip_hooks[r].on_nonsip_req(msg);
113 		if (ret!=NONSIP_MSG_PASS) break;
114 	}
115 	return ret;
116 }
117 
118 
119 
120