core: build_req_buf_from_sip_req() has a new parameter
[sip-router] / receive.c
index 11f895c..9f59933 100644 (file)
--- a/receive.c
+++ b/receive.c
@@ -37,6 +37,7 @@
  * 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>
@@ -73,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 
@@ -121,9 +131,15 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
        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)){
-                       if (IS_HTTP(msg)) goto skip; /* Skip Via tests for HTTP requests */
                        /* no via, send back error ? */
                        LOG(L_ERR, "ERROR: receive_msg: no via found in request\n");
                        goto error02;
@@ -131,7 +147,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
                /* 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)
@@ -146,7 +162,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
                }
 #endif
                        
-               skip:
+       /*      skip: */
                DBG("preparing to run routing scripts...\n");
 #ifdef  STATS
                gettimeofday( & tvb, &tz );
@@ -158,11 +174,12 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
                   (like presence of at least one via), so you can count
                   on via1 being parsed in a pre-script callback --andrei
                */
-               if (exec_pre_req_cb(msg)==0 )
+               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_actions(main_rt.rlist[DEFAULT_RT], msg)<0){
+               if (run_top_route(main_rt.rlist[DEFAULT_RT], msg)<0){
                        LOG(L_WARN, "WARNING: receive_msg: "
                                        "error while trying script\n");
                        goto error_req;
@@ -178,7 +195,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
 #endif
 
                /* execute post request-script callbacks */
-               exec_post_req_cb(msg);
+               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)){
@@ -199,20 +216,23 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
                   (like presence of at least one via), so you can count
                   on via1 being parsed in a pre-script callback --andrei
                */
-               if (exec_pre_rpl_cb(msg)==0 )
+               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]){
-                       ret=run_actions(onreply_rt.rlist[DEFAULT_RT], msg);
+                       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 end; /* drop the message, no error */
+                       }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);
@@ -222,7 +242,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
 #endif
 
                /* execute post reply-script callbacks */
-               exec_post_rpl_cb(msg);
+               exec_post_script_cb(msg, ONREPLY_CB_TYPE);
        }
 
 end:
@@ -240,13 +260,14 @@ end:
        return 0;
 error_rpl:
        /* execute post reply-script callbacks */
-       exec_post_rpl_cb(msg);
+       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_req_cb(msg);
+       exec_post_script_cb(msg, REQUEST_CB_TYPE);
+error03:
        /* free possible loaded avps -bogdan */
        reset_avps();
 error02: