最近用winpcap編程發(fā)現(xiàn)winpcap不能直接的提取本地網(wǎng)卡的mac地址.
???? 于是通過(guò)向自己發(fā)arp請(qǐng)求來(lái)獲得本地的mac.自己寫了段代碼,如下:
int
?getmmac()
{???
????unsigned? char ???sendbuf[ 42 ];
???? int ????i = 7 ,k;
????ETHDR??eth;
????ARPHDR?arp;
???? struct ?pcap_pkthdr? * ??pkt_header;
????u_char? * ?pkt_data;?
???? for (k = 0 ;k < 6 ;k ++ )
????{
????????eth.eh_dst[k] = 0xff ;
????????eth.eh_src[k] = 0x0f ;
????????arp.arp_sha[k] = 0x0f ;
????????arp.arp_tha[k] = 0x00 ;
????}
????eth.eh_type = htons(ETH_ARP);
????arp.arp_hdr = htons(ARP_HARDWARE);
????arp.arp_pro = htons(ETH_IP);
????arp.arp_hln = 6 ;
????arp.arp_pln = 4 ;
????arp.arp_opt = htons(ARP_REQUEST);
????arp.arp_tpa = myip -> ip;
????arp.arp_spa = inet_addr( " 127.0.0.2 " );
????memset(sendbuf, 0 , sizeof (sendbuf));
????memcpy(sendbuf, & eth, sizeof (eth));
????memcpy(sendbuf + sizeof (eth), & arp, sizeof (arp));
???? if (pcap_sendpacket(slecadopt,sendbuf, 42 ) == 0 )
????{
????????printf( " PacketSend?succeed\n\n " );
????}
???? else
????{
????????printf( " PacketSendPacket?in?getmine?Error:?%d\n " ,GetLastError());
???????? return ? 0 ;
????}
????
???? while ((k = pcap_next_ex(slecadopt, & pkt_header,( const ?u_char ** ) & pkt_data)) >= 0 )
????{???????
???????? if ( * (unsigned? short ? * )(pkt_data + 12 ) == htons(ETH_ARP) &&* (unsigned? short * )(pkt_data + 20 ) == htons(ARP_REPLY) &&* (unsigned? long * )(pkt_data + 38 ) == inet_addr( " 127.0.0.2 " ))
????????{
????????????
???????????? for (i = 0 ;i < 6 ;i ++ )
????????????{
????????????????myip -> mac[i] =* (unsigned? char * )(pkt_data + 22 + i);
????????????}
???????????????? break ;
????????}
????}
???? if (i == 6 )
????{
???????? return ? 1 ;
????}
???? else
????{
???????? return ? 0 ;
????}
}
{???
????unsigned? char ???sendbuf[ 42 ];
???? int ????i = 7 ,k;
????ETHDR??eth;
????ARPHDR?arp;
???? struct ?pcap_pkthdr? * ??pkt_header;
????u_char? * ?pkt_data;?
???? for (k = 0 ;k < 6 ;k ++ )
????{
????????eth.eh_dst[k] = 0xff ;
????????eth.eh_src[k] = 0x0f ;
????????arp.arp_sha[k] = 0x0f ;
????????arp.arp_tha[k] = 0x00 ;
????}
????eth.eh_type = htons(ETH_ARP);
????arp.arp_hdr = htons(ARP_HARDWARE);
????arp.arp_pro = htons(ETH_IP);
????arp.arp_hln = 6 ;
????arp.arp_pln = 4 ;
????arp.arp_opt = htons(ARP_REQUEST);
????arp.arp_tpa = myip -> ip;
????arp.arp_spa = inet_addr( " 127.0.0.2 " );
????memset(sendbuf, 0 , sizeof (sendbuf));
????memcpy(sendbuf, & eth, sizeof (eth));
????memcpy(sendbuf + sizeof (eth), & arp, sizeof (arp));
???? if (pcap_sendpacket(slecadopt,sendbuf, 42 ) == 0 )
????{
????????printf( " PacketSend?succeed\n\n " );
????}
???? else
????{
????????printf( " PacketSendPacket?in?getmine?Error:?%d\n " ,GetLastError());
???????? return ? 0 ;
????}
????
???? while ((k = pcap_next_ex(slecadopt, & pkt_header,( const ?u_char ** ) & pkt_data)) >= 0 )
????{???????
???????? if ( * (unsigned? short ? * )(pkt_data + 12 ) == htons(ETH_ARP) &&* (unsigned? short * )(pkt_data + 20 ) == htons(ARP_REPLY) &&* (unsigned? long * )(pkt_data + 38 ) == inet_addr( " 127.0.0.2 " ))
????????{
????????????
???????????? for (i = 0 ;i < 6 ;i ++ )
????????????{
????????????????myip -> mac[i] =* (unsigned? char * )(pkt_data + 22 + i);
????????????}
???????????????? break ;
????????}
????}
???? if (i == 6 )
????{
???????? return ? 1 ;
????}
???? else
????{
???????? return ? 0 ;
????}
}
寫的是一個(gè)函數(shù),其中arp請(qǐng)求的發(fā)送方地址填的是127.0.0.2.這個(gè)都沒(méi)什么關(guān)系.
當(dāng)然提取本地的mac還可以用api直接獲取.但是不知道用pcap還有別的好方法.高手指教.謝謝