Well, before I said so much, finally arrived, we can fencing the time.
Before programming the ARP protocol, we must be able to control the network adapter (NIC), this part is the driver! "We're going to write the NIC driver?" "Yes, but at least we don't need it at this stage. There are well-written drivers and development kits on the network that we can use to get them. I'm using WinPcap's driver and development package, so we'll start by installing and building our programming environment:
1, download WinPcap driver. If you are installing theWireshark, it will automatically help us to install the WinPcap drive. (Wireshark was developed based on WinPcap) Http://www.winpcap.org/install/default.htm This is the WinPcap website download page, I downloaded is 4.1.3 version of the. Once downloaded to the local, double-click the installation directly.
2. Download WinPcap Development PackageHttp://www.winpcap.org/devel.htm This is the development of the WINPCAP official website download, mainly its version to be consistent with the driver version. After downloading to the local, unzip it. There are WinPcap libraries, header files, and examples and usage documents.
Http://www.ferrisxu.com/WinPcap/This is a description of the use of the WinPcap correlation function (Chinese)
http://www.winpcap.org/docs/docs_412/html/main.html English explanation
3, environment configuration. VS Environment Settings 3.1 There are two ways to import header files to vs2012, both of which are possible, but I prefer the 2nd, because there is a risk of a source pool in the 1th (1) Directly put ... \wpdpack\include\ (this directory is the 2nd step in the development package after the decompression directory) All files are copied to vs2012 's include directory (2) in the VS2012 configuration: Project Properties--Configuration Properties-->c/c++----general; Right "additional Include directories" in this option add the path,
3.2 Import Libraries to VSThere are two ways, both of which are possible, but I prefer the 2nd because there is a risk of a source pool in the 1th
(1) directly to the \wpdpack\lib\ (this directory is the 2nd step in the development package after the decompression directory) all the files are copied to the vs2012 lib directory
(2) Configure in vs2012: (i) Item Properties--Configuration Properties--add to the right side of the linker--the additional library directory, in this option, add the path, (ii)Item Properties--Configuration Properties--add dependencies to the right side of the linker--Enter the library:
3.3 Add compilation Options WIN32 If you follow the steps above, compile, return: Pcap/pcap.h can't find <system/time.h> We open pcap/pcap.h This file, you can see: there should be no WIN32 definition, so only To add a definition like this,
4. Test:Our environment is basically configured, so let's test it and see if it works.
The following code is from: http://www.winpcap.org/docs/docs_412/html/group__wpcap__tut1.html, the main function is to obtain all the network adapter (NIC) on the machine.
In the implementation, you have to add #define HAVE_REMOTE this macro, because PCAP_FINDALLDEVS_EX () This function is defined in the remote-ext.h, and to use this function correctly, you need to have the definition of this macro. (Readers can enter pcap.h to find remote-ext.h this file contains)
#define Have_remote#include "Pcap.h" void Main () { pcap_if_t *alldevs;//adapter list, which is a data structure of a linked list pcap_if_t *d; Save an adapter int i=0; Char errbuf[pcap_errbuf_size]; Get a list of local devices /* Retrieve the device list from the local machine * /if (PCAP_FINDALLDEVS_EX (pcap_src_if_string, NULL /* Auth is not needed */, &alldevs, errbuf) = =-1) { fprintf (stderr, "Error in PCAP_FINDALLDEVS_EX:%s\n", err BUF); Exit (1); } /* Print the list * /for (d= Alldevs; d! = NULL; d= d->next) { printf ("%d.%s", ++i, d->name); if (d->description) printf ("(%s) \ n", d->description); else printf ("(No description available) \ n"); } if (i = = 0) { printf ("\nno Interfaces found! Make sure WinPcap is installed.\n "); return; } /* We don ' t need any more the device list. Free It * /Pcap_freealldevs (Alldevs);}
Here are some key code to make a brief description of the other detailed instructions and use,You can refer to WinPcap's technical documentation.
pcap_if_t *alldevs;//Adapter list, which is a data structure of a linked list//find network adapter, and save the results in Alldevs pcap_findalldevs_ex (pcap_src_if_string, NULL/* Auth is not needed */, &alldevs, ERRBUF)//After use to release Pcap_freealldevs (Alldevs);
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
ARP Protocol (3) ARP programming--winpcap&vs2012 configuration