📄 icmp_a.c
字号:
/* * Copyright (c) 2001-2004 Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * * This file is part of the lwIP TCP/IP stack. * * Author: Adam Dunkels <adam@sics.se> * *//* Some ICMP messages should be passed to the transport protocols. This is not implemented. */#include <string.h>#include "lwip/opt.h"#include "lwip/icmp.h"#include "lwip/inet.h"#include "lwip/ip.h"#include "lwip/def.h"#include "lwip/stats.h"#include "lwip/snmp.h"//rou_cache_tmr(), this function will be put in a wrap function that will// be called when route cache function is enabled. it periodically add the// count 1,till it reaches 255,which means this entry has expired ,and could//be reused next time.static void rou_cache_tmr(){ u8_t expire_count ; rd_count=0; for(i=0,expire_count=0;i<CACHE_ENTRY;i++) { if(route_cache[i].count < 255) route_cache[i].count++; else expire_count++; } if (expire_count== CACHE_ENTRY) {rc_on=0; sys_untimeout(rou_cache_timer, null);}//关闭cache}static void rou_cache_timer( ){ rou_cache_tmr(); sys_timeout(ROU_TMR_INTERVAL, rou_cache_timer, NULL);}voidicmp_input(struct pbuf *p, struct netif *inp){ u8_t type; u8_t code; struct icmp_echo_hdr *iecho; struct icmp_rd_hdr *ird; struct ip_hdr *iphdr; struct ip_addr tmpaddr; u16_t hlen; ICMP_STATS_INC(icmp.recv); snmp_inc_icmpinmsgs(); iphdr = p->payload; hlen = IPH_HL(iphdr) * 4; if (pbuf_header(p, -((s16_t)hlen)) || (p->tot_len < sizeof(u16_t)*2)) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len)); pbuf_free(p); ICMP_STATS_INC(icmp.lenerr); snmp_inc_icmpinerrors(); return; } type = *((u8_t *)p->payload); code = *(((u8_t *)p->payload)+1); switch (type) { case ICMP_ECHO: /* broadcast or multicast destination address? */ if (ip_addr_isbroadcast(&iphdr->dest, inp) || ip_addr_ismulticast(&iphdr->dest)) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast or broadcast pings\n")); ICMP_STATS_INC(icmp.err); pbuf_free(p); return; } LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n")); if (p->tot_len < sizeof(struct icmp_echo_hdr)) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n")); pbuf_free(p); ICMP_STATS_INC(icmp.lenerr); snmp_inc_icmpinerrors(); return; } iecho = p->payload; if (inet_chksum_pbuf(p) != 0) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n")); pbuf_free(p); ICMP_STATS_INC(icmp.chkerr); snmp_inc_icmpinerrors(); return; } tmpaddr.addr = iphdr->src.addr; iphdr->src.addr = iphdr->dest.addr; iphdr->dest.addr = tmpaddr.addr; ICMPH_TYPE_SET(iecho, ICMP_ER); /* adjust the checksum */ if (iecho->chksum >= htons(0xffff - (ICMP_ECHO << 8))) { iecho->chksum += htons(ICMP_ECHO << 8) + 1; } else { iecho->chksum += htons(ICMP_ECHO << 8); } ICMP_STATS_INC(icmp.xmit); /* increase number of messages attempted to send */ snmp_inc_icmpoutmsgs(); /* increase number of echo replies attempted to send */ snmp_inc_icmpoutechoreps(); pbuf_header(p, hlen); ip_output_if(p, &(iphdr->src), IP_HDRINCL, IPH_TTL(iphdr), 0, IP_PROTO_ICMP, inp); break; case ICMP_RD: LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n")); if (p->tot_len < sizeof(struct icmp_echo_hdr)||code!=1) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP received\n")); pbuf_free(p); ICMP_STATS_INC(icmp.lenerr); snmp_inc_icmpinerrors(); return; } ird = p->payload; if (inet_chksum_pbuf(p) != 0) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for redirect ICMP message\n")); pbuf_free(p); ICMP_STATS_INC(icmp.chkerr); snmp_inc_icmpinerrors(); return; } // 提取源,目标ip地址,网关地址,作为重定向的信息保存下来. if (!rc_on){ /*rc is disabled now, count the rd messages that received, // if it reaches the 3 limit, then enable rc_on variable, //and initialize the array route_cache[],and fill the // first entry that picked up from the redirect message. // fill the new entry ,count fields with 0, and others with // 255 */ rd_count++; if (rd_count>=3) { rd_count=0; //rd_count计数器清零 rc_on=1; // route_cache 标志位置1,表示打开路由缓存功能 for(i=1;i<CACHE_ENTRY;i++) //将缓存条目的计数器设为最大,表示可用. { route_cache[i].count=255; } route_cache[0].src= ird.ip_hdr.src; route_cache[0].dest= ird.ip_hdr.dest; route_cache[0].gw= ird.gw ; route_cache[0].count=0; //register the timer, so it will call function rou_cache_tmr() // rou_tmr_interval millseconds later. sys_timeout(ROU_TMR_INTERVAL, rou_cache_timer, NULL); //1 } else { // rc is enabled now, try to pick the info from rd message //and fill the route_cache[] entry or fresh one of the entry, // fresh the count in the array. the fresh entry with 0 count, // others with plus 1 count. u8_t index=0; u8_t maxcount=0; u8_t flag=0; for (i=0;i<CACHE_ENTRY;i++){ if (route_cache[i].dest== ird.iphdr.dest&& route_cache[i].src==ird.ip_hdr.src) {route_cache[i].gw= ird.gw; route_cache[i].count=0; flag=1;break;} else {if (route_cache[i].count> maxcount ) index=i;} } if(!flag){ //if there is no match in the array ,then fill it. route_cache[index].dest=ird.iphdr.dest; route_cache[index].src=ird.ip_hdr.src; route_cache[index].gw= ird.gw; route_cache[index].count=0; } } pbuf_free(p); default: LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n", (s16_t)type, (s16_t)code)); ICMP_STATS_INC(icmp.proterr); ICMP_STATS_INC(icmp.drop); } pbuf_free(p);}voidicmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t){ struct pbuf *q; struct ip_hdr *iphdr; struct icmp_dur_hdr *idur; q = pbuf_alloc(PBUF_IP, 8 + IP_HLEN + 8, PBUF_RAM); /* ICMP header + IP header + 8 bytes of data */ iphdr = p->payload; idur = q->payload; ICMPH_TYPE_SET(idur, ICMP_DUR); ICMPH_CODE_SET(idur, t); memcpy((u8_t *)q->payload + 8, p->payload, IP_HLEN + 8); /* calculate checksum */ idur->chksum = 0; idur->chksum = inet_chksum(idur, q->len); ICMP_STATS_INC(icmp.xmit); /* increase number of messages attempted to send */ snmp_inc_icmpoutmsgs(); /* increase number of destination unreachable messages attempted to send */ snmp_inc_icmpoutdestunreachs(); ip_output(q, NULL, &(iphdr->src), ICMP_TTL, 0, IP_PROTO_ICMP); pbuf_free(q);}#if IP_FORWARDvoidicmp_time_exceeded(struct pbuf *p, enum icmp_te_type t){ struct pbuf *q; struct ip_hdr *iphdr; struct icmp_te_hdr *tehdr; q = pbuf_alloc(PBUF_IP, 8 + IP_HLEN + 8, PBUF_RAM); iphdr = p->payload; LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded from ")); ip_addr_debug_print(ICMP_DEBUG, &(iphdr->src)); LWIP_DEBUGF(ICMP_DEBUG, (" to ")); ip_addr_debug_print(ICMP_DEBUG, &(iphdr->dest)); LWIP_DEBUGF(ICMP_DEBUG, ("\n")); tehdr = q->payload; ICMPH_TYPE_SET(tehdr, ICMP_TE); ICMPH_CODE_SET(tehdr, t); /* copy fields from original packet */ memcpy((u8_t *)q->payload + 8, (u8_t *)p->payload, IP_HLEN + 8); /* calculate checksum */ tehdr->chksum = 0; tehdr->chksum = inet_chksum(tehdr, q->len); ICMP_STATS_INC(icmp.xmit); /* increase number of messages attempted to send */ snmp_inc_icmpoutmsgs(); /* increase number of destination unreachable messages attempted to send */ snmp_inc_icmpouttimeexcds(); ip_output(q, NULL, &(iphdr->src), ICMP_TTL, 0, IP_PROTO_ICMP); pbuf_free(q);}#endif /* IP_FORWARD */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -