通过IP获取MAC地址例子(应用层)

  博客地址:http://home.cnblogs.com/u/zengjianrong/

  由于某种需求,需要获取某个ip的mac地址,在应用层实现例子如下代码。

  流程:1. 先遍历arp表,若存在对应mac地址,则取出并结束。否则继续。

     2. 构造arp包,发arp request,若收不到arp reply,则返回失败并结束。否则继续。

     3. 解析arp reply包,获取MAC,并将ip、mac信息存到内核的arp表。

#include <errno.h>
#include <net/if_arp.h>
#include <netinet/if_ether.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h> #define MAC_BCAST_ADDR (unsigned char *)"\xff\xff\xff\xff\xff\xff"
#define LOCAL_MAC_ADDR (unsigned char *)"\x00\x50\x56\xCC\x99\x0E"
#define LOCAL_DEV (unsigned char *)"eth0" #ifndef ATF_FORCE_ADD
#define ATF_FORCE_ADD 0x80 /* force to add an entry --add by zengjianrong */
#endif typedef unsigned int u_int32_t;
typedef unsigned int u_int; #ifdef ZJR_READ  /* just for reading */
typedef unsigned short sa_family_t;
struct sockaddr {
sa_family_t sa_family; /* address family, AF_xxx */
char sa_data[]; /* 14 bytes of protocol address */
};
struct sockaddr_in
{
sa_family_t sin_family; /* Address family */
__be16 sin_port; /* Port number */
struct in_addr sin_addr; /* Internet address */ /* Pad to size of `struct sockaddr'. */
unsigned char __pad[__SOCK_SIZE__ - sizeof(short int) -
sizeof(unsigned short int) - sizeof(struct in_addr)];
};
struct arpreq
{
struct sockaddr arp_pa; /* protocol address */
struct sockaddr arp_ha; /* hardware address */
int arp_flags; /* flags */
struct sockaddr arp_netmask; /* netmask (only for proxy arps) */
char arp_dev[];
};
#endif #define SYSTEM_ERROR 1
#define PROCESS_PRINT 0
#define DEBUGGING_LVL 0
#define DEBUGGING #ifdef DEBUGGING
#define DEBUG(lvl, ...)\
do\
{\
if (lvl >= DEBUGGING_LVL)\
{\
printf("[DEBUG] in %s:%d %s(), ",\
__FILE__, __LINE__, __FUNCTION__);\
printf(__VA_ARGS__);\
printf("\n");\
}\
} while()
#else
#define DEBUG(lvl, ...) do {} while(0)
#endif typedef enum
{
ARP_TBL_BEGIN = ,
ARP_TBL_GET,
ARP_TBL_SET,
ARP_TBL_DEL,
ARP_TBL_END,
}ARP_TBL_TRL_ENUM; struct arpMsg
{
struct ethhdr ethhdr; /* Ethernet header */
u_short htype; /* hardware type (must be ARPHRD_ETHER) */
u_short ptype; /* protocol type (must be ETH_P_IP) */
u_char hlen; /* hardware address length (must be 6) */
u_char plen; /* protocol address length (must be 4) */
u_short operation; /* ARP opcode */
u_char sHaddr[]; /* sender's hardware address */
u_char sInaddr[]; /* sender's IP address */
u_char tHaddr[]; /* target's hardware address */
u_char tInaddr[]; /* target's IP address */
u_char pad[]; /* pad for min. Ethernet payload (60 bytes) */
}; static int fd_arp_tbl = -; /******************************************************************************
* nSendArp - send arp, get mac from arp reply if have
* DESCRIPTION: -
* Input: unRemoteIp - remote ip
* unLocalIp - our ip
* pucLocalMac - our mac address
* pchEth - interface to use
* Output: pstArp - arp info
* Returns: 1 - addr free
* 0 - addr used, get mac ok
* -1 - error
*
* modification history
* --------------------
* 2.00, 2014-12-25 , zengjianrong written
* --------------------
******************************************************************************/
int nSendArp(u_int32_t unRemoteIp, u_int32_t unLocalIp, unsigned char *pucLocalMac, char *pchEth, struct arpMsg *pstArp)
{
int nTimeOut = ;
int nOptVal = ;
int nSk = -; /* socket */
int nRtVal = ; /* return value */
fd_set fdset;
time_t prevTime;
struct sockaddr addr; /* for interface name */
struct timeval tm; if (- == (nSk=socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))))
{
DEBUG(SYSTEM_ERROR, "func->socket error(%d:%s).", errno, strerror(errno));
return -;
} if (- == setsockopt(nSk, SOL_SOCKET, SO_BROADCAST, &nOptVal, sizeof(nOptVal)))
{
DEBUG(SYSTEM_ERROR, "func->setsocketopt error(%d:%s).", errno, strerror(errno));
close(nSk);
return -;
} /* make a arp request */
memset(pstArp, , sizeof(struct arpMsg));
memcpy(pstArp->ethhdr.h_dest, MAC_BCAST_ADDR, ); /* MAC DA */
memcpy(pstArp->ethhdr.h_source, pucLocalMac, ); /* MAC SA */
pstArp->ethhdr.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */
pstArp->htype = htons(ARPHRD_ETHER); /* hardware type */
pstArp->ptype = htons(ETH_P_IP); /* protocol type (ARP message) */
pstArp->hlen = ; /* hardware address length */
pstArp->plen = ; /* protocol address length */
pstArp->operation = htons(ARPOP_REQUEST); /* ARP op code */
*((u_int *) pstArp->sInaddr) = unLocalIp; /* source IP address */
memcpy(pstArp->sHaddr, pucLocalMac, ); /* source hardware address */
//*((u_int *) pstArp->tInaddr) = unRemoteIp;
memcpy(pstArp->tInaddr, (unsigned char *)&unRemoteIp, sizeof(pstArp->tInaddr));/* target IP address */
memset(&addr, , sizeof(addr));
strcpy(addr.sa_data, pchEth); /* send arp request */
if ( > sendto(nSk, pstArp, sizeof(struct arpMsg), , &addr, sizeof(addr)))
{
DEBUG(SYSTEM_ERROR, "func->sendto error(%d:%s).", errno, strerror(errno));
close(nSk);
return -;
} /* wait arp reply, and check it */
tm.tv_usec = ;
time(&prevTime);
while (nTimeOut > )
{
FD_ZERO(&fdset);
FD_SET(nSk, &fdset);
tm.tv_sec = nTimeOut;
if (select(nSk + , &fdset, (fd_set *) NULL, (fd_set *) NULL, &tm) < )
{
DEBUG(SYSTEM_ERROR, "func->select error(%d:%s).", errno, strerror(errno));
if (errno != EINTR)
{
nRtVal = ;
}
}
else if (FD_ISSET(nSk, &fdset))
{
if (recv(nSk, pstArp, sizeof(struct arpMsg), ) < )
{
nRtVal = ;
}
if (pstArp->operation == htons(ARPOP_REPLY) &&
bcmp(pstArp->tHaddr, pucLocalMac, ) == &&
*((u_int *) pstArp->sInaddr) == unRemoteIp)
{
//DEBUG(PROCESS_PRINT, "valid arp reply receved for this address.");
nRtVal = ;
break;
}
}
nTimeOut -= time(NULL) - prevTime;
time(&prevTime);
}
close(nSk);
DEBUG(PROCESS_PRINT, "%salid arp replies for this address", nRtVal ? "no v" : "v");
return nRtVal;
} /******************************************************************************
* nArpTblCtl - get/set/del ip<->mac info
* DESCRIPTION: -
* Input:
* Output:
* Returns:
*
* modification history
* --------------------
* 2.00, 2014-12-25 , zengjianrong written
* --------------------
******************************************************************************/
int nArpTblCtl(ARP_TBL_TRL_ENUM cmd, struct in_addr stRemoteIp, char *pcDevName, unsigned char *pucMac)
{
unsigned int unCmd = ;
unsigned char *pucHwAddr = NULL;
struct sockaddr_in *pSin = NULL;
struct arpreq stArpreq; if (( == stRemoteIp.s_addr)
|| (cmd <= ARP_TBL_BEGIN)
|| (cmd >= ARP_TBL_END)
|| (ARP_TBL_DEL != cmd && NULL == pucMac))
{
DEBUG(SYSTEM_ERROR, "param error.");
return -;
} memset(&stArpreq, , sizeof(struct arpreq));
switch (cmd)
{
case ARP_TBL_GET:
unCmd = SIOCGARP;
break;
case ARP_TBL_DEL:
stArpreq.arp_ha.sa_family = AF_UNSPEC;
unCmd = SIOCDARP;
break;
case ARP_TBL_SET:
stArpreq.arp_ha.sa_family = AF_UNSPEC;
//stArpreq.arp_flags = ATF_PERM | ATF_PUBL | ATF_FORCE_ADD;
stArpreq.arp_flags = ATF_COM;
memcpy((char *)stArpreq.arp_ha.sa_data, pucMac, );
unCmd = SIOCSARP;
break;
default:
return -;
} if (- == fd_arp_tbl)
{
fd_arp_tbl = socket(AF_INET, SOCK_DGRAM, );
if (fd_arp_tbl < )
{
DEBUG(SYSTEM_ERROR, "func->socket error(%d:%s).", errno, strerror(errno));
return -;
}
} pSin = (struct sockaddr_in *) &stArpreq.arp_pa;
pSin->sin_family = AF_INET;
memcpy(&(pSin->sin_addr), &stRemoteIp, sizeof(struct in_addr));
strcpy(stArpreq.arp_dev, pcDevName); if ( > ioctl(fd_arp_tbl, unCmd, &stArpreq))
{
DEBUG(SYSTEM_ERROR, "func->ioctl error(%d:%s).", errno, strerror(errno));
close(fd_arp_tbl);
fd_arp_tbl = -;
return -;
}
else if (ARP_TBL_GET == cmd)
{
pucHwAddr = (unsigned char *) stArpreq.arp_ha.sa_data;
memcpy(pucMac, pucHwAddr, );
if ( == pucMac[] && == pucMac[] && == pucMac[]
&& == pucMac[] && == pucMac[] && == pucMac[])
{
return -;
}
} return ;
} int main(int argc, char *argv[])
{
unsigned char aucMac[];
struct in_addr sin_remote_addr;
struct in_addr sin_local_addr;
struct arpMsg stArpMsg; memset(&sin_local_addr, , sizeof(struct in_addr));
memset(&stArpMsg, , sizeof(struct arpMsg));
memset(aucMac, , ); if (argc!=)
{
DEBUG(SYSTEM_ERROR, "usage: %s <IP address>\n",argv[]);
return -;
} /* initial global values */
fd_arp_tbl = -; if ( == (inet_aton("138.0.225.226", &sin_local_addr)))
{
DEBUG(SYSTEM_ERROR, "func->inet_aton error(%d:%s).", errno, strerror(errno));
return -;
} if ( == (inet_aton(argv[], &sin_remote_addr)))
{
DEBUG(SYSTEM_ERROR, "func->inet_aton error(%d:%s), '%s' not valid.",
errno, strerror(errno), argv[]);
return -;
} if ( > (nArpTblCtl(ARP_TBL_GET, sin_remote_addr, LOCAL_DEV, aucMac)))
{
DEBUG(PROCESS_PRINT, "no entry in arp_cache for '%s', send arp request.", argv[]);
if ( != (nSendArp(sin_remote_addr.s_addr, sin_local_addr.s_addr,
LOCAL_MAC_ADDR, LOCAL_DEV, &stArpMsg)))
{
DEBUG(SYSTEM_ERROR, "there is no device using '%s', or nSendArp error.", argv[]);
return -;
}
else
{
if ( > (nArpTblCtl(ARP_TBL_SET, sin_remote_addr, LOCAL_DEV, stArpMsg.sHaddr)))
{
DEBUG(SYSTEM_ERROR, "func->nArpTblCtl set error.");
return -;
}
if ( > (nArpTblCtl(ARP_TBL_GET, sin_remote_addr, LOCAL_DEV, aucMac)))
{
DEBUG(PROCESS_PRINT, "there is no device using '%s'.", argv[]);
return ;
}
}
}
DEBUG(PROCESS_PRINT, "%s-->%02x:%02x:%02x:%02x:%02x:%02x.", argv[],
aucMac[], aucMac[], aucMac[], aucMac[], aucMac[], aucMac[]); return ;
}

  测试结果如下:

[root@zeng test]# ifconfig
eth0 Link encap:Ethernet HWaddr :::CC::0E
inet addr:138.0.225.226 Bcast:138.0.255.255 Mask:255.255.0.0
inet6 addr: fe80:::56ff:fecc:990e/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (154.2 MiB) TX bytes: (17.9 MiB)
Interrupt: Base address:0x2024 eth1 Link encap:Ethernet HWaddr :::DE::0E
inet addr:192.168.123.225 Bcast:192.168.123.255 Mask:255.255.255.0
inet6 addr: fe80:::56ff:fede:990e/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (2.4 MiB) TX bytes: (1016.2 KiB)
Interrupt: Base address:0x20a4 eth2 Link encap:Ethernet HWaddr :::DC::0E
inet addr:192.168.213.225 Bcast:192.168.213.255 Mask:255.255.255.0
inet6 addr: fe80:::56ff:fedc:990e/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (324.9 MiB) TX bytes: (1.3 GiB)
Interrupt: Base address:0x2424 lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::/ Scope:Host
UP LOOPBACK RUNNING MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (54.7 KiB) TX bytes: (54.7 KiB) [root@zeng test]# ./getMacFromIp.o 138.0.0.90
[DEBUG] in /mnt/hgfs/source_code_20140312/test/2getMacFromIp.c: nArpTblCtl(), func->ioctl error(:No such device or address).
[DEBUG] in /mnt/hgfs/source_code_20140312/test/2getMacFromIp.c: main(), no entry in arp_cache for '138.0.0.90', send arp request.
[DEBUG] in /mnt/hgfs/source_code_20140312/test/2getMacFromIp.c: nSendArp(), valid arp replies for this address
[DEBUG] in /mnt/hgfs/source_code_20140312/test/2getMacFromIp.c: main(), 138.0.0.90-->:d4:::5b:7c.
[root@zeng test]# ./getMacFromIp.o 138.0.156.125
[DEBUG] in /mnt/hgfs/source_code_20140312/test/2getMacFromIp.c: nArpTblCtl(), func->ioctl error(:No such device or address).
[DEBUG] in /mnt/hgfs/source_code_20140312/test/2getMacFromIp.c: main(), no entry in arp_cache for '138.0.156.125', send arp request.
[DEBUG] in /mnt/hgfs/source_code_20140312/test/2getMacFromIp.c: nSendArp(), no valid arp replies for this address
[DEBUG] in /mnt/hgfs/source_code_20140312/test/2getMacFromIp.c: main(), there is no device using '138.0.156.125', or nSendArp error.
[root@zeng test]#
上一篇:[Android_项目]Android开源项目分类汇总


下一篇:c#获取本地IP和MAC地址