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