# Include "stdafx. H"
# Include <winsock2.h>
# Include <iphlpapi. h>
# Pragma comment (Lib, "iphlpapi. lib ")
# Pragma comment (Lib, "ws2_32.lib ")
Int main (INT argc, char * argv [])
{
Int ireturn;
DWORD dwip;
Byte bymac [6];
DWORD dwlen;
Wsadata;
Wsastartup (makeword (2, 0), & wsadata );
_ Try
{
Printf ("ip2mac ver 1.0 -- get MAC address from IP address though ARP request./n ");
If (argc! = 2)
{
Printf ("Usage: % s/n", argv [0]);
_ Leave;
}
Dwip = inet_addr (argv [1]);
If (dwip = inaddr_none)
{
Printf ("invalid IP Address: % S./N", argv [1]);
_ Leave;
}
// Send an ARP query packet to obtain the MAC address
Dwlen = 6;
Ireturn = sendarp (dwip, 0, (Pulong) & bymac, & dwlen );
If (ireturn! = No_error)
{
Printf ("error: send ARP request./NBE sure that connectting to % s need no gateway./N", argv [1]);
_ Leave;
}
Printf ("%. 2X-%. 2X-%. 2X-%. 2X-%. 2X-%. 2x/N ",
Bymac [0],
Bymac [1],
Bymac [2],
Bymac [3],
Bymac [4],
Bymac [5]);
}
_ Finally
{
Wsacleanup ();
}
Return 0;
}