Pmp.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // Authors:
  3. // Ben Motmans <ben.motmans@gmail.com>
  4. // Nicholas Terry <nick.i.terry@gmail.com>
  5. //
  6. // Copyright (C) 2007 Ben Motmans
  7. // Copyright (C) 2014 Nicholas Terry
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Linq;
  31. using System.Net;
  32. using System.Net.NetworkInformation;
  33. using System.Net.Sockets;
  34. using System.Text;
  35. namespace Mono.Nat.Pmp
  36. {
  37. internal abstract class Pmp
  38. {
  39. public static List<UdpClient> sockets;
  40. protected static Dictionary<UdpClient, List<IPEndPoint>> gatewayLists;
  41. internal static void CreateSocketsAndAddGateways()
  42. {
  43. sockets = new List<UdpClient>();
  44. gatewayLists = new Dictionary<UdpClient, List<IPEndPoint>>();
  45. try
  46. {
  47. foreach (NetworkInterface n in NetworkInterface.GetAllNetworkInterfaces())
  48. {
  49. if (n.OperationalStatus != OperationalStatus.Up && n.OperationalStatus != OperationalStatus.Unknown)
  50. continue;
  51. IPInterfaceProperties properties = n.GetIPProperties();
  52. List<IPEndPoint> gatewayList = new List<IPEndPoint>();
  53. foreach (GatewayIPAddressInformation gateway in properties.GatewayAddresses)
  54. {
  55. if (gateway.Address.AddressFamily == AddressFamily.InterNetwork)
  56. {
  57. gatewayList.Add(new IPEndPoint(gateway.Address, PmpConstants.ServerPort));
  58. }
  59. }
  60. if (gatewayList.Count == 0)
  61. {
  62. /* Mono on OSX doesn't give any gateway addresses, so check DNS entries */
  63. foreach (var gw2 in properties.DnsAddresses)
  64. {
  65. if (gw2.AddressFamily == AddressFamily.InterNetwork)
  66. {
  67. gatewayList.Add(new IPEndPoint(gw2, PmpConstants.ServerPort));
  68. }
  69. }
  70. foreach (var unicast in properties.UnicastAddresses)
  71. {
  72. if (/*unicast.DuplicateAddressDetectionState == DuplicateAddressDetectionState.Preferred
  73. && unicast.AddressPreferredLifetime != UInt32.MaxValue
  74. && */unicast.Address.AddressFamily == AddressFamily.InterNetwork)
  75. {
  76. var bytes = unicast.Address.GetAddressBytes();
  77. bytes[3] = 1;
  78. gatewayList.Add(new IPEndPoint(new IPAddress(bytes), PmpConstants.ServerPort));
  79. }
  80. }
  81. }
  82. if (gatewayList.Count > 0)
  83. {
  84. foreach (UnicastIPAddressInformation address in properties.UnicastAddresses)
  85. {
  86. if (address.Address.AddressFamily == AddressFamily.InterNetwork)
  87. {
  88. UdpClient client;
  89. try
  90. {
  91. client = new UdpClient(new IPEndPoint(address.Address, 0));
  92. }
  93. catch (SocketException)
  94. {
  95. continue; // Move on to the next address.
  96. }
  97. gatewayLists.Add(client, gatewayList);
  98. sockets.Add(client);
  99. }
  100. }
  101. }
  102. }
  103. }
  104. catch (Exception)
  105. {
  106. // NAT-PMP does not use multicast, so there isn't really a good fallback.
  107. }
  108. }
  109. }
  110. }