Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://www.binarytides.com/get-mac-address-from-ip-in-winsock/
- /*
- Author: Silver Moon ( m00n.silv3r@gmail.com )
- Find mac address of a given IP address using iphlpapi
- */
- #include "winsock2.h"
- #include "iphlpapi.h" //For SendARP
- #include "stdio.h"
- #include "conio.h"
- #pragma comment(lib , "iphlpapi.lib") //For iphlpapi
- #pragma comment(lib , "ws2_32.lib") //For winsock
- void GetMacAddress(unsigned char * , struct in_addr );
- int main()
- {
- unsigned char mac[6];
- struct in_addr srcip;
- char ip_address[32];
- WSADATA firstsock;
- if (WSAStartup(MAKEWORD(2,2),&firstsock) != 0)
- {
- printf("\nFailed to initialise winsock.");
- printf("\nError Code : %d" , WSAGetLastError() );
- return 1;
- }
- //Ask user to select the device he wants to use
- printf("Enter the ip address : ");
- scanf_s("%s",ip_address);
- srcip.s_addr = inet_addr(ip_address);
- //Get mac addresses of the ip
- GetMacAddress(mac , srcip);
- printf("Selected device has mac address : %.2X-%.2X-%.2X-%.2X-%.2X-%.2X",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
- printf("\n");
- _getch();
- return 0;
- }
- /*
- Get the mac address of a given ip
- */
- void GetMacAddress(unsigned char *mac , struct in_addr destip)
- {
- DWORD ret;
- IPAddr srcip;
- ULONG MacAddr[2];
- ULONG PhyAddrLen = 6; /* default to length of six bytes */
- int i;
- srcip = 0;
- //Send an arp packet
- ret = SendARP((IPAddr) destip.S_un.S_addr , srcip , MacAddr , &PhyAddrLen);
- //Prepare the mac address
- if(PhyAddrLen)
- {
- BYTE *bMacAddr = (BYTE *) & MacAddr;
- for (i = 0; i < (int) PhyAddrLen; i++)
- {
- mac[i] = (char)bMacAddr[i];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement