Hand handle you to play ARP package (3) [One of the game series of packets]

zhaozj2021-02-16  53

Author:

9CBS VC / MFC Network Programming PiggyXP ^ _ ^

table of Contents:

two. Programming implementation of sending packets

Plip the packet

2. Send a packet

three. Some additional steps and instructions

1. If WinPCAP is used in VC

2. Get a list of network card information

3. Get list of system ARP information

................

Wen ..............

1.

Packing packet

Let me give an example of filling the header, I first define a function of a conversion character, as follows

/ ************************************************** ***********************************

* Name & params ::

* FormatStMac

*

* Const LPSTR LPHWADDRSTR: MAC address string entered by the user

* Unsigned char * hwaddr: Return to the MAC address string (assigned to the data package structure)

*)

* Purpose:

* Transfer the user-entered MAC address characters to the format required for the data packet structure

*********************************************************** ********************************** /

Void FormatStMac (Const LPSTR LPHWADDRSTR, Unsigned Char * Hwaddr)

{

Unsigned Int I, Index = 0, Value, Temp

UNSIGNED CHAR C;

_Strlwr (lphwaddrstr); // Convert to lowercase

For (i = 0; i

{

C = * (LPHWADDRSTR I);

IF ((c> = '0' && c <= '9') || (c> = 'a' && c <= 'f'))

{

IF (c> = '0' && c <= '9') TEMP = C - '0'; // Digital

IF (c> = 'a' && c <= 'f') TEMP = C - 'A' 0xA; // Letter

IF ((INDEX% 2) == 1)

{

Value = value * 0x10 TEMP;

HWADDR [index / 2] = value;

}

Else value = TEMP;

INDEX ;

}

IF (INDEX == 12) Break;

}

}

// Start populate each field

Arppacket ArppCket; / / Define Arppacket Structural Variables

MEMSET (& arpppacket, 0, sizeof (arppcket)); // Packet initialization

FormatSTRTOMAC ("DLC Source Mac String", arppitive); // DLC Framehead FormatSTRTMAC ("DLC Destination MAC String", arppckt.dlcHeader.Desmac);

FormatSTRTOMAC ("ARP Source Mac String", arppcket.arpframe.send_hw_addr); // Source Mac

Arppacket.arpframe.send_prot_addr = inet_addr (srcip); // Source IP

FormatStMac ("ARP destination MAC string", arppcket.arpframe.targ_hw_addr); // destination Mac

Arppacket.arpframe.targ_prot_addr = inet_addr (desip); // destination IP

Arppacket.arpframe.opcode = htons ((unsigned short) arptype; // ARP package type

// Automatic filling constant

Arppckt.dlcHeader.ethertype = HTONS ((unsigned short) 0x0806); // DLC Header Ethernet type

Arppacket.arpframe.hw_type = htons ((unsigned short) 1); // hardware type

Arppacket.arpframe.prot_type = htons ((unsigned short) 0x0800); // Upper layer protocol type

Arppacket.arpframe.hw_addr_len = (unsigned char) 6; // MAC address length

Arppacket.arpframe.prot_addr_len = (unsigned char) 4; // IP address length

That's all! ^ _ ^

After filling, what we need to do is to send our Arppacket structure.

2. Send an ARP packet:

We send an ARP package to use the WinPCAP API. The specific steps and functions are like this. For the sake of easy understanding, I will drop the wrong place to handle, see the code.

/ ************************************************** ********************

* Name & params ::

* Sendarppacket ()

* Purpose:

* Send an ARP packet

* Remarks:

* Used is WinPCAP API function

*********************************************************** ******************** /

Void Sendarppacket ()

{

Char * adapterdevicename = getcuradaptername (); // First get the NIC name

LPADAPTER = packetopenadapter (adapterDevicename); / / Open the network card according to the NIC name

LPPACKET = packetallocatePacket (); // Assign memory for Packet struct pointer

PacketinitPacket (LPPACKET, & ArppPacket, SizeOf (arpppacket)); // Initialize Packet Structures

// The arpppacket is our previously filled ARP package packetsetsetNumWrites (LPADAPTER, 1); / / only one package

PacketSendPacket (LPadapter, LPPACKET, TRUE) // Send !!!!! ^ _ ^

PacketFreePacket (LPPACKET); // Release Resources

PacketCloseadapter (LPADAPTER);

}

Oh, here, about the most critical part of the ARP package is finished, you can come to send your own ARP package as you wish.

Since it is a "popular articles", I will talk about additional steps and explanations related to the entire project.

three. Additional steps and description

1. How to use WinPCAP driver in VC

Although the WinPCAP development kit is very easy to use, the pre-preparation work is still costly, it is not possible. ^ _ ^

The first is to install its driver, you can download it to its homepage, update very fast

http://winpcap.polito.it/install/default.htm

Download WinPCAP Auto-Installer (DRIVER DLLS), just installed directly, or there is also the code package I have.

I hope that friends developed in WinPCAP will also download developer's pack, decompression.

Then, you need to set the additional container directory of our project to download the exclulux directory of the developer's pack development package, and the additional dependency library of the connector is set to the lib directory of the developer's Pack.

Of course, because our work is relatively simple, it is borrowed by WinPCAP, so only

In the include folder of the WinPCAP development package, copy packet32.h, to our project, and contain it

In

But pay attention to, packet32.h itself has a deviocTl.h, but also copies it, and of course there is

The library packet.lib, a total of three files need to be copied, if you don't need me to join the library, don't use it, set it in the project

Set, or add #pragma Comment (lib, "packet.lib") where it is needed.

The entire project can actually be divided into four parts, populate packets, send packets, enumerate system network card lists,

Related information and enumeration system ARP cache list, let me talk about how to get the system's network card and ARP column

Table, these two parts are used to use the IP Helper's API, so IPHLPAPI.H> and library file iPhlpapi.lib,

In fact, it is very simple, just use a few lines.

2. Enumerate the system network card and information

It is best to define a structure of the network card information, so that the structure is relatively clear.

// NIC information

Typedef struct tagadapterinfo

{

Char szdevicename [128]; //

Char szipaddrstr [16]; // ip

Char Szhwaddrstr [18]; // Mac

DWORD DWINDEX; // Number

INFO_ADAPTER, * PINFO_ADAPTER;

/ ************************************************** ******************** Name & params ::

* AddApinfotolist

*

* ClistCtrl & List: CARPPLAYERDLG Incoming list handle

*)

* Purpose:

* Get the network card information of the system and add it to the List control

* Remarks:

* Get the network card IP and MAC to use the Iphelper API GetAdaptersInfo

*********************************************************** **************** /

Void Addadapinfotolist (Clistctrl & List)

{

CHAR TEMPCHAR;

Ulong ulistsize = 1;

PIP_ADAPTER_INFO PADAPTER; / / Define PIP_ADAPTER_INFO Structure Storage Net Card Information

INT NADADAPTERINDEX = 0;

DWORD DWRET = GetAdaptersInfo (Pip_adapter_info) & Tempchar, & ULISTSIZE); / / Key Function

IF (dwret == error_buffer_overflow)

{

PIP_ADAPTER_INFO PADAPTERLISTBUFFER = (pip_adapter_info) New (char [ulistsize]);

DWRET = GetAdaptersInfo (PadapterListBuffer, & ULISTSIZE);

IF (dwret == error_success)

{

Padapter = padapterlistbuffer;

While (Padapter) // Enumerates the NIC and add relevant entries to list

{

// NIC name

CString Strtemp = Padapter-> Adaptername;

Strtemp = "// device // NPF_" strTemp; // plus prefix

List.insertitem (NADAPTERINDEX, STRTEMP);

STRCPY (AdapterList [NADAPTERINDEX] .szdeviceName, Strtemp);

// ip

STRCPY (AdapterList [NADAPTERINDEX] .szipaddrStr,

Padapter-> ipaddresslist.ipaddress.string;

List.setitemtext (NadapterIndex, 1, AdapterList [NADAPTERINDEX] .szipAddrstr);

// mac

FormatMactostr (AdapterList [NadapterIndex] .szhwaddrstr, Padapter-> Address;

List.SetItemText (NadapterIndex, 2, Adapterlis [NadapterIndex] .szhwaddrstr);

// NIC number

AdapterList [NadapterIndex] .dwindex = Padapter-> Index;

Padapter = Padapter-> Next; NadapterIndex ;

}

Delete PadapterlistBuffer;

}

}

}

2) Get list of ARP entries

// ARP entry information

Typedef struct tagarpinfo

{

Char szipaddrstr [16]; // ip

Char Szhwaddrstr [18]; // Mac

DWORD DWTYPE; // Type

} Info_arp, * pinfo_arp;

/ ************************************************** ********************

* Name & params ::

* Addarpinfotolist

*

* ClistCtrl & List: CARPPLAYERDLG Incoming list handle

* Const Short NadapterIndex: User Select Net Card Number

*)

* Purpose:

* Read in the system's ARP cache list, and add to the dialog

* Remarks:

* Used Iphelper API GetipnetTable

* And use the Winsock's API, so you want to include

*********************************************************** *************** /

Void Addarpinfotolist (ClistCtrl & List, Const Short NadapterIndex)

{

CHAR TEMPCHAR;

DWORD dwlistsize = 1;

DWORD DWRET;

IN_ADDR INADDR;

List.deleteallItems ();

DWRET = GetipnetTable (PMIB_IPNETTABLE) & TEMPCHAR, & DWLISTSIZE, TRUE); // Key Function

IF (dwret == error_INSUFFICIENT_BUFFER)

{

PMIB_IPNETTABLE PIPNETTABLE = (PMIB_IPNetTable) New (char [dwlistsize]);

DWRET = GetipnetTable (PipnetTable, & DWLISTSIZE, TRUE);

IF (dwret == error_success)

{

For (int i = 0; i <(int) pipnettable-> dwnumentries; i )

{

// ip

INADDR.S_UN.S_ADDR = PipnetTable-> Table [i] .dwaddr;

STRCPY (Arplist [i] .szipaddrStr, INET_NTOA (INADDR));

// mac

FormatMactostr (Arplist [i] .szhwaddrstr, PipnetTable-> Table [i] .bphysaddr);

// Type

Arplist [i] .dwtype = pipnettable-> Table [i] .dwtype;

IF (AdapterList [NADAPTERINDEX] .dwindex! = PipnetTable-> Table [i] .dwindex) Continue; List.InsertItem (i, arplist [i] .szipaddrStr);

List.SetItemtext (i, 1, arplist [i] .szhwaddrstr);

Switch (arplist [i] .dwtype) {// converted to a character display according to the value of Type

Case 3:

List.SetItemtext (i, 2, "dynamic");

Break;

Case 4:

List.SetItemtext (i, 2, "static");

Break;

Case 1:

List.SetItemtext (i, 2, "invalid");

DEFAULT:

List.SetItemtext (i, 2, "");

}

}

}

DELETE PIPNETTABLE;

}

}

In this way, we basically tell it, and some things are not talking here. You can download my code to see it.

Let's use the ARP to play some small tricks ^ _ ^.

(Endlessly)

. . . . . . . . . . . . . . . .

============================================================================================================================================================================================================= =====================

The first two parts of this article:

Teach you to play ARP package (on)

http://www.9cbs.net/develop/read_article.asp?id=28419

Teach you to play the ARP package (middle)

http://www.9cbs.net/develop/Article/28/28421.shtm

------ Finished AT 2004-05-30 14:41

------- Made in dLUT | DIP

转载请注明原文地址:https://www.9cbs.com/read-23716.html

New Post(0)