c++解析wireshark中的rtp包

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());
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章