📄 icmp.c
字号:
// ***************************************************************************// An 8051 Based Web Server// icmp.c: ICMP protocol processing// By Mason Kidd 3/14/02// ***************************************************************************#include <string.h>#include "packets.h"#include "ip.h"#include "icmp.h"void tx_icmp_packet(unsigned char tx_type, unsigned char *szData, unsigned char nLength){ unsigned char tx_buf[BUF_LEN]; struct ip_hdr *tx_ip_hdr = (struct ip_hdr *)(tx_buf + sizeof(struct eth_hdr)); struct icmp_hdr *tx_icmp_hdr = (struct icmp_hdr *)(tx_buf + sizeof(struct ip_hdr) + sizeof(struct eth_hdr)); unsigned char *icmp_data = (unsigned char *)(tx_buf + sizeof(struct eth_hdr) + sizeof(struct ip_hdr) + sizeof(struct icmp_hdr)); unsigned int *chksum_hdr = (unsigned int *)tx_icmp_hdr; int i; unsigned long chksum = 0; tx_icmp_hdr->type = tx_type; tx_icmp_hdr->sequence = 0x01; for (i = 0; i < nLength; i++) { *icmp_data = *szData; icmp_data++; szData++; } // Compute the checksum for (i = 0; i < 5; i++, chksum_hdr++) if (i != 1) chksum += *chksum_hdr; chksum = ~((chksum >> 16) + (chksum & 0xffff)); tx_icmp_hdr->checksum = (unsigned int)(chksum & 0xffff); tx_ip_packet(tx_buf, nLength + sizeof(struct icmp_hdr), IP_ICMP);}void rx_icmp_packet(unsigned char *rx_buffer){ struct ip_hdr *rx_ip_hdr = (struct ip_hdr *)(rx_buffer + sizeof(struct eth_hdr)); struct icmp_hdr *rx_icmp_hdr = (struct icmp_hdr *)(rx_buffer + sizeof(struct eth_hdr) + sizeof(struct ip_hdr)); unsigned char *pszData = (unsigned char *)(rx_buffer + sizeof(struct eth_hdr) + sizeof(struct ip_hdr) + sizeof(struct icmp_hdr)); unsigned int nDataLen = rx_ip_hdr->totlen - sizeof(struct icmp_hdr) - sizeof(struct ip_hdr); // if it's an echo request, send a reply if (rx_icmp_hdr->type == ICMP_ECHO) { // switch source and destination IP's memcpy(targetMAC, srcMAC, sizeof(unsigned char) * 6); memcpy(targetIP, srcIP, sizeof(unsigned char) * 4); tx_icmp_packet(ICMP_ECHOREPLY, pszData, nDataLen); } // else discard packet}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -