/* 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 PacketDotNet.Utils; namespace PacketDotNet.LLDP { /// /// An Organization Specific TLV /// /// [TLV Type Length : 2][Organizationally Unique Identifier OUI : 3] /// [Organizationally Defined Subtype : 1][Organizationally Defined Information String : 0 - 507] /// public class OrganizationSpecific : 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 private const int OUILength = 3; private const int OUISubTypeLength = 1; #region Constructors /// /// Creates an Organization Specific TLV /// /// /// The LLDP Data unit being modified /// /// /// The Organization Specific TLV's offset from the /// origin of the LLDP /// public OrganizationSpecific(byte[] bytes, int offset) : base(bytes, offset) { log.Debug(""); } /// /// Creates an Organization Specific TLV and sets it value /// /// /// An Organizationally Unique Identifier /// /// /// An Organizationally Defined SubType /// /// /// An Organizationally Defined Information String /// public OrganizationSpecific(byte[] oui, int subType, byte[] infoString) { log.Debug(""); var length = TLVTypeLength.TypeLengthLength + OUILength + OUISubTypeLength; var bytes = new byte[length]; var offset = 0; tlvData = new ByteArraySegment(bytes, offset, length); Type = TLVTypes.OrganizationSpecific; OrganizationUniqueID = oui; OrganizationDefinedSubType = subType; OrganizationDefinedInfoString = infoString; } #endregion #region Properties /// /// An Organizationally Unique Identifier /// public byte[] OrganizationUniqueID { get { byte[] oui = new byte[OUILength]; Array.Copy(tlvData.Bytes, ValueOffset, oui, 0, OUILength); return oui; } set { Array.Copy(value, 0, tlvData.Bytes, ValueOffset, OUILength); } } /// /// An Organizationally Defined SubType /// public int OrganizationDefinedSubType { get { return tlvData.Bytes[ValueOffset + OUILength]; } set { tlvData.Bytes[ValueOffset + OUILength] = (byte)value; } } /// /// An Organizationally Defined Information String /// public byte[] OrganizationDefinedInfoString { get { var length = Length - (OUILength + OUISubTypeLength); var bytes = new byte[length]; Array.Copy(tlvData.Bytes, ValueOffset + OUILength + OUISubTypeLength, bytes, 0, length); return bytes; } set { var length = Length - (OUILength + OUISubTypeLength); // do we have the right sized tlv? if(value.Length != length) { var headerLength = TLVTypeLength.TypeLengthLength + OUILength + OUISubTypeLength; // resize the tlv var newLength = headerLength + value.Length; var bytes = new byte[newLength]; // copy the header bytes over Array.Copy(tlvData.Bytes, tlvData.Offset, bytes, 0, headerLength); // assign a new ByteArrayAndOffset to tlvData var offset = 0; tlvData = new ByteArraySegment(bytes, offset, newLength); } // copy the byte array in Array.Copy(value, 0, tlvData.Bytes, ValueOffset + OUILength + OUISubTypeLength, value.Length); } } /// /// Convert this Organization Specific TLV to a string. /// /// /// A human readable string /// public override string ToString () { return string.Format("[OrganizationSpecific: OrganizationUniqueID={0}, OrganizationDefinedSubType={1}, OrganizationDefinedInfoString={2}]", OrganizationUniqueID, OrganizationDefinedSubType, OrganizationDefinedInfoString); } #endregion } }