Module: sip-router Branch: tmp/build_request Commit: 61f864106e136ab3100717f35511a0a82bcf9668 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=61f86410...
Author: Daniel-Constantin Mierla miconda@gmail.com Committer: Daniel-Constantin Mierla miconda@gmail.com Date: Wed Jul 8 13:10:37 2009 +0200
core: build_req_buf_from_sip_req() has a new parameter
- mode - a set of flags that define how the sip request is printed to buffer - BUILD_NO_LOCAL_VIA (1<<0) - skip adding local via - BUILD_NO_VIA1_UPDATE (1<<1) - skip updating top most via - BUILD_IN_SHM (1<<2) - output buffer allocated in memory - new function inc_msg_no() - increment and return msg_no - new function build_sip_msg_from_buf(...) to parse and partially fill sip_msg structure from a buffer
---
forward.c | 2 +- msg_translator.c | 57 ++++++++++++++++++++++++++++++++++++++++++++--------- msg_translator.h | 9 +++++++- receive.c | 7 ++++++ receive.h | 2 +- 5 files changed, 64 insertions(+), 13 deletions(-)
diff --git a/forward.c b/forward.c index f651120..3a9cb0c 100644 --- a/forward.c +++ b/forward.c @@ -433,7 +433,7 @@ int forward_request(struct sip_msg* msg, str* dst, unsigned short port, #endif if (buf) pkg_free(buf); send_info->proto=proto; - buf = build_req_buf_from_sip_req(msg, &len, send_info); + buf = build_req_buf_from_sip_req(msg, &len, send_info, 0); if (!buf){ LOG(L_ERR, "ERROR: forward_request: building failed\n"); ret=E_OUT_OF_MEM; /* most probable */ diff --git a/msg_translator.c b/msg_translator.c index 2d1e9e5..41878e3 100644 --- a/msg_translator.c +++ b/msg_translator.c @@ -1522,8 +1522,8 @@ error: */ char * build_req_buf_from_sip_req( struct sip_msg* msg, unsigned int *returned_len, - struct dest_info* send_info - ) + struct dest_info* send_info, + unsigned int mode) { unsigned int len, new_len, received_len, rport_len, uri_len, via_len, body_delta; @@ -1561,6 +1561,9 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg, goto error00; }
+ if(unlikely(mode&BUILD_NO_LOCAL_VIA)) + goto after_local_via; + /* create the via header */ branch.s=msg->add_to_branch_s; branch.len=msg->add_to_branch_len; @@ -1571,6 +1574,9 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg, "memory allocation failure\n"); goto error00; } +after_local_via: + if(unlikely(mode&BUILD_NO_VIA1_UPDATE)) + goto after_update_via1; /* check if received needs to be added */ if ( received_test(msg) ) { if ((received_buf=received_builder(msg,&received_len))==0){ @@ -1645,6 +1651,7 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
}
+after_update_via1: /* compute new msg len and fix overlapping zones*/ new_len=len+body_delta+lumps_len(msg, msg->add_rm, send_info)+via_len; #ifdef XL_DEBUG @@ -1653,7 +1660,8 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg, udp_mtu=cfg_get(core, core_cfg, udp_mtu); di.proto=PROTO_NONE; if (unlikely((send_info->proto==PROTO_UDP) && udp_mtu && - (flags & FL_MTU_FB_MASK) && (new_len>udp_mtu))){ + (flags & FL_MTU_FB_MASK) && (new_len>udp_mtu) + && (!(mode&BUILD_NO_LOCAL_VIA)))){
di=*send_info; /* copy whole struct - will be used in the Via builder */ di.proto=PROTO_NONE; /* except the proto */ @@ -1681,7 +1689,7 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg, if (di.proto!=PROTO_NONE){ new_len-=via_len; - pkg_free(line_buf); + if(likely(line_buf)) pkg_free(line_buf); line_buf = create_via_hf( &via_len, msg, &di, &branch); if (!line_buf){ LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: " @@ -1694,17 +1702,21 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg, /* add via header to the list */ /* try to add it before msg. 1st via */ /* add first via, as an anchor for second via*/ - via_anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0, HDR_VIA_T); - if (via_anchor==0) goto error04; - if ((via_lump=insert_new_lump_before(via_anchor, line_buf, via_len, + if(likely(line_buf)) { + via_anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0, HDR_VIA_T); + if (via_anchor==0) goto error04; + if ((via_lump=insert_new_lump_before(via_anchor, line_buf, via_len, HDR_VIA_T))==0) - goto error04; - + goto error04; + } if (msg->new_uri.s){ uri_len=msg->new_uri.len; new_len=new_len-msg->first_line.u.request.uri.len+uri_len; } - new_buf=(char*)pkg_malloc(new_len+1); + if(unlikely(mode&BUILD_IN_SHM)) + new_buf=(char*)shm_malloc(new_len+1); + else + new_buf=(char*)pkg_malloc(new_len+1); if (new_buf==0){ ser_error=E_OUT_OF_MEM; LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: out of memory\n"); @@ -2543,3 +2555,28 @@ char * build_all( struct sip_msg* msg, int touch_clen, *returned_len = offset; return new_buf; } + + + +/** + * parse buf in msg and fill several fields + */ +int build_sip_msg_from_buf(struct sip_msg *msg, char *buf, int len, + unsigned int id) +{ + if(msg==0 || buf==0) + return -1; + + memset(msg, 0, sizeof(sip_msg_t)); + msg->id = id; + msg->buf = buf; + msg->len = len; + if (parse_msg(buf, len, msg)!=0) { + LM_ERR("parsing failed"); + return -1; + } + msg->set_global_address=default_global_address; + msg->set_global_port=default_global_port; + return 0; +} + diff --git a/msg_translator.h b/msg_translator.h index a2a6b6a..95758df 100644 --- a/msg_translator.h +++ b/msg_translator.h @@ -48,6 +48,10 @@
/*#define MAX_CONTENT_LEN_BUF INT2STR_MAX_LEN *//* see ut.h/int2str() */
+#define BUILD_NO_LOCAL_VIA (1<<0) +#define BUILD_NO_VIA1_UPDATE (1<<1) +#define BUILD_IN_SHM (1<<2) + #include "parser/msg_parser.h" #include "ip_addr.h"
@@ -76,7 +80,8 @@ struct hostport { }while(0)
char * build_req_buf_from_sip_req ( struct sip_msg* msg, - unsigned int *returned_len, struct dest_info* send_info); + unsigned int *returned_len, struct dest_info* send_info, + unsigned int mode);
char * build_res_buf_from_sip_res( struct sip_msg* msg, unsigned int *returned_len); @@ -152,4 +157,6 @@ char * build_all( struct sip_msg* msg, int adjust_clen, /** cfg framework fixup */ void fix_global_req_flags(str* gname, str* name);
+int build_sip_msg_from_buf(struct sip_msg *msg, char *buf, int len, + unsigned int id); #endif diff --git a/receive.c b/receive.c index d5215bf..9f59933 100644 --- a/receive.c +++ b/receive.c @@ -76,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 diff --git a/receive.h b/receive.h index 5cdc25b..911598f 100644 --- a/receive.h +++ b/receive.h @@ -33,6 +33,6 @@ #include "ip_addr.h"
int receive_msg(char* buf, unsigned int len, struct receive_info *ri); - +unsigned int inc_msg_no(void);
#endif