|
4 | 4 | // See LICENSE file in the project root for full license information. |
5 | 5 | // |
6 | 6 |
|
| 7 | +using System.Net.NetworkInformation; |
| 8 | + |
7 | 9 | namespace System.Net |
8 | 10 | { |
9 | 11 | /// <summary> |
@@ -195,52 +197,26 @@ private static int ConvertStringToInt32(string value) |
195 | 197 | /// <returns>The default IP address.</returns> |
196 | 198 | public static IPAddress GetDefaultLocalAddress() |
197 | 199 | { |
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(); |
210 | 201 |
|
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++) |
218 | 204 | { |
219 | | - } |
| 205 | + NetworkInterface ni = interfaces[i]; |
220 | 206 |
|
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") |
229 | 208 | { |
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); |
237 | 210 | } |
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 | + //} |
241 | 217 | } |
242 | 218 |
|
243 | | - return IPAddress.Any; |
| 219 | + return Any; |
244 | 220 | } |
245 | 221 | } |
246 | 222 | } |
0 commit comments