
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包
}
