Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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