📄 arp.c
字号:
#include "skbuff.h"
#include "eth.h"
#include "arp.h"
//#include "utils.h"
#define ARP_CACHE_SIZE 16
typedef struct arp_entry {
unsigned char ar_ha[ETH_ALEN];
Uint8 ar_ip[4];
} arp_entry;
static arp_entry arp_entrys;
static arp_entry arp_cache[ARP_CACHE_SIZE];
static int arp_chche_cur = 0;
int arp_init(void)
{
memset(&arp_entrys, 0, sizeof(arp_entry));
memset(arp_cache, 0, sizeof(arp_entry) * ARP_CACHE_SIZE);
return 0;
}
int arp_add_entry(unsigned char *ha, Uint8 *ip)
{
Uint32 *i,*j;
memcpy(arp_entrys.ar_ha, ha, ETH_ALEN);
j=(Uint32 *)ip;
*i= htonl(*j);
memcpy(arp_entrys.ar_ip,(Uint8 *)i,4);
return 0;
}
int arp_add_cache(unsigned char *ha, Uint8 *ip)
{
int i;
for (i = 0; i < ARP_CACHE_SIZE; i++)
{
if(ip[0]==arp_cache[i].ar_ip[0])
{
if(ip[1]==arp_cache[i].ar_ip[1])
{
if(ip[2]==arp_cache[i].ar_ip[2])
{
if(ip[3]==arp_cache[i].ar_ip[3])
{
break;
}
}
}
}
}
if (i == ARP_CACHE_SIZE)
{
i = arp_chche_cur;
arp_chche_cur = (arp_chche_cur + 1) % ARP_CACHE_SIZE;
}
memcpy(arp_cache[i].ar_ha, ha, ETH_ALEN);
memcpy(arp_cache[i].ar_ip,ip,4);
return 0;
}
int arp_send_req(Uint32 ip)
{
struct sk_buff skb;
struct arphdr *arp_req;
Uint32 i;
unsigned char broadcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
if(alloc_skb(&skb,ETH_HLEN + sizeof(struct arphdr))==-1)
{
return -1;
}
eth_skb_reserve(&skb);
arp_req = (struct arphdr *)skb_put(&skb, sizeof(struct arphdr));
i=htons(ARPHRD_ETHER);
arp_req->ar_hrd[0]=i&0x00ff;
i=(i>>8);
arp_req->ar_hrd[1]=i&0x00ff;
i=htons(ETH_P_IP);
arp_req->ar_pro[0]=i&0x00ff;
i=(i>>8);
arp_req->ar_pro[1]=i&0x00ff;
arp_req->ar_hln = 0x06;
arp_req->ar_pln = 0x04;
i=htons(ARPOP_REQUEST);
arp_req->ar_op[0] = i&0x00ff;
i=(i>>8);
arp_req->ar_op[1] = i&0x00ff;
memcpy((unsigned char *)arp_req->ar_sha, arp_entrys.ar_ha, ETH_ALEN);
memcpy(arp_req->ar_sip,arp_entrys.ar_ip,4);
memset((unsigned char *)arp_req->ar_tha, 0x00, ETH_ALEN);
i=htonl(ip);
arp_req->ar_tip[0]=i&0x000000ff;
i=(i>>8);
arp_req->ar_tip[1]=i&0x000000ff;
i=(i>>8);
arp_req->ar_tip[2]=i&0x000000ff;
i=(i>>8);
arp_req->ar_tip[3]=i&0x000000ff;
if(eth_send(&skb, broadcast, ETH_P_ARP)==-1)
{
free_skb(&skb);
return -1;
}
return 0;
}
int arp_send_rsp(struct arphdr *arp_hdr)
{
struct sk_buff skb;
struct arphdr *arp_rsp;
Uint32 i;
if(alloc_skb(&skb,ETH_HLEN + sizeof(struct arphdr))==-1)
{
return -1;
}
eth_skb_reserve(&skb);
arp_rsp = (struct arphdr *)skb_put(&skb, sizeof(struct arphdr));
i=htons(ARPHRD_ETHER);
arp_rsp->ar_hrd[0]=i&0x0ff;
i=(i>>8);
arp_rsp->ar_hrd[1]=i&0x0ff;
i=htons(ETH_P_IP);
arp_rsp->ar_pro[0]=i&0x00ff;
i=(i>>8);
arp_rsp->ar_pro[1]=i&0x00ff;
arp_rsp->ar_hln = 0x06;
arp_rsp->ar_pln = 0x04;
i=htons(ARPOP_REPLY);
arp_rsp->ar_op[0] = i&0x00ff;
i=i>>8;
arp_rsp->ar_op[1] = i&0x00ff;
memcpy((unsigned char *)arp_rsp->ar_sha, arp_entrys.ar_ha, ETH_ALEN);
memcpy(arp_rsp->ar_sip,arp_hdr->ar_tip,4);
memcpy((unsigned char *)arp_rsp->ar_tha, (unsigned char *)arp_hdr->ar_sha, ETH_ALEN);
memcpy(arp_rsp->ar_tip,arp_hdr->ar_sip,4);
if(eth_send(&skb, arp_hdr->ar_sha, ETH_P_ARP)==-1)
{
free_skb(&skb);
}
return 0;
}
int arp_get_eth_addr(Uint32 ip, unsigned char *ha)
{
int i;
Uint32 j,IPHtonl;
IPHtonl=htonl(ip);
for (i = 0; i < ARP_CACHE_SIZE; i++)
{
j=arp_cache[i].ar_ip[3];
j=j<<8;
j+=arp_cache[i].ar_ip[2];
j=j<<8;
j+=arp_cache[i].ar_ip[1];
j=j<<8;
j+=arp_cache[i].ar_ip[0];
if(IPHtonl == j)
{
memcpy(ha, arp_cache[i].ar_ha, ETH_ALEN);
break;
}
}
if (i == ARP_CACHE_SIZE)
{
arp_send_req(ip);
return -1;
}
return 0;
}
int arp_rcv_packet(struct sk_buff *skb)
{
struct arphdr *arp_hdr = (struct arphdr *)(skb->data);
Uint32 i;
if(arp_hdr->ar_tip[0]!=arp_entrys.ar_ip[0])
{
return -1;
}
if(arp_hdr->ar_tip[1]!=arp_entrys.ar_ip[1])
{
return -1;
}
if(arp_hdr->ar_tip[2]!=arp_entrys.ar_ip[2])
{
return -1;
}
if(arp_hdr->ar_tip[3]!=arp_entrys.ar_ip[3])
{
return -1;
}
i=htons(ARPOP_REQUEST);
if (arp_hdr->ar_op[0] == (i&0x00ff))
{
i=(i>>8);
if (arp_hdr->ar_op[1] == (i&0x00ff))
{
arp_send_rsp(arp_hdr);
}
}
arp_add_cache(arp_hdr->ar_sha, arp_hdr->ar_sip);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -