PrimaryZone.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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.Threading;
  18. using System.Threading.Tasks;
  19. using TechnitiumLibrary.Net.Dns;
  20. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  21. namespace DnsServerCore.Dns.Zones
  22. {
  23. class PrimaryZone : AuthZone
  24. {
  25. #region variables
  26. readonly DnsServer _dnsServer;
  27. readonly bool _internal;
  28. readonly Timer _notifyTimer;
  29. const int NOTIFY_TIMER_INTERVAL = 10000;
  30. readonly List<NameServerAddress> _notifyList = new List<NameServerAddress>();
  31. const int NOTIFY_TIMEOUT = 10000;
  32. const int NOTIFY_RETRIES = 5;
  33. #endregion
  34. #region constructor
  35. public PrimaryZone(DnsServer dnsServer, AuthZoneInfo zoneInfo)
  36. : base(zoneInfo.Name)
  37. {
  38. _dnsServer = dnsServer;
  39. _disabled = zoneInfo.Disabled;
  40. _notifyTimer = new Timer(NotifyTimerCallback, null, Timeout.Infinite, Timeout.Infinite);
  41. }
  42. public PrimaryZone(DnsServer dnsServer, string name, string primaryNameServer, bool @internal)
  43. : base(name)
  44. {
  45. _dnsServer = dnsServer;
  46. _internal = @internal;
  47. DnsSOARecord soa = new DnsSOARecord(primaryNameServer, "hostadmin." + name, 1, 14400, 3600, 604800, 900);
  48. _entries[DnsResourceRecordType.SOA] = new DnsResourceRecord[] { new DnsResourceRecord(_name, DnsResourceRecordType.SOA, DnsClass.IN, soa.Refresh, soa) };
  49. _entries[DnsResourceRecordType.NS] = new DnsResourceRecord[] { new DnsResourceRecord(_name, DnsResourceRecordType.NS, DnsClass.IN, soa.Refresh, new DnsNSRecord(soa.PrimaryNameServer)) };
  50. _notifyTimer = new Timer(NotifyTimerCallback, null, Timeout.Infinite, Timeout.Infinite);
  51. }
  52. internal PrimaryZone(DnsServer dnsServer, string name, DnsSOARecord soa, DnsNSRecord ns)
  53. : base(name)
  54. {
  55. _dnsServer = dnsServer;
  56. _internal = true;
  57. _entries[DnsResourceRecordType.SOA] = new DnsResourceRecord[] { new DnsResourceRecord(_name, DnsResourceRecordType.SOA, DnsClass.IN, soa.Refresh, soa) };
  58. _entries[DnsResourceRecordType.NS] = new DnsResourceRecord[] { new DnsResourceRecord(_name, DnsResourceRecordType.NS, DnsClass.IN, soa.Refresh, ns) };
  59. _notifyTimer = new Timer(NotifyTimerCallback, null, Timeout.Infinite, Timeout.Infinite);
  60. }
  61. #endregion
  62. #region IDisposable
  63. bool _disposed;
  64. protected override void Dispose(bool disposing)
  65. {
  66. if (_disposed)
  67. return;
  68. if (disposing)
  69. {
  70. if (_notifyTimer != null)
  71. _notifyTimer.Dispose();
  72. }
  73. _disposed = true;
  74. }
  75. #endregion
  76. #region private
  77. private async void NotifyTimerCallback(object state)
  78. {
  79. try
  80. {
  81. IReadOnlyList<NameServerAddress> secondaryNameServers = await GetSecondaryNameServerAddressesAsync(_dnsServer);
  82. foreach (NameServerAddress secondaryNameServer in secondaryNameServers)
  83. NotifyNameServer(secondaryNameServer);
  84. }
  85. catch (Exception ex)
  86. {
  87. LogManager log = _dnsServer.LogManager;
  88. if (log != null)
  89. log.Write(ex);
  90. }
  91. }
  92. private void NotifyNameServer(NameServerAddress nameServer)
  93. {
  94. //use notify list to prevent multiple threads from notifying the same name server
  95. lock (_notifyList)
  96. {
  97. if (_notifyList.Contains(nameServer))
  98. return; //already notifying the name server in another thread
  99. _notifyList.Add(nameServer);
  100. }
  101. _ = NotifyNameServerAsync(nameServer);
  102. }
  103. private async Task NotifyNameServerAsync(NameServerAddress nameServer)
  104. {
  105. try
  106. {
  107. DnsClient client = new DnsClient(nameServer);
  108. client.Timeout = NOTIFY_TIMEOUT;
  109. client.Retries = NOTIFY_RETRIES;
  110. DnsDatagram notifyRequest = new DnsDatagram(0, false, DnsOpcode.Notify, true, false, false, false, false, false, DnsResponseCode.NoError, new DnsQuestionRecord[] { new DnsQuestionRecord(_name, DnsResourceRecordType.SOA, DnsClass.IN) }, _entries[DnsResourceRecordType.SOA]);
  111. DnsDatagram response = await client.ResolveAsync(notifyRequest);
  112. switch (response.RCODE)
  113. {
  114. case DnsResponseCode.NoError:
  115. case DnsResponseCode.NotImplemented:
  116. {
  117. //transaction complete
  118. LogManager log = _dnsServer.LogManager;
  119. if (log != null)
  120. log.Write("DNS Server successfully notified name server for '" + _name + "' zone changes: " + nameServer.ToString());
  121. }
  122. break;
  123. default:
  124. {
  125. //transaction failed
  126. LogManager log = _dnsServer.LogManager;
  127. if (log != null)
  128. log.Write("DNS Server received RCODE=" + response.RCODE.ToString() + " from name server for '" + _name + "' zone notification: " + nameServer.ToString());
  129. }
  130. break;
  131. }
  132. }
  133. catch (Exception ex)
  134. {
  135. LogManager log = _dnsServer.LogManager;
  136. if (log != null)
  137. {
  138. log.Write("DNS Server failed to notify name server for '" + _name + "' zone changes: " + nameServer.ToString());
  139. log.Write(ex);
  140. }
  141. }
  142. finally
  143. {
  144. lock (_notifyList)
  145. {
  146. _notifyList.Remove(nameServer);
  147. }
  148. }
  149. }
  150. #endregion
  151. #region public
  152. public void IncrementSoaSerial()
  153. {
  154. DnsResourceRecord record = _entries[DnsResourceRecordType.SOA][0];
  155. DnsSOARecord soa = record.RDATA as DnsSOARecord;
  156. uint serial = soa.Serial;
  157. if (serial < uint.MaxValue)
  158. serial++;
  159. else
  160. serial = 0;
  161. DnsResourceRecord newRecord = new DnsResourceRecord(record.Name, record.Type, record.Class, record.TtlValue, new DnsSOARecord(soa.PrimaryNameServer, soa.ResponsiblePerson, serial, soa.Refresh, soa.Retry, soa.Expire, soa.Minimum)) { Tag = record.Tag };
  162. _entries[DnsResourceRecordType.SOA] = new DnsResourceRecord[] { newRecord };
  163. }
  164. public void NotifyNameServers()
  165. {
  166. _notifyTimer.Change(NOTIFY_TIMER_INTERVAL, Timeout.Infinite);
  167. }
  168. public override void SetRecords(DnsResourceRecordType type, IReadOnlyList<DnsResourceRecord> records)
  169. {
  170. switch (type)
  171. {
  172. case DnsResourceRecordType.CNAME:
  173. throw new InvalidOperationException("Cannot set CNAME record to zone root.");
  174. case DnsResourceRecordType.SOA:
  175. if ((records.Count != 1) || !records[0].Name.Equals(_name, StringComparison.OrdinalIgnoreCase))
  176. throw new InvalidOperationException("Invalid SOA record.");
  177. //remove any resource record info
  178. records[0].Tag = null;
  179. break;
  180. }
  181. base.SetRecords(type, records);
  182. IncrementSoaSerial();
  183. NotifyNameServers();
  184. }
  185. public override void AddRecord(DnsResourceRecord record)
  186. {
  187. base.AddRecord(record);
  188. IncrementSoaSerial();
  189. NotifyNameServers();
  190. }
  191. public override bool DeleteRecords(DnsResourceRecordType type)
  192. {
  193. if (type == DnsResourceRecordType.SOA)
  194. throw new InvalidOperationException("Cannot delete SOA record.");
  195. if (base.DeleteRecords(type))
  196. {
  197. IncrementSoaSerial();
  198. NotifyNameServers();
  199. return true;
  200. }
  201. return false;
  202. }
  203. public override bool DeleteRecord(DnsResourceRecordType type, DnsResourceRecordData record)
  204. {
  205. if (type == DnsResourceRecordType.SOA)
  206. throw new InvalidOperationException("Cannot delete SOA record.");
  207. if (base.DeleteRecord(type, record))
  208. {
  209. IncrementSoaSerial();
  210. NotifyNameServers();
  211. return true;
  212. }
  213. return false;
  214. }
  215. #endregion
  216. #region properties
  217. public bool Internal
  218. { get { return _internal; } }
  219. public override bool Disabled
  220. {
  221. get { return _disabled; }
  222. set
  223. {
  224. if (_disabled != value)
  225. {
  226. _disabled = value;
  227. if (_disabled)
  228. _notifyTimer.Change(Timeout.Infinite, Timeout.Infinite);
  229. else
  230. NotifyNameServers();
  231. }
  232. }
  233. }
  234. #endregion
  235. }
  236. }