source: mainline/uspace/app/ping/ping.c@ 1c7ba2da

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1c7ba2da was 3495654, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Factor out internet address parsing and formatting.

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