Browse Source

Correct minor issues in NetTools.cs

master
Starbeamrainbowlabs 5 years ago
parent
commit
41e2ab6396
1 changed files with 8 additions and 7 deletions
  1. +8
    -7
      PixelHub-Server/PixelHub/NetTools.cs

+ 8
- 7
PixelHub-Server/PixelHub/NetTools.cs View File

@ -1,6 +1,5 @@
using System;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.Sockets;
@ -11,9 +10,9 @@ namespace SBRL.Utilities
/// <summary>
/// case-insensitively gets the IPv4 index of the network interface with the given name.
/// </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>
public static int GetNetworkIndexByName4(string targetInterfaceName)
public static int GetNetworkIndexByName4 (string targetInterfaceName)
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface nic in nics)
@ -30,13 +29,13 @@ namespace SBRL.Utilities
/// </summary>
/// <param name="targetIP">The target IP to search for.</param>
/// <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();
foreach(NetworkInterface nic in nics)
foreach (NetworkInterface nic in nics)
{
IPInterfaceProperties ipProps = nic.GetIPProperties();
foreach(UnicastIPAddressInformation ip in ipProps.UnicastAddresses)
foreach (UnicastIPAddressInformation ip in ipProps.UnicastAddresses)
{
// Only bother with external addresses
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();
IPv4InterfaceProperties ip4Props = ipProps.GetIPv4Properties();