ذخیره پاکت ها درون فایل رونوشت
پیش از هرکاری بیایید ببینیم چگونه می توان پاکت ها را در فرمت Libcap نوشت.مثال بعدی پاکت ها را از واسط تعیین شده می خواند و آنها را درون یک فایل می نویسد، نام فایل را کاربر تعیین می کند.
using System;
using System.Collections.Generic;
using PcapDotNet.Core;
namespace SavingPacketsToADumpFile
{
class Program
{
static void Main(string[] args)
{
// Check command line
if (args.Length != 1)
{
Console.WriteLine("usage: " + Environment.GetCommandLineArgs()[0] + " <filename>");
return;
}
// Retrieve the device list on the local machine
IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;
if (allDevices.Count == 0)
{
Console.WriteLine("No interfaces found! Make sure WinPcap is installed.");
return;
}
// Print the list
for (int i = 0; i != allDevices.Count; ++i)
{
LivePacketDevice device = allDevices[i];
Console.Write((i + 1) + ". " + device.Name);
if (device.Description != null)
Console.WriteLine(" (" + device.Description + ")");
else
Console.WriteLine(" (No description available)");
}
int deviceIndex = 0;
do
{
Console.WriteLine("Enter the interface number (1-" + allDevices.Count + "):");
string deviceIndexString = Console.ReadLine();
if (!int.TryParse(deviceIndexString, out deviceIndex) ||
deviceIndex < 1 || deviceIndex > allDevices.Count)
{
deviceIndex = 0;
}
} while (deviceIndex == 0);
// Take the selected adapter
PacketDevice selectedDevice = allDevices[deviceIndex - 1];
// Open the device
using (PacketCommunicator communicator =
selectedDevice.Open(65536, // portion of the packet to capture
// 65536 guarantees that the whole packet will be captured on all the link layers
PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
1000)) // read timeout
{
// Open the dump file
using (PacketDumpFile dumpFile = communicator.OpenDump(args[0]))
{
Console.WriteLine("Listening on " + selectedDevice.Description + "... Press Ctrl+C to stop...");
// start the capture
communicator.ReceivePackets(0, dumpFile.Dump);
}
}
}
}
}
همانطور که می توانید ببینید ، ساختار برنامه بسیار شبیه به آنچه که در درسهای گذشته دیده بودیم است.
تفاوت ها:
یک فراخوانی به تابع OpenDump() زمانی که واسط باز شده است اتفاق افتاده است. این فراخوانی فایل رونوشت را باز کرده و آن را در تعامل با واسط قرار می دهد.
با ست کردن تابع CallBack به تابع Dump() فایل رونوشت ، پاکت ها درون فایل نوشته می شوند. دیدگاه دیگر می تواند قرار دادن یک تابع اختصاصی به جای CallBack یا یک نمایندگی بی نام[1] به جای آن باشد.
خواندن پاکت ها از فایل رونوشت
حال که فایل رونوشت را آماده داریم ، می توانیم اقدام به خواندن محتوای آن بکنیم.مثال بعدی فایل رونشت Libcap را بازکرده و تمامی پاکت های موجود در آن را چاپ می کند.فایل با تابع Open() باز می شود، سپس از متد متداول RecivePackets() برای خواندن پشت سرهم پاکت ها استفاده می شود.همانطور که می بینید خواندن بسته ها ازفایل رونوشت بسیار شبیه به خواندن آنها از واسط فزیکی است.
using System;
using PcapDotNet.Core;
using PcapDotNet.Packets;
namespace ReadingPacketsFromADumpFile
{
class Program
{
static void Main(string[] args)
{
// Check command line
if (args.Length != 1)
{
Console.WriteLine("usage: " + Environment.GetCommandLineArgs()[0] + " <filename>");
return;
}
// Create the offline device
OfflinePacketDevice selectedDevice = new OfflinePacketDevice(args[0]);
// Open the capture file
using (PacketCommunicator communicator =
selectedDevice.Open(65536, // portion of the packet to capture
// 65536 guarantees that the whole packet will be captured on all the link layers
PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
1000)) // read timeout
{
// Read and dispatch packets until EOF is reached
communicator.ReceivePackets(0, DispatcherHandler);
}
}
private static void DispatcherHandler(Packet packet)
{
// print packet timestamp and packet length
Console.WriteLine(packet.Timestamp.ToString("yyyy-MM-dd hh:mm:ss.fff") + " length:" + packet.Length);
// Print the packet
const int LineLength = 64;
for (int i = 0; i != packet.Length; ++i)
{
Console.Write((packet[i]).ToString("X2"));
if ((i + 1) % LineLength == 0)
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine();
}
}
}