魅力博客

魅力Linux|魅力空间|魅力博客|学习Linux|ubuntu日记|电脑教程|手机软件

Linux 下获取LAN中指定IP的网卡的MAC(物理地址)




// all.h
// 2005/06/20,a.m. wenxy
#ifndef _ALL_H
#define _ALL_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
// ---------------------
// 创建ARP包的头文件
#include
#include
/* #include */
#include
#include
// ---------------------
#include
#include
#include
using namespace std;
#define MAX_MAC_LEN 24 // MAC字串缓冲区的最大长度(byte)
#define COMPART_MAC ":" // MAC字串的分格符, Warnning:修改此宏,必须再修改源程序!!!
#endif /* end _ALL_h */
// end file
// -------------------------------------------------------------------------------------------------------------
// main.cpp
// 2005/06/20,a.m. wenxy
#include "all.h"
// function declare
static string GetLocalMac(string & strEth); // get loacl NIC's MAC
void set_ip_addr(char *,char *); // 填充IP
void set_hw_addr(char buf[], char *str); // 填充MAC
static string GetMacByIP(string strSrcIP, string strSrcMAC, string strDesIP ,string strNIC); // 获取指定IP的MAC
/*
#define SRC_IP "10.0.1.77" // 源IP
#define DES_IP "10.0.1.35" // 目的IP
#define LOCAL_HW "00:C0:4C:39:0D:6F" // 10.0.1.77的eth0的MAC
#define DEVICE "eth0" // 接口
*/
#define PADD_MAC "00:00:00:00:00:00" // 填充的MAC
#define DES_MAC "FF:FF:FF:FF:FF:FF" // 广播MAC
#define ARP_SEND_COUNT 3 // 发送ARP请求的ARP包的个数
struct ether_header
{
unsigned char ether_dhost[6]; /* destination eth addr */
unsigned char ether_shost[6]; /* source ether addr */
unsigned short ether_type; /* packet type ID field */
};
struct arp_header
{
unsigned short int ar_hrd; /* Format of hardware address. */
unsigned short int ar_pro; /* Format of protocol address. */
unsigned char ar_hln; /* Length of hardware address. */
unsigned char ar_pln; /* Length of protocol address. */
// -------------------------
unsigned short int ar_op; /* ARP opcode (command). */
unsigned char __ar_sha[6]; /* Sender hardware address. */
unsigned char __ar_sip[4]; /* Sender IP address. */
unsigned char __ar_tha[6]; /* Target hardware address. */
unsigned char __ar_tip[4]; /* Target IP address. */
// -------------------------
};
struct arp_packet
{
struct ether_header ethhdr;
struct arp_header arphdr;
unsigned char padding[18]; /* filled with 0 */
};

/* arp reply:
* op = 2
* ethhdr.ether_dhost = arphdr.__ar_tha = switch hard addr
* ethhdr.ether_shost = arphdr.__ar_sha = local hard addr
* arphdr.__ar_tip = switch ip
* arphdr.__ar_sip = victim ip
*/
#define FRAME_TYPE 0x0806 /* arp=0x0806,rarp=0x8035 */
#define HARD_TYPE 1 /* ethernet is 1 */
#define PROTO_TYPE 0x0800 /* IP is 0x0800 */
#define OP_CODE 1 /* arp=1/2,1为请求,2为应答,rarp=3/4 */

// linux下获取LAN里指定IP的网卡MAC
// In: argv[1]:本机IP,argv[2]:目的IP
// Out:
int main( int argc, char *argv[] )
{
string strETH; // 本机NIC名称
string strLocalMAC; // 本机MAC
string strSrcIP; // 本机IP
string strDesMAC; // 目的MAC
string strDesIP; // 目的IP

// 检查参数
if ( argc != 4 )
{
printf("Useage: get_mac [interface name of the IP] [IP] [ARP IP]\n\n");
return 0;
}
strETH = argv[1]; //"eth0";
strSrcIP = argv[2]; //"10.0.1.77";
strDesIP = argv[3]; //"10.0.1.69";

printf("Run ......\n");
printf("获取 %s 接口的MAC ......\n", strETH.c_str());
// 获取指定NIC名称的MAC
strLocalMAC = GetLocalMac(strETH);
#if 0
printf("Note: %s[%s]\n", strETH.c_str(), strLocalMAC.c_str());
#endif

// 获取指定接口MAC
if ( 0 == strcmp( (const char*)strLocalMAC.c_str(), (const char*)"") )
{
printf("Error: call strcmp() failed\n");
printf("--------------------------------\n\n");
return -1;
}
else
{
printf("获取接口MAC成功: %s [%s]\n", strETH.c_str(), strLocalMAC.c_str());
}

// 获取指定IP网卡的MAC
strDesMAC = GetMacByIP(strSrcIP, strLocalMAC, strDesIP, strETH);
printf("strDesMAC = %s\n", strDesMAC.c_str());
//
return 0;
}
// 获取本地某网卡的MAC
// In: strEth
// Out: 若成功,返回MAC字符串,失败,返回""(空串)
static string GetLocalMac(string & strEth)
{
string strLocalMAC;
int s;
struct ifreq buffer;
char chBuff[MAX_MAC_LEN];

memset(chBuff, 0x0, sizeof(chBuff));
//arp_process(NULL);
s = socket(PF_INET, SOCK_DGRAM, 0);
if (-1 == s)
{
printf("Error: create socket failture\n");
printf("--------------------------------\n\n");
return "";
}
memset(&buffer, 0x00, sizeof(buffer));
strcpy(buffer.ifr_name, strEth.c_str()); // "eth0"
if ( -1 == ioctl(s, SIOCGIFHWADDR, &buffer))
{
printf("Error: 获取接口 %S MAC 失败\n", strEth.c_str());
printf("--------------------------------\n\n");
return "";
}
close(s);

for( s = 0; s 0)
{
printf("发送ARP请求包[%d Bytes]...... [第%d个]\n", sizeof(arp), nSendCount);
if( sendto(sockfd, &arp, sizeof(arp), 0, (struct sockaddr*) &sa, sizeof(sa)) = 60 && 2 == ntohs(arpRes.arphdr.ar_op) )
{
char chBuff[MAX_MAC_LEN];
string strTarIP; /* Target IP address */
// 格式化IP
for (int s = 0; s = 'a' && c = 'a' && c


返回顶部

发表评论:

Powered By Z-BlogPHP 1.7.3


知识共享许可协议
本作品采用知识共享署名 3.0 中国大陆许可协议进行许可。
网站备案号粤ICP备15104741号-1