Lease.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 DnsServerCore.Dhcp.Options;
  16. using System;
  17. using System.Globalization;
  18. using System.IO;
  19. using System.Net;
  20. using TechnitiumLibrary.IO;
  21. using TechnitiumLibrary.Net;
  22. namespace DnsServerCore.Dhcp
  23. {
  24. public enum LeaseType : byte
  25. {
  26. None = 0,
  27. Dynamic = 1,
  28. Reserved = 2
  29. }
  30. public class Lease : IComparable<Lease>
  31. {
  32. #region variables
  33. static readonly char[] _hyphenColonSeparator = new char[] { '-', ':' };
  34. LeaseType _type;
  35. readonly ClientIdentifierOption _clientIdentifier;
  36. string _hostName;
  37. readonly byte[] _hardwareAddress;
  38. readonly IPAddress _address;
  39. string _comments;
  40. readonly DateTime _leaseObtained;
  41. DateTime _leaseExpires;
  42. #endregion
  43. #region constructor
  44. internal Lease(LeaseType type, ClientIdentifierOption clientIdentifier, string hostName, byte[] hardwareAddress, IPAddress address, string comments, uint leaseTime)
  45. {
  46. _type = type;
  47. _clientIdentifier = clientIdentifier;
  48. _hostName = hostName;
  49. _hardwareAddress = hardwareAddress;
  50. _address = address;
  51. _comments = comments;
  52. _leaseObtained = DateTime.UtcNow;
  53. ExtendLease(leaseTime);
  54. }
  55. internal Lease(LeaseType type, string hostName, DhcpMessageHardwareAddressType hardwareAddressType, byte[] hardwareAddress, IPAddress address, string comments)
  56. : this(type, new ClientIdentifierOption((byte)hardwareAddressType, hardwareAddress), hostName, hardwareAddress, address, comments, 0)
  57. { }
  58. internal Lease(LeaseType type, string hostName, DhcpMessageHardwareAddressType hardwareAddressType, string hardwareAddress, IPAddress address, string comments)
  59. : this(type, hostName, hardwareAddressType, ParseHardwareAddress(hardwareAddress), address, comments)
  60. { }
  61. internal Lease(BinaryReader bR)
  62. {
  63. byte version = bR.ReadByte();
  64. switch (version)
  65. {
  66. case 1:
  67. case 2:
  68. _type = (LeaseType)bR.ReadByte();
  69. _clientIdentifier = DhcpOption.Parse(bR.BaseStream) as ClientIdentifierOption;
  70. _clientIdentifier.ParseOptionValue();
  71. _hostName = bR.ReadShortString();
  72. if (string.IsNullOrWhiteSpace(_hostName))
  73. _hostName = null;
  74. _hardwareAddress = bR.ReadBuffer();
  75. _address = IPAddressExtensions.ReadFrom(bR);
  76. if (version >= 2)
  77. {
  78. _comments = bR.ReadShortString();
  79. if (string.IsNullOrWhiteSpace(_comments))
  80. _comments = null;
  81. }
  82. _leaseObtained = bR.ReadDateTime();
  83. _leaseExpires = bR.ReadDateTime();
  84. break;
  85. default:
  86. throw new InvalidDataException("Lease data format version not supported.");
  87. }
  88. }
  89. #endregion
  90. #region internal
  91. internal static byte[] ParseHardwareAddress(string hardwareAddress)
  92. {
  93. string[] parts = hardwareAddress.Split(_hyphenColonSeparator);
  94. byte[] address = new byte[parts.Length];
  95. for (int i = 0; i < parts.Length; i++)
  96. address[i] = byte.Parse(parts[i], NumberStyles.HexNumber, CultureInfo.InvariantCulture);
  97. return address;
  98. }
  99. internal void ConvertToReserved()
  100. {
  101. _type = LeaseType.Reserved;
  102. }
  103. internal void ConvertToDynamic()
  104. {
  105. _type = LeaseType.Dynamic;
  106. }
  107. internal void SetHostName(string hostName)
  108. {
  109. _hostName = hostName;
  110. }
  111. #endregion
  112. #region public
  113. public void ExtendLease(uint leaseTime)
  114. {
  115. _leaseExpires = DateTime.UtcNow.AddSeconds(leaseTime);
  116. }
  117. public void WriteTo(BinaryWriter bW)
  118. {
  119. bW.Write((byte)2); //version
  120. bW.Write((byte)_type);
  121. _clientIdentifier.WriteTo(bW.BaseStream);
  122. if (string.IsNullOrWhiteSpace(_hostName))
  123. bW.Write((byte)0);
  124. else
  125. bW.WriteShortString(_hostName);
  126. bW.WriteBuffer(_hardwareAddress);
  127. _address.WriteTo(bW);
  128. if (string.IsNullOrWhiteSpace(_comments))
  129. bW.Write((byte)0);
  130. else
  131. bW.WriteShortString(_comments);
  132. bW.Write(_leaseObtained);
  133. bW.Write(_leaseExpires);
  134. }
  135. public string GetClientInfo()
  136. {
  137. string hardwareAddress = BitConverter.ToString(_hardwareAddress);
  138. if (string.IsNullOrWhiteSpace(_hostName))
  139. return "[" + hardwareAddress + "]";
  140. return _hostName + " [" + hardwareAddress + "]";
  141. }
  142. public int CompareTo(Lease other)
  143. {
  144. return _address.ConvertIpToNumber().CompareTo(other._address.ConvertIpToNumber());
  145. }
  146. #endregion
  147. #region properties
  148. public LeaseType Type
  149. { get { return _type; } }
  150. internal ClientIdentifierOption ClientIdentifier
  151. { get { return _clientIdentifier; } }
  152. public string HostName
  153. { get { return _hostName; } }
  154. public byte[] HardwareAddress
  155. { get { return _hardwareAddress; } }
  156. public IPAddress Address
  157. { get { return _address; } }
  158. public string Comments
  159. {
  160. get { return _comments; }
  161. set { _comments = value; }
  162. }
  163. public DateTime LeaseObtained
  164. { get { return _leaseObtained; } }
  165. public DateTime LeaseExpires
  166. { get { return _leaseExpires; } }
  167. #endregion
  168. }
  169. }