PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/GKNetCore/NetHelper.cs

https://bitbucket.org/Serg-Norseman/gkcommunicator
C# | 93 lines | 54 code | 11 blank | 28 comment | 8 complexity | 320b596940321d701b69fa6387bd8920 MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. * "GKCommunicator", the chat and bulletin board of the genealogical network.
  3. * Copyright (C) 2018 by Sergey V. Zhdanovskih.
  4. *
  5. * This file is part of "GEDKeeper".
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. using System.Net;
  21. using System.Net.NetworkInformation;
  22. using System.Net.Sockets;
  23. namespace GKNet
  24. {
  25. public static class NetHelper
  26. {
  27. // Fatal problem:
  28. // if there is an address statically assigned to the corporate network,
  29. // then there is still no correct external address
  30. private static IPAddress GetPublicAddress()
  31. {
  32. NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
  33. // weed out addresses of virtual adapters (VirtualBox, VMWare, Tunngle, etc.)
  34. foreach (NetworkInterface network in networkInterfaces) {
  35. IPInterfaceProperties properties = network.GetIPProperties();
  36. if (properties.GatewayAddresses.Count == 0) {
  37. // all the magic is in this line
  38. continue;
  39. }
  40. foreach (IPAddressInformation address in properties.UnicastAddresses) {
  41. if (address.Address.AddressFamily != AddressFamily.InterNetwork)
  42. continue;
  43. if (IPAddress.IsLoopback(address.Address))
  44. continue;
  45. return address.Address;
  46. }
  47. }
  48. return default(IPAddress);
  49. }
  50. public static string GetPublicIPAddress()
  51. {
  52. if (!NetworkInterface.GetIsNetworkAvailable()) {
  53. return null;
  54. }
  55. try {
  56. string externalIP = GetPublicAddress().ToString();
  57. /*externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
  58. externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
  59. .Matches(externalIP)[0].ToString();*/
  60. return externalIP;
  61. } catch { return null; }
  62. }
  63. #if NET35
  64. // IPv4 192.168.1.1 maps as ::FFFF:192.168.1.1
  65. public static IPAddress MapToIPv6(this IPAddress address)
  66. {
  67. if (address.AddressFamily == AddressFamily.InterNetworkV6) {
  68. return address;
  69. }
  70. byte[] addr = address.GetAddressBytes();
  71. byte[] labels = new byte[16];
  72. labels[10] = 0xFF;
  73. labels[11] = 0xFF;
  74. labels[12] = addr[0];
  75. labels[13] = addr[1];
  76. labels[14] = addr[2];
  77. labels[15] = addr[3];
  78. return new IPAddress(labels, 0);
  79. }
  80. #endif
  81. }
  82. }