private void SendCaptureFile()
{
if (!ValidateUserInput())
{
return;
}
// Get the network device through which the packets will be sent.
PcapDevice device = chooseDeviceUserControl.Device;
// Get an offline file pcap device
PcapDevice offlineDevice = new PcapOfflineDevice(textBoxCaptureFile.Text);
PcapSendQueue sendQueue = null;
bool isDeviceOpenedInThisForm = false;
try
{
Cursor = Cursors.WaitCursor;
if (!device.Opened)
{
device.Open();
isDeviceOpenedInThisForm = true;
}
// Open the device for capturing
offlineDevice.Open();
// Allocate a new send queue
sendQueue = new PcapSendQueue
((int)((PcapOfflineDevice)offlineDevice).FileSize);
Packet packet;
// Go through all packets in the file and add to the queue
while ((packet = offlineDevice.GetNextPacket()) != null)
{
if (!sendQueue.Add(packet))
{
MessageBox.Show(
"Packet buffer too small, not all the packets will be sent.",
"Warning",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
break;
}
}
if (device.PcapDataLink != offlineDevice.PcapDataLink)
{
DialogResult answer = MessageBox.Show(
"The datalink of the capture differs from the one of the" +
"\nselected interface. Do you want to continue?",
"Question",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (answer == DialogResult.No)
{
return;
}
}
int bytesSent = device.SendQueue(sendQueue, false);
if (bytesSent < sendQueue.CurrentLength)
{
MessageBox.Show(
String.Format("An error occurred sending the packets: {0}.\nOnly {1} bytes were sent.", device.LastError, bytesSent),
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
// Close the pcap device
offlineDevice.Close();
// Free the queue
if (sendQueue != null)
{
sendQueue.Dispose();
}
if (isDeviceOpenedInThisForm)
{
device.Close();
}
Cursor = Cursors.Default;
}
}