Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using PcapDotNet.Core;
- using PcapDotNet.Packets;
- using PcapDotNet.Packets.Ethernet;
- using PcapDotNet.Packets.IpV4;
- using PcapDotNet.Packets.Transport;
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace SendingASinglePacketWithSendPacket
- {
- internal class Program
- {
- private static void Main(string[] args)
- {
- // Retrieve the device list from the local machine
- IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;
- if (allDevices.Count == 0)
- {
- Console.WriteLine("No interfaces found! Make sure WinPcap is installed.");
- return;
- }
- // Print the list
- for (int i = 0; i != allDevices.Count; ++i)
- {
- LivePacketDevice device = allDevices[i];
- Console.Write(i + 1 + ". " + device.Name);
- if (device.Description != null)
- Console.WriteLine(" (" + device.Description + ")");
- else
- Console.WriteLine(" (No description available)");
- }
- int deviceIndex = 0;
- do
- {
- Console.WriteLine("Enter the interface number (1-" + allDevices.Count + "):");
- string deviceIndexString = Console.ReadLine();
- if (!int.TryParse(deviceIndexString, out deviceIndex) ||
- deviceIndex < 1 || deviceIndex > allDevices.Count)
- {
- deviceIndex = 0;
- }
- } while (deviceIndex == 0);
- // Take the selected adapter
- PacketDevice selectedDevice = allDevices[deviceIndex - 1];
- // Open the output device
- using (PacketCommunicator communicator = selectedDevice.Open(100, // name of the device
- PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
- 1000)) // read timeout
- {
- communicator.SendPacket(BuildUdpPacket());
- }
- }
- private static Packet BuildUdpPacket()
- {
- EthernetLayer ethernetLayer =
- new EthernetLayer
- {
- Source = new MacAddress("01:01:01:01:01:01"),
- Destination = new MacAddress("02:02:02:02:02:02"),
- EtherType = EthernetType.None, // Will be filled automatically.
- };
- IpV4Layer ipV4Layer =
- new IpV4Layer
- {
- Source = new IpV4Address("1.2.3.4"),
- CurrentDestination = new IpV4Address("11.22.33.44"),
- Fragmentation = IpV4Fragmentation.None,
- HeaderChecksum = null, // Will be filled automatically.
- Identification = 123,
- Options = IpV4Options.None,
- Protocol = null, // Will be filled automatically.
- Ttl = 100,
- TypeOfService = 0,
- };
- UdpLayer udpLayer =
- new UdpLayer
- {
- SourcePort = 4050,
- DestinationPort = 25,
- Checksum = null, // Will be filled automatically.
- CalculateChecksumValue = true,
- };
- PayloadLayer payloadLayer =
- new PayloadLayer
- {
- Data = new Datagram(Encoding.ASCII.GetBytes("mi she kore et ze homo")),
- };
- PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, payloadLayer);
- return builder.Build(DateTime.Now);
- }
- }
- }
Add Comment
Please, Sign In to add comment