DnsProvider.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2019 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.IO;
  18. using System.Net;
  19. using System.Net.Sockets;
  20. using TechnitiumLibrary.IO;
  21. using TechnitiumLibrary.Net;
  22. namespace DnsServerSystemTrayApp
  23. {
  24. public class DnsProvider : IComparable<DnsProvider>
  25. {
  26. #region variables
  27. public string Name;
  28. public ICollection<IPAddress> Addresses;
  29. #endregion
  30. #region constructor
  31. public DnsProvider(string name, ICollection<IPAddress> addresses)
  32. {
  33. this.Name = name;
  34. this.Addresses = addresses;
  35. }
  36. public DnsProvider(BinaryReader bR)
  37. {
  38. this.Name = bR.ReadShortString();
  39. this.Addresses = new List<IPAddress>();
  40. int count = bR.ReadInt32();
  41. for (int i = 0; i < count; i++)
  42. this.Addresses.Add(IPAddressExtension.Parse(bR));
  43. }
  44. #endregion
  45. #region static
  46. public static DnsProvider[] GetDefaultProviders()
  47. {
  48. return new DnsProvider[] {
  49. new DnsProvider("Technitium", new IPAddress[] { IPAddress.Loopback, IPAddress.IPv6Loopback }),
  50. new DnsProvider("Cloudflare", new IPAddress[] { IPAddress.Parse("1.1.1.1"), IPAddress.Parse("1.0.0.1"), IPAddress.Parse("[2606:4700:4700::1111]"), IPAddress.Parse("[2606:4700:4700::1001]") }),
  51. new DnsProvider("Google", new IPAddress[] { IPAddress.Parse("8.8.8.8"), IPAddress.Parse("8.8.4.4"), IPAddress.Parse("[2001:4860:4860::8888]"), IPAddress.Parse("[2001:4860:4860::8844]") }),
  52. new DnsProvider("Quad9", new IPAddress[] { IPAddress.Parse("9.9.9.9"), IPAddress.Parse("[2620:fe::fe]") }),
  53. new DnsProvider("OpenDNS", new IPAddress[] { IPAddress.Parse("208.67.222.222"), IPAddress.Parse("208.67.220.220"), IPAddress.Parse("[2620:0:ccc::2]"), IPAddress.Parse("[2620:0:ccd::2]") })
  54. };
  55. }
  56. #endregion
  57. #region public
  58. public string GetIpv4Addresses()
  59. {
  60. string ipv4Addresses = null;
  61. foreach (IPAddress address in Addresses)
  62. {
  63. if (address.AddressFamily == AddressFamily.InterNetwork)
  64. {
  65. if (ipv4Addresses == null)
  66. ipv4Addresses = address.ToString();
  67. else
  68. ipv4Addresses += ", " + address.ToString();
  69. }
  70. }
  71. return ipv4Addresses;
  72. }
  73. public string GetIpv6Addresses()
  74. {
  75. string ipv6Addresses = null;
  76. foreach (IPAddress address in Addresses)
  77. {
  78. if (address.AddressFamily == AddressFamily.InterNetworkV6)
  79. {
  80. if (ipv6Addresses == null)
  81. ipv6Addresses = address.ToString();
  82. else
  83. ipv6Addresses += ", " + address.ToString();
  84. }
  85. }
  86. return ipv6Addresses;
  87. }
  88. public override string ToString()
  89. {
  90. return Name;
  91. }
  92. public int CompareTo(DnsProvider other)
  93. {
  94. return this.Name.CompareTo(other.Name);
  95. }
  96. public void WriteTo(BinaryWriter bW)
  97. {
  98. bW.WriteShortString(Name);
  99. bW.Write(Addresses.Count);
  100. foreach (IPAddress address in Addresses)
  101. address.WriteTo(bW);
  102. }
  103. #endregion
  104. }
  105. }