DnsResourceRecordExtensions.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Net;
  19. using System.Net.Sockets;
  20. using TechnitiumLibrary;
  21. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  22. namespace DnsServerCore.Dns.ResourceRecords
  23. {
  24. static class DnsResourceRecordExtensions
  25. {
  26. public static void SetGlueRecords(this DnsResourceRecord record, string glueAddresses)
  27. {
  28. if (record.RDATA is not DnsNSRecordData nsRecord)
  29. throw new InvalidOperationException();
  30. string domain = nsRecord.NameServer;
  31. IReadOnlyList<IPAddress> glueAddressesList = glueAddresses.Split(IPAddress.Parse, ',');
  32. DnsResourceRecord[] glueRecords = new DnsResourceRecord[glueAddressesList.Count];
  33. for (int i = 0; i < glueRecords.Length; i++)
  34. {
  35. switch (glueAddressesList[i].AddressFamily)
  36. {
  37. case AddressFamily.InterNetwork:
  38. glueRecords[i] = new DnsResourceRecord(domain, DnsResourceRecordType.A, DnsClass.IN, record.TTL, new DnsARecordData(glueAddressesList[i]));
  39. break;
  40. case AddressFamily.InterNetworkV6:
  41. glueRecords[i] = new DnsResourceRecord(domain, DnsResourceRecordType.AAAA, DnsClass.IN, record.TTL, new DnsAAAARecordData(glueAddressesList[i]));
  42. break;
  43. }
  44. }
  45. record.GetAuthNSRecordInfo().GlueRecords = glueRecords;
  46. }
  47. public static void SyncGlueRecords(this DnsResourceRecord record, IReadOnlyList<DnsResourceRecord> allGlueRecords)
  48. {
  49. if (record.RDATA is not DnsNSRecordData nsRecord)
  50. throw new InvalidOperationException();
  51. string domain = nsRecord.NameServer;
  52. List<DnsResourceRecord> foundGlueRecords = new List<DnsResourceRecord>(2);
  53. foreach (DnsResourceRecord glueRecord in allGlueRecords)
  54. {
  55. switch (glueRecord.Type)
  56. {
  57. case DnsResourceRecordType.A:
  58. case DnsResourceRecordType.AAAA:
  59. if (glueRecord.Name.Equals(domain, StringComparison.OrdinalIgnoreCase))
  60. foundGlueRecords.Add(glueRecord);
  61. break;
  62. }
  63. }
  64. record.GetAuthNSRecordInfo().GlueRecords = foundGlueRecords;
  65. }
  66. public static void SyncGlueRecords(this DnsResourceRecord record, IReadOnlyCollection<DnsResourceRecord> deletedGlueRecords, IReadOnlyCollection<DnsResourceRecord> addedGlueRecords)
  67. {
  68. if (record.RDATA is not DnsNSRecordData nsRecord)
  69. throw new InvalidOperationException();
  70. bool updated = false;
  71. List<DnsResourceRecord> updatedGlueRecords = new List<DnsResourceRecord>();
  72. IReadOnlyList<DnsResourceRecord> existingGlueRecords = record.GetAuthNSRecordInfo().GlueRecords;
  73. if (existingGlueRecords is not null)
  74. {
  75. foreach (DnsResourceRecord existingGlueRecord in existingGlueRecords)
  76. {
  77. if (deletedGlueRecords.Contains(existingGlueRecord))
  78. updated = true; //skipped to delete existing glue record
  79. else
  80. updatedGlueRecords.Add(existingGlueRecord);
  81. }
  82. }
  83. string domain = nsRecord.NameServer;
  84. foreach (DnsResourceRecord addedGlueRecord in addedGlueRecords)
  85. {
  86. switch (addedGlueRecord.Type)
  87. {
  88. case DnsResourceRecordType.A:
  89. case DnsResourceRecordType.AAAA:
  90. if (addedGlueRecord.Name.Equals(domain, StringComparison.OrdinalIgnoreCase))
  91. {
  92. updatedGlueRecords.Add(addedGlueRecord);
  93. updated = true;
  94. }
  95. break;
  96. }
  97. }
  98. if (updated)
  99. record.GetAuthNSRecordInfo().GlueRecords = updatedGlueRecords;
  100. }
  101. public static GenericRecordInfo GetAuthGenericRecordInfo(this DnsResourceRecord record)
  102. {
  103. if (record.Tag is null)
  104. {
  105. GenericRecordInfo rrInfo;
  106. switch (record.Type)
  107. {
  108. case DnsResourceRecordType.NS:
  109. rrInfo = new NSRecordInfo();
  110. break;
  111. case DnsResourceRecordType.SOA:
  112. rrInfo = new SOARecordInfo();
  113. break;
  114. case DnsResourceRecordType.SVCB:
  115. case DnsResourceRecordType.HTTPS:
  116. rrInfo = new SVCBRecordInfo();
  117. break;
  118. default:
  119. rrInfo = new GenericRecordInfo();
  120. break;
  121. }
  122. record.Tag = rrInfo;
  123. return rrInfo;
  124. }
  125. else if (record.Tag is GenericRecordInfo rrInfo)
  126. {
  127. return rrInfo;
  128. }
  129. else
  130. {
  131. throw new InvalidOperationException();
  132. }
  133. }
  134. public static NSRecordInfo GetAuthNSRecordInfo(this DnsResourceRecord record)
  135. {
  136. if (record.Tag is null)
  137. {
  138. NSRecordInfo info = new NSRecordInfo();
  139. record.Tag = info;
  140. return info;
  141. }
  142. else if (record.Tag is NSRecordInfo nsInfo)
  143. {
  144. return nsInfo;
  145. }
  146. else
  147. {
  148. throw new InvalidOperationException();
  149. }
  150. }
  151. public static SOARecordInfo GetAuthSOARecordInfo(this DnsResourceRecord record)
  152. {
  153. if (record.Tag is null)
  154. {
  155. SOARecordInfo info = new SOARecordInfo();
  156. record.Tag = info;
  157. return info;
  158. }
  159. else if (record.Tag is SOARecordInfo soaInfo)
  160. {
  161. return soaInfo;
  162. }
  163. else
  164. {
  165. throw new InvalidOperationException();
  166. }
  167. }
  168. public static SVCBRecordInfo GetAuthSVCBRecordInfo(this DnsResourceRecord record)
  169. {
  170. if (record.Tag is null)
  171. {
  172. SVCBRecordInfo info = new SVCBRecordInfo();
  173. record.Tag = info;
  174. return info;
  175. }
  176. else if (record.Tag is SVCBRecordInfo svcbInfo)
  177. {
  178. return svcbInfo;
  179. }
  180. else
  181. {
  182. throw new InvalidOperationException();
  183. }
  184. }
  185. public static HistoryRecordInfo GetAuthHistoryRecordInfo(this DnsResourceRecord record)
  186. {
  187. if (record.Tag is null)
  188. {
  189. HistoryRecordInfo info = new HistoryRecordInfo();
  190. record.Tag = info;
  191. return info;
  192. }
  193. else if (record.Tag is HistoryRecordInfo info)
  194. {
  195. return info;
  196. }
  197. else
  198. {
  199. throw new InvalidOperationException();
  200. }
  201. }
  202. public static CacheRecordInfo GetCacheRecordInfo(this DnsResourceRecord record)
  203. {
  204. if (record.Tag is not CacheRecordInfo rrInfo)
  205. {
  206. rrInfo = new CacheRecordInfo();
  207. record.Tag = rrInfo;
  208. }
  209. return rrInfo;
  210. }
  211. public static void CopyRecordInfoFrom(this DnsResourceRecord record, DnsResourceRecord otherRecord)
  212. {
  213. record.Tag = otherRecord.Tag;
  214. }
  215. }
  216. }