📄 ip.c
字号:
//-----------------------------------------------------------------------------
// IP
//-----------------------------------------------------------------------------
#include <string.h>
#include "tcpip_stack.h"
#include "hal_mac.h"
#include "ip.h"
#include "stdlib.h"
//-----------------------------------------------------------------------------
//Name:Ip_Sum()
//Funcionality: Calculates the ip header checksum
//-----------------------------------------------------------------------------
u16 Ip_Sum(u16 len_ip_header, u8 buff[])
{
u16 word16;
u32 sum=0;
u16 i;
for (i=0;i<len_ip_header;i=i+2){
word16 =((buff[i]<<8)&0xFF00)+(buff[i+1]&0xFF);
if (i!=10)sum = sum + (u32) word16;
}
while (sum>>16)
sum = (sum & 0xFFFF)+(sum >> 16);
sum = ~sum;
sum = ((sum >> 8) & 0x00FF) + ((sum << 8) & 0xFF00);
return ((u16) sum);
}
//-----------------------------------------------------------------------------
//Name:Rx_Ip_Frame()
//Funcionality: Checks IP address of the Received frame, compares chesksum,
//returns ip protocol if everything from above is ok
//-----------------------------------------------------------------------------
u8 Rx_Ip_Frame(EthernetFrame *frame)
{
ip_hdr *rx_ip_hdr = (ip_hdr *)((u8 *)frame + ETHER_HEADER_LENGTH);
u8 *chksum_hdr = (u8 *)rx_ip_hdr;
// Is the packet a broadcast or directly for me
if ((!memcmp(srcIP, &rx_ip_hdr->destIP, IP_ADDR_LEN)) || (!memcmp(brcastADD, &rx_ip_hdr->destIP, IP_ADDR_LEN)))
{
//Checksum comparison
if (Ip_Sum(IP_HEADER_LENGTH,chksum_hdr) == rx_ip_hdr->hdrchksum)
{
return rx_ip_hdr->protocol; //check sum IS OK and the packet is for me!!
}
else
return 0; //packet is for me but the checksum is bad
}
return 0; //the packet is not for me
}
//-----------------------------------------------------------------------------
//Name:Tx_Ip_Frame()
//Funcionality: enters a valid IP header values, calculates a checksum and
// calls the MAC driver.
//-----------------------------------------------------------------------------
void Tx_Ip_Frame(EthernetFrame *frame, u16 tx_length, u8 proto)
{
ether_hdr *tx_eth_hdr = (ether_hdr *)frame;
ip_hdr *tx_ip_hdr = (ip_hdr *)((u8 *)frame + ETHER_HEADER_LENGTH);
u8 *chksum_hdr = (u8 *)tx_ip_hdr;
u16 temp = tx_length + sizeof(ip_hdr);
temp = ((temp >> 8) & 0x00FF) + ((temp << 8) & 0xFF00);
//IP header
tx_ip_hdr->length = temp;
tx_ip_hdr->id += 0x1024;
tx_ip_hdr->ttl = 0x32;
tx_ip_hdr->version = 0x45;
tx_ip_hdr->protocol = proto;
memcpy(tx_ip_hdr->srcIP, srcIP, IP_ADDR_LEN);
memcpy(tx_ip_hdr->destIP, destIP, IP_ADDR_LEN);
memcpy(tx_eth_hdr->type, etherIP, ETHER_TYPE_LENGTH);
memcpy(tx_eth_hdr->src_mac, srcMAC, MAC_ADDR_LEN);
memcpy(tx_eth_hdr->dest_mac, destMAC, MAC_ADDR_LEN);
//checksum
chksum_hdr = (u8 *)tx_ip_hdr;
tx_ip_hdr->hdrchksum = Ip_Sum(IP_HEADER_LENGTH,chksum_hdr);
tx_length += IP_HEADER_LENGTH + ETHER_HEADER_LENGTH;
//Call MAC driver
MAC_Send(frame, tx_length);
}
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -