コード例 #1
0
            public static void EnchantedPacketOption_ConvertToByte_Test(bool reorder)
            {
                EnchantedPacketOption preOption = new EnchantedPacketOption();
                EnchantedPacketOption postOption;
                preOption.Comment = "Test Comment";
                preOption.DropCount = 25;
                byte[] md5Hash = { 3, 87, 248, 225, 163, 56, 121, 102, 219, 226, 164, 68, 165, 51, 9, 177, 59 };
                preOption.Hash = new HashBlock(md5Hash);
                preOption.PacketFlag = new PacketBlockFlags(0xFF000000);
                byte[] preOptionByte = preOption.ConvertToByte(reorder, null);
                using (MemoryStream stream = new MemoryStream(preOptionByte))
                {
                    using (BinaryReader binaryReader = new BinaryReader(stream))
                    {
                        postOption = EnchantedPacketOption.Parse(binaryReader, reorder, null);
                    }
                }

                Assert.IsNotNull(postOption);
                Assert.AreEqual(preOption.Comment, postOption.Comment);
                Assert.AreEqual(preOption.DropCount, postOption.DropCount);
                Assert.AreEqual(preOption.Hash.Algorithm, postOption.Hash.Algorithm);
                Assert.AreEqual(preOption.Hash.Value, postOption.Hash.Value);
                Assert.AreEqual(preOption.PacketFlag.Flag, postOption.PacketFlag.Flag);
            }
コード例 #2
0
            public static void EnchantedPacketOption_ConvertToByte_Test(bool reorder)
            {
                EnchantedPacketOption preOption = new EnchantedPacketOption();
                EnchantedPacketOption postOption;

                preOption.Comment   = "Test Comment";
                preOption.DropCount = 25;
                byte[] md5Hash = { 3, 87, 248, 225, 163, 56, 121, 102, 219, 226, 164, 68, 165, 51, 9, 177, 59 };
                preOption.Hash       = new HashBlock(md5Hash);
                preOption.PacketFlag = new PacketBlockFlags(0xFF000000);
                byte[] preOptionByte = preOption.ConvertToByte(reorder, null);
                using (MemoryStream stream = new MemoryStream(preOptionByte))
                {
                    using (BinaryReader binaryReader = new BinaryReader(stream))
                    {
                        postOption = EnchantedPacketOption.Parse(binaryReader, reorder, null);
                    }
                }

                Assert.IsNotNull(postOption);
                Assert.AreEqual(preOption.Comment, postOption.Comment);
                Assert.AreEqual(preOption.DropCount, postOption.DropCount);
                Assert.AreEqual(preOption.Hash.Algorithm, postOption.Hash.Algorithm);
                Assert.AreEqual(preOption.Hash.Value, postOption.Hash.Value);
                Assert.AreEqual(preOption.PacketFlag.Flag, postOption.PacketFlag.Flag);
            }
コード例 #3
0
 public static EnchantedPacketOption Parse(BinaryReader binaryReader, bool reverseByteOrder, Action<Exception> ActionOnException)
 {
     Contract.Requires<ArgumentNullException>(binaryReader != null, "binaryReader cannot be null");
     EnchantedPacketOption option = new EnchantedPacketOption();
     List<KeyValuePair<ushort, byte[]>> optionsList = EkstractOptions(binaryReader, reverseByteOrder, ActionOnException);
     
     if (optionsList.Any())
     {
         foreach (var item in optionsList)
         {   
             try
             {
                 switch (item.Key)
                 {
                     case (ushort)EnchantedPacketOptionCode.CommentCode:
                         option.Comment = UTF8Encoding.UTF8.GetString(item.Value);
                         break;
                     case (ushort)EnchantedPacketOptionCode.PacketFlagCode:
                         if (item.Value.Length == 4)
                         {
                             uint packetFlag = (BitConverter.ToUInt32(item.Value, 0)).ReverseByteOrder(reverseByteOrder);
                             option.PacketFlag = new PacketBlockFlags(packetFlag);
                         }
                         else
                             throw new ArgumentException(string.Format("[EnchantedPacketOptio.Parse] PacketFlagCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 4));
                         break;
                     case (ushort)EnchantedPacketOptionCode.HashCode:
                         option.Hash = new HashBlock(item.Value);
                         break;
                     case (ushort)EnchantedPacketOptionCode.DropCountCode:
                         if (item.Value.Length == 8)
                             option.DropCount = (BitConverter.ToInt64(item.Value, 0)).ReverseByteOrder(reverseByteOrder);
                         else
                             throw new ArgumentException(string.Format("[EnchantedPacketOptio.Parse] HashCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 8));
                         break;
                     case (ushort)EnchantedPacketOptionCode.EndOfOptionsCode:
                     default:
                         break;
                 }
             }
             catch(Exception exc)
             {
                 if (ActionOnException != null)
                     ActionOnException(exc);
             }
         }
     }
     return option;
 }
コード例 #4
0
        public static EnchantedPacketOption Parse(BinaryReader binaryReader, bool reverseByteOrder, Action <Exception> ActionOnException)
        {
            Contract.Requires <ArgumentNullException>(binaryReader != null, "binaryReader cannot be null");
            EnchantedPacketOption option = new EnchantedPacketOption();
            List <KeyValuePair <ushort, byte[]> > optionsList = EkstractOptions(binaryReader, reverseByteOrder, ActionOnException);

            if (optionsList.Any())
            {
                foreach (var item in optionsList)
                {
                    try
                    {
                        switch (item.Key)
                        {
                        case (ushort)EnchantedPacketOptionCode.CommentCode:
                            option.Comment = UTF8Encoding.UTF8.GetString(item.Value);
                            break;

                        case (ushort)EnchantedPacketOptionCode.PacketFlagCode:
                            if (item.Value.Length == 4)
                            {
                                uint packetFlag = (BitConverter.ToUInt32(item.Value, 0)).ReverseByteOrder(reverseByteOrder);
                                option.PacketFlag = new PacketBlockFlags(packetFlag);
                            }
                            else
                            {
                                throw new ArgumentException(string.Format("[EnchantedPacketOptio.Parse] PacketFlagCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 4));
                            }
                            break;

                        case (ushort)EnchantedPacketOptionCode.HashCode:
                            option.Hash = new HashBlock(item.Value);
                            break;

                        case (ushort)EnchantedPacketOptionCode.DropCountCode:
                            if (item.Value.Length == 8)
                            {
                                option.DropCount = (BitConverter.ToInt64(item.Value, 0)).ReverseByteOrder(reverseByteOrder);
                            }
                            else
                            {
                                throw new ArgumentException(string.Format("[EnchantedPacketOptio.Parse] HashCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 8));
                            }
                            break;

                        case (ushort)EnchantedPacketOptionCode.EndOfOptionsCode:
                        default:
                            break;
                        }
                    }
                    catch (Exception exc)
                    {
                        if (ActionOnException != null)
                        {
                            ActionOnException(exc);
                        }
                    }
                }
            }
            return(option);
        }
コード例 #5
0
        /// <summary>
        /// An Enhanced Packet Block is the standard container for storing the packets coming from the network. The Enhanced Packet Block 
        /// is optional because packets can be stored either by means of this block or the Simple Packet Block, which can be used to speed 
        /// up dump generation. 
        /// The Enhanced Packet Block is an improvement over the original Packet Block: 
        /// it stores the Interface Identifier as a 32bit integer value. This is a requirement when a capture stores packets coming from 
        /// a large number of interfaces differently from the Packet Block, the number of packets dropped by the capture system between 
        /// this packet and the previous one is not stored in the header, but rather in an option of the block itself.
        /// </summary>        
        public EnchantedPacketBlock(int InterfaceID, TimestampHelper Timestamp, int PacketLength, byte[] Data, EnchantedPacketOption Options, long PositionInStream = 0)
        {
            Contract.Requires<ArgumentNullException>(Timestamp != null, "Timestamp cannot be null");
            Contract.Requires<ArgumentNullException>(Options != null, "Options cannot be null");
            Contract.Requires<ArgumentNullException>(Data != null, "Data cannot be null");

            this.InterfaceID = InterfaceID;
            this.Timestamp = Timestamp;
            this.PacketLength = PacketLength;
            this.data = Data;
            this.options = Options;
            this.PositionInStream = PositionInStream;
        }