使用WindowsAPI发送ARP请求获得MAC地址 佟强 2008.10.9
- //SendArp.cpp
- #include <stdio.h>
- #include <winsock2.h>
- #include <iphlpapi.h>
- #pragma comment(lib,"ws2_32")
- #pragma comment(lib,"Iphlpapi")
- int main(int argc,char* argv[]){
- if(argc!=2){
- fprintf(stderr,"Usage: %s inet_addr/n",argv[0]);
- return -1;
- }
- ULONG targetIP = inet_addr(argv[1]);
- if(targetIP==INADDR_NONE){
- fprintf(stderr,"Invalid IP: %s/n",argv[1]);
- return -1;
- }
- ULONG macBuf[2];
- ULONG macLen = 6;
- //填什么IP都可以,Windows不处理这个参数
- ULONG localIP = inet_addr("192.168.1.222");
- DWORD retValue = SendARP(targetIP,localIP,macBuf,&macLen);
- if(retValue!=NO_ERROR){
- fprintf(stderr,"SendARP error/n");
- return -1;
- }
- unsigned char *mac = (unsigned char*)macBuf;
- printf("%s --> ",argv[1]);
- for(int i=0; i<macLen; i++){
- printf("%.2X",mac[i]);
- if(i!=macLen-1){
- printf("-");
- }
- }
- printf("/n");
- return 0;
- }