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);
}
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();
listenerThreads[i].Start();
i++;
}
}
public void beginThread(object obj)
{
int devId = Convert.ToInt16(Thread.CurrentThread.Name.ToString());
PcapDevice currentDevice = devices[devId];
Console.WriteLine("[" + devId + "]" + "Current Thread: "+devId);
Console.WriteLine("[" + devId + "]" + "Current Device: " + currentDevice.PcapDescription.ToString());
// set packet arrival handler
currentDevice.PcapOnPacketArrival += new SharpPcap.PacketArrivalEvent(device_PcapOnPacketArrival);
//Associate the filter with this capture
//currentDevice.PcapSetFilter("ip and tcp"); // LINE 76
// open and start capture
try
{
currentDevice.PcapOpen(true, 1000); // LINE 81
Console.WriteLine("[" + devId + "]" + "Opened device");
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());
}
}
catch (Exception e)
{
Console.WriteLine("[" + devId + "]" + "Unable to open " + currentDevice.PcapDescription.ToString());
Console.WriteLine("[" + devId + "]" + "Reason: " + e.ToString());
frmMainApplication.formMainWindow.addToTextBox("Unable to open the following device for capture: " + currentDevice.PcapDescription.ToString());
}
//currentDevice.PcapStartCapture();
}
///
/// 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());
}
/*
Console.WriteLine(
"[#{0}] {1}",
Thread.CurrentThread.Name.ToString(),
packet.Data.ToString()
);
* */
}
///
/// Set devices
///
/// the list of devices
public static void setDevices(PcapDeviceList devs)
{
devices = devs;
}
}
}