Zone.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 System;
  16. using System.Collections.Concurrent;
  17. using System.Collections.Generic;
  18. using System.Net;
  19. using System.Net.Sockets;
  20. using TechnitiumLibrary.Net;
  21. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  22. namespace DnsServerCore.Dns.Zones
  23. {
  24. abstract class Zone
  25. {
  26. #region variables
  27. protected readonly string _name;
  28. protected readonly ConcurrentDictionary<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>> _entries;
  29. #endregion
  30. #region constructor
  31. protected Zone(string name)
  32. {
  33. _name = name.ToLower();
  34. _entries = new ConcurrentDictionary<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>>(1, 5);
  35. }
  36. protected Zone(string name, int capacity)
  37. {
  38. _name = name.ToLower();
  39. _entries = new ConcurrentDictionary<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>>(1, capacity);
  40. }
  41. protected Zone(string name, ConcurrentDictionary<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>> entries)
  42. {
  43. _name = name.ToLower();
  44. _entries = entries;
  45. }
  46. #endregion
  47. #region static
  48. public static string GetReverseZone(IPAddress address, IPAddress subnetMask)
  49. {
  50. return GetReverseZone(address, subnetMask.GetSubnetMaskWidth());
  51. }
  52. public static string GetReverseZone(IPAddress address, int subnetMaskWidth)
  53. {
  54. int addressByteCount = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(subnetMaskWidth) / 8));
  55. byte[] addressBytes = address.GetAddressBytes();
  56. string reverseZone = "";
  57. switch (address.AddressFamily)
  58. {
  59. case AddressFamily.InterNetwork:
  60. for (int i = 0; i < addressByteCount; i++)
  61. reverseZone = addressBytes[i] + "." + reverseZone;
  62. reverseZone += "in-addr.arpa";
  63. break;
  64. case AddressFamily.InterNetworkV6:
  65. for (int i = 0; i < addressByteCount; i++)
  66. reverseZone = (addressBytes[i] & 0x0F).ToString("X") + "." + (addressBytes[i] >> 4).ToString("X") + "." + reverseZone;
  67. reverseZone += "ip6.arpa";
  68. break;
  69. default:
  70. throw new NotSupportedException("AddressFamily not supported.");
  71. }
  72. return reverseZone;
  73. }
  74. #endregion
  75. #region public
  76. public virtual void ListAllRecords(List<DnsResourceRecord> records)
  77. {
  78. foreach (KeyValuePair<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>> entry in _entries)
  79. records.AddRange(entry.Value);
  80. }
  81. public abstract bool ContainsNameServerRecords();
  82. #endregion
  83. #region properties
  84. public string Name
  85. { get { return _name; } }
  86. public virtual bool IsEmpty
  87. { get { return _entries.IsEmpty; } }
  88. #endregion
  89. }
  90. }