Teach you how to switch ARP packets (2) [one of the game series of data packets]

Source: Internet
Author: User

Http://blog.csdn.net/piggyxp/archive/2004/05/31/19607.aspx

Author:

Csdn VC/MFC Network Programming piggyxp pai_^     Directory:

IBasic knowledge about ARP

3. ARP package Filling

II. Programming and Implementation of Data Packet sending

1. fill data packets

......................... Next ............................

3. ARP Package Filling1) Fill in the request package: for example, if the MAC address of our computer is aa-aa and the IP address is 192.168.0.1, we want to query the MAC address of 192.168.0.99, what should I do? First, fill in the DLC header. Through the previous learning, we know that if you want to know the MAC address of a computer, broadcast is sent to the whole network. Receiver MacIt must be ffffffffff, Sender MACOf course we are myself, so our DLC header is filled, and the bold value is the value we need to manually enter (of course, my program is intelligent, it will automatically fill in some fields based on the type of ARP packet you selected. When you use it, you will know ^_^ ).
DLC Header
Field Length (byte) Fill Value
Receiver Mac 6 Ffffffffff
Sender MAC 6 Aaaaaaaaaaaa
Ethertype 2 Zero X 0806
Figure 3 the content of the DLC header in the ARP request packet follows the ARP frame. Operation CodeOf course it is 1, Sender's MacAnd IPOf course fill in our own, and pay attention to, here's Receiver IP AddressFill in the IP address we want to query, that is, 192.168.0.99, and Receiver MacEnter any value. This parameter does not work. Therefore ,,
ARP Frame
Field Length (byte) Fill Value
Hardware type 2 1
Upper-layer protocol type 2 0800
MAC address Length 1 6
IP address Length 1 4
Operation Code 2 1
Sender MAC 6 Aaaaaaaaaaaa
Sender IP 4 192.168.0.1
Receiver Mac 6 Any value: xxxxxxxxxxxx
Receiver IP Address 4 192.168.0.99
Fill data 18 0
Figure 4 content of the ARP frame in the ARP request packet if we construct such a packet and send it out, if 192.168.0.99 exists and is active, we will immediately receive a response packet sent from 192.168.0.99. We can check our ARP cache list to see if there is an additional entry similar to this: 192.168.0.99 BB-BB is amazing? Let's take a look at the construction of ARP response packets. 2) Response package FillingWith the above detailed explanation, you will surely be able to say the filling method of the response package by yourself, so I will not elaborate on it, for example, you can send an ARP response packet to 192.168.0.99 (the Mac is BB-BB, tell it that our MAC address is aa-aa, so fill in each field.
DLC Header
Field Length (byte) Fill Value
Receiver Mac 6 Bbbbbbbbbbbb
Sender MAC 6 Aaaaaaaaaaaa
Ethertype 2 Zero X 0806
Figure 5 DLC header content in ARP response packet
ARP Frame
Field Length (byte) Fill Value
Hardware type 2 1
Upper-layer protocol type 2 0800
MAC address Length 1 6
IP address Length 1 4
Operation Code 2 2
Sender MAC 6 Aaaaaaaaaaaa
Sender IP 4 192.168.0.1
Receiver Mac 6 Bbbbbbbbbbbb
Receiver IP Address 4 192.168.0.99
Fill data 18 0
Figure 6 the content of the ARP frame in the ARP response packet. In this way, the IP address ing about 192.168.0.1 will be added to the ARP cache of 192.168.0.99. Now, it's time for programming to implement it. ^_^

Ii. Programming Implementation of ARP packet sending

  1. Fill data packets

The above tables about the various fields of the ARP packet correspond to the struct in the program, corresponding to the above table, so we need three such struct

// DLC Header

Typedef struct tagdlcheader

{

Unsigned char desmac [6];/* destination HW addrress */

Unsigned char srcmac [6];/* Source HW addresss */

Unsigned short ethertype;/* Ethernet type */

} Dlcheader, * pdlcheader;

// ARP Frame

Typedef struct tagarpframe

{

Unsigned short hw_type;/* hardware address */

Unsigned short prot_type;/* Protocol address */

Unsigned char hw_addr_len;/* length of hardware address */

Unsigned char prot_addr_len;/* length of Protocol address */

Unsigned short opcode;/* arp/RARP */

 

Unsigned char send_hw_addr [6];/* sender hardware address */

Unsigned long send_prot_addr;/* sender Protocol address */

Unsigned char targ_hw_addr [6];/* target hardware address */

Unsigned long targ_prot_addr;/* Target Protocol address */

Unsigned char padding [18];

} Arpframe, * parpframe;

// ARP packet = DLC header + ARP Frame

Typedef struct tagarppacket

{

Dlcheader;

Arpframe;

} Arppacket, * parppacket;

These structs must be able to understand. In the program, it is enough to sit on the right signs.

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

========================================================== ========================================== It's so fast and full, I don't know how many copies I need to split .. -_-B, please look forward to the following :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.