c++解析wireshark中的rtp包

来源:互联网 发布:免费外汇交易软件 编辑:程序博客网 时间:2024/06/05 04:00

1.使用wireshark抓取rtp的包,点击一下你要解析的某一个包,然后点击左上角的“文件”-->“导出分组解析结果”-->“As JSON”-->选择"select packet"-->点击"保存"


2.现在打开保存的json文件可以看到对应的信息,比如rtp的传输内容的信息,包头的信息等。c++具体操作为:通过c++获取文件操作,先把json文件中的数据读取到一个string字符串中,使用jsoncpp或者poco库中的json来解析,博主使用的是poco的库,在使用时需要先把json文件的最前面和最后面的'[' ']'删除掉才可以解析,因为这个我找了很久程序崩溃的问题。

下面给出程序,需要poco库的支持,大家可以学习一下


util.h

#pragma once#include <iostream>#include <fstream>#include <string>#include <Poco/File.h>#include <Poco/FileStream.h>#include <Poco/FileStream_WIN32.h>#include <Poco/JSON/Parser.h>#include <Poco/JSON/ParseHandler.h>#include <Poco/JSON/Query.h>#include <Poco/JSON/Object.h>#include <Poco/Dynamic/Var.h>#include <Poco/Util/JSONConfiguration.h>#include <Poco/AutoPtr.h>#include <Poco/Util/Application.h>using Poco::File;using Poco::FileStream;using Poco::FileInputStream;using Poco::FileStreamBuf;using Poco::Dynamic::Var;using Poco::JSON::Parser;using Poco::JSON::ParseHandler;using Poco::JSON::Query;using Poco::JSON::Object;using Poco::Util::JSONConfiguration;using Poco::AutoPtr;using Poco::Util::Application;


resolve_rtp.h

#pragma once#include "util.h"struct RTP_Header{unsigned __int16 csrc_count;unsigned __int16 extension;unsigned __int16 padding;unsigned __int16 version;unsigned __int16 payloadtype;unsigned __int16 marker;unsigned __int16 seq;unsigned __int32 timestamp;unsigned __int32 ssrc;};class ResolveRtp{public:ResolveRtp(std::string &path):_filePath(path){ReadJson();GetRtpHeaderInfo();}RTP_Header GetRtpHeader(){return _rtpHeader;}private:void GetRtpHeaderInfo();void ReadJson();int GiveKeyGetVal(std::vector<std::string> nameVec, std::string name);std::string GetVal(std::vector<std::string> &nameVec);private:std::string _filePath;Object::Ptr _object;RTP_Header _rtpHeader;};


main.cpp

#include "resolve_rtp.h"int main(){std::string filePath("C:/Users/CC000033/Desktop/test.json");ResolveRtp resRtp(filePath);RTP_Header header = resRtp.GetRtpHeader();return 0;}


resolve_rtp.cpp

#include "resolve_rtp.h"#include <Poco/FileStream.h>#include <fstream>void ResolveRtp::ReadJson(){Parser parser;std::string content;FILE *stream;std::ifstream infile;infile.open(_filePath);   //将文件流对象与文件连接起来 std::string s;while (getline(infile, s)){content += s;}infile.close();             //关闭文件输入流 parser.parse(content);Var result = parser.result();_object = result.extract<Object::Ptr>();}std::string ResolveRtp::GetVal(std::vector<std::string> &nameVec){std::string val;int vecSize = 0;Object::Ptr tmpObj = _object;Var res;vecSize = nameVec.size();for (int i = 0; i < vecSize; ++i){std::string &name = nameVec[i];res = tmpObj->get(name);if (i != vecSize - 1){tmpObj = res.extract<Object::Ptr>();}}val = res.convert<std::string>();return val;}void ResolveRtp::GetRtpHeaderInfo(){std::vector<std::string> nameVec;nameVec.push_back("_source");nameVec.push_back("layers");nameVec.push_back("rtp");_rtpHeader.version = GiveKeyGetVal(nameVec, "rtp.version");_rtpHeader.csrc_count = GiveKeyGetVal(nameVec, "rtp.cc");_rtpHeader.extension = GiveKeyGetVal(nameVec, "rtp.ext");_rtpHeader.padding = GiveKeyGetVal(nameVec, "rtp.padding");_rtpHeader.payloadtype = GiveKeyGetVal(nameVec, "rtp.payload");_rtpHeader.marker = GiveKeyGetVal(nameVec, "rtp.marker");_rtpHeader.seq = GiveKeyGetVal(nameVec, "rtp.seq");_rtpHeader.timestamp = GiveKeyGetVal(nameVec, "rtp.timestamp");_rtpHeader.ssrc = GiveKeyGetVal(nameVec, "rtp.ssrc");}int ResolveRtp::GiveKeyGetVal(std::vector<std::string> nameVec, std::string name){nameVec.push_back(name);std::string str = GetVal(nameVec);return atoi(str.data());}


原创粉丝点击