Question: For my assignment I have to adapt the C sharp code at the bottom in order to accomplish this: 2) Adapt the code from class
For my assignment I have to adapt the C sharp code at the bottom in order to accomplish this:
2) Adapt the code from class such that it breaks recieved packets into differing protocols (TCP, UDP, etc..) and displays percentages and number of packets.
I am struggling with this and am not sure where to start or how to adapt the code.
Please use the existing code to adapt.
Code:
namespace MyPacketCaputurer { public partial class frmCapture : Form { CaptureDeviceList devices; //List of devices for this computer public static ICaptureDevice device; //The device we will be using public static string stringPackets = " "; //Data that is captured static int numPackets = 0; frmSend fSend; //This will be our send form
public frmCapture() { InitializeComponent();
//Get list of devices devices = CaptureDeviceList.Instance;
//Check to see if there is at least one device if (devices.Count < 1) { MessageBox.Show("No Capture Devices Found!!!"); Application.Exit(); } //Add the devices to the combo box foreach(ICaptureDevice dev in devices) { cmbDevices.Items.Add(dev.Description); }
//Gets/Displays first device in the combo box device = devices[0]; cmbDevices.Text = device.Description; //Register our handler function to 'packet arrival' event device.OnPacketArrival += new SharpPcap.PacketArrivalEventHandler(device_OnPacketArrival); //Open the device for capturing int readTimeoutMilliseconds = 1000; device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds); }
private static void device_OnPacketArrival(object sender, CaptureEventArgs packet) {
//Increment the number of packets captured numPackets++;
//Put the packet number in the capture window stringPackets += "Packet Number: " + Convert.ToString(numPackets); stringPackets += Environment.NewLine;
//Array to store our data byte[] data = packet.Packet.Data; int byteCounter = 0;
stringPackets += "Destination MAC Address: "; //Parsing the packets foreach (byte b in data) { if (byteCounter <= 13) stringPackets += b.ToString("X2") + " "; byteCounter++;
switch (byteCounter) { case 6: stringPackets += Environment.NewLine; stringPackets += "Source MAC Address: "; break; case 12: stringPackets += Environment.NewLine; stringPackets += "EtherType: "; break; case 14: if(data[12] == 8) { if (data[13] == 0) stringPackets += "(IP)"; if (data[13] == 6) stringPackets += "(ARP)"; } break; } }
stringPackets += Environment.NewLine + Environment.NewLine; byteCounter = 0; stringPackets += "Raw Data" + Environment.NewLine; foreach(byte b in data) { stringPackets += b.ToString("X2") + " "; byteCounter++;
if(byteCounter == 16) { byteCounter = 0; stringPackets += Environment.NewLine; } }
stringPackets += Environment.NewLine; stringPackets += Environment.NewLine; }
private void btnStartStop_Click(object sender, EventArgs e) { try { if (btnStartStop.Text == "Start") { device.StartCapture(); timer1.Enabled = true; btnStartStop.Text = "Stop"; } else { device.StartCapture(); timer1.Enabled = false; btnStartStop.Text = "Start"; } }
catch(Exception exp) {
} } private void cmbDevices_SelectedIndexChanged(object sender, EventArgs e) { device = devices[cmbDevices.SelectedIndex]; cmbDevices.Text = device.Description; device.OnPacketArrival += new SharpPcap.PacketArrivalEventHandler(device_OnPacketArrival); //Open the device for capturing int readTimeoutMilliseconds = 1000; device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds); }
private void txtCaptureData_TextChanged(object sender, EventArgs e) {
} private void timer1_Tick(object sender, EventArgs e) { txtCaptureData.AppendText(stringPackets); stringPackets = " "; txtNumPackets.Text = Convert.ToString(numPackets); }
private void Form1_Load(object sender, EventArgs e) {
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e) { saveFileDialog1.Filter = "Text Files | *.txt | All Files| *.*"; saveFileDialog1.Title = "Save the Captured Packets"; saveFileDialog1.ShowDialog(); if (saveFileDialog1.FileName != "") { System.IO.File.WriteAllText(saveFileDialog1.FileName, txtCaptureData.Text); } }
private void openToolStripMenuItem_Click(object sender, EventArgs e) { openFileDialog1.Filter = "Text Files | *.txt | All Files| *.*"; openFileDialog1.Title = "Open Captured Packets"; openFileDialog1.ShowDialog(); if (openFileDialog1.FileName != "") { txtCaptureData.Text = System.IO.File.ReadAllText(openFileDialog1.FileName); } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
