Repository navigation
Expand file tree
/
Copy pathNetworkInterface.cs
More file actions
659 lines (567 loc) · 31.6 KB
/
Copy pathNetworkInterface.cs
File metadata and controls
659 lines (567 loc) · 31.6 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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Security.Principal;
using System.Threading.Tasks;
using log4net;
using Microsoft.Win32;
using NETworkManager.Utilities;
using SMA = System.Management.Automation;
namespace NETworkManager.Models.Network;
/// <summary>
/// Provides static and instance methods for retrieving information about network interfaces, detecting local IP
/// addresses and gateways, and configuring network interface settings on the local machine.
/// </summary>
/// <remarks>The NetworkInterface class offers both synchronous and asynchronous methods for enumerating network
/// interfaces, detecting routing and gateway information, and performing network configuration tasks such as setting IP
/// addresses, DNS servers, and flushing the DNS cache. Most configuration operations require administrative privileges.
/// Events are provided to notify when user-initiated cancellations occur, such as when a UAC prompt is dismissed. This
/// class is intended for use in applications that need to query or modify network interface settings
/// programmatically.</remarks>
public sealed class NetworkInterface
{
#region Variables
/// <summary>
/// List of network interface name patterns to filter out virtual/filter adapters
/// introduced in .NET 9/10. These are typically not actual network interfaces but rather
/// drivers, filters, or extensions attached to real network interfaces.
/// See: https://github.com/dotnet/runtime/issues/122751
/// </summary>
private static readonly ILog Log = LogManager.GetLogger(typeof(NetworkInterface));
private static readonly List<string> NetworkInterfaceFilteredPatterns =
[
"Hyper-V Virtual Switch Extension Filter",
"Hyper-V Virtual Switch Extension Adapter",
"WFP Native MAC Layer LightWeight Filter",
"Npcap Packet Driver (NPCAP)",
"QoS Packet Scheduler",
"WFP 802.3 MAC Layer LightWeight Filter",
"Ethernet (Kerneldebugger)",
"Filter Driver",
"WAN Miniport",
"Microsoft Wi-Fi Direct Virtual Adapter"
];
#endregion
#region Methods
/// <summary>
/// Asynchronously retrieves a list of available network interfaces on the local machine.
/// </summary>
/// <returns>A task that represents the asynchronous operation. The task result contains a list of <see
/// cref="NetworkInterfaceInfo"/> objects describing each detected network interface.</returns>
public static Task<List<NetworkInterfaceInfo>> GetNetworkInterfacesAsync()
{
return Task.Run(GetNetworkInterfaces);
}
/// <summary>
/// Retrieves a list of network interfaces on the local machine, including detailed information about each interface
/// such as addresses, gateways, and DHCP settings.
/// </summary>
/// <remarks>Only Ethernet, Wireless80211, and proprietary virtual/internal interfaces are included. The
/// returned information includes both IPv4 and IPv6 details, as well as DHCP and DNS configuration where available.
/// This method may require appropriate permissions to access network configuration data.</remarks>
/// <returns>A list of <see cref="NetworkInterfaceInfo"/> objects, each representing a network interface with its associated
/// properties. The list is empty if no matching interfaces are found.</returns>
public static List<NetworkInterfaceInfo> GetNetworkInterfaces()
{
List<NetworkInterfaceInfo> listNetworkInterfaceInfo = [];
// Query network profiles (Domain/Private/Public) for all connected interfaces via PowerShell.
// Keyed by InterfaceAlias which matches networkInterface.Name in the .NET API.
var profileByAlias = new Dictionary<string, NetworkProfile>(StringComparer.OrdinalIgnoreCase);
try
{
using var ps = SMA.PowerShell.Create();
ps.AddScript("Get-NetConnectionProfile | Select-Object InterfaceAlias, NetworkCategory");
foreach (var result in ps.Invoke())
{
var alias = result.Properties["InterfaceAlias"]?.Value?.ToString();
var category = result.Properties["NetworkCategory"]?.Value?.ToString();
if (string.IsNullOrEmpty(alias))
continue;
profileByAlias[alias] = category switch
{
"DomainAuthenticated" => NetworkProfile.Domain,
"Private" => NetworkProfile.Private,
"Public" => NetworkProfile.Public,
_ => NetworkProfile.NotConfigured
};
}
}
catch (Exception ex)
{
Log.Warn("Failed to query network connection profiles via Get-NetConnectionProfile.", ex);
}
foreach (var networkInterface in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
{
// NetworkInterfaceType 53 is proprietary virtual/internal interface
// https://docs.microsoft.com/en-us/windows-hardware/drivers/network/ndis-interface-types
if (networkInterface.NetworkInterfaceType != NetworkInterfaceType.Ethernet &&
networkInterface.NetworkInterfaceType != NetworkInterfaceType.Wireless80211 &&
(int)networkInterface.NetworkInterfaceType != 53)
continue;
// Filter out virtual/filter adapters introduced in .NET 9/10
// Check if the adapter name or description contains any filtered pattern
// See: https://github.com/dotnet/runtime/issues/122751
if (NetworkInterfaceFilteredPatterns.Any(pattern =>
networkInterface.Name.Contains(pattern) ||
networkInterface.Description.Contains(pattern)))
continue;
var listIPv4Address = new List<Tuple<IPAddress, IPAddress>>();
var listIPv6AddressLinkLocal = new List<IPAddress>();
var listIPv6Address = new List<IPAddress>();
var dhcpLeaseObtained = new DateTime();
var dhcpLeaseExpires = new DateTime();
var ipProperties = networkInterface.GetIPProperties();
foreach (var unicastIPAddrInfo in ipProperties.UnicastAddresses)
{
switch (unicastIPAddrInfo.Address.AddressFamily)
{
case AddressFamily.InterNetwork:
listIPv4Address.Add(new Tuple<IPAddress, IPAddress>(unicastIPAddrInfo.Address,
unicastIPAddrInfo.IPv4Mask));
dhcpLeaseExpires =
(DateTime.UtcNow + TimeSpan.FromSeconds(unicastIPAddrInfo.AddressPreferredLifetime))
.ToLocalTime();
dhcpLeaseObtained =
(DateTime.UtcNow + TimeSpan.FromSeconds(unicastIPAddrInfo.AddressValidLifetime) -
TimeSpan.FromSeconds(unicastIPAddrInfo.DhcpLeaseLifetime)).ToLocalTime();
break;
case AddressFamily.InterNetworkV6 when unicastIPAddrInfo.Address.IsIPv6LinkLocal:
listIPv6AddressLinkLocal.Add(unicastIPAddrInfo.Address);
break;
case AddressFamily.InterNetworkV6:
listIPv6Address.Add(unicastIPAddrInfo.Address);
break;
}
}
var listIPv4Gateway = new List<IPAddress>();
var listIPv6Gateway = new List<IPAddress>();
foreach (var gatewayIPAddrInfo in ipProperties.GatewayAddresses)
{
switch (gatewayIPAddrInfo.Address.AddressFamily)
{
case AddressFamily.InterNetwork:
listIPv4Gateway.Add(gatewayIPAddrInfo.Address);
break;
case AddressFamily.InterNetworkV6:
listIPv6Gateway.Add(gatewayIPAddrInfo.Address);
break;
}
}
var dnsAutoconfigurationEnabled = IsDNSAutoconfigurationEnabled(networkInterface.Id) == true;
// Check if IPv4 protocol is available
var ipv4ProtocolAvailable = true;
IPv4InterfaceProperties ipv4Properties = null;
try
{
ipv4Properties = ipProperties.GetIPv4Properties();
}
catch (NetworkInformationException)
{
ipv4ProtocolAvailable = false;
}
// Check if IPv6 protocol is available
var ipv6ProtocolAvailable = true;
try
{
ipProperties.GetIPv6Properties();
}
catch (NetworkInformationException)
{
ipv6ProtocolAvailable = false;
}
listNetworkInterfaceInfo.Add(new NetworkInterfaceInfo
{
Id = networkInterface.Id,
Name = networkInterface.Name,
Description = networkInterface.Description,
Type = networkInterface.NetworkInterfaceType.ToString(),
PhysicalAddress = networkInterface.GetPhysicalAddress(),
Status = networkInterface.OperationalStatus,
IsOperational = networkInterface.OperationalStatus == OperationalStatus.Up,
Speed = networkInterface.Speed,
IPv4ProtocolAvailable = ipv4ProtocolAvailable,
IPv4Address = [.. listIPv4Address],
IPv4Gateway = [.. listIPv4Gateway],
DhcpEnabled = ipv4Properties is { IsDhcpEnabled: true },
DhcpServer = [.. ipProperties.DhcpServerAddresses.Where(dhcpServerIPAddress =>
dhcpServerIPAddress.AddressFamily == AddressFamily.InterNetwork)],
DhcpLeaseObtained = dhcpLeaseObtained,
DhcpLeaseExpires = dhcpLeaseExpires,
IPv6ProtocolAvailable = ipv6ProtocolAvailable,
IPv6AddressLinkLocal = [.. listIPv6AddressLinkLocal],
IPv6Address = [.. listIPv6Address],
IPv6Gateway = [.. listIPv6Gateway],
DNSAutoconfigurationEnabled = dnsAutoconfigurationEnabled,
DNSSuffix = ipProperties.DnsSuffix,
DNSServer = [.. ipProperties.DnsAddresses],
Profile = profileByAlias.GetValueOrDefault(networkInterface.Name, NetworkProfile.NotConfigured)
});
}
return listNetworkInterfaceInfo;
}
/// <summary>
/// Asynchronously determines the local IP address that would be used to route traffic to the specified remote IP
/// address.
/// </summary>
/// <remarks>This method is useful for identifying the local network interface that would be selected by
/// the system's routing table when communicating with a given remote address. The result may vary depending on the
/// current network configuration and routing rules.</remarks>
/// <param name="remoteIPAddress">The destination IP address for which to determine the corresponding local source IP address. Cannot be null.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the local IP address that would be
/// used to reach the specified remote IP address.</returns>
public static Task<IPAddress> DetectLocalIPAddressBasedOnRoutingAsync(IPAddress remoteIPAddress)
{
return Task.Run(() => DetectLocalIPAddressFromRouting(remoteIPAddress));
}
/// <summary>
/// Determines the local IP address that would be used to route traffic to the specified remote IP address.
/// </summary>
/// <remarks>This method creates a UDP socket to determine the local IP address selected by the system's
/// routing table for the given remote address. No data is sent over the network. This method may return null if the
/// routing information is unavailable or an error occurs.</remarks>
/// <param name="remoteIPAddress">The destination IP address for which to determine the local routing address. Must not be null.</param>
/// <returns>An IPAddress representing the local address that would be used to reach the specified remote address; or null if
/// the local address cannot be determined.</returns>
private static IPAddress DetectLocalIPAddressFromRouting(IPAddress remoteIPAddress)
{
var isIPv4 = remoteIPAddress.AddressFamily == AddressFamily.InterNetwork;
using var socket = new Socket(remoteIPAddress.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
// return null on error...
try
{
socket.Bind(new IPEndPoint(isIPv4 ? IPAddress.Any : IPAddress.IPv6Any, 0));
socket.Connect(new IPEndPoint(remoteIPAddress, 0));
if (socket.LocalEndPoint is IPEndPoint ipAddress)
return ipAddress.Address;
}
catch (SocketException) { }
return null;
}
/// <summary>
/// Asynchronously detects the local IP address associated with a network interface that matches the specified
/// address family.
/// </summary>
/// <param name="addressFamily">The address family to use when searching for a local IP address. Typically, use AddressFamily.InterNetwork for
/// IPv4 or AddressFamily.InterNetworkV6 for IPv6.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the detected local IP address, or
/// null if no suitable address is found.</returns>
public static Task<IPAddress> DetectLocalIPAddressFromNetworkInterfaceAsync(AddressFamily addressFamily)
{
return Task.Run(() => DetectLocalIPAddressFromNetworkInterface(addressFamily));
}
/// <summary>
/// Detects and returns the first local IP address assigned to an operational network interface that matches the
/// specified address family.
/// </summary>
/// <remarks>For IPv4, the method prefers non-link-local addresses but will return a link-local address if
/// no other is available. For IPv6, the method returns the first global or unique local address if present;
/// otherwise, it returns a link-local address if available. The returned address is selected from operational
/// network interfaces only.</remarks>
/// <param name="addressFamily">The address family to search for. Specify <see cref="AddressFamily.InterNetwork"/> for IPv4 addresses or <see
/// cref="AddressFamily.InterNetworkV6"/> for IPv6 addresses.</param>
/// <returns>An <see cref="IPAddress"/> representing the first detected local IP address for the specified address family, or
/// <see langword="null"/> if no suitable address is found.</returns>
public static IPAddress DetectLocalIPAddressFromNetworkInterface(AddressFamily addressFamily)
{
// Filter operational network interfaces
var networkInterfaces = GetNetworkInterfaces()
.Where(x => x.IsOperational)
.ToList();
var candidates = new List<IPAddress>();
switch (addressFamily)
{
// IPv4
case AddressFamily.InterNetwork:
{
foreach (var networkInterface in networkInterfaces)
{
foreach (var ipAddress in networkInterface.IPv4Address)
candidates.Add(ipAddress.Item1);
}
// Prefer non-link-local addresses
var nonLinkLocal = candidates.FirstOrDefault(x =>
{
var bytes = x.GetAddressBytes();
return !(bytes[0] == 169 && bytes[1] == 254);
});
// Return first non-link-local or first candidate if none found (might be null - no addresses at all)
return nonLinkLocal ?? candidates.FirstOrDefault();
}
// IPv6
case AddressFamily.InterNetworkV6:
{
// First try to get global or unique local addresses
foreach (var networkInterface in networkInterfaces)
candidates.AddRange(networkInterface.IPv6Address);
// Return first candidate if any found
if (candidates.Count != 0)
return candidates.First();
// Fallback to link-local addresses
var firstWithLinkLocal = networkInterfaces
.FirstOrDefault(ni => ni.IPv6AddressLinkLocal.Length != 0);
if (firstWithLinkLocal != null)
return firstWithLinkLocal.IPv6AddressLinkLocal.First();
break;
}
}
return null;
}
/// <summary>
/// Asynchronously detects the default gateway address associated with the specified local IP address.
/// </summary>
/// <param name="localIPAddress">The local IP address for which to determine the corresponding default gateway. Cannot be null.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the IP address of the detected
/// default gateway, or null if no gateway is found.</returns>
public static Task<IPAddress> DetectGatewayFromLocalIPAddressAsync(IPAddress localIPAddress)
{
return Task.Run(() => DetectGatewayFromLocalIPAddress(localIPAddress));
}
/// <summary>
/// Attempts to determine the default gateway address associated with the specified local IP address.
/// </summary>
/// <remarks>This method searches all available network interfaces to find one that has the specified
/// local IP address assigned. If found, it returns the first associated gateway address for that interface and
/// address family. Returns null if the local IP address is not assigned to any interface or if no gateway is
/// configured.</remarks>
/// <param name="localIPAddress">The local IP address for which to detect the corresponding gateway. Must be either an IPv4 or IPv6 address.</param>
/// <returns>An IPAddress representing the default gateway for the specified local IP address, or null if no matching gateway
/// is found.</returns>
private static IPAddress DetectGatewayFromLocalIPAddress(IPAddress localIPAddress)
{
foreach (var networkInterface in GetNetworkInterfaces())
{
switch (localIPAddress.AddressFamily)
{
// IPv4
case AddressFamily.InterNetwork when networkInterface.IPv4Address.Any(x => x.Item1.Equals(localIPAddress)):
return networkInterface.IPv4Gateway.FirstOrDefault();
// IPv6
case AddressFamily.InterNetworkV6 when networkInterface.IPv6Address.Contains(localIPAddress):
return networkInterface.IPv6Gateway.FirstOrDefault();
}
}
return null;
}
/// <summary>
/// Asynchronously applies the specified network interface configuration.
/// </summary>
/// <param name="config">The configuration settings to apply to the network interface. Cannot be null.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public static Task ConfigureNetworkInterfaceAsync(NetworkInterfaceConfig config)
{
return Task.Run(() => ConfigureNetworkInterface(config));
}
/// <summary>
/// Configures the network interface according to the specified settings.
/// </summary>
/// <remarks>Requires the application to be running with administrator privileges.</remarks>
/// <param name="config">An object containing the configuration parameters for the network interface, including IP address, subnet mask,
/// gateway, and DNS server settings. Cannot be null.</param>
private static void ConfigureNetworkInterface(NetworkInterfaceConfig config)
{
var name = PowerShellHelper.EscapeSingleQuotes(config.Name);
var commands = new List<string>();
// netsh returns an error if DHCP is already enabled. Skip these commands, so every error can be reported.
var (dhcpEnabled, dnsAutoconfigurationEnabled) = GetCurrentDhcpState(config.Name);
// IP
if (config.EnableStaticIPAddress)
commands.Add($"& netsh interface ipv4 set address name='{name}' source=static address={config.IPAddress} mask={config.Subnetmask} gateway={config.Gateway}");
else if (dhcpEnabled != true)
commands.Add($"& netsh interface ipv4 set address name='{name}' source=dhcp");
// DNS
if (config.EnableStaticDNS)
{
commands.Add($"& netsh interface ipv4 set dnsservers name='{name}' source=static address={config.PrimaryDNSServer} register=primary validate=no");
if (!string.IsNullOrEmpty(config.SecondaryDNSServer))
commands.Add($"& netsh interface ipv4 add dnsservers name='{name}' address={config.SecondaryDNSServer} index=2 validate=no");
}
else if (dnsAutoconfigurationEnabled != true)
{
commands.Add($"& netsh interface ipv4 set dnsservers name='{name}' source=dhcp");
}
RunCommands(commands);
}
/// <summary>
/// Checks whether the DNS servers of a network interface are assigned automatically (DHCP). This can only be
/// detected via the registry: the "NameServer" value of the interface is empty, if no static DNS servers are set.
/// </summary>
/// <param name="interfaceId">ID (GUID) of the network interface.</param>
/// <returns>Whether DNS autoconfiguration is enabled, or <see langword="null" /> if it cannot be determined.</returns>
private static bool? IsDNSAutoconfigurationEnabled(string interfaceId)
{
using var interfaceKey = Registry.LocalMachine.OpenSubKey(
$@"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{interfaceId}");
var nameServer = interfaceKey?.GetValue("NameServer");
return nameServer == null ? null : string.IsNullOrEmpty(nameServer.ToString());
}
/// <summary>
/// Reads the current DHCP state of a network interface right before it is configured (the information shown in the
/// UI can be outdated). Returns <see langword="null" /> for values that cannot be determined, in which case the
/// corresponding command is not skipped.
/// </summary>
/// <param name="name">Name of the network interface.</param>
/// <returns>Whether DHCP is enabled for the IPv4 address and for the DNS servers.</returns>
private static (bool? DhcpEnabled, bool? DNSAutoconfigurationEnabled) GetCurrentDhcpState(string name)
{
try
{
var networkInterface = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
.FirstOrDefault(x => x.Name == name);
if (networkInterface == null)
return (null, null);
return (networkInterface.GetIPProperties().GetIPv4Properties()?.IsDhcpEnabled,
IsDNSAutoconfigurationEnabled(networkInterface.Id));
}
catch (Exception ex)
{
Log.Warn($"Could not read the DHCP state of network interface \"{name}\".", ex);
return (null, null);
}
}
/// <summary>
/// Checks whether the current process is allowed to change the network configuration (IP addresses, DNS servers,
/// release/renew, flush DNS cache). This is the case if it is running as administrator or as member of the
/// "Network Configuration Operators" group, which may modify the TCP/IP settings without being an administrator.
/// </summary>
/// <remarks><see cref="WindowsPrincipal.IsInRole(SecurityIdentifier)" /> only returns <see langword="true" /> if the
/// group is enabled in the access token. With UAC, both groups are only enabled if the application is running
/// elevated, so a non-elevated process is correctly reported as not allowed.</remarks>
/// <returns><see langword="true" /> if the network configuration can be changed.</returns>
public static bool HasPermissionToConfigure()
{
var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
return principal.IsInRole(WindowsBuiltInRole.Administrator) ||
principal.IsInRole(new SecurityIdentifier(WellKnownSidType.BuiltinNetworkConfigurationOperatorsSid,
null));
}
/// <summary>
/// Asynchronously flushes the system DNS resolver cache.
/// </summary>
/// <remarks>This method initiates the DNS cache flush operation on a background thread. The operation may
/// require elevated permissions depending on the system configuration.</remarks>
/// <returns>A task that represents the asynchronous flush operation.</returns>
public static Task FlushDnsAsync()
{
return Task.Run(FlushDns);
}
/// <summary>
/// Clears the local DNS resolver cache on the system.
/// </summary>
/// <remarks>Requires the application to be running with administrator privileges.</remarks>
private static void FlushDns()
{
RunCommands(["& ipconfig /flushdns"]);
}
/// <summary>
/// Asynchronously releases and renews the IP configuration for the specified network adapter using the given mode.
/// </summary>
/// <param name="mode">The release and renew operation mode to apply to the network adapter.</param>
/// <param name="adapterName">The name of the network adapter whose IP configuration will be released and renewed. Cannot be null or empty.</param>
/// <returns>A task that represents the asynchronous release and renew operation.</returns>
public static Task ReleaseRenewAsync(IPConfigReleaseRenewMode mode, string adapterName)
{
return Task.Run(() => ReleaseRenew(mode, adapterName));
}
/// <summary>
/// Releases and/or renews the IP configuration for the specified network adapter using the given mode.
/// </summary>
/// <remarks>Requires the application to be running with administrator privileges.</remarks>
/// <param name="mode">A value that specifies which IP configuration operation to perform.</param>
/// <param name="adapterName">The name of the network adapter to target. Cannot be null or empty.</param>
private static void ReleaseRenew(IPConfigReleaseRenewMode mode, string adapterName)
{
var name = PowerShellHelper.EscapeSingleQuotes(adapterName);
var commands = new List<string>();
if (mode is IPConfigReleaseRenewMode.ReleaseRenew or IPConfigReleaseRenewMode.Release)
commands.Add($"& ipconfig /release '{name}'");
if (mode is IPConfigReleaseRenewMode.ReleaseRenew or IPConfigReleaseRenewMode.Renew)
commands.Add($"& ipconfig /renew '{name}'");
if (mode is IPConfigReleaseRenewMode.ReleaseRenew6 or IPConfigReleaseRenewMode.Release6)
commands.Add($"& ipconfig /release6 '{name}'");
if (mode is IPConfigReleaseRenewMode.ReleaseRenew6 or IPConfigReleaseRenewMode.Renew6)
commands.Add($"& ipconfig /renew6 '{name}'");
RunCommands(commands);
}
/// <summary>
/// Asynchronously adds an IP address to the specified network interface using the provided configuration.
/// </summary>
/// <param name="config">The configuration settings that specify the network interface and IP address to add. Cannot be null.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public static Task AddIPAddressToNetworkInterfaceAsync(NetworkInterfaceConfig config)
{
return Task.Run(() => AddIPAddressToNetworkInterface(config));
}
/// <summary>
/// Adds an IP address to the specified network interface using the provided configuration.
/// </summary>
/// <remarks>Requires the application to be running with administrator privileges.</remarks>
/// <param name="config">The network interface configuration containing the interface name, IP address, subnet mask, and DHCP/static
/// coexistence settings. Cannot be null.</param>
private static void AddIPAddressToNetworkInterface(NetworkInterfaceConfig config)
{
var name = PowerShellHelper.EscapeSingleQuotes(config.Name);
var commands = new List<string>();
if (config.EnableDhcpStaticIpCoexistence)
commands.Add($"& netsh interface ipv4 set interface interface='{name}' dhcpstaticipcoexistence=enabled");
commands.Add($"& netsh interface ipv4 add address '{name}' {config.IPAddress} {config.Subnetmask}");
RunCommands(commands);
}
/// <summary>
/// Asynchronously removes the IP address specified in the configuration from the associated network interface.
/// </summary>
/// <param name="config">The configuration object that specifies the network interface and IP address to remove. Cannot be null.</param>
/// <returns>A task that represents the asynchronous remove operation.</returns>
public static Task RemoveIPAddressFromNetworkInterfaceAsync(NetworkInterfaceConfig config)
{
return Task.Run(() => RemoveIPAddressFromNetworkInterface(config));
}
/// <summary>
/// Removes the specified IP address from the given network interface configuration.
/// </summary>
/// <remarks>Requires the application to be running with administrator privileges.</remarks>
/// <param name="config">The network interface configuration containing the name of the interface and the IP address to remove. Cannot be
/// null.</param>
private static void RemoveIPAddressFromNetworkInterface(NetworkInterfaceConfig config)
{
var name = PowerShellHelper.EscapeSingleQuotes(config.Name);
RunCommands([$"& netsh interface ipv4 delete address '{name}' {config.IPAddress}"]);
}
/// <summary>
/// Runs one or more native commands (<c>netsh</c>, <c>ipconfig</c>) one after another via a PowerShell runspace.
/// Stops at the first command that fails (non-zero exit code) and throws an exception with the output of the
/// command (e.g. "The requested operation requires elevation."), which is localized by Windows and shown to the
/// user. Commands that would not change anything (and therefore fail with <c>netsh</c>) must not be passed.
/// </summary>
/// <param name="commands">Commands to run, e.g. <c>& netsh interface ipv4 ...</c>.</param>
/// <exception cref="Exception">If a command fails.</exception>
private static void RunCommands(IEnumerable<string> commands)
{
foreach (var command in commands)
{
using var ps = SMA.PowerShell.Create();
ps.AddScript($"$output = {command} 2>&1 | Out-String{Environment.NewLine}" +
"[PSCustomObject]@{ Output = $output; ExitCode = $LASTEXITCODE }");
var result = ps.Invoke().LastOrDefault();
// The script itself failed and did not return a result.
if (result == null)
throw new Exception(ps.HadErrors
? string.Join("; ", ps.Streams.Error.Select(e => e.ToString()))
: $"\"{command}\" did not return a result.");
// Each command runs in a new runspace, so the exit code is only set if the native command was started.
var exitCode = result.Properties["ExitCode"]?.Value as int?;
if (exitCode == 0)
continue;
var output = result.Properties["Output"]?.Value?.ToString()?.Trim();
Log.Error($"Command \"{command}\" failed with exit code {exitCode?.ToString() ?? "-/-"}: {output}");
if (!string.IsNullOrEmpty(output))
throw new Exception(output);
throw new Exception(exitCode == null
? $"\"{command.TrimStart('&', ' ')}\" could not be run."
: $"\"{command.TrimStart('&', ' ')}\" failed with exit code {exitCode}.");
}
}
#endregion
}