ClientFullyQualifiedDomainNameOption.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 System.Text;
  18. using TechnitiumLibrary.IO;
  19. using TechnitiumLibrary.Net.Dns;
  20. namespace DnsServerCore.Dhcp.Options
  21. {
  22. [Flags]
  23. enum ClientFullyQualifiedDomainNameFlags : byte
  24. {
  25. None = 0,
  26. ShouldUpdateDns = 1,
  27. OverrideByServer = 2,
  28. EncodeUsingCanonicalWireFormat = 4,
  29. NoDnsUpdate = 8,
  30. }
  31. class ClientFullyQualifiedDomainNameOption : DhcpOption
  32. {
  33. #region variables
  34. ClientFullyQualifiedDomainNameFlags _flags;
  35. byte _rcode1;
  36. byte _rcode2;
  37. string _domainName;
  38. #endregion
  39. #region constructor
  40. public ClientFullyQualifiedDomainNameOption(ClientFullyQualifiedDomainNameFlags flags, byte rcode1, byte rcode2, string domainName)
  41. : base(DhcpOptionCode.ClientFullyQualifiedDomainName)
  42. {
  43. _flags = flags;
  44. _rcode1 = rcode1;
  45. _rcode2 = rcode2;
  46. _domainName = domainName;
  47. }
  48. public ClientFullyQualifiedDomainNameOption(Stream s)
  49. : base(DhcpOptionCode.ClientFullyQualifiedDomainName, s)
  50. { }
  51. #endregion
  52. #region protected
  53. protected override void ParseOptionValue(Stream s)
  54. {
  55. if (s.Length < 3)
  56. throw new InvalidDataException();
  57. int flags = s.ReadByte();
  58. if (flags < 0)
  59. throw new EndOfStreamException();
  60. _flags = (ClientFullyQualifiedDomainNameFlags)flags;
  61. int rcode;
  62. rcode = s.ReadByte();
  63. if (rcode < 0)
  64. throw new EndOfStreamException();
  65. _rcode1 = (byte)rcode;
  66. rcode = s.ReadByte();
  67. if (rcode < 0)
  68. throw new EndOfStreamException();
  69. _rcode2 = (byte)rcode;
  70. if (_flags.HasFlag(ClientFullyQualifiedDomainNameFlags.EncodeUsingCanonicalWireFormat))
  71. _domainName = DnsDatagram.DeserializeDomainName(s, 0, true);
  72. else
  73. _domainName = Encoding.ASCII.GetString(s.ReadExactly((int)s.Length - 3));
  74. }
  75. protected override void WriteOptionValue(Stream s)
  76. {
  77. s.WriteByte((byte)_flags);
  78. s.WriteByte(_rcode1);
  79. s.WriteByte(_rcode2);
  80. if (_flags.HasFlag(ClientFullyQualifiedDomainNameFlags.EncodeUsingCanonicalWireFormat))
  81. DnsDatagram.SerializeDomainName(_domainName, s);
  82. else
  83. s.Write(Encoding.ASCII.GetBytes(_domainName));
  84. }
  85. #endregion
  86. #region properties
  87. public ClientFullyQualifiedDomainNameFlags Flags
  88. { get { return _flags; } }
  89. public byte RCODE1
  90. { get { return _rcode1; } }
  91. public byte RCODE2
  92. { get { return _rcode2; } }
  93. public string DomainName
  94. { get { return _domainName; } }
  95. #endregion
  96. }
  97. }