Webserver+3d print

Dependents:   Nucleo

Committer:
Sergunb
Date:
Sat Feb 04 18:15:49 2017 +0000
Revision:
0:8918a71cdbe9
nothing else

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sergunb 0:8918a71cdbe9 1 /**
Sergunb 0:8918a71cdbe9 2 * @file ping.c
Sergunb 0:8918a71cdbe9 3 * @brief Ping utility
Sergunb 0:8918a71cdbe9 4 *
Sergunb 0:8918a71cdbe9 5 * @section License
Sergunb 0:8918a71cdbe9 6 *
Sergunb 0:8918a71cdbe9 7 * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
Sergunb 0:8918a71cdbe9 8 *
Sergunb 0:8918a71cdbe9 9 * This file is part of CycloneTCP Open.
Sergunb 0:8918a71cdbe9 10 *
Sergunb 0:8918a71cdbe9 11 * This program is free software; you can redistribute it and/or
Sergunb 0:8918a71cdbe9 12 * modify it under the terms of the GNU General Public License
Sergunb 0:8918a71cdbe9 13 * as published by the Free Software Foundation; either version 2
Sergunb 0:8918a71cdbe9 14 * of the License, or (at your option) any later version.
Sergunb 0:8918a71cdbe9 15 *
Sergunb 0:8918a71cdbe9 16 * This program is distributed in the hope that it will be useful,
Sergunb 0:8918a71cdbe9 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Sergunb 0:8918a71cdbe9 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Sergunb 0:8918a71cdbe9 19 * GNU General Public License for more details.
Sergunb 0:8918a71cdbe9 20 *
Sergunb 0:8918a71cdbe9 21 * You should have received a copy of the GNU General Public License
Sergunb 0:8918a71cdbe9 22 * along with this program; if not, write to the Free Software Foundation,
Sergunb 0:8918a71cdbe9 23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Sergunb 0:8918a71cdbe9 24 *
Sergunb 0:8918a71cdbe9 25 * @author Oryx Embedded SARL (www.oryx-embedded.com)
Sergunb 0:8918a71cdbe9 26 * @version 1.7.6
Sergunb 0:8918a71cdbe9 27 **/
Sergunb 0:8918a71cdbe9 28
Sergunb 0:8918a71cdbe9 29 //Switch to the appropriate trace level
Sergunb 0:8918a71cdbe9 30 #define TRACE_LEVEL PING_TRACE_LEVEL
Sergunb 0:8918a71cdbe9 31
Sergunb 0:8918a71cdbe9 32 //Dependencies
Sergunb 0:8918a71cdbe9 33 #include "core/net.h"
Sergunb 0:8918a71cdbe9 34 #include "core/ping.h"
Sergunb 0:8918a71cdbe9 35 #include "core/ip.h"
Sergunb 0:8918a71cdbe9 36 #include "ipv4/ipv4.h"
Sergunb 0:8918a71cdbe9 37 #include "ipv4/icmp.h"
Sergunb 0:8918a71cdbe9 38 #include "ipv6/ipv6.h"
Sergunb 0:8918a71cdbe9 39 #include "ipv6/ipv6_misc.h"
Sergunb 0:8918a71cdbe9 40 #include "ipv6/icmpv6.h"
Sergunb 0:8918a71cdbe9 41 #include "core/socket.h"
Sergunb 0:8918a71cdbe9 42 #include "debug.h"
Sergunb 0:8918a71cdbe9 43
Sergunb 0:8918a71cdbe9 44 //Check TCP/IP stack configuration
Sergunb 0:8918a71cdbe9 45 #if (PING_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 46
Sergunb 0:8918a71cdbe9 47 //Sequence number field
Sergunb 0:8918a71cdbe9 48 static uint16_t pingSequenceNumber = 0;
Sergunb 0:8918a71cdbe9 49
Sergunb 0:8918a71cdbe9 50
Sergunb 0:8918a71cdbe9 51 /**
Sergunb 0:8918a71cdbe9 52 * @brief Test the reachability of a host
Sergunb 0:8918a71cdbe9 53 *
Sergunb 0:8918a71cdbe9 54 * Ping operates by sending an ICMP Echo Request message to the
Sergunb 0:8918a71cdbe9 55 * target host and waiting for an ICMP Echo Reply message
Sergunb 0:8918a71cdbe9 56 *
Sergunb 0:8918a71cdbe9 57 * @param[in] interface Underlying network interface (optional parameter)
Sergunb 0:8918a71cdbe9 58 * @param[in] targetIpAddr IP address of the host to reach
Sergunb 0:8918a71cdbe9 59 * @param[in] size Size of the data payload in bytes
Sergunb 0:8918a71cdbe9 60 * @param[in] ttl Time-To-Live value to be used
Sergunb 0:8918a71cdbe9 61 * @param[in] timeout Maximum time to wait before giving up
Sergunb 0:8918a71cdbe9 62 * @param[out] rtt Round-trip time (optional parameter)
Sergunb 0:8918a71cdbe9 63 * @return Error code
Sergunb 0:8918a71cdbe9 64 **/
Sergunb 0:8918a71cdbe9 65
Sergunb 0:8918a71cdbe9 66 error_t ping(NetInterface *interface, const IpAddr *targetIpAddr,
Sergunb 0:8918a71cdbe9 67 size_t size, uint8_t ttl, systime_t timeout, systime_t *rtt)
Sergunb 0:8918a71cdbe9 68 {
Sergunb 0:8918a71cdbe9 69 error_t error;
Sergunb 0:8918a71cdbe9 70 PingContext context;
Sergunb 0:8918a71cdbe9 71
Sergunb 0:8918a71cdbe9 72 //Check parameters
Sergunb 0:8918a71cdbe9 73 if(targetIpAddr == NULL)
Sergunb 0:8918a71cdbe9 74 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 75
Sergunb 0:8918a71cdbe9 76 //Initialize context
Sergunb 0:8918a71cdbe9 77 pingInit(&context);
Sergunb 0:8918a71cdbe9 78
Sergunb 0:8918a71cdbe9 79 //Start of exception handling block
Sergunb 0:8918a71cdbe9 80 do
Sergunb 0:8918a71cdbe9 81 {
Sergunb 0:8918a71cdbe9 82 //Select the specified network interface
Sergunb 0:8918a71cdbe9 83 error = pingBindToInterface(&context, interface);
Sergunb 0:8918a71cdbe9 84 //Any error to report?
Sergunb 0:8918a71cdbe9 85 if(error)
Sergunb 0:8918a71cdbe9 86 break;
Sergunb 0:8918a71cdbe9 87
Sergunb 0:8918a71cdbe9 88 //Set timeout value
Sergunb 0:8918a71cdbe9 89 error = pingSetTimeout(&context, timeout);
Sergunb 0:8918a71cdbe9 90 //Any error to report?
Sergunb 0:8918a71cdbe9 91 if(error)
Sergunb 0:8918a71cdbe9 92 break;
Sergunb 0:8918a71cdbe9 93
Sergunb 0:8918a71cdbe9 94 //Send an ICMP Echo Request message
Sergunb 0:8918a71cdbe9 95 error = pingSendRequest(&context, targetIpAddr, size, ttl);
Sergunb 0:8918a71cdbe9 96 //Any error to report?
Sergunb 0:8918a71cdbe9 97 if(error)
Sergunb 0:8918a71cdbe9 98 break;
Sergunb 0:8918a71cdbe9 99
Sergunb 0:8918a71cdbe9 100 //Wait for a matching Echo Reply message
Sergunb 0:8918a71cdbe9 101 error = pingWaitForReply(&context, NULL, rtt);
Sergunb 0:8918a71cdbe9 102 //Any error to report?
Sergunb 0:8918a71cdbe9 103 if(error)
Sergunb 0:8918a71cdbe9 104 break;
Sergunb 0:8918a71cdbe9 105
Sergunb 0:8918a71cdbe9 106 //End of exception handling block
Sergunb 0:8918a71cdbe9 107 } while(0);
Sergunb 0:8918a71cdbe9 108
Sergunb 0:8918a71cdbe9 109 //Release resources
Sergunb 0:8918a71cdbe9 110 pingRelease(&context);
Sergunb 0:8918a71cdbe9 111
Sergunb 0:8918a71cdbe9 112 //Return status code
Sergunb 0:8918a71cdbe9 113 return error;
Sergunb 0:8918a71cdbe9 114 }
Sergunb 0:8918a71cdbe9 115
Sergunb 0:8918a71cdbe9 116
Sergunb 0:8918a71cdbe9 117 /**
Sergunb 0:8918a71cdbe9 118 * @brief Initialize ping context
Sergunb 0:8918a71cdbe9 119 * @param[in] context Pointer to the ping context
Sergunb 0:8918a71cdbe9 120 **/
Sergunb 0:8918a71cdbe9 121
Sergunb 0:8918a71cdbe9 122 void pingInit(PingContext *context)
Sergunb 0:8918a71cdbe9 123 {
Sergunb 0:8918a71cdbe9 124 //Make sure the context is valid
Sergunb 0:8918a71cdbe9 125 if(context != NULL)
Sergunb 0:8918a71cdbe9 126 {
Sergunb 0:8918a71cdbe9 127 //Initialize context
Sergunb 0:8918a71cdbe9 128 memset(context, 0, sizeof(PingContext));
Sergunb 0:8918a71cdbe9 129
Sergunb 0:8918a71cdbe9 130 //Set the default timeout to be used
Sergunb 0:8918a71cdbe9 131 context->timeout = PING_DEFAULT_TIMEOUT;
Sergunb 0:8918a71cdbe9 132 }
Sergunb 0:8918a71cdbe9 133 }
Sergunb 0:8918a71cdbe9 134
Sergunb 0:8918a71cdbe9 135
Sergunb 0:8918a71cdbe9 136 /**
Sergunb 0:8918a71cdbe9 137 * @brief Set timeout value
Sergunb 0:8918a71cdbe9 138 * @param[in] context Pointer to the ping context
Sergunb 0:8918a71cdbe9 139 * @param[in] timeout Maximum time to wait
Sergunb 0:8918a71cdbe9 140 * @return Error code
Sergunb 0:8918a71cdbe9 141 **/
Sergunb 0:8918a71cdbe9 142
Sergunb 0:8918a71cdbe9 143 error_t pingSetTimeout(PingContext *context, systime_t timeout)
Sergunb 0:8918a71cdbe9 144 {
Sergunb 0:8918a71cdbe9 145 //Invalid context?
Sergunb 0:8918a71cdbe9 146 if(context == NULL)
Sergunb 0:8918a71cdbe9 147 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 148
Sergunb 0:8918a71cdbe9 149 //Save timeout value
Sergunb 0:8918a71cdbe9 150 context->timeout = timeout;
Sergunb 0:8918a71cdbe9 151
Sergunb 0:8918a71cdbe9 152 //Successful processing
Sergunb 0:8918a71cdbe9 153 return NO_ERROR;
Sergunb 0:8918a71cdbe9 154 }
Sergunb 0:8918a71cdbe9 155
Sergunb 0:8918a71cdbe9 156
Sergunb 0:8918a71cdbe9 157 /**
Sergunb 0:8918a71cdbe9 158 * @brief Select a particular network interface
Sergunb 0:8918a71cdbe9 159 * @param[in] context Pointer to the ping context
Sergunb 0:8918a71cdbe9 160 * @param[in] interface Network interface to be used
Sergunb 0:8918a71cdbe9 161 * @return Error code
Sergunb 0:8918a71cdbe9 162 **/
Sergunb 0:8918a71cdbe9 163
Sergunb 0:8918a71cdbe9 164 error_t pingBindToInterface(PingContext *context, NetInterface *interface)
Sergunb 0:8918a71cdbe9 165 {
Sergunb 0:8918a71cdbe9 166 //Invalid context?
Sergunb 0:8918a71cdbe9 167 if(context == NULL)
Sergunb 0:8918a71cdbe9 168 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 169
Sergunb 0:8918a71cdbe9 170 //Select the specified network interface
Sergunb 0:8918a71cdbe9 171 context->interface = interface;
Sergunb 0:8918a71cdbe9 172
Sergunb 0:8918a71cdbe9 173 //Successful processing
Sergunb 0:8918a71cdbe9 174 return NO_ERROR;
Sergunb 0:8918a71cdbe9 175 }
Sergunb 0:8918a71cdbe9 176
Sergunb 0:8918a71cdbe9 177
Sergunb 0:8918a71cdbe9 178 /**
Sergunb 0:8918a71cdbe9 179 * @brief Send an ICMP Echo Request message
Sergunb 0:8918a71cdbe9 180 * @param[in] context Pointer to the ping context
Sergunb 0:8918a71cdbe9 181 * @param[in] targetIpAddr IP address of the host to reach
Sergunb 0:8918a71cdbe9 182 * @param[in] size Size of the data payload, in bytes
Sergunb 0:8918a71cdbe9 183 * @param[in] ttl Time-To-Live value to be used
Sergunb 0:8918a71cdbe9 184 * @return Error code
Sergunb 0:8918a71cdbe9 185 **/
Sergunb 0:8918a71cdbe9 186
Sergunb 0:8918a71cdbe9 187 error_t pingSendRequest(PingContext *context,
Sergunb 0:8918a71cdbe9 188 const IpAddr *targetIpAddr, size_t size, uint8_t ttl)
Sergunb 0:8918a71cdbe9 189 {
Sergunb 0:8918a71cdbe9 190 error_t error;
Sergunb 0:8918a71cdbe9 191 size_t i;
Sergunb 0:8918a71cdbe9 192 size_t length;
Sergunb 0:8918a71cdbe9 193 NetInterface *interface;
Sergunb 0:8918a71cdbe9 194 IcmpEchoMessage *message;
Sergunb 0:8918a71cdbe9 195
Sergunb 0:8918a71cdbe9 196 //Invalid context?
Sergunb 0:8918a71cdbe9 197 if(context == NULL)
Sergunb 0:8918a71cdbe9 198 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 199
Sergunb 0:8918a71cdbe9 200 //Limit the size of the data payload
Sergunb 0:8918a71cdbe9 201 context->dataPayloadSize = MIN (size, PING_MAX_DATA_SIZE);
Sergunb 0:8918a71cdbe9 202
Sergunb 0:8918a71cdbe9 203 //Close existing socket, if necessary
Sergunb 0:8918a71cdbe9 204 if(context->socket != NULL)
Sergunb 0:8918a71cdbe9 205 {
Sergunb 0:8918a71cdbe9 206 socketClose(context->socket);
Sergunb 0:8918a71cdbe9 207 context->socket = NULL;
Sergunb 0:8918a71cdbe9 208 }
Sergunb 0:8918a71cdbe9 209
Sergunb 0:8918a71cdbe9 210 //Identifier field is used to help matching requests and replies
Sergunb 0:8918a71cdbe9 211 context->identifier = netGetRand();
Sergunb 0:8918a71cdbe9 212
Sergunb 0:8918a71cdbe9 213 //Get exclusive access
Sergunb 0:8918a71cdbe9 214 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 215 //Sequence Number field is increment each time an Echo Request is sent
Sergunb 0:8918a71cdbe9 216 context->sequenceNumber = pingSequenceNumber++;
Sergunb 0:8918a71cdbe9 217 //Release exclusive access
Sergunb 0:8918a71cdbe9 218 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 219
Sergunb 0:8918a71cdbe9 220 //Point to the buffer where to format the ICMP message
Sergunb 0:8918a71cdbe9 221 message = (IcmpEchoMessage *) context->buffer;
Sergunb 0:8918a71cdbe9 222
Sergunb 0:8918a71cdbe9 223 //Format ICMP Echo Request message
Sergunb 0:8918a71cdbe9 224 message->type = ICMP_TYPE_ECHO_REQUEST;
Sergunb 0:8918a71cdbe9 225 message->code = 0;
Sergunb 0:8918a71cdbe9 226 message->checksum = 0;
Sergunb 0:8918a71cdbe9 227 message->identifier = context->identifier;
Sergunb 0:8918a71cdbe9 228 message->sequenceNumber = context->sequenceNumber;
Sergunb 0:8918a71cdbe9 229
Sergunb 0:8918a71cdbe9 230 //Initialize data payload
Sergunb 0:8918a71cdbe9 231 for(i = 0; i < context->dataPayloadSize; i++)
Sergunb 0:8918a71cdbe9 232 message->data[i] = i & 0xFF;
Sergunb 0:8918a71cdbe9 233
Sergunb 0:8918a71cdbe9 234 //Length of the complete ICMP message including header and data
Sergunb 0:8918a71cdbe9 235 length = sizeof(IcmpEchoMessage) + context->dataPayloadSize;
Sergunb 0:8918a71cdbe9 236
Sergunb 0:8918a71cdbe9 237 //Select the relevant network interface
Sergunb 0:8918a71cdbe9 238 interface = context->interface;
Sergunb 0:8918a71cdbe9 239
Sergunb 0:8918a71cdbe9 240 #if (IPV4_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 241 //Is target address an IPv4 address?
Sergunb 0:8918a71cdbe9 242 if(targetIpAddr->length == sizeof(Ipv4Addr))
Sergunb 0:8918a71cdbe9 243 {
Sergunb 0:8918a71cdbe9 244 Ipv4Addr srcIpAddr;
Sergunb 0:8918a71cdbe9 245
Sergunb 0:8918a71cdbe9 246 //Select the source IPv4 address and the relevant network
Sergunb 0:8918a71cdbe9 247 //interface to use when pinging the specified host
Sergunb 0:8918a71cdbe9 248 error = ipv4SelectSourceAddr(&interface, targetIpAddr->ipv4Addr,
Sergunb 0:8918a71cdbe9 249 &srcIpAddr);
Sergunb 0:8918a71cdbe9 250
Sergunb 0:8918a71cdbe9 251 //Any error to report?
Sergunb 0:8918a71cdbe9 252 if(error)
Sergunb 0:8918a71cdbe9 253 return error;
Sergunb 0:8918a71cdbe9 254
Sergunb 0:8918a71cdbe9 255 //ICMP Echo Request message
Sergunb 0:8918a71cdbe9 256 message->type = ICMP_TYPE_ECHO_REQUEST;
Sergunb 0:8918a71cdbe9 257 //Message checksum calculation
Sergunb 0:8918a71cdbe9 258 message->checksum = ipCalcChecksum(message, length);
Sergunb 0:8918a71cdbe9 259
Sergunb 0:8918a71cdbe9 260 //Open a raw socket
Sergunb 0:8918a71cdbe9 261 context->socket = socketOpen(SOCKET_TYPE_RAW_IP, SOCKET_IP_PROTO_ICMP);
Sergunb 0:8918a71cdbe9 262 }
Sergunb 0:8918a71cdbe9 263 else
Sergunb 0:8918a71cdbe9 264 #endif
Sergunb 0:8918a71cdbe9 265 #if (IPV6_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 266 //Is target address an IPv6 address?
Sergunb 0:8918a71cdbe9 267 if(targetIpAddr->length == sizeof(Ipv6Addr))
Sergunb 0:8918a71cdbe9 268 {
Sergunb 0:8918a71cdbe9 269 Ipv6PseudoHeader pseudoHeader;
Sergunb 0:8918a71cdbe9 270
Sergunb 0:8918a71cdbe9 271 //Select the source IPv6 address and the relevant network
Sergunb 0:8918a71cdbe9 272 //interface to use when pinging the specified host
Sergunb 0:8918a71cdbe9 273 error = ipv6SelectSourceAddr(&interface, &targetIpAddr->ipv6Addr,
Sergunb 0:8918a71cdbe9 274 &pseudoHeader.srcAddr);
Sergunb 0:8918a71cdbe9 275
Sergunb 0:8918a71cdbe9 276 //Any error to report?
Sergunb 0:8918a71cdbe9 277 if(error)
Sergunb 0:8918a71cdbe9 278 return error;
Sergunb 0:8918a71cdbe9 279
Sergunb 0:8918a71cdbe9 280 //ICMPv6 Echo Request message
Sergunb 0:8918a71cdbe9 281 message->type = ICMPV6_TYPE_ECHO_REQUEST;
Sergunb 0:8918a71cdbe9 282
Sergunb 0:8918a71cdbe9 283 //Format IPv6 pseudo header
Sergunb 0:8918a71cdbe9 284 pseudoHeader.destAddr = targetIpAddr->ipv6Addr;
Sergunb 0:8918a71cdbe9 285 pseudoHeader.length = htonl(length);
Sergunb 0:8918a71cdbe9 286 pseudoHeader.reserved = 0;
Sergunb 0:8918a71cdbe9 287 pseudoHeader.nextHeader = IPV6_ICMPV6_HEADER;
Sergunb 0:8918a71cdbe9 288
Sergunb 0:8918a71cdbe9 289 //Message checksum calculation
Sergunb 0:8918a71cdbe9 290 message->checksum = ipCalcUpperLayerChecksum(&pseudoHeader,
Sergunb 0:8918a71cdbe9 291 sizeof(Ipv6PseudoHeader), message, length);
Sergunb 0:8918a71cdbe9 292
Sergunb 0:8918a71cdbe9 293 //Open a raw socket
Sergunb 0:8918a71cdbe9 294 context->socket = socketOpen(SOCKET_TYPE_RAW_IP, SOCKET_IP_PROTO_ICMPV6);
Sergunb 0:8918a71cdbe9 295 }
Sergunb 0:8918a71cdbe9 296 else
Sergunb 0:8918a71cdbe9 297 #endif
Sergunb 0:8918a71cdbe9 298 //Invalid target address?
Sergunb 0:8918a71cdbe9 299 {
Sergunb 0:8918a71cdbe9 300 //Report an error
Sergunb 0:8918a71cdbe9 301 return ERROR_INVALID_ADDRESS;
Sergunb 0:8918a71cdbe9 302 }
Sergunb 0:8918a71cdbe9 303
Sergunb 0:8918a71cdbe9 304 //Failed to open socket?
Sergunb 0:8918a71cdbe9 305 if(context->socket == NULL)
Sergunb 0:8918a71cdbe9 306 return ERROR_OPEN_FAILED;
Sergunb 0:8918a71cdbe9 307
Sergunb 0:8918a71cdbe9 308 //Set the TTL value to be used
Sergunb 0:8918a71cdbe9 309 context->socket->ttl = ttl;
Sergunb 0:8918a71cdbe9 310
Sergunb 0:8918a71cdbe9 311 //Start of exception handling block
Sergunb 0:8918a71cdbe9 312 do
Sergunb 0:8918a71cdbe9 313 {
Sergunb 0:8918a71cdbe9 314 //Associate the newly created socket with the relevant interface
Sergunb 0:8918a71cdbe9 315 error = socketBindToInterface(context->socket, interface);
Sergunb 0:8918a71cdbe9 316 //Unable to bind the socket to the desired interface?
Sergunb 0:8918a71cdbe9 317 if(error)
Sergunb 0:8918a71cdbe9 318 break;
Sergunb 0:8918a71cdbe9 319
Sergunb 0:8918a71cdbe9 320 //Debug message
Sergunb 0:8918a71cdbe9 321 TRACE_INFO("Sending ICMP echo request to %s (%" PRIuSIZE " bytes)...\r\n",
Sergunb 0:8918a71cdbe9 322 ipAddrToString(targetIpAddr, NULL), length);
Sergunb 0:8918a71cdbe9 323
Sergunb 0:8918a71cdbe9 324 //Send Echo Request message
Sergunb 0:8918a71cdbe9 325 error = socketSendTo(context->socket, targetIpAddr, 0,
Sergunb 0:8918a71cdbe9 326 message, length, NULL, 0);
Sergunb 0:8918a71cdbe9 327 //Failed to send message ?
Sergunb 0:8918a71cdbe9 328 if(error)
Sergunb 0:8918a71cdbe9 329 break;
Sergunb 0:8918a71cdbe9 330
Sergunb 0:8918a71cdbe9 331 //Save the time at which the request was sent
Sergunb 0:8918a71cdbe9 332 context->timestamp = osGetSystemTime();
Sergunb 0:8918a71cdbe9 333
Sergunb 0:8918a71cdbe9 334 //End of exception handling block
Sergunb 0:8918a71cdbe9 335 } while(0);
Sergunb 0:8918a71cdbe9 336
Sergunb 0:8918a71cdbe9 337 //Any error to report?
Sergunb 0:8918a71cdbe9 338 if(error)
Sergunb 0:8918a71cdbe9 339 {
Sergunb 0:8918a71cdbe9 340 //Clean up side effects
Sergunb 0:8918a71cdbe9 341 socketClose(context->socket);
Sergunb 0:8918a71cdbe9 342 context->socket = NULL;
Sergunb 0:8918a71cdbe9 343 }
Sergunb 0:8918a71cdbe9 344
Sergunb 0:8918a71cdbe9 345 //Return status code
Sergunb 0:8918a71cdbe9 346 return error;
Sergunb 0:8918a71cdbe9 347 }
Sergunb 0:8918a71cdbe9 348
Sergunb 0:8918a71cdbe9 349
Sergunb 0:8918a71cdbe9 350 /**
Sergunb 0:8918a71cdbe9 351 * @brief Check whether an incoming ICMP message is acceptable
Sergunb 0:8918a71cdbe9 352 * @param[in] context Pointer to the ping context
Sergunb 0:8918a71cdbe9 353 * @param[in] srcIpAddr Source IP address
Sergunb 0:8918a71cdbe9 354 * @param[in] destIpAddr Destination IP address
Sergunb 0:8918a71cdbe9 355 * @param[in] message Pointer to the incoming ICMP message
Sergunb 0:8918a71cdbe9 356 * @param[in] length Length of the message, in bytes
Sergunb 0:8918a71cdbe9 357 * @return Error code
Sergunb 0:8918a71cdbe9 358 **/
Sergunb 0:8918a71cdbe9 359
Sergunb 0:8918a71cdbe9 360 error_t pingCheckReply(PingContext *context, const IpAddr *srcIpAddr,
Sergunb 0:8918a71cdbe9 361 const IpAddr *destIpAddr, const IcmpEchoMessage *message, size_t length)
Sergunb 0:8918a71cdbe9 362 {
Sergunb 0:8918a71cdbe9 363 size_t i;
Sergunb 0:8918a71cdbe9 364
Sergunb 0:8918a71cdbe9 365 //Check message length
Sergunb 0:8918a71cdbe9 366 if(length != (sizeof(IcmpEchoMessage) + context->dataPayloadSize))
Sergunb 0:8918a71cdbe9 367 return ERROR_INVALID_MESSAGE;
Sergunb 0:8918a71cdbe9 368
Sergunb 0:8918a71cdbe9 369 #if (IPV4_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 370 //Is target address an IPv4 address?
Sergunb 0:8918a71cdbe9 371 if(context->socket->protocol == SOCKET_IP_PROTO_ICMP)
Sergunb 0:8918a71cdbe9 372 {
Sergunb 0:8918a71cdbe9 373 //Check address type
Sergunb 0:8918a71cdbe9 374 if(destIpAddr->length != sizeof(Ipv4Addr))
Sergunb 0:8918a71cdbe9 375 return ERROR_INVALID_MESSAGE;
Sergunb 0:8918a71cdbe9 376
Sergunb 0:8918a71cdbe9 377 //Check message type
Sergunb 0:8918a71cdbe9 378 if(message->type != ICMP_TYPE_ECHO_REPLY)
Sergunb 0:8918a71cdbe9 379 return ERROR_INVALID_MESSAGE;
Sergunb 0:8918a71cdbe9 380
Sergunb 0:8918a71cdbe9 381 //Verify checksum value
Sergunb 0:8918a71cdbe9 382 if(ipCalcChecksum(message, length) != 0x0000)
Sergunb 0:8918a71cdbe9 383 return ERROR_INVALID_MESSAGE;
Sergunb 0:8918a71cdbe9 384 }
Sergunb 0:8918a71cdbe9 385 else
Sergunb 0:8918a71cdbe9 386 #endif
Sergunb 0:8918a71cdbe9 387 #if (IPV6_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 388 //Is target address an IPv6 address?
Sergunb 0:8918a71cdbe9 389 if(context->socket->protocol == SOCKET_IP_PROTO_ICMPV6)
Sergunb 0:8918a71cdbe9 390 {
Sergunb 0:8918a71cdbe9 391 Ipv6PseudoHeader pseudoHeader;
Sergunb 0:8918a71cdbe9 392
Sergunb 0:8918a71cdbe9 393 //Check address type
Sergunb 0:8918a71cdbe9 394 if(destIpAddr->length != sizeof(Ipv6Addr))
Sergunb 0:8918a71cdbe9 395 return ERROR_INVALID_MESSAGE;
Sergunb 0:8918a71cdbe9 396
Sergunb 0:8918a71cdbe9 397 //Check message type
Sergunb 0:8918a71cdbe9 398 if(message->type != ICMPV6_TYPE_ECHO_REPLY)
Sergunb 0:8918a71cdbe9 399 return ERROR_INVALID_MESSAGE;
Sergunb 0:8918a71cdbe9 400
Sergunb 0:8918a71cdbe9 401 //Format IPv6 pseudo header
Sergunb 0:8918a71cdbe9 402 pseudoHeader.srcAddr = srcIpAddr->ipv6Addr;
Sergunb 0:8918a71cdbe9 403 pseudoHeader.destAddr = destIpAddr->ipv6Addr;
Sergunb 0:8918a71cdbe9 404 pseudoHeader.length = htonl(length);
Sergunb 0:8918a71cdbe9 405 pseudoHeader.reserved = 0;
Sergunb 0:8918a71cdbe9 406 pseudoHeader.nextHeader = IPV6_ICMPV6_HEADER;
Sergunb 0:8918a71cdbe9 407
Sergunb 0:8918a71cdbe9 408 //Verify checksum value
Sergunb 0:8918a71cdbe9 409 if(ipCalcUpperLayerChecksum(&pseudoHeader,
Sergunb 0:8918a71cdbe9 410 sizeof(Ipv6PseudoHeader), message, length) != 0x0000)
Sergunb 0:8918a71cdbe9 411 {
Sergunb 0:8918a71cdbe9 412 //The checksum is not valid
Sergunb 0:8918a71cdbe9 413 return ERROR_INVALID_MESSAGE;
Sergunb 0:8918a71cdbe9 414 }
Sergunb 0:8918a71cdbe9 415 }
Sergunb 0:8918a71cdbe9 416 else
Sergunb 0:8918a71cdbe9 417 #endif
Sergunb 0:8918a71cdbe9 418 //Invalid target address?
Sergunb 0:8918a71cdbe9 419 {
Sergunb 0:8918a71cdbe9 420 //Report an error
Sergunb 0:8918a71cdbe9 421 return ERROR_INVALID_ADDRESS;
Sergunb 0:8918a71cdbe9 422 }
Sergunb 0:8918a71cdbe9 423
Sergunb 0:8918a71cdbe9 424 //Make sure the response identifier matches the request identifier
Sergunb 0:8918a71cdbe9 425 if(message->identifier != context->identifier)
Sergunb 0:8918a71cdbe9 426 return ERROR_INVALID_MESSAGE;
Sergunb 0:8918a71cdbe9 427 //Make sure the sequence number is correct
Sergunb 0:8918a71cdbe9 428 if(message->sequenceNumber != context->sequenceNumber)
Sergunb 0:8918a71cdbe9 429 return ERROR_INVALID_MESSAGE;
Sergunb 0:8918a71cdbe9 430
Sergunb 0:8918a71cdbe9 431 //Verify data payload
Sergunb 0:8918a71cdbe9 432 for(i = 0; i < context->dataPayloadSize; i++)
Sergunb 0:8918a71cdbe9 433 {
Sergunb 0:8918a71cdbe9 434 //Compare received data against expected data pattern
Sergunb 0:8918a71cdbe9 435 if(message->data[i] != (i & 0xFF))
Sergunb 0:8918a71cdbe9 436 return ERROR_INVALID_MESSAGE;
Sergunb 0:8918a71cdbe9 437 }
Sergunb 0:8918a71cdbe9 438
Sergunb 0:8918a71cdbe9 439 //The ICMP Echo Reply message is acceptable
Sergunb 0:8918a71cdbe9 440 return NO_ERROR;
Sergunb 0:8918a71cdbe9 441 }
Sergunb 0:8918a71cdbe9 442
Sergunb 0:8918a71cdbe9 443
Sergunb 0:8918a71cdbe9 444 /**
Sergunb 0:8918a71cdbe9 445 * @brief Wait for a matching ICMP Echo Reply message
Sergunb 0:8918a71cdbe9 446 * @param[in] context Pointer to the ping context
Sergunb 0:8918a71cdbe9 447 * @param[out] targetIpAddr IP address of the remote host (optional parameter)
Sergunb 0:8918a71cdbe9 448 * @param[out] rtt Round-trip time (optional parameter)
Sergunb 0:8918a71cdbe9 449 * @return Error code
Sergunb 0:8918a71cdbe9 450 **/
Sergunb 0:8918a71cdbe9 451
Sergunb 0:8918a71cdbe9 452 error_t pingWaitForReply(PingContext *context,
Sergunb 0:8918a71cdbe9 453 IpAddr *targetIpAddr, systime_t *rtt)
Sergunb 0:8918a71cdbe9 454 {
Sergunb 0:8918a71cdbe9 455 error_t error;
Sergunb 0:8918a71cdbe9 456 size_t length;
Sergunb 0:8918a71cdbe9 457 systime_t time;
Sergunb 0:8918a71cdbe9 458 systime_t timeout;
Sergunb 0:8918a71cdbe9 459 IpAddr srcIpAddr;
Sergunb 0:8918a71cdbe9 460 IpAddr destIpAddr;
Sergunb 0:8918a71cdbe9 461
Sergunb 0:8918a71cdbe9 462 //Invalid context?
Sergunb 0:8918a71cdbe9 463 if(context == NULL)
Sergunb 0:8918a71cdbe9 464 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 465
Sergunb 0:8918a71cdbe9 466 //Wait for an ICMP Echo Reply message
Sergunb 0:8918a71cdbe9 467 do
Sergunb 0:8918a71cdbe9 468 {
Sergunb 0:8918a71cdbe9 469 //Get current time
Sergunb 0:8918a71cdbe9 470 time = osGetSystemTime();
Sergunb 0:8918a71cdbe9 471
Sergunb 0:8918a71cdbe9 472 //Compute the timeout to be used
Sergunb 0:8918a71cdbe9 473 if(timeCompare(time, context->timestamp + context->timeout) < 0)
Sergunb 0:8918a71cdbe9 474 timeout = context->timestamp + context->timeout - time;
Sergunb 0:8918a71cdbe9 475 else
Sergunb 0:8918a71cdbe9 476 timeout = 0;
Sergunb 0:8918a71cdbe9 477
Sergunb 0:8918a71cdbe9 478 //Adjust receive timeout
Sergunb 0:8918a71cdbe9 479 error = socketSetTimeout(context->socket, timeout);
Sergunb 0:8918a71cdbe9 480 //Any error to report?
Sergunb 0:8918a71cdbe9 481 if(error)
Sergunb 0:8918a71cdbe9 482 break;
Sergunb 0:8918a71cdbe9 483
Sergunb 0:8918a71cdbe9 484 //Wait for an incoming ICMP message
Sergunb 0:8918a71cdbe9 485 error = socketReceiveEx(context->socket, &srcIpAddr, NULL,
Sergunb 0:8918a71cdbe9 486 &destIpAddr, context->buffer, PING_BUFFER_SIZE, &length, 0);
Sergunb 0:8918a71cdbe9 487
Sergunb 0:8918a71cdbe9 488 #if (NET_RTOS_SUPPORT == DISABLED)
Sergunb 0:8918a71cdbe9 489 //Catch timeout exception
Sergunb 0:8918a71cdbe9 490 if(error == ERROR_TIMEOUT)
Sergunb 0:8918a71cdbe9 491 error = ERROR_WOULD_BLOCK;
Sergunb 0:8918a71cdbe9 492 #endif
Sergunb 0:8918a71cdbe9 493
Sergunb 0:8918a71cdbe9 494 //Get current time
Sergunb 0:8918a71cdbe9 495 time = osGetSystemTime();
Sergunb 0:8918a71cdbe9 496
Sergunb 0:8918a71cdbe9 497 //Check status code
Sergunb 0:8918a71cdbe9 498 if(!error)
Sergunb 0:8918a71cdbe9 499 {
Sergunb 0:8918a71cdbe9 500 //Check whether the incoming ICMP message is acceptable
Sergunb 0:8918a71cdbe9 501 error = pingCheckReply(context, &srcIpAddr, &destIpAddr,
Sergunb 0:8918a71cdbe9 502 (IcmpEchoMessage *) context->buffer, length);
Sergunb 0:8918a71cdbe9 503 }
Sergunb 0:8918a71cdbe9 504
Sergunb 0:8918a71cdbe9 505 //Check status code
Sergunb 0:8918a71cdbe9 506 if(!error)
Sergunb 0:8918a71cdbe9 507 {
Sergunb 0:8918a71cdbe9 508 //Calculate round-trip time
Sergunb 0:8918a71cdbe9 509 context->rtt = time - context->timestamp;
Sergunb 0:8918a71cdbe9 510
Sergunb 0:8918a71cdbe9 511 //Debug message
Sergunb 0:8918a71cdbe9 512 TRACE_INFO("ICMP echo reply received from %s (%" PRIu32 " ms)...\r\n",
Sergunb 0:8918a71cdbe9 513 ipAddrToString(&srcIpAddr, NULL), context->rtt);
Sergunb 0:8918a71cdbe9 514
Sergunb 0:8918a71cdbe9 515 //Return the IP address of the host
Sergunb 0:8918a71cdbe9 516 if(targetIpAddr != NULL)
Sergunb 0:8918a71cdbe9 517 *targetIpAddr = srcIpAddr;
Sergunb 0:8918a71cdbe9 518
Sergunb 0:8918a71cdbe9 519 //Return the round-trip time
Sergunb 0:8918a71cdbe9 520 if(rtt != NULL)
Sergunb 0:8918a71cdbe9 521 *rtt = context->rtt;
Sergunb 0:8918a71cdbe9 522 }
Sergunb 0:8918a71cdbe9 523 else
Sergunb 0:8918a71cdbe9 524 {
Sergunb 0:8918a71cdbe9 525 //Timeout value exceeded?
Sergunb 0:8918a71cdbe9 526 if(timeCompare(time, context->timestamp + context->timeout) >= 0)
Sergunb 0:8918a71cdbe9 527 {
Sergunb 0:8918a71cdbe9 528 //Report an error
Sergunb 0:8918a71cdbe9 529 error = ERROR_TIMEOUT;
Sergunb 0:8918a71cdbe9 530 }
Sergunb 0:8918a71cdbe9 531 }
Sergunb 0:8918a71cdbe9 532
Sergunb 0:8918a71cdbe9 533 //Wait for the next incoming ICMP message
Sergunb 0:8918a71cdbe9 534 } while(error == ERROR_INVALID_MESSAGE);
Sergunb 0:8918a71cdbe9 535
Sergunb 0:8918a71cdbe9 536 //Return status code
Sergunb 0:8918a71cdbe9 537 return error;
Sergunb 0:8918a71cdbe9 538 }
Sergunb 0:8918a71cdbe9 539
Sergunb 0:8918a71cdbe9 540
Sergunb 0:8918a71cdbe9 541 /**
Sergunb 0:8918a71cdbe9 542 * @brief Release ping context
Sergunb 0:8918a71cdbe9 543 * @param[in] context Pointer to the ping context
Sergunb 0:8918a71cdbe9 544 **/
Sergunb 0:8918a71cdbe9 545
Sergunb 0:8918a71cdbe9 546 void pingRelease(PingContext *context)
Sergunb 0:8918a71cdbe9 547 {
Sergunb 0:8918a71cdbe9 548 //Make sure the context is valid
Sergunb 0:8918a71cdbe9 549 if(context != NULL)
Sergunb 0:8918a71cdbe9 550 {
Sergunb 0:8918a71cdbe9 551 //Close underlying socket
Sergunb 0:8918a71cdbe9 552 if(context->socket != NULL)
Sergunb 0:8918a71cdbe9 553 {
Sergunb 0:8918a71cdbe9 554 socketClose(context->socket);
Sergunb 0:8918a71cdbe9 555 context->socket = NULL;
Sergunb 0:8918a71cdbe9 556 }
Sergunb 0:8918a71cdbe9 557 }
Sergunb 0:8918a71cdbe9 558 }
Sergunb 0:8918a71cdbe9 559
Sergunb 0:8918a71cdbe9 560 #endif
Sergunb 0:8918a71cdbe9 561