我正在尝试使用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);
}产出如下:
1.2.3.4:5060 -> 4.3.2.1:5060
copying message with len 333
parsing message with size 333然后它静静地死去。
如果我删除这条线:
osip_message_parse( sip, msg, strlen( msg ) );一切都很完美..。
非常感谢你的帮助!
发布于 2015-02-25 15:49:08
我终于发现了问题。将解析器初始化为
parser_init();任何地方都没有记载:
现在它不再对我造成影响了,但是解析不能正常工作。我需要调查更多。
谢谢大家!
大卫
发布于 2015-02-23 11:51:37
首先,如果以前发生过内存损坏,那么崩溃可能发生在osip_message_parse中,但这可能不是初始损坏的根源。
为了用libosip测试sip消息,您可以进入osip的构建目录并创建一个包含您的sip消息的文件: mymessage.txt。
$> ./src/test/torture_test mymessage.txt 0 -v即使是更深入地检查一下瓦兰的情况:
$> valgrind ./src/test/.libs/torture_test mymessage.txt 0 -v如果您的代码对所有sip消息都失败了,我想问题是libosip之外的内存损坏。
您确实有另一个具有SIP消息大小的错误:
osip_message_parse( sip, msg, strlen( msg ) );SIP消息可以包含包含\0 char的二进制数据,因此您的代码应该使用二进制有效负载的确切长度,而不是strlen()。这种更改是必需的(但不会解决您的主要问题):
osip_message_parse( sip, msg, payload.end() - payload.begin() );我还建议您尝试最新的osip git,并以SIP消息失败的副本完成您的问题。
编辑:正如David发现的,init没有完成,这就是问题的根源。然而,正确的init方式是由第一行文档指定的:
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;https://stackoverflow.com/questions/28651599
复制相似问题