⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ip.c

📁 网络单片机 让嵌入式系统上网,基于keil C. 文件名:microweb-keil-0.1
💻 C
字号:
// ***************************************************************************// An 8051 Based Web Server// ip.c: IP protocol processing// By Mason Kidd 4/18/02// ***************************************************************************#include <string.h>#include "packets.h"#include "csio.h"unsigned char rx_ip_packet(unsigned char *rx_buffer){	struct eth_hdr *rx_eth_hdr = (struct eth_hdr *)rx_buffer;	struct ip_hdr *rx_ip_hdr = (struct ip_hdr *)(rx_buffer + sizeof(struct eth_hdr));	unsigned int *chksum_hdr = (unsigned int *)rx_ip_hdr;	int i;	unsigned long chksum = 0;		// Make sure that the packet is destined for me or for broadcast	if ((!memcmp(myIP, &rx_ip_hdr->destIP, sizeof(unsigned char) * 4)) || (!memcmp(myBCAST, &rx_ip_hdr->destIP, sizeof(unsigned char) * 4)))	{       	// Save the source MAC and IP for when I reply       	memcpy(srcMAC, &rx_eth_hdr->shost, sizeof(unsigned char) * 6);       	memcpy(srcIP, &rx_ip_hdr->srcIP, sizeof(unsigned char) * 4);				// Compute the checksum		for (i = 0; i < 10; i++, chksum_hdr++)			if (i != 5)				chksum += *chksum_hdr;		chksum = ~((chksum >> 16) + (chksum & 0xffff)); 		 		// packet is valid if they match 		if ((chksum & 0xffff) == rx_ip_hdr->hdrchksum)  		{			return rx_ip_hdr->proto;    	}     	else      		return 0;	}	return 0;}void tx_ip_packet(unsigned char *tx_buffer, unsigned char tx_length, unsigned char cProto){	struct eth_hdr *tx_eth_hdr = (struct eth_hdr *)tx_buffer;	struct ip_hdr *tx_ip_hdr = (struct ip_hdr *)(tx_buffer + sizeof(struct eth_hdr));	unsigned int *chksum_hdr = (unsigned int *)tx_ip_hdr;	int i;	unsigned long chksum = 0;    tx_ip_hdr->verIHL = 0x45;    tx_ip_hdr->totlen = tx_length + sizeof(struct ip_hdr);    tx_ip_hdr->TTL = 0x32;    tx_ip_hdr->proto = cProto;    	memcpy(tx_ip_hdr->srcIP, myIP, sizeof(unsigned char) * 4);	memcpy(tx_ip_hdr->destIP, targetIP, sizeof(unsigned char) * 4);		chksum_hdr = (unsigned int *)tx_ip_hdr;	// Compute the checksum	for (i = 0; i < 10; i++, chksum_hdr++)		if (i != 5)			chksum += *chksum_hdr;	chksum = ~((chksum >> 16) + (chksum & 0xffff));	tx_ip_hdr->hdrchksum = (unsigned int)(chksum & 0xffff);	tx_eth_hdr->type = ETHER_IP;	memcpy(tx_eth_hdr->shost, myMAC, sizeof(unsigned char) * 6);	memcpy(tx_eth_hdr->dhost, targetMAC, sizeof(unsigned char) * 6);	   	tx_packet(tx_buffer, tx_length);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -