資源簡(jiǎn)介
arp(地址解析協(xié)議)實(shí)例示范, 含發(fā)送和接收,在linux下跑通,帶makefile,可用來(lái)學(xué)習(xí)理解arp協(xié)議

代碼片段和文件信息
//?arp.cpp?:?定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include??/*?需要里面的?glibc?版本號(hào)?*/
#include?
#if?__GLIBC__?>=?2?&&?__GLIBC_MINOR?>=?1
#include?
#include??/*?鏈路層(L2)協(xié)議?*/
#else
#include?
#include?
#include??/*?鏈路層協(xié)議?*/
#endif
#include?
/**
以太網(wǎng)的頭部結(jié)構(gòu):
struct?ether_header
{
u_int8_t?ether_dhost[ETH_ALEN];??????//?destination?eth?addr
u_int8_t?ether_shost[ETH_ALEN];??????//?source?ether?addr???
u_int16_t?ether_type;?????????????????//?packet?type?ID?field
}?__attribute__?((__packed__));
整個(gè)以太網(wǎng)的頭部包括:?目的地址(6字節(jié)),源地址(6字節(jié)),類型(2字節(jié)),幀內(nèi)數(shù)據(jù)(46-1500個(gè)字節(jié)),CRC校驗(yàn)和(4字節(jié))
#define?ETH_ALEN?6?//以太網(wǎng)地址的長(zhǎng)度
#define?ETH_HALEN?14?//以太網(wǎng)頭部的總長(zhǎng)度??(6+6+2)
#define?ETH_ZLEN?60?//不含CRC校驗(yàn)數(shù)據(jù)的數(shù)據(jù)最小長(zhǎng)度(46+14)
#define?ETH_DATA_LEN?1500??//幀內(nèi)數(shù)據(jù)的最大長(zhǎng)度
#define?ETH_frame_LEN?1514//不含CRC最大以太網(wǎng)長(zhǎng)度(1500+14)
ARP頭部信息:
struct?arphdr{
__be16?ar_hrd;//硬件類型?1-硬件接口為以太網(wǎng)接口
__be16?ar_pro;//協(xié)議類型-0x0800高層協(xié)議為IP協(xié)議?
unsigned?char?ar_hln;//硬件地址長(zhǎng)度-6字節(jié)?MAC
unsigned?char?ar_pln;//協(xié)議地址長(zhǎng)度-4字節(jié)為IP
__be16?ar_op;//ARP操作碼-1?ARP請(qǐng)求
}
ARP協(xié)議數(shù)據(jù)結(jié)構(gòu):
struct?ether_arp{
struct?arphdr?ea_hdr;?//ARPfixed-size?header(ARP固定大小的報(bào)頭)
u_char?arp_sha[ETHER_ADDR_LEN];?//sender?hardware?address(發(fā)送端硬件地址)
u_char?arp_spa[4];?//sender?protocol?address(發(fā)送端協(xié)議地址)
u_char?arp_tha[ETHER_ADDR_LEN];?//?target?hardware?address(接收端硬件地址)
u_char?arp_tpa[4];?//target?protocol?address(接收端協(xié)議地址)
};
#define?arp_hrd?ea_hdr.ar_hrd
#define?arp_pro?ea_hdr.ar_pro
#define?arp_hln?ea_hdr.ar_hln
#define?arp_pln?ea_hdr.ar_pln
#define?arp_op?ea_hdr.ar_op
sockaddr_ll為設(shè)備無(wú)關(guān)的物理層地址結(jié)構(gòu),描述發(fā)送端的地址結(jié)構(gòu)
struct?sockaddr_ll
{
unsigned?short?sll_family;??總填?AF_PACKET?
unsigned?short?sll_protocol;?網(wǎng)絡(luò)序列的物理層協(xié)議號(hào)?0x806為ARP協(xié)議
int?sll_ifindex;??接口編號(hào)??eth0對(duì)應(yīng)的編號(hào)?
unsigned?short?sll_hatype;?頭部類型?ARPHRD_ETHER為以太網(wǎng)
unsigned?char?sll_pkttype;?包類型?PACKET_HOST
unsigned?char?sll_halen;??地址長(zhǎng)度??MAC地址長(zhǎng)度6字節(jié)
unsigned?char?sll_addr[8];物理地址?MAC地址只用了前面的6字節(jié)
};
FF:FF:FF:FF:FF:FF
SOCK_RAW原始套接字的分析:
(1)socket(AF_INETSOCK_RAWIPPROTO_TCP|IPPROTO_UDP|IPPROTO_ICMP);//發(fā)送或接收ip數(shù)據(jù)包得到原始的IP包
(2)socket(PF_PACKETSOCK_RAWhtons(ETH_P_IP|ETH_P_ARP|ETH_P_RAP|ETH_P_ALL));//發(fā)送或接收以太網(wǎng)數(shù)據(jù)幀
(1)使用第一種套接字類型,能得到發(fā)往本機(jī)的原始的IP數(shù)據(jù)包,但不能得到發(fā)往非本機(jī)的IP數(shù)據(jù)包,被過(guò)濾了,也不能得到從本機(jī)發(fā)出去的數(shù)據(jù)包。這類協(xié)議可自己組織TCP,ICMP,UDP數(shù)據(jù)包。
(2)第二種套接字能收到發(fā)往本地的MAC幀,也能收到從本機(jī)發(fā)出去的數(shù)據(jù)幀(第3個(gè)參數(shù)為ETH_P_ALL)能接收到非發(fā)往本地的MAC數(shù)據(jù)幀(網(wǎng)卡需要設(shè)置為promisc混雜模式)
協(xié)議類型:
ETH_P_IP?0X800?只接收發(fā)往本機(jī)的mac的ip類型的數(shù)據(jù)幀
ETH_P_ARP?0X806?只接收發(fā)往本機(jī)的arp類型的數(shù)據(jù)幀
ETH_P_RARP?0x8035?只接受發(fā)往本機(jī)的rarp類型的數(shù)據(jù)幀
ETH_P_ALL?0X3????接收發(fā)往本機(jī)的MAC所有類型iparprarp數(shù)據(jù)幀,接收從本機(jī)發(fā)出去的數(shù)據(jù)幀,混雜模式打開(kāi)的情況下,會(huì)接收到非發(fā)往本地的MAC數(shù)據(jù)幀
此時(shí)設(shè)備無(wú)關(guān)的物理地址使用struct?sockaddr_ll
從而得到MAC幀
**/
//發(fā)送ARP數(shù)據(jù),ARP協(xié)議結(jié)構(gòu)+以太網(wǎng)頭部
int?main(int?argc?char*argv[]
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????1159??2015-01-15?22:27??ReadMe.txt
?????目錄???????????0??2017-02-16?15:39??arp-recv\
?????文件????????2275??2015-01-18?11:17??arp-recv\arp_recv.cpp
?????文件?????????906??2015-01-18?11:28??arp-recv\Makefile
?????目錄???????????0??2017-02-16?15:39??arp-send\
?????文件????????3069??2015-01-20?21:11??arp-send\arp_send.cpp
?????文件?????????906??2015-01-15?23:44??arp-send\Makefile
?????文件???????15092??2015-01-15?23:18??arp
?????文件????????8309??2015-01-15?23:18??arp.cpp
?????文件?????????871??2015-01-15?22:27??arp.sln
?????文件????????4111??2015-01-20?23:58??arp.vcproj
?????文件???????11071??2015-01-16?00:11??arp_send
評(píng)論
共有 條評(píng)論