[21580dd] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Lukas Mejdrech
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup ping
|
---|
[2721a75] | 30 | * @{
|
---|
[21580dd] | 31 | */
|
---|
| 32 |
|
---|
| 33 | /** @file
|
---|
[2721a75] | 34 | * Packet Internet Network Grouper.
|
---|
[21580dd] | 35 | */
|
---|
| 36 |
|
---|
[ffa2c8ef] | 37 | #include <async.h>
|
---|
[21580dd] | 38 | #include <stdio.h>
|
---|
[19f857a] | 39 | #include <str.h>
|
---|
[21580dd] | 40 | #include <task.h>
|
---|
| 41 | #include <time.h>
|
---|
| 42 | #include <ipc/services.h>
|
---|
[2721a75] | 43 | #include <str_error.h>
|
---|
[e98b1d5] | 44 | #include <errno.h>
|
---|
[2721a75] | 45 | #include <arg_parse.h>
|
---|
[21580dd] | 46 |
|
---|
[753bca3] | 47 | #include <net/icmp_api.h>
|
---|
[e4554d4] | 48 | #include <net/in.h>
|
---|
| 49 | #include <net/in6.h>
|
---|
| 50 | #include <net/inet.h>
|
---|
[d9e2e0e] | 51 | #include <net/socket_parse.h>
|
---|
[ff2a62d] | 52 | #include <net/ip_codes.h>
|
---|
[21580dd] | 53 |
|
---|
[849ed54] | 54 | #include "print_error.h"
|
---|
[21580dd] | 55 |
|
---|
[2721a75] | 56 | #define NAME "ping"
|
---|
[21580dd] | 57 |
|
---|
[2721a75] | 58 | #define CL_OK 0
|
---|
| 59 | #define CL_USAGE -1
|
---|
| 60 | #define CL_MISSING -2
|
---|
| 61 | #define CL_INVALID -3
|
---|
| 62 | #define CL_UNSUPPORTED -4
|
---|
| 63 | #define CL_ERROR -5
|
---|
[21580dd] | 64 |
|
---|
[2721a75] | 65 | /** Ping configuration
|
---|
| 66 | *
|
---|
[21580dd] | 67 | */
|
---|
[2721a75] | 68 | typedef struct {
|
---|
| 69 | bool verbose; /**< Verbose printouts. */
|
---|
| 70 | size_t size; /**< Outgoing packet size. */
|
---|
| 71 | unsigned int count; /**< Number of packets to send. */
|
---|
| 72 | suseconds_t timeout; /**< Reply wait timeout. */
|
---|
| 73 | int af; /**< Address family. */
|
---|
| 74 | ip_tos_t tos; /**< Type of service. */
|
---|
| 75 | ip_ttl_t ttl; /**< Time-to-live. */
|
---|
| 76 | bool fragments; /**< Fragmentation. */
|
---|
| 77 |
|
---|
| 78 | char *dest_addr; /**< Destination address. */
|
---|
| 79 | struct sockaddr_in dest; /**< IPv4 destionation. */
|
---|
| 80 | struct sockaddr_in6 dest6; /**< IPv6 destionation. */
|
---|
| 81 |
|
---|
| 82 | struct sockaddr *dest_raw; /**< Raw destination address. */
|
---|
| 83 | socklen_t dest_len; /**< Raw destination address length. */
|
---|
| 84 |
|
---|
| 85 | /** Converted address string. */
|
---|
| 86 | char dest_str[INET6_ADDRSTRLEN];
|
---|
| 87 | } ping_config_t;
|
---|
[21580dd] | 88 |
|
---|
| 89 |
|
---|
[2721a75] | 90 | static void usage(void)
|
---|
| 91 | {
|
---|
| 92 | printf(
|
---|
| 93 | "Usage: ping [-c count] [-s size] [-W timeout] [-f family] [-t ttl]\n" \
|
---|
| 94 | " [-Q tos] [--dont_fragment] destination\n" \
|
---|
| 95 | "\n" \
|
---|
| 96 | "Options:\n" \
|
---|
| 97 | "\t-c count\n" \
|
---|
| 98 | "\t--count=count\n" \
|
---|
| 99 | "\t\tNumber of outgoing packets (default: 4)\n" \
|
---|
| 100 | "\n" \
|
---|
| 101 | "\t-s size\n" \
|
---|
| 102 | "\t--size=bytes\n" \
|
---|
| 103 | "\t\tOutgoing packet size (default: 56 bytes)\n" \
|
---|
| 104 | "\n" \
|
---|
| 105 | "\t-W timeout\n" \
|
---|
| 106 | "\t--timeout=ms\n" \
|
---|
| 107 | "\t\tReply wait timeout (default: 3000 ms)\n" \
|
---|
| 108 | "\n" \
|
---|
| 109 | "\t-f family\n" \
|
---|
| 110 | "\t--family=family\n" \
|
---|
| 111 | "\t\tDestination address family, AF_INET or AF_INET6 (default: AF_INET)\n" \
|
---|
| 112 | "\n" \
|
---|
| 113 | "\t-t ttl\n" \
|
---|
| 114 | "\t--ttl=ttl\n" \
|
---|
| 115 | "\t\tOutgoing packet time-to-live (default: 0)\n" \
|
---|
| 116 | "\n" \
|
---|
| 117 | "\t-Q tos\n" \
|
---|
| 118 | "\t--tos=tos\n" \
|
---|
| 119 | "\t\tOutgoing packet type of service (default: 0)\n" \
|
---|
| 120 | "\n" \
|
---|
| 121 | "\t--dont_fragment\n" \
|
---|
| 122 | "\t\tDisable packet fragmentation (default: enabled)\n" \
|
---|
| 123 | "\n" \
|
---|
| 124 | "\t-v\n" \
|
---|
| 125 | "\t--verbose\n" \
|
---|
| 126 | "\t\tVerbose operation\n" \
|
---|
| 127 | "\n" \
|
---|
| 128 | "\t-h\n" \
|
---|
| 129 | "\t--help\n" \
|
---|
| 130 | "\t\tPrint this usage information\n"
|
---|
| 131 | );
|
---|
| 132 | }
|
---|
[21580dd] | 133 |
|
---|
[2721a75] | 134 | static int args_parse(int argc, char *argv[], ping_config_t *config)
|
---|
| 135 | {
|
---|
| 136 | if (argc < 2)
|
---|
| 137 | return CL_USAGE;
|
---|
| 138 |
|
---|
| 139 | int i;
|
---|
| 140 | int ret;
|
---|
| 141 |
|
---|
| 142 | for (i = 1; i < argc; i++) {
|
---|
| 143 |
|
---|
| 144 | /* Not an option */
|
---|
| 145 | if (argv[i][0] != '-')
|
---|
| 146 | break;
|
---|
| 147 |
|
---|
| 148 | /* Options terminator */
|
---|
| 149 | if (str_cmp(argv[i], "--") == 0) {
|
---|
| 150 | i++;
|
---|
| 151 | break;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | int off;
|
---|
| 155 |
|
---|
| 156 | /* Usage */
|
---|
[d8e3467] | 157 | if ((off = arg_parse_short_long(argv[i], "-h", "--help")) != -1)
|
---|
[2721a75] | 158 | return CL_USAGE;
|
---|
| 159 |
|
---|
| 160 | /* Verbose */
|
---|
[d8e3467] | 161 | if ((off = arg_parse_short_long(argv[i], "-v", "--verbose")) != -1) {
|
---|
[2721a75] | 162 | config->verbose = true;
|
---|
| 163 | continue;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | /* Don't fragment */
|
---|
| 167 | if (str_cmp(argv[i], "--dont_fragment") == 0) {
|
---|
| 168 | config->fragments = false;
|
---|
| 169 | continue;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /* Count */
|
---|
[d8e3467] | 173 | if ((off = arg_parse_short_long(argv[i], "-c", "--count=")) != -1) {
|
---|
| 174 | int tmp;
|
---|
[2721a75] | 175 | ret = arg_parse_int(argc, argv, &i, &tmp, off);
|
---|
| 176 |
|
---|
| 177 | if ((ret != EOK) || (tmp < 0))
|
---|
| 178 | return i;
|
---|
| 179 |
|
---|
| 180 | config->count = (unsigned int) tmp;
|
---|
| 181 | continue;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | /* Outgoing packet size */
|
---|
[d8e3467] | 185 | if ((off = arg_parse_short_long(argv[i], "-s", "--size=")) != -1) {
|
---|
| 186 | int tmp;
|
---|
[2721a75] | 187 | ret = arg_parse_int(argc, argv, &i, &tmp, off);
|
---|
| 188 |
|
---|
| 189 | if ((ret != EOK) || (tmp < 0))
|
---|
| 190 | return i;
|
---|
| 191 |
|
---|
| 192 | config->size = (size_t) tmp;
|
---|
| 193 | continue;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | /* Reply wait timeout */
|
---|
[d8e3467] | 197 | if ((off = arg_parse_short_long(argv[i], "-W", "--timeout=")) != -1) {
|
---|
| 198 | int tmp;
|
---|
[2721a75] | 199 | ret = arg_parse_int(argc, argv, &i, &tmp, off);
|
---|
| 200 |
|
---|
| 201 | if ((ret != EOK) || (tmp < 0))
|
---|
| 202 | return i;
|
---|
| 203 |
|
---|
| 204 | config->timeout = (suseconds_t) tmp;
|
---|
| 205 | continue;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | /* Address family */
|
---|
[d8e3467] | 209 | if ((off = arg_parse_short_long(argv[i], "-f", "--family=")) != -1) {
|
---|
[2721a75] | 210 | ret = arg_parse_name_int(argc, argv, &i, &config->af, off,
|
---|
| 211 | socket_parse_address_family);
|
---|
| 212 |
|
---|
| 213 | if (ret != EOK)
|
---|
| 214 | return i;
|
---|
| 215 |
|
---|
| 216 | continue;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | /* Type of service */
|
---|
[d8e3467] | 220 | if ((off = arg_parse_short_long(argv[i], "-Q", "--tos=")) != -1) {
|
---|
| 221 | int tmp;
|
---|
[2721a75] | 222 | ret = arg_parse_name_int(argc, argv, &i, &tmp, off,
|
---|
| 223 | socket_parse_address_family);
|
---|
| 224 |
|
---|
| 225 | if ((ret != EOK) || (tmp < 0))
|
---|
| 226 | return i;
|
---|
| 227 |
|
---|
| 228 | config->tos = (ip_tos_t) tmp;
|
---|
| 229 | continue;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | /* Time to live */
|
---|
[d8e3467] | 233 | if ((off = arg_parse_short_long(argv[i], "-t", "--ttl=")) != -1) {
|
---|
| 234 | int tmp;
|
---|
[2721a75] | 235 | ret = arg_parse_name_int(argc, argv, &i, &tmp, off,
|
---|
| 236 | socket_parse_address_family);
|
---|
| 237 |
|
---|
| 238 | if ((ret != EOK) || (tmp < 0))
|
---|
| 239 | return i;
|
---|
| 240 |
|
---|
| 241 | config->ttl = (ip_ttl_t) tmp;
|
---|
| 242 | continue;
|
---|
[21580dd] | 243 | }
|
---|
| 244 | }
|
---|
[2721a75] | 245 |
|
---|
| 246 | if (i >= argc)
|
---|
| 247 | return CL_MISSING;
|
---|
| 248 |
|
---|
| 249 | config->dest_addr = argv[i];
|
---|
| 250 |
|
---|
| 251 | /* Resolve destionation address */
|
---|
| 252 | switch (config->af) {
|
---|
| 253 | case AF_INET:
|
---|
| 254 | config->dest_raw = (struct sockaddr *) &config->dest;
|
---|
| 255 | config->dest_len = sizeof(config->dest);
|
---|
| 256 | config->dest.sin_family = config->af;
|
---|
| 257 | ret = inet_pton(config->af, config->dest_addr,
|
---|
| 258 | (uint8_t *) &config->dest.sin_addr.s_addr);
|
---|
| 259 | break;
|
---|
| 260 | case AF_INET6:
|
---|
| 261 | config->dest_raw = (struct sockaddr *) &config->dest6;
|
---|
| 262 | config->dest_len = sizeof(config->dest6);
|
---|
| 263 | config->dest6.sin6_family = config->af;
|
---|
| 264 | ret = inet_pton(config->af, config->dest_addr,
|
---|
| 265 | (uint8_t *) &config->dest6.sin6_addr.s6_addr);
|
---|
| 266 | break;
|
---|
| 267 | default:
|
---|
| 268 | return CL_UNSUPPORTED;
|
---|
[3be62bc] | 269 | }
|
---|
[2721a75] | 270 |
|
---|
| 271 | if (ret != EOK)
|
---|
| 272 | return CL_INVALID;
|
---|
| 273 |
|
---|
| 274 | /* Convert destination address back to string */
|
---|
| 275 | switch (config->af) {
|
---|
| 276 | case AF_INET:
|
---|
| 277 | ret = inet_ntop(config->af,
|
---|
| 278 | (uint8_t *) &config->dest.sin_addr.s_addr,
|
---|
| 279 | config->dest_str, sizeof(config->dest_str));
|
---|
| 280 | break;
|
---|
| 281 | case AF_INET6:
|
---|
| 282 | ret = inet_ntop(config->af,
|
---|
| 283 | (uint8_t *) &config->dest6.sin6_addr.s6_addr,
|
---|
| 284 | config->dest_str, sizeof(config->dest_str));
|
---|
| 285 | break;
|
---|
| 286 | default:
|
---|
| 287 | return CL_UNSUPPORTED;
|
---|
[21580dd] | 288 | }
|
---|
[2721a75] | 289 |
|
---|
| 290 | if (ret != EOK)
|
---|
| 291 | return CL_ERROR;
|
---|
| 292 |
|
---|
| 293 | return CL_OK;
|
---|
| 294 | }
|
---|
[21580dd] | 295 |
|
---|
[2721a75] | 296 | int main(int argc, char *argv[])
|
---|
| 297 | {
|
---|
| 298 | ping_config_t config;
|
---|
| 299 |
|
---|
| 300 | /* Default configuration */
|
---|
| 301 | config.verbose = false;
|
---|
| 302 | config.size = 56;
|
---|
| 303 | config.count = 4;
|
---|
| 304 | config.timeout = 3000;
|
---|
| 305 | config.af = AF_INET;
|
---|
| 306 | config.tos = 0;
|
---|
| 307 | config.ttl = 0;
|
---|
| 308 | config.fragments = true;
|
---|
| 309 |
|
---|
| 310 | int ret = args_parse(argc, argv, &config);
|
---|
| 311 |
|
---|
| 312 | switch (ret) {
|
---|
| 313 | case CL_OK:
|
---|
| 314 | break;
|
---|
| 315 | case CL_USAGE:
|
---|
| 316 | usage();
|
---|
| 317 | return 0;
|
---|
| 318 | case CL_MISSING:
|
---|
| 319 | fprintf(stderr, "%s: Destination address missing\n", NAME);
|
---|
| 320 | return 1;
|
---|
| 321 | case CL_INVALID:
|
---|
| 322 | fprintf(stderr, "%s: Destination address '%s' invalid or malformed\n",
|
---|
| 323 | NAME, config.dest_addr);
|
---|
| 324 | return 2;
|
---|
| 325 | case CL_UNSUPPORTED:
|
---|
| 326 | fprintf(stderr, "%s: Destination address '%s' unsupported\n",
|
---|
| 327 | NAME, config.dest_addr);
|
---|
| 328 | return 3;
|
---|
| 329 | case CL_ERROR:
|
---|
| 330 | fprintf(stderr, "%s: Destination address '%s' error\n",
|
---|
| 331 | NAME, config.dest_addr);
|
---|
| 332 | return 4;
|
---|
| 333 | default:
|
---|
| 334 | fprintf(stderr, "%s: Unknown or invalid option '%s'\n", NAME,
|
---|
| 335 | argv[ret]);
|
---|
| 336 | return 5;
|
---|
[21580dd] | 337 | }
|
---|
[2721a75] | 338 |
|
---|
[7e752b2] | 339 | printf("PING %s (%s) %zu(%zu) bytes of data\n", config.dest_addr,
|
---|
[2721a75] | 340 | config.dest_str, config.size, config.size);
|
---|
| 341 |
|
---|
[f1938c6] | 342 | int icmp_phone = icmp_connect_module(ICMP_CONNECT_TIMEOUT);
|
---|
[2721a75] | 343 | if (icmp_phone < 0) {
|
---|
| 344 | fprintf(stderr, "%s: Unable to connect to ICMP service (%s)\n", NAME,
|
---|
| 345 | str_error(icmp_phone));
|
---|
[1a0fb3f8] | 346 | return icmp_phone;
|
---|
[21580dd] | 347 | }
|
---|
[2721a75] | 348 |
|
---|
| 349 | unsigned int seq;
|
---|
| 350 | for (seq = 0; seq < config.count; seq++) {
|
---|
| 351 | struct timeval t0;
|
---|
| 352 | ret = gettimeofday(&t0, NULL);
|
---|
| 353 | if (ret != EOK) {
|
---|
| 354 | fprintf(stderr, "%s: gettimeofday failed (%s)\n", NAME,
|
---|
| 355 | str_error(ret));
|
---|
| 356 |
|
---|
[ffa2c8ef] | 357 | async_hangup(icmp_phone);
|
---|
[2721a75] | 358 | return ret;
|
---|
[21580dd] | 359 | }
|
---|
[2721a75] | 360 |
|
---|
| 361 | /* Ping! */
|
---|
| 362 | int result = icmp_echo_msg(icmp_phone, config.size, config.timeout,
|
---|
| 363 | config.ttl, config.tos, !config.fragments, config.dest_raw,
|
---|
| 364 | config.dest_len);
|
---|
| 365 |
|
---|
| 366 | struct timeval t1;
|
---|
| 367 | ret = gettimeofday(&t1, NULL);
|
---|
| 368 | if (ret != EOK) {
|
---|
| 369 | fprintf(stderr, "%s: gettimeofday failed (%s)\n", NAME,
|
---|
| 370 | str_error(ret));
|
---|
| 371 |
|
---|
[ffa2c8ef] | 372 | async_hangup(icmp_phone);
|
---|
[2721a75] | 373 | return ret;
|
---|
[21580dd] | 374 | }
|
---|
[2721a75] | 375 |
|
---|
| 376 | suseconds_t elapsed = tv_sub(&t1, &t0);
|
---|
| 377 |
|
---|
| 378 | switch (result) {
|
---|
| 379 | case ICMP_ECHO:
|
---|
[7e752b2] | 380 | printf("%zu bytes from ? (?): icmp_seq=%u ttl=? time=%ld.%04ld\n",
|
---|
[2721a75] | 381 | config.size, seq, elapsed / 1000, elapsed % 1000);
|
---|
| 382 | break;
|
---|
| 383 | case ETIMEOUT:
|
---|
[7e752b2] | 384 | printf("%zu bytes from ? (?): icmp_seq=%u Timed out\n",
|
---|
[2721a75] | 385 | config.size, seq);
|
---|
| 386 | break;
|
---|
| 387 | default:
|
---|
| 388 | print_error(stdout, result, NULL, "\n");
|
---|
[21580dd] | 389 | }
|
---|
| 390 | }
|
---|
[2721a75] | 391 |
|
---|
[ffa2c8ef] | 392 | async_hangup(icmp_phone);
|
---|
[2721a75] | 393 |
|
---|
| 394 | return 0;
|
---|
[3be62bc] | 395 | }
|
---|
| 396 |
|
---|
[21580dd] | 397 | /** @}
|
---|
| 398 | */
|
---|