Skip to content

Commit 4fe9ec5

Browse files
authored
Feature: Release & renew IPv6 & target ethernet adapter (BornToBeRoot#1982)
* Feature: Release & renew IPv6 & target ethernet adapter * Docs: Add BornToBeRoot#1982
1 parent 041b8ec commit 4fe9ec5

7 files changed

Lines changed: 298 additions & 209 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace NETworkManager.Models.Network;
2+
3+
/// <summary>
4+
/// Release or renew modes to control ipconfig.exe.
5+
/// </summary>
6+
public enum IPConfigReleaseRenewMode
7+
{
8+
/// <summary>
9+
/// Release and renew IPv4.
10+
/// </summary>
11+
ReleaseRenew,
12+
13+
/// <summary>
14+
/// Release and renew IPv6.
15+
/// </summary>
16+
ReleaseRenew6,
17+
18+
/// <summary>
19+
/// Release IPv4.
20+
/// </summary>
21+
Release,
22+
23+
/// <summary>
24+
/// Release IPv6.
25+
/// </summary>
26+
Release6,
27+
28+
/// <summary>
29+
/// Renew IPv4.
30+
/// </summary>
31+
Renew,
32+
33+
/// <summary>
34+
/// Renew IPv6.
35+
/// </summary>
36+
Renew6
37+
}

Source/NETworkManager.Models/Network/NetworkInterface.cs

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace NETworkManager.Models.Network
1313
{
14-
public class NetworkInterface
14+
public partial class NetworkInterface
1515
{
1616
#region Events
1717
public event EventHandler UserHasCanceled;
@@ -167,19 +167,18 @@ public static IPAddress DetectLocalIPAddressBasedOnRouting(IPAddress remoteIPAdd
167167
{
168168
bool isIPv4 = remoteIPAddress.AddressFamily == AddressFamily.InterNetwork;
169169

170-
using (var socket = new Socket(isIPv4 ? AddressFamily.InterNetwork : AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp))
170+
using var socket = new Socket(isIPv4 ? AddressFamily.InterNetwork : AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
171+
172+
// return null on error...
173+
try
171174
{
172-
// return null on error...
173-
try
174-
{
175-
socket.Bind(new IPEndPoint( isIPv4 ? IPAddress.Any : IPAddress.IPv6Any, 0));
176-
socket.Connect(new IPEndPoint(remoteIPAddress, 0));
175+
socket.Bind(new IPEndPoint(isIPv4 ? IPAddress.Any : IPAddress.IPv6Any, 0));
176+
socket.Connect(new IPEndPoint(remoteIPAddress, 0));
177177

178-
if (socket.LocalEndPoint is IPEndPoint ipAddress)
179-
return ipAddress.Address;
180-
}
181-
catch (SocketException) { }
178+
if (socket.LocalEndPoint is IPEndPoint ipAddress)
179+
return ipAddress.Address;
182180
}
181+
catch (SocketException) { }
183182

184183
return null;
185184
}
@@ -249,72 +248,101 @@ public void ConfigureNetworkInterface(NetworkInterfaceConfig config)
249248
}
250249
}
251250

251+
/// <summary>
252+
/// Flush the DNS cache asynchronously.
253+
/// </summary>
254+
/// <returns>Running task.</returns>
252255
public static Task FlushDnsAsync()
253256
{
254257
return Task.Run(() => FlushDns());
255258
}
256259

260+
/// <summary>
261+
/// Flush the DNS cache.
262+
/// </summary>
257263
public static void FlushDns()
258264
{
259265
const string command = @"ipconfig /flushdns";
260266

261267
PowerShellHelper.ExecuteCommand(command);
262268
}
263269

264-
public static Task ReleaseRenewAsync(IPConfigReleaseRenewMode mode)
270+
/// <summary>
271+
/// Release or renew the IP address of the specified network adapter asynchronously.
272+
/// </summary>
273+
/// <param name="mode">ipconfig.exe modes which are used like /release(6) or /renew(6)</param>
274+
/// <param name="adapterName">Name of the ethernet adapter.</param>
275+
/// <returns>Running task.</returns>
276+
public static Task ReleaseRenewAsync(IPConfigReleaseRenewMode mode, string adapterName)
265277
{
266-
return Task.Run(() => ReleaseRenew(mode));
278+
return Task.Run(() => ReleaseRenew(mode, adapterName));
267279
}
268280

269-
public static void ReleaseRenew(IPConfigReleaseRenewMode mode)
281+
/// <summary>
282+
/// Release or renew the IP address of the specified network adapter.
283+
/// </summary>
284+
/// <param name="mode">ipconfig.exe modes which are used like /release(6) or /renew(6)</param>
285+
/// <param name="adapterName">Name of the ethernet adapter.</param>
286+
public static void ReleaseRenew(IPConfigReleaseRenewMode mode, string adapterName)
270287
{
271-
if (mode == IPConfigReleaseRenewMode.ReleaseRenew || mode == IPConfigReleaseRenewMode.Release)
272-
{
273-
const string command = @"ipconfig /release";
288+
string command = string.Empty;
274289

275-
PowerShellHelper.ExecuteCommand(command);
276-
}
290+
if (mode == IPConfigReleaseRenewMode.ReleaseRenew || mode == IPConfigReleaseRenewMode.Release)
291+
command += @"ipconfig /release '" + adapterName + "';";
277292

278293
if (mode == IPConfigReleaseRenewMode.ReleaseRenew || mode == IPConfigReleaseRenewMode.Renew)
279-
{
280-
const string command = @"ipconfig /renew";
294+
command += @"ipconfig /renew '" + adapterName + "';";
281295

282-
PowerShellHelper.ExecuteCommand(command);
283-
}
296+
if (mode == IPConfigReleaseRenewMode.ReleaseRenew6 || mode == IPConfigReleaseRenewMode.Release6)
297+
command += @"ipconfig /release6 '" + adapterName + "';";
298+
299+
if (mode == IPConfigReleaseRenewMode.ReleaseRenew6 || mode == IPConfigReleaseRenewMode.Renew6)
300+
command += @"ipconfig /renew6 '" + adapterName + "';";
301+
302+
PowerShellHelper.ExecuteCommand(command);
284303
}
285304

305+
/// <summary>
306+
/// Add an IP address to a network interface asynchronously.
307+
/// </summary>
308+
/// <param name="config">Ethernet adapter name, IP address and subnetmask.</param>
309+
/// <returns>Running task.</returns>
286310
public static Task AddIPAddressToNetworkInterfaceAsync(NetworkInterfaceConfig config)
287311
{
288312
return Task.Run(() => AddIPAddressToNetworkInterface(config));
289313
}
290314

315+
/// <summary>
316+
/// Add an IP address to a network interface.
317+
/// </summary>
318+
/// <param name="config">Ethernet adapter name, IP address and subnetmask.</param>
291319
public static void AddIPAddressToNetworkInterface(NetworkInterfaceConfig config)
292320
{
293321
var command = @"netsh interface ipv4 add address '" + config.Name + @"' " + config.IPAddress + @" " + config.Subnetmask;
294322

295323
PowerShellHelper.ExecuteCommand(command, true);
296324
}
297325

326+
/// <summary>
327+
/// Remove an IP address from a network interface asynchronously.
328+
/// </summary>
329+
/// <param name="config">Ethernet adapter name, IP address</param>
330+
/// <returns>Running task.</returns>
298331
public static Task RemoveIPAddressFromNetworkInterfaceAsync(NetworkInterfaceConfig config)
299332
{
300333
return Task.Run(() => RemoveIPAddressFromNetworkInterface(config));
301334
}
302335

336+
/// <summary>
337+
/// Remove an IP address from a network interface.
338+
/// </summary>
339+
/// <param name="config">Ethernet adapter name, IP address</param>
303340
public static void RemoveIPAddressFromNetworkInterface(NetworkInterfaceConfig config)
304341
{
305342
var command = @"netsh interface ipv4 delete address '" + config.Name + @"' " + config.IPAddress;
306343

307344
PowerShellHelper.ExecuteCommand(command, true);
308345
}
309346
#endregion
310-
311-
#region Enum
312-
public enum IPConfigReleaseRenewMode
313-
{
314-
ReleaseRenew,
315-
Release,
316-
Renew
317-
}
318-
#endregion
319347
}
320348
}
Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
using System.Diagnostics;
22

3-
namespace NETworkManager.Utilities
3+
namespace NETworkManager.Utilities;
4+
5+
public static class PowerShellHelper
46
{
5-
public static class PowerShellHelper
7+
/// <summary>
8+
/// Execute a PowerShell command.
9+
/// </summary>
10+
/// <param name="command">Command to execute.</param>
11+
/// <param name="asAdmin">Start PowerShell as administrator. Error code 1223 is returned when UAC dialog is canceled by user.</param>
12+
/// <param name="windowStyle">Window style of the PowerShell console (Default: Hidden)</param>
13+
public static void ExecuteCommand(string command, bool asAdmin = false, ProcessWindowStyle windowStyle = ProcessWindowStyle.Hidden)
614
{
7-
public static void ExecuteCommand(string command, bool asAdmin = false, ProcessWindowStyle windowStyle = ProcessWindowStyle.Hidden)
15+
var info = new ProcessStartInfo()
816
{
9-
var info = new ProcessStartInfo()
10-
{
11-
FileName = "powershell.exe",
12-
Arguments = $"-NoProfile -NoLogo -Command {command}",
13-
UseShellExecute = true,
14-
WindowStyle = windowStyle
15-
};
17+
FileName = "powershell.exe",
18+
Arguments = $"-NoProfile -NoLogo -Command {command}",
19+
UseShellExecute = true,
20+
WindowStyle = ProcessWindowStyle.Normal //windowStyle
21+
};
1622

17-
if (asAdmin)
18-
info.Verb = "runas";
23+
if (asAdmin)
24+
info.Verb = "runas";
1925

20-
using var process = new Process();
26+
using var process = new Process();
2127

22-
process.StartInfo = info;
28+
process.StartInfo = info;
2329

24-
process.Start();
25-
process.WaitForExit();
26-
}
30+
process.Start();
31+
process.WaitForExit();
2732
}
2833
}

0 commit comments

Comments
 (0)