Lease.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2020 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. readonly 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, byte[] hardwareAddress, IPAddress address, string comments)
  55. : this(type, new ClientIdentifierOption((byte)DhcpMessageHardwareAddressType.Ethernet, hardwareAddress), hostName, hardwareAddress, address, comments, 0)
  56. { }
  57. internal Lease(LeaseType type, string hostName, string hardwareAddress, IPAddress address, string comments)
  58. : this(type, hostName, 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 = IPAddressExtension.Parse(bR);
  75. if (version >= 2)
  76. {
  77. _comments = bR.ReadShortString();
  78. if (string.IsNullOrWhiteSpace(_comments))
  79. _comments = null;
  80. }
  81. _leaseObtained = bR.ReadDate();
  82. _leaseExpires = bR.ReadDate();
  83. break;
  84. default:
  85. throw new InvalidDataException("Lease data format version not supported.");
  86. }
  87. }
  88. #endregion
  89. #region private
  90. private 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 SetHostName(string hostName)
  99. {
  100. _hostName = hostName;
  101. }
  102. #endregion
  103. #region public
  104. public void ExtendLease(uint leaseTime)
  105. {
  106. _leaseExpires = DateTime.UtcNow.AddSeconds(leaseTime);
  107. }
  108. public void WriteTo(BinaryWriter bW)
  109. {
  110. bW.Write((byte)2); //version
  111. bW.Write((byte)_type);
  112. _clientIdentifier.WriteTo(bW.BaseStream);
  113. if (string.IsNullOrWhiteSpace(_hostName))
  114. bW.Write((byte)0);
  115. else
  116. bW.WriteShortString(_hostName);
  117. bW.WriteBuffer(_hardwareAddress);
  118. _address.WriteTo(bW);
  119. if (string.IsNullOrWhiteSpace(_comments))
  120. bW.Write((byte)0);
  121. else
  122. bW.WriteShortString(_comments);
  123. bW.Write(_leaseObtained);
  124. bW.Write(_leaseExpires);
  125. }
  126. public string GetClientFullIdentifier()
  127. {
  128. string hardwareAddress = BitConverter.ToString(_hardwareAddress);
  129. if (string.IsNullOrWhiteSpace(_hostName))
  130. return "[" + hardwareAddress + "]";
  131. return _hostName + " [" + hardwareAddress + "]";
  132. }
  133. public int CompareTo(Lease other)
  134. {
  135. return _address.ConvertIpToNumber().CompareTo(other._address.ConvertIpToNumber());
  136. }
  137. #endregion
  138. #region properties
  139. public LeaseType Type
  140. { get { return _type; } }
  141. internal ClientIdentifierOption ClientIdentifier
  142. { get { return _clientIdentifier; } }
  143. public string HostName
  144. { get { return _hostName; } }
  145. public byte[] HardwareAddress
  146. { get { return _hardwareAddress; } }
  147. public IPAddress Address
  148. { get { return _address; } }
  149. public string Comments
  150. {
  151. get { return _comments; }
  152. set { _comments = value; }
  153. }
  154. public DateTime LeaseObtained
  155. { get { return _leaseObtained; } }
  156. public DateTime LeaseExpires
  157. { get { return _leaseExpires; } }
  158. #endregion
  159. }
  160. }