DnsResourceRecordExtensions.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.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.GetAuthRecordInfo().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.GetAuthRecordInfo().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.GetAuthRecordInfo().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.GetAuthRecordInfo().GlueRecords = updatedGlueRecords;
  100. }
  101. public static AuthRecordInfo GetAuthRecordInfo(this DnsResourceRecord record)
  102. {
  103. if (record.Tag is not AuthRecordInfo rrInfo)
  104. {
  105. rrInfo = new AuthRecordInfo();
  106. record.Tag = rrInfo;
  107. }
  108. return rrInfo;
  109. }
  110. public static CacheRecordInfo GetCacheRecordInfo(this DnsResourceRecord record)
  111. {
  112. if (record.Tag is not CacheRecordInfo rrInfo)
  113. {
  114. rrInfo = new CacheRecordInfo();
  115. record.Tag = rrInfo;
  116. }
  117. return rrInfo;
  118. }
  119. public static void CopyRecordInfoFrom(this DnsResourceRecord record, DnsResourceRecord otherRecord)
  120. {
  121. record.Tag = otherRecord.Tag;
  122. }
  123. }
  124. }