Changes in uspace/app/ping/ping.c [959d2ec:3e6a98c5] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/ping/ping.c
r959d2ec r3e6a98c5 1 1 /* 2 * Copyright (c) 201 3Jiri Svoboda2 * Copyright (c) 2012 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 37 37 #include <errno.h> 38 38 #include <fibril_synch.h> 39 #include <inet/dnsr.h>40 #include <inet/addr.h>41 39 #include <inet/inetping.h> 42 40 #include <io/console.h> … … 70 68 static void print_syntax(void) 71 69 { 72 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; 73 112 } 74 113 … … 86 125 int rc; 87 126 88 rc = inet_addr_format(&sdu->src, &asrc);127 rc = addr_format(&sdu->src, &asrc); 89 128 if (rc != EOK) 90 129 return ENOMEM; 91 130 92 rc = inet_addr_format(&sdu->dest, &adest);131 rc = addr_format(&sdu->dest, &adest); 93 132 if (rc != EOK) { 94 133 free(asrc); … … 149 188 { 150 189 console_ctrl_t *con; 151 cons_event_t ev;190 kbd_event_t ev; 152 191 153 192 con = console_init(stdin, stdout); … … 155 194 156 195 while (true) { 157 if (!console_get_ event(con, &ev))196 if (!console_get_kbd_event(con, &ev)) 158 197 break; 159 198 160 if (ev.type == CEV_KEY && ev.ev.key.type == KEY_PRESS && 161 (ev.ev.key.mods & (KM_ALT | KM_SHIFT)) == 162 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) { 163 201 /* Ctrl+key */ 164 if (ev. ev.key.key == KC_Q) {202 if (ev.key == KC_Q) { 165 203 ping_signal_done(); 166 204 return 0; … … 174 212 int main(int argc, char *argv[]) 175 213 { 176 dnsr_hostinfo_t *hinfo = NULL;177 char *asrc = NULL;178 char *adest = NULL;179 char *sdest = NULL;180 214 int rc; 181 215 int argi; … … 185 219 printf(NAME ": Failed connecting to internet ping service " 186 220 "(%d).\n", rc); 187 goto error;221 return 1; 188 222 } 189 223 … … 198 232 if (argc - argi != 1) { 199 233 print_syntax(); 200 goto error;234 return 1; 201 235 } 202 236 203 237 /* Parse destination address */ 204 rc = inet_addr_parse(argv[argi], &dest_addr); 205 if (rc != EOK) { 206 /* Try interpreting as a host name */ 207 rc = dnsr_name2host(argv[argi], &hinfo); 208 if (rc != EOK) { 209 printf(NAME ": Error resolving host '%s'.\n", argv[argi]); 210 goto error; 211 } 212 213 dest_addr = hinfo->addr; 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; 214 243 } 215 244 … … 218 247 if (rc != EOK) { 219 248 printf(NAME ": Failed determining source address.\n"); 220 goto error; 221 } 222 223 rc = inet_addr_format(&src_addr, &asrc); 224 if (rc != EOK) { 225 printf(NAME ": Out of memory.\n"); 226 goto error; 227 } 228 229 rc = inet_addr_format(&dest_addr, &adest); 230 if (rc != EOK) { 231 printf(NAME ": Out of memory.\n"); 232 goto error; 233 } 234 235 if (hinfo != NULL) { 236 rc = asprintf(&sdest, "%s (%s)", hinfo->cname, adest); 237 if (rc < 0) { 238 printf(NAME ": Out of memory.\n"); 239 goto error; 240 } 241 } else { 242 sdest = adest; 243 adest = NULL; 244 } 245 246 printf("Sending ICMP echo request from %s to %s.\n", 247 asrc, sdest); 249 return 1; 250 } 248 251 249 252 fid_t fid; … … 253 256 if (fid == 0) { 254 257 printf(NAME ": Failed creating transmit fibril.\n"); 255 goto error;258 return 1; 256 259 } 257 260 … … 261 264 if (fid == 0) { 262 265 printf(NAME ": Failed creating input fibril.\n"); 263 goto error;266 return 1; 264 267 } 265 268 … … 279 282 if (rc == ETIMEOUT) { 280 283 printf(NAME ": Echo request timed out.\n"); 281 goto error; 282 } 283 284 free(asrc); 285 free(adest); 286 free(sdest); 287 dnsr_hostinfo_destroy(hinfo); 284 return 1; 285 } 286 288 287 return 0; 289 error:290 free(asrc);291 free(adest);292 free(sdest);293 dnsr_hostinfo_destroy(hinfo);294 return 1;295 288 } 296 289
Note:
See TracChangeset
for help on using the changeset viewer.