/* This file is part of PacketDotNet PacketDotNet is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. PacketDotNet is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with PacketDotNet. If not, see . */ /* * Copyright 2010 Evan Plaice * Copyright 2010 Chris Morgan */ using System; using MiscUtil.Conversion; using PacketDotNet.Utils; namespace PacketDotNet.LLDP { /// /// A Type-Length-Value object /// public class TLV { // NOTE: No need to warn about lack of use, the compiler won't // put any calls to 'log' here but we need 'log' to exist to compile #pragma warning disable 0169, 0649 private static readonly ILogInactive log; #pragma warning restore 0169, 0649 #region Constructors /// /// Create a tlv /// public TLV() { } /// /// Creates a TLV /// /// /// Bytes that comprise the TLV /// /// /// The TLVs offset from the start of byte[] bytes /// public TLV(byte[] bytes, int offset) { // setup a local ByteArrayAndOffset in order to retrieve the value length // NOTE: we cannot set tlvData to retrieve the value length as // setting tlvData results in the TypeLength.Length being updated with // the length of the ByteArrayAndOffset which would overwrite the value // we are trying to retrieve var byteArraySegment = new ByteArraySegment(bytes, offset, TLVTypeLength.TypeLengthLength); TypeLength = new TLVTypeLength(byteArraySegment); // set the tlvData assuming we have at least the bytes required for the // type/length fields tlvData = new ByteArraySegment(bytes, offset, TypeLength.Length + TLVTypeLength.TypeLengthLength); // retrieve the actual length tlvData.Length = TypeLength.Length + TLVTypeLength.TypeLengthLength; } #endregion #region Properties /// /// Length of value portion of the TLV /// NOTE: Does not include the length of the Type and Length fields /// public int Length { get { return TypeLength.Length; } // Length set property is internal because the tlv length is // automatically set based on the length of the tlv value internal set { TypeLength.Length = value; } } /// /// Total length of the TLV, including the length of the Type and Length fields /// public int TotalLength { get { return tlvData.Length; } } /// /// Tlv type /// public TLVTypes Type { get { return TypeLength.Type; } set { log.DebugFormat("value {0}", value); TypeLength.Type = value; } } /// /// Offset to the value bytes of the TLV /// internal int ValueOffset { get { return tlvData.Offset + TLVTypeLength.TypeLengthLength; } } /// /// Return a byte[] that contains the tlv /// public virtual byte[] Bytes { get { return tlvData.ActualBytes(); } } #endregion #region Members /// /// Points to the TLV data /// private ByteArraySegment _tlvData; /// /// Points to the TLV data /// internal ByteArraySegment tlvData { get { return _tlvData; } set { _tlvData = value; // create a new TypeLength that points at the new ByteArrayAndOffset TypeLength = new TLVTypeLength(value); // update the length based upon the length of the ByteArrayAndOffset TypeLength.Length = value.Length - TLVTypeLength.TypeLengthLength; } } /// /// Interface to this TLVs type and length /// protected TLVTypeLength TypeLength; #endregion } }