📄 icmp.c
字号:
#include "type.h"
#include "icmp.h"
#include "arp.h"
/*******************************/
static u16_t ip_len = 0;
static u8_t *ip_outbuf = NULL;
/******/
void ip_init(void)
{
ip_len = 0;
ip_outbuf = NULL;
}
// header hdr
/*
//IP header
__packed struct ip_hdr{
u8_t vhl, tos;
u16_t len, ipid, ipoffset;
u8_t ttl, proto;
u16_t ipchksum;
u32_t srcipaddr, destipaddr;
};
*/
u32_t ip_input(u8_t *buf, u16_t len)
{
struct ip_hdr *hdr = (struct ip_hdr*)buf;
if(hdr->vhl != 0x45) //IP version and header length
return -1;
len = HTONS(hdr->len);
//Check the fragment flag.
if(hdr->ipoffset != 0)
return -1;
//Check for ICMP packet
if(hdr->proto == PROTO_ICMP)
return icmp_input(buf, len);
return 0;
}
u32_t ip_send_packet(u8_t *buf, u16_t len)
{
ip_len = len;
ip_outbuf = buf;
return 0;
}
u16_t ip_getout_packet(u8_t **buf)
{
u16_t len = ip_len;
ip_len = 0;
*buf = ip_outbuf;
ip_outbuf = NULL;
return len;
}
/***********************/
/***********************/
//ICMP (echo) header.
/* __packed struct icmp_hdr{
struct ip_hdr iphdr;
u8_t type, icode;
u16_t icmpchksum;
u16_t id, seqno;
}; */
u32_t icmp_input(u8_t *buf, u16_t len)
{
u32_t tmp;
struct icmp_hdr *hdr = (struct icmp_hdr*)buf;
if(hdr->type != ICMP_ECHO)
return -1;
hdr->type = ICMP_ECHO_REPLY;
if(hdr->icmpchksum >= HTONS(0xffff - (ICMP_ECHO << 8)))
hdr->icmpchksum += HTONS(ICMP_ECHO << 8) + 1;
else
hdr->icmpchksum += HTONS(ICMP_ECHO << 8);
//Swap IP addresses.
tmp = hdr->iphdr.destipaddr;
hdr->iphdr.destipaddr = hdr->iphdr.srcipaddr;
hdr->iphdr.srcipaddr = tmp;
ip_send_packet(buf, len);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -