Correct minor issues in NetTools.cs

This commit is contained in:
Starbeamrainbowlabs 2016-10-29 13:04:29 +01:00
parent 0e6967cd33
commit 41e2ab6396
1 changed files with 8 additions and 7 deletions

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
@ -11,9 +10,9 @@ namespace SBRL.Utilities
/// <summary> /// <summary>
/// case-insensitively gets the IPv4 index of the network interface with the given name. /// case-insensitively gets the IPv4 index of the network interface with the given name.
/// </summary> /// </summary>
/// <param name="interfaceName">The interface name to search for.</param> /// <param name="targetInterfaceName">The interface name to search for.</param>
/// <returns>The IPv4 index of the network interface with the given name.</returns> /// <returns>The IPv4 index of the network interface with the given name.</returns>
public static int GetNetworkIndexByName4(string targetInterfaceName) public static int GetNetworkIndexByName4 (string targetInterfaceName)
{ {
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface nic in nics) foreach (NetworkInterface nic in nics)
@ -30,13 +29,13 @@ namespace SBRL.Utilities
/// </summary> /// </summary>
/// <param name="targetIP">The target IP to search for.</param> /// <param name="targetIP">The target IP to search for.</param>
/// <returns>The network index with the given IP address.</returns> /// <returns>The network index with the given IP address.</returns>
public static int GetNetworkIndexByIP(IPAddress targetIP) public static int GetNetworkIndexByIP (IPAddress targetIP)
{ {
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach(NetworkInterface nic in nics) foreach (NetworkInterface nic in nics)
{ {
IPInterfaceProperties ipProps = nic.GetIPProperties(); IPInterfaceProperties ipProps = nic.GetIPProperties();
foreach(UnicastIPAddressInformation ip in ipProps.UnicastAddresses) foreach (UnicastIPAddressInformation ip in ipProps.UnicastAddresses)
{ {
// Only bother with external addresses // Only bother with external addresses
if (ip.Address.AddressFamily != AddressFamily.InterNetwork) if (ip.Address.AddressFamily != AddressFamily.InterNetwork)
@ -48,9 +47,11 @@ namespace SBRL.Utilities
} }
} }
} }
throw new Exception($"Error: Can't find network interface with the ip {targetIP}.");
} }
public static int GetIPv4Index(this NetworkInterface nic) public static int GetIPv4Index (this NetworkInterface nic)
{ {
IPInterfaceProperties ipProps = nic.GetIPProperties(); IPInterfaceProperties ipProps = nic.GetIPProperties();
IPv4InterfaceProperties ip4Props = ipProps.GetIPv4Properties(); IPv4InterfaceProperties ip4Props = ipProps.GetIPv4Properties();