GenericRecordInfo.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.Dns.ResourceRecords
  19. {
  20. class GenericRecordInfo : AuthRecordInfo
  21. {
  22. #region variables
  23. bool _disabled;
  24. string _comments;
  25. DateTime _lastModified;
  26. uint _expiryTtl;
  27. DateTime _lastUsedOn; //not serialized
  28. #endregion
  29. #region constructor
  30. public GenericRecordInfo()
  31. { }
  32. public GenericRecordInfo(BinaryReader bR)
  33. : base(bR)
  34. { }
  35. #endregion
  36. #region protected
  37. protected sealed override void ReadRecordInfoFrom(BinaryReader bR)
  38. {
  39. byte version = bR.ReadByte();
  40. switch (version)
  41. {
  42. case 1:
  43. _disabled = bR.ReadBoolean();
  44. _comments = bR.ReadShortString();
  45. ReadExtendedRecordInfoFrom(bR);
  46. break;
  47. case 2:
  48. _disabled = bR.ReadBoolean();
  49. _comments = bR.ReadShortString();
  50. _lastModified = bR.ReadDateTime();
  51. _expiryTtl = bR.ReadUInt32();
  52. ReadExtendedRecordInfoFrom(bR);
  53. break;
  54. default:
  55. throw new InvalidDataException("GenericRecordInfo format version not supported.");
  56. }
  57. }
  58. protected sealed override void WriteRecordInfoTo(BinaryWriter bW)
  59. {
  60. bW.Write((byte)2); //version
  61. bW.Write(_disabled);
  62. if (string.IsNullOrEmpty(_comments))
  63. bW.Write((byte)0);
  64. else
  65. bW.WriteShortString(_comments);
  66. bW.Write(_lastModified);
  67. bW.Write(_expiryTtl);
  68. WriteExtendedRecordInfoTo(bW);
  69. }
  70. protected virtual void ReadExtendedRecordInfoFrom(BinaryReader bR)
  71. {
  72. _ = bR.ReadByte(); //read byte to move ahead
  73. }
  74. protected virtual void WriteExtendedRecordInfoTo(BinaryWriter bW)
  75. {
  76. bW.Write((byte)0); //no extended info
  77. }
  78. #endregion
  79. #region public
  80. public uint GetPendingExpiryTtl()
  81. {
  82. uint elapsedSeconds = Convert.ToUInt32((DateTime.UtcNow - _lastModified).TotalSeconds);
  83. if (elapsedSeconds < _expiryTtl)
  84. return _expiryTtl - elapsedSeconds;
  85. return 0u;
  86. }
  87. #endregion
  88. #region properties
  89. public virtual bool Disabled
  90. {
  91. get { return _disabled; }
  92. set { _disabled = value; }
  93. }
  94. public string Comments
  95. {
  96. get { return _comments; }
  97. set
  98. {
  99. if ((value is not null) && (value.Length > 255))
  100. throw new ArgumentOutOfRangeException(nameof(Comments), "Resource record comment text cannot exceed 255 characters.");
  101. _comments = value;
  102. }
  103. }
  104. public DateTime LastModified
  105. {
  106. get { return _lastModified; }
  107. set { _lastModified = value; }
  108. }
  109. public virtual uint ExpiryTtl
  110. {
  111. get { return _expiryTtl; }
  112. set { _expiryTtl = value; }
  113. }
  114. public DateTime LastUsedOn
  115. {
  116. get { return _lastUsedOn; }
  117. set { _lastUsedOn = value; }
  118. }
  119. #endregion
  120. }
  121. }