Example #1
0
        public static PacketOption Parse(BinaryReader binaryReader, bool reverseByteOrder, Action <Exception> ActionOnException)
        {
            Contract.Requires <ArgumentNullException>(binaryReader != null, "binaryReader cannot be null");

            PacketOption option = new PacketOption();
            List <KeyValuePair <ushort, byte[]> > optionsList = EkstractOptions(binaryReader, reverseByteOrder, ActionOnException);

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

                        case (ushort)PacketOptionCode.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("[PacketOption.Parse] PacketFlagCode contains invalid length. Received: {0} bytes, expected: {1}.", item.Value.Length, 4));
                            }
                            break;

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

                        case (ushort)PacketOptionCode.EndOfOptionsCode:
                        default:
                            break;
                        }
                    }
                    catch (Exception exc)
                    {
                        if (ActionOnException != null)
                        {
                            ActionOnException(exc);
                        }
                    }
                }
            }
            return(option);
        }
Example #2
0
            public static void PacketOption_ConvertToByte_Test(bool reorder)
            {
                PacketOption preOption = new PacketOption();
                PacketOption postOption;
                preOption.Comment = "Test Comment";
                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 = PacketOption.Parse(binaryReader, reorder, null);
                    }
                }

                Assert.IsNotNull(postOption);
                Assert.AreEqual(preOption.Comment, postOption.Comment);
                Assert.AreEqual(preOption.Hash.Algorithm, postOption.Hash.Algorithm);
                Assert.AreEqual(preOption.Hash.Value, postOption.Hash.Value);
                Assert.AreEqual(preOption.PacketFlag.Flag, postOption.PacketFlag.Flag);
            }
Example #3
0
            public static void PacketOption_ConvertToByte_Test(bool reorder)
            {
                PacketOption preOption = new PacketOption();
                PacketOption postOption;

                preOption.Comment = "Test Comment";
                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 = PacketOption.Parse(binaryReader, reorder, null);
                    }
                }

                Assert.IsNotNull(postOption);
                Assert.AreEqual(preOption.Comment, postOption.Comment);
                Assert.AreEqual(preOption.Hash.Algorithm, postOption.Hash.Algorithm);
                Assert.AreEqual(preOption.Hash.Value, postOption.Hash.Value);
                Assert.AreEqual(preOption.PacketFlag.Flag, postOption.PacketFlag.Flag);
            }
Example #4
0
        public static PacketOption Parse(BinaryReader binaryReader, bool reverseByteOrder, Action<Exception> ActionOnException)
        {
            Contract.Requires<ArgumentNullException>(binaryReader != null, "binaryReader cannot be null");

            PacketOption option = new PacketOption();
            List<KeyValuePair<ushort, byte[]>> optionsList = EkstractOptions(binaryReader, reverseByteOrder, ActionOnException);
            if (optionsList.Any())
            {
                foreach (var item in optionsList)
                {
                    try
                    {
                        switch (item.Key)
                        {
                            case (ushort)PacketOptionCode.CommentCode:
                                option.Comment = UTF8Encoding.UTF8.GetString(item.Value);
                                break;
                            case (ushort)PacketOptionCode.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("[PacketOption.Parse] PacketFlagCode contains invalid length. Received: {0} bytes, expected: {1}.", item.Value.Length, 4));
                                break;
                            case (ushort)PacketOptionCode.HashCode:
                                option.Hash = new HashBlock(item.Value);
                                break;
                            case (ushort)PacketOptionCode.EndOfOptionsCode:
                            default:
                                break;
                        }
                    }
                    catch (Exception exc)
                    {
                        if (ActionOnException != null)
                            ActionOnException(exc);
                    }
                }
            }
            return option;
        }
Example #5
0
 /// <summary>
 /// The Packet Block is marked obsolete, better use the Enhanced Packet Block instead!
 /// A Packet Block is the standard container for storing the packets coming from the network. The 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.
 /// </summary>          
 public PacketBlock(short InterfaceID, TimestampHelper Timestamp,  byte[] Data, int PacketLength, PacketOption 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;
 }