ClientIdentifierOption.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2024 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;
  18. using TechnitiumLibrary.IO;
  19. namespace DnsServerCore.Dhcp.Options
  20. {
  21. public class ClientIdentifierOption : DhcpOption, IEquatable<ClientIdentifierOption>
  22. {
  23. #region variables
  24. byte _type;
  25. byte[] _identifier;
  26. #endregion
  27. #region constructor
  28. public ClientIdentifierOption(byte type, byte[] identifier)
  29. : base(DhcpOptionCode.ClientIdentifier)
  30. {
  31. _type = type;
  32. _identifier = identifier;
  33. }
  34. public ClientIdentifierOption(Stream s)
  35. : base(DhcpOptionCode.ClientIdentifier, s)
  36. { }
  37. #endregion
  38. #region static
  39. public static ClientIdentifierOption Parse(string clientIdentifier)
  40. {
  41. string[] parts = clientIdentifier.Split('-');
  42. return new ClientIdentifierOption(byte.Parse(parts[0]), Convert.FromHexString(parts[1]));
  43. }
  44. #endregion
  45. #region protected
  46. protected override void ParseOptionValue(Stream s)
  47. {
  48. if (s.Length < 2)
  49. throw new InvalidDataException();
  50. int type = s.ReadByte();
  51. if (type < 0)
  52. throw new EndOfStreamException();
  53. _type = (byte)type;
  54. _identifier = s.ReadExactly((int)s.Length - 1);
  55. }
  56. protected override void WriteOptionValue(Stream s)
  57. {
  58. s.WriteByte(_type);
  59. s.Write(_identifier);
  60. }
  61. #endregion
  62. #region public
  63. public override bool Equals(object obj)
  64. {
  65. if (obj is null)
  66. return false;
  67. if (ReferenceEquals(this, obj))
  68. return true;
  69. return Equals(obj as ClientIdentifierOption);
  70. }
  71. public bool Equals(ClientIdentifierOption other)
  72. {
  73. if (other is null)
  74. return false;
  75. if (this._type != other._type)
  76. return false;
  77. if (this._identifier.Length != other._identifier.Length)
  78. return false;
  79. for (int i = 0; i < this._identifier.Length; i++)
  80. {
  81. if (this._identifier[i] != other._identifier[i])
  82. return false;
  83. }
  84. return true;
  85. }
  86. public override int GetHashCode()
  87. {
  88. return HashCode.Combine(_type, _identifier.GetArrayHashCode());
  89. }
  90. public override string ToString()
  91. {
  92. return _type.ToString() + "-" + Convert.ToHexString(_identifier);
  93. }
  94. #endregion
  95. #region properties
  96. public byte Type
  97. { get { return _type; } }
  98. public byte[] Identifier
  99. { get { return _identifier; } }
  100. #endregion
  101. }
  102. }