MaximumDhcpMessageSizeOption.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.IO;
  18. namespace DnsServerCore.Dhcp.Options
  19. {
  20. class MaximumDhcpMessageSizeOption : DhcpOption
  21. {
  22. #region variables
  23. ushort _length;
  24. #endregion
  25. #region constructor
  26. public MaximumDhcpMessageSizeOption(ushort length)
  27. : base(DhcpOptionCode.MaximumDhcpMessageSize)
  28. {
  29. if (length < 576)
  30. throw new ArgumentOutOfRangeException(nameof(length), "Length must be 576 bytes or more.");
  31. _length = length;
  32. }
  33. public MaximumDhcpMessageSizeOption(Stream s)
  34. : base(DhcpOptionCode.MaximumDhcpMessageSize, 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. byte[] buffer = s.ReadExactly(2);
  43. Array.Reverse(buffer);
  44. _length = BitConverter.ToUInt16(buffer, 0);
  45. if (_length < 576)
  46. _length = 576;
  47. }
  48. protected override void WriteOptionValue(Stream s)
  49. {
  50. byte[] buffer = BitConverter.GetBytes(_length);
  51. Array.Reverse(buffer);
  52. s.Write(buffer);
  53. }
  54. #endregion
  55. #region properties
  56. public uint Length
  57. { get { return _length; } }
  58. #endregion
  59. }
  60. }