Changeset a62ceaf in mainline for uspace/lib/c/generic/inet/addr.c
- Timestamp:
- 2016-02-29T19:19:19Z (8 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 129b92c6
- Parents:
- 5147ff1
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/inet/addr.c
r5147ff1 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' || *cur == '/')308 if (*cur != '.') 309 309 break; 310 311 if (*cur != '.')312 return EINVAL;313 310 314 311 if (i < 4) … … 326 323 } 327 324 328 if (i != 4 || (*cur != '\0')) 325 if (i != 4) 326 return EINVAL; 327 328 if (endptr == NULL && *cur != '\0') 329 329 return EINVAL; 330 330 … … 332 332 raddr->addr = a; 333 333 334 if (endptr != NULL) 335 *endptr = cur; 336 334 337 return EOK; 335 338 } 336 339 337 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) 338 342 { 339 343 uint8_t data[16]; 344 int explicit_groups; 340 345 341 346 memset(data, 0, 16); … … 351 356 wildcard_pos = 0; 352 357 wildcard_size = 16; 353 354 /* Handle the unspecified address */355 if (*cur == '\0')356 goto success;357 358 } 358 359 359 360 while (i < 16) { 360 361 uint16_t bioctet; 361 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); 362 364 if (rc != EOK) 363 return rc;365 break; 364 366 365 367 data[i] = (bioctet >> 8) & 0xff; … … 375 377 i += 2; 376 378 377 if (*cur != ':') 379 if (*gend != ':') { 380 cur = gend; 378 381 break; 382 } 379 383 380 384 if (i < 16) { 381 cur++;382 383 385 /* Handle wildcard */ 384 if ( *cur== ':') {386 if (gend[1] == ':') { 385 387 if (wildcard_pos != (size_t) -1) 386 388 return EINVAL; … … 388 390 wildcard_pos = i; 389 391 wildcard_size = 16 - i; 390 cur++; 391 392 if (*cur == '\0' || *cur == '/') 393 break; 392 cur = gend + 2; 394 393 } 395 394 } 396 395 } 396 397 /* Number of explicitly specified groups */ 398 explicit_groups = i; 397 399 398 400 if (prefix != NULL) { … … 406 408 } 407 409 408 if ( *cur != '\0')410 if (endptr == NULL && *cur != '\0') 409 411 return EINVAL; 410 412 … … 418 420 data[j] = 0; 419 421 } 420 } 421 422 success: 422 } else { 423 /* Verify that all groups have been specified */ 424 if (explicit_groups != 16) 425 return EINVAL; 426 } 427 423 428 raddr->version = ip_v6; 424 429 memcpy(raddr->addr6, data, 16); 430 if (endptr != NULL) 431 *endptr = (char *)cur; 425 432 return EOK; 426 433 } 427 434 428 435 /** Parse node address. 436 * 437 * Will fail if @a text contains extra characters at the and and @a endptr 438 * is @c NULL. 429 439 * 430 440 * @param text Network address in common notation. 431 441 * @param addr Place to store node address. 442 * @param endptr Place to store pointer to next character oc @c NULL 432 443 * 433 444 * @return EOK on success, EINVAL if input is not in valid format. 434 445 * 435 446 */ 436 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) 437 448 { 438 449 int rc; 439 450 440 rc = inet_addr_parse_v4(text, addr, NULL );451 rc = inet_addr_parse_v4(text, addr, NULL, endptr); 441 452 if (rc == EOK) 442 453 return EOK; 443 454 444 rc = inet_addr_parse_v6(text, addr, NULL );455 rc = inet_addr_parse_v6(text, addr, NULL, endptr); 445 456 if (rc == EOK) 446 457 return EOK; … … 451 462 /** Parse network address. 452 463 * 464 * Will fail if @a text contains extra characters at the and and @a endptr 465 * is @c NULL. 466 * 453 467 * @param text Network address in common notation. 454 468 * @param naddr Place to store network address. 469 * @param endptr Place to store pointer to next character oc @c NULL 455 470 * 456 471 * @return EOK on success, EINVAL if input is not in valid format. 457 472 * 458 473 */ 459 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) 460 475 { 461 476 int rc; … … 463 478 int prefix; 464 479 465 rc = inet_addr_parse_v4(text, &addr, &prefix );480 rc = inet_addr_parse_v4(text, &addr, &prefix, endptr); 466 481 if (rc == EOK) { 467 482 inet_addr_naddr(&addr, prefix, naddr); … … 469 484 } 470 485 471 rc = inet_addr_parse_v6(text, &addr, &prefix );486 rc = inet_addr_parse_v6(text, &addr, &prefix, endptr); 472 487 if (rc == EOK) { 473 488 inet_addr_naddr(&addr, prefix, naddr);
Note:
See TracChangeset
for help on using the changeset viewer.