using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Windows.Forms; using Tamir.IPLib; using Tamir.IPLib.Packets; namespace L2HelperClient { class packetCapture { /// /// The listener threads to listen to each device /// private static Thread[] listenerThreads; /// /// The devices installed and detected on the system. /// private static PcapDeviceList devices; /// /// Constructor. Will populate device list automatically. /// public packetCapture() { devices = SharpPcap.GetAllDevices(); } /// /// Constructor. Device list must be provided. /// /// the list of devices public packetCapture(PcapDeviceList devs) { setDevices(devs); } /// /// Creates a thread for each device. These threads will listen for incoming /// data on each device and shoot off the callback function on arrival of /// the packets. /// public void createListenerThreads() { // intialize...IMPORTANT! listenerThreads = new Thread[devices.Count]; int i = 0; foreach (PcapDevice devs in devices) { try { listenerThreads[i] = new Thread(beginThread); } catch (Exception e) { frmMainApplication.formMainWindow.addToTextBox("The following error(s) occured while attempting to create thread #" + i + ":"); frmMainApplication.formMainWindow.addToTextBox(e.ToString()); } // next line necassary? listenerThreads[i].SetApartmentState(ApartmentState.MTA); listenerThreads[i].Name = i.ToString(); // set on packet arrival callback Console.WriteLine("[" + i + "]" + "Current Thread: " + i); Console.WriteLine("[" + i + "]" + "Current Device: " + devs.PcapDescription.ToString()); devs.PcapOnPacketArrival += new SharpPcap.PacketArrivalEvent(device_PcapOnPacketArrival); // open the device try { devs.PcapOpen(true, 1000); Console.WriteLine("[" + i + "]" + "Opened device"); // set filter devs.PcapSetFilter("ip and tcp"); } catch (Exception e) { Console.WriteLine("[" + i + "]" + "Unable to open " + devs.PcapDescription.ToString()); Console.WriteLine("[" + i + "]" + "Reason: " + e.ToString()); frmMainApplication.formMainWindow.addToTextBox("Unable to open the following device for capture: " + devs.PcapDescription.ToString()); } // begin thread listenerThreads[i].Start(); i++; } } /// /// Begins the thread. Starts capture on the device if possible. /// /// public void beginThread(object obj) { int devId = Convert.ToInt16(Thread.CurrentThread.Name.ToString()); PcapDevice currentDevice = devices[devId]; try { frmMainApplication.formMainWindow.addToTextBox("Begun capture on device: " + currentDevice.PcapDescription.ToString()); currentDevice.PcapCapture(1000); } catch (Exception e) { Console.WriteLine("[" + devId + "]" + "Unable to capture on " + currentDevice.PcapDescription.ToString()); Console.WriteLine("[" + devId + "]" + "Reason: " + e.ToString()); frmMainApplication.formMainWindow.addToTextBox("Unable to start capture on the following device: " + currentDevice.PcapDescription.ToString()); } } /// /// Prints the time and length of each received packet /// private static void device_PcapOnPacketArrival(object sender, Packet packet) { DateTime time = packet.PcapHeader.Date; int len = packet.PcapHeader.PacketLength; /* frmMainApplication.formMainWindow.addToTextBox(String.Format( "[#{0}] {1} Len={2}", Thread.CurrentThread.Name.ToString(), time.Millisecond, len )); * */ if (packet is TCPPacket) { //DateTime time = packet.Timeval.Date; //int len = packet.PcapHeader.CaptureLength; TCPPacket tcp = (TCPPacket)packet; string srcIp = tcp.SourceAddress; string dstIp = tcp.DestinationAddress; int srcPort = tcp.SourcePort; int dstPort = tcp.DestinationPort; Console.WriteLine("[#{9}] {0}:{1}:{2},{3} Len={4} {5}:{6} -> {7}:{8}", time.Hour, time.Minute, time.Second, time.Millisecond, len, srcIp, srcPort, dstIp, dstPort, Thread.CurrentThread.Name.ToString()); } } /// /// Set devices /// /// the list of devices public static void setDevices(PcapDeviceList devs) { devices = devs; } } }