Changes in uspace/lib/c/generic/inet/addr.c [683e584:a62ceaf] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/inet/addr.c
r683e584 ra62ceaf 290 290 291 291 static int inet_addr_parse_v4(const char *str, inet_addr_t *raddr, 292 int *prefix )292 int *prefix, char **endptr) 293 293 { 294 294 uint32_t a = 0; … … 306 306 i++; 307 307 308 if (*cur == '\0')308 if (*cur != '.') 309 309 break; 310 311 if (*cur != '.')312 return EINVAL;313 310 314 311 if (i < 4) … … 317 314 318 315 if (prefix != NULL) { 316 if (*cur != '/') 317 return EINVAL; 318 cur++; 319 319 320 *prefix = strtoul(cur, &cur, 10); 320 321 if (*prefix > 32) … … 322 323 } 323 324 324 if (i != 4 || (*cur != '\0')) 325 if (i != 4) 326 return EINVAL; 327 328 if (endptr == NULL && *cur != '\0') 325 329 return EINVAL; 326 330 … … 328 332 raddr->addr = a; 329 333 334 if (endptr != NULL) 335 *endptr = cur; 336 330 337 return EOK; 331 338 } 332 339 333 static int inet_addr_parse_v6(const char *str, inet_addr_t *raddr, int *prefix) 340 static int inet_addr_parse_v6(const char *str, inet_addr_t *raddr, int *prefix, 341 char **endptr) 334 342 { 335 343 uint8_t data[16]; 344 int explicit_groups; 336 345 337 346 memset(data, 0, 16); … … 347 356 wildcard_pos = 0; 348 357 wildcard_size = 16; 349 350 /* Handle the unspecified address */351 if (*cur == '\0')352 goto success;353 358 } 354 359 355 360 while (i < 16) { 356 361 uint16_t bioctet; 357 int rc = str_uint16_t(cur, &cur, 16, false, &bioctet); 362 const char *gend; 363 int rc = str_uint16_t(cur, &gend, 16, false, &bioctet); 358 364 if (rc != EOK) 359 return rc;365 break; 360 366 361 367 data[i] = (bioctet >> 8) & 0xff; … … 371 377 i += 2; 372 378 373 if (*cur != ':') 379 if (*gend != ':') { 380 cur = gend; 374 381 break; 382 } 375 383 376 384 if (i < 16) { 377 cur++;378 379 385 /* Handle wildcard */ 380 if ( *cur== ':') {386 if (gend[1] == ':') { 381 387 if (wildcard_pos != (size_t) -1) 382 388 return EINVAL; … … 384 390 wildcard_pos = i; 385 391 wildcard_size = 16 - i; 386 cur++; 387 388 if (*cur == '\0' || *cur == '/') 389 break; 392 cur = gend + 2; 390 393 } 391 394 } 392 395 } 396 397 /* Number of explicitly specified groups */ 398 explicit_groups = i; 393 399 394 400 if (prefix != NULL) { … … 402 408 } 403 409 404 if ( *cur != '\0')410 if (endptr == NULL && *cur != '\0') 405 411 return EINVAL; 406 412 … … 414 420 data[j] = 0; 415 421 } 416 } 417 418 success: 422 } else { 423 /* Verify that all groups have been specified */ 424 if (explicit_groups != 16) 425 return EINVAL; 426 } 427 419 428 raddr->version = ip_v6; 420 429 memcpy(raddr->addr6, data, 16); 430 if (endptr != NULL) 431 *endptr = (char *)cur; 421 432 return EOK; 422 433 } 423 434 424 435 /** Parse node address. 436 * 437 * Will fail if @a text contains extra characters at the and and @a endptr 438 * is @c NULL. 425 439 * 426 440 * @param text Network address in common notation. 427 441 * @param addr Place to store node address. 442 * @param endptr Place to store pointer to next character oc @c NULL 428 443 * 429 444 * @return EOK on success, EINVAL if input is not in valid format. 430 445 * 431 446 */ 432 int inet_addr_parse(const char *text, inet_addr_t *addr )447 int inet_addr_parse(const char *text, inet_addr_t *addr, char **endptr) 433 448 { 434 449 int rc; 435 450 436 rc = inet_addr_parse_v4(text, addr, NULL );451 rc = inet_addr_parse_v4(text, addr, NULL, endptr); 437 452 if (rc == EOK) 438 453 return EOK; 439 454 440 rc = inet_addr_parse_v6(text, addr, NULL );455 rc = inet_addr_parse_v6(text, addr, NULL, endptr); 441 456 if (rc == EOK) 442 457 return EOK; … … 447 462 /** Parse network address. 448 463 * 464 * Will fail if @a text contains extra characters at the and and @a endptr 465 * is @c NULL. 466 * 449 467 * @param text Network address in common notation. 450 468 * @param naddr Place to store network address. 469 * @param endptr Place to store pointer to next character oc @c NULL 451 470 * 452 471 * @return EOK on success, EINVAL if input is not in valid format. 453 472 * 454 473 */ 455 int inet_naddr_parse(const char *text, inet_naddr_t *naddr )474 int inet_naddr_parse(const char *text, inet_naddr_t *naddr, char **endptr) 456 475 { 457 476 int rc; … … 459 478 int prefix; 460 479 461 rc = inet_addr_parse_v4(text, &addr, &prefix );480 rc = inet_addr_parse_v4(text, &addr, &prefix, endptr); 462 481 if (rc == EOK) { 463 482 inet_addr_naddr(&addr, prefix, naddr); … … 465 484 } 466 485 467 rc = inet_addr_parse_v6(text, &addr, &prefix );486 rc = inet_addr_parse_v6(text, &addr, &prefix, endptr); 468 487 if (rc == EOK) { 469 488 inet_addr_naddr(&addr, prefix, naddr);
Note:
See TracChangeset
for help on using the changeset viewer.