ClientFullyQualifiedDomainNameOption.cs 3.5 KB

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