core: updates to kemi callbacks for core routing blocks
[sip-router] / receive.c
1 /*
2  * Copyright (C) 2001-2003 FhG Fokus
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 /*!
23  * \file
24  * \brief Kamailio core ::
25  * \ingroup core
26  * Module: \ref core
27  */
28
29
30 #include <string.h>
31 #include <stdlib.h>
32 #include <sys/time.h>
33
34 #include "receive.h"
35 #include "globals.h"
36 #include "dprint.h"
37 #include "route.h"
38 #include "parser/msg_parser.h"
39 #include "forward.h"
40 #include "action.h"
41 #include "mem/mem.h"
42 #include "stats.h"
43 #include "ip_addr.h"
44 #include "script_cb.h"
45 #include "nonsip_hooks.h"
46 #include "dset.h"
47 #include "usr_avp.h"
48 #ifdef WITH_XAVP
49 #include "xavp.h"
50 #endif
51 #include "select_buf.h"
52
53 #include "tcp_server.h" /* for tcpconn_add_alias */
54 #include "tcp_options.h" /* for access to tcp_accept_aliases*/
55 #include "cfg/cfg.h"
56 #include "core_stats.h"
57 #include "kemi.h"
58
59 #ifdef DEBUG_DMALLOC
60 #include <mem/dmalloc.h>
61 #endif
62
63 unsigned int msg_no=0;
64 /* address preset vars */
65 str default_global_address={0,0};
66 str default_global_port={0,0};
67 str default_via_address={0,0};
68 str default_via_port={0,0};
69
70 /**
71  * increment msg_no and return the new value
72  */
73 unsigned int inc_msg_no(void)
74 {
75         return ++msg_no;
76 }
77
78 /**
79  *
80  */
81 int sip_check_fline(char* buf, unsigned int len)
82 {
83         char *p;
84         int m;
85
86         m = 0;
87         for(p=buf; p<buf+len; p++) {
88                 /* first check if is a reply - starts with SIP/2.0 */
89                 if(m==0) {
90                         if(*p==' ' || *p=='\t' || *p=='\r' || *p=='\n') continue;
91                         if(buf+len-p<10) return -1;
92                         if(strncmp(p, "SIP/2.0 ", 8)==0) {
93                                 LM_DBG("first line indicates a SIP reply\n");
94                                 return 0;
95                         }
96                         m=1;
97                 } else {
98                         /* check if a request - before end of first line is SIP/2.0 */
99                         if(*p!='\r' && *p!='\n') continue;
100                         if(p-10>=buf) {
101                                 if(strncmp(p-8, " SIP/2.0", 8)==0) {
102                                         LM_DBG("first line indicates a SIP request\n");
103                                         return 0;
104                                 }
105                         }
106                         return -1;
107                 }
108         }
109         return -1;
110 }
111
112 /** Receive message
113  *  WARNING: buf must be 0 terminated (buf[len]=0) or some things might
114  * break (e.g.: modules/textops)
115  */
116 int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
117 {
118         struct sip_msg* msg;
119         struct run_act_ctx ctx;
120         struct run_act_ctx *bctx;
121         int ret;
122 #ifdef STATS
123         int skipped = 1;
124         int stats_on = 1;
125 #else
126         int stats_on = 0;
127 #endif
128         struct timeval tvb, tve;
129         struct timezone tz;
130         unsigned int diff = 0;
131         str inb;
132         sr_net_info_t netinfo;
133         sr_kemi_eng_t *keng = NULL;
134
135         if(sr_event_enabled(SREV_NET_DATA_RECV)) {
136                 if(sip_check_fline(buf, len)==0) {
137                         memset(&netinfo, 0, sizeof(sr_net_info_t));
138                         netinfo.data.s = buf;
139                         netinfo.data.len = len;
140                         netinfo.rcv = rcv_info;
141                         sr_event_exec(SREV_NET_DATA_RECV, (void*)&netinfo);
142                 }
143         }
144
145         inb.s = buf;
146         inb.len = len;
147         sr_event_exec(SREV_NET_DATA_IN, (void*)&inb);
148         len = inb.len;
149
150         msg=pkg_malloc(sizeof(struct sip_msg));
151         if (msg==0) {
152                 LM_ERR("no mem for sip_msg\n");
153                 goto error00;
154         }
155         msg_no++;
156         /* number of vias parsed -- good for diagnostic info in replies */
157         via_cnt=0;
158
159         memset(msg,0, sizeof(struct sip_msg)); /* init everything to 0 */
160         /* fill in msg */
161         msg->buf=buf;
162         msg->len=len;
163         /* zero termination (termination of orig message bellow not that
164          * useful as most of the work is done with scratch-pad; -jiri  */
165         /* buf[len]=0; */ /* WARNING: zero term removed! */
166         msg->rcv=*rcv_info;
167         msg->id=msg_no;
168         msg->pid=my_pid();
169         msg->set_global_address=default_global_address;
170         msg->set_global_port=default_global_port;
171
172         if(likely(sr_msg_time==1)) msg_set_time(msg);
173
174         if (parse_msg(buf,len, msg)!=0){
175                 if((ret=sr_event_exec(SREV_RCV_NOSIP, (void*)msg))<NONSIP_MSG_DROP) {
176                         LOG(cfg_get(core, core_cfg, corelog),
177                                 "core parsing of SIP message failed (%s:%d/%d)\n",
178                                 ip_addr2a(&msg->rcv.src_ip), (int)msg->rcv.src_port,
179                                 (int)msg->rcv.proto);
180                         sr_core_ert_run(msg, SR_CORE_ERT_RECEIVE_PARSE_ERROR);
181                 }
182                 else if(ret == NONSIP_MSG_DROP) goto error02;
183         }
184
185         parse_headers(msg, HDR_FROM_F|HDR_TO_F|HDR_CALLID_F|HDR_CSEQ_F, 0);
186         LM_DBG("--- received sip message - %s - call-id: [%.*s] - cseq: [%.*s]\n",
187                         (msg->first_line.type==SIP_REQUEST)?"request":"reply",
188                         (msg->callid && msg->callid->body.s)?msg->callid->body.len:0,
189                         (msg->callid && msg->callid->body.s)?msg->callid->body.s:"",
190                         (msg->cseq && msg->cseq->body.s)?msg->cseq->body.len:0,
191                         (msg->cseq && msg->cseq->body.s)?msg->cseq->body.s:"");
192
193         /* set log prefix */
194         log_prefix_set(msg);
195
196         /* ... clear branches from previous message */
197         clear_branches();
198
199         if (msg->first_line.type==SIP_REQUEST){
200                 ruri_mark_new(); /* ruri is usable for forking (not consumed yet) */
201                 if (!IS_SIP(msg)){
202                         if ((ret=nonsip_msg_run_hooks(msg))!=NONSIP_MSG_ACCEPT){
203                                 if (unlikely(ret==NONSIP_MSG_ERROR))
204                                         goto error03;
205                                 goto end; /* drop the message */
206                         }
207                 }
208                 /* sanity checks */
209                 if ((msg->via1==0) || (msg->via1->error!=PARSE_OK)){
210                         /* no via, send back error ? */
211                         LM_ERR("no via found in request\n");
212                         STATS_BAD_MSG();
213                         goto error02;
214                 }
215                 /* check if necessary to add receive?->moved to forward_req */
216                 /* check for the alias stuff */
217 #ifdef USE_TCP
218                 if (msg->via1->alias && cfg_get(tcp, tcp_cfg, accept_aliases) &&
219                                 (((rcv_info->proto==PROTO_TCP) && !tcp_disable)
220 #ifdef USE_TLS
221                                         || ((rcv_info->proto==PROTO_TLS) && !tls_disable)
222 #endif
223                                 )
224                         ){
225                         if (tcpconn_add_alias(rcv_info->proto_reserved1, msg->via1->port,
226                                                                         rcv_info->proto)!=0){
227                                 LM_ERR("tcp alias failed\n");
228                                 /* continue */
229                         }
230                 }
231 #endif
232
233         /*      skip: */
234                 LM_DBG("preparing to run routing scripts...\n");
235                 if(is_printable(cfg_get(core, core_cfg, latency_cfg_log))
236                                 || stats_on==1) {
237                         gettimeofday( & tvb, &tz );
238                 }
239                 /* execute pre-script callbacks, if any; -jiri */
240                 /* if some of the callbacks said not to continue with
241                  * script processing, don't do so
242                  * if we are here basic sanity checks are already done
243                  * (like presence of at least one via), so you can count
244                  * on via1 being parsed in a pre-script callback --andrei
245                 */
246                 if (exec_pre_script_cb(msg, REQUEST_CB_TYPE)==0 )
247                 {
248                         STATS_REQ_FWD_DROP();
249                         goto end; /* drop the request */
250                 }
251
252                 set_route_type(REQUEST_ROUTE);
253                 /* exec the routing script */
254                 if(unlikely(main_rt.rlist[DEFAULT_RT]==NULL)) {
255                         keng = sr_kemi_eng_get();
256                         if(keng==NULL) {
257                                 LM_ERR("no config routing engine registered\n");
258                                 goto error_req;
259                         }
260                         if(keng->froute(msg, REQUEST_ROUTE, NULL, NULL)<0) {
261                                 LM_NOTICE("negative return code from engine function\n");
262                         }
263                 } else {
264                         if (run_top_route(main_rt.rlist[DEFAULT_RT], msg, 0)<0){
265                                 LM_WARN("error while trying script\n");
266                                 goto error_req;
267                         }
268                 }
269
270                 if(is_printable(cfg_get(core, core_cfg, latency_cfg_log))
271                                 || stats_on==1) {
272                         gettimeofday( & tve, &tz );
273                         diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
274                         LOG(cfg_get(core, core_cfg, latency_cfg_log),
275                                         "request-route executed in: %d usec\n", diff);
276 #ifdef STATS
277                         stats->processed_requests++;
278                         stats->acc_req_time += diff;
279                         STATS_RX_REQUEST( msg->first_line.u.request.method_value );
280 #endif
281                 }
282
283                 /* execute post request-script callbacks */
284                 exec_post_script_cb(msg, REQUEST_CB_TYPE);
285         }else if (msg->first_line.type==SIP_REPLY){
286                 /* sanity checks */
287                 if ((msg->via1==0) || (msg->via1->error!=PARSE_OK)){
288                         /* no via, send back error ? */
289                         LM_ERR("no via found in reply\n");
290                         STATS_BAD_RPL();
291                         goto error02;
292                 }
293
294                 if(is_printable(cfg_get(core, core_cfg, latency_cfg_log))
295                                 || stats_on==1) {
296                         gettimeofday( & tvb, &tz );
297                 }
298 #ifdef STATS
299                 STATS_RX_RESPONSE ( msg->first_line.u.reply.statuscode / 100 );
300 #endif
301
302                 /* execute pre-script callbacks, if any; -jiri */
303                 /* if some of the callbacks said not to continue with
304                  * script processing, don't do so
305                  * if we are here basic sanity checks are already done
306                  * (like presence of at least one via), so you can count
307                  * on via1 being parsed in a pre-script callback --andrei
308                 */
309                 if (exec_pre_script_cb(msg, ONREPLY_CB_TYPE)==0 )
310                 {
311                         STATS_RPL_FWD_DROP();
312                         goto end; /* drop the reply */
313                 }
314
315                 /* exec the onreply routing script */
316                 keng = sr_kemi_eng_get();
317                 if (onreply_rt.rlist[DEFAULT_RT]!=NULL || keng!=NULL){
318                         set_route_type(CORE_ONREPLY_ROUTE);
319                         ret = 1;
320                         if(unlikely(keng!=NULL)) {
321                                 bctx = sr_kemi_act_ctx_get();
322                                 init_run_actions_ctx(&ctx);
323                                 sr_kemi_act_ctx_set(&ctx);
324                                 ret = keng->froute(msg, CORE_ONREPLY_ROUTE, NULL, NULL);
325                                 sr_kemi_act_ctx_set(bctx);
326                         } else {
327                                 ret=run_top_route(onreply_rt.rlist[DEFAULT_RT], msg, &ctx);
328                         }
329 #ifndef NO_ONREPLY_ROUTE_ERROR
330                         if (unlikely(ret<0)){
331                                 LM_WARN("error while trying onreply script\n");
332                                 goto error_rpl;
333                         }else
334 #endif /* NO_ONREPLY_ROUTE_ERROR */
335                         if (unlikely(ret==0 || (ctx.run_flags&DROP_R_F))){
336                                 STATS_RPL_FWD_DROP();
337                                 goto skip_send_reply; /* drop the message, no error */
338                         }
339                 }
340                 /* send the msg */
341                 forward_reply(msg);
342         skip_send_reply:
343                 if(is_printable(cfg_get(core, core_cfg, latency_cfg_log))
344                                 || stats_on==1) {
345                         gettimeofday( & tve, &tz );
346                         diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
347                         LOG(cfg_get(core, core_cfg, latency_cfg_log),
348                                         "reply-route executed in: %d usec\n", diff);
349 #ifdef STATS
350                         stats->processed_responses++;
351                         stats->acc_res_time+=diff;
352 #endif
353                 }
354
355                 /* execute post reply-script callbacks */
356                 exec_post_script_cb(msg, ONREPLY_CB_TYPE);
357         }
358
359 end:
360 #ifdef STATS
361         skipped = 0;
362 #endif
363         /* free possible loaded avps -bogdan */
364         reset_avps();
365 #ifdef WITH_XAVP
366         xavp_reset_list();
367 #endif
368         LM_DBG("cleaning up\n");
369         free_sip_msg(msg);
370         pkg_free(msg);
371 #ifdef STATS
372         if (skipped) STATS_RX_DROPS;
373 #endif
374         /* reset log prefix */
375         log_prefix_set(NULL);
376         return 0;
377
378 #ifndef NO_ONREPLY_ROUTE_ERROR
379 error_rpl:
380         /* execute post reply-script callbacks */
381         exec_post_script_cb(msg, ONREPLY_CB_TYPE);
382         reset_avps();
383 #ifdef WITH_XAVP
384         xavp_reset_list();
385 #endif
386         goto error02;
387 #endif /* NO_ONREPLY_ROUTE_ERROR */
388 error_req:
389         LM_DBG("error:...\n");
390         /* execute post request-script callbacks */
391         exec_post_script_cb(msg, REQUEST_CB_TYPE);
392 error03:
393         /* free possible loaded avps -bogdan */
394         reset_avps();
395 #ifdef WITH_XAVP
396         xavp_reset_list();
397 #endif
398 error02:
399         free_sip_msg(msg);
400         pkg_free(msg);
401 error00:
402         STATS_RX_DROPS;
403         /* reset log prefix */
404         log_prefix_set(NULL);
405         return -1;
406 }
407