How can I use the object "communicator" created in main(), in the callback function? The PacketCommunicator class doesn't have a constructor.
class Program
{
//public static PacketCommunicator communicator = new PacketCommunicator(); - can't do this; PacketCommunicator constructor doesn't exist
//public static PacketCommunicator communicator = null; - this gives "Object reference not set to an instance of an object"
public static void Main(string[] args)
{
PacketCommunicator communicator = selectedDevice.Open(...)
communicator.ReceivePackets(0, PacketHandler);
}
// Callback function invoked by ReceivePackets for every incoming packet
private static void PacketHandler(Packet packet)
{
communicator.SendPacket(packet);
}
}
The library I'm using is Pcap.Net. Basically, what I'm trying to do is to capture packets on one interface, perform NAT on them, and send them on another interface.
As @SLaks mentioned in the comments, you could use a lambda. Then communicator would not lose scope.
The way that PCAP works with receive packets is to just put it in a continual loop, breaking when you hit a time when you want to close.
There is a nice example of that here, though they are just reporting on the packets, not sending them on as you appear to be.
In this example you would instantiate communicator thusly