0

I'm totally out of C# hence hanging a little here. I stole the code from https://stackoverflow.com/a/13175574 to read out all adapter settings available on the pc. So far so good.

What I need now is a way to check, which of the adapters are able to connect to an attached device with a given ip address.

I'd like to have a function like "bool CheckIfValidIP(IPAddress adapter, IPAddress IPv4Mask, IPAddress address)".

Can you help me here? I know it's pretty trivial :-/

Edit:

    public static class IPAddressExtensions
{
    public static IPAddress GetNetworkAddress(this IPAddress address, IPAddress subnetMask)
    {
        byte[] ipAdressBytes = address.GetAddressBytes();
        byte[] subnetMaskBytes = subnetMask.GetAddressBytes();

        if (ipAdressBytes.Length != subnetMaskBytes.Length)
            throw new ArgumentException("Lengths of IP address and subnet mask do not match.");

        byte[] broadcastAddress = new byte[ipAdressBytes.Length];
        for (int i = 0; i < broadcastAddress.Length; i++)
        {
            broadcastAddress[i] = (byte)(ipAdressBytes[i] & (subnetMaskBytes[i]));
        }
        return new IPAddress(broadcastAddress);
    }

    public static bool IsInSameSubnet(IPAddress address2, IPAddress address, IPAddress subnetMask)
    {
        IPAddress network1 = address.GetNetworkAddress(subnetMask);
        IPAddress network2 = address2.GetNetworkAddress(subnetMask);

        return network1.Equals(network2);
    }
}

This code shall do it. Is it safe to use?

AllDayPiano
  • 414
  • 1
  • 4
  • 20
  • You really should show that you have put some effort into a solution & show your code. With a quick google search, I found this, is it what you are looking for? https://stackoverflow.com/questions/22575485/how-to-ping-using-a-specific-network-interface-or-a-specific-source-ip-address – PaulF Mar 03 '22 at 11:22

1 Answers1

0

A very simple example to get you in the right direction, this is created in .Net core 6:

using System.Net.NetworkInformation;
using System.Net.Sockets;

NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
var adaptertList = new List<NetworkInterface>();

const string IpAddressToValidate = "192.168.1.1"; //Both of these values can be retrieved from appsetting.json file
const string IpV4MaskToValidate = "255.255.255.0";

adaptertList = GetValidAdapterList(interfaces);

List<NetworkInterface> GetValidAdapterList(NetworkInterface[] interfaces) {
   var adaptertList = new List<NetworkInterface>();

   foreach (var adapter in interfaces) {
      var ipProps = adapter.GetIPProperties();

      foreach (var ip in ipProps.UnicastAddresses) {
         if ((adapter.OperationalStatus == OperationalStatus.Up)//Check for any conditions appropriate to your case
         && (ip.Address.AddressFamily == AddressFamily.InterNetwork)) {
            if (CheckIsValidIP(ip.Address.ToString(), ip.IPv4Mask.ToString(), IpAddressToValidate, IpV4MaskToValidate)) {
               adaptertList.Add(adapter);
               continue;//If IP found exit the loop, as the adapter is already added
            }
         }
      }
   }
   return adaptertList;
}

bool CheckIsValidIP(string ipAddress, string ipV4Mask, string validIpAddress, string validIpV4Mask) {
   return (ipAddress == validIpAddress && ipV4Mask == validIpV4Mask);
}

This code will return a list of all the adapter in the machine that meet the Ip address and ipv4mask criteria.

Armando Bracho
  • 659
  • 5
  • 21