[sr-dev] git:master:4a904b5f: core: function to get a new faked message in a specific structure
Daniel-Constantin Mierla
miconda at gmail.com
Fri Aug 16 12:58:30 CEST 2019
Module: kamailio
Branch: master
Commit: 4a904b5fe90253d0d5517cf1a5cdca3695e59ec3
URL: https://github.com/kamailio/kamailio/commit/4a904b5fe90253d0d5517cf1a5cdca3695e59ec3
Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2019-08-16T12:37:58+02:00
core: function to get a new faked message in a specific structure
- refactored the code to be able to fill a sip msg structure with
content from the faked request
---
Modified: src/core/fmsg.c
---
Diff: https://github.com/kamailio/kamailio/commit/4a904b5fe90253d0d5517cf1a5cdca3695e59ec3.diff
Patch: https://github.com/kamailio/kamailio/commit/4a904b5fe90253d0d5517cf1a5cdca3695e59ec3.patch
---
diff --git a/src/core/fmsg.c b/src/core/fmsg.c
index 28d24cd116..1450509f8c 100644
--- a/src/core/fmsg.c
+++ b/src/core/fmsg.c
@@ -30,48 +30,69 @@
#define FAKED_SIP_MSG "OPTIONS sip:you at kamailio.org SIP/2.0\r\nVia: SIP/2.0/UDP 127.0.0.1\r\nFrom: <sip:you at kamailio.org>;tag=123\r\nTo: <sip:you at kamailio.org>\r\nCall-ID: 123\r\nCSeq: 1 OPTIONS\r\nContent-Length: 0\r\n\r\n"
#define FAKED_SIP_MSG_LEN (sizeof(FAKED_SIP_MSG)-1)
-static char _faked_sip_buf[FAKED_SIP_MSG_LEN+1];
-static struct sip_msg _faked_msg;
+static char _faked_sip_buf[BUF_SIZE];
+static int _faked_sip_buf_init = 0;
+static sip_msg_t _faked_msg;
static unsigned int _faked_msg_no = 0;
-int faked_msg_init(void)
+static unsigned int faked_msg_get_next_id(void)
{
- if(_faked_msg_no>0)
- return 0;
- /* init faked sip msg */
+ _faked_msg_no += ((_faked_msg_no+1)==0)?2:1;
+ return _faked_msg_no;
+}
+
+static void faked_msg_buf_init(void)
+{
+ if(_faked_sip_buf_init!=0) {
+ return;
+ }
memcpy(_faked_sip_buf, FAKED_SIP_MSG, FAKED_SIP_MSG_LEN);
_faked_sip_buf[FAKED_SIP_MSG_LEN] = '\0';
+ _faked_sip_buf_init = 1;
+}
+
+static int faked_msg_init_new(sip_msg_t *fmsg)
+{
+ faked_msg_buf_init();
- memset(&_faked_msg, 0, sizeof(struct sip_msg));
+ /* init faked sip msg */
+ memset(fmsg, 0, sizeof(sip_msg_t));
- _faked_msg.buf=_faked_sip_buf;
- _faked_msg.len=FAKED_SIP_MSG_LEN;
+ fmsg->buf=_faked_sip_buf;
+ fmsg->len=FAKED_SIP_MSG_LEN;
- _faked_msg.set_global_address=default_global_address;
- _faked_msg.set_global_port=default_global_port;
+ fmsg->set_global_address=default_global_address;
+ fmsg->set_global_port=default_global_port;
- if (parse_msg(_faked_msg.buf, _faked_msg.len, &_faked_msg)!=0)
- {
- LM_ERR("parse_msg failed\n");
+ if (parse_msg(fmsg->buf, fmsg->len, fmsg)!=0) {
+ LM_ERR("parse faked msg failed\n");
return -1;
}
- _faked_msg.rcv.proto = PROTO_UDP;
- _faked_msg.rcv.src_port = 5060;
- _faked_msg.rcv.src_ip.u.addr32[0] = 0x7f000001;
- _faked_msg.rcv.src_ip.af = AF_INET;
- _faked_msg.rcv.src_ip.len = 4;
- _faked_msg.rcv.dst_port = 5060;
- _faked_msg.rcv.dst_ip.u.addr32[0] = 0x7f000001;
- _faked_msg.rcv.dst_ip.af = AF_INET;
- _faked_msg.rcv.dst_ip.len = 4;
+ fmsg->rcv.proto = PROTO_UDP;
+ fmsg->rcv.src_port = 5060;
+ fmsg->rcv.src_ip.u.addr32[0] = 0x7f000001;
+ fmsg->rcv.src_ip.af = AF_INET;
+ fmsg->rcv.src_ip.len = 4;
+ fmsg->rcv.dst_port = 5060;
+ fmsg->rcv.dst_ip.u.addr32[0] = 0x7f000001;
+ fmsg->rcv.dst_ip.af = AF_INET;
+ fmsg->rcv.dst_ip.len = 4;
return 0;
}
-static inline sip_msg_t* faked_msg_next_id(int mode)
+int faked_msg_init(void)
+{
+ if(_faked_msg_no>0) {
+ return 0;
+ }
+ return faked_msg_init_new(&_faked_msg);
+}
+
+static inline sip_msg_t* faked_msg_build_next(int mode)
{
- _faked_msg.id = 1 + _faked_msg_no++;
+ _faked_msg.id = faked_msg_get_next_id();
_faked_msg.pid = my_pid();
memset(&_faked_msg.tval, 0, sizeof(struct timeval));
if(mode) clear_branches();
@@ -80,12 +101,12 @@ static inline sip_msg_t* faked_msg_next_id(int mode)
sip_msg_t* faked_msg_next(void)
{
- return faked_msg_next_id(0);
+ return faked_msg_build_next(0);
}
sip_msg_t* faked_msg_next_clear(void)
{
- return faked_msg_next_id(1);
+ return faked_msg_build_next(1);
}
sip_msg_t* faked_msg_get_next(void)
@@ -104,6 +125,18 @@ sip_msg_t* faked_msg_get_next_clear(void)
return faked_msg_next_clear();
}
+int faked_msg_get_new(sip_msg_t *fmsg)
+{
+ if(faked_msg_init_new(fmsg)<0) {
+ return -1;
+ }
+ fmsg->id = faked_msg_get_next_id();
+ fmsg->pid = my_pid();
+ memset(&fmsg->tval, 0, sizeof(struct timeval));
+
+ return 0;
+}
+
int faked_msg_match(sip_msg_t *tmsg)
{
return ( tmsg == &_faked_msg ) ? 1 : 0;
More information about the sr-dev
mailing list