Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RodColeman 0:8f5825f330b0 1 /**
RodColeman 0:8f5825f330b0 2 * @file
RodColeman 0:8f5825f330b0 3 * ICMP - Internet Control Message Protocol
RodColeman 0:8f5825f330b0 4 *
RodColeman 0:8f5825f330b0 5 */
RodColeman 0:8f5825f330b0 6
RodColeman 0:8f5825f330b0 7 /*
RodColeman 0:8f5825f330b0 8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
RodColeman 0:8f5825f330b0 9 * All rights reserved.
RodColeman 0:8f5825f330b0 10 *
RodColeman 0:8f5825f330b0 11 * Redistribution and use in source and binary forms, with or without modification,
RodColeman 0:8f5825f330b0 12 * are permitted provided that the following conditions are met:
RodColeman 0:8f5825f330b0 13 *
RodColeman 0:8f5825f330b0 14 * 1. Redistributions of source code must retain the above copyright notice,
RodColeman 0:8f5825f330b0 15 * this list of conditions and the following disclaimer.
RodColeman 0:8f5825f330b0 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
RodColeman 0:8f5825f330b0 17 * this list of conditions and the following disclaimer in the documentation
RodColeman 0:8f5825f330b0 18 * and/or other materials provided with the distribution.
RodColeman 0:8f5825f330b0 19 * 3. The name of the author may not be used to endorse or promote products
RodColeman 0:8f5825f330b0 20 * derived from this software without specific prior written permission.
RodColeman 0:8f5825f330b0 21 *
RodColeman 0:8f5825f330b0 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
RodColeman 0:8f5825f330b0 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
RodColeman 0:8f5825f330b0 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
RodColeman 0:8f5825f330b0 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
RodColeman 0:8f5825f330b0 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
RodColeman 0:8f5825f330b0 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
RodColeman 0:8f5825f330b0 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
RodColeman 0:8f5825f330b0 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
RodColeman 0:8f5825f330b0 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
RodColeman 0:8f5825f330b0 31 * OF SUCH DAMAGE.
RodColeman 0:8f5825f330b0 32 *
RodColeman 0:8f5825f330b0 33 * This file is part of the lwIP TCP/IP stack.
RodColeman 0:8f5825f330b0 34 *
RodColeman 0:8f5825f330b0 35 * Author: Adam Dunkels <adam@sics.se>
RodColeman 0:8f5825f330b0 36 *
RodColeman 0:8f5825f330b0 37 */
RodColeman 0:8f5825f330b0 38
RodColeman 0:8f5825f330b0 39 /* Some ICMP messages should be passed to the transport protocols. This
RodColeman 0:8f5825f330b0 40 is not implemented. */
RodColeman 0:8f5825f330b0 41
RodColeman 0:8f5825f330b0 42 #include "lwip/opt.h"
RodColeman 0:8f5825f330b0 43
RodColeman 0:8f5825f330b0 44 #if LWIP_ICMP /* don't build if not configured for use in lwipopts.h */
RodColeman 0:8f5825f330b0 45
RodColeman 0:8f5825f330b0 46 #include "lwip/icmp.h"
RodColeman 0:8f5825f330b0 47 #include "lwip/inet_chksum.h"
RodColeman 0:8f5825f330b0 48 #include "lwip/ip.h"
RodColeman 0:8f5825f330b0 49 #include "lwip/def.h"
RodColeman 0:8f5825f330b0 50 #include "lwip/stats.h"
RodColeman 0:8f5825f330b0 51 #include "lwip/snmp.h"
RodColeman 0:8f5825f330b0 52
RodColeman 0:8f5825f330b0 53 #include <string.h>
RodColeman 0:8f5825f330b0 54
RodColeman 0:8f5825f330b0 55 /** Small optimization: set to 0 if incoming PBUF_POOL pbuf always can be
RodColeman 0:8f5825f330b0 56 * used to modify and send a response packet (and to 1 if this is not the case,
RodColeman 0:8f5825f330b0 57 * e.g. when link header is stripped of when receiving) */
RodColeman 0:8f5825f330b0 58 #ifndef LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
RodColeman 0:8f5825f330b0 59 #define LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN 1
RodColeman 0:8f5825f330b0 60 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
RodColeman 0:8f5825f330b0 61
RodColeman 0:8f5825f330b0 62 /* The amount of data from the original packet to return in a dest-unreachable */
RodColeman 0:8f5825f330b0 63 #define ICMP_DEST_UNREACH_DATASIZE 8
RodColeman 0:8f5825f330b0 64
RodColeman 0:8f5825f330b0 65 static void icmp_send_response(struct pbuf *p, u8_t type, u8_t code);
RodColeman 0:8f5825f330b0 66
RodColeman 0:8f5825f330b0 67 /**
RodColeman 0:8f5825f330b0 68 * Processes ICMP input packets, called from ip_input().
RodColeman 0:8f5825f330b0 69 *
RodColeman 0:8f5825f330b0 70 * Currently only processes icmp echo requests and sends
RodColeman 0:8f5825f330b0 71 * out the echo response.
RodColeman 0:8f5825f330b0 72 *
RodColeman 0:8f5825f330b0 73 * @param p the icmp echo request packet, p->payload pointing to the ip header
RodColeman 0:8f5825f330b0 74 * @param inp the netif on which this packet was received
RodColeman 0:8f5825f330b0 75 */
RodColeman 0:8f5825f330b0 76 void
RodColeman 0:8f5825f330b0 77 icmp_input(struct pbuf *p, struct netif *inp)
RodColeman 0:8f5825f330b0 78 {
RodColeman 0:8f5825f330b0 79 u8_t type;
RodColeman 0:8f5825f330b0 80 #ifdef LWIP_DEBUG
RodColeman 0:8f5825f330b0 81 u8_t code;
RodColeman 0:8f5825f330b0 82 #endif /* LWIP_DEBUG */
RodColeman 0:8f5825f330b0 83 struct icmp_echo_hdr *iecho;
RodColeman 0:8f5825f330b0 84 struct ip_hdr *iphdr;
RodColeman 0:8f5825f330b0 85 s16_t hlen;
RodColeman 0:8f5825f330b0 86
RodColeman 0:8f5825f330b0 87 ICMP_STATS_INC(icmp.recv);
RodColeman 0:8f5825f330b0 88 snmp_inc_icmpinmsgs();
RodColeman 0:8f5825f330b0 89
RodColeman 0:8f5825f330b0 90
RodColeman 0:8f5825f330b0 91 iphdr = (struct ip_hdr *)p->payload;
RodColeman 0:8f5825f330b0 92 hlen = IPH_HL(iphdr) * 4;
RodColeman 0:8f5825f330b0 93 if (pbuf_header(p, -hlen) || (p->tot_len < sizeof(u16_t)*2)) {
RodColeman 0:8f5825f330b0 94 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len));
RodColeman 0:8f5825f330b0 95 goto lenerr;
RodColeman 0:8f5825f330b0 96 }
RodColeman 0:8f5825f330b0 97
RodColeman 0:8f5825f330b0 98 type = *((u8_t *)p->payload);
RodColeman 0:8f5825f330b0 99 #ifdef LWIP_DEBUG
RodColeman 0:8f5825f330b0 100 code = *(((u8_t *)p->payload)+1);
RodColeman 0:8f5825f330b0 101 #endif /* LWIP_DEBUG */
RodColeman 0:8f5825f330b0 102 switch (type) {
RodColeman 0:8f5825f330b0 103 case ICMP_ER:
RodColeman 0:8f5825f330b0 104 /* This is OK, echo reply might have been parsed by a raw PCB
RodColeman 0:8f5825f330b0 105 (as obviously, an echo request has been sent, too). */
RodColeman 0:8f5825f330b0 106 break;
RodColeman 0:8f5825f330b0 107 case ICMP_ECHO:
RodColeman 0:8f5825f330b0 108 #if !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING
RodColeman 0:8f5825f330b0 109 {
RodColeman 0:8f5825f330b0 110 int accepted = 1;
RodColeman 0:8f5825f330b0 111 #if !LWIP_MULTICAST_PING
RodColeman 0:8f5825f330b0 112 /* multicast destination address? */
RodColeman 0:8f5825f330b0 113 if (ip_addr_ismulticast(&current_iphdr_dest)) {
RodColeman 0:8f5825f330b0 114 accepted = 0;
RodColeman 0:8f5825f330b0 115 }
RodColeman 0:8f5825f330b0 116 #endif /* LWIP_MULTICAST_PING */
RodColeman 0:8f5825f330b0 117 #if !LWIP_BROADCAST_PING
RodColeman 0:8f5825f330b0 118 /* broadcast destination address? */
RodColeman 0:8f5825f330b0 119 if (ip_addr_isbroadcast(&current_iphdr_dest, inp)) {
RodColeman 0:8f5825f330b0 120 accepted = 0;
RodColeman 0:8f5825f330b0 121 }
RodColeman 0:8f5825f330b0 122 #endif /* LWIP_BROADCAST_PING */
RodColeman 0:8f5825f330b0 123 /* broadcast or multicast destination address not acceptd? */
RodColeman 0:8f5825f330b0 124 if (!accepted) {
RodColeman 0:8f5825f330b0 125 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast or broadcast pings\n"));
RodColeman 0:8f5825f330b0 126 ICMP_STATS_INC(icmp.err);
RodColeman 0:8f5825f330b0 127 pbuf_free(p);
RodColeman 0:8f5825f330b0 128 return;
RodColeman 0:8f5825f330b0 129 }
RodColeman 0:8f5825f330b0 130 }
RodColeman 0:8f5825f330b0 131 #endif /* !LWIP_MULTICAST_PING || !LWIP_BROADCAST_PING */
RodColeman 0:8f5825f330b0 132 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n"));
RodColeman 0:8f5825f330b0 133 if (p->tot_len < sizeof(struct icmp_echo_hdr)) {
RodColeman 0:8f5825f330b0 134 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n"));
RodColeman 0:8f5825f330b0 135 goto lenerr;
RodColeman 0:8f5825f330b0 136 }
RodColeman 0:8f5825f330b0 137 if (inet_chksum_pbuf(p) != 0) {
RodColeman 0:8f5825f330b0 138 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n"));
RodColeman 0:8f5825f330b0 139 pbuf_free(p);
RodColeman 0:8f5825f330b0 140 ICMP_STATS_INC(icmp.chkerr);
RodColeman 0:8f5825f330b0 141 snmp_inc_icmpinerrors();
RodColeman 0:8f5825f330b0 142 return;
RodColeman 0:8f5825f330b0 143 }
RodColeman 0:8f5825f330b0 144 #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
RodColeman 0:8f5825f330b0 145 if (pbuf_header(p, (PBUF_IP_HLEN + PBUF_LINK_HLEN))) {
RodColeman 0:8f5825f330b0 146 /* p is not big enough to contain link headers
RodColeman 0:8f5825f330b0 147 * allocate a new one and copy p into it
RodColeman 0:8f5825f330b0 148 */
RodColeman 0:8f5825f330b0 149 struct pbuf *r;
RodColeman 0:8f5825f330b0 150 /* switch p->payload to ip header */
RodColeman 0:8f5825f330b0 151 if (pbuf_header(p, hlen)) {
RodColeman 0:8f5825f330b0 152 LWIP_ASSERT("icmp_input: moving p->payload to ip header failed\n", 0);
RodColeman 0:8f5825f330b0 153 goto memerr;
RodColeman 0:8f5825f330b0 154 }
RodColeman 0:8f5825f330b0 155 /* allocate new packet buffer with space for link headers */
RodColeman 0:8f5825f330b0 156 r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
RodColeman 0:8f5825f330b0 157 if (r == NULL) {
RodColeman 0:8f5825f330b0 158 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n"));
RodColeman 0:8f5825f330b0 159 goto memerr;
RodColeman 0:8f5825f330b0 160 }
RodColeman 0:8f5825f330b0 161 LWIP_ASSERT("check that first pbuf can hold struct the ICMP header",
RodColeman 0:8f5825f330b0 162 (r->len >= hlen + sizeof(struct icmp_echo_hdr)));
RodColeman 0:8f5825f330b0 163 /* copy the whole packet including ip header */
RodColeman 0:8f5825f330b0 164 if (pbuf_copy(r, p) != ERR_OK) {
RodColeman 0:8f5825f330b0 165 LWIP_ASSERT("icmp_input: copying to new pbuf failed\n", 0);
RodColeman 0:8f5825f330b0 166 goto memerr;
RodColeman 0:8f5825f330b0 167 }
RodColeman 0:8f5825f330b0 168 iphdr = (struct ip_hdr *)r->payload;
RodColeman 0:8f5825f330b0 169 /* switch r->payload back to icmp header */
RodColeman 0:8f5825f330b0 170 if (pbuf_header(r, -hlen)) {
RodColeman 0:8f5825f330b0 171 LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
RodColeman 0:8f5825f330b0 172 goto memerr;
RodColeman 0:8f5825f330b0 173 }
RodColeman 0:8f5825f330b0 174 /* free the original p */
RodColeman 0:8f5825f330b0 175 pbuf_free(p);
RodColeman 0:8f5825f330b0 176 /* we now have an identical copy of p that has room for link headers */
RodColeman 0:8f5825f330b0 177 p = r;
RodColeman 0:8f5825f330b0 178 } else {
RodColeman 0:8f5825f330b0 179 /* restore p->payload to point to icmp header */
RodColeman 0:8f5825f330b0 180 if (pbuf_header(p, -(s16_t)(PBUF_IP_HLEN + PBUF_LINK_HLEN))) {
RodColeman 0:8f5825f330b0 181 LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0);
RodColeman 0:8f5825f330b0 182 goto memerr;
RodColeman 0:8f5825f330b0 183 }
RodColeman 0:8f5825f330b0 184 }
RodColeman 0:8f5825f330b0 185 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
RodColeman 0:8f5825f330b0 186 /* At this point, all checks are OK. */
RodColeman 0:8f5825f330b0 187 /* We generate an answer by switching the dest and src ip addresses,
RodColeman 0:8f5825f330b0 188 * setting the icmp type to ECHO_RESPONSE and updating the checksum. */
RodColeman 0:8f5825f330b0 189 iecho = (struct icmp_echo_hdr *)p->payload;
RodColeman 0:8f5825f330b0 190 ip_addr_copy(iphdr->src, *ip_current_dest_addr());
RodColeman 0:8f5825f330b0 191 ip_addr_copy(iphdr->dest, *ip_current_src_addr());
RodColeman 0:8f5825f330b0 192 ICMPH_TYPE_SET(iecho, ICMP_ER);
RodColeman 0:8f5825f330b0 193 /* adjust the checksum */
RodColeman 0:8f5825f330b0 194 if (iecho->chksum >= PP_HTONS(0xffff - (ICMP_ECHO << 8))) {
RodColeman 0:8f5825f330b0 195 iecho->chksum += PP_HTONS(ICMP_ECHO << 8) + 1;
RodColeman 0:8f5825f330b0 196 } else {
RodColeman 0:8f5825f330b0 197 iecho->chksum += PP_HTONS(ICMP_ECHO << 8);
RodColeman 0:8f5825f330b0 198 }
RodColeman 0:8f5825f330b0 199
RodColeman 0:8f5825f330b0 200 /* Set the correct TTL and recalculate the header checksum. */
RodColeman 0:8f5825f330b0 201 IPH_TTL_SET(iphdr, ICMP_TTL);
RodColeman 0:8f5825f330b0 202 IPH_CHKSUM_SET(iphdr, 0);
RodColeman 0:8f5825f330b0 203 #if CHECKSUM_GEN_IP
RodColeman 0:8f5825f330b0 204 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
RodColeman 0:8f5825f330b0 205 #endif /* CHECKSUM_GEN_IP */
RodColeman 0:8f5825f330b0 206
RodColeman 0:8f5825f330b0 207 ICMP_STATS_INC(icmp.xmit);
RodColeman 0:8f5825f330b0 208 /* increase number of messages attempted to send */
RodColeman 0:8f5825f330b0 209 snmp_inc_icmpoutmsgs();
RodColeman 0:8f5825f330b0 210 /* increase number of echo replies attempted to send */
RodColeman 0:8f5825f330b0 211 snmp_inc_icmpoutechoreps();
RodColeman 0:8f5825f330b0 212
RodColeman 0:8f5825f330b0 213 if(pbuf_header(p, hlen)) {
RodColeman 0:8f5825f330b0 214 LWIP_ASSERT("Can't move over header in packet", 0);
RodColeman 0:8f5825f330b0 215 } else {
RodColeman 0:8f5825f330b0 216 err_t ret;
RodColeman 0:8f5825f330b0 217 /* send an ICMP packet, src addr is the dest addr of the curren packet */
RodColeman 0:8f5825f330b0 218 ret = ip_output_if(p, ip_current_dest_addr(), IP_HDRINCL,
RodColeman 0:8f5825f330b0 219 ICMP_TTL, 0, IP_PROTO_ICMP, inp);
RodColeman 0:8f5825f330b0 220 if (ret != ERR_OK) {
RodColeman 0:8f5825f330b0 221 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ip_output_if returned an error: %c.\n", ret));
RodColeman 0:8f5825f330b0 222 }
RodColeman 0:8f5825f330b0 223 }
RodColeman 0:8f5825f330b0 224 break;
RodColeman 0:8f5825f330b0 225 default:
RodColeman 0:8f5825f330b0 226 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" code %"S16_F" not supported.\n",
RodColeman 0:8f5825f330b0 227 (s16_t)type, (s16_t)code));
RodColeman 0:8f5825f330b0 228 ICMP_STATS_INC(icmp.proterr);
RodColeman 0:8f5825f330b0 229 ICMP_STATS_INC(icmp.drop);
RodColeman 0:8f5825f330b0 230 }
RodColeman 0:8f5825f330b0 231 pbuf_free(p);
RodColeman 0:8f5825f330b0 232 return;
RodColeman 0:8f5825f330b0 233 lenerr:
RodColeman 0:8f5825f330b0 234 pbuf_free(p);
RodColeman 0:8f5825f330b0 235 ICMP_STATS_INC(icmp.lenerr);
RodColeman 0:8f5825f330b0 236 snmp_inc_icmpinerrors();
RodColeman 0:8f5825f330b0 237 return;
RodColeman 0:8f5825f330b0 238 #if LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN
RodColeman 0:8f5825f330b0 239 memerr:
RodColeman 0:8f5825f330b0 240 pbuf_free(p);
RodColeman 0:8f5825f330b0 241 ICMP_STATS_INC(icmp.err);
RodColeman 0:8f5825f330b0 242 snmp_inc_icmpinerrors();
RodColeman 0:8f5825f330b0 243 return;
RodColeman 0:8f5825f330b0 244 #endif /* LWIP_ICMP_ECHO_CHECK_INPUT_PBUF_LEN */
RodColeman 0:8f5825f330b0 245 }
RodColeman 0:8f5825f330b0 246
RodColeman 0:8f5825f330b0 247 /**
RodColeman 0:8f5825f330b0 248 * Send an icmp 'destination unreachable' packet, called from ip_input() if
RodColeman 0:8f5825f330b0 249 * the transport layer protocol is unknown and from udp_input() if the local
RodColeman 0:8f5825f330b0 250 * port is not bound.
RodColeman 0:8f5825f330b0 251 *
RodColeman 0:8f5825f330b0 252 * @param p the input packet for which the 'unreachable' should be sent,
RodColeman 0:8f5825f330b0 253 * p->payload pointing to the IP header
RodColeman 0:8f5825f330b0 254 * @param t type of the 'unreachable' packet
RodColeman 0:8f5825f330b0 255 */
RodColeman 0:8f5825f330b0 256 void
RodColeman 0:8f5825f330b0 257 icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
RodColeman 0:8f5825f330b0 258 {
RodColeman 0:8f5825f330b0 259 icmp_send_response(p, ICMP_DUR, t);
RodColeman 0:8f5825f330b0 260 }
RodColeman 0:8f5825f330b0 261
RodColeman 0:8f5825f330b0 262 #if IP_FORWARD || IP_REASSEMBLY
RodColeman 0:8f5825f330b0 263 /**
RodColeman 0:8f5825f330b0 264 * Send a 'time exceeded' packet, called from ip_forward() if TTL is 0.
RodColeman 0:8f5825f330b0 265 *
RodColeman 0:8f5825f330b0 266 * @param p the input packet for which the 'time exceeded' should be sent,
RodColeman 0:8f5825f330b0 267 * p->payload pointing to the IP header
RodColeman 0:8f5825f330b0 268 * @param t type of the 'time exceeded' packet
RodColeman 0:8f5825f330b0 269 */
RodColeman 0:8f5825f330b0 270 void
RodColeman 0:8f5825f330b0 271 icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
RodColeman 0:8f5825f330b0 272 {
RodColeman 0:8f5825f330b0 273 icmp_send_response(p, ICMP_TE, t);
RodColeman 0:8f5825f330b0 274 }
RodColeman 0:8f5825f330b0 275
RodColeman 0:8f5825f330b0 276 #endif /* IP_FORWARD || IP_REASSEMBLY */
RodColeman 0:8f5825f330b0 277
RodColeman 0:8f5825f330b0 278 /**
RodColeman 0:8f5825f330b0 279 * Send an icmp packet in response to an incoming packet.
RodColeman 0:8f5825f330b0 280 *
RodColeman 0:8f5825f330b0 281 * @param p the input packet for which the 'unreachable' should be sent,
RodColeman 0:8f5825f330b0 282 * p->payload pointing to the IP header
RodColeman 0:8f5825f330b0 283 * @param type Type of the ICMP header
RodColeman 0:8f5825f330b0 284 * @param code Code of the ICMP header
RodColeman 0:8f5825f330b0 285 */
RodColeman 0:8f5825f330b0 286 static void
RodColeman 0:8f5825f330b0 287 icmp_send_response(struct pbuf *p, u8_t type, u8_t code)
RodColeman 0:8f5825f330b0 288 {
RodColeman 0:8f5825f330b0 289 struct pbuf *q;
RodColeman 0:8f5825f330b0 290 struct ip_hdr *iphdr;
RodColeman 0:8f5825f330b0 291 /* we can use the echo header here */
RodColeman 0:8f5825f330b0 292 struct icmp_echo_hdr *icmphdr;
RodColeman 0:8f5825f330b0 293 ip_addr_t iphdr_src;
RodColeman 0:8f5825f330b0 294
RodColeman 0:8f5825f330b0 295 /* ICMP header + IP header + 8 bytes of data */
RodColeman 0:8f5825f330b0 296 q = pbuf_alloc(PBUF_IP, sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE,
RodColeman 0:8f5825f330b0 297 PBUF_RAM);
RodColeman 0:8f5825f330b0 298 if (q == NULL) {
RodColeman 0:8f5825f330b0 299 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded: failed to allocate pbuf for ICMP packet.\n"));
RodColeman 0:8f5825f330b0 300 return;
RodColeman 0:8f5825f330b0 301 }
RodColeman 0:8f5825f330b0 302 LWIP_ASSERT("check that first pbuf can hold icmp message",
RodColeman 0:8f5825f330b0 303 (q->len >= (sizeof(struct icmp_echo_hdr) + IP_HLEN + ICMP_DEST_UNREACH_DATASIZE)));
RodColeman 0:8f5825f330b0 304
RodColeman 0:8f5825f330b0 305 iphdr = (struct ip_hdr *)p->payload;
RodColeman 0:8f5825f330b0 306 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded from "));
RodColeman 0:8f5825f330b0 307 ip_addr_debug_print(ICMP_DEBUG, &(iphdr->src));
RodColeman 0:8f5825f330b0 308 LWIP_DEBUGF(ICMP_DEBUG, (" to "));
RodColeman 0:8f5825f330b0 309 ip_addr_debug_print(ICMP_DEBUG, &(iphdr->dest));
RodColeman 0:8f5825f330b0 310 LWIP_DEBUGF(ICMP_DEBUG, ("\n"));
RodColeman 0:8f5825f330b0 311
RodColeman 0:8f5825f330b0 312 icmphdr = (struct icmp_echo_hdr *)q->payload;
RodColeman 0:8f5825f330b0 313 icmphdr->type = type;
RodColeman 0:8f5825f330b0 314 icmphdr->code = code;
RodColeman 0:8f5825f330b0 315 icmphdr->id = 0;
RodColeman 0:8f5825f330b0 316 icmphdr->seqno = 0;
RodColeman 0:8f5825f330b0 317
RodColeman 0:8f5825f330b0 318 /* copy fields from original packet */
RodColeman 0:8f5825f330b0 319 SMEMCPY((u8_t *)q->payload + sizeof(struct icmp_echo_hdr), (u8_t *)p->payload,
RodColeman 0:8f5825f330b0 320 IP_HLEN + ICMP_DEST_UNREACH_DATASIZE);
RodColeman 0:8f5825f330b0 321
RodColeman 0:8f5825f330b0 322 /* calculate checksum */
RodColeman 0:8f5825f330b0 323 icmphdr->chksum = 0;
RodColeman 0:8f5825f330b0 324 icmphdr->chksum = inet_chksum(icmphdr, q->len);
RodColeman 0:8f5825f330b0 325 ICMP_STATS_INC(icmp.xmit);
RodColeman 0:8f5825f330b0 326 /* increase number of messages attempted to send */
RodColeman 0:8f5825f330b0 327 snmp_inc_icmpoutmsgs();
RodColeman 0:8f5825f330b0 328 /* increase number of destination unreachable messages attempted to send */
RodColeman 0:8f5825f330b0 329 snmp_inc_icmpouttimeexcds();
RodColeman 0:8f5825f330b0 330 ip_addr_copy(iphdr_src, iphdr->src);
RodColeman 0:8f5825f330b0 331 ip_output(q, NULL, &iphdr_src, ICMP_TTL, 0, IP_PROTO_ICMP);
RodColeman 0:8f5825f330b0 332 pbuf_free(q);
RodColeman 0:8f5825f330b0 333 }
RodColeman 0:8f5825f330b0 334
RodColeman 0:8f5825f330b0 335 #endif /* LWIP_ICMP */