ClientIdentifierOption.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2021 Shreyas Zare (shreyas@technitium.com)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using System;
  16. using System.IO;
  17. using TechnitiumLibrary.IO;
  18. namespace DnsServerCore.Dhcp.Options
  19. {
  20. public class ClientIdentifierOption : DhcpOption, IEquatable<ClientIdentifierOption>
  21. {
  22. #region variables
  23. byte _type;
  24. byte[] _identifier;
  25. #endregion
  26. #region constructor
  27. public ClientIdentifierOption(byte type, byte[] identifier)
  28. : base(DhcpOptionCode.ClientIdentifier)
  29. {
  30. _type = type;
  31. _identifier = identifier;
  32. }
  33. public ClientIdentifierOption(Stream s)
  34. : base(DhcpOptionCode.ClientIdentifier, s)
  35. { }
  36. #endregion
  37. #region protected
  38. protected override void ParseOptionValue(Stream s)
  39. {
  40. if (s.Length < 2)
  41. throw new InvalidDataException();
  42. int type = s.ReadByte();
  43. if (type < 0)
  44. throw new EndOfStreamException();
  45. _type = (byte)type;
  46. _identifier = s.ReadBytes((int)s.Length - 1);
  47. }
  48. protected override void WriteOptionValue(Stream s)
  49. {
  50. s.WriteByte(_type);
  51. s.Write(_identifier);
  52. }
  53. #endregion
  54. #region public
  55. public override bool Equals(object obj)
  56. {
  57. if (obj is null)
  58. return false;
  59. if (ReferenceEquals(this, obj))
  60. return true;
  61. return Equals(obj as ClientIdentifierOption);
  62. }
  63. public bool Equals(ClientIdentifierOption other)
  64. {
  65. if (other is null)
  66. return false;
  67. if (this._type != other._type)
  68. return false;
  69. if (this._identifier.Length != other._identifier.Length)
  70. return false;
  71. for (int i = 0; i < this._identifier.Length; i++)
  72. {
  73. if (this._identifier[i] != other._identifier[i])
  74. return false;
  75. }
  76. return true;
  77. }
  78. public override int GetHashCode()
  79. {
  80. int hashCode = 937899003;
  81. hashCode = hashCode * -1521134295 + _type.GetHashCode();
  82. hashCode = hashCode * -1521134295 + BitConverter.ToInt32(_identifier, 0);
  83. return hashCode;
  84. }
  85. #endregion
  86. #region properties
  87. public byte Type
  88. { get { return _type; } }
  89. public byte[] Identifier
  90. { get { return _identifier; } }
  91. #endregion
  92. }
  93. }