public async Task <bool> RunScan(CancellationTokenSource token, PacketDevice pd) { return(await Task.Run(async() => { using (var communicator = pd.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000)) { using (var filter = communicator.CreateFilter("ip and udp")) { communicator.SetFilter(filter); } var packetTable = new PacketTable(); do { Packet packet; var result = communicator.ReceivePacket(out packet); switch (result) { case PacketCommunicatorReceiveResult.Ok: if (packet.IpV4 == null) { continue; } var ip = packet.Ethernet.IpV4; var udp = ip.Udp; var packetItem = new PacketLogItem { Destination = ip.Destination.ToString(), DestinationPort = udp.DestinationPort, Size = packet.Length, Source = ip.Source.ToString(), SourcePort = udp.SourcePort, TimeStamp = DateTime.Now }; OnNewPacketEntry(packetItem); await packetTable.WritePacketAsync(packetItem); break; } if (token.IsCancellationRequested) { return true; } } while (true); return true; } }, token.Token)); }
public async Task <bool> WritePacketAsync(PacketLogItem packetItem) { return(await WriteAsync(new Packets { Size = packetItem.Size, DestinationPort = packetItem.DestinationPort, DestinationIP = packetItem.Destination, SourceIP = packetItem.Source, SourtPort = packetItem.SourcePort, Timestamp = packetItem.TimeStamp })); }
private void Scanner_NewPacketEntry(object sender, PacketLogItem e) { Packets.Enqueue(e); Packets = new ConcurrentQueue <PacketLogItem>(Packets); }
public void OnNewPacketEntry(PacketLogItem e) { NewPacketEntry?.Invoke(null, e); }