All pastes #233867 Raw Edit

Untitled

public text v1 · immutable
·published 2006-11-02 05:04 UTC
rendered paste body
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
    {
        /// <summary>
        /// The listener threads to listen to each device
        /// </summary>
        private static Thread[] listenerThreads;

        /// <summary>
        /// The devices installed and detected on the system.
        /// </summary>
        private static PcapDeviceList devices;

        /// <summary>
        /// Constructor. Will populate device list automatically.
        /// </summary>
        public packetCapture()
        {
            devices = SharpPcap.GetAllDevices();
        }

        /// <summary>
        /// Constructor. Device list must be provided.
        /// </summary>
        /// <param name="devs">the list of devices</param>
        public packetCapture(PcapDeviceList devs)
        {
            setDevices(devs);
        }

        /// <summary>
        /// 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.
        /// </summary>
        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++;
            }
        }

        /// <summary>
        /// Begins the thread. Starts capture on the device if possible.
        /// </summary>
        /// <param name="obj"></param>
        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());
            }            
        }

        /// <SUMMARY>
        /// Prints the time and length of each received packet
        /// </SUMMARY>
        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());
            }
        }

        /// <summary>
        /// Set devices
        /// </summary>
        /// <param name="devs">the list of devices</param>
        public static void setDevices(PcapDeviceList devs) 
        {
            devices = devs;
        }
    }
}