Changes in uspace/app/ping/ping.c [fff7ef4:3495654] in mainline
- File:
-
- 1 edited
-
uspace/app/ping/ping.c (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/ping/ping.c
rfff7ef4 r3495654 37 37 #include <errno.h> 38 38 #include <fibril_synch.h> 39 #include <inet/ dnsr.h>39 #include <inet/addr.h> 40 40 #include <inet/inetping.h> 41 41 #include <io/console.h> … … 69 69 static void print_syntax(void) 70 70 { 71 printf("syntax: " NAME " [-r] <host>\n"); 72 } 73 74 static int addr_parse(const char *text, inet_addr_t *addr) 75 { 76 unsigned long a[4]; 77 char *cp = (char *)text; 78 int i; 79 80 for (i = 0; i < 3; i++) { 81 a[i] = strtoul(cp, &cp, 10); 82 if (*cp != '.') 83 return EINVAL; 84 ++cp; 85 } 86 87 a[3] = strtoul(cp, &cp, 10); 88 if (*cp != '\0') 89 return EINVAL; 90 91 addr->ipv4 = 0; 92 for (i = 0; i < 4; i++) { 93 if (a[i] > 255) 94 return EINVAL; 95 addr->ipv4 = (addr->ipv4 << 8) | a[i]; 96 } 97 98 return EOK; 99 } 100 101 static int addr_format(inet_addr_t *addr, char **bufp) 102 { 103 int rc; 104 105 rc = asprintf(bufp, "%d.%d.%d.%d", addr->ipv4 >> 24, 106 (addr->ipv4 >> 16) & 0xff, (addr->ipv4 >> 8) & 0xff, 107 addr->ipv4 & 0xff); 108 109 if (rc < 0) 110 return ENOMEM; 111 112 return EOK; 71 printf("syntax: " NAME " [-r] <addr>\n"); 113 72 } 114 73 … … 126 85 int rc; 127 86 128 rc = addr_format(&sdu->src, &asrc);87 rc = inet_addr_format(&sdu->src, &asrc); 129 88 if (rc != EOK) 130 89 return ENOMEM; 131 90 132 rc = addr_format(&sdu->dest, &adest);91 rc = inet_addr_format(&sdu->dest, &adest); 133 92 if (rc != EOK) { 134 93 free(asrc); … … 214 173 int main(int argc, char *argv[]) 215 174 { 216 dnsr_hostinfo_t *hinfo = NULL;217 char *asrc = NULL;218 char *adest = NULL;219 char *sdest = NULL;220 175 int rc; 221 176 int argi; … … 225 180 printf(NAME ": Failed connecting to internet ping service " 226 181 "(%d).\n", rc); 227 goto error;182 return 1; 228 183 } 229 184 … … 238 193 if (argc - argi != 1) { 239 194 print_syntax(); 240 goto error;195 return 1; 241 196 } 242 197 243 198 /* Parse destination address */ 244 rc = addr_parse(argv[argi], &dest_addr); 245 if (rc != EOK) { 246 /* Try interpreting as a host name */ 247 rc = dnsr_name2host(argv[argi], &hinfo); 248 if (rc != EOK) { 249 printf(NAME ": Error resolving host '%s'.\n", argv[argi]); 250 goto error; 251 } 252 253 dest_addr = hinfo->addr; 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; 254 204 } 255 205 … … 258 208 if (rc != EOK) { 259 209 printf(NAME ": Failed determining source address.\n"); 260 goto error; 261 } 262 263 rc = addr_format(&src_addr, &asrc); 264 if (rc != EOK) { 265 printf(NAME ": Out of memory.\n"); 266 goto error; 267 } 268 269 rc = addr_format(&dest_addr, &adest); 270 if (rc != EOK) { 271 printf(NAME ": Out of memory.\n"); 272 goto error; 273 } 274 275 if (hinfo != NULL) { 276 rc = asprintf(&sdest, "%s (%s)", hinfo->name, adest); 277 if (rc < 0) { 278 printf(NAME ": Out of memory.\n"); 279 goto error; 280 } 281 } else { 282 sdest = adest; 283 adest = NULL; 284 } 285 286 printf("Sending ICMP echo request from %s to %s.\n", 287 asrc, sdest); 210 return 1; 211 } 288 212 289 213 fid_t fid; … … 293 217 if (fid == 0) { 294 218 printf(NAME ": Failed creating transmit fibril.\n"); 295 goto error;219 return 1; 296 220 } 297 221 … … 301 225 if (fid == 0) { 302 226 printf(NAME ": Failed creating input fibril.\n"); 303 goto error;227 return 1; 304 228 } 305 229 … … 319 243 if (rc == ETIMEOUT) { 320 244 printf(NAME ": Echo request timed out.\n"); 321 goto error; 322 } 323 324 free(asrc); 325 free(adest); 326 free(sdest); 327 dnsr_hostinfo_destroy(hinfo); 245 return 1; 246 } 247 328 248 return 0; 329 error:330 free(asrc);331 free(adest);332 free(sdest);333 dnsr_hostinfo_destroy(hinfo);334 return 1;335 249 } 336 250
Note:
See TracChangeset
for help on using the changeset viewer.
