| 1 | /*
|
|---|
| 2 | * Copyright (c) 2012 Jiri Svoboda
|
|---|
| 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
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file ICMP echo utility.
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <async.h>
|
|---|
| 36 | #include <bool.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <fibril_synch.h>
|
|---|
| 39 | #include <inet/inetping.h>
|
|---|
| 40 | #include <io/console.h>
|
|---|
| 41 | #include <stdio.h>
|
|---|
| 42 | #include <stdlib.h>
|
|---|
| 43 | #include <sys/types.h>
|
|---|
| 44 |
|
|---|
| 45 | #define NAME "ping"
|
|---|
| 46 |
|
|---|
| 47 | /** Delay between subsequent ping requests in microseconds */
|
|---|
| 48 | #define PING_DELAY (1000 * 1000)
|
|---|
| 49 |
|
|---|
| 50 | /** Ping request timeout in microseconds */
|
|---|
| 51 | #define PING_TIMEOUT (1000 * 1000)
|
|---|
| 52 |
|
|---|
| 53 | static int ping_ev_recv(inetping_sdu_t *);
|
|---|
| 54 |
|
|---|
| 55 | static bool done = false;
|
|---|
| 56 | static FIBRIL_CONDVAR_INITIALIZE(done_cv);
|
|---|
| 57 | static FIBRIL_MUTEX_INITIALIZE(done_lock);
|
|---|
| 58 |
|
|---|
| 59 | static inetping_ev_ops_t ev_ops = {
|
|---|
| 60 | .recv = ping_ev_recv
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | static inet_addr_t src_addr;
|
|---|
| 64 | static inet_addr_t dest_addr;
|
|---|
| 65 |
|
|---|
| 66 | static bool ping_repeat = false;
|
|---|
| 67 |
|
|---|
| 68 | static void print_syntax(void)
|
|---|
| 69 | {
|
|---|
| 70 | printf("syntax: " NAME " [-r] <addr>\n");
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | static int addr_parse(const char *text, inet_addr_t *addr)
|
|---|
| 74 | {
|
|---|
| 75 | unsigned long a[4];
|
|---|
| 76 | char *cp = (char *)text;
|
|---|
| 77 | int i;
|
|---|
| 78 |
|
|---|
| 79 | for (i = 0; i < 3; i++) {
|
|---|
| 80 | a[i] = strtoul(cp, &cp, 10);
|
|---|
| 81 | if (*cp != '.')
|
|---|
| 82 | return EINVAL;
|
|---|
| 83 | ++cp;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | a[3] = strtoul(cp, &cp, 10);
|
|---|
| 87 | if (*cp != '\0')
|
|---|
| 88 | return EINVAL;
|
|---|
| 89 |
|
|---|
| 90 | addr->ipv4 = 0;
|
|---|
| 91 | for (i = 0; i < 4; i++) {
|
|---|
| 92 | if (a[i] > 255)
|
|---|
| 93 | return EINVAL;
|
|---|
| 94 | addr->ipv4 = (addr->ipv4 << 8) | a[i];
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | return EOK;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | static int addr_format(inet_addr_t *addr, char **bufp)
|
|---|
| 101 | {
|
|---|
| 102 | int rc;
|
|---|
| 103 |
|
|---|
| 104 | rc = asprintf(bufp, "%d.%d.%d.%d", addr->ipv4 >> 24,
|
|---|
| 105 | (addr->ipv4 >> 16) & 0xff, (addr->ipv4 >> 8) & 0xff,
|
|---|
| 106 | addr->ipv4 & 0xff);
|
|---|
| 107 |
|
|---|
| 108 | if (rc < 0)
|
|---|
| 109 | return ENOMEM;
|
|---|
| 110 |
|
|---|
| 111 | return EOK;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | static void ping_signal_done(void)
|
|---|
| 115 | {
|
|---|
| 116 | fibril_mutex_lock(&done_lock);
|
|---|
| 117 | done = true;
|
|---|
| 118 | fibril_mutex_unlock(&done_lock);
|
|---|
| 119 | fibril_condvar_broadcast(&done_cv);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | static int ping_ev_recv(inetping_sdu_t *sdu)
|
|---|
| 123 | {
|
|---|
| 124 | char *asrc, *adest;
|
|---|
| 125 | int rc;
|
|---|
| 126 |
|
|---|
| 127 | rc = addr_format(&sdu->src, &asrc);
|
|---|
| 128 | if (rc != EOK)
|
|---|
| 129 | return ENOMEM;
|
|---|
| 130 |
|
|---|
| 131 | rc = addr_format(&sdu->dest, &adest);
|
|---|
| 132 | if (rc != EOK) {
|
|---|
| 133 | free(asrc);
|
|---|
| 134 | return ENOMEM;
|
|---|
| 135 | }
|
|---|
| 136 | printf("Received ICMP echo reply: from %s to %s, seq. no %u, "
|
|---|
| 137 | "payload size %zu\n", asrc, adest, sdu->seq_no, sdu->size);
|
|---|
| 138 |
|
|---|
| 139 | if (!ping_repeat) {
|
|---|
| 140 | ping_signal_done();
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | free(asrc);
|
|---|
| 144 | free(adest);
|
|---|
| 145 | return EOK;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | static int ping_send(uint16_t seq_no)
|
|---|
| 149 | {
|
|---|
| 150 | inetping_sdu_t sdu;
|
|---|
| 151 | int rc;
|
|---|
| 152 |
|
|---|
| 153 | sdu.src = src_addr;
|
|---|
| 154 | sdu.dest = dest_addr;
|
|---|
| 155 | sdu.seq_no = seq_no;
|
|---|
| 156 | sdu.data = (void *) "foo";
|
|---|
| 157 | sdu.size = 3;
|
|---|
| 158 |
|
|---|
| 159 | rc = inetping_send(&sdu);
|
|---|
| 160 | if (rc != EOK) {
|
|---|
| 161 | printf(NAME ": Failed sending echo request (%d).\n", rc);
|
|---|
| 162 | return rc;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | return EOK;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | static int transmit_fibril(void *arg)
|
|---|
| 169 | {
|
|---|
| 170 | uint16_t seq_no = 0;
|
|---|
| 171 |
|
|---|
| 172 | while (true) {
|
|---|
| 173 | fibril_mutex_lock(&done_lock);
|
|---|
| 174 | if (done) {
|
|---|
| 175 | fibril_mutex_unlock(&done_lock);
|
|---|
| 176 | return 0;
|
|---|
| 177 | }
|
|---|
| 178 | fibril_mutex_unlock(&done_lock);
|
|---|
| 179 |
|
|---|
| 180 | (void) ping_send(++seq_no);
|
|---|
| 181 | async_usleep(PING_DELAY);
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | return 0;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | static int input_fibril(void *arg)
|
|---|
| 188 | {
|
|---|
| 189 | console_ctrl_t *con;
|
|---|
| 190 | kbd_event_t ev;
|
|---|
| 191 |
|
|---|
| 192 | con = console_init(stdin, stdout);
|
|---|
| 193 | printf("[Press Ctrl-Q to quit]\n");
|
|---|
| 194 |
|
|---|
| 195 | while (true) {
|
|---|
| 196 | if (!console_get_kbd_event(con, &ev))
|
|---|
| 197 | break;
|
|---|
| 198 |
|
|---|
| 199 | if (ev.type == KEY_PRESS && (ev.mods & (KM_ALT | KM_SHIFT)) ==
|
|---|
| 200 | 0 && (ev.mods & KM_CTRL) != 0) {
|
|---|
| 201 | /* Ctrl+key */
|
|---|
| 202 | if (ev.key == KC_Q) {
|
|---|
| 203 | ping_signal_done();
|
|---|
| 204 | return 0;
|
|---|
| 205 | }
|
|---|
| 206 | }
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | return 0;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | int main(int argc, char *argv[])
|
|---|
| 213 | {
|
|---|
| 214 | int rc;
|
|---|
| 215 | int argi;
|
|---|
| 216 |
|
|---|
| 217 | rc = inetping_init(&ev_ops);
|
|---|
| 218 | if (rc != EOK) {
|
|---|
| 219 | printf(NAME ": Failed connecting to internet ping service "
|
|---|
| 220 | "(%d).\n", rc);
|
|---|
| 221 | return 1;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | argi = 1;
|
|---|
| 225 | if (argi < argc && str_cmp(argv[argi], "-r") == 0) {
|
|---|
| 226 | ping_repeat = true;
|
|---|
| 227 | ++argi;
|
|---|
| 228 | } else {
|
|---|
| 229 | ping_repeat = false;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | if (argc - argi != 1) {
|
|---|
| 233 | print_syntax();
|
|---|
| 234 | return 1;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | /* Parse destination address */
|
|---|
| 238 | rc = addr_parse(argv[argi], &dest_addr);
|
|---|
| 239 | if (rc != EOK) {
|
|---|
| 240 | printf(NAME ": Invalid address format.\n");
|
|---|
| 241 | print_syntax();
|
|---|
| 242 | return 1;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | /* Determine source address */
|
|---|
| 246 | rc = inetping_get_srcaddr(&dest_addr, &src_addr);
|
|---|
| 247 | if (rc != EOK) {
|
|---|
| 248 | printf(NAME ": Failed determining source address.\n");
|
|---|
| 249 | return 1;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | fid_t fid;
|
|---|
| 253 |
|
|---|
| 254 | if (ping_repeat) {
|
|---|
| 255 | fid = fibril_create(transmit_fibril, NULL);
|
|---|
| 256 | if (fid == 0) {
|
|---|
| 257 | printf(NAME ": Failed creating transmit fibril.\n");
|
|---|
| 258 | return 1;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | fibril_add_ready(fid);
|
|---|
| 262 |
|
|---|
| 263 | fid = fibril_create(input_fibril, NULL);
|
|---|
| 264 | if (fid == 0) {
|
|---|
| 265 | printf(NAME ": Failed creating input fibril.\n");
|
|---|
| 266 | return 1;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | fibril_add_ready(fid);
|
|---|
| 270 | } else {
|
|---|
| 271 | ping_send(1);
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | fibril_mutex_lock(&done_lock);
|
|---|
| 275 | rc = EOK;
|
|---|
| 276 | while (!done && rc != ETIMEOUT) {
|
|---|
| 277 | rc = fibril_condvar_wait_timeout(&done_cv, &done_lock,
|
|---|
| 278 | ping_repeat ? 0 : PING_TIMEOUT);
|
|---|
| 279 | }
|
|---|
| 280 | fibril_mutex_unlock(&done_lock);
|
|---|
| 281 |
|
|---|
| 282 | if (rc == ETIMEOUT) {
|
|---|
| 283 | printf(NAME ": Echo request timed out.\n");
|
|---|
| 284 | return 1;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | return 0;
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | /** @}
|
|---|
| 291 | */
|
|---|