Stripped down version of Segundos NetService library (http://mbed.org/users/segundo/libraries/NetServices ). I have removed all NetServices, and all functions which had been disabled. Use this version when you need only pure TCP or UDP functions - this library compiles faster.

Dependencies:   lwip lwip-sys

Dependents:   christmasLights device_server pop3demo device_server_udp ... more

Committer:
hlipka
Date:
Mon Jan 10 21:03:11 2011 +0000
Revision:
0:8b387bed54c2
initial version

Who changed what in which revision?

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