ClientIdentifierOption.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2022 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 static
  38. public static ClientIdentifierOption Parse(string clientIdentifier)
  39. {
  40. string[] parts = clientIdentifier.Split('-');
  41. return new ClientIdentifierOption(byte.Parse(parts[0]), Convert.FromHexString(parts[1]));
  42. }
  43. #endregion
  44. #region protected
  45. protected override void ParseOptionValue(Stream s)
  46. {
  47. if (s.Length < 2)
  48. throw new InvalidDataException();
  49. int type = s.ReadByte();
  50. if (type < 0)
  51. throw new EndOfStreamException();
  52. _type = (byte)type;
  53. _identifier = s.ReadBytes((int)s.Length - 1);
  54. }
  55. protected override void WriteOptionValue(Stream s)
  56. {
  57. s.WriteByte(_type);
  58. s.Write(_identifier);
  59. }
  60. #endregion
  61. #region public
  62. public override bool Equals(object obj)
  63. {
  64. if (obj is null)
  65. return false;
  66. if (ReferenceEquals(this, obj))
  67. return true;
  68. return Equals(obj as ClientIdentifierOption);
  69. }
  70. public bool Equals(ClientIdentifierOption other)
  71. {
  72. if (other is null)
  73. return false;
  74. if (this._type != other._type)
  75. return false;
  76. if (this._identifier.Length != other._identifier.Length)
  77. return false;
  78. for (int i = 0; i < this._identifier.Length; i++)
  79. {
  80. if (this._identifier[i] != other._identifier[i])
  81. return false;
  82. }
  83. return true;
  84. }
  85. public override int GetHashCode()
  86. {
  87. int hashCode = 937899003;
  88. hashCode = hashCode * -1521134295 + _type.GetHashCode();
  89. hashCode = hashCode * -1521134295 + BitConverter.ToInt32(_identifier, 0);
  90. return hashCode;
  91. }
  92. public override string ToString()
  93. {
  94. return _type.ToString() + "-" + Convert.ToHexString(_identifier);
  95. }
  96. #endregion
  97. #region properties
  98. public byte Type
  99. { get { return _type; } }
  100. public byte[] Identifier
  101. { get { return _identifier; } }
  102. #endregion
  103. }
  104. }