... | ... |
@@ -123,6 +123,7 @@ |
123 | 123 |
#include "hash_func.h" |
124 | 124 |
#include "pt.h" |
125 | 125 |
#include "script_cb.h" |
126 |
+#include "nonsip_hooks.h" |
|
126 | 127 |
#include "ut.h" |
127 | 128 |
#include "signals.h" |
128 | 129 |
#ifdef USE_TCP |
... | ... |
@@ -437,6 +438,7 @@ void cleanup(show_status) |
437 | 438 |
#endif |
438 | 439 |
destroy_timer(); |
439 | 440 |
destroy_script_cb(); |
441 |
+ destroy_nonsip_hooks(); |
|
440 | 442 |
destroy_routes(); |
441 | 443 |
destroy_atomic_ops(); |
442 | 444 |
#ifdef PKG_MALLOC |
... | ... |
@@ -1242,6 +1244,7 @@ int main(int argc, char** argv) |
1242 | 1244 |
} |
1243 | 1245 |
|
1244 | 1246 |
if (init_routes()<0) goto error; |
1247 |
+ if (init_nonsip_hooks()<0) goto error; |
|
1245 | 1248 |
/* fill missing arguments with the default values*/ |
1246 | 1249 |
if (cfg_file==0) cfg_file=CFG_FILE; |
1247 | 1250 |
|
1248 | 1251 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,125 @@ |
1 |
+/* |
|
2 |
+ * $Id$ |
|
3 |
+ * |
|
4 |
+ * Copyright (C) 2006 iptelorg GmbH |
|
5 |
+ * |
|
6 |
+ * This file is part of ser, a free SIP server. |
|
7 |
+ * |
|
8 |
+ * ser is free software; you can redistribute it and/or modify |
|
9 |
+ * it under the terms of the GNU General Public License as published by |
|
10 |
+ * the Free Software Foundation; either version 2 of the License, or |
|
11 |
+ * (at your option) any later version |
|
12 |
+ * |
|
13 |
+ * For a license to use the ser software under conditions |
|
14 |
+ * other than those described here, or to purchase support for this |
|
15 |
+ * software, please contact iptel.org by e-mail at the following addresses: |
|
16 |
+ * info@iptel.org |
|
17 |
+ * |
|
18 |
+ * ser is distributed in the hope that it will be useful, |
|
19 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 |
+ * GNU General Public License for more details. |
|
22 |
+ * |
|
23 |
+ * You should have received a copy of the GNU General Public License |
|
24 |
+ * along with this program; if not, write to the Free Software |
|
25 |
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
26 |
+ */ |
|
27 |
+/* |
|
28 |
+ * non-sip callbacks, called whenever a message with protocol != SIP/2.0 |
|
29 |
+ * is received (the message must have at least a sip like first line or |
|
30 |
+ * else they will be dropped before this callbacks are called |
|
31 |
+ */ |
|
32 |
+/* |
|
33 |
+ * History: |
|
34 |
+ * -------- |
|
35 |
+ * 2006-11-29 created by andrei |
|
36 |
+ */ |
|
37 |
+ |
|
38 |
+#include "nonsip_hooks.h" |
|
39 |
+#include "mem/mem.h" |
|
40 |
+ |
|
41 |
+static struct nonsip_hook* nonsip_hooks; |
|
42 |
+static unsigned int nonsip_max_hooks=MAX_NONSIP_HOOKS; |
|
43 |
+static int last_hook_idx=0; |
|
44 |
+ |
|
45 |
+ |
|
46 |
+ |
|
47 |
+int init_nonsip_hooks() |
|
48 |
+{ |
|
49 |
+ nonsip_hooks=pkg_malloc(nonsip_max_hooks* |
|
50 |
+ sizeof(struct nonsip_hook)); |
|
51 |
+ if (nonsip_hooks==0){ |
|
52 |
+ goto error; |
|
53 |
+ } |
|
54 |
+ memset(nonsip_hooks, 0, nonsip_max_hooks*sizeof(struct nonsip_hook)); |
|
55 |
+ return 0; |
|
56 |
+error: |
|
57 |
+ LOG(L_ERR, "nonsip_hooks: memory allocation failure\n"); |
|
58 |
+ return -1; |
|
59 |
+} |
|
60 |
+ |
|
61 |
+ |
|
62 |
+ |
|
63 |
+void destroy_nonsip_hooks() |
|
64 |
+{ |
|
65 |
+ int r; |
|
66 |
+ |
|
67 |
+ if (nonsip_hooks){ |
|
68 |
+ for (r=0; r<last_hook_idx; r++){ |
|
69 |
+ if (nonsip_hooks[r].destroy) |
|
70 |
+ nonsip_hooks[r].destroy(); |
|
71 |
+ } |
|
72 |
+ pkg_free(nonsip_hooks); |
|
73 |
+ nonsip_hooks=0; |
|
74 |
+ } |
|
75 |
+} |
|
76 |
+ |
|
77 |
+ |
|
78 |
+ |
|
79 |
+/* allocates a new hook |
|
80 |
+ * returns 0 on success and -1 on error */ |
|
81 |
+int register_nonsip_msg_hook(struct nonsip_hook *h) |
|
82 |
+{ |
|
83 |
+ struct nonsip_hook* tmp; |
|
84 |
+ int new_max_hooks; |
|
85 |
+ |
|
86 |
+ if (nonsip_max_hooks==0) |
|
87 |
+ goto error; |
|
88 |
+ if (last_hook_idx >= nonsip_max_hooks){ |
|
89 |
+ new_max_hooks=2*nonsip_max_hooks; |
|
90 |
+ tmp=pkg_realloc(nonsip_hooks, |
|
91 |
+ new_max_hooks*sizeof(struct nonsip_hook)); |
|
92 |
+ if (tmp==0){ |
|
93 |
+ goto error; |
|
94 |
+ } |
|
95 |
+ nonsip_hooks=tmp; |
|
96 |
+ /* init the new chunk */ |
|
97 |
+ memset(&nonsip_hooks[last_hook_idx+1], 0, |
|
98 |
+ (new_max_hooks-nonsip_max_hooks-1)* |
|
99 |
+ sizeof(struct nonsip_hook)); |
|
100 |
+ nonsip_max_hooks=new_max_hooks; |
|
101 |
+ } |
|
102 |
+ nonsip_hooks[last_hook_idx]=*h; |
|
103 |
+ last_hook_idx++; |
|
104 |
+ return 0; |
|
105 |
+error: |
|
106 |
+ return -1; |
|
107 |
+} |
|
108 |
+ |
|
109 |
+ |
|
110 |
+ |
|
111 |
+int nonsip_msg_run_hooks(struct sip_msg* msg) |
|
112 |
+{ |
|
113 |
+ int r; |
|
114 |
+ int ret; |
|
115 |
+ |
|
116 |
+ ret=NONSIP_MSG_DROP; /* default, if no hook installed, drop */ |
|
117 |
+ for (r=0; r<last_hook_idx; r++){ |
|
118 |
+ ret=nonsip_hooks[r].on_nonsip_req(msg); |
|
119 |
+ if (ret!=NONSIP_MSG_PASS) break; |
|
120 |
+ } |
|
121 |
+ return ret; |
|
122 |
+} |
|
123 |
+ |
|
124 |
+ |
|
125 |
+ |
0 | 126 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,67 @@ |
1 |
+/* |
|
2 |
+ * $Id$ |
|
3 |
+ * |
|
4 |
+ * Copyright (C) 2006 iptelorg GmbH |
|
5 |
+ * |
|
6 |
+ * This file is part of ser, a free SIP server. |
|
7 |
+ * |
|
8 |
+ * ser is free software; you can redistribute it and/or modify |
|
9 |
+ * it under the terms of the GNU General Public License as published by |
|
10 |
+ * the Free Software Foundation; either version 2 of the License, or |
|
11 |
+ * (at your option) any later version |
|
12 |
+ * |
|
13 |
+ * For a license to use the ser software under conditions |
|
14 |
+ * other than those described here, or to purchase support for this |
|
15 |
+ * software, please contact iptel.org by e-mail at the following addresses: |
|
16 |
+ * info@iptel.org |
|
17 |
+ * |
|
18 |
+ * ser is distributed in the hope that it will be useful, |
|
19 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 |
+ * GNU General Public License for more details. |
|
22 |
+ * |
|
23 |
+ * You should have received a copy of the GNU General Public License |
|
24 |
+ * along with this program; if not, write to the Free Software |
|
25 |
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
26 |
+ */ |
|
27 |
+/* |
|
28 |
+ * non-sip callbacks, called whenever a message with protocol != SIP/2.0 |
|
29 |
+ * is received (the message must have at least a sip like first line or |
|
30 |
+ * else they will be dropped before this callbacks are called |
|
31 |
+ */ |
|
32 |
+/* |
|
33 |
+ * History: |
|
34 |
+ * -------- |
|
35 |
+ * 2006-11-29 created by andrei |
|
36 |
+ */ |
|
37 |
+ |
|
38 |
+ |
|
39 |
+#ifndef _nonsip_hooks_h |
|
40 |
+#define _nonsip_hooks_h |
|
41 |
+ |
|
42 |
+#include "parser/msg_parser.h" /* sip_msg */ |
|
43 |
+ |
|
44 |
+#define MAX_NONSIP_HOOKS 1 |
|
45 |
+ |
|
46 |
+enum nonsip_msg_returns{ NONSIP_MSG_ERROR=-1, NONSIP_MSG_DROP=0, |
|
47 |
+ NONSIP_MSG_PASS, NONSIP_MSG_ACCEPT }; |
|
48 |
+ |
|
49 |
+struct nonsip_hook{ |
|
50 |
+ char* name; /* must be !=0, it has only "debugging" value */ |
|
51 |
+ /* called each time a sip like request (from the first line point of view) |
|
52 |
+ * with protocol/version != SIP/2.0 is received |
|
53 |
+ * return: 0 - drop message immediately, >0 - continue with other hooks, |
|
54 |
+ * <0 - error (drop message) |
|
55 |
+ */ |
|
56 |
+ int (*on_nonsip_req)(struct sip_msg* msg); |
|
57 |
+ /* called before ser shutdown (last minute cleanups) */ |
|
58 |
+ void (*destroy)(void); |
|
59 |
+}; |
|
60 |
+ |
|
61 |
+ |
|
62 |
+int init_nonsip_hooks(); |
|
63 |
+void destroy_nonsip_hooks(); |
|
64 |
+int register_nonsip_msg_hook(struct nonsip_hook *h); |
|
65 |
+int nonsip_msg_run_hooks(struct sip_msg* msg); |
|
66 |
+ |
|
67 |
+#endif |
... | ... |
@@ -101,6 +101,11 @@ if ( (*tmp==(firstchar) || *tmp==((firstchar) | 32)) && \ |
101 | 101 |
!strncasecmp((req)->first_line.u.request.version.s, \ |
102 | 102 |
HTTP_VERSION, HTTP_VERSION_LEN)) |
103 | 103 |
|
104 |
+#define IS_SIP(req) \ |
|
105 |
+ ((req)->first_line.u.request.version.len >= SIP_VERSION_LEN && \ |
|
106 |
+ !strncasecmp((req)->first_line.u.request.version.s, \ |
|
107 |
+ SIP_VERSION, SIP_VERSION_LEN)) |
|
108 |
+ |
|
104 | 109 |
/* |
105 | 110 |
* Return a URI to which the message should be really sent (not what should |
106 | 111 |
* be in the Request URI. The following fields are tried in this order: |
... | ... |
@@ -37,6 +37,7 @@ |
37 | 37 |
* 2004-04-30 exec_pre_cb is called after basic sanity checks (at least one |
38 | 38 |
* via present & parsed ok) (andrei) |
39 | 39 |
* 2004-08-23 avp core changed - destroy_avp-> reset_avps (bogdan) |
40 |
+ * 2006-11-29 nonsip_msg hooks called for non-sip msg (e.g HTTP) (andrei) |
|
40 | 41 |
*/ |
41 | 42 |
|
42 | 43 |
|
... | ... |
@@ -55,6 +56,7 @@ |
55 | 56 |
#include "stats.h" |
56 | 57 |
#include "ip_addr.h" |
57 | 58 |
#include "script_cb.h" |
59 |
+#include "nonsip_hooks.h" |
|
58 | 60 |
#include "dset.h" |
59 | 61 |
#include "usr_avp.h" |
60 | 62 |
#include "select_buf.h" |
... | ... |
@@ -122,9 +124,12 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info) |
122 | 124 |
reset_static_buffer(); |
123 | 125 |
|
124 | 126 |
if (msg->first_line.type==SIP_REQUEST){ |
127 |
+ if (!IS_SIP(msg)){ |
|
128 |
+ if (nonsip_msg_run_hooks(msg)!=NONSIP_MSG_ACCEPT); |
|
129 |
+ goto end; /* drop the message */ |
|
130 |
+ } |
|
125 | 131 |
/* sanity checks */ |
126 | 132 |
if ((msg->via1==0) || (msg->via1->error!=PARSE_OK)){ |
127 |
- if (IS_HTTP(msg)) goto skip; /* Skip Via tests for HTTP requests */ |
|
128 | 133 |
/* no via, send back error ? */ |
129 | 134 |
LOG(L_ERR, "ERROR: receive_msg: no via found in request\n"); |
130 | 135 |
goto error02; |
... | ... |
@@ -147,7 +152,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info) |
147 | 152 |
} |
148 | 153 |
#endif |
149 | 154 |
|
150 |
- skip: |
|
155 |
+ /* skip: */ |
|
151 | 156 |
DBG("preparing to run routing scripts...\n"); |
152 | 157 |
#ifdef STATS |
153 | 158 |
gettimeofday( & tvb, &tz ); |
... | ... |
@@ -209,7 +214,8 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info) |
209 | 214 |
LOG(L_WARN, "WARNING: receive_msg: " |
210 | 215 |
"error while trying onreply script\n"); |
211 | 216 |
goto error_rpl; |
212 |
- }else if (ret==0) goto skip_send_reply; /* drop the message, no error */ |
|
217 |
+ }else if (ret==0) goto skip_send_reply; /* drop the message, |
|
218 |
+ no error */ |
|
213 | 219 |
} |
214 | 220 |
/* send the msg */ |
215 | 221 |
forward_reply(msg); |