1

I'm trying to convert IP address to MAC address and then convert it to byte array. I'm stuck in the first part and not sure how to do this. I seen some search results talking about System.Net.NetworkInformation.NetworkInterface but not sure how to use it.

This is my code which needs MAC byte array. How to do this?

[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(int DestIP, int SrcIP, byte[] pMacAddr, ref uint PhyAddrLen);

private void Ping(IPAddress address)
{
    byte[] macAddr = new byte[6];
    uint macAddrLen = uint.Parse(macAddr.Length.ToString());

    if (SendARP(int.Parse(address.ToString()), 0, macAddr, ref macAddrLen) == 0)
    {
        //SUCCESS!
    }
}
HelpNeeder
  • 6,383
  • 24
  • 91
  • 155
  • 1
    What are you trying to do? MAC address is not accessible, except on the host machine, so I don't understand your question – CharlesB Jan 19 '12 at 15:16
  • @CharlesB, I'm trying to make [this](http://stackoverflow.com/a/8924588/720323) work. The problem is that my last variable macAddrLen is not assigned. – HelpNeeder Jan 19 '12 at 15:17
  • 2
    The MAC address of an adapter is only available on the same network segment. Not on the other side of a router. – spender Jan 19 '12 at 15:17

3 Answers3

6

Get about about what you try to do. It isakin to translate your phone number to the name of the street - there is NO correlation between them.

The MAC address is coded in the ethernet level driver while the IP address is an artificial construct higher up by the IP protocol. THey ahve ZERo relationship. Routers find the MAC addresses to send IP packets to via aprotocol (ARP - Address Resolution Protocol) and this can not cross network segments.

TomTom
  • 61,059
  • 10
  • 88
  • 148
4

Have a look here:

byte[] macAddr = new byte[6];
uint macAddrLen = (uint) macAddr.Length;
if (SendARP((int)address.Address, 0, macAddr, ref macAddrLen) != 0)
    throw new InvalidOperationException("SendARP failed.");

string[] str = new string[(int)macAddrLen];
for (int i = 0; i < macAddrLen; i++)
    str[i] = macAddr[i].ToString("x2");

Console.WriteLine(string.Join(":", str));
Strillo
  • 2,952
  • 13
  • 15
  • I'm trying this code but same as mine it causes error at if statement line: Input string was not in a correct format. You know why is that? – HelpNeeder Jan 19 '12 at 15:30
  • @HelpNeeder please see my edit. The error is in the call to int.Parse (converting the address to a string would return an IP in the form "x.x.x.x" which cannot be converted to int) – Strillo Jan 19 '12 at 15:33
  • The code doesn't work because you're trying to convert an IP to string and then parse that into an int -> int.Parse(address.ToString()). For the code to work you need an IPaddress object and then use it like this: (int)YourIPObject.Address – SanBen Jan 19 '12 at 15:40
  • @R34lthing, int.Parse(address.Address) doesn't quite cut it. I need int.Parse(address.Address.ToString()) so it would even compile. Now the problem is that my address range goes for example from 255.255.255.0 to 255.255.255.127 and then I get an error: Value was either too large or too small for an Int32. – HelpNeeder Jan 19 '12 at 16:28
  • Never mind. I have solved it to change type of my first parameter to string. – HelpNeeder Jan 19 '12 at 16:31
3

Actually the MAC Address is given to you. You do not need to know what MAC Address the Computer has. macAddr will hold the MAC address after you've called the function SendARP.

In the MSDN documentation you can see that the parameters macAddr and macAddrLen are marked as [out] which means the function uses this arguments to give you a value back.

The whole point of the Address Resolution Protocol is that you can resolve Network layer addresses (IP) to physical adresses (MAC)

SanBen
  • 2,581
  • 26
  • 35