你知道在 cmd 输入 ping 之后发生了什么吗? —— 详解 ICMP 协议

时间:2022-07-22
本文章向大家介绍你知道在 cmd 输入 ping 之后发生了什么吗? —— 详解 ICMP 协议,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在解释之前我们先来了解一下 ICMP 报文。

ICMP 报文 ?

❔ 为什么要引入 ICMP 协议 ?

  • ✅ 为了更有效地转发 IP 数据报和提高交付成功的机会,在网际层使用了网际控制报文协议 ICMP (Internet Control Message Protocol)。
  • ✅ ICMP 是互联网的标准协议。
  • ✅ ICMP 允许主机或路由器报告差错情况和提供有关异常情况的报告。
  • ✅ ICMP 不是高层协议(因为 ICMP 报文是装在 IP 数据报中,作为其中的数据部分),它是 IP 层的协议。

ICMP 报文的格式

ICMP 报文的种类

  • ?ICMP 报文的种类有两种,即 ICMP 差错报告报文和 ICMP 询问报文。
  • ?ICMP 报文的前 4 个字节是统一的格式,共有三个字段:即类型、代码和检验和。接着的 4 个字节的内容与 ICMP 的类型有关。

ICMP 差错报告报文

ICMP 差错报告报文共有 4 种:

  • 1️⃣ 终点不可达
  • 2️⃣ 时间超过
  • 3️⃣ 参数问题
  • 4️⃣ 改变路由(重定向)(Redirect)

ICMP 差错报告报文的数据字段的内容:

❔既然是差错报告报文,那么他肯定是有差错的时候才发送的吧 ?

下面我们来看一下它什么时候不需要发送:

  • 1️⃣ 对 ICMP 差错报告报文不再发送 ICMP 差错报告报文。
  • 2️⃣ 对第一个分片的数据报片的所有后续数据报片都不发送 ICMP 差错报告报文。
  • 3️⃣ 对具有多播地址的数据报都不发送 ICMP 差错报告报文。
  • 4️⃣ 对具有特殊地址(如127.0.0.0 或 0.0.0.0)的数据报不发送 ICMP 差错报告报文。

ICMP 询问报文

询问报文有两种:

  • 1️⃣ 回送请求和回答报文
  • 2️⃣ 时间戳请求和回答报文

说了那么多好像和标题咩有任何关系额?,下面我们就来回答标题的问题。

ICMP的应用举例

其实有一个最常见的例子,就是我们常用的 ping 操作,我们常常使用 ping 来看一下网络连接是否畅通?。

  • ?PING (Packet InterNet Groper)
  • PING 用来测试两个主机之间的连通性。
  • PING 使用了 ICMP 回送请求与回送回答报文。
  • PING 是应用层直接使用网络层 ICMP 的例子,它没有通过运输层的 TCP 或UDP。

也就是说直接从应用层跳到网络层?。

再来看一个Traceroute 的应用举例

  • 1️⃣ 在 Windows 操作系统中这个命令是 tracert。
  • 2️⃣ 用来跟踪一个分组从源点到终点的路径。
  • 3️⃣ 它利用 IP 数据报中的 TTL 字段和 ICMP 时间超过差错报告报文实现对从源点到终点的路径的跟踪。

? 最后附上使用 C# 模拟 ping 指令的部分源码:

namespace SaurabhPing
{
	using System;
	using System.Net;
	using System.Net.Sockets;
	/// <summary>
	///		The Main Ping Class
	/// </summary>
	class Ping
	{
		//声明常量
		const int SOCKET_ERROR = -1;        
		const int ICMP_ECHO = 8;
		
		public static void Main(string[] argv)
		{
			if(argv.Length==0)
			{
				//如果用户没有输入任何参数则给出提示
				Console.WriteLine("Usage:Ping <hostname> /r") ;
				Console.WriteLine("<hostname> The name of the Host who you want to ping");
				Console.WriteLine("/r Optional Switch to Ping the host continuously") ;
			}
			else if(argv.Length==1)
			{
				//即为用户提供的主机名
				//调用 PingHost 方法并将主机名作为参数传递给它
				PingHost(argv[0]) ;
			}
			else if(argv.Length==2)
			{
				//用户提供了主机名和循环参数(开关,“/r”)
				if(argv[1]=="/r")
				{
					//无限重复下去
					while(true)
					{
						//调用"PingHost"方法并把主机名作为参数传递过去
						PingHost(argv[0]) ;
					}
				}
				else
				{
					//如果用户还提供了其他的参数则忽略
					PingHost(argv[0]) ;
				}
				
			}
			else
			{
				//出现错误
				Console.WriteLine("Error in Arguments") ;
			}
		}
		
		/// <summary>
		///		这个方法以主机名作为参数ping远程主机,并显示回复时间
		/// </summary>
		public static void PingHost(string host)
		{
			//声明IPHostEntry 
			IPHostEntry serverHE, fromHE;
			int nBytes = 0;
			int dwStart = 0, dwStop = 0;
			//初始化一个ICMP类型的Socket
			Socket socket = new Socket(AddressFamily.InterNetwork , SocketType.Raw, ProtocolType.Icmp);
			// 取得目标主机的主机名
			try
			{
				serverHE = Dns.GetHostByName(host);	
			}
			catch(Exception)
			{
				Console.WriteLine("目标主机不存在"); // 失败
				return ;
			}
			IPEndPoint ipepServer = new IPEndPoint(serverHE.AddressList[0], 0);
			EndPoint epServer = (ipepServer);	
			fromHE = Dns.GetHostByName(Dns.GetHostName());
			IPEndPoint ipEndPointFrom = new IPEndPoint(fromHE.AddressList[0], 0);        		EndPoint EndPointFrom = (ipEndPointFrom);
			int PacketSize = 0;
			IcmpPacket packet = new IcmpPacket();
			// 构造数据报
			packet.Type = ICMP_ECHO; //8
			packet.SubCode = 0;
			packet.CheckSum = UInt16.Parse("0");
			packet.Identifier   = UInt16.Parse("45"); 
			packet.SequenceNumber  = UInt16.Parse("0"); 
			int PingData = 32; // sizeof(IcmpPacket) - 8;
			packet.Data = new Byte[PingData];
			//初始化 Packet.Data
			for (int i = 0; i < PingData; i++)
			{
				packet.Data[i] = (byte)'#';
			}
			//保存数据报的长度
			PacketSize = PingData + 8;
			Byte [] icmp_pkt_buffer = new Byte[ PacketSize ]; 
			Int32 Index = 0;
			//调用Serialize方法
			//报文总共的字节数
			Index = Serialize(  
				packet, 
				icmp_pkt_buffer, 
				PacketSize, 
				PingData );
			//报文大小有错
			if( Index == -1 )
			{
				Console.WriteLine("Error in Making Packet");
				return ;
			}
			// 转化为Uint16类型的数组
			//取得数据报长度的一半
			Double double_length = Convert.ToDouble(Index);
			Double dtemp = Math.Ceiling( double_length / 2);
			int cksum_buffer_length = Convert.ToInt32(dtemp);
			//生成一个字节数组
			UInt16 [] cksum_buffer = new UInt16[cksum_buffer_length];
			//初始化 Uint16类型 array 
			int icmp_header_buffer_index = 0;
			for( int i = 0; i < cksum_buffer_length; i++ ) 
			{
				cksum_buffer[i] = 
					BitConverter.ToUInt16(icmp_pkt_buffer,icmp_header_buffer_index);
				icmp_header_buffer_index += 2;
			}
			//调用checksum,返回检查和
			UInt16 u_cksum = checksum(cksum_buffer, cksum_buffer_length);
			//检查和存在报文中
			packet.CheckSum  = u_cksum; 
			// Now that we have the checksum, serialize the packet again
			Byte [] sendbuf = new Byte[ PacketSize ]; 
			//再次检查报文大小
			Index = Serialize(  
				packet, 
				sendbuf, 
				PacketSize, 
				PingData );
			//如果有错,则报告错误
			if( Index == -1 )
			{
				Console.WriteLine("Error in Making Packet");
				return ;
			}
	                

			dwStart = System.Environment.TickCount; // 开始时间
			//用socket发送数据报
			if ((nBytes = socket.SendTo(sendbuf, PacketSize, 0, epServer)) == SOCKET_ERROR) 
			{		
				Console.WriteLine("Socket Error cannot Send Packet");
			}
			//初始化缓冲区.接受缓冲区			Initialize the buffers. The receive buffer is the size of the
			// ICMP 头 +IP 头 (20 字节)
			Byte [] ReceiveBuffer = new Byte[256]; 
			nBytes = 0;
			//接受字节流
			bool recd =false ;
			int timeout=0 ;
			//循环检查目标主机相应时间
			while(!recd)
			{
				nBytes = socket.ReceiveFrom(ReceiveBuffer, 256, 0, ref EndPointFrom);
				if (nBytes == SOCKET_ERROR) 
				{
					Console.WriteLine("Host not Responding") ;
					recd=true ;
					break;
				}
				else if(nBytes>0)
				{
					dwStop = System.Environment.TickCount - dwStart; 
					// 停止计时
					Console.WriteLine("Reply from "+epServer.ToString()+": bytes=" + nBytes + "  time="+dwStop + "ms");
					recd=true;
					break;
				}
				timeout=System.Environment.TickCount - dwStart;
				if(timeout>1000)
				{
					Console.WriteLine("Time Out") ;
					recd=true;
				}					
			}
			//关闭socket
			socket.Close();     
		}
		/// <summary>
		///  取得报文内容,转化为字节数组,然后计算报文的长度
		/// </summary>
		public static Int32 Serialize(  IcmpPacket packet, Byte [] Buffer, Int32 PacketSize, Int32 PingData )
		{
			Int32 cbReturn = 0;
			// 数据报结构转化为数组
			int Index=0;
			Byte [] b_type = new Byte[1];
			b_type[0] = (packet.Type);
			Byte [] b_code = new Byte[1];
			b_code[0] = (packet.SubCode);
			Byte [] b_cksum = BitConverter.GetBytes(packet.CheckSum);
			Byte [] b_id = BitConverter.GetBytes(packet.Identifier);
			Byte [] b_seq = BitConverter.GetBytes(packet.SequenceNumber);
			Array.Copy( b_type, 0, Buffer, Index, b_type.Length );
			Index += b_type.Length;
			Array.Copy( b_code, 0, Buffer, Index, b_code.Length );
			Index += b_code.Length;
			Array.Copy( b_cksum, 0, Buffer, Index, b_cksum.Length );
			Index += b_cksum.Length;
			Array.Copy( b_id, 0, Buffer, Index, b_id.Length );
			Index += b_id.Length;
			Array.Copy( b_seq, 0, Buffer, Index, b_seq.Length );
			Index += b_seq.Length;
			// 复制数据	        
			Array.Copy( packet.Data, 0, Buffer, Index, PingData );
			Index += PingData;
			if( Index != PacketSize/* sizeof(IcmpPacket)  */) 
			{
				cbReturn = -1;
				return cbReturn;
			}
			cbReturn = Index;
			return cbReturn;
		}
		/// <summary>
		///		校验和算法
		/// </summary>
		public static UInt16 checksum( UInt16[] buffer, int size )
		{
			Int32 cksum = 0;
			int counter;
			counter = 0;
			/*把ICMP报头二进制数据以2字节为单位累加起来*/
			while ( size > 0 ) 
			{
				UInt16 val = buffer[counter];
				cksum += Convert.ToInt32( buffer[counter] );
				counter += 1;
				size -= 1;
			}
			/*	若ICMP报头为奇数个字节,会剩下最后一字节。把最后一个字节视为一个
			 *	2字节数据的高字节,这个2字节数据的低字节为0,继续累加*/
			cksum = (cksum >> 16) + (cksum & 0xffff);
			cksum += (cksum >> 16);
			return (UInt16)(~cksum);
		}
	} // class ping
	/// <summary>
	///		IcmpPacket类,存储报文内容
	/// </summary>
	public class IcmpPacket 
	{ 
		public Byte  Type;    // 消息类型
		public Byte  SubCode;    // 子码类型
		public UInt16 CheckSum;   // 校检和
		public UInt16 Identifier;      // 标志符
		public UInt16 SequenceNumber;     // 顺序号  
		public Byte [] Data;   // 数据

	} // ICMP包
}