使用libtins和libosip

使用libtins和libosip,sip,sniffer,Sip,Sniffer,我正在尝试使用libtins编写一个简单的SIP嗅探器,它工作得很好。 然后我尝试解析接收到的数据包到libosip。 虽然它确实正确地解析了消息,但它会默默地死去 我不知道这里会出什么问题,非常感谢您的帮助 这是我的消息来源: #include <iostream> #include "tins/tins.h" #include <osip2/osip.h> #include <osipparser2/osip_message.h> #include <

我正在尝试使用libtins编写一个简单的SIP嗅探器,它工作得很好。 然后我尝试解析接收到的数据包到libosip。 虽然它确实正确地解析了消息,但它会默默地死去

我不知道这里会出什么问题,非常感谢您的帮助

这是我的消息来源:

#include <iostream>
#include "tins/tins.h"
#include <osip2/osip.h>
#include <osipparser2/osip_message.h>
#include <vector>

using namespace Tins;

bool invalidChar (char c);
void stripUnicode(std::string & str); 

bool callback(const PDU &pdu) 
{

    const IP &ip = pdu.rfind_pdu<IP>(); // Find the IP layer
    const UDP &udp = pdu.rfind_pdu<UDP>(); // Find the TCP layer

    osip_message *sip;
    osip_message_init(&sip);

    // First here we print Source and Destination Information
    std::cout << ip.src_addr() << ':' << udp.sport() << " -> " 
          << ip.dst_addr() << ':' << udp.dport() << std::endl;

    // Extract the RawPDU object.
    const RawPDU& raw = udp.rfind_pdu<RawPDU>();

    // Finally, take the payload (this is a vector<uint8_t>)
    const RawPDU::payload_type& payload = raw.payload();

    // We create a string message
    std::string message( payload.begin(), payload.end() );
    std::string sip_message;

    // Try to parse the message
    std::cout << "copying message with len " << message.size() << std::endl;
    const char *msg = message.c_str();

    std::cout << "parsing message with size " << strlen(msg) << std::endl;
    osip_message_parse( sip, msg, strlen( msg ) );

    std::cout << "freeing message" << std::endl;
    osip_message_free(sip);
    return true;
}

int main(int argc, char *argv[]) 
{
    if(argc != 2) {
        std::cout << "Usage: " << *argv << " <interface>" << std::endl;
        return 1;
    }
    // Sniff on the provided interface in promiscuos mode
    Sniffer sniffer(argv[1], Sniffer::PROMISC);

    // Only capture udp packets sent to port 53
    sniffer.set_filter("port 5060");

    // Start the capture
    sniffer.sniff_loop(callback);
}
它静静地死去

如果我删除该行:

osip_message_parse( sip, msg, strlen( msg ) );
它一直很完美


非常感谢你的帮助

首先,如果之前发生内存损坏,则osip_消息_解析中可能会发生崩溃,但这可能不是初始损坏的根源

为了使用libosip测试sip消息,您可以进入osip的build目录并创建一个包含sip消息的文件:mymessage.txt

$> ./src/test/torture_test mymessage.txt 0 -v
即使要与valgrind进行更深入的检查:

$> valgrind ./src/test/.libs/torture_test mymessage.txt 0 -v
如果您的代码在所有sip消息中都失败了,我想问题在于libosip之外的内存损坏

SIP消息的大小确实存在另一个错误:

osip_message_parse( sip, msg, strlen( msg ) );
SIP消息可以包含包含\0字符的二进制数据,因此您的代码应该使用二进制有效负载的确切长度,而不是strlen。这样的更改是必需的,但不会解决您的主要问题:

osip_message_parse( sip, msg, payload.end() - payload.begin() );
我还建议您尝试最新的osip git,并使用SIP消息副本完成您的问题

编辑:正如David发现的,初始化没有完成,这就是问题的根源。但是,初始化的正确方法如第一行文档所述:


我终于找到了问题所在。 有必要使用初始化语法分析器

parser_init();
任何地方都没有记录:

现在它不再对我产生影响,但解析工作不正常。我需要更多的调查

谢谢大家


大卫

我无法抗拒,我只是不知道出了什么问题!请注意,可能在调试器和hook exit/\u exit中运行此操作?我不能,我的调试器对我的远程系统不起作用…:请注意,您正在使用哪些库版本?它们中似乎有不少在流通
When using osip, your first task is to initialize the parser and the state machine. This must be done prior to any use of libosip2.

#include <sys/time.h>
#include <osip2/osip.h>
int i;
osip_t *osip;
i=osip_init(&osip);
if (i!=0)
  return -1;
parser_init();