icmp.c

来自「2410 boot loader,usb ftp」· C语言 代码 · 共 102 行

C
102
字号
/*	Copyright 2001-2004 Georges Menie (www.menie.org)	This file is part of Tftpnaive.    Tftpnaive is free software; you can redistribute it and/or modify    it under the terms of the GNU Lesser General Public License as published by    the Free Software Foundation; either version 2 of the License, or    (at your option) any later version.    Tftpnaive is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU Lesser General Public License for more details.    You should have received a copy of the GNU Lesser General Public License    along with Tftpnaive; if not, write to the Free Software    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*/#include "tftpnaive.h"#include "net.h"#include "baselib.h"#include "timer.h"void processICMP (unsigned char *pkt, unsigned short len){	register ICMPpkt *p = (ICMPpkt *) pkt;	if (len >= sizeof (ICMPpkt)) {		if (cks (&p->op, len - sizeof (IPpkt)) == 0) {			// Translate network data to host data			p->op = ntoh(p->op); 	 			switch (p->op & 0xff00) {			case ICMP_ECHO_REQUEST:				if (netif.ip) {	/* reply only if we have our IP address */					memcpy (p->iphdr.ehdr.dst_hwadr, p->iphdr.ehdr.src_hwadr,							6);					memcpy (p->iphdr.ehdr.src_hwadr, netif.IEEEIA, 6);					p->iphdr.ehdr.frametype = hton(p->iphdr.ehdr.frametype);					p->iphdr.vlt = hton(p->iphdr.vlt);					p->iphdr.length = hton(p->iphdr.length);					p->iphdr.id = hton(p->iphdr.id);					p->iphdr.frag = hton(p->iphdr.frag);					p->iphdr.dst_ip = hton4(p->iphdr.src_ip);					p->iphdr.src_ip = hton4(netif.ip);					p->iphdr.cks = 0;					p->iphdr.cks =						cks (&p->iphdr.vlt,							 sizeof (IPpkt) - sizeof (EthHeader));					p->op = hton(ICMP_ECHO_REPLY);					p->cks = 0;					p->cks = cks (&p->op, len - sizeof (IPpkt));					netif.send (p, len);				}				break;#if TFTPNAIVE_TIMEREQUEST			case ICMP_TIMESTAMP_REPLY: {					extern int timestamp;					if ((((ICMPTSpkt *) p)->tra) < 86400000)						timestamp = ((ICMPTSpkt *) p)->tra;				}				break;#endif			case 0x0300:				Uart_Printf ("ICMP error: destination unreachable\n");				break;			case 0x0400:				Uart_Printf ("ICMP error: source quench\n");				break;			case 0x0500:				Uart_Printf ("ICMP error: redirect\n");				break;			case 0x0b00:				Uart_Printf ("ICMP error: time-to-live exceeded\n");				break;			case 0x0c00:				Uart_Printf ("ICMP error: parameter problem\n");				break;			default:				break;			}		}	}}#if TFTPNAIVE_TIMEREQUESTint sendICMPTimestampRequest (unsigned int dst_ip){	ICMPTSpkt p;	memset (&p, 0, sizeof p);	p.icmphdr.op = ICMP_TIMESTAMP_REQUEST;	p.icmphdr.seq = 1;	p.icmphdr.cks = cks (&p.icmphdr.op, sizeof (ICMPTSpkt) - sizeof (IPpkt));	return sendIP ((IPpkt *) & p, sizeof (ICMPTSpkt) - sizeof (IPpkt),				   PROTO_ICMP, dst_ip);}#endif

⌨️ 快捷键说明

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