如何使用 PJSIP 库解码 SIP 字节流

How to decode SIP byte stream using PJSIP library?

本文关键字:SIP 字节流 解码 何使用 PJSIP      更新时间:2023-10-16

我正在尝试使用 pjsip 库解码以下 SIP 字节流,但我遇到分段错误。我的代码有什么问题?

#include <pjsip.h>
int main()
{
    char __MSG[] = {
        0x49, 0x4e, 0x56, 0x49, 0x54, 0x45, 0x20, 0x73, 0x69, 0x70,
        0x3a, 0x40, 0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
        0x31, 0x20, 0x53, 0x49, 0x50, 0x2f, 0x32, 0x2e, 0x30, 0x0d,
        0x0a, 0x54, 0x6f, 0x3a, 0x20, 0x3c, 0x73, 0x69, 0x70, 0x3a,
        0x31, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x2e, 0x34, 0x35, 0x3e,
        0x0d, 0x0a, 0x56, 0x69, 0x61, 0x3a, 0x20, 0x53, 0x49, 0x50,
        0x2f, 0x32, 0x2e, 0x30, 0x2f, 0x55, 0x44, 0x50, 0x20, 0x31,
        0x30, 0x2e, 0x30, 0x2e, 0x31, 0x2e, 0x34, 0x35, 0x0d, 0x0a,
        0x46, 0x72, 0x6f, 0x6d, 0x3a, 0x20, 0x22, 0x74, 0x65, 0x73,
        0x74, 0x74, 0x65, 0x73, 0x74, 0x22, 0x3c, 0x73, 0x69, 0x70,
        0x3a, 0x31, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x2e, 0x31, 0x39,
        0x39, 0x3e, 0x0d, 0x0a, 0x43, 0x61, 0x6c, 0x6c, 0x2d, 0x49,
        0x44, 0x3a, 0x20, 0x31, 0x34, 0x38, 0x31, 0x30, 0x2e, 0x30,
        0x2e, 0x31, 0x2e, 0x34, 0x35, 0x0d, 0x0a, 0x43, 0x53, 0x65,
        0x71, 0x3a, 0x20, 0x31, 0x20, 0x49, 0x4e, 0x56, 0x49, 0x54,
        0x45, 0x0d, 0x0a, 0x4d, 0x61, 0x78, 0x2d, 0x46, 0x6f, 0x72,
        0x77, 0x61, 0x72, 0x64, 0x73, 0x3a, 0x20, 0x32, 0x30, 0x0d,
        0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x63, 0x74, 0x3a, 0x20,
        0x3c, 0x73, 0x69, 0x70, 0x3a, 0x31, 0x32, 0x37, 0x2e, 0x30,
        0x2e, 0x30, 0x2e, 0x31, 0x3e, 0x0d, 0x0a, 0x0d, 0x0a, NULL
    };

    char *testmsg = __MSG;
    pj_size_t msgsize;
    pj_status_t status;

    // INIT
    status = pj_init();
    PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
    status = pjlib_util_init();
    PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
    // PARSING
    pj_caching_pool cp;
    pj_caching_pool_init(&cp, NULL, 1024*1024);
    pj_pool_t *pool = pj_pool_create(&cp.factory, "parser_pool", 4000, 4000, NULL);
    pjsip_parser_err_report err;
    int len = strlen(testmsg);
    pjsip_msg *msg = pjsip_parse_msg(pool, __MSG, len, &err);
    printf("The end...");
    return 0;
}

程序终止,信号 11,分段错误。
#0 0x0805befb in pj_scan_peek () (gdb) bt
排名 #0 0x0805befb 在 pj_scan_peek ()
排名 #1 0x080507f6 在 int_parse_msg ()
排名 #2 0x080523a2 在 pjsip_parse_msg ()
#3 0x0804fa89 in main () (gdb)

您需要

使用 SIP 终端实例。从文档中:

SIP 端点实例 (pjsip_endpoint) 可视为 应用程序中所有 SIP 对象的主/所有者。它执行 以下角色:

  • 它管理所有对象的内存池的分配/释放。
  • 它管理侦听器和传输,以及事务如何使用它们。
  • 它接收来自传输层的传入消息,并自动将它们分派到正确的事务(或创建一个新事务)。
  • 它具有计时器管理(计时器堆)的单个实例。
  • 它管理模块,这是扩展库的主要手段。
  • 它为所有对象提供单一轮询功能并分发事件。
  • 它会自动处理现有模块无法处理的传入请求(例如,当传入请求不受支持 方法)。
  • 等等..

应用程序应仅实例化一个 SIP 终结点实例 每个过程。

需要调用 pjsip_endpt_create 来创建终结点。

查看无状态 SIP 终结点示例,了解初始化和设置终结点时需要进行的库调用的一般结构。

其他注意事项:

  • pjsip_parse_msg函数将pjsip_parser_err_report列表作为最后一个参数。您必须使用 pj_list_init(&err) 初始化列表。
  • 确保在调用pj_pool_create后检查池是否为 NULL。
  • 该文档没有指示是否可以不将 NULL 池工厂策略传递给 pj_caching_pool_init;可能最好只传入默认值(如下面的示例所示)。

我相信它应该看起来像这样(但还没有测试过):

    // INIT
    status = pj_init();
    PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
    status = pjlib_util_init();
    PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
    // Create pool factory (for memory allocations)
    pj_caching_pool cp;
    pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 1024*1024);
    // Create global endpoint
    static pjsip_endpoint *sip_endpt;
    status = pjsip_endpt_create(&cp.factory, "uniquesipendpointname", &sip_endpt);
    PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
    // PARSING    
    pj_pool_t *pool = pj_pool_create(&cp.factory, "parser_pool", 4000, 4000, NULL);
    pjsip_parser_err_report err;
    int len = strlen(testmsg);
    pj_list_init(&err);
    pjsip_msg *msg = pjsip_parse_msg(pool, __MSG, len, &err);