core: build_req_buf_from_sip_req() has a new parameter
[sip-router] / receive.c
index 5ff616d..9f59933 100644 (file)
--- a/receive.c
+++ b/receive.c
@@ -1,7 +1,7 @@
 /* 
  *$Id$
  *
- * Copyright (C) 2001-2003 Fhg Fokus
+ * Copyright (C) 2001-2003 FhG Fokus
  *
  * This file is part of ser, a free SIP server.
  *
  * 2003-02-10 moved zero-term in the calling functions (udp_receive &
  *            tcp_read_req)
  * 2003-08-13 fixed exec_pre_cb returning 0 (backported from stable) (andrei)
+ * 2004-02-06 added user preferences support - destroy_avps() (bogdan)
+ * 2004-04-30 exec_pre_cb is called after basic sanity checks (at least one
+ *            via present & parsed ok)  (andrei)
+ * 2004-08-23 avp core changed - destroy_avp-> reset_avps (bogdan)
+ * 2006-11-29 nonsip_msg hooks called for non-sip msg (e.g HTTP) (andrei)
  */
 
 
 #include "stats.h"
 #include "ip_addr.h"
 #include "script_cb.h"
+#include "nonsip_hooks.h"
 #include "dset.h"
+#include "usr_avp.h"
+#include "select_buf.h"
 
 #include "tcp_server.h" /* for tcpconn_add_alias */
-
+#include "tcp_options.h" /* for access to tcp_accept_aliases*/
+#include "cfg/cfg.h"
 
 #ifdef DEBUG_DMALLOC
 #include <mem/dmalloc.h>
@@ -67,6 +76,13 @@ str default_global_port={0,0};
 str default_via_address={0,0};
 str default_via_port={0,0};
 
+/**
+ * increment msg_no and return the new value
+ */
+unsigned int inc_msg_no(void)
+{
+       return ++msg_no;
+}
 
 
 /* WARNING: buf must be 0 terminated (buf[len]=0) or some things might 
@@ -97,7 +113,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
        msg->buf=buf;
        msg->len=len;
        /* zero termination (termination of orig message bellow not that
-          useful as most of the work is done with scrath-pad; -jiri  */
+          useful as most of the work is done with scratch-pad; -jiri  */
        /* buf[len]=0; */ /* WARNING: zero term removed! */
        msg->rcv=*rcv_info;
        msg->id=msg_no;
@@ -110,30 +126,28 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
        }
        DBG("After parse_msg...\n");
 
-       /* execute pre-script callbacks, if any; -jiri */
-       /* if some of the callbacks said not to continue with
-          script processing, don't do so
-       */
-       ret=exec_pre_cb(msg);
-       if (ret<=0){
-               if (ret<0) goto error;
-               else goto end; /* drop the message -- no error -- andrei */
-       }
 
-       /* ... and clear branches from previous message */
+       /* ... clear branches from previous message */
        clear_branches();
 
        if (msg->first_line.type==SIP_REQUEST){
+               if (!IS_SIP(msg)){
+                       if ((ret=nonsip_msg_run_hooks(msg))!=NONSIP_MSG_ACCEPT){
+                               if (unlikely(ret==NONSIP_MSG_ERROR))
+                                       goto error03;
+                               goto end; /* drop the message */
+                       }
+               }
                /* sanity checks */
                if ((msg->via1==0) || (msg->via1->error!=PARSE_OK)){
                        /* no via, send back error ? */
                        LOG(L_ERR, "ERROR: receive_msg: no via found in request\n");
-                       goto error;
+                       goto error02;
                }
-               /* check if neccesarry to add receive?->moved to forward_req */
+               /* check if necessary to add receive?->moved to forward_req */
                /* check for the alias stuff */
 #ifdef USE_TCP
-               if (msg->via1->alias && tcp_accept_aliases && 
+               if (msg->via1->alias && cfg_get(tcp, tcp_cfg, accept_aliases) && 
                                (((rcv_info->proto==PROTO_TCP) && !tcp_disable)
 #ifdef USE_TLS
                                        || ((rcv_info->proto==PROTO_TLS) && !tls_disable)
@@ -147,66 +161,96 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
                        }
                }
 #endif
-
-               /* exec routing script */
+                       
+       /*      skip: */
                DBG("preparing to run routing scripts...\n");
 #ifdef  STATS
                gettimeofday( & tvb, &tz );
 #endif
-               if (run_actions(rlist[0], msg)<0){
+               /* execute pre-script callbacks, if any; -jiri */
+               /* if some of the callbacks said not to continue with
+                  script processing, don't do so
+                  if we are here basic sanity checks are already done
+                  (like presence of at least one via), so you can count
+                  on via1 being parsed in a pre-script callback --andrei
+               */
+               if (exec_pre_script_cb(msg, REQUEST_CB_TYPE)==0 )
+                       goto end; /* drop the request */
 
+               set_route_type(REQUEST_ROUTE);
+               /* exec the routing script */
+               if (run_top_route(main_rt.rlist[DEFAULT_RT], msg)<0){
                        LOG(L_WARN, "WARNING: receive_msg: "
                                        "error while trying script\n");
-                       goto error;
+                       goto error_req;
                }
 
-
 #ifdef STATS
                gettimeofday( & tve, &tz );
                diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
                stats->processed_requests++;
                stats->acc_req_time += diff;
-               DBG("succesfully ran routing scripts...(%d usec)\n", diff);
+               DBG("successfully ran routing scripts...(%d usec)\n", diff);
                STATS_RX_REQUEST( msg->first_line.u.request.method_value );
 #endif
+
+               /* execute post request-script callbacks */
+               exec_post_script_cb(msg, REQUEST_CB_TYPE);
        }else if (msg->first_line.type==SIP_REPLY){
                /* sanity checks */
                if ((msg->via1==0) || (msg->via1->error!=PARSE_OK)){
                        /* no via, send back error ? */
                        LOG(L_ERR, "ERROR: receive_msg: no via found in reply\n");
-                       goto error;
-               }
-#if 0
-               if ((msg->via2==0) || (msg->via2->error!=PARSE_OK)){
-                       /* no second via => error? */
-                       LOG(L_ERR, "ERROR: receive_msg: no 2nd via found in reply\n");
-                       goto error;
+                       goto error02;
                }
-               /* check if via1 == us */
-#endif
 
 #ifdef STATS
                gettimeofday( & tvb, &tz );
                STATS_RX_RESPONSE ( msg->first_line.u.reply.statuscode / 100 );
 #endif
                
+               /* execute pre-script callbacks, if any; -jiri */
+               /* if some of the callbacks said not to continue with
+                  script processing, don't do so
+                  if we are here basic sanity checks are already done
+                  (like presence of at least one via), so you can count
+                  on via1 being parsed in a pre-script callback --andrei
+               */
+               if (exec_pre_script_cb(msg, ONREPLY_CB_TYPE)==0 )
+                       goto end; /* drop the request */
+
+               /* exec the onreply routing script */
+               if (onreply_rt.rlist[DEFAULT_RT]){
+                       set_route_type(ONREPLY_ROUTE);
+                       ret=run_top_route(onreply_rt.rlist[DEFAULT_RT], msg);
+                       if (ret<0){
+                               LOG(L_WARN, "WARNING: receive_msg: "
+                                               "error while trying onreply script\n");
+                               goto error_rpl;
+                       }else if (ret==0) goto skip_send_reply; /* drop the message, 
+                                                                                                          no error */
+               }
                /* send the msg */
                forward_reply(msg);
-
+       skip_send_reply:
 #ifdef STATS
                gettimeofday( & tve, &tz );
                diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
                stats->processed_responses++;
                stats->acc_res_time+=diff;
-               DBG("succesfully ran reply processing...(%d usec)\n", diff);
+               DBG("successfully ran reply processing...(%d usec)\n", diff);
 #endif
+
+               /* execute post reply-script callbacks */
+               exec_post_script_cb(msg, ONREPLY_CB_TYPE);
        }
+
 end:
 #ifdef STATS
        skipped = 0;
 #endif
-       /* execute post-script callbacks, if any; -jiri */
-       exec_post_cb(msg);
+       /* free possible loaded avps -bogdan */
+       reset_avps();
        DBG("receive_msg: cleaning up\n");
        free_sip_msg(msg);
        pkg_free(msg);
@@ -214,10 +258,18 @@ end:
        if (skipped) STATS_RX_DROPS;
 #endif
        return 0;
-error:
-       DBG("error:...\n");
-       /* execute post-script callbacks, if any; -jiri */
-       exec_post_cb(msg);
+error_rpl:
+       /* execute post reply-script callbacks */
+       exec_post_script_cb(msg, ONREPLY_CB_TYPE);
+       reset_avps();
+       goto error02;
+error_req:
+       DBG("receive_msg: error:...\n");
+       /* execute post request-script callbacks */
+       exec_post_script_cb(msg, REQUEST_CB_TYPE);
+error03:
+       /* free possible loaded avps -bogdan */
+       reset_avps();
 error02:
        free_sip_msg(msg);
        pkg_free(msg);