tcpDump包的解封读取(附源代码)

前几天因为项目需要写了下tcpDump包的解包程序,源文件格式Dump文件

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <bitset>



//(1)时间戳
//(2)源IP地址
//(3)目的IP地址
//(4)源端口地址
//(5)目的端口地址
//(6)包的大小

using namespace std;

struct timeStamp
{
	unsigned int sec;						//秒
	unsigned int usec;						//微秒
};
struct tcpDumpHead							//整个tcpDump头
{
	unsigned int	TcpDumpFormatLabel;		//TcpDump格式标记
	unsigned short	MainVersionNumber;		//主版本号
	unsigned short	BranchVersionNumber;	//子版本号
	unsigned int	TimeZone;				//时区
	unsigned int	AccurateTimeStamp;		//精确时间戳
	unsigned int	PacketSize;				//每个数据包大小
	unsigned int	DataLinkType;			//数据链类型
};
struct tcpDumpDataHead						//tcpDumpData头
{
	struct timeStamp TimeStamp;				//时间戳
	unsigned int DatagramLengthSaved;			//本次保存IP数据报长度
	unsigned int DatagramLengthOriginal;	//IP数据报原有长度
};
struct LinkLayerHead						//链路层数据头 14字节
{
	bitset<48> MACAddressDest;
	bitset<48> MACAddressSorce;
	unsigned short protocolType;
};

struct ipheader								//IP报文数据头
{
	bitset<4> Version;						//版本号
	bitset<4> IHL;							//报头长度
	bitset<8> TOS;							//服务类型
	unsigned short ipPacketSize;			//总长度字段
	unsigned short Label;					//标志字段
	bitset<3> LabelBit;						//标志位字段
	bitset<13> Offset;						//段偏移字段
	unsigned char TTL;						//生存期
	unsigned char Protocol;					//协议字段
	bitset<16> TBJYH;						//头部校验和字段  

	unsigned int IPAddressSorce;			//源IP地址
	unsigned int IPAddressDest;				//目的IP地址

	bitset<32> Option;						//可选项字段
};

struct TCPheader							//TCP包头
{
	unsigned short PortNumSorce;			//源端口号
	unsigned short PortNumDest;				//目的端口号
	unsigned int   SeqNum;					//序列号
	unsigned int   AckNum;					//确认序列号
	bitset<4> HeadLength;					//首部长度
	bitset<6> UAPRSF;						//UAPRSF
	unsigned short WindowSize;				//窗口大小
	bitset<16> TCPJYH;						//TCP校验和
	short UrgentPoint;						//紧急指针
	bitset<32> Options;						//可选项字段
};
struct UDPheader							//UDP包头
{
	unsigned short PortNumSorce;			//源端口号
	unsigned short PortNumDest;				//目的端口号
	unsigned short UDPPacketLength;			//UDP包大小
	bitset<16> UDPJYH;						//UDP首部校验和
};

struct tcpDumpResult
{
	unsigned int timeStamp_S;		// 	(1)时间戳(秒)
	unsigned int timeStamp_uS;		// 	(2)时间戳(微秒)
	unsigned int IPAddressSorce;	// 	(3)源IP地址
	unsigned int IPAddressDest;		// 	(4)目的IP地址
	unsigned char protocolType;		//	(5)协议号
	unsigned short PortNumSorce;	// 	(6)源端口地址
	unsigned short PortNumDest;		// 	(7)目的端口地址
	unsigned int packetSize;       //	(8)包大小(包头大小+当前截取数据包大小)    
};

void ipAddressShow(unsigned int ip);
unsigned int readFileToBuf(char *strPath,char *&buffer);
void readtcpDumpHead(char *Buffer,struct tcpDumpHead *tempTcpDumpHead);
void printtcpDumpHead(struct tcpDumpHead *tempTcpDumpHead);
void readtcpDumpDataHead(char *Buffer,	struct tcpDumpDataHead *temptcpDumpDataHead);
void printtcpDumpDataHead(struct tcpDumpDataHead *temptcpDumpDataHead);
unsigned int gettcpDumupDataLengthSaved(struct tcpDumpDataHead *temptcpDumpDataHead);
void readLinkLayerHead(char *Buffer,struct LinkLayerHead *tempLinkLayerHead);
unsigned short GetProtocolType(struct LinkLayerHead *tempLinkLayerHead);
void printLinkLayerHead(struct LinkLayerHead *tempLinkLayerHead);
void readIPHead(char *Buffer,struct ipheader *tempipHead);
void printIPHead(struct ipheader *tempipHead);
unsigned int getIPHeadLength(struct ipheader *tempipHead);
unsigned int getIPPacketSize(struct ipheader *tempipHead);
unsigned char getIPPacketProtocol(struct ipheader *tempipHead);

void readTCPhead(char *Buffer, struct TCPheader	*tempTCPheader);
void printTCPhead(struct TCPheader	*tempTCPheader);
void readUDPhead(char *Buffer, struct UDPheader	*tempUDPheader);
void printUDPhead(struct UDPheader	*tempUDPheader);
void readtcpDumpToFile(char *strPath,char *pFileSavedPath,
									   vector<unsigned int>    &vTimeStamp_S,
										vector<unsigned int>    &vTimeStamp_uS,
										vector<unsigned int>    &IPSorce,
										vector<unsigned int>    &IPDest,
										vector<unsigned short> &PortSorce,
										vector<unsigned short> &PortDest,
										vector<unsigned int>  &packetSize);
char *queryProtocol(unsigned char protocolType);
void ip2Str(unsigned int ip,unsigned char *pStr);








unsigned int readFileToBuf(char *strPath,char *&buffer)
{
	std::ifstream is(strPath, std::ifstream::binary);
	unsigned int length=0;
	if (is) {
		// get length of file:
		is.seekg (0, is.end);
		length = is.tellg();
		is.seekg (0, is.beg);

		buffer = new char [length];

		std::cout << "Reading " << length << " characters... ";
		// read data as a block:
		is.read (buffer,length);

		if (is)
			std::cout << "all characters read successfully."<<endl;
		else
			std::cout << "error: only " << is.gcount() << " could be read"<<endl;
		is.close();
	}
	return length;
}

void readtcpDumpHead(char *Buffer,struct tcpDumpHead *tempTcpDumpHead)
{
	//struct tcpDumpHead tempTcpDumpHead;
	char *BufferCurrent=Buffer;
	unsigned int tempInt;
	unsigned short tempShort;

	memcpy(&tempInt,BufferCurrent,sizeof(int));
	// 	cout<<tempInt<<endl;
	// 	tempInt=htonl(tempInt);
	// 	cout<<tempInt<<endl;
	(*tempTcpDumpHead).TcpDumpFormatLabel=tempInt;
	BufferCurrent+=sizeof(int);

	memcpy(&tempShort,BufferCurrent,sizeof(short));
	//tempShort=htons(tempShort);
	(*tempTcpDumpHead).MainVersionNumber=tempShort;
	BufferCurrent+=sizeof(short);

	memcpy(&tempShort,BufferCurrent,sizeof(short));
	//tempShort=htons(tempShort);
	(*tempTcpDumpHead).BranchVersionNumber=tempShort;
	BufferCurrent+=sizeof(short);

	memcpy(&tempInt,BufferCurrent,sizeof(int));
	//tempInt=htonl(tempInt);
	(*tempTcpDumpHead).TimeZone=tempInt;
	BufferCurrent+=sizeof(int);

	memcpy(&tempInt,BufferCurrent,sizeof(int));
	//tempInt=htonl(tempInt);
	(*tempTcpDumpHead).AccurateTimeStamp=tempInt;
	BufferCurrent+=sizeof(int);

	memcpy(&tempInt,BufferCurrent,sizeof(int));
	//tempInt=htonl(tempInt);
	(*tempTcpDumpHead).PacketSize=tempInt;
	BufferCurrent+=sizeof(int);

	memcpy(&tempInt,BufferCurrent,sizeof(int));
	//tempInt=htonl(tempInt);
	(*tempTcpDumpHead).DataLinkType=tempInt;
	BufferCurrent+=sizeof(int);
};

void printtcpDumpHead(struct tcpDumpHead *tempTcpDumpHead)
{
	cout<<"读取tcpDumpHead内容..."<<endl;
	//数据链类型
	cout<<"TcpDump格式文件标记:";
	cout<<(*tempTcpDumpHead).TcpDumpFormatLabel<<endl;
	cout<<"主版本号:";
	cout<<(*tempTcpDumpHead).MainVersionNumber<<endl;
	cout<<"子版本号:";
	cout<<(*tempTcpDumpHead).BranchVersionNumber<<endl;
	cout<<"时区:";
	cout<<(*tempTcpDumpHead).TimeZone<<endl;
	cout<<"精确时间戳:";
	cout<<(*tempTcpDumpHead).AccurateTimeStamp<<endl;
	cout<<"包大小:";
	cout<<(*tempTcpDumpHead).PacketSize<<endl;
	cout<<"数据链类型:";
	cout<<(*tempTcpDumpHead).DataLinkType<<endl;
}


void readtcpDumpDataHead(char *Buffer,	struct tcpDumpDataHead *temptcpDumpDataHead)
{

	//struct tcpDumpDataHead temptcpDumpDataHead;
	char *BufferCurrent=Buffer;
	unsigned int tempInt;

	memcpy(&tempInt,BufferCurrent,sizeof(int));
	//tempShort=htons(tempShort);
	(*temptcpDumpDataHead).TimeStamp.sec=tempInt;
	BufferCurrent+=sizeof(int);

	memcpy(&tempInt,BufferCurrent,sizeof(int));
	//tempShort=htons(tempShort);
	(*temptcpDumpDataHead).TimeStamp.usec=tempInt;
	BufferCurrent+=sizeof(int);

	memcpy(&tempInt,BufferCurrent,sizeof(int));
	//tempInt=htonl(tempInt);
	(*temptcpDumpDataHead).DatagramLengthSaved=tempInt;
	BufferCurrent+=sizeof(int);

	memcpy(&tempInt,BufferCurrent,sizeof(int));
	//tempInt=htonl(tempInt);
	(*temptcpDumpDataHead).DatagramLengthOriginal=tempInt;
	BufferCurrent+=sizeof(int);
}

void printtcpDumpDataHead(struct tcpDumpDataHead *temptcpDumpDataHead)
{
	cout<<"读取tcpDumpDataHead内容..."<<endl;
	cout<<"时间戳(秒):";
	cout<<(*temptcpDumpDataHead).TimeStamp.sec<<endl;
	cout<<"时间戳(微秒):";
	cout<<(*temptcpDumpDataHead).TimeStamp.usec<<endl;
	cout<<"本次保存数据报文长度:";
	cout<<(*temptcpDumpDataHead).DatagramLengthSaved<<endl;
	cout<<"原有数据报文长度:";
	cout<<(*temptcpDumpDataHead).DatagramLengthOriginal<<endl;
}
unsigned int gettcpDumupDataLengthSaved(struct tcpDumpDataHead *temptcpDumpDataHead)
{
	return temptcpDumpDataHead->DatagramLengthSaved;
}


void readLinkLayerHead(char *Buffer,struct LinkLayerHead *tempLinkLayerHead)
{
	char *BufferCurrent=Buffer+12;
	unsigned short tempShort;
	memcpy(&tempShort,BufferCurrent,sizeof(short));
	(*tempLinkLayerHead).protocolType=tempShort;
}


unsigned short GetProtocolType(struct LinkLayerHead *tempLinkLayerHead)  
{
	return (*tempLinkLayerHead).protocolType;
};

void printLinkLayerHead(struct LinkLayerHead *tempLinkLayerHead)
{
	cout<<"读取LinkLayerHead数据..."<<endl;
	cout<<"协议号:";
	cout<<(*tempLinkLayerHead).protocolType<<endl;
}


void readIPHead(char *Buffer,struct ipheader *tempipHead)
{
	char *BufferCurrent=Buffer;

	unsigned char tempChar;
	memcpy(&tempChar,BufferCurrent,sizeof(char));
	for (int i=3;i>=0;i--)
	{
		if (tempChar&0x80)
		{
			(*tempipHead).Version[i]=1;
		}
		else
			(*tempipHead).Version[i]=0;
		tempChar<<=1;
	}
	for (int i=3;i>=0;i--)
	{
		if (tempChar&0x80)
		{
			(*tempipHead).IHL[i]=1;
		}
		else
			(*tempipHead).IHL[i]=0;
		tempChar<<=1;
	}
	BufferCurrent+=sizeof(char);

	BufferCurrent+=sizeof(char);

	unsigned short tempShort;
	memcpy(&tempShort,BufferCurrent,sizeof(short));
	tempipHead->ipPacketSize=tempShort;
	BufferCurrent+=sizeof(short);

	BufferCurrent=Buffer+9;
	memcpy(&tempChar,BufferCurrent,sizeof(char));
	tempipHead->Protocol=tempChar;


	BufferCurrent=Buffer+12;
	unsigned int tempInt;

	memcpy(&tempInt,BufferCurrent,sizeof(int));
	(*tempipHead).IPAddressSorce=tempInt;
	BufferCurrent+=sizeof(int);

	memcpy(&tempInt,BufferCurrent,sizeof(int));
	(*tempipHead).IPAddressDest=tempInt;
	BufferCurrent+=sizeof(int);

}

void printIPHead(struct ipheader *tempipHead)
{
	cout<<"读取IPHead数据..."<<endl;
	cout<<"版本号:";
	cout<<(*tempipHead).Version.to_ulong()<<endl;
	cout<<"IP包头长度:";
	cout<<(*tempipHead).IHL.to_ulong()*4<<endl;

	cout<<"IP包总长:";
	cout<<(*tempipHead).ipPacketSize<<endl;

	cout<<"源IP地址:";
	cout<<(*tempipHead).IPAddressSorce<<endl;
	ipAddressShow((*tempipHead).IPAddressSorce);
	cout<<"目的IP地址:";
	cout<<(*tempipHead).IPAddressDest<<endl;
	ipAddressShow((*tempipHead).IPAddressDest);
}

unsigned int getIPHeadLength(struct ipheader *tempipHead)
{
	return (*tempipHead).IHL.to_ulong()*4;
}

unsigned int getIPPacketSize(struct ipheader *tempipHead)
{
	return tempipHead->ipPacketSize;
}

unsigned char getIPPacketProtocol(struct ipheader *tempipHead)
{
	return tempipHead->Protocol;
}
void ipAddressShow(unsigned int ip)
{	
	unsigned char* ipTemp=(unsigned char*)&ip;
	unsigned char tempchar[4];
	for (int i=0;i<4;i++)
	{
		memcpy(&tempchar[i],ipTemp,sizeof(char));
		ipTemp+=1;
	}
	for (int i=0;i<4;i++)
	{
		cout<<(unsigned short)tempchar[i];
		if (i<3)
		{
			cout<<".";
		}
	}
	cout<<endl;
}
void readTCPhead(char *Buffer, struct TCPheader	*tempTCPheader)
{
	char *BufferCurrent=Buffer;
	unsigned short tempShort;

	memcpy(&tempShort,BufferCurrent,sizeof(short));
	tempTCPheader->PortNumSorce=tempShort;
	BufferCurrent+=sizeof(short);

	memcpy(&tempShort,BufferCurrent,sizeof(short));
	tempTCPheader->PortNumDest=tempShort;
	BufferCurrent+=sizeof(short);

}



void printTCPhead(struct TCPheader	*tempTCPheader)
{
	cout<<"读取TCPhead数据..."<<endl;
	cout<<"TCP源端口号";
	cout<<tempTCPheader->PortNumSorce<<endl;
	cout<<"TCP目的端口号";
	cout<<tempTCPheader->PortNumDest<<endl;

}
void readUDPhead(char *Buffer, struct UDPheader	*tempUDPheader)
{
	char *BufferCurrent=Buffer;
	unsigned short tempShort;

	memcpy(&tempShort,BufferCurrent,sizeof(short));
	tempUDPheader->PortNumSorce=tempShort;
	BufferCurrent+=sizeof(short);

	memcpy(&tempShort,BufferCurrent,sizeof(short));
	tempUDPheader->PortNumDest=tempShort;
	BufferCurrent+=sizeof(short);
}
void printUDPhead(struct UDPheader	*tempUDPheader)
{
	cout<<"读取UDPhead数据..."<<endl;
	cout<<"UDP源端口号";
	cout<<tempUDPheader->PortNumSorce<<endl;
	cout<<"UDP目的端口号";
	cout<<tempUDPheader->PortNumDest<<endl;
}



void readtcpDumpToFile(char *strPath,char *pFileSavedPath,
					   vector<unsigned int>    &vTimeStamp_S,
					   vector<unsigned int>    &vTimeStamp_uS,
					   vector<unsigned int>    &IPSorce,
					   vector<unsigned int>    &IPDest,
					   vector<unsigned short> &PortSorce,
					   vector<unsigned short> &PortDest,
					   vector<unsigned int>  &packetSize)
{
	char *buffer=NULL;
	unsigned int nLen=readFileToBuf(strPath,buffer);
	if (nLen==0)
	{
		return;
	}

	char *BufferCurrent=buffer;

	struct tcpDumpHead temptcpDumpHead;
	struct tcpDumpDataHead temptcpDumpDataHead;
	struct LinkLayerHead tempLinkLayerHead;
	struct ipheader tempipheader;
	struct TCPheader tempTCPheader;
	struct UDPheader tempUDPheader;

	struct tcpDumpResult temptcpDumpResult;

	readtcpDumpHead(BufferCurrent,&temptcpDumpHead);

	BufferCurrent+=24;
	printtcpDumpHead(&temptcpDumpHead);


	FILE *fp=fopen(pFileSavedPath,"w");
	if (fp==NULL)
	{
		cout<<"File Open Error"<<endl;
		return;
	}
	fprintf(fp,"TimeStamp(S)\tTimeStamp(uS)\tPacketSize\t\t\t\tIPAddressSorce\t\t\t\t\t\t\t\t\tIPAddressDest\t\t\t\t\tType(TCP6_UDP17)\tPortNumSorce\tPortNumDest\n");
	unsigned int DataGramNum=0;
	unsigned int statis[256];
	memset(statis,0,sizeof(int)*256);
	unsigned char pStrforIP[20];
	
	cout<<"运行中......\n"<<endl;
	while(BufferCurrent<buffer+nLen)
	{
		//cout<<"***********************数据报编号"<<DataGramNum<<"******************************"<<endl;
		//整个包头
		readtcpDumpDataHead(BufferCurrent,&temptcpDumpDataHead);
		//printtcpDumpDataHead(&temptcpDumpDataHead);
		BufferCurrent+=16;
		char *pNextStart=BufferCurrent+gettcpDumupDataLengthSaved(&temptcpDumpDataHead);


		//包身
		readLinkLayerHead(BufferCurrent,&tempLinkLayerHead);  //链路层14字节
		//printLinkLayerHead(&tempLinkLayerHead);
		BufferCurrent+=14;

		readIPHead(BufferCurrent,&tempipheader);
		//printIPHead(&tempipheader);
		unsigned int ipHeadLength=getIPHeadLength(&tempipheader);
		unsigned int ipPaketSize=getIPPacketSize(&tempipheader);
		BufferCurrent+=ipHeadLength;

		unsigned char tempProtocol=getIPPacketProtocol(&tempipheader);
		statis[tempProtocol]+=1;
		switch (tempProtocol)  //此处留待扩展
		{
		case	0:		//HOPOPT
			//cout<<"协议类型:HOPOPT"<<endl;
			break;
		case	1:		//ICMP
			//cout<<"协议类型:ICMP"<<endl;
			break;
		case	2:		//IGMP
			//cout<<"协议类型:IGMP"<<endl;
			break;
		case	4:		//IP
			//cout<<"协议类型:IP"<<endl;
			break;
		case	6:		//TCP
			//cout<<"协议类型:TCP"<<endl;
			readTCPhead(BufferCurrent,&tempTCPheader);
			//printTCPhead(&tempTCPheader);
			break;	
		case	17:		//UDP
			//cout<<"协议类型:UDP"<<endl;
			readUDPhead(BufferCurrent,&tempUDPheader);
			//printUDPhead(&tempUDPheader);
			break;
		case	45:		//IDRP
			//cout<<"协议类型:IDRP"<<endl;
			break;
		case	46:		//RSVP
			//cout<<"协议类型:RSVP"<<endl;
			break;
		case	47:		//GRE
			//cout<<"协议类型:GRE"<<endl;
			break;
		case	52:		//INLSP
			//cout<<"协议类型:INLSP"<<endl;
			break;
		case	54:		//NHRP
			//cout<<"协议类型:NHRP"<<endl;
			break;
		case	88:		//IGRP
			//cout<<"协议类型:IGRP"<<endl;
			break;
		case	89:		//OSPF
			//cout<<"协议类型:OSPF"<<endl;
			break;
		default:
			//cout<<"未知协议类型"<<tempProtocol<<" 警告!"<<endl;
			//getchar();
			break;
		}

		BufferCurrent=pNextStart;

	

		temptcpDumpResult.IPAddressDest=tempipheader.IPAddressDest;
		temptcpDumpResult.IPAddressSorce=tempipheader.IPAddressSorce;
		temptcpDumpResult.protocolType=tempipheader.Protocol;
		temptcpDumpResult.timeStamp_S=temptcpDumpDataHead.TimeStamp.sec;
		temptcpDumpResult.timeStamp_uS=temptcpDumpDataHead.TimeStamp.usec;
		temptcpDumpResult.packetSize=temptcpDumpDataHead.DatagramLengthSaved+16;
		if (temptcpDumpResult.protocolType==6)
		{
			temptcpDumpResult.PortNumDest=tempTCPheader.PortNumDest;
			temptcpDumpResult.PortNumSorce=tempTCPheader.PortNumSorce;
		}
		else if (temptcpDumpResult.protocolType==17)
		{
			temptcpDumpResult.PortNumDest=tempUDPheader.PortNumDest;
			temptcpDumpResult.PortNumSorce=tempUDPheader.PortNumSorce;
		}
		else
		{
			temptcpDumpResult.PortNumDest=0;
			temptcpDumpResult.PortNumSorce=0;
		}

		vTimeStamp_S.push_back(temptcpDumpResult.timeStamp_S);
		vTimeStamp_uS.push_back(temptcpDumpResult.timeStamp_uS);
		IPSorce.push_back(temptcpDumpResult.IPAddressSorce);
		IPDest.push_back(temptcpDumpResult.IPAddressDest);
		PortSorce.push_back(temptcpDumpResult.PortNumSorce);
		PortDest.push_back(temptcpDumpResult.PortNumDest);
		packetSize.push_back(temptcpDumpResult.packetSize);

		fprintf(fp,"%12u\t%9u\t%9u\t",
			temptcpDumpResult.timeStamp_S,
			temptcpDumpResult.timeStamp_uS,
			temptcpDumpResult.packetSize);
		fprintf(fp,"%15u\t",temptcpDumpResult.IPAddressSorce);
		ip2Str(temptcpDumpResult.IPAddressSorce,pStrforIP);
		fprintf(fp,"%16s\t",pStrforIP);
		fprintf(fp,"%12u\t",temptcpDumpResult.IPAddressDest);
		ip2Str(temptcpDumpResult.IPAddressDest,pStrforIP);
		fprintf(fp,"%16s",pStrforIP);
			
		fprintf(fp,"%8u\t",temptcpDumpResult.protocolType);
		fprintf(fp,"%6s",queryProtocol(temptcpDumpResult.protocolType));
	
		fprintf(fp,"%12u\t%12u\n",
			temptcpDumpResult.PortNumSorce,
			temptcpDumpResult.PortNumDest);
		DataGramNum++;

	}

	fprintf(fp,"总结:\n本次截取数据包数:%d\n",DataGramNum);

	for (int i=0;i<256;i++)
	{
		if (statis[i])
		{
			fprintf(fp,"%d %s:%d\n",i,queryProtocol(i),statis[i]);
		}
	}
	cout<<"运行结束,按回车返回"<<endl;
	getchar();
	delete []buffer;
	fclose(fp);
}

char *queryProtocol(unsigned char protocolType)
{

	switch(protocolType)
	{ 
	case 0: return "HOPOPT"; //IPv6逐跳选项 
	case 1: return "ICMP"; //控制消息 
	case 2: return "IGMP"; //组管理 
	case 3: return "GGP"; //网关对网关 
	case 4: return "IP in IP"; //IP中的IP(封装) 
	case 5: return "ST"; //流 
	case 6: return "TCP"; //TCP传输控制 
	case 7: return "CBT"; //CBT 
	case 8: return "EGP"; //外部网关协议 
	case 9: return "IGB"; //任何专用内部网关(Cisco将其用于IGRP) 
	case 10: return "BBN-RCC-MON"; //BBN RCC监视 
	case 11: return "NVP-II"; //网络语音协议 
	case 12: return "PUP"; //PUP 
	case 13: return "ARGUS"; //ARGUS 
	case 14: return "EMCON"; //EMCON 
	case 15: return "XNET"; //跨网调试器 
	case 16: return "CHAOS"; //Chaos 
	case 17: return "UDP"; //用户数据报 
	case 18: return "MUX"; //多路复用 
	case 19: return "DCN-MEAS"; //DCN测量子系统 
	case 20: return "HMP"; //主机监视 
	case 21: return "PRM"; //数据包无线测量 
	case 22: return "Xns_IDP"; //XEROX NS IDP 
	case 23: return "TRUNK-1"; //第1主干 
	case 24: return "TRUNK-2"; //第2主干 
	case 25: return "LEAF-1"; //第1叶 
	case 26: return "LEAF-2"; //第2叶 
	case 27: return "RDP"; //可靠数据协议 
	case 28: return "IRTP"; //Internet可靠事务 
	case 29: return "ISO-TP4"; //ISO传输协议第4类 
	case 30: return "NETBLT"; //批量数据传输协议 
	case 31: return "MFE-NSP"; //MFE网络服务协议 
	case 32: return "MERIT-INP"; //MERIT节点间协议 
	case 33: return "SEP"; //顺序交换协议 
	case 34: return "3PC"; //第三方连接协议 
	case 35: return "IDPR"; //IDPR域间策略路由协议 
	case 36: return "XTP"; //XTP 
	case 37: return "DDP"; //数据报传送协议 
	case 38: return "IDPR-CMTP"; //IDPR控制消息传输协议 
	case 39: return "TP++"; //TP++传输协议 
	case 40: return "IL"; //IL传输协议 
	case 41: return "IPv6"; //IPv6 
	case 42: return "SDRP"; //源要求路由协议 
	case 43: return "IPv6-Route"; //IPv6的路由标头 
	case 44: return "IPv6-Frag"; //IPv6的片段标头 
	case 45: return "IDRP"; //域间路由协议 
	case 46: return "RSVP"; //保留协议 
	case 47: return "GRE"; // 通用路由封装 
	case 48: return "MHRP"; // 移动主机路由协议 
	case 49: return "BNA"; // BNA 
	case 50: return "ESP"; // IPv6 的封装安全负载 
	case 51: return "AH"; // IPv6 的身份验证标头 
	case 52: return "I-NLSP"; // 集成网络层安全性 TUBA 
	case 53: return "SWIPE"; // 采用加密的 IP 
	case 54: return "NARP"; // NBMA 地址解析协议 
	case 55: return "MOBILE IP"; // 移动性 
	case 56: return "TLSP"; // 传输层安全协议使用 Kryptonet 密钥管理 
	case 57: return "SKIP"; // SKIP 
	case 58: return "IPv6-ICMP"; // 用于 IPv6 的 ICMP 
	case 59: return "IPv6-NoNxt"; // 用于 IPv6 的无下一个标头 
	case 60: return "IPv6-Opts"; // IPv6 的目标选项 
	case 61: return "Anyone Betwen Hosts"; // 任意主机内部协议 
	case 62: return "CFTP"; // CFTP 
	case 63: return "Anyone of LocalHost"; // 任意本地网络 
	case 64: return "SAT-EXPAK"; // SATNET 与后台 EXPAK 
	case 65: return "KRYPTOLAN"; // Kryptolan 
	case 66: return "RVD MIT"; // 远程虚拟磁盘协议 
	case 67: return "IPPC"; // Internet Pluribus 数据包核心 
	case 68: return "Any Distributed File System"; //任意分布式文件系统 
	case 69: return "SAT-MON"; // SATNET 监视 
	case 70: return "VISA"; // VISA 协议 
	case 71: return "IPCV"; // Internet 数据包核心工具 
	case 72: return "CPNX"; // 计算机协议网络管理 
	case 73: return "CPHB"; // 计算机协议检测信号 
	case 74: return "WSN"; // 无线电脑网络 
	case 75: return "PVP"; // 数据包视频协议 
	case 76: return "BR-SAT-MON"; // 后台 SATNET 监视 
	case 77: return "SUN-ND"; // SUN ND PROTOCOL-Temporary 
	case 78: return "WB-MON"; // WIDEBAND 监视 
	case 79: return "WB-EXPAK"; // WIDEBAND EXPAK 
	case 80: return "ISO-IP"; // ISO Internet 协议 
	case 81: return "VMTP"; // VMTP 
	case 82: return "SECURE-VMTP"; // SECURE-VMTP 
	case 83: return "VINES VINES"; 
	case 84: return "TTP"; // TTP 
	case 85: return "NSFNET-IGP"; // NSFNET-IGP 
	case 86: return "DGP"; // 异类网关协议 
	case 87: return "TCF"; // TCF 
	case 88: return "EIGRP"; // EIGRP 
	case 89: return "OSPFIGP"; // OSPFIGP 
	case 90: return "Sprite-RPC"; // Sprite RPC 协议 
	case 91: return "LARP"; // 轨迹地址解析协议 
	case 92: return "MTP"; // 多播传输协议 
	case 93: return "AX.25"; // AX.25 帧 
	case 94: return "IPIP"; // IP 中的 IP 封装协议 
	case 95: return "MICP"; // 移动互联控制协议 
	case 96: return "SCC-SP"; // 信号通讯安全协议 
	case 97: return "ETHERIP"; // IP 中的以太网封装 
	case 98: return "ENCAP"; // 封装标头 
	case 99: return "Any Encrypt Plan"; //任意专用加密方案 
	case 100: return "GMTP"; // GMTP 
	case 101: return "IFMP"; // Ipsilon 流量管理协议 
	case 102: return "PNNI"; // IP 上的 PNNI 
	case 103: return "PIM"; // 独立于协议的多播 
	case 104: return "ARIS"; // ARIS 
	case 105: return "SCPS"; // SCPS 
	case 106: return "QNX"; // QNX 
	case 107: return "A/N"; // 活动网络 
	case 108: return "IPComp"; // IP 负载压缩协议 
	case 109: return "SNP"; // Sitara 网络协议 
	case 110: return "Compaq-Peer"; // Compaq 对等协议 
	case 111: return "IPX-in-IP"; // IP 中的 IPX 
	case 112: return "VRRP"; // 虚拟路由器冗余协议 
	case 113: return "PGM"; // PGM 可靠传输协议 
	case 114: return "Zero Hop Protocal"; //任意 0 跳协议 
	case 115: return "L2TP"; // 第二层隧道协议 
	case 116: return "DDX"; // D-II 数据交换 (DDX) 
	case 117: return "IATP"; // 交互式代理传输协议 
	case 118: return "STP"; // 计划传输协议 
	case 119: return "SRP"; // SpectraLink 无线协议 
	case 120: return "UTI"; // UTI 
	case 121: return "SMP"; // 简单邮件协议 
	case 122: return "SM"; // SM 
	case 123: return "PTP"; // 性能透明协议 
	case 124: return "ISIS"; // over IPv4 
	case 125: return "FIRE"; // 
	case 126: return "CRTP"; // Combat 无线传输协议 
	case 127: return "CRUDP"; // Combat 无线用户数据报 
	case 128: return "SSCOPMCE"; // 
	case 129: return "IPLT"; // 
	case 130: return "SPS"; // 安全数据包防护 
	case 131: return "PIPE"; // IP 中的专用 IP 封装 
	case 132: return "SCTP"; // 流控制传输协议 
	case 133: return "FC"; // 光纤通道 
	case 255: return "保留"; // 
	default: return "未分配"; 
	}

}
void ip2Str(unsigned int ip,unsigned char *pStr)
{
	unsigned char* ipTemp=(unsigned char*)&ip;
	unsigned char tempchar[4];
	//for (int i=0;i<4;i++)
	//{
	//	memcpy(&tempchar[i],ipTemp,sizeof(char));
	//	ipTemp+=1;
	//}
	tempchar[3] = ip >> 24;
	tempchar[2] = ip >> 16;
	tempchar[1] = ip >> 8;
	tempchar[0] = ip;

	int length = 0;
	for (int i = 0; i < 4; i++)
	{
		if (tempchar[i] == 0)
		{
			pStr[length++] = '0';
		}
		else
		{
			if (tempchar[i] /100 == 0)
			{
				if (tempchar[i] / 10 == 0)
				{
					pStr[length++] = tempchar[i] + '0';
				}
				else
				{
					pStr[length++] = tempchar[i]/10 + '0';
					pStr[length++] = tempchar[i]%10 + '0';
				}
			}
			else
			{
				pStr[length++]  =  tempchar[i]/100 + '0';
				tempchar[i] = tempchar[i]%100;
				pStr[length++]  =  tempchar[i]/10 + '0';
				pStr[length++]  =  tempchar[i]%10 + '0';
			}
		}
		pStr[length++] = '.';
	}
	pStr[length-1] = '\0';
}


版权声明:本文为wangxin110000原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。