1. 程式人生 > 其它 >wireshark 抓包rtp over tcp分析PS/H264負載的過程

wireshark 抓包rtp over tcp分析PS/H264負載的過程

wireshark抓包之後

/*
 * describe: analyze rtp over tcp.
 * create time: 2021年11月16日 星期二 19時14分24秒 CST
 * author: [email protected]
 */
#include <iostream>
#include <string>
#include <fstream>
#include <stdint.h>
using namespace std;

streamsize Read(istream &stream, char* buffer, streamsize count)
{
    streamsize reads 
= stream.rdbuf()->sgetn(buffer, count); stream.rdstate(); stream.peek(); return reads; } int main(int argc, char* argv[]) { if (argc < 2) { cerr << "usage: " << argv[0] << " [inputfile]" << endl; exit(1); } string input_file = argv[1
]; // 以讀模式開啟檔案 ifstream infile; infile.open(input_file); if (!infile.is_open()) { cerr << "Canot open " << input_file << endl; exit(-1); } // 以寫模式開啟檔案 ofstream outfile; outfile.open("output.ps"); if (!outfile.is_open()) { cerr
<< "Canot open " << "ourput.ps" << endl; exit(-1); } long count = 0; cout << "start parse rtp/tcp stream file " << input_file << endl; while (!infile.eof()) { // 讀tcp header len uint16_t tcp_header_len = 0; char* tcp_header = (char*)&tcp_header_len; size_t read = Read(infile, tcp_header, 2); if (infile.eof()) { cerr << "end of file after read tcp header len" << endl; break; } count += read; uint16_t tmp_len = tcp_header_len; tcp_header_len = (tcp_header_len << 8) | (tmp_len >> 8); if (read != 2 || tcp_header_len <= 2) { cerr << "read tcp_header len failed!" << endl; break; } cout << " tcp payload len = " << tcp_header_len << endl; if (tcp_header_len >= 1500) { cout << "tcp_header_len may failed from here. count num=" << count << endl; } // 讀rtpheader char rtp_header[12]= {0}; read = Read(infile, rtp_header, 12); if (infile.eof()) { cout << "end of file after read tcp header len" << endl; break; } if (read != 12) { cout << "read tcp_header len failed!" << endl; break; } count += read; for (unsigned char n : rtp_header) { cout << "0x" << std::hex << static_cast<unsigned short>(n) << " "; } cout << endl; // 讀ps char *buffer = new char(tcp_header_len - 12); if (buffer == NULL) { cerr << "failed new buffer" << endl; break; } read = Read(infile, buffer, tcp_header_len - 12); if (read != tcp_header_len - 12) { cerr << "read tcp_header len failed! " << endl; break; } count += read; // 寫檔案 outfile.write(buffer, tcp_header_len - 12); outfile.flush(); } cout << "end of parse rtp/tcp stream." << endl; // 關閉開啟的檔案 infile.close(); outfile.close(); }