public static void InterfaceDescriptionOption_ConvertToByte_Test(bool reorder)
            {
                InterfaceDescriptionOption preOption = new InterfaceDescriptionOption();
                InterfaceDescriptionOption postOption;
                preOption.Comment = "Test Comment";
                preOption.Description = "Test Description";
                preOption.EuiAddress = new byte[] { 0x00, 0x0A, 0xE6, 0xFF, 0xFE, 0x3E, 0xFD, 0xE1 };
                preOption.Filter = new byte[] { 5, 6, 7, 8 };
                preOption.FrameCheckSequence = 255;
                preOption.IPv4Address = new IPAddress_v4(new byte[] { 127, 0, 0, 1, 255, 255, 255, 0 });
                preOption.IPv6Address = new IPAddress_v6(new byte[] { 0x20, 0x01, 0x0d, 0x0db, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x14, 0x28, 0x57, 0xab, 0x40 });
                preOption.MacAddress = new PhysicalAddress(new byte[] { 0x00, 0x0A, 0xE6, 0x3E, 0xFD, 0xE1 });
                preOption.Name = "Test Name";
                preOption.OperatingSystem = "Test OperatingSystem";
                preOption.Speed = 12345678;
                preOption.TimeOffsetSeconds = 1234;
                preOption.TimestampResolution = 6;
                preOption.TimeZone = 1;


                byte[] preOptionByte = preOption.ConvertToByte(reorder, null);
                using (MemoryStream stream = new MemoryStream(preOptionByte))
                {
                    using (BinaryReader binaryReader = new BinaryReader(stream))
                    {
                        postOption = InterfaceDescriptionOption.Parse(binaryReader, reorder, null);
                    }
                }

                Assert.IsNotNull(postOption);
                Assert.AreEqual(preOption.Comment, postOption.Comment);
                Assert.AreEqual(preOption.Description, postOption.Description);
                Assert.AreEqual(preOption.EuiAddress, postOption.EuiAddress);
                Assert.AreEqual(preOption.Filter, postOption.Filter);
                Assert.AreEqual(preOption.FrameCheckSequence, postOption.FrameCheckSequence);
                Assert.AreEqual(preOption.IPv4Address, postOption.IPv4Address);
                Assert.AreEqual(preOption.IPv6Address, postOption.IPv6Address);
                Assert.AreEqual(preOption.MacAddress, postOption.MacAddress);
                Assert.AreEqual(preOption.Name, postOption.Name);
                Assert.AreEqual(preOption.OperatingSystem, postOption.OperatingSystem);
                Assert.AreEqual(preOption.Speed, postOption.Speed);
                Assert.AreEqual(preOption.TimeOffsetSeconds, postOption.TimeOffsetSeconds);
                Assert.AreEqual(preOption.TimestampResolution, postOption.TimestampResolution);
                Assert.AreEqual(preOption.TimeZone, postOption.TimeZone);

            }
            public static void InterfaceDescriptionOption_ConvertToByte_Test(bool reorder)
            {
                InterfaceDescriptionOption preOption = new InterfaceDescriptionOption();
                InterfaceDescriptionOption postOption;

                preOption.Comment            = "Test Comment";
                preOption.Description        = "Test Description";
                preOption.EuiAddress         = new byte[] { 0x00, 0x0A, 0xE6, 0xFF, 0xFE, 0x3E, 0xFD, 0xE1 };
                preOption.Filter             = new byte[] { 5, 6, 7, 8 };
                preOption.FrameCheckSequence = 255;
                preOption.IPv4Address        = new IPAddress_v4(new byte[] { 127, 0, 0, 1, 255, 255, 255, 0 });
                preOption.IPv6Address        = new IPAddress_v6(new byte[] { 0x20, 0x01, 0x0d, 0x0db, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x14, 0x28, 0x57, 0xab, 0x40 });
                preOption.MacAddress         = new PhysicalAddress(new byte[] { 0x00, 0x0A, 0xE6, 0x3E, 0xFD, 0xE1 });
                preOption.Name                = "Test Name";
                preOption.OperatingSystem     = "Test OperatingSystem";
                preOption.Speed               = 12345678;
                preOption.TimeOffsetSeconds   = 1234;
                preOption.TimestampResolution = 6;
                preOption.TimeZone            = 1;


                byte[] preOptionByte = preOption.ConvertToByte(reorder, null);
                using (MemoryStream stream = new MemoryStream(preOptionByte))
                {
                    using (BinaryReader binaryReader = new BinaryReader(stream))
                    {
                        postOption = InterfaceDescriptionOption.Parse(binaryReader, reorder, null);
                    }
                }

                Assert.IsNotNull(postOption);
                Assert.AreEqual(preOption.Comment, postOption.Comment);
                Assert.AreEqual(preOption.Description, postOption.Description);
                Assert.AreEqual(preOption.EuiAddress, postOption.EuiAddress);
                Assert.AreEqual(preOption.Filter, postOption.Filter);
                Assert.AreEqual(preOption.FrameCheckSequence, postOption.FrameCheckSequence);
                Assert.AreEqual(preOption.IPv4Address, postOption.IPv4Address);
                Assert.AreEqual(preOption.IPv6Address, postOption.IPv6Address);
                Assert.AreEqual(preOption.MacAddress, postOption.MacAddress);
                Assert.AreEqual(preOption.Name, postOption.Name);
                Assert.AreEqual(preOption.OperatingSystem, postOption.OperatingSystem);
                Assert.AreEqual(preOption.Speed, postOption.Speed);
                Assert.AreEqual(preOption.TimeOffsetSeconds, postOption.TimeOffsetSeconds);
                Assert.AreEqual(preOption.TimestampResolution, postOption.TimestampResolution);
                Assert.AreEqual(preOption.TimeZone, postOption.TimeZone);
            }
 public static InterfaceDescriptionBlock GetEmptyInterfaceDescription(bool reverseBytesOrder)
 {
     InterfaceDescriptionOption Options = new InterfaceDescriptionOption();
     return new InterfaceDescriptionBlock(LinkTypes.Ethernet, 65535, Options);
 }
        /// <summary>
        /// The Interface Description Block is mandatory. This block is needed to specify the characteristics of the network interface 
        /// on which the capture has been made. In order to properly associate the captured data to the corresponding interface, the Interface 
        /// Description Block must be defined before any other block that uses it; therefore, this block is usually placed immediately after 
        /// the Section Header Block.
        /// </summary>          
        public InterfaceDescriptionBlock(LinkTypes LinkType, int SnapLength, InterfaceDescriptionOption Options, long PositionInStream = 0)
        {
            Contract.Requires<ArgumentNullException>(Options != null, "Options cannot be null");

            this.LinkType = LinkType;
            this.SnapLength = SnapLength;
            this.options = Options;
            this.PositionInStream = PositionInStream;
        }
 public static InterfaceDescriptionOption Parse(BinaryReader binaryReader, bool reverseByteOrder, Action<Exception> ActionOnException)
 {
     Contract.Requires<ArgumentNullException>(binaryReader != null, "binaryReader cannot be null");
     InterfaceDescriptionOption option = new InterfaceDescriptionOption();
     List<KeyValuePair<ushort, byte[]>> optionsList = EkstractOptions(binaryReader, reverseByteOrder, ActionOnException);
     
     if (optionsList.Any())
     {                          
         foreach (var item in optionsList)
         {
             try
             {
                 switch (item.Key)
                 {
                     case (ushort)InterfaceDescriptionOptionCode.CommentCode:
                         option.Comment = UTF8Encoding.UTF8.GetString(item.Value);
                         break;
                     case (ushort)InterfaceDescriptionOptionCode.NameCode:
                         option.Name = UTF8Encoding.UTF8.GetString(item.Value);
                         break;
                     case (ushort)InterfaceDescriptionOptionCode.DescriptionCode:
                         option.Description = UTF8Encoding.UTF8.GetString(item.Value);
                         break;
                     case (ushort)InterfaceDescriptionOptionCode.IPv4AddressCode:                                 
                         if (item.Value.Length == 8)
                         {
                             option.IPv4Address = new IPAddress_v4(item.Value);
                         }
                         else
                             throw new ArgumentException(string.Format("[InterfaceDescriptio.Parse] IPv4AddressCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 8));
                         break;
                     case (ushort)InterfaceDescriptionOptionCode.IPv6AddressCode:                                
                         if (item.Value.Length == 17)
                         {
                             option.IPv6Address = new IPAddress_v6(item.Value);
                         }
                         else
                             throw new ArgumentException(string.Format("[InterfaceDescriptio.Parse] IPv6AddressCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 17));
                         break;
                     case (ushort)InterfaceDescriptionOptionCode.MacAddressCode:                                
                         if (item.Value.Length == 6)
                             option.MacAddress = new PhysicalAddress(item.Value);
                         else
                             throw new ArgumentException(string.Format("[InterfaceDescriptio.Parse] MacAddressCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 6));
                         break;
                     case (ushort)InterfaceDescriptionOptionCode.EuiAddressCode:                                 
                         if (item.Value.Length == 8)
                             option.EuiAddress = item.Value;
                         else
                             throw new ArgumentException(string.Format("[InterfaceDescriptio.Parse] EuiAddressCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 8));
                         break;
                     case (ushort)InterfaceDescriptionOptionCode.SpeedCode:                               
                         if (item.Value.Length == 8)
                             option.Speed = (BitConverter.ToInt64(item.Value, 0)).ReverseByteOrder(reverseByteOrder);
                         else
                             throw new ArgumentException(string.Format("[InterfaceDescriptio.Parse] SpeedCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 8));
                         break;
                     case (ushort)InterfaceDescriptionOptionCode.TimestampResolutionCode:                               
                         if (item.Value.Length == 1)
                             option.TimestampResolution = item.Value[0];
                         else
                             throw new ArgumentException(string.Format("[InterfaceDescriptio.Parse] TimestampResolutionCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 1));
                         break;
                     case (ushort)InterfaceDescriptionOptionCode.TimeZoneCode:                                						
                         if (item.Value.Length == 4)
                             option.TimeZone = (BitConverter.ToInt32(item.Value, 0)).ReverseByteOrder(reverseByteOrder);	// GMT offset
                         else
                             throw new ArgumentException(string.Format("[InterfaceDescriptio.Parse] TimeZoneCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 4));
                         break;
                     case (ushort)InterfaceDescriptionOptionCode.FilterCode:
                         option.Filter = item.Value;	
                         break;
                     case (ushort)InterfaceDescriptionOptionCode.OperatingSystemCode:
                         option.OperatingSystem = UTF8Encoding.UTF8.GetString(item.Value);
                         break;
                     case (ushort)InterfaceDescriptionOptionCode.FrameCheckSequenceCode:                                
                          if (item.Value.Length == 1)
                              option.FrameCheckSequence = item.Value[0];
                         else
                              throw new ArgumentException(string.Format("[InterfaceDescriptio.Parse] FrameCheckSequenceCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 1));
                         break;
                     case (ushort)InterfaceDescriptionOptionCode.TimeOffsetSecondsCode:                               
                         if (item.Value.Length == 8)
                             option.TimeOffsetSeconds = (BitConverter.ToInt64(item.Value, 0)).ReverseByteOrder(reverseByteOrder);
                         else
                             throw new ArgumentException(string.Format("[InterfaceDescriptio.Parse] TimeOffsetSecondsCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 8));
                         break;
                     case (ushort)InterfaceDescriptionOptionCode.EndOfOptionsCode:
                     default:
                         break;
                 }
             }
             catch (Exception exc)
             {
                 if (ActionOnException != null)
                     ActionOnException(exc);
             }
         }
     }               
     return option;
 }
        public static InterfaceDescriptionOption Parse(BinaryReader binaryReader, bool reverseByteOrder, Action <Exception> ActionOnException)
        {
            //Contract.Requires<ArgumentNullException>(binaryReader != null, "binaryReader cannot be null");
            InterfaceDescriptionOption            option      = new InterfaceDescriptionOption();
            List <KeyValuePair <ushort, byte[]> > optionsList = EkstractOptions(binaryReader, reverseByteOrder, ActionOnException);

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

                        case (ushort)InterfaceDescriptionOptionCode.NameCode:
                            option.Name = UTF8Encoding.UTF8.GetString(item.Value);
                            break;

                        case (ushort)InterfaceDescriptionOptionCode.DescriptionCode:
                            option.Description = UTF8Encoding.UTF8.GetString(item.Value);
                            break;

                        case (ushort)InterfaceDescriptionOptionCode.IPv4AddressCode:
                            if (item.Value.Length == 8)
                            {
                                option.IPv4Address = new IPAddress_v4(item.Value);
                            }
                            else
                            {
                                throw new ArgumentException(string.Format("[InterfaceDescriptio.Parse] IPv4AddressCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 8));
                            }
                            break;

                        case (ushort)InterfaceDescriptionOptionCode.IPv6AddressCode:
                            if (item.Value.Length == 17)
                            {
                                option.IPv6Address = new IPAddress_v6(item.Value);
                            }
                            else
                            {
                                throw new ArgumentException(string.Format("[InterfaceDescriptio.Parse] IPv6AddressCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 17));
                            }
                            break;

                        case (ushort)InterfaceDescriptionOptionCode.MacAddressCode:
                            if (item.Value.Length == 6)
                            {
                                option.MacAddress = new PhysicalAddress(item.Value);
                            }
                            else
                            {
                                throw new ArgumentException(string.Format("[InterfaceDescriptio.Parse] MacAddressCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 6));
                            }
                            break;

                        case (ushort)InterfaceDescriptionOptionCode.EuiAddressCode:
                            if (item.Value.Length == 8)
                            {
                                option.EuiAddress = item.Value;
                            }
                            else
                            {
                                throw new ArgumentException(string.Format("[InterfaceDescriptio.Parse] EuiAddressCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 8));
                            }
                            break;

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

                        case (ushort)InterfaceDescriptionOptionCode.TimestampResolutionCode:
                            if (item.Value.Length == 1)
                            {
                                option.TimestampResolution = item.Value[0];
                            }
                            else
                            {
                                throw new ArgumentException(string.Format("[InterfaceDescriptio.Parse] TimestampResolutionCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 1));
                            }
                            break;

                        case (ushort)InterfaceDescriptionOptionCode.TimeZoneCode:
                            if (item.Value.Length == 4)
                            {
                                option.TimeZone = (BitConverter.ToInt32(item.Value, 0)).ReverseByteOrder(reverseByteOrder);     // GMT offset
                            }
                            else
                            {
                                throw new ArgumentException(string.Format("[InterfaceDescriptio.Parse] TimeZoneCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 4));
                            }
                            break;

                        case (ushort)InterfaceDescriptionOptionCode.FilterCode:
                            option.Filter = item.Value;
                            break;

                        case (ushort)InterfaceDescriptionOptionCode.OperatingSystemCode:
                            option.OperatingSystem = UTF8Encoding.UTF8.GetString(item.Value);
                            break;

                        case (ushort)InterfaceDescriptionOptionCode.FrameCheckSequenceCode:
                            if (item.Value.Length == 1)
                            {
                                option.FrameCheckSequence = item.Value[0];
                            }
                            else
                            {
                                throw new ArgumentException(string.Format("[InterfaceDescriptio.Parse] FrameCheckSequenceCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 1));
                            }
                            break;

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

                        case (ushort)InterfaceDescriptionOptionCode.EndOfOptionsCode:
                        default:
                            break;
                        }
                    }
                    catch (Exception exc)
                    {
                        if (ActionOnException != null)
                        {
                            ActionOnException(exc);
                        }
                    }
                }
            }
            return(option);
        }