views:

575

answers:

4

Is there a way to resolve the mac address off the default gateway using c#?

update im working with

var x = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].GetIPProperties().GatewayAddresses; 

but I feel like I'm missing something.

+1  A: 

You'll probably need to use P/Invoke and some native Win API functions.

Have a look at this tutorial.

Tony
+1  A: 

Something like this should work for you, although you probably want to add more error checking:

[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(uint destIP, uint srcIP, byte[] macAddress, ref uint macAddressLength);

public static byte[] GetMacAddress(IPAddress address)
{
  byte[] mac = new byte[6];
  uint len = (uint)mac.Length;      
  byte[] addressBytes = address.GetAddressBytes();      
  uint dest = ((uint)addressBytes[3] << 24) 
    + ((uint)addressBytes[2] << 16) 
    + ((uint)addressBytes[1] << 8) 
    + ((uint)addressBytes[0]);      
  if (SendARP(dest, 0, mac, ref len) != 0)
  {
    throw new Exception("The ARP request failed.");        
  }
  return mac;
}
Iceman
+2  A: 

What you really want is to perform an Adress Resolution Protocol (ARP) request. There are good and not soo good ways to do this.

  • Use an existing method in the .NET framework (though I doubt it exists)
  • Write your own ARP request method (probably more work than you're looking for)
  • Use a managed library (if exists)
  • Use an unmanaged library (such as iphlpapi.dll as suggested by Kevin)
  • If you know you only need remote to get the MAC adress of a remote Windows computer within your network you may use Windows Management Instrumentation (WMI)

WMI example:

using System;
using System.Management;

namespace WMIGetMacAdr
{
    class Program
    {
        static void Main(string[] args)
        {
            ManagementScope scope = new ManagementScope(@"\\localhost");  // TODO: remote computer (Windows WMI enabled computers only!)
            //scope.Options = new ConnectionOptions() { Username = ...  // use this to log on to another windows computer using a different l/p
            scope.Connect();

            ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_NetworkAdapterConfiguration"); 
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

            foreach (ManagementObject obj in searcher.Get())
            {
                string macadr = obj["MACAddress"] as string;
                string[] ips = obj["IPAddress"] as string[];
                if (ips != null)
                {
                    foreach (var ip in ips)
                    {
                        Console.WriteLine("IP address {0} has MAC address {1}", ip, macadr );
                    }
                }
            }
        }
    }
}
Christian Vik
+1 for a nice explanation and a good technique (as long as the gateway is running Windows).
Iceman
A: 

wireshark..... it's a beautiful thing

Yes
"using c#" is a requirement
Crash893