Lease.cs 6.4 KB

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