Home > OS >  Asked the great spirit, grows a new one, I used the c language implementation ARP protocol implement
Asked the great spirit, grows a new one, I used the c language implementation ARP protocol implement

Time:10-11

#include
#include
#include
#include
#include
#include
#include


# pragma comment (lib, "Ws2_32. Lib")
# pragma comment (lib, "wpcap. Lib")
using namespace std;

# define HARDWARE_TYPE 0 x0001
# define PROTOCOL_TYPE 0 x0800
# define CAOZUO_REQ 0 x0001
# define CAOZUO_REP 0 x0002
# define PHY_TYPE 0 x8086
# define MACIPLEN 0 x0604
# define ARP_PAD,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 {0}
# define BROADMAC {0 XFF, 0 XFF, 0 XFF, 0 XFF, 0 XFF, 0 XFF}
# define ETH_HRD_DEFAULT {BROADMAC,0,0,0,0,0 {0}, htons (PHY_TYPE)}
# define ARP_HRD_DEFAULT {htons (HARDWARE_TYPE), htons (PROTOCOL_TYPE), htons (MACIPLEN), htons (CAOZUO_REQ),,0,0,0,0,0} {0, 0,,0,0,0,0,0} {0, 0, ARP_PAD}

Struct ethernet_head
{
U_char dest_mac [6].
U_char sour_mac [6].
U_short eth_type;
};

Struct arp_head
{
U_short hard_type;
U_short proto_type;
U_short maciplen;
U_short caozuo;
U_char sour_addr [6].
U_long sour_ip;
U_char dest_addr [6].
U_long dest_ip;
U_char padding [18].
};

Struct arp_packet
{
Ethernet_head eth.
Arp_head arp.
};


//build message
U_char * enArpReqPack (u_char * source_mac u_char * destine_mac, u_long source_ip, u_long destine_ip, int packetsize)
{
Arp_packet arpPackStru={ETH_HRD_DEFAULT ARP_HRD_DEFAULT};
U_char * arpStruToStr=(u_char *) malloc (sizeof (u_char *) * packetsize);
Memset (arpStruToStr, 0, sizeof (arpStruToStr));
Memcpy (arpStruToStr, & amp; ArpPackStru. Eth. Dest_mac, 6);
Memcpy (source_mac arpStruToStr + 6, 6);
Memcpy (arpStruToStr + 12, & amp; ArpPackStru. Eth. Eth_type, 2);
Memcpy (arpStruToStr + 14, & amp; ArpPackStru. Arp. Hard_type, 2);
Memcpy (arpStruToStr + 16, & amp; ArpPackStru. Arp. Proto_type, 2);
Memcpy (arpStruToStr + 18, & amp; ArpPackStru. Arp. Maciplen, 2);
Memcpy (arpStruToStr + 20, & amp; ArpPackStru. Arp. Caozuo, 2);
Memcpy (arpStruToStr + 22, source_mac, 6);
Memcpy (arpStruToStr + 28, & amp; Source_ip, 4);
Memcpy (arpStruToStr + 32, & amp; ArpPackStru. Arp. Dest_addr, 6);
Memcpy (arpStruToStr + 38, & amp; Destine_ip, 4);
Return arpStruToStr;
}

//get the local MAC address
U_char * getSelfMac (char * pDevName, u_long chLocalIP)
{
Int GotMac=0;
Pcap_t * pAdaptHandle;
Char errbuf [PCAP_ERRBUF_SIZE];
If ((pAdaptHandle=pcap_open_live (pDevName, 60,1,100, errbuf))==NULL)
{
Printf (" \ n't need adapter \ n ");
return NULL;
}
Printf (" 3-1 the done \ n ");
Struct pcap_pkthdr * header;
Const u_char * pkt_data;
Int res;
U_short arp_option;
U_long arp_yuanip=0;
U_long arp_muip=0;
U_char arp_yuanmac [6].
U_char source_mac [6]=,0,0,0,0,0 {0};
U_char * packetforself;
Packetforself=enArpReqPack (source_mac source_mac, chLocalIP, chLocalIP, 60).
Printf (" 3-2 done \ n ");
while(! GotMac)
{
Pcap_sendpacket (pAdaptHandle packetforself, 60);
Sleep (10);
Res=pcap_next_ex (pAdaptHandle, & amp; The header, & amp; Pkt_data);
//printf (" 3-3 done \ n ");
If (res==0) continue;
Printf (" 3-4 done \ n ");
Memcpy (& amp; Arp_option pkt_data + 20, 2);
Memcpy (arp_yuanmac and pkt_data + 22, 6);
Memcpy (& amp; Arp_yuanip pkt_data + 28, 4);
Memcpy (& amp; Arp_muip pkt_data + 38, 4);
If (arp_option==htons (CAOZUO_REP) & amp; & Arp_muip==chLocalIP& & Arp_yuanip==chLocalIP)
{
GotMac=1;
Pcap_close (pAdaptHandle);
Return arp_yuanmac;
}
Sleep(100);
}
Pcap_close (pAdaptHandle);
Return arp_yuanmac;
}


//receive packets callback function

Void packet_handler (u_char * param, const struct pcap_pkthdr * header, const u_char * pkt_data)
{
Printf (" DONE \ n ");
* ltime struct tm;
Char timestr [16].
Time_t local_tv_sec;
Strftime (timestr, sizeof (timestr), "% H % M % S", ltime);
Printf (" % s ", timestr);
U_char mudimac [6].
U_long mudiip;
Memcpy (mudimac, & amp; Pkt_data + 22, 6);
Memcpy (& amp; Mudiip, & amp; Pkt_data + 28, 4);
Printf (" % s... % d \ n ", mudimac, mudiip);

}

Int main ()
{
Pcap_if_t * alldevs;
Pcap_if_t * d;
U_char * LOCALMAC;
U_long LOCALIP;
U_long destineIP;
U_char * arp_for_request;
U_char mudimac [6]=,0,0,0,0,0 {0};
Int iresult;
int i=0;
Int inum;
U_int netsize;
Pcap_t * adhandle;
Char errbuf [PCAP_ERRBUF_SIZE];
U_int netmask.
Char * filter="Mr Proto \ \ arp";//filter rules
Struct bpf_program fcode;//filter the results
//get the local equipment list
If (pcap_findalldevs (& amp; Alldevs, errbuf)==1)
{
Fprintf (stderr, "using the function error, cannot find the equipment list of % s \ n", errbuf);
return -1;
}
//display device list
For (d=alldevs; d; D=d - & gt; Next)
{
Printf (" % d % s ", + + I, d - & gt; Name);
If (d - & gt; The description)
{
Printf (" % s \ n ", d - & gt; The description);
}
The else
Printf (" no introduction \ n ");
}
If (I==0)
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related