Repository navigation
Expand file tree
/
Copy pathNetworkInterface.cs
More file actions
496 lines (425 loc) · 16.2 KB
/
Copy pathNetworkInterface.cs
File metadata and controls
496 lines (425 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//
// Copyright (c) .NET Foundation and Contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
//
using System.Collections;
using System.Runtime.CompilerServices;
using System.Threading;
namespace System.Net.NetworkInformation
{
/// <summary>
/// Provides information about network interfaces and enables applications to control them.
/// </summary>
/// <remarks>
/// This class is exclusive of nanoFramework and it does not exist on the UWP API.
/// </remarks>
public class NetworkInterface
{
//set update flags...
[Flags]
internal enum UpdateOperation : byte
{
Invalid = 0x00,
Dns = 0x01,
Dhcp = 0x02,
DhcpRenew = 0x04,
DhcpRelease = 0x08,
Mac = 0x10,
}
private readonly int _interfaceIndex;
private byte[] _macAddress;
private AddressMode _startupAddressMode;
private uint _specificConfigId;
private bool _automaticDns;
// IPv4 fields
private uint _ipv4Address;
private uint _ipv4NetMask;
private uint _ipv4GatewayAddress;
private uint _ipv4dnsAddress1;
private uint _ipv4dnsAddress2;
// IPv6 fields
private uint[] _ipv6Address;
private uint[] _ipv6NetMask;
private uint[] _ipv6GatewayAddress;
private uint[] _ipv6dnsAddress1;
private uint[] _ipv6dnsAddress2;
private NetworkInterfaceType _networkInterfaceType;
/// <summary>
/// Indicates whether any network connection is available.
/// </summary>
/// <returns>
/// <see langword="true"/> if a network connection is available; otherwise, <see langword="false"/>.
/// </returns>
/// <remarks>
/// A network connection is considered to be available if any network interface is marked "up" and is not a loopback or tunnel interface.
/// </remarks>
[MethodImpl(MethodImplOptions.InternalCall)]
public extern static bool GetIsNetworkAvailable();
/// <summary>
/// Initializes a new instance of the <see cref="NetworkInterface"/> class.
/// </summary>
/// <param name="interfaceIndex"></param>
protected NetworkInterface(int interfaceIndex)
{
_interfaceIndex = interfaceIndex;
_networkInterfaceType = NetworkInterfaceType.Unknown;
_startupAddressMode = AddressMode.Invalid;
_specificConfigId = uint.MaxValue;
_automaticDns = true;
_ipv6Address = new uint[4];
_ipv6NetMask = new uint[4];
_ipv6GatewayAddress = new uint[4];
_ipv6dnsAddress1 = new uint[4];
_ipv6dnsAddress2 = new uint[4];
}
/// <summary>
/// Retrieves an array of all of the device's network interfaces.
/// </summary>
/// <returns>An array containing all of the device's network interfaces. </returns>
public static NetworkInterface[] GetAllNetworkInterfaces()
{
int count = GetNetworkInterfaceCount();
NetworkInterface[] ifaces = new NetworkInterface[count];
for (uint i = 0; i < count; i++)
{
ifaces[i] = GetNetworkInterface(i);
}
return ifaces;
}
private string IPv6AddressToString(uint[] ipv6Address)
{
throw new NotImplementedException();
// return string.Concat(
// ipv6Address[0].ToString("X4"),
// ":",
// ipv6Address[1].ToString("X4"),
// ".",
// ipv6Address[2].ToString("X4"),
// ".",
// ipv6Address[3].ToString("X4")
// );
}
/// <summary>
/// Enables an application to set and use a static IPv4 address.
/// </summary>
/// <param name="ipv4Address">Holds the IPv4 address to use. </param>
/// <param name="ipv4SubnetMask">Contains the IPv4 address's subnet mask.</param>
/// <param name="ipv4GatewayAddress">Specifies the IPv4 address of the gateway. </param>
public void EnableStaticIPv4(string ipv4Address, string ipv4SubnetMask, string ipv4GatewayAddress)
{
try
{
_ipv4Address = (uint)IPV4AddressFromString(ipv4Address);
_ipv4NetMask = (uint)IPV4AddressFromString(ipv4SubnetMask);
_ipv4GatewayAddress = (uint)IPV4AddressFromString(ipv4GatewayAddress);
_startupAddressMode = AddressMode.Static;
UpdateConfiguration((int)UpdateOperation.Dhcp);
}
finally
{
ReloadSettings();
}
}
/// <summary>
/// Enables an application to set and use a static IPv6 address.
/// </summary>
/// <param name="ipv6Address">Holds the IPv6 address to use. </param>
/// <param name="ipv6SubnetMask">Contains the IPv6 address's subnet mask.</param>
/// <param name="ipv6GatewayAddress">Specifies the IPv6 address of the gateway. </param>
public void EnableStaticIPv6(string ipv6Address, string ipv6SubnetMask, string ipv6GatewayAddress)
{
try
{
throw new NotImplementedException();
// FIXME
// need to test this
//_ipv6Address = IPV4AddressFromString(ipv6Address);
//_ipv6NetMask = IPV4AddressFromString(ipv6subnetMask);
//_ipv6GatewayAddress = IPV4AddressFromString(ipv6gatewayAddress);
//_startupAddressMode = AddressMode.Static;
//UpdateConfiguration(UPDATE_FLAGS_DHCP);
}
finally
{
ReloadSettings();
}
}
/// <summary>
/// Enables an application to set and use a static IPv4 and IPv6 address.
/// </summary>
/// <param name="ipv4Address">Holds the IPv4 address to use. </param>
/// <param name="ipv4subnetMask">Contains the IPv4 address's subnet mask.</param>
/// <param name="ipv4gatewayAddress">Specifies the IPv4 address of the gateway. </param>
/// <param name="ipv6Address">Holds the IPv6 address to use. </param>
/// <param name="ipv6SubnetMask">Contains the IPv6 address's subnet mask.</param>
/// <param name="ipv6GatewayAddress">Specifies the IPv6 address of the gateway. </param>
public void EnableStaticIP(string ipv4Address, string ipv4subnetMask, string ipv4gatewayAddress, string ipv6Address, string ipv6SubnetMask, string ipv6GatewayAddress)
{
try
{
throw new NotImplementedException();
_ipv4Address = (uint)IPV4AddressFromString(ipv4Address);
_ipv4NetMask = (uint)IPV4AddressFromString(ipv4subnetMask);
_ipv4GatewayAddress = (uint)IPV4AddressFromString(ipv4gatewayAddress);
// FIXME
// need to test this
//_ipv6Address = IPV4AddressFromString(ipv6Address);
//_ipv6NetMask = IPV4AddressFromString(ipv6subnetMask);
//_ipv6GatewayAddress = IPV4AddressFromString(ipv6gatewayAddress);
_startupAddressMode = AddressMode.Static;
UpdateConfiguration((int)UpdateOperation.Dhcp);
}
finally
{
ReloadSettings();
}
}
/// <summary>
/// Enables the Dynamic Host Configuration Protocol (DHCP) for service with this network interface.
/// </summary>
public void EnableDhcp()
{
try
{
if (_startupAddressMode != AddressMode.DHCP)
{
_startupAddressMode = AddressMode.DHCP;
UpdateConfiguration((int)UpdateOperation.Dhcp);
}
}
finally
{
ReloadSettings();
}
}
/// <summary>
/// Enables a network interface to use a specific DNS server IPv4 address.
/// </summary>
/// <param name="dnsAddresses">Holds the DNS server address. </param>
public void EnableStaticIPv4Dns(string[] dnsAddresses)
{
if (dnsAddresses == null || dnsAddresses.Length == 0 || dnsAddresses.Length > 2)
{
throw new ArgumentException();
}
uint[] addresses = new uint[2];
int iAddress = 0;
for (int i = 0; i < dnsAddresses.Length; i++)
{
uint address = (uint)IPV4AddressFromString(dnsAddresses[i]);
addresses[iAddress] = address;
if (address != 0)
{
iAddress++;
}
}
try
{
_ipv4dnsAddress1 = addresses[0];
_ipv4dnsAddress2 = addresses[1];
// clear flag
_automaticDns = false;
UpdateConfiguration((int)UpdateOperation.Dns);
}
finally
{
ReloadSettings();
}
}
/// <summary>
/// Enables a network interface to use a specific DNS server IPv6 address.
/// </summary>
/// <param name="dnsAddresses">Holds the DNS server address. </param>
public void EnableStaticIPv6Dns(string[] dnsAddresses)
{
throw new NotImplementedException();
// reset flag
//_flags &= ~Options.DynamicDNS;
//if (dnsAddresses == null || dnsAddresses.Length == 0 || dnsAddresses.Length > 2)
//{
// throw new ArgumentException();
//}
//uint[] addresses = new uint[2];
//int iAddress = 0;
//for (int i = 0; i < dnsAddresses.Length; i++)
//{
// uint address = IPV4AddressFromString(dnsAddresses[i]);
// addresses[iAddress] = address;
// if (address != 0)
// {
// iAddress++;
// }
//}
//try
//{
// _ipv6dnsAddress1 = addresses[0];
// _ipv6dnsAddress2 = addresses[1];
// UpdateConfiguration(UPDATE_FLAGS_DNS);
//}
//finally
//{
// ReloadSettings();
//}
}
/// <summary>
/// Enables a network interface to obtain a DNS server address automatically.
/// </summary>
[Obsolete("This method will be removed in a future version. Using automatic DNS it's part of DHCP configuration and can't be used as an individual operation.")]
public void EnableAutomaticDns()
{
try
{
// reset IPv4 DNS addresses
_ipv4dnsAddress1 = 0;
_ipv4dnsAddress2 = 0;
// set flag
_automaticDns = true;
UpdateConfiguration((int)UpdateOperation.Dns);
}
finally
{
ReloadSettings();
}
}
/// <summary>
/// Retrieves a value indicating whether a network interface can obtain a DNS server address automatically.
/// true if dynamic DNS is enabled, or false if not.
/// </summary>
public bool IsAutomaticDnsEnabled
{
get
{
return _automaticDns;
}
}
/// <summary>
/// Holds the IP v4 address of the network interface.
/// </summary>
public string IPv4Address
{
get { return IPAddress.IPv4ToString(_ipv4Address); }
}
/// <summary>
/// Contains the gateway IPv4 address.
/// </summary>
public string IPv4GatewayAddress
{
get { return IPAddress.IPv4ToString(_ipv4GatewayAddress); }
}
/// <summary>
/// Retrieves the network interface's IPv4 subnet mask.
/// </summary>
public string IPv4SubnetMask
{
get { return IPAddress.IPv4ToString(_ipv4NetMask); }
}
/// <summary>
/// Gets a value specifying whether DHCP is enabled for this network interfaces.
/// true if DHCP is enabled, or false if not.
/// </summary>
public bool IsDhcpEnabled
{
get { return (_startupAddressMode == AddressMode.DHCP); }
}
/// <summary>
/// Holds the IPv4 DNS server address.
/// </summary>
public string[] IPv4DnsAddresses
{
get
{
ArrayList list = new ArrayList();
if (_ipv4dnsAddress1 != 0)
{
list.Add(IPAddress.IPv4ToString(_ipv4dnsAddress1));
}
if (_ipv4dnsAddress2 != 0)
{
list.Add(IPAddress.IPv4ToString(_ipv4dnsAddress2));
}
return (string[])list.ToArray(typeof(string));
}
}
private void ReloadSettings()
{
Thread.Sleep(100);
InitializeNetworkInterfaceSettings();
}
/// <summary>
/// Releases the DHCP lease, which releases the IP address bound to a DHCP-enabled network interface.
/// </summary>
public void ReleaseDhcpLease()
{
try
{
UpdateConfiguration((int)UpdateOperation.DhcpRelease);
}
finally
{
ReloadSettings();
}
}
/// <summary>
/// Renews a DHCP lease, which renews the IP address on a DHCP-enabled network interface.
/// </summary>
public void RenewDhcpLease()
{
try
{
UpdateConfiguration((int)UpdateOperation.DhcpRenew);
}
finally
{
ReloadSettings();
}
}
/// <summary>
/// Gets or sets the Media Access Control (MAC) address for a network interface.
/// </summary>
public byte[] PhysicalAddress
{
get { return _macAddress; }
set
{
try
{
_macAddress = value;
UpdateConfiguration((int)UpdateOperation.Mac);
}
finally
{
ReloadSettings();
}
}
}
/// <summary>
/// Retrieves a value specifying the type of network interface being used by the device.
/// </summary>
public NetworkInterfaceType NetworkInterfaceType
{
get { return _networkInterfaceType; }
}
/// <summary>
/// The ID of the associated configuration, if any. To be used as the foreign key of that configuration.
/// </summary>
/// <remarks>
/// If there is no configuration associated it reads as <see cref="uint.MaxValue"/>.
/// </remarks>
public uint SpecificConfigId { get => _specificConfigId; set => _specificConfigId = value; }
#region native methods
[MethodImpl(MethodImplOptions.InternalCall)]
private extern static int GetNetworkInterfaceCount();
[MethodImpl(MethodImplOptions.InternalCall)]
private extern static NetworkInterface GetNetworkInterface(uint interfaceIndex);
[MethodImpl(MethodImplOptions.InternalCall)]
private extern void InitializeNetworkInterfaceSettings();
[MethodImpl(MethodImplOptions.InternalCall)]
private extern void UpdateConfiguration(int updateType);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern long IPV4AddressFromString(string ipAddress);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern ushort[] IPV6AddressFromString(string ipAddress);
#endregion
}
}