Untitled
unknown
plain_text
4 years ago
5.6 kB
8
Indexable
using System; using System.Collections.Generic; using PcapDotNet.Core; using PcapDotNet.Packets; using System.Threading; namespace CapturingThePacketsWithoutTheCallback { class Program { /* private static void PacketHandler1(Packet packet) { Console.WriteLine("1: length:" + packet.Length + " " + packet.Ethernet.Destination); } static void INT1(PacketCommunicator communicator1, PacketCommunicator communicator2) { using (communicator1) { // start the capture communicator1.ReceivePackets(0, PacketHandler1); } } private static void PacketHandler2(Packet packet) { Console.WriteLine("2: length:" + packet.Length + " " + packet.Ethernet.Destination); } static void INT2(PacketCommunicator communicator1, PacketCommunicator communicator2) { using (communicator2) { // start the capture communicator2.ReceivePackets(0, PacketHandler2); } } */ static void INT1(PacketCommunicator communicator1, PacketCommunicator communicator2) { using (communicator1) { // Retrieve the packets Packet packet; do { PacketCommunicatorReceiveResult result = communicator1.ReceivePacket(out packet); switch (result) { case PacketCommunicatorReceiveResult.Timeout: // Timeout elapsed continue; case PacketCommunicatorReceiveResult.Ok: Console.WriteLine("2: length:" + packet.Length + " " + packet.Ethernet.Destination); communicator2.SendPacket(packet); break; default: throw new InvalidOperationException("The result " + result + " shoudl never be reached here"); } } while (true); } } static void INT2(PacketCommunicator communicator1, PacketCommunicator communicator2) { using (communicator2) { // Retrieve the packets Packet packet; do { PacketCommunicatorReceiveResult result = communicator2.ReceivePacket(out packet); switch (result) { case PacketCommunicatorReceiveResult.Timeout: // Timeout elapsed continue; case PacketCommunicatorReceiveResult.Ok: Console.WriteLine("2: length:" + packet.Length + " " + packet.Ethernet.Destination); communicator1.SendPacket(packet); break; default: throw new InvalidOperationException("The result " + result + " shoudl never be reached here"); } } while (true); } } 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); PacketDevice selectedDevice = allDevices[deviceIndex - 1]; PacketDevice selectedDevice2 = allDevices[deviceIndex +3]; PacketCommunicator communicator1 = selectedDevice.Open(1000, PacketDeviceOpenAttributes.NoCaptureLocal | PacketDeviceOpenAttributes.Promiscuous, 1); PacketCommunicator communicator2 = selectedDevice2.Open(1000, PacketDeviceOpenAttributes.NoCaptureLocal | PacketDeviceOpenAttributes.Promiscuous, 1); Console.WriteLine("Listening on " + selectedDevice2.Description + "..."); Thread INT1_thread = new Thread(() => INT1(communicator1, communicator2)); Thread INT2_thread = new Thread(() => INT2(communicator1, communicator2)); INT1_thread.Start(); INT2_thread.Start(); INT1_thread.Join(); INT2_thread.Join(); } } }
Editor is loading...