void route_timer_f(unsigned int ticks, void* param) { struct action* a = (struct action*)param; static struct sip_msg* req= NULL; if(req == NULL) { req = (struct sip_msg*)pkg_malloc(sizeof(struct sip_msg)); if(req == NULL) { LM_ERR("No more memory\n"); return; } memset(req, 0, sizeof(struct sip_msg)); req->first_line.type = SIP_REQUEST; req->first_line.u.request.method.s= "DUMMY"; req->first_line.u.request.method.len= 5; req->first_line.u.request.uri.s= "sip:[email protected]"; req->first_line.u.request.uri.len= 19; } if(a == NULL) { LM_ERR("NULL action\n"); return; } run_top_route(a, req); /* clean whatever extra structures were added by script functions */ free_sip_msg(req); /* remove all added AVP - here we use all the time the default AVP list */ reset_avps( ); }
/* WARNING: buf must be 0 terminated (buf[len]=0) or some things might * break (e.g.: modules/textops) */ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info) { struct sip_msg* msg; struct run_act_ctx ctx; int ret; #ifdef STATS int skipped = 1; struct timeval tvb, tve; struct timezone tz; unsigned int diff; #endif str inb; inb.s = buf; inb.len = len; sr_event_exec(SREV_NET_DATA_IN, (void*)&inb); len = inb.len; msg=pkg_malloc(sizeof(struct sip_msg)); if (msg==0) { LOG(L_ERR, "ERROR: receive_msg: no mem for sip_msg\n"); goto error00; } msg_no++; /* number of vias parsed -- good for diagnostic info in replies */ via_cnt=0; memset(msg,0, sizeof(struct sip_msg)); /* init everything to 0 */ /* fill in msg */ msg->buf=buf; msg->len=len; /* zero termination (termination of orig message bellow not that 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; msg->pid=my_pid(); msg->set_global_address=default_global_address; msg->set_global_port=default_global_port; if(likely(sr_msg_time==1)) msg_set_time(msg); if (parse_msg(buf,len, msg)!=0){ if(sr_event_exec(SREV_RCV_NOSIP, (void*)msg)!=0) { LOG(cfg_get(core, core_cfg, corelog), "core parsing of SIP message failed (%s:%d/%d)\n", ip_addr2a(&msg->rcv.src_ip), (int)msg->rcv.src_port, (int)msg->rcv.proto); sr_core_ert_run(msg, SR_CORE_ERT_RECEIVE_PARSE_ERROR); } goto error02; } DBG("After parse_msg...\n"); /* ... clear branches from previous message */ clear_branches(); if (msg->first_line.type==SIP_REQUEST){ ruri_mark_new(); /* ruri is usable for forking (not consumed yet) */ 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"); STATS_BAD_MSG(); goto error02; } /* check if necessary to add receive?->moved to forward_req */ /* check for the alias stuff */ #ifdef USE_TCP 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) #endif ) ){ if (tcpconn_add_alias(rcv_info->proto_reserved1, msg->via1->port, rcv_info->proto)!=0){ LOG(L_ERR, " ERROR: receive_msg: tcp alias failed\n"); /* continue */ } } #endif /* skip: */ DBG("preparing to run routing scripts...\n"); #ifdef STATS gettimeofday( & tvb, &tz ); #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, REQUEST_CB_TYPE)==0 ) { STATS_REQ_FWD_DROP(); 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)<0){ LOG(L_WARN, "WARNING: receive_msg: " "error while trying script\n"); 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("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"); STATS_BAD_RPL(); goto error02; } #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 ) { STATS_RPL_FWD_DROP(); goto end; /* drop the reply */ } /* exec the onreply routing script */ if (onreply_rt.rlist[DEFAULT_RT]){ set_route_type(CORE_ONREPLY_ROUTE); ret=run_top_route(onreply_rt.rlist[DEFAULT_RT], msg, &ctx); #ifndef NO_ONREPLY_ROUTE_ERROR if (unlikely(ret<0)){ LOG(L_WARN, "WARNING: receive_msg: " "error while trying onreply script\n"); goto error_rpl; }else #endif /* NO_ONREPLY_ROUTE_ERROR */ if (unlikely(ret==0 || (ctx.run_flags&DROP_R_F))){ STATS_RPL_FWD_DROP(); 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("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 /* free possible loaded avps -bogdan */ reset_avps(); #ifdef WITH_XAVP xavp_reset_list(); #endif DBG("receive_msg: cleaning up\n"); free_sip_msg(msg); pkg_free(msg); #ifdef STATS if (skipped) STATS_RX_DROPS; #endif return 0; #ifndef NO_ONREPLY_ROUTE_ERROR error_rpl: /* execute post reply-script callbacks */ exec_post_script_cb(msg, ONREPLY_CB_TYPE); reset_avps(); #ifdef WITH_XAVP xavp_reset_list(); #endif goto error02; #endif /* NO_ONREPLY_ROUTE_ERROR */ 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(); #ifdef WITH_XAVP xavp_reset_list(); #endif error02: free_sip_msg(msg); pkg_free(msg); error00: STATS_RX_DROPS; return -1; }
/* WARNING: buf must be 0 terminated (buf[len]=0) or some things might * break (e.g.: modules/textops) */ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info) { struct sip_msg* msg; int ret; #ifdef STATS int skipped = 1; struct timeval tvb, tve; struct timezone tz; unsigned int diff; #endif msg=pkg_malloc(sizeof(struct sip_msg)); if (msg==0) { LOG(L_ERR, "ERROR: receive_msg: no mem for sip_msg\n"); goto error00; } msg_no++; /* number of vias parsed -- good for diagnostic info in replies */ via_cnt=0; memset(msg,0, sizeof(struct sip_msg)); /* init everything to 0 */ /* fill in msg */ 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 */ /* buf[len]=0; */ /* WARNING: zero term removed! */ msg->rcv=*rcv_info; msg->id=msg_no; msg->set_global_address=default_global_address; msg->set_global_port=default_global_port; if (parse_msg(buf,len, msg)!=0) { LOG(L_ERR, "ERROR: receive_msg: parse_msg failed\n"); goto error02; } DBG("After parse_msg...\n"); /* ... clear branches from previous message */ clear_branches(); if (msg->first_line.type==SIP_REQUEST) { /* 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; } /* check if neccesarry to add receive?->moved to forward_req */ DBG("preparing to run routing scripts...\n"); #ifdef STATS gettimeofday( & tvb, &tz ); #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 */ ret=exec_pre_cb(msg); if (ret<=0) { if (ret<0) goto error; else goto end; /* drop the message -- no error -- andrei */ } /* exec the routing script */ if (run_actions(rlist[0], msg)<0) { LOG(L_WARN, "WARNING: receive_msg: " "error while trying script\n"); goto error; } #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); STATS_RX_REQUEST( msg->first_line.u.request.method_value ); #endif } 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; } /* 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 */ ret=exec_pre_cb(msg); if (ret<=0) { if (ret<0) goto error; else goto end; /* drop the message -- no error -- andrei */ } /* send the msg */ forward_reply(msg); #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); #endif } 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); #ifdef STATS if (skipped) STATS_RX_DROPS; #endif return 0; error: DBG("error:...\n"); /* execute post-script callbacks, if any; -jiri */ exec_post_cb(msg); /* free possible loaded avps -bogdan */ reset_avps(); error02: free_sip_msg(msg); pkg_free(msg); error00: STATS_RX_DROPS; return -1; }
/** * This function cancels a previously created local invite * transaction. The cancel parameter should NOT have any via (CANCEL is * hop by hop). returns 0 if error return >0 if OK (returns the LABEL of * the cancel).*/ unsigned int t_uac_cancel( str *headers, str *body, unsigned int cancelled_hashIdx, unsigned int cancelled_label, transaction_cb cb, void* cbp) { struct cell *t_invite,*cancel_cell; struct retr_buf *cancel,*invite; unsigned int len,ret; char *buf; ret=0; if(t_lookup_ident(&t_invite,cancelled_hashIdx,cancelled_label)<0){ LM_ERR("failed to t_lookup_ident hash_idx=%d," "label=%d\n", cancelled_hashIdx,cancelled_label); return 0; } /* <sanity_checks> */ if(! is_local(t_invite)) { LM_ERR("tried to cancel a non-local transaction\n"); goto error3; } if(t_invite->uac[0].last_received < 100) { LM_WARN("trying to cancel a transaction not in " "Proceeding state !\n"); goto error3; } if(t_invite->uac[0].last_received > 200) { LM_WARN("trying to cancel a completed transaction !\n"); goto error3; } /* </sanity_checks*/ /* <build_cell> */ if(!(cancel_cell = build_cell(0))){ ret=0; LM_ERR("no more shm memory!\n"); goto error3; } reset_avps(); if(cb && insert_tmcb(&(cancel_cell->tmcb_hl), TMCB_RESPONSE_IN|TMCB_LOCAL_COMPLETED,cb,cbp,0)!=1){ ret=0; LM_ERR("short of tmcb shmem !\n"); goto error2; } /* </build_cell> */ /* <insert_into_hashtable> */ cancel_cell->flags |= T_IS_LOCAL_FLAG; cancel_cell->hash_index=t_invite->hash_index; LOCK_HASH(cancel_cell->hash_index); insert_into_hash_table_unsafe(cancel_cell,cancel_cell->hash_index); ret=cancel_cell->label; cancel_cell->label=t_invite->label; UNLOCK_HASH(cancel_cell->hash_index); /* </insert_into_hashtable> */ /* <prepare_cancel> */ cancel=&cancel_cell->uac[0].request; invite=&t_invite->uac[0].request; cancel->dst.to = invite->dst.to; cancel->dst.send_sock = invite->dst.send_sock; cancel->dst.proto = invite->dst.proto; //cancel->dst.proto_reserved1 = invite->dst.proto_reserved1; if(!(buf = build_uac_cancel(headers,body,t_invite,0,&len, &(cancel->dst)))){ ret=0; LM_ERR("attempt to build a CANCEL failed\n"); goto error1; } cancel->buffer=buf; cancel->buffer_len=len; cancel_cell->method.s = buf; cancel_cell->method.len = 6 /*c-a-n-c-e-l*/; /* </prepare_cancel> */ /* <strart_sending> */ cancel_cell->nr_of_outgoings++; if (SEND_BUFFER(cancel)==-1) { ret=0; LM_ERR("send failed\n"); goto error1; } start_retr(cancel); /* </start_sending> */ return ret; error1: LOCK_HASH(cancel_cell->hash_index); remove_from_hash_table_unsafe(cancel_cell); UNLOCK_HASH(cancel_cell->hash_index); error2: free_cell(cancel_cell); error3: return ret; }
int async_script_launch(struct sip_msg *msg, struct action* a, int report_route) { struct sip_msg req; async_launch_ctx *ctx; int fd; /* run the function (the action) and get back from it the FD, * resume function and param */ if ( a->type!=AMODULE_T || a->elem[0].type!=ACMD_ST || a->elem[0].u.data==NULL ) { LM_CRIT("BUG - invalid action for async I/O - it must be" " a MODULE_T ACMD_ST \n"); return -1; } if ( (ctx=shm_malloc(sizeof(async_launch_ctx)))==NULL) { LM_ERR("failed to allocate new ctx, forcing sync mode\n"); return -1; } async_status = ASYNC_NO_IO; /*assume defauly status "no IO done" */ return_code = ((acmd_export_t*)(a->elem[0].u.data))->function(msg, (async_ctx*)ctx, (char*)a->elem[1].u.data, (char*)a->elem[2].u.data, (char*)a->elem[3].u.data, (char*)a->elem[4].u.data, (char*)a->elem[5].u.data, (char*)a->elem[6].u.data ); /* what to do now ? */ if (async_status>=0) { /* async I/O was successfully launched */ fd = async_status; } else if (async_status==ASYNC_NO_FD) { /* async was successfully launched but without a FD resume * in this case, we need to push the async ctx back to the * function, so it can trigger the resume later, by itself */ } else if (async_status==ASYNC_NO_IO) { /* no IO, so simply continue with the script */ shm_free(ctx); return 1; } else if (async_status==ASYNC_SYNC) { /* IO already done in SYNC'ed way */ goto report; } else if (async_status==ASYNC_CHANGE_FD) { LM_ERR("Incorrect ASYNC_CHANGE_FD status usage!" "You should use this status only from the" "resume function in case something went wrong" "and you have other alternatives!\n"); shm_free(ctx); return -1; } else { /* generic error, go for resume route, report it to script */ shm_free(ctx); return -1; } /* ctx is to be used from this point further */ ctx->report_route = report_route; if (async_status!=ASYNC_NO_FD) { LM_DBG("placing launch job into reactor\n"); /* place the FD + resume function (as param) into reactor */ if (reactor_add_reader(fd,F_LAUNCH_ASYNC,RCT_PRIO_ASYNC,(void*)ctx)<0){ LM_ERR("failed to add async FD to reactor -> act in sync mode\n"); goto sync; } } /* done, return to the script */ return 1; sync: /* run the resume function */ LM_DBG("running launch job in sync mode\n"); do { async_status = ASYNC_DONE; return_code = ((async_resume_module*)(ctx->async.resume_f)) ( fd, msg, ctx->async.resume_param ); if (async_status == ASYNC_CHANGE_FD) fd = return_code; } while(async_status==ASYNC_CONTINUE||async_status==ASYNC_CHANGE_FD); /* the IO completed, so report now */ report: shm_free(ctx); if (report_route==-1) return 1; /* run the report route inline */ init_dummy_request( req ); set_route_type( REQUEST_ROUTE ); run_top_route( rlist[report_route].a, &req); /* remove all added AVP */ reset_avps( ); return 1; }
int async_launch_resume(int *fd, void *param) { struct sip_msg req; async_launch_ctx *ctx = (async_launch_ctx *)param; LM_DBG("resume for a launch job\n"); init_dummy_request( req ); async_status = ASYNC_DONE; /* assume default status as done */ /* call the resume function in order to read and handle data */ return_code = ((async_resume_module*)(ctx->async.resume_f)) ( *fd, &req, ctx->async.resume_param ); if (async_status==ASYNC_CONTINUE) { /* do not run the report route, leave the fd into the reactor*/ goto restore; } else if (async_status==ASYNC_DONE_NO_IO) { /* don't do any change on the fd, since the module handled everything*/ goto run_route; } else if (async_status==ASYNC_CHANGE_FD) { if (return_code<0) { LM_ERR("ASYNC_CHANGE_FD: given file descriptor must be " "positive!\n"); goto restore; } else if (return_code>0 && return_code==*fd) { /*trying to add the same fd; shall continue*/ LM_CRIT("You are trying to replace the old fd with the same fd!" "Will act as in ASYNC_CONTINUE!\n"); goto restore; } /* remove the old fd from the reactor */ reactor_del_reader( *fd, -1, IO_FD_CLOSING); *fd=return_code; /* insert the new fd inside the reactor */ if (reactor_add_reader( *fd, F_LAUNCH_ASYNC, RCT_PRIO_ASYNC, (void*)ctx)<0 ) { LM_ERR("failed to add async FD to reactor -> act in sync mode\n"); do { async_status = ASYNC_DONE; return_code = ((async_resume_module*)(ctx->async.resume_f)) ( *fd, &req, ctx->async.resume_param ); if (async_status == ASYNC_CHANGE_FD) *fd=return_code; } while(async_status==ASYNC_CONTINUE||async_status==ASYNC_CHANGE_FD); goto run_route; } else { /* successfully changed fd */ goto restore; } } /* remove from reactor, we are done */ reactor_del_reader( *fd, -1, IO_FD_CLOSING); run_route: if (async_status == ASYNC_DONE_CLOSE_FD) close(*fd); if (ctx->report_route!=-1) { LM_DBG("runinng report route for a launch job\n"); set_route_type( REQUEST_ROUTE ); run_top_route( rlist[ctx->report_route].a, &req); /* remove all added AVP */ reset_avps( ); } /* no need for the context anymore */ shm_free(ctx); LM_DBG("done with a launch job\n"); restore: /* clean whatever extra structures were added by script functions */ free_sip_msg(&req); return 0; }
static ticks_t timer_handler(ticks_t ticks, struct timer_ln* tl, void* data) { /*?min length of first line of message is 16 char!?*/ #define MSG "GET /timer HTTP/0.9\n\n" struct sip_msg* msg; struct timer_action *a; struct run_act_ctx ra_ctx; a = data; if (!a->disable_itself) { DEBUG(MODULE_NAME": handler: called at %d ticks, timer: '%s', pid:%d\n", ticks, a->timer_name, getpid()); if (a->route_no >= main_rt.idx) { BUG(MODULE_NAME": invalid routing table number #%d of %d\n", a->route_no, main_rt.idx); goto err2; } if (!main_rt.rlist[a->route_no]) { WARN(MODULE_NAME": route not declared (hash:%d)\n", a->route_no); goto err2; } msg=pkg_malloc(sizeof(struct sip_msg)); if (msg==0) { ERR(MODULE_NAME": handler: no mem for sip_msg\n"); goto err2; } timer_msg_no++; memset(msg, 0, sizeof(struct sip_msg)); /* init everything to 0 */ msg->buf=MSG; msg->len=sizeof(MSG)-1; msg->rcv= rcv_info; msg->id=timer_msg_no; msg->set_global_address=default_global_address; msg->set_global_port=default_global_port; if (parse_msg(msg->buf, msg->len, msg)!=0){ ERR(MODULE_NAME": handler: parse_msg failed\n"); goto err; } /* ... clear branches from previous message */ clear_branches(); reset_static_buffer(); if (exec_pre_req_cb(msg)==0 ) goto end; /* drop the request */ /* exec the routing script */ timer_executed = a; init_run_actions_ctx(&ra_ctx); run_actions(&ra_ctx, main_rt.rlist[a->route_no], msg); timer_executed = 0; /* execute post request-script callbacks */ exec_post_req_cb(msg); end: reset_avps(); DEBUG(MODULE_NAME": handler: cleaning up\n"); err: free_sip_msg(msg); pkg_free(msg); err2: ; } /* begin critical section */ if (a->disable_itself) { timer_allow_del(a->link); timer_del(a->link); timer_reinit(a->link); a->disable_itself = 0; /* end critical section */ return 0; /* do no call more */ } else return (ticks_t)(-1); /* periodical */ }
static inline int t_uac_prepare(str* method, str* headers, str* body, dlg_t* dialog, transaction_cb cb, void* cbp, struct retr_buf **dst_req, struct cell **dst_cell) { struct dest_info dst; struct cell *new_cell; struct retr_buf *request; char* buf; int buf_len, ret, flags; unsigned int hi; int is_ack; #ifdef USE_DNS_FAILOVER struct dns_srv_handle dns_h; #endif ret=-1; hi=0; /* make gcc happy */ /*if (dst_req) *dst_req = NULL;*/ is_ack = (((method->len == 3) && (memcmp("ACK", method->s, 3)==0)) ? 1 : 0); /*** added by dcm * - needed by external ua to send a request within a dlg */ if (w_calculate_hooks(dialog)<0 && !dialog->hooks.next_hop) goto error2; if (!dialog->loc_seq.is_set) { /* this is the first request in the dialog, set cseq to default value now - Miklos */ dialog->loc_seq.value = DEFAULT_CSEQ; dialog->loc_seq.is_set = 1; } DBG("DEBUG:tm:t_uac: next_hop=<%.*s>\n",dialog->hooks.next_hop->len, dialog->hooks.next_hop->s); /* it's a new message, so we will take the default socket */ #ifdef USE_DNS_FAILOVER if (use_dns_failover){ dns_srv_handle_init(&dns_h); if ((uri2dst(&dns_h, &dst, 0, dialog->hooks.next_hop, PROTO_NONE)==0) || (dst.send_sock==0)){ dns_srv_handle_put(&dns_h); ser_error = E_NO_SOCKET; ret=ser_error; LOG(L_ERR, "t_uac: no socket found\n"); goto error2; } dns_srv_handle_put(&dns_h); /* not needed anymore */ }else{ if ((uri2dst(0, &dst, 0, dialog->hooks.next_hop, PROTO_NONE)==0) || (dst.send_sock==0)){ ser_error = E_NO_SOCKET; ret=ser_error; LOG(L_ERR, "t_uac: no socket found\n"); goto error2; } } #else if ((uri2dst(&dst, 0, dialog->hooks.next_hop, PROTO_NONE)==0) || (dst.send_sock==0)){ ser_error = E_NO_SOCKET; ret=ser_error; LOG(L_ERR, "t_uac: no socket found\n"); goto error2; } #endif new_cell = build_cell(0); if (!new_cell) { ret=E_OUT_OF_MEM; LOG(L_ERR, "t_uac: short of cell shmem\n"); goto error2; } /* init timers hack, new_cell->fr_timer and new_cell->fr_inv_timer * must be set, or else the fr will happen immediately * we can't call init_new_t() because we don't have a sip msg * => we'll ignore t_set_fr() or avp timer value and will use directly the * module params fr_inv_timer and fr_timer -- andrei */ new_cell->fr_timeout=fr_timeout; new_cell->fr_inv_timeout=fr_inv_timeout; /* better reset avp list now - anyhow, it's useless from * this point (bogdan) */ reset_avps(); /* add the callback the the transaction for LOCAL_COMPLETED event */ flags = TMCB_LOCAL_COMPLETED; /* Add also TMCB_LOCAL_REPLY_OUT if provisional replies are desired */ if (pass_provisional_replies) flags |= TMCB_LOCAL_RESPONSE_OUT; if(cb && insert_tmcb(&(new_cell->tmcb_hl), flags, cb, cbp)!=1){ ret=E_OUT_OF_MEM; LOG(L_ERR, "t_uac: short of tmcb shmem\n"); goto error2; } if (method->len==INVITE_LEN && memcmp(method->s, INVITE, INVITE_LEN)==0) new_cell->flags |= T_IS_INVITE_FLAG; new_cell->flags |= T_IS_LOCAL_FLAG; set_kr(REQ_FWDED); request = &new_cell->uac[0].request; request->dst = dst; if (!is_ack) { hi=dlg2hash(dialog); LOCK_HASH(hi); insert_into_hash_table_unsafe(new_cell, hi); UNLOCK_HASH(hi); } buf = build_uac_req(method, headers, body, dialog, 0, new_cell, &buf_len, &dst); if (!buf) { LOG(L_ERR, "t_uac: Error while building message\n"); ret=E_OUT_OF_MEM; goto error1; } new_cell->method.s = buf; new_cell->method.len = method->len; request->buffer = buf; request->buffer_len = buf_len; new_cell->nr_of_outgoings++; if (dst_req) *dst_req = request; if (dst_cell) *dst_cell = new_cell; return 1; error1: if (!is_ack) { LOCK_HASH(hi); remove_from_hash_table_unsafe(new_cell); UNLOCK_HASH(hi); } free_cell(new_cell); error2: return ret; }
/* * Send a request using data from the dialog structure */ int t_uac(str* method, str* headers, str* body, dlg_t* dialog, transaction_cb cb, void* cbp) { struct socket_info* send_sock; union sockaddr_union to_su; struct cell *new_cell; struct retr_buf *request; char* buf; int buf_len; int ret; unsigned int hi; ret=-1; /*** added by dcm * - needed by external ua to send a request within a dlg */ if(!dialog->hooks.next_hop && w_calculate_hooks(dialog)<0) goto error2; DBG("DEBUG:tm:t_uac: next_hop=<%.*s>\n",dialog->hooks.next_hop->len, dialog->hooks.next_hop->s); /* it's a new message, so we will take the default socket */ send_sock = uri2sock(0, dialog->hooks.next_hop, &to_su, PROTO_NONE); if (!send_sock) { ret=ser_error; LOG(L_ERR, "t_uac: no socket found\n"); goto error2; } new_cell = build_cell(0); if (!new_cell) { ret=E_OUT_OF_MEM; LOG(L_ERR, "t_uac: short of cell shmem\n"); goto error2; } /* better reset avp list now - anyhow, it's useless from * this point (bogdan) */ reset_avps(); /* add the callback the the transaction for LOCAL_COMPLETED event */ if(cb && insert_tmcb(&(new_cell->tmcb_hl),TMCB_LOCAL_COMPLETED,cb,cbp)!=1){ ret=E_OUT_OF_MEM; LOG(L_ERR, "t_uac: short of tmcb shmem\n"); goto error2; } if (method->len==INVITE_LEN && memcmp(method->s, INVITE, INVITE_LEN)==0) new_cell->flags |= T_IS_INVITE_FLAG; new_cell->flags |= T_IS_LOCAL_FLAG; set_kr(REQ_FWDED); request = &new_cell->uac[0].request; request->dst.to = to_su; request->dst.send_sock = send_sock; request->dst.proto = send_sock->proto; request->dst.proto_reserved1 = 0; hi=dlg2hash(dialog); LOCK_HASH(hi); insert_into_hash_table_unsafe(new_cell, hi); UNLOCK_HASH(hi); buf = build_uac_req(method, headers, body, dialog, 0, new_cell, &buf_len, send_sock); if (!buf) { LOG(L_ERR, "t_uac: Error while building message\n"); ret=E_OUT_OF_MEM; goto error1; } new_cell->method.s = buf; new_cell->method.len = method->len; request->buffer = buf; request->buffer_len = buf_len; new_cell->nr_of_outgoings++; if (SEND_BUFFER(request) == -1) { LOG(L_ERR, "t_uac: Attempt to send to '%.*s' failed\n", dialog->hooks.next_hop->len, dialog->hooks.next_hop->s); } start_retr(request); return 1; error1: LOCK_HASH(hi); remove_from_hash_table_unsafe(new_cell); UNLOCK_HASH(hi); free_cell(new_cell); error2: return ret; }
/*! \note WARNING: buf must be 0 terminated (buf[len]=0) or some things might * break (e.g.: modules/textops) */ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info) { struct sip_msg* msg; struct timeval start; int rc; char *tmp; str in_buff; in_buff.len = len; in_buff.s = buf; /* the raw processing callbacks can change the buffer, further use in_buff.s and at the end try to free in_buff.s if changed by callbacks */ run_raw_processing_cb(PRE_RAW_PROCESSING,&in_buff); /* update the length for further processing */ len = in_buff.len; msg=pkg_malloc(sizeof(struct sip_msg)); if (msg==0) { LM_ERR("no pkg mem left for sip_msg\n"); goto error; } msg_no++; /* number of vias parsed -- good for diagnostic info in replies */ via_cnt=0; memset(msg,0, sizeof(struct sip_msg)); /* init everything to 0 */ /* fill in msg */ msg->buf=in_buff.s; msg->len=len; msg->rcv=*rcv_info; msg->id=msg_no; msg->set_global_address=default_global_address; msg->set_global_port=default_global_port; if (parse_msg(in_buff.s,len, msg)!=0){ tmp=ip_addr2a(&(rcv_info->src_ip)); LM_ERR("Unable to parse msg received from [%s:%d]\n", tmp, rcv_info->src_port); /* if a REQUEST msg was detected (first line was succesfully parsed) we should trigger the error route */ if ( msg->first_line.type==SIP_REQUEST && error_rlist.a!=NULL ) run_error_route(msg, 1); goto parse_error; } LM_DBG("After parse_msg...\n"); start_expire_timer(start,execmsgthreshold); /* ... clear branches from previous message */ clear_branches(); if (msg->first_line.type==SIP_REQUEST) { update_stat( rcv_reqs, 1); /* sanity checks */ if ((msg->via1==0) || (msg->via1->error!=PARSE_OK)){ /* no via, send back error ? */ LM_ERR("no via found in request\n"); update_stat( err_reqs, 1); goto parse_error; } /* 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 && (((rcv_info->proto==PROTO_TCP) && !tcp_disable) #ifdef USE_TLS || ((rcv_info->proto==PROTO_TLS) && !tls_disable) #endif ) ){ if (tcpconn_add_alias(rcv_info->proto_reserved1, msg->via1->port, rcv_info->proto)!=0){ LM_ERR("tcp alias failed\n"); /* continue */ } } #endif LM_DBG("preparing to run routing scripts...\n"); /* set request route type --bogdan*/ set_route_type( REQUEST_ROUTE ); /* execute pre-script callbacks, if any; * 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 */ rc = exec_pre_req_cb(msg); if (rc == SCB_DROP_MSG) { update_stat( drp_reqs, 1); goto end; /* drop the message */ } /* exec the routing script */ if (rc & SCB_RUN_TOP_ROUTE) run_top_route(rlist[DEFAULT_RT].a, msg); /* execute post request-script callbacks */ if (rc & SCB_RUN_POST_CBS) exec_post_req_cb(msg); } else if (msg->first_line.type==SIP_REPLY) { update_stat( rcv_rpls, 1); /* sanity checks */ if ((msg->via1==0) || (msg->via1->error!=PARSE_OK)){ /* no via, send back error ? */ LM_ERR("no via found in reply\n"); update_stat( err_rpls, 1); goto parse_error; } /* set reply route type --bogdan*/ set_route_type( ONREPLY_ROUTE ); /* execute pre-script callbacks, if any ; * 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 */ rc = exec_pre_rpl_cb(msg); if (rc == SCB_DROP_MSG) { update_stat( drp_rpls, 1); goto end; /* drop the reply */ } /* exec the onreply routing script */ if (rc & SCB_RUN_TOP_ROUTE && onreply_rlist[DEFAULT_RT].a && (run_top_route(onreply_rlist[DEFAULT_RT].a,msg) & ACT_FL_DROP) && msg->REPLY_STATUS < 200) { LM_DBG("dropping provisional reply %d\n", msg->REPLY_STATUS); update_stat( drp_rpls, 1); goto end; /* drop the message */ } else { /* send the msg */ forward_reply(msg); /* TODO - TX reply stat */ } /* execute post reply-script callbacks */ if (rc & SCB_RUN_POST_CBS) exec_post_rpl_cb(msg); } end: stop_expire_timer( start, execmsgthreshold, "msg processing", msg->buf, msg->len, 0); reset_longest_action_list(execmsgthreshold); /* free possible loaded avps -bogdan */ reset_avps(); LM_DBG("cleaning up\n"); free_sip_msg(msg); pkg_free(msg); if (in_buff.s != buf) pkg_free(in_buff.s); return 0; parse_error: exec_parse_err_cb(msg); free_sip_msg(msg); pkg_free(msg); error: if (in_buff.s != buf) pkg_free(in_buff.s); return -1; }
/** Receive message * WARNING: buf must be 0 terminated (buf[len]=0) or some things might * break (e.g.: modules/textops) */ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info) { struct sip_msg* msg; struct run_act_ctx ctx; struct run_act_ctx *bctx; int ret; #ifdef STATS int skipped = 1; int stats_on = 1; #else int stats_on = 0; #endif struct timeval tvb, tve; struct timezone tz; unsigned int diff = 0; str inb; sr_net_info_t netinfo; sr_kemi_eng_t *keng = NULL; if(sr_event_enabled(SREV_NET_DATA_RECV)) { if(sip_check_fline(buf, len)==0) { memset(&netinfo, 0, sizeof(sr_net_info_t)); netinfo.data.s = buf; netinfo.data.len = len; netinfo.rcv = rcv_info; sr_event_exec(SREV_NET_DATA_RECV, (void*)&netinfo); } } inb.s = buf; inb.len = len; sr_event_exec(SREV_NET_DATA_IN, (void*)&inb); len = inb.len; msg=pkg_malloc(sizeof(struct sip_msg)); if (msg==0) { LM_ERR("no mem for sip_msg\n"); goto error00; } msg_no++; /* number of vias parsed -- good for diagnostic info in replies */ via_cnt=0; memset(msg,0, sizeof(struct sip_msg)); /* init everything to 0 */ /* fill in msg */ msg->buf=buf; msg->len=len; /* zero termination (termination of orig message bellow not that * 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; msg->pid=my_pid(); msg->set_global_address=default_global_address; msg->set_global_port=default_global_port; if(likely(sr_msg_time==1)) msg_set_time(msg); if (parse_msg(buf,len, msg)!=0){ if((ret=sr_event_exec(SREV_RCV_NOSIP, (void*)msg))<NONSIP_MSG_DROP) { LOG(cfg_get(core, core_cfg, corelog), "core parsing of SIP message failed (%s:%d/%d)\n", ip_addr2a(&msg->rcv.src_ip), (int)msg->rcv.src_port, (int)msg->rcv.proto); sr_core_ert_run(msg, SR_CORE_ERT_RECEIVE_PARSE_ERROR); } else if(ret == NONSIP_MSG_DROP) goto error02; } parse_headers(msg, HDR_FROM_F|HDR_TO_F|HDR_CALLID_F|HDR_CSEQ_F, 0); LM_DBG("--- received sip message - %s - call-id: [%.*s] - cseq: [%.*s]\n", (msg->first_line.type==SIP_REQUEST)?"request":"reply", (msg->callid && msg->callid->body.s)?msg->callid->body.len:0, (msg->callid && msg->callid->body.s)?msg->callid->body.s:"", (msg->cseq && msg->cseq->body.s)?msg->cseq->body.len:0, (msg->cseq && msg->cseq->body.s)?msg->cseq->body.s:""); /* set log prefix */ log_prefix_set(msg); /* ... clear branches from previous message */ clear_branches(); if (msg->first_line.type==SIP_REQUEST){ ruri_mark_new(); /* ruri is usable for forking (not consumed yet) */ 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 ? */ LM_ERR("no via found in request\n"); STATS_BAD_MSG(); goto error02; } /* check if necessary to add receive?->moved to forward_req */ /* check for the alias stuff */ #ifdef USE_TCP 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) #endif ) ){ if (tcpconn_add_alias(rcv_info->proto_reserved1, msg->via1->port, rcv_info->proto)!=0){ LM_ERR("tcp alias failed\n"); /* continue */ } } #endif /* skip: */ LM_DBG("preparing to run routing scripts...\n"); if(is_printable(cfg_get(core, core_cfg, latency_cfg_log)) || stats_on==1) { gettimeofday( & tvb, &tz ); } /* 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 ) { STATS_REQ_FWD_DROP(); goto end; /* drop the request */ } set_route_type(REQUEST_ROUTE); /* exec the routing script */ if(unlikely(main_rt.rlist[DEFAULT_RT]==NULL)) { keng = sr_kemi_eng_get(); if(keng==NULL) { LM_ERR("no config routing engine registered\n"); goto error_req; } if(keng->froute(msg, REQUEST_ROUTE, NULL, NULL)<0) { LM_NOTICE("negative return code from engine function\n"); } } else { if (run_top_route(main_rt.rlist[DEFAULT_RT], msg, 0)<0){ LM_WARN("error while trying script\n"); goto error_req; } } if(is_printable(cfg_get(core, core_cfg, latency_cfg_log)) || stats_on==1) { gettimeofday( & tve, &tz ); diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec); LOG(cfg_get(core, core_cfg, latency_cfg_log), "request-route executed in: %d usec\n", diff); #ifdef STATS stats->processed_requests++; stats->acc_req_time += 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 ? */ LM_ERR("no via found in reply\n"); STATS_BAD_RPL(); goto error02; } if(is_printable(cfg_get(core, core_cfg, latency_cfg_log)) || stats_on==1) { gettimeofday( & tvb, &tz ); } #ifdef STATS 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 ) { STATS_RPL_FWD_DROP(); goto end; /* drop the reply */ } /* exec the onreply routing script */ keng = sr_kemi_eng_get(); if (onreply_rt.rlist[DEFAULT_RT]!=NULL || keng!=NULL){ set_route_type(CORE_ONREPLY_ROUTE); ret = 1; if(unlikely(keng!=NULL)) { bctx = sr_kemi_act_ctx_get(); init_run_actions_ctx(&ctx); sr_kemi_act_ctx_set(&ctx); ret = keng->froute(msg, CORE_ONREPLY_ROUTE, NULL, NULL); sr_kemi_act_ctx_set(bctx); } else { ret=run_top_route(onreply_rt.rlist[DEFAULT_RT], msg, &ctx); } #ifndef NO_ONREPLY_ROUTE_ERROR if (unlikely(ret<0)){ LM_WARN("error while trying onreply script\n"); goto error_rpl; }else #endif /* NO_ONREPLY_ROUTE_ERROR */ if (unlikely(ret==0 || (ctx.run_flags&DROP_R_F))){ STATS_RPL_FWD_DROP(); goto skip_send_reply; /* drop the message, no error */ } } /* send the msg */ forward_reply(msg); skip_send_reply: if(is_printable(cfg_get(core, core_cfg, latency_cfg_log)) || stats_on==1) { gettimeofday( & tve, &tz ); diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec); LOG(cfg_get(core, core_cfg, latency_cfg_log), "reply-route executed in: %d usec\n", diff); #ifdef STATS stats->processed_responses++; stats->acc_res_time+=diff; #endif } /* execute post reply-script callbacks */ exec_post_script_cb(msg, ONREPLY_CB_TYPE); } end: #ifdef STATS skipped = 0; #endif /* free possible loaded avps -bogdan */ reset_avps(); #ifdef WITH_XAVP xavp_reset_list(); #endif LM_DBG("cleaning up\n"); free_sip_msg(msg); pkg_free(msg); #ifdef STATS if (skipped) STATS_RX_DROPS; #endif /* reset log prefix */ log_prefix_set(NULL); return 0; #ifndef NO_ONREPLY_ROUTE_ERROR error_rpl: /* execute post reply-script callbacks */ exec_post_script_cb(msg, ONREPLY_CB_TYPE); reset_avps(); #ifdef WITH_XAVP xavp_reset_list(); #endif goto error02; #endif /* NO_ONREPLY_ROUTE_ERROR */ error_req: LM_DBG("error:...\n"); /* execute post request-script callbacks */ exec_post_script_cb(msg, REQUEST_CB_TYPE); error03: /* free possible loaded avps -bogdan */ reset_avps(); #ifdef WITH_XAVP xavp_reset_list(); #endif error02: free_sip_msg(msg); pkg_free(msg); error00: STATS_RX_DROPS; /* reset log prefix */ log_prefix_set(NULL); return -1; }
struct cell* build_cell( struct sip_msg* p_msg ) { struct cell* new_cell; int sip_msg_len; avp_list_t* old; /* allocs a new cell */ new_cell = (struct cell*)shm_malloc( sizeof( struct cell ) ); if ( !new_cell ) { ser_error=E_OUT_OF_MEM; return NULL; } /* filling with 0 */ memset( new_cell, 0, sizeof( struct cell ) ); /* UAS */ new_cell->uas.response.my_T=new_cell; init_rb_timers(&new_cell->uas.response); /* timers */ init_cell_timers(new_cell); old = set_avp_list(AVP_TRACK_FROM | AVP_CLASS_URI, &new_cell->uri_avps_from ); new_cell->uri_avps_from = *old; *old = 0; old = set_avp_list(AVP_TRACK_TO | AVP_CLASS_URI, &new_cell->uri_avps_to ); new_cell->uri_avps_to = *old; *old = 0; old = set_avp_list(AVP_TRACK_FROM | AVP_CLASS_USER, &new_cell->user_avps_from ); new_cell->user_avps_from = *old; *old = 0; old = set_avp_list(AVP_TRACK_TO | AVP_CLASS_USER, &new_cell->user_avps_to ); new_cell->user_avps_to = *old; *old = 0; /* We can just store pointer to domain avps in the transaction context, * because they are read-only */ new_cell->domain_avps_from = get_avp_list(AVP_TRACK_FROM | AVP_CLASS_DOMAIN); new_cell->domain_avps_to = get_avp_list(AVP_TRACK_TO | AVP_CLASS_DOMAIN); /* enter callback, which may potentially want to parse some stuff, * before the request is shmem-ized */ if ( p_msg && has_reqin_tmcbs() ) run_reqin_callbacks( new_cell, p_msg, p_msg->REQ_METHOD); if (p_msg) { /* clean possible previous added vias/clen header or else they would * get propagated in the failure routes */ free_via_clen_lump(&p_msg->add_rm); new_cell->uas.request = sip_msg_cloner(p_msg,&sip_msg_len); if (!new_cell->uas.request) goto error; new_cell->uas.end_request=((char*)new_cell->uas.request)+sip_msg_len; } /* UAC */ init_branches(new_cell); new_cell->relayed_reply_branch = -1; /* new_cell->T_canceled = T_UNDEFINED; */ init_synonym_id(new_cell); init_cell_lock( new_cell ); return new_cell; error: destroy_avp_list(&new_cell->user_avps_from); destroy_avp_list(&new_cell->user_avps_to); destroy_avp_list(&new_cell->uri_avps_from); destroy_avp_list(&new_cell->uri_avps_to); shm_free(new_cell); /* unlink transaction AVP list and link back the global AVP list (bogdan)*/ reset_avps(); return NULL; }
/* WARNING: buf must be 0 terminated (buf[len]=0) or some things might * break (e.g.: modules/textops) */ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info) { struct sip_msg* msg; int ret; #ifdef STATS int skipped = 1; struct timeval tvb, tve; struct timezone tz; unsigned int diff; #endif msg=pkg_malloc(sizeof(struct sip_msg)); if (msg==0) { LOG(L_ERR, "ERROR: receive_msg: no mem for sip_msg\n"); goto error00; } msg_no++; /* number of vias parsed -- good for diagnostic info in replies */ via_cnt=0; memset(msg,0, sizeof(struct sip_msg)); /* init everything to 0 */ /* fill in msg */ msg->buf=buf; msg->len=len; /* zero termination (termination of orig message bellow not that 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; msg->set_global_address=default_global_address; msg->set_global_port=default_global_port; if (parse_msg(buf,len, msg)!=0){ LOG(L_ERR, "ERROR: receive_msg: parse_msg failed\n"); goto error02; } DBG("After parse_msg...\n"); /* ... clear branches from previous message */ clear_branches(); reset_static_buffer(); if (msg->first_line.type==SIP_REQUEST){ if (!IS_SIP(msg)){ if (nonsip_msg_run_hooks(msg)!=NONSIP_MSG_ACCEPT); 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 error02; } /* 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 && (((rcv_info->proto==PROTO_TCP) && !tcp_disable) #ifdef USE_TLS || ((rcv_info->proto==PROTO_TLS) && !tls_disable) #endif ) ){ if (tcpconn_add_alias(rcv_info->proto_reserved1, msg->via1->port, rcv_info->proto)!=0){ LOG(L_ERR, " ERROR: receive_msg: tcp alias failed\n"); /* continue */ } } #endif /* skip: */ DBG("preparing to run routing scripts...\n"); #ifdef STATS gettimeofday( & tvb, &tz ); #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_req_cb(msg)==0 ) goto end; /* drop the request */ /* exec the routing script */ if (run_actions(main_rt.rlist[DEFAULT_RT], msg)<0){ LOG(L_WARN, "WARNING: receive_msg: " "error while trying script\n"); 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("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_req_cb(msg); }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 error02; } #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_rpl_cb(msg)==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); 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("successfully ran reply processing...(%d usec)\n", diff); #endif /* execute post reply-script callbacks */ exec_post_rpl_cb(msg); } end: #ifdef STATS skipped = 0; #endif /* free possible loaded avps -bogdan */ reset_avps(); DBG("receive_msg: cleaning up\n"); free_sip_msg(msg); pkg_free(msg); #ifdef STATS if (skipped) STATS_RX_DROPS; #endif return 0; error_rpl: /* execute post reply-script callbacks */ exec_post_rpl_cb(msg); reset_avps(); goto error02; error_req: DBG("receive_msg: error:...\n"); /* execute post request-script callbacks */ exec_post_req_cb(msg); /* free possible loaded avps -bogdan */ reset_avps(); error02: free_sip_msg(msg); pkg_free(msg); error00: STATS_RX_DROPS; return -1; }