Changes in uspace/app/ping/ping.c [02a09ed:3b3c689] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/ping/ping.c
r02a09ed r3b3c689 1 1 /* 2 * Copyright (c) 201 3Jiri Svoboda2 * Copyright (c) 2012 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 34 34 35 35 #include <async.h> 36 #include < stdbool.h>36 #include <bool.h> 37 37 #include <errno.h> 38 38 #include <fibril_synch.h> 39 #include <net/socket_codes.h>40 #include <inet/dnsr.h>41 #include <inet/addr.h>42 39 #include <inet/inetping.h> 43 40 #include <io/console.h> … … 64 61 }; 65 62 66 static addr32_t src;67 static addr32_t dest;63 static inet_addr_t src_addr; 64 static inet_addr_t dest_addr; 68 65 69 66 static bool ping_repeat = false; … … 71 68 static void print_syntax(void) 72 69 { 73 printf("syntax: " NAME " [-r] <host>\n"); 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; 74 112 } 75 113 … … 84 122 static int ping_ev_recv(inetping_sdu_t *sdu) 85 123 { 86 inet_addr_t src_addr; 87 inet_addr_set(sdu->src, &src_addr); 88 89 inet_addr_t dest_addr; 90 inet_addr_set(sdu->dest, &dest_addr); 91 92 char *asrc; 93 int rc = inet_addr_format(&src_addr, &asrc); 124 char *asrc, *adest; 125 int rc; 126 127 rc = addr_format(&sdu->src, &asrc); 94 128 if (rc != EOK) 95 129 return ENOMEM; 96 97 char *adest; 98 rc = inet_addr_format(&dest_addr, &adest); 130 131 rc = addr_format(&sdu->dest, &adest); 99 132 if (rc != EOK) { 100 133 free(asrc); 101 134 return ENOMEM; 102 135 } 103 104 136 printf("Received ICMP echo reply: from %s to %s, seq. no %u, " 105 137 "payload size %zu\n", asrc, adest, sdu->seq_no, sdu->size); 106 107 if (!ping_repeat) 138 139 if (!ping_repeat) { 108 140 ping_signal_done(); 109 141 } 142 110 143 free(asrc); 111 144 free(adest); … … 118 151 int rc; 119 152 120 sdu.src = src ;121 sdu.dest = dest ;153 sdu.src = src_addr; 154 sdu.dest = dest_addr; 122 155 sdu.seq_no = seq_no; 123 156 sdu.data = (void *) "foo"; … … 155 188 { 156 189 console_ctrl_t *con; 157 cons_event_t ev;190 kbd_event_t ev; 158 191 159 192 con = console_init(stdin, stdout); … … 161 194 162 195 while (true) { 163 if (!console_get_ event(con, &ev))196 if (!console_get_kbd_event(con, &ev)) 164 197 break; 165 198 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) { 199 if (ev.type == KEY_PRESS && (ev.mods & (KM_ALT | KM_SHIFT)) == 200 0 && (ev.mods & KM_CTRL) != 0) { 169 201 /* Ctrl+key */ 170 if (ev. ev.key.key == KC_Q) {202 if (ev.key == KC_Q) { 171 203 ping_signal_done(); 172 204 return 0; … … 180 212 int main(int argc, char *argv[]) 181 213 { 182 dnsr_hostinfo_t *hinfo = NULL;183 char *asrc = NULL;184 char *adest = NULL;185 char *sdest = NULL;186 214 int rc; 187 215 int argi; … … 191 219 printf(NAME ": Failed connecting to internet ping service " 192 220 "(%d).\n", rc); 193 goto error;221 return 1; 194 222 } 195 223 … … 204 232 if (argc - argi != 1) { 205 233 print_syntax(); 206 goto error;234 return 1; 207 235 } 208 236 209 237 /* Parse destination address */ 210 inet_addr_t dest_addr; 211 rc = inet_addr_parse(argv[argi], &dest_addr); 212 if (rc != EOK) { 213 /* Try interpreting as a host name */ 214 rc = dnsr_name2host(argv[argi], &hinfo); 215 if (rc != EOK) { 216 printf(NAME ": Error resolving host '%s'.\n", argv[argi]); 217 goto error; 218 } 219 220 dest_addr = hinfo->addr; 221 } 222 223 uint16_t af = inet_addr_get(&dest_addr, &dest, NULL); 224 if (af != AF_INET) { 225 printf(NAME ": Destination '%s' is not an IPv4 address.\n", 226 argv[argi]); 227 goto error; 228 } 229 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 230 245 /* Determine source address */ 231 rc = inetping_get_srcaddr( dest, &src);246 rc = inetping_get_srcaddr(&dest_addr, &src_addr); 232 247 if (rc != EOK) { 233 248 printf(NAME ": Failed determining source address.\n"); 234 goto error; 235 } 236 237 inet_addr_t src_addr; 238 inet_addr_set(src, &src_addr); 239 240 rc = inet_addr_format(&src_addr, &asrc); 241 if (rc != EOK) { 242 printf(NAME ": Out of memory.\n"); 243 goto error; 244 } 245 246 rc = inet_addr_format(&dest_addr, &adest); 247 if (rc != EOK) { 248 printf(NAME ": Out of memory.\n"); 249 goto error; 250 } 251 252 if (hinfo != NULL) { 253 rc = asprintf(&sdest, "%s (%s)", hinfo->cname, adest); 254 if (rc < 0) { 255 printf(NAME ": Out of memory.\n"); 256 goto error; 257 } 258 } else { 259 sdest = adest; 260 adest = NULL; 261 } 262 263 printf("Sending ICMP echo request from %s to %s.\n", 264 asrc, sdest); 249 return 1; 250 } 265 251 266 252 fid_t fid; … … 270 256 if (fid == 0) { 271 257 printf(NAME ": Failed creating transmit fibril.\n"); 272 goto error;258 return 1; 273 259 } 274 260 … … 278 264 if (fid == 0) { 279 265 printf(NAME ": Failed creating input fibril.\n"); 280 goto error;266 return 1; 281 267 } 282 268 … … 296 282 if (rc == ETIMEOUT) { 297 283 printf(NAME ": Echo request timed out.\n"); 298 goto error; 299 } 300 301 free(asrc); 302 free(adest); 303 free(sdest); 304 dnsr_hostinfo_destroy(hinfo); 284 return 1; 285 } 286 305 287 return 0; 306 307 error:308 free(asrc);309 free(adest);310 free(sdest);311 dnsr_hostinfo_destroy(hinfo);312 return 1;313 288 } 314 289
Note:
See TracChangeset
for help on using the changeset viewer.