Home > Back-end >  Capture the ARP packet error when determining if a data link Ethernet, debugging said had untreated
Capture the ARP packet error when determining if a data link Ethernet, debugging said had untreated

Time:09-25

#include
# include
# include
# include
# include "pcap. H"
# include
# pragma comment (lib, "ws2_32. Lib")
# pragma comment (lib, "wpcap. Lib")
# pragma comment (lib, "packet. Lib")//at the bottom of the API, used to direct access to the driver function
using namespace std;

Struct arppkt
{
Unsigned short hdtype;//hardware type
Unsigned short protype;//protocol type
Unsigned short hdsize;//hardware address length
Unsigned short prosize;//protocol address length
Unsigned short op;//operation type
U_char smac [6].//the source MAC address
U_char sip [4];//the source IP address
U_char 10-channel dmac [6].//destination MAC address
U_char dip [4].//destination IP address
};

Void packet_handler (const pcap_pkthdr * header, const u_char * pkt_data, ostream& Out)
{
//found in the ARP packet head position
Arppkt * arph=(arppkt *) (pkt_data + 14);
//output source IP address
for(int i=0; i<3; I++)
OutOut. Setf (ios: : left);
OutOut. Unsetf (ios: : left);
//output source MAC address
Char oldfillchar=out. The fill (' 0 ');
Out. Setf (ios: : uppercase);
for(int i=0; i<5; I++)
OutOutOut. The fill (oldfillchar);
Out. Unsetf (ios: : hex | ios: : uppercase);
//output destination IP address
for(int i=0; i<3; I++)
OutOut. Unsetf (ios: : left);
OutOut. Unsetf (ios: : left);
//output destination MAC address
Out. The fill (' 0 ');
Out. Setf (ios: : uppercase);
for(int i=0; i<5; I++)
OutOut. The fill (oldfillchar);
Out. Unsetf (ios: : hex | ios: : uppercase);
//output operations type
Out//output operations time
* ltime struct tm;
Time_t local_tv_sec;
Local_tv_sec=header - & gt; Ts. Tv_sec;
Ltime=localtime (& amp; Local_tv_sec);
Out. The fill (' 0 ');
OutOut. The fill (oldfillchar);
Out}

Void main (int arg c, char * argv [])//command line parameter
{
//check the input command format
/* if (arg c!
=2){
Cout<" Both Please input command: ParseArp output_file "& lt;
return;
} */
//initialize network equipment related parameters of
Pcap_if_t * alldevs;//used to store the adapter information
Pcap_if_t * d;//equipment
Pcap_t * adhandle;
Int I=0, inum;
Char errbuf [PCAP_ERRBUF_SIZE + 1]="ERR".//store error message
U_int netmask.
Char packet_filter []="Mr Proto \ \ arp";
Struct bpf_program fcode;
Struct pcap_pkthdr * header;//packet storage file pointer
Const u_char * pkt_data;//pointer to the packet content, including the agreement, can be calculated to obtain the location of the IP packet header, the position of the UDP header,
//access network equipment list
If (pcap_findalldevs (& amp; Alldevs, errbuf)==1)
{
Cout<" The Error in pcap_find all devs;" return;
}
//print the list
For (d=alldevs; d; D=d - & gt; Next)
{
Printf (" % d % s ", + + I, d - & gt; Name);
If (d - & gt; The description)
Printf (" \ n (% s) ", d - & gt; The description);
The else
Printf (" (no description available) \ n ");
}
If (I==0)
{
Printf (" \ n't find network interface! To determine whether Winpcap installation. \ n ");
return ;
}
//select an Ethernet nic
The scanf (" % d ", & amp; Inum);
//pcap_freealldevs (alldevs);
If (inum & lt; 1 | | inum & gt; I)
{
Printf (" \ nInterface number out of range. \ n ");
//release equipment list
Pcap_freealldevs (alldevs);
return ;
}


//jump to the selected adapter
For (d=alldevs, I=0; i//open the equipment
//nic set to promiscuous mode, receive all frames
If ((adhandle=pcap_open_live (d - & gt; The name, 1000,1,300, errbuf))=NULL)
{
Cout<" \ n Unable to open the adapter. ";
Pcap_freealldevs (alldevs);
return;
}
//check whether data link for Ethernet;
/* check the data link layer, for the sake of simplicity, we only consider the Ethernet */

if (pcap_datalink (adhandle)!=DLT_EN10MB)
{
Fprintf (stderr, "nThis program works only on Ethernet networks. N");
*//* release device list
Pcap_freealldevs (alldevs);
return ;
}
Printf (" datalink: [% d] n, "pcap_datalink (adhandle));
If (d - & gt; Addresses!=NULL)
/* get the interface of the first address mask */
//get the subnet mask
Netmask=((struct sockaddr_in *) (d - & gt; Addresses - & gt; Netmask)) - & gt; Sin_addr. S_un. S_addr;
The else
/* if the interface is no address, then we assume that a class C */mask
Netmask=0 XFFFFFF;

//compile filter, can only capture the ARP packet
If (pcap_compile (adhandle, & amp; Fcode packet_filter, 1, netmask) & lt; 0)
{
Cout<" \ n Unable to compile the packet filter. Check the syntax. \ n ";
Pcap_freealldevs (alldevs);
return;
}
//set filters
If (pcap_setfilter (adhandle, & amp; Fcode) & lt; 0)
{
Cout<" \ nError setting the filter. \ n ";
Pcap_freealldevs (alldevs);
return;
}
//show the message and the meaning of each
Cout<" Listening on "& lt; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related