private void OnPacketArrval(object sender, CaptureEventArgs e) { Packet packet = null; try { packet = Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data); } catch { return; } IPAddress source = IPAddress.Parse("127.0.0.1"); IPAddress destination = IPAddress.Parse("127.0.0.1"); string type = GetTopProtocolType(packet, ref source, ref destination); Thread thread = new Thread(() => { string info = string.Format("time:{0}:{1}:{2}/{5} {3} -> {4} type:{6} size:{7}B\n", DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, source, destination, DateTime.Now.Millisecond, type, e.Packet.Data.Length); OnAppendPacketInfo(info); lock (packets) { ListViewItem item = new ListViewItem(packets.Count.ToString()); item.SubItems.AddRange(new string[] { source.ToString(), destination.ToString(), e.Packet.Data.Length.ToString(), type }); OnAddListviewItem(item); packets.Add(packet); } }); thread.IsBackground = true; thread.Start(); if (e.Packet.LinkLayerType == LinkLayers.Ethernet) { try { EthernetPacket eth = (EthernetPacket)EthernetPacket.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data); if (eth.PayloadPacket is ARPPacket) { ARPPacket arp = (ARPPacket)eth.PayloadPacket; if (arp.Operation == ARPOperation.Response || arp.Operation == ARPOperation.Request) { if (!macAddress.ContainsKey(arp.SenderProtocolAddress)) { macAddress.Add(arp.SenderProtocolAddress, arp.SenderHardwareAddress); } } } else if (eth.PayloadPacket is IPv4Packet) { IPv4Packet ip = (IPv4Packet)eth.PayloadPacket; if (hadHandledIpList.Contains(ip.Id)) { return; } hadHandledIpList.Add(ip.Id); bool hadSent = false; #region 直连路由包 foreach (Device dev in deviceList) { if (dev.Interface == e.Device) { continue; } if (GetNetIpAddress(ip.DestinationAddress, dev.MaskAddress).ToString() == dev.NetAddress.ToString()) { eth.SourceHwAddress = dev.MacAddress; if (!macAddress.ContainsKey(ip.DestinationAddress)) { MacAddress.GetMacAddress(ip.DestinationAddress); return; } else { eth.DestinationHwAddress = macAddress[ip.DestinationAddress]; } dev.Interface.SendPacket(eth); hadSent = true; } } if (hadSent) { return; } #endregion #region 非直连路由包 RouteTable route = staticRouting[ip.DestinationAddress]; if (route != null) { eth.SourceHwAddress = route.OutInterface.MacAddress; if (!macAddress.ContainsKey(route.NextHop)) { MacAddress.GetMacAddress(route.NextHop); return; } else { eth.DestinationHwAddress = macAddress[route.NextHop]; } route.OutInterface.SendPacket(eth); return; } #endregion } else if (eth.PayloadPacket is PPPoEPacket) { //暂不处理 } } catch { //Protocol of 49185 is not implemented //不支持此协议,对此类协议包进行忽略 return; } } }
private void button1_Click(object sender, EventArgs e) { //ICMP if (comboBox1.SelectedIndex == 4) { byte[] buffer = new byte[32]; ICMPv4Packet icmp = new ICMPv4Packet(new PacketDotNet.Utils.ByteArraySegment(buffer)); icmp.TypeCode = ICMPv4TypeCodes.EchoRequest; icmp.ID = (ushort)0; icmp.Sequence = (ushort)0; icmp.PayloadData = new byte[32]; icmp.Checksum = GetChecksum(icmp.Header); IPAddress sourceIP = IPAddress.Parse(ipAddressBox1.Text); IPAddress destinationIP = IPAddress.Parse(ipAddressBox2.Text); IPv4Packet ipv4 = new IPv4Packet(sourceIP, destinationIP); ipv4.PayloadPacket = icmp; ipv4.TimeToLive = 128; ipv4.Checksum = GetChecksum(ipv4.Header); if (!softRoute.macAddress.ContainsKey(sourceIP)) { MacAddress.GetMacAddress(sourceIP); MessageBox.Show("源MAC不存在,获取中,请稍候再试!", "提示"); return; } PhysicalAddress sourceMac = softRoute.macAddress[sourceIP]; if (!softRoute.macAddress.ContainsKey(destinationIP)) { MacAddress.GetMacAddress(destinationIP); MessageBox.Show("目的MAC不存在,获取中,请稍候再试!", "提示"); return; } PhysicalAddress destinationMac = softRoute.macAddress[destinationIP]; EthernetPacket eth = new EthernetPacket(sourceMac, destinationMac, EthernetPacketType.IpV4); eth.PayloadPacket = ipv4; softRoute.deviceList[comboBox2.SelectedIndex].Interface.SendPacket(eth); } //ARP else if (comboBox1.SelectedIndex == 0) { ARPPacket arp = new ARPPacket(ARPOperation.Request, PhysicalAddress.Parse(textBox2.Text), IPAddress.Parse(ipAddressBox2.Text), PhysicalAddress.Parse(textBox1.Text), IPAddress.Parse(ipAddressBox1.Text)); EthernetPacket eth = new EthernetPacket(PhysicalAddress.Parse(textBox1.Text), PhysicalAddress.Parse(textBox2.Text), EthernetPacketType.Arp); eth.PayloadPacket = arp; softRoute.deviceList[comboBox2.SelectedIndex].Interface.SendPacket(eth); } //IP else if (comboBox1.SelectedIndex == 1) { //IPv4Packet ipv4 = new IPv4Packet(IPAddress.Parse(ipAddressBox1.Text), IPAddress.Parse(ipAddressBox2.Text)); } //TCP else if (comboBox1.SelectedIndex == 2) { //TcpPacket tcp = new TcpPacket(1, 2); } //UDP else if (comboBox1.SelectedIndex == 3) { } }