- wireshark收下的流是pcapng格式的,需要另存为pcap格式
- 依赖Npcap SDK
- 不同的需求抽取负载的方法不同,主要是负载的起始位置不同
我的需求:UDP传输,端口为10005,绑定的IP为192.168.178.1
所以设置过滤器"host 192.168.178.1 and udp port 9000"
#define RTP_HEAD_LEN (12)
#define ipFrameFrontLen (16 * 2 + 8 + 2) //UDP负载开始位置
/* 4 bytes IP address */
typedef struct ip_address
{
u_char byte1;
u_char byte2;
u_char byte3;
u_char byte4;
}ip_address;
/* IPv4 header */
typedef struct ip_header
{
u_char ver_ihl; // Version (4 bits) + Internet header length (4 bits)
u_char tos; // Type of service
u_short tlen; // Total length
u_short identification; // Identification
u_short flags_fo; // Flags (3 bits) + Fragment offset (13 bits)
u_char ttl; // Time to live
u_char proto; // Protocol
u_short crc; // Header checksum
ip_address saddr; // Source address
ip_address daddr; // Destination address
u_int op_pad; // Option + Padding
}ip_header;
int main()
{
pcap_t* fp;
char errbuf[
PCAP_ERRBUF_SIZE];
//打开Pcap包
if ((fp = pcap_open_offline(“1.pcap”, errbuf)) == NULL)
{
return 1;
}
std::string packet_filter = "host 192.168.178.1 and udp port 9000"; //过滤(源IP:172.16.21.174且端口8080)
struct bpf_program fcode; // used in pcap_compile()
if (pcap_compile(fp, &fcode, packet_filter.c_str(), 1, 0) >= 0)
{
//设置过滤器
if (pcap_setfilter(fp, &fcode) < 0)
{
fprintf(stderr, "\nError setting the filter.\n");
return 1;
}
}
else
{
fprintf(stderr, "\nError setting the filter.\n");
return 1;
}
//读取文件直到结尾
struct pcap_pkthdr* header; //报文头
const unsigned char* pkt_data; //报文内容
ip_header* ih;
int read_count = 0;
while ((pcap_next_ex(fp, &header, &pkt_data)) > 0)
{
memcpy(buffer, &pkt_data[ipFrameFrontLen], header->caplen- ipFrameFrontLen);
//buff 就是RTP数据
Sleep(1);
}
pcap_close(fp);
}