Exemple #1
0
        public (LinkLayerType linkLayer, byte[] data) GetPacket(int index)
        {
            if (_offsetsList == null || _offsetsList.Count <= index)
            {
                _offsetsList = _pcapngWeakHandle.GetPacketsOffsets();
            }

            EnhancedPacketBlock pkt = _pcapngWeakHandle.GetPacketAt(_offsetsList[index]);
            var ifaces = _pcapngWeakHandle.GetInterfaces();
            InterfaceDescriptionBlock iface = null;

            try
            {
                iface = ifaces.ElementAt(pkt.AssociatedInterfaceID.Value);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Issues when " + ex);
            }

            return((LinkLayerType)iface.LinkType, pkt.Data);
        }
Exemple #2
0
        private string DoExportBasedOfFile(PcapngWeakHandle basePcapngFile, IEnumerable <TempPacketSaveData> packets)
        {
            if (!packets.Any())
            {
                // No packets, just return the pack of the pcapng file
                return(basePcapngFile.Path);
            }

            // Some packets defined, need to create modified version of the file
            string tempFile = Path.ChangeExtension(Path.GetTempFileName(), "pcapng");

            File.Copy(basePcapngFile.Path, tempFile);

            PcapngWeakHandle tempHandle = new PcapngWeakHandle(tempFile);
            var offsets = tempHandle.GetPacketsOffsets();
            var ifaces  = tempHandle.GetInterfaces();

            foreach (TempPacketSaveData packet in packets)
            {
                long packetOffset = offsets[packet.PacketNumber];
                EnhancedPacketBlock replacedEpb = tempHandle.GetPacketAt(packetOffset);

                InterfaceDescriptionBlock interfaceOfReplacedPacket = ifaces[replacedEpb.InterfaceID];

                // TODO: Sooooo I'm having some issues here because I didn't carry the interface ID into the 'TempPacketSaveData' object
                // nor did I allow the user to set which interface he wants to associate the packet to.
                // I'm resorting to 2 heuristics:
                //  1. If the Link Layer of the exported packet looks like the one of the replaced packet, use the replaced packet's iface ID
                //  2. Otherwise, look for the first interface with the new link layer
                //  3. Otheriwse - NOT IMPLEMENTED, will crash. (TODO: That's where we need to inject new interfaces descirption blocks...)
                int ifacedId = -1;
                if (interfaceOfReplacedPacket.LinkType == (LinkTypes)packet.LinkLayer)
                {
                    ifacedId = replacedEpb.InterfaceID;
                }
                else
                {
                    for (int i = 0; i < ifaces.Count; i++)
                    {
                        if (ifaces[i].LinkType == (LinkTypes)packet.LinkLayer)
                        {
                            ifacedId = i;
                        }
                    }

                    if (ifacedId == -1)
                    {
                        throw new ArgumentException(
                                  $"Couldn't find an interface in the PCAPNG for the requested link layer of the exported packet. Link Layer: {packet.LinkLayer}");
                    }
                }

                EnhancedPacketBlock epb = new EnhancedPacketBlock(ifacedId,
                                                                  replacedEpb.Timestamp,
                                                                  packet.Data.Length,
                                                                  packet.Data,
                                                                  new EnhancedPacketOption()
                                                                  );

                tempHandle.ReplacePacket(packetOffset, epb);
            }

            return(tempFile);
        }