Changes in uspace/app/ping/ping.c [3495654:fff7ef4] in mainline
- File:
-
- 1 edited
-
uspace/app/ping/ping.c (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/ping/ping.c
r3495654 rfff7ef4 37 37 #include <errno.h> 38 38 #include <fibril_synch.h> 39 #include <inet/ addr.h>39 #include <inet/dnsr.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] <addr>\n"); 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; 72 113 } 73 114 … … 85 126 int rc; 86 127 87 rc = inet_addr_format(&sdu->src, &asrc);128 rc = addr_format(&sdu->src, &asrc); 88 129 if (rc != EOK) 89 130 return ENOMEM; 90 131 91 rc = inet_addr_format(&sdu->dest, &adest);132 rc = addr_format(&sdu->dest, &adest); 92 133 if (rc != EOK) { 93 134 free(asrc); … … 173 214 int main(int argc, char *argv[]) 174 215 { 216 dnsr_hostinfo_t *hinfo = NULL; 217 char *asrc = NULL; 218 char *adest = NULL; 219 char *sdest = NULL; 175 220 int rc; 176 221 int argi; … … 180 225 printf(NAME ": Failed connecting to internet ping service " 181 226 "(%d).\n", rc); 182 return 1;227 goto error; 183 228 } 184 229 … … 193 238 if (argc - argi != 1) { 194 239 print_syntax(); 195 return 1;240 goto error; 196 241 } 197 242 198 243 /* 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; 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; 204 254 } 205 255 … … 208 258 if (rc != EOK) { 209 259 printf(NAME ": Failed determining source address.\n"); 210 return 1; 211 } 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); 212 288 213 289 fid_t fid; … … 217 293 if (fid == 0) { 218 294 printf(NAME ": Failed creating transmit fibril.\n"); 219 return 1;295 goto error; 220 296 } 221 297 … … 225 301 if (fid == 0) { 226 302 printf(NAME ": Failed creating input fibril.\n"); 227 return 1;303 goto error; 228 304 } 229 305 … … 243 319 if (rc == ETIMEOUT) { 244 320 printf(NAME ": Echo request timed out.\n"); 245 return 1; 246 } 247 321 goto error; 322 } 323 324 free(asrc); 325 free(adest); 326 free(sdest); 327 dnsr_hostinfo_destroy(hinfo); 248 328 return 0; 329 error: 330 free(asrc); 331 free(adest); 332 free(sdest); 333 dnsr_hostinfo_destroy(hinfo); 334 return 1; 249 335 } 250 336
Note:
See TracChangeset
for help on using the changeset viewer.
