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

📄 icmp.c

📁 Coldfire MCF5282 DBug bootloader
💻 C
字号:
/*
 * File:		icmp.c
 * Purpose:		Handle ICMP packets.
 *
 * Notes:		See RFC 792 "Internet Control Message Protocol"
 *				for more details.
 *
 * Modifications:
 */


#include "src/include/dbug.h"
#include "src/uif/net/net.h"

/********************************************************************/
void
dump_icmp_frame (icmp_message *msg)
{
	switch (msg->type)
	{
		case ICMP_DEST_UNREACHABLE:
			printf("ICMP : ");
			switch (msg->code)
			{
				case ICMP_NET_UNREACHABLE:
					printf("Net Unreachable\n");
					break;
				case ICMP_HOST_UNREACHABLE:
					printf("Host Unreachable\n");
					break;
				case ICMP_PROTOCOL_UNREACHABLE:
					printf("Protocol Unreachable\n");
					break;
				case ICMP_PORT_UNREACHABLE:
					printf("Port Unreachable\n");
					break;
				case ICMP_FRAG_NEEDED:
					printf("Fragmentation needed and DF set\n");
					break;
				case ICMP_ROUTE_FAILED:
					printf("Source route failed\n");
					break;
				default:
					printf("Destination Unreachable\n");
					break;
			}
			break;
		case ICMP_TIME_EXCEEDED:
			printf("ICMP_TIME_EXCEEDED\n");
			break;
		case ICMP_PARAMETER_PROBLEM:
			printf("ICMP_PARAMETER_PROBLEM\n");
			break;
		case ICMP_SOURCE_QUENCH:
			printf("ICMP_SOURCE_QUENCH\n");
			break;
		case ICMP_REDIRECT:
			printf("ICMP_REDIRECT\n");
			break;
		case ICMP_ECHO:
			printf("ICMP_ECHO\n");
			break;
		case ICMP_ECHO_REPLY:
			printf("ICMP_ECHO_REPLY\n");
			break;
		case ICMP_INFORMATION_REQUEST:
			printf("ICMP_INFORMATION_REQUEST\n");
			break;
		case ICMP_INFORMATION_REPLY:
			printf("ICMP_INFORMATION_REPLY\n");
			break;
		case ICMP_TIMESTAMP:
			printf("ICMP_TIMESTAMP\n");
			break;
		case ICMP_TIMESTAMP_REPLY:
			printf("ICMP_TIMESTAMP_REPLY\n");
			break;
		default:
			printf("Unknown ICMP message\n");
			break;
	}
}

/********************************************************************/
void
icmp_handler (NIF *nif, NBUF *pNbuf, int hdr_offset)
{
	/*
	 * This function handles the ICMP packets.
	 */
	int i;
	NBUF *echo;
	icmp_message *rx_icmpmsg, *tx_icmpmsg;
	ip_frame_hdr *ipframe;

	rx_icmpmsg = (icmp_message *)&pNbuf->data[hdr_offset];

	switch (rx_icmpmsg->type)
	{
		case ICMP_ECHO:
			echo = nbuf_tx_allocate();
			if (echo == NULL)
			{
				#if defined(DEBUG_PRINT)
					printf("ICMP: couldn't allocate Tx buffer\n");
				#endif
				return;
			}
			tx_icmpmsg = (icmp_message *)&echo->data[ICMP_HDR_OFFSET];
			
			/* 
			 * Copy the Rx buffer to the Tx buffer 
			 */
			echo->length = pNbuf->length;
			for (i = ICMP_HDR_OFFSET; i < (echo->length + ICMP_HDR_OFFSET); i++)
				echo->data[i] = pNbuf->data[i];

			/* 
			 * Change ICMP echo message into a echo reply message 
			 */
			tx_icmpmsg->type = ICMP_ECHO_REPLY;
			tx_icmpmsg->code = rx_icmpmsg->code;
			tx_icmpmsg->msg.echo.identifier = rx_icmpmsg->msg.echo.identifier;
			tx_icmpmsg->msg.echo.sequence = rx_icmpmsg->msg.echo.sequence;
			tx_icmpmsg->msg.echo.data = rx_icmpmsg->msg.echo.data;
			
			/* 
			 * Recompute checksum 
			 */
			if (echo->length & 0x01)	/* odd */
			{
				((uint8 *)tx_icmpmsg)[echo->length] = 0x00;
				echo->length++;
			}
			tx_icmpmsg->chksum = 0;
			tx_icmpmsg->chksum = ip_chksum((uint16 *)tx_icmpmsg,echo->length);

			ipframe = (ip_frame_hdr *)&pNbuf->data[IP_HDR_OFFSET];
			ip_send (	nif,
						&ipframe->source_addr[0],	/* destination */
						&ipframe->dest_addr[0],		/* source */
						ipframe->protocol,
						echo) ;
			break;
		case ICMP_DEST_UNREACHABLE:
		case ICMP_TIME_EXCEEDED:
		case ICMP_PARAMETER_PROBLEM:
		case ICMP_SOURCE_QUENCH:
		case ICMP_REDIRECT:
		case ICMP_ECHO_REPLY:
		case ICMP_INFORMATION_REQUEST:
		case ICMP_INFORMATION_REPLY:
		case ICMP_TIMESTAMP:
		case ICMP_TIMESTAMP_REPLY:
		default:
			dump_icmp_frame(rx_icmpmsg);
			break;
	}
}

/******************************************************************/

⌨️ 快捷键说明

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