MT#62181 store sip_msg buffer as std::string

For automatic management of memory and lifetimes.

No functional change.

Change-Id: I0492277737435417860d9d891ea945c8b293d5ce
mr26.0
Richard Fuchs 1 year ago
parent 4a1f149021
commit d847534a6a

@ -260,7 +260,7 @@ void AmSipRequest::log(const shared_ptr<msg_logger>& logger) const
const sip_trans* t = tt.get_trans();
if (t) {
sip_msg* msg = t->msg;
logger->log(msg->buf,msg->len,&msg->remote_ip,
logger->log(msg->buf.c_str(), msg->buf.length(), &msg->remote_ip,
&msg->local_ip,msg->u.request->method_str);
}
tt.unlock_bucket();

@ -311,7 +311,7 @@ int SipCtrlInterface::send(AmSipRequest &req, const string& dialog_id,
msg->content_type = new sip_header(0,SIP_HDR_CONTENT_TYPE, stl2cstr(content_type));
msg->hdrs.push_back(msg->content_type);
req.body.print(body);
msg->body = stl2cstr(body);
msg->body = body;
}
int res = trans_layer::instance()->send_request(msg,&req.tt,
@ -400,7 +400,7 @@ int SipCtrlInterface::send(const AmSipReply &rep, const string& dialog_id,
ERROR("Reply does not contain a Content-Type whereby body is not empty\n");
return -1;
}
msg.body = stl2cstr(body);
msg.body = body;
msg.hdrs.push_back(new sip_header(sip_header::H_CONTENT_TYPE,
SIP_HDR_CONTENT_TYPE,
stl2cstr(content_type)));
@ -437,7 +437,7 @@ inline bool SipCtrlInterface::sip_msg2am_request(const sip_msg *msg,
if(parse_first_nameaddr(&na,contact.s,contact.len) < 0) {
WARN("Contact parsing failed\n");
WARN("\tcontact = '%.*s'\n",contact.len,contact.s);
WARN("\trequest = '%.*s'\n",msg->len,msg->buf);
WARN("\trequest = '%s'\n", msg->buf.c_str());
trans_layer::instance()->send_sf_error_reply(&tt, msg, 400,
"Bad Contact");
@ -450,7 +450,7 @@ inline bool SipCtrlInterface::sip_msg2am_request(const sip_msg *msg,
if(parse_uri(&u,na.addr.s,na.addr.len)){
DBG("'Contact' in new request contains a malformed URI\n");
DBG("\tcontact uri = '%.*s'\n",na.addr.len,na.addr.s);
DBG("\trequest = '%.*s'\n",msg->len,msg->buf);
DBG("\trequest = '%s'\n", msg->buf.c_str());
trans_layer::instance()->
send_sf_error_reply(&tt, msg, 400, "Malformed Contact URI");
@ -471,7 +471,7 @@ inline bool SipCtrlInterface::sip_msg2am_request(const sip_msg *msg,
else {
if (req.method == SIP_METH_INVITE) {
DBG("Request has no contact header\n");
DBG("\trequest = '%.*s'\n",msg->len,msg->buf);
DBG("\trequest = '%s'\n", msg->buf.c_str());
trans_layer::instance()->
send_sf_error_reply(&tt, msg, 400, "Missing Contact-HF");
return false;
@ -505,8 +505,8 @@ inline bool SipCtrlInterface::sip_msg2am_request(const sip_msg *msg,
if (msg->content_type) {
if(req.body.parse(c2stlstr(msg->content_type->value),
msg->body.s,
msg->body.len) < 0) {
msg->body.c_str(),
msg->body.length()) < 0) {
DBG("could not parse MIME body\n");
}
else {
@ -572,8 +572,8 @@ inline bool SipCtrlInterface::sip_msg2am_reply(sip_msg *msg, AmSipReply &reply)
if (msg->content_type) {
if(reply.body.parse(c2stlstr(msg->content_type->value),
msg->body.s,
msg->body.len) < 0) {
msg->body.c_str(),
msg->body.length()) < 0) {
DBG("could not parse MIME body\n");
}
else {

@ -44,8 +44,7 @@
using std::unique_ptr;
sip_msg::sip_msg(const char* msg_buf, int msg_len)
: buf(NULL),
hdrs(),
: hdrs(),
to(NULL),
from(NULL),
cseq(NULL),
@ -71,8 +70,7 @@ sip_msg::sip_msg(const char* msg_buf, int msg_len)
}
sip_msg::sip_msg()
: buf(NULL),
hdrs(),
: hdrs(),
to(NULL),
from(NULL),
cseq(NULL),
@ -97,8 +95,6 @@ sip_msg::sip_msg()
sip_msg::~sip_msg()
{
delete [] buf;
list<sip_header*>::iterator it;
for(it = hdrs.begin();
it != hdrs.end(); ++it) {
@ -119,15 +115,12 @@ sip_msg::~sip_msg()
void sip_msg::copy_msg_buf(const char* msg_buf, int msg_len)
{
buf = new char[msg_len+1];
memcpy(buf,msg_buf,msg_len);
buf[msg_len] = '\0';
len = msg_len;
buf = string(msg_buf, msg_len);
}
void sip_msg::release()
{
buf = NULL;
buf.clear();
hdrs.clear();
u.request = NULL;
local_socket = NULL;
@ -136,7 +129,7 @@ void sip_msg::release()
int sip_msg::send(unsigned int flags)
{
assert(local_socket);
return local_socket->send(&remote_ip,buf,len,flags);
return local_socket->send(&remote_ip, buf.c_str(), buf.length(), flags);
}
@ -528,8 +521,8 @@ int parse_headers(sip_msg* msg, const char** c, const char* end)
int parse_sip_msg(sip_msg* msg, char*& err_msg)
{
const char* c = msg->buf;
const char* end = c + msg->len;
const char* c = msg->buf.c_str();
const char* end = c + msg->buf.length();
int err = parse_first_line(msg,&c,end);
@ -541,7 +534,7 @@ int parse_sip_msg(sip_msg* msg, char*& err_msg)
err = parse_headers(msg,&c,end);
if(!err){
msg->body.set(c,msg->len - (c - msg->buf));
msg->body = string(c, msg->buf.length() - (c - msg->buf.c_str()));
}
if(!msg->via1 ||

@ -102,8 +102,7 @@ struct sip_reply
struct sip_msg
{
char* buf;
int len;
string buf;
// Request or Reply?
int type;
@ -132,7 +131,7 @@ struct sip_msg
list<sip_header*> record_route;
sip_header* content_type;
sip_header* content_length;
cstring body;
string body; // TODO: use a string view?
sockaddr_storage local_ip;
shared_ptr<trsp_socket> local_socket;
@ -144,6 +143,7 @@ struct sip_msg
~sip_msg();
void copy_msg_buf(const char* msg_buf, int msg_len);
void copy_msg_buf(const string& _buf) { buf = _buf; }
int send(unsigned flags);

@ -394,12 +394,12 @@ int trans_layer::send_reply(sip_msg* msg, const trans_ticket* tt,
reply_len += copy_hdrs_len(msg->hdrs);
string c_len = int2str(msg->body.len);
string c_len = int2str((unsigned int) msg->body.length());
reply_len += content_length_len((char*)c_len.c_str());
if(msg->body.len){
if(msg->body.length()){
reply_len += msg->body.len;
reply_len += msg->body.length();
}
reply_len += 2/*CRLF*/;
@ -525,8 +525,8 @@ int trans_layer::send_reply(sip_msg* msg, const trans_ticket* tt,
*c++ = CR;
*c++ = LF;
if(msg->body.len){
memcpy(c,msg->body.s,msg->body.len);
if(msg->body.length()){
memcpy(c, msg->body.c_str(), msg->body.length());
}
int err = -1;
@ -660,7 +660,7 @@ int trans_layer::send_sf_error_reply(const trans_ticket* tt, const sip_msg* req,
ERROR("Malformed additional header\n");
return -1;
}
reply.body = body;
reply.body = c2stlstr(body);
return send_reply(&reply, tt, "", to_tag);
}
@ -974,7 +974,7 @@ void trans_layer::transport_error(sip_msg* msg)
}
DBG("parsing error: %s\n",err_msg);
DBG("Message was: \"%.*s\"\n",msg->len,msg->buf);
DBG("Message was: \"%s\"\n", msg->buf.c_str());
return;
}
@ -995,7 +995,8 @@ void trans_layer::transport_error(sip_msg* msg)
static void translate_string(sip_msg* dst_msg, cstring& dst,
const sip_msg* src_msg, const cstring& src)
{
dst.s = (char*)src.s + (dst_msg->buf - src_msg->buf);
// TODO: use string views
dst.s = dst_msg->buf.c_str() + (src.s - src_msg->buf.c_str());
dst.len = src.len;
}
@ -1013,7 +1014,7 @@ static void translate_hdr(sip_msg* dst_msg, sip_header*& dst,
static void gen_error_reply_from_req(sip_msg& reply, const sip_msg* req,
int code, const char* reason)
{
reply.copy_msg_buf(req->buf,req->len);
reply.copy_msg_buf(req->buf);
reply.type = SIP_REPLY;
reply.u.reply = new sip_reply();
@ -1139,22 +1140,21 @@ static int generate_and_parse_new_msg(sip_msg* msg, sip_msg*& p_msg)
request_len += copy_hdrs_len_no_via_contact(msg->hdrs);
request_len += copy_hdrs_len(n_contacts);
string content_len = int2str(msg->body.len);
string content_len = int2str((unsigned int) msg->body.length());
request_len += content_length_len(stl2cstr(content_len));
request_len += 2/* CRLF end-of-headers*/;
if(msg->body.len){
request_len += msg->body.len;
if (msg->body.length()) {
request_len += msg->body.length();
}
// Allocate new message
p_msg = new sip_msg();
p_msg->buf = new char[request_len+1];
p_msg->len = request_len;
char* buf = new char[request_len+1];
// generate it
char* c = p_msg->buf;
char* c = buf;
request_line_wr(&c,msg->u.request->method_str,
msg->u.request->ruri_str);
@ -1170,18 +1170,20 @@ static int generate_and_parse_new_msg(sip_msg* msg, sip_msg*& p_msg)
*c++ = CR;
*c++ = LF;
if(msg->body.len){
memcpy(c,msg->body.s,msg->body.len);
if (msg->body.length()) {
memcpy(c, msg->body.c_str(), msg->body.length());
c += msg->body.len;
c += msg->body.length();
}
*c++ = '\0';
p_msg->buf = string(buf, request_len);
// and parse it
char* err_msg=0;
if(parse_sip_msg(p_msg,err_msg)){
ERROR("Parser failed on generated request\n");
ERROR("Message was: <%.*s>\n",p_msg->len,p_msg->buf);
ERROR("Message was: <%s>\n", p_msg->buf.c_str());
delete p_msg;
p_msg = NULL;
return MALFORMED_SIP_MSG;
@ -1289,10 +1291,10 @@ int trans_layer::send_request(sip_msg* msg, trans_ticket* tt,
err = generate_and_parse_new_msg(msg,p_msg);
if(err != 0) { return err; }
DBG("Sending to %s:%i <%.*s...>\n",
DBG("Sending to %s:%i <%s...>\n",
get_addr_str(&p_msg->remote_ip).c_str(),
ntohs(((sockaddr_in*)&p_msg->remote_ip)->sin_port),
p_msg->len,p_msg->buf);
p_msg->buf.c_str());
tt->_bucket = get_trans_bucket(p_msg->callid->value,
get_cseq(p_msg)->num_str);
@ -1349,7 +1351,7 @@ int trans_layer::send_request(sip_msg* msg, trans_ticket* tt,
msg->local_socket->copy_addr_to(&src_ip);
cstring method_str = msg->u.request->method_str;
char* msg_buffer=NULL;
const char* msg_buffer=NULL;
unsigned int msg_len=0;
if(tt->_t && (method == sip_request::ACK)) {
@ -1359,8 +1361,8 @@ int trans_layer::send_request(sip_msg* msg, trans_ticket* tt,
}
/* p_msg could have been freed already by update_uac_request() */
else if (p_msg) {
msg_buffer = p_msg->buf;
msg_len = p_msg->len;
msg_buffer = p_msg->buf.c_str();
msg_len = p_msg->buf.length();
}
logger->log(msg_buffer,msg_len,
@ -1481,11 +1483,10 @@ int trans_layer::cancel(trans_ticket* tt, const string& dialog_id,
// Allocate new message
sip_msg* p_msg = new sip_msg();
p_msg->buf = new char[request_len+1];
p_msg->len = request_len;
char* buf = new char[request_len+1];
// generate it
char* c = p_msg->buf;
char* c = buf;
request_line_wr(&c,cancel_str,
req->u.request->ruri_str);
@ -1506,13 +1507,14 @@ int trans_layer::cancel(trans_ticket* tt, const string& dialog_id,
*c++ = CR;
*c++ = LF;
*c = '\0';
p_msg->buf = string(buf, request_len);
// and parse it
char* err_msg=0;
if(parse_sip_msg(p_msg,err_msg)){
ERROR("Parser failed on generated request\n");
ERROR("Message was: <%.*s>\n",p_msg->len,p_msg->buf);
ERROR("Message was: <%s>\n", p_msg->buf.c_str());
delete p_msg;
bucket->unlock();
return MALFORMED_SIP_MSG;
@ -1521,10 +1523,10 @@ int trans_layer::cancel(trans_ticket* tt, const string& dialog_id,
memcpy(&p_msg->remote_ip,&req->remote_ip,sizeof(sockaddr_storage));
p_msg->local_socket = req->local_socket;
DBG("Sending to %s:%i:\n<%.*s>\n",
DBG("Sending to %s:%i:\n<%s>\n",
get_addr_str(&p_msg->remote_ip).c_str(),
ntohs(((sockaddr_in*)&p_msg->remote_ip)->sin_port),
p_msg->len,p_msg->buf);
p_msg->buf.c_str());
int send_err = p_msg->send(t->flags);
if(send_err < 0){
@ -1546,7 +1548,7 @@ int trans_layer::cancel(trans_ticket* tt, const string& dialog_id,
if(t->logger) {
sockaddr_storage src_ip;
p_msg->local_socket->copy_addr_to(&src_ip);
t->logger->log(p_msg->buf,p_msg->len,&src_ip,
t->logger->log(p_msg->buf.c_str(), p_msg->buf.length(), &src_ip,
&p_msg->remote_ip,cancel_str);
if(!cancel_t->logger) {
@ -1579,7 +1581,7 @@ void trans_layer::received_msg(sip_msg* msg)
DBG("parsing error: %s\n",err_msg);
DBG("Message was: \"%.*s\"\n",msg->len,msg->buf);
DBG("Message was: \"%s\"\n", msg->buf.c_str());
if((err != MALFORMED_FLINE)
&& (msg->type == SIP_REQUEST)
@ -1613,7 +1615,7 @@ void trans_layer::process_rcvd_msg(sip_msg* msg)
if((t = bucket->match_request(msg,TT_UAS)) != NULL){
if(t->logger) {
t->logger->log(msg->buf,msg->len,&msg->remote_ip,
t->logger->log(msg->buf.c_str(), msg->buf.length(), &msg->remote_ip,
&msg->local_ip,msg->u.request->method_str);
}
@ -1715,8 +1717,8 @@ void trans_layer::process_rcvd_msg(sip_msg* msg)
DBG("Reply matched an existing transaction\n");
if(t->logger && msg->local_socket && msg->buf && msg->len) {
t->logger->log(msg->buf,msg->len,&msg->remote_ip,
if(t->logger && msg->local_socket && !msg->buf.empty()) {
t->logger->log(msg->buf.c_str(), msg->buf.length(), &msg->remote_ip,
&msg->local_ip,get_cseq(msg)->method_str,
msg->u.reply->code);
}
@ -2072,10 +2074,10 @@ int trans_layer::update_uac_request(trans_bucket* bucket, sip_trans*& t,
// transfer the message buffer
// to the transaction (incl. ownership)
t->retr_buf = msg->buf;
t->retr_len = msg->len;
msg->buf = NULL;
msg->len = 0;
t->retr_buf = new char[msg->buf.length() + 1];
memcpy(t->retr_buf, msg->buf.c_str(), msg->buf.length() + 1);
t->retr_len = msg->buf.length();
msg->buf.clear();
// copy destination address
memcpy(&t->retr_addr,&msg->remote_ip,sizeof(sockaddr_storage));
@ -2335,7 +2337,7 @@ void trans_layer::timer_expired(trans_timer* t, trans_bucket* bucket,
if(tr->logger) {
sockaddr_storage src_ip;
tr->msg->local_socket->copy_addr_to(&src_ip);
tr->logger->log(tr->msg->buf,tr->msg->len,&src_ip,&tr->msg->remote_ip,
tr->logger->log(tr->msg->buf.c_str(), tr->msg->buf.length(), &src_ip,&tr->msg->remote_ip,
tr->msg->u.request->method_str);
}
@ -2489,7 +2491,7 @@ void trans_layer::timer_expired(trans_timer* t, trans_bucket* bucket,
if(tr->logger) {
sockaddr_storage src_ip;
tr->msg->local_socket->copy_addr_to(&src_ip);
tr->logger->log(tr->msg->buf,tr->msg->len,
tr->logger->log(tr->msg->buf.c_str(), tr->msg->buf.length(),
&src_ip,&tr->msg->remote_ip,
tr->msg->u.request->method_str);
}
@ -2752,7 +2754,7 @@ int trans_layer::try_next_ip(trans_bucket* bucket, sip_trans* tr,
if(tr->logger) {
sockaddr_storage src_ip;
tr->msg->local_socket->copy_addr_to(&src_ip);
tr->logger->log(tr->msg->buf,tr->msg->len,
tr->logger->log(tr->msg->buf.c_str(), tr->msg->buf.length(),
&src_ip,&tr->msg->remote_ip,
tr->msg->u.request->method_str);
}

@ -317,10 +317,10 @@ void udp_trsp::run()
char host[NI_MAXHOST] = "";
_LOG(trsp_socket::log_level_raw_msgs,
"received msg via UDP from %s:%i:\n"
"%.*s\n",
"%s\n",
am_inet_ntop_sip(&s_msg->remote_ip,host,NI_MAXHOST),
am_get_port(&s_msg->remote_ip),
s_msg->len, s_msg->buf);
s_msg->buf.c_str());
}
s_msg->local_socket = sock;

Loading…
Cancel
Save