DnsResourceRecordExtension.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 System;
  16. using System.Collections.Generic;
  17. using System.Net;
  18. using System.Net.Sockets;
  19. using TechnitiumLibrary.Net.Dns;
  20. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  21. namespace DnsServerCore.Dns.ResourceRecords
  22. {
  23. static class DnsResourceRecordExtension
  24. {
  25. public static void SetGlueRecords(this DnsResourceRecord record, IReadOnlyList<DnsResourceRecord> glueRecords)
  26. {
  27. DnsResourceRecordInfo rrInfo = record.Tag as DnsResourceRecordInfo;
  28. if (rrInfo == null)
  29. {
  30. rrInfo = new DnsResourceRecordInfo();
  31. record.Tag = rrInfo;
  32. }
  33. rrInfo.GlueRecords = glueRecords;
  34. }
  35. public static void SetGlueRecords(this DnsResourceRecord record, string glueAddresses)
  36. {
  37. List<IPAddress> addresses = new List<IPAddress>();
  38. foreach (string address in glueAddresses.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
  39. addresses.Add(IPAddress.Parse(address.Trim()));
  40. SetGlueRecords(record, addresses);
  41. }
  42. public static void SetGlueRecords(this DnsResourceRecord record, IReadOnlyList<IPAddress> glueAddresses)
  43. {
  44. string domain;
  45. switch (record.Type)
  46. {
  47. case DnsResourceRecordType.NS:
  48. domain = (record.RDATA as DnsNSRecord).NameServer;
  49. break;
  50. case DnsResourceRecordType.SOA:
  51. domain = (record.RDATA as DnsSOARecord).PrimaryNameServer;
  52. break;
  53. default:
  54. throw new NotSupportedException();
  55. }
  56. DnsResourceRecord[] glueRecords = new DnsResourceRecord[glueAddresses.Count];
  57. for (int i = 0; i < glueRecords.Length; i++)
  58. {
  59. switch (glueAddresses[i].AddressFamily)
  60. {
  61. case AddressFamily.InterNetwork:
  62. glueRecords[i] = new DnsResourceRecord(domain, DnsResourceRecordType.A, DnsClass.IN, record.TtlValue, new DnsARecord(glueAddresses[i]));
  63. break;
  64. case AddressFamily.InterNetworkV6:
  65. glueRecords[i] = new DnsResourceRecord(domain, DnsResourceRecordType.AAAA, DnsClass.IN, record.TtlValue, new DnsAAAARecord(glueAddresses[i]));
  66. break;
  67. }
  68. }
  69. SetGlueRecords(record, glueRecords);
  70. }
  71. public static void SyncGlueRecords(this DnsResourceRecord record, IReadOnlyList<DnsResourceRecord> allGlueRecords)
  72. {
  73. string domain;
  74. switch (record.Type)
  75. {
  76. case DnsResourceRecordType.NS:
  77. domain = (record.RDATA as DnsNSRecord).NameServer;
  78. break;
  79. case DnsResourceRecordType.SOA:
  80. domain = (record.RDATA as DnsSOARecord).PrimaryNameServer;
  81. break;
  82. default:
  83. throw new NotSupportedException();
  84. }
  85. List<DnsResourceRecord> foundGlueRecords = new List<DnsResourceRecord>(2);
  86. foreach (DnsResourceRecord glueRecord in allGlueRecords)
  87. {
  88. switch (glueRecord.Type)
  89. {
  90. case DnsResourceRecordType.A:
  91. case DnsResourceRecordType.AAAA:
  92. if (glueRecord.Name.Equals(domain, StringComparison.OrdinalIgnoreCase))
  93. foundGlueRecords.Add(glueRecord);
  94. break;
  95. }
  96. }
  97. if (foundGlueRecords.Count > 0)
  98. SetGlueRecords(record, foundGlueRecords);
  99. }
  100. public static IReadOnlyList<DnsResourceRecord> GetGlueRecords(this DnsResourceRecord record)
  101. {
  102. DnsResourceRecordInfo rrInfo = record.Tag as DnsResourceRecordInfo;
  103. if (rrInfo == null)
  104. return Array.Empty<DnsResourceRecord>();
  105. IReadOnlyList<DnsResourceRecord> glueRecords = rrInfo.GlueRecords;
  106. if (glueRecords == null)
  107. return Array.Empty<DnsResourceRecord>();
  108. return glueRecords;
  109. }
  110. public static IReadOnlyList<DnsResourceRecord> GetGlueRecords(this IReadOnlyList<DnsResourceRecord> records)
  111. {
  112. if (records.Count == 1)
  113. return GetGlueRecords(records[0]);
  114. List<DnsResourceRecord> glueRecords = new List<DnsResourceRecord>(records.Count * 2);
  115. foreach (DnsResourceRecord nsRecord in records)
  116. glueRecords.AddRange(GetGlueRecords(nsRecord));
  117. return glueRecords;
  118. }
  119. public static bool IsDisabled(this DnsResourceRecord record)
  120. {
  121. DnsResourceRecordInfo rrInfo = record.Tag as DnsResourceRecordInfo;
  122. if (rrInfo == null)
  123. return false;
  124. return rrInfo.Disabled;
  125. }
  126. public static void Disable(this DnsResourceRecord record)
  127. {
  128. DnsResourceRecordInfo rrInfo = record.Tag as DnsResourceRecordInfo;
  129. if (rrInfo == null)
  130. {
  131. rrInfo = new DnsResourceRecordInfo();
  132. record.Tag = rrInfo;
  133. }
  134. rrInfo.Disabled = true;
  135. }
  136. public static void Enable(this DnsResourceRecord record)
  137. {
  138. DnsResourceRecordInfo rrInfo = record.Tag as DnsResourceRecordInfo;
  139. if (rrInfo == null)
  140. return;
  141. rrInfo.Disabled = false;
  142. }
  143. }
  144. }