protected IEnumerable <Frame> GetFramesFromPcapAndDefragment(Uri pcapUri) { var ipv4DefragmentationEngine = new Ipv4DefragmentationEngine(this._services.GetService <ILoggerFactory>().CreateLogger <Ipv4DefragmentationEngine>()); var frames = new List <Frame>(); var pcapLoader = this._services.GetService <IPcapLoader>(); using (pcapLoader) { RawCapture rawCapture; pcapLoader.Open(pcapUri); while ((rawCapture = pcapLoader.GetNextPacket()) != null) { Frame frame; var parsedPacket = Packet.ParsePacket(pcapLoader.LinkType, rawCapture.Data); if (!(parsedPacket.PayloadPacket is IPPacket ipPacket)) { continue; ; } if (ipPacket is IPv4Packet ipv4Packet && Ipv4Helpers.Ipv4PacketIsFragmented(ipv4Packet)) { var(isDefragmentationSuccessful, firstTimeStamp, defragmentedIpv4Packet) = ipv4DefragmentationEngine.TryDefragmentFragments( FrameFactory.CreateFromIpPacket(ipv4Packet, rawCapture.Timeval.Date.Ticks)); if (!isDefragmentationSuccessful) { continue; } frame = FrameFactory.CreateFromIpPacket(defragmentedIpv4Packet, firstTimeStamp); }
protected IEnumerable <Frame> GetFramesFromPcapAndDefragment(String pcapFileName) { var ipv4DefragmentationEngine = new Ipv4DefragmentationEngine(this._services.GetService <ILoggerFactory>().CreateLogger <Ipv4DefragmentationEngine>()); var frames = new List <Frame>(); foreach (var(packet, timestampTicks) in this.GetPacketsFromPcap(pcapFileName)) { Frame frame; if (!(packet.PayloadPacket is IPPacket ipPacket)) { continue; } if (ipPacket is IPv4Packet ipv4Packet && Ipv4Helpers.Ipv4PacketIsFragmented(ipv4Packet)) { var(isDefragmentationSuccessful, defragmentedIpv4Packet, fragments) = ipv4DefragmentationEngine.TryDefragmentFragments(FrameFactory.CreateFromIpPacket(ipv4Packet, timestampTicks)); if (!isDefragmentationSuccessful) { continue; } frame = FrameFactory.CreateFromDefragmentedIpPacket(defragmentedIpv4Packet, fragments); }
protected IMaybeMultipleValues <RawPacket> ParsePacket(RawCapture rawCapture) { if (rawCapture == null) { return(new ProcessRawPacketRequestError("Missing packet payload")); } // return new RawPacket(rawCapture) {EntityId = 0}; Packet parsedPacket; try { parsedPacket = Packet.ParsePacket(rawCapture.LinkLayerType, rawCapture.Data); } catch (ArgumentOutOfRangeException ex) { return(new ProcessRawPacketRequestError($"Parsing error {ex}")); } // Ignore non-IP traffic (STP, ... ) if (!(parsedPacket?.PayloadPacket is IPPacket ipPacket)) { return(new ProcessRawPacketRequestError("Non-IP packet")); } // Attempt to defragment if (ipPacket is IPv4Packet ipv4Packet && Ipv4Helpers.Ipv4PacketIsFragmented(ipv4Packet)) { var(isDefragmentationSuccessful, firstTimestamp, defragmentedIpv4Packet) = this.IPIpv4DefragmentationEngine.TryDefragmentFragments( FrameFactory.CreateFromIpPacket(ipv4Packet, rawCapture.Timeval.Date.Ticks)); if (!isDefragmentationSuccessful) { return(ProcessRawPacketFragmentsRequest.EmptyInstance); } var defragmentedL3L4Key = new L3L4ConversationKey(defragmentedIpv4Packet); return(new RawPacket(firstTimestamp, LinkLayers.Raw, defragmentedIpv4Packet.Bytes, defragmentedL3L4Key, this.MaxNumberOfShards)); } // Ignore unsupported transport protocols var transportPacket = ipPacket.PayloadPacket; if (!(transportPacket is TcpPacket || transportPacket is UdpPacket)) { return(new ProcessRawPacketRequestError("Unsupported transport protocol")); } var l3L4Key = new L3L4ConversationKey(ipPacket); return(new RawPacket(rawCapture, l3L4Key, this.MaxNumberOfShards)); }
private Boolean TryDefragmentFrame(ref Frame frame) { var(isDefragmentationSuccessful, firstTimeStamp, defragmentedIpv4Packet) = this._ipv4DefragmentationEngine.TryDefragmentFragments(frame); if (!isDefragmentationSuccessful) { this._logger.Debug("Packet fragment stored to defragmentation buffer"); return(false); } frame = FrameFactory.CreateFromIpPacket(defragmentedIpv4Packet, firstTimeStamp); return(true); }
private IEnumerable <Frame> GetFramesFromPcap(Uri pcapUri) { var ipv4DefragmentationEngine = new Ipv4DefragmentationEngine(this._loggerFactory.CreateLogger <Ipv4DefragmentationEngine>()); foreach (var(packet, timestampTicks) in this.GetPacketsFromPcap(pcapUri)) { Frame frame; if (!(packet.PayloadPacket is IPPacket ipPacket)) { continue; } if (ipPacket is IPv4Packet ipv4Packet && Ipv4Helpers.Ipv4PacketIsFragmented(ipv4Packet)) { var(isDefragmentationSuccessful, defragmentedIpv4Packet, fragments) = ipv4DefragmentationEngine.TryDefragmentFragments(FrameFactory.CreateFromIpPacket(ipv4Packet, timestampTicks)); if (!isDefragmentationSuccessful) { continue; } frame = FrameFactory.CreateFromDefragmentedIpPacket(defragmentedIpv4Packet, fragments); }