Skip to content

Commit ee4e05a

Browse files
authored
Fix GetDefaultLocalAddress (#65)
1 parent 9929c94 commit ee4e05a

File tree

1 file changed

+15
-39
lines changed

1 file changed

+15
-39
lines changed

source/nanoFramework.System.Net/IPAddress.cs

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// See LICENSE file in the project root for full license information.
55
//
66

7+
using System.Net.NetworkInformation;
8+
79
namespace System.Net
810
{
911
/// <summary>
@@ -195,52 +197,26 @@ private static int ConvertStringToInt32(string value)
195197
/// <returns>The default IP address.</returns>
196198
public static IPAddress GetDefaultLocalAddress()
197199
{
198-
// Special conditions are implemented here because of a problem with GetHostEntry
199-
// on the digi device and NetworkInterface from the emulator.
200-
// In the emulator we must use GetHostEntry.
201-
// On the device and Windows NetworkInterface works and is preferred.
202-
try
203-
{
204-
//NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
205-
206-
//int cnt = interfaces.Length;
207-
//for (int i = 0; i < cnt; i++)
208-
//{
209-
// NetworkInterface ni = interfaces[i];
200+
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
210201

211-
// if (ni.IPAddress != "0.0.0.0" && ni.SubnetMask != "0.0.0.0")
212-
// {
213-
// return IPAddress.Parse(ni.IPAddress);
214-
// }
215-
//}
216-
}
217-
catch
202+
int cnt = interfaces.Length;
203+
for (int i = 0; i < cnt; i++)
218204
{
219-
}
205+
NetworkInterface ni = interfaces[i];
220206

221-
try
222-
{
223-
IPAddress localAddress = null;
224-
// IPHostEntry hostEntry = Dns.GetHostEntry("");
225-
IPHostEntry hostEntry = new IPHostEntry();
226-
227-
int cnt = hostEntry.AddressList.Length;
228-
for (int i = 0; i < cnt; ++i)
207+
if (ni.IPv4Address != "0.0.0.0" && ni.IPv4SubnetMask != "0.0.0.0")
229208
{
230-
if ((localAddress = hostEntry.AddressList[i]) != null)
231-
{
232-
if(localAddress.m_Address != 0)
233-
{
234-
return localAddress;
235-
}
236-
}
209+
return Parse(ni.IPv4Address);
237210
}
238-
}
239-
catch
240-
{
211+
// FIXME
212+
// TODO implement this when IPv6 support is added
213+
//else (ni.IPv6Address != "0.0.0.0" )
214+
//{
215+
// return IPAddress.Parse(ni.IPv6Address);
216+
//}
241217
}
242218

243-
return IPAddress.Any;
219+
return Any;
244220
}
245221
}
246222
}

0 commit comments

Comments
 (0)