public unsafe void SendPacket(Packets.Packet pkt) { bool worked = false; if (pkt.Outbound) { ETH_REQUEST Request = new ETH_REQUEST(); INTERMEDIATE_BUFFER *ib = pkt.IB; Request.EthPacket.Buffer = (IntPtr)ib; Request.hAdapterHandle = adapterHandle; if (!Ndisapi.SendPacketToAdapter(hNdisapi, ref Request)) { worked = false; } else { worked = true; } } else { ETH_REQUEST Request = new ETH_REQUEST(); INTERMEDIATE_BUFFER *ib = pkt.IB; Request.EthPacket.Buffer = (IntPtr)ib; Request.hAdapterHandle = adapterHandle; worked = Ndisapi.SendPacketToMstcp(hNdisapi, ref Request); } if (pkt.CodeGenerated) { pkt.ClearGeneratedPacket(); } }
public unsafe void ProcessLoop() { // Allocate and initialize packet structures ETH_REQUEST Request = new ETH_REQUEST(); INTERMEDIATE_BUFFER PacketBuffer = new INTERMEDIATE_BUFFER(); IntPtr PacketBufferIntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(PacketBuffer)); try { win32api.ZeroMemory(PacketBufferIntPtr, Marshal.SizeOf(PacketBuffer)); Request.hAdapterHandle = adapterHandle; Request.EthPacket.Buffer = PacketBufferIntPtr; Modules = new ModuleList(this); Modules.LoadExternalModules(); Modules.UpdateModuleOrder(); string folder = Configuration.ConfigurationManagement.Instance.ConfigurationPath; folder = folder + System.IO.Path.DirectorySeparatorChar + "pcapLogs"; if (!System.IO.Directory.Exists(folder)) { System.IO.Directory.CreateDirectory(folder); } string f = folder + System.IO.Path.DirectorySeparatorChar + "blocked-" + inter.Name + "-" + DateTime.Now.ToBinary() + ".pcap"; pcaplog = new PcapFileWriter(f); INTERMEDIATE_BUFFER *PacketPointer; while (true) { hEvent.WaitOne(); while (Ndisapi.ReadPacket(hNdisapi, ref Request)) { PacketPointer = (INTERMEDIATE_BUFFER *)PacketBufferIntPtr; Packet pkt = new EthPacket(PacketPointer).MakeNextLayerPacket(); if (pkt.Outbound) { inter.DataOut.AddBits(pkt.Length()); } else { inter.DataIn.AddBits(pkt.Length()); } bool drop = false; bool log = false; if (this.Filtering) { for (int x = 0; x < Modules.Count; x++) { NDISModule fm = Modules.GetModule(x); int pmr = fm.PacketMain(ref pkt); if (pmr == null) { continue; } if ((pmr & (int)PacketMainReturnType.LogPacket) == (int)PacketMainReturnType.LogPacket) { log = true; } if ((pmr & (int)PacketMainReturnType.Drop) == (int)PacketMainReturnType.Drop) { drop = true; break; } } } if (!drop && !DropAll) { if (pkt.Outbound) { Ndisapi.SendPacketToAdapter(hNdisapi, ref Request); } else { Ndisapi.SendPacketToMstcp(hNdisapi, ref Request); } } if (log) { pcaplog.AddPacket(pkt.Data(), (int)pkt.Length()); } } //OM NOM NOM PASTA! while (processQueue.Count != 0) { Packet pkt = processQueue.Dequeue().MakeNextLayerPacket(); if (pkt.Outbound) { inter.DataOut.AddBits(pkt.Length()); } else { inter.DataIn.AddBits(pkt.Length()); } bool drop = false; bool log = false; if (this.Filtering) { for (int x = 0; x < Modules.Count; x++) { NDISModule fm = Modules.GetModule(x); int pmr = fm.PacketMain(ref pkt); if (pmr == 0) { continue; } if ((pmr & (int)PacketMainReturnType.LogPacket) == (int)PacketMainReturnType.LogPacket) { log = true; } if ((pmr & (int)PacketMainReturnType.Drop) == (int)PacketMainReturnType.Drop) { drop = true; break; } } } if (!drop && !DropAll) { if (pkt.Outbound) { Ndisapi.SendPacketToAdapter(hNdisapi, ref Request); } else { Ndisapi.SendPacketToMstcp(hNdisapi, ref Request); } } if (log) { pcaplog.AddPacket(pkt.Data(), (int)pkt.Length()); } } hEvent.Reset(); } } catch (Exception tae) { Marshal.FreeHGlobal(PacketBufferIntPtr); } }