Skip to content

How to use sofia-sip to parse sip msg? #304

@wenyuanZZ

Description

@wenyuanZZ

@briankwest ,Hi Brian:
I want to use sofia-sip to parse sip msg, if sip msg is err, return an err value. I use it like:
#include "msg/msg_internal.h"
#include <sofia-sip/msg.h>
#include <sofia-sip/nua.h>
#include <sofia-sip/sip_status.h>
#include <sofia-sip/sdp.h>
#include <sofia-sip/sip_protos.h>
#include <sofia-sip/auth_module.h>
#include <sofia-sip/su_md5.h>
#include <sofia-sip/su_log.h>
#include <sofia-sip/su_strlst.h>
#include <sofia-sip/nea.h>
#include <sofia-sip/msg_addr.h>
#include <sofia-sip/tport_tag.h>
#include <sofia-sip/sip_extra.h>
#include "sofia-sip/msg_parser.h"
#include "sofia-sip/sip_parser.h"
#include "sofia-sip/tport_tag.h"
#include <sofia-sip/uniqueid.h>
#include "sofia-sip/msg_header.h"
#include "sofia-sip/bnf.h"
#include "sofia-sip/msg_parser.h"
#include "sofia-sip/msg_mclass.h"
#include "sofia-sip/msg_mclass_hash.h"
#include "sofia-sip/msg_mime.h"
.......
#define SIP_INVITE_MSG_CONTACT_ERR_ZWYTEST "INVITE sip:[email protected]:5060 SIP/2.0\r\n
Record-Route: sip:199.45.10.99:7098;ftag=1796780028;lr;did=13d.723d0655\r\n
Via: SIP/2.0/UDP 10.0.0.8:5090;branch=z9hG4bK96d8.1dd9f537.0\r\n
Via: SIP/2.0/UDP 0.0.0.0:64135;rport=64135;received=173.208.223.146;branch=z9hG4bK1213005543\r\n
Max-Forwards: 30\r\n
From: sip:[email protected];tag=1796780028\r\n
To: sip:[email protected]\r\n
Call-ID: 1403923132-1093036234-888019752\r\n
CSeq: 1 INVITE\r\n
Contact: sip:130703\"@173.208.223.146:64135\r\n
Content-Type: application/sdp\r\n
Content-Length: 406\r\n
Allow: ACK, BYE, CANCEL, INFO, INVITE, MESSAGE, NOTIFY, OPTIONS, PRACK, REFER, REGISTER, SUBSCRIBE, UPDATE, PUBLISH\r\n
User-Agent: pplsip\r\n
X-AUTH-IP: 173.208.223.146\r\n
\r\n
v=0\r\n
o=- 329918574 329919044 IN IP4 0.0.0.0\r\n
s=sipcall\r\n
c=IN IP4 0.0.0.0\r\n
t=0 0\r\n
m=audio 34068 RTP/AVP 100 6 0 8 3 18 5 101\r\n
a=alt:1 1 : DF50DC48 0000006A 0.0.0.0 7628\r\n
a=rtpmap:100 speex/16000\r\n
a=rtpmap:6 DVI4/16000\r\n
a=rtpmap:0 PCMU/8000\r\n
a=rtpmap:8 PCMA/8000\r\n
a=rtpmap:3 GSM/8000\r\n
a=rtpmap:18 G729/8000\r\n
a=rtpmap:5 DVI4/8000\r\n
a=rtpmap:101 telephone-event/8000\r\n
a=fmtp:101 0-11\r\n
a=sendonly\r\n
a=rtcp:34069\r\n"

..............
//Contact: sip:130703\"@173.208.223.146:64135 has a " in user;

    int extract_res = 0;
    msg_mclass_t const *_default_zwytest = sip_default_mclass();

/**** I have write this func in sofia-sip/libsofia-sip-ua/sip/sofia-sip/sip_parser.h
//add by zwy start 2025.08.08 start
// Return a built-in SIP parser object.
SOFIAPUBFUN msg_mclass_t const *sip_default_mclass(void);
//add by zwy start 2025.08.08 stop
***/
msg_err = msg_create(_default_zwytest, 2);
switch_zmalloc(msg_err->m_buffer->mb_data, strlen(SIP_INVITE_MSG_CONTACT_ERR_ZWYTEST) + 1);
snprintf(msg_err->m_buffer->mb_data, strlen(SIP_INVITE_MSG_CONTACT_ERR_ZWYTEST) + 1, "%s", SIP_INVITE_MSG_CONTACT_ERR_ZWYTEST);
msg_err->m_buffer->mb_used = 0;
msg_err->m_buffer->mb_commit = strlen(SIP_INVITE_MSG_CONTACT_ERR_ZWYTEST);
msg_err->m_buffer->mb_size = strlen(SIP_INVITE_MSG_CONTACT_ERR_ZWYTEST) + 1;
msg_err->m_buffer->mb_eos = 1;

    extract_res = msg_extract(msg_err);

    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "SIP_INVITE_MSG_CONTACT_ERR_ZWYTEST:\n%s\n", SIP_INVITE_MSG_CONTACT_ERR_ZWYTEST);
    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "extract_res:%d, msg_get_flags:%d\n", extract_res, msg_get_flags(msg_err, MSG_FLG_ERROR));

    switch_safe_free(msg_err->m_buffer->mb_data);

..................

I run this codes, it comes:
===============debug logs start===================
2025-08-08 10:47:13.133395 98.17% [INFO] mod_yeastar_zwytest_parse_sip_and_check.c:200 SIP_INVITE_MSG_CONTACT_ERR_ZWYTEST:
INVITE sip:[email protected]:5060 SIP/2.0
Record-Route: sip:199.45.10.99:7098;ftag=1796780028;lr;did=13d.723d0655
Via: SIP/2.0/UDP 10.0.0.8:5090;branch=z9hG4bK96d8.1dd9f537.0
Via: SIP/2.0/UDP 0.0.0.0:64135;rport=64135;received=173.208.223.146;branch=z9hG4bK1213005543
Max-Forwards: 30
From: sip:[email protected];tag=1796780028
To: sip:[email protected]
Call-ID: 1403923132-1093036234-888019752
Contact: sip:130703"@173.208.223.146:64135
ontent-Type: application/sdp
Content-Length: 406
Allow: ACK, BYE, CANCEL, INFO, INVITE, MESSAGE, NOTIFY, OPTIONS, PRACK, REFER, REGISTER, SUBSCRIBE, UPDATE, PUBLISH
User-Agent: pplsip
X-AUTH-IP: 173.208.223.146

v=0
o=- 329918574 329919044 IN IP4 0.0.0.0
s=sipcall
c=IN IP4 0.0.0.0
t=0 0
m=audio 34068 RTP/AVP 100 6 0 8 3 18 5 101
a=alt:1 1 : DF50DC48 0000006A 0.0.0.0 7628
a=rtpmap:100 speex/16000
a=rtpmap:6 DVI4/16000
a=rtpmap:0 PCMU/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:3 GSM/8000
a=rtpmap:18 G729/8000
a=rtpmap:5 DVI4/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-11
a=sendonly
a=rtcp:34069

2025-08-08 10:47:13.133395 98.17% [INFO] mod_yeastar_zwytest_parse_sip_and_check.c:201 extract_res:1, msg_get_flags:0
===============debug logs end===================

even i use an err Contact in SIP_INVITE_MSG_CONTACT_ERR_ZWYTEST, like: Contact: ceafr or Contact: $$$; msg_extract(msg_err) still return 1, msg_get_flags(msg_err, MSG_FLG_ERROR) still no error.

it let me confusion. I view the code, it seems like it can parse SIP header, if some err happended.
============ the codes in sofia-sip/libsofia-sip-ua/msg/msg_parser.c extract_header func start===========
........
hr = msg_find_hclass(mc, b, &n); /* Get header name /
error = n == 0;
if (hr == NULL) /
Panic */
return -1;

xtra = span_ws(b + n);

/* Find next crlf which is not followed by whitespace /
do {
n += xtra + crlf;
if (!eos && bsiz == n)
return 0;
m = span_non_crlf(b + n);
if (!name_len_set && m)
name_len = n, name_len_set = 1; /
First non-ws after COLON */
n += m;
crlf = CRLF_TEST(b + n);
xtra = span_ws(b + n + crlf);
}
while (xtra);

if (!eos && bsiz == n + crlf)
return 0;

if (hr->hr_class->hc_hash == msg_unknown_hash)
name_len = 0, name_len_set = 1;

if (error) {
msg->m_extract_err |= hr->hr_flags;
if (hr->hr_class->hc_critical)
mo->msg_flags |= MSG_FLG_ERROR;
hr = mc->mc_error;
copy_buffer = 1;
h = error_header_parse(msg, mo, hr);
}
else {
...........
============ the codes in sofia-sip/libsofia-sip-ua/msg/msg_parser.c extract_header func stop===========

How to use sofia-sip to parse sip msg, if some err in SIP Header content, or mis some SIP header must has; return an err value, and report the err SIP header line.
wish today. thks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions