基本信息
源码名称:C#实现tracert(Icmp报文)
源码大小:0.05M
文件格式:.rar
开发语言:C#
更新时间:2019-04-17
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 3 元×
微信扫码支付:3 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Net; using System.Net.Sockets; namespace Tracert { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } public class IcmpPacket { private Byte _type; private Byte _subCode; private UInt16 _checkSum; private UInt16 _identifier; private UInt16 _sequenceNumber; private Byte[] _data; public IcmpPacket(Byte type, Byte subCode, UInt16 checkSum, UInt16 identifier, UInt16 sequenceNumber, int dataSize) { _type = type; _subCode = subCode; _checkSum = checkSum; _identifier = identifier; _sequenceNumber = sequenceNumber; _data = new Byte[dataSize]; for (int i = 0; i < dataSize; i ) { _data[i] = (byte)'#'; } } public UInt16 CheckSum { get { return _checkSum; } set { _checkSum = value; } } //初始化ICMP报文 public int CountByte(Byte[] buffer) { Byte[] b_type = new Byte[1] { _type }; Byte[] b_code = new Byte[1] { _subCode }; Byte[] b_cksum = BitConverter.GetBytes(_checkSum); Byte[] b_id = BitConverter.GetBytes(_identifier); Byte[] b_seq = BitConverter.GetBytes(_sequenceNumber); int i = 0; Array.Copy(b_type, 0, buffer, i, b_type.Length); i = b_type.Length; Array.Copy(b_code, 0, buffer, i, b_code.Length); i = b_code.Length; Array.Copy(b_cksum, 0, buffer, i, b_cksum.Length); i = b_cksum.Length; Array.Copy(b_id, 0, buffer, i, b_id.Length); i = b_id.Length; Array.Copy(b_seq, 0, buffer, i, b_seq.Length); i = b_seq.Length; Array.Copy(_data, 0, buffer, i, _data.Length); i = _data.Length; return i; } //将ICMP报文转化为Byte数据包 public static UInt16 SumOfCheck(UInt16[] buffer) { int cksum = 0; for (int i = 0; i < buffer.Length; i ) { cksum = (int)buffer[i]; } cksum = (cksum >> 16) (cksum & 0xffff); cksum = (cksum >> 16); return (UInt16)(~cksum); } } private void button1_Click(object sender, RoutedEventArgs e) { listBox1.Items.Clear(); try { if (textBox1.Text.Length == 0) { listBox1.Items.Add("请键入IP地址"); textBox1.Focus(); return; } Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp); //创建接收和发送ICMP报文的Socket IPEndPoint ipDEST = new IPEndPoint(Dns.Resolve(textBox1.Text).AddressList[0], 30); //设定服务器端30端口号对应的网络节点 IPEndPoint ipSRC = new IPEndPoint(Dns.GetHostByName(Dns.GetHostName()).AddressList[0], 30); EndPoint epSRC = (EndPoint)ipSRC; int DataSize = 4;//ICMP报文中的数据长度 int PacketSize = DataSize 8;//报文总长度 const int ICMP_ECHO = 8; IcmpPacket packet = new IcmpPacket(ICMP_ECHO, 0, 0, 45, 0, DataSize); Byte[] Buffer = new Byte[PacketSize]; int index = packet.CountByte(Buffer); if (index != PacketSize) { listBox1.Items.Add("报文有问题"); return; } int cksum_buffer_length = (int)Math.Ceiling(((Double)index) / 2); UInt16[] cksum_buffer = new UInt16[cksum_buffer_length]; int icmp_header_buffer_index = 0; for (int i = 0; i < cksum_buffer_length; i ) { cksum_buffer[i] = BitConverter.ToUInt16(Buffer, icmp_header_buffer_index); icmp_header_buffer_index = 2; } //将校验和保存在报文中 packet.CheckSum = IcmpPacket.SumOfCheck(cksum_buffer); //保存校验和后,再次将报文转化为数据包 Byte[] ByteSend = new Byte[PacketSize]; index = packet.CountByte(ByteSend); if (index != PacketSize) { listBox1.Items.Add("报文有问题"); return; } //发送不同TTL值的数据包 for (int ittl = 1; ittl <= 255; ittl ) { Byte[] ByteRecv = new Byte[256]; s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, ittl); s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 10000); s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 10000); DateTime dt = DateTime.Now; int iRet = s.SendTo(ByteSend, ByteSend.Length, SocketFlags.None, ipDEST); //发送ICMP报文 if (iRet == -1) { listBox1.Items.Add("发送数据出错"); } iRet = s.ReceiveFrom(ByteRecv, ByteRecv.Length, SocketFlags.None, ref epSRC); TimeSpan ts = DateTime.Now - dt; //检测回应是否正确 if (iRet == -1) { listBox1.Items.Add("接收数据出错"); } listBox1.Items.Add("TTL=" ittl.ToString() "IP=" ((IPEndPoint)epSRC).Address.ToString() "Time=" ts.Milliseconds.ToString()); //判断是否到达目标地址 if (Dns.GetHostEntry(textBox1.Text).AddressList[0].ToString() == ((IPEndPoint)epSRC).Address.ToString()) { break; } //超时错误 if (ByteRecv[20] != 11) { listBox1.Items.Add("超时,请重试"); break; } } } catch (SocketException b) { MessageBox.Show(b.Message); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }