Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/inet/addr.c

    r683e584 ra62ceaf  
    290290
    291291static int inet_addr_parse_v4(const char *str, inet_addr_t *raddr,
    292     int *prefix)
     292    int *prefix, char **endptr)
    293293{
    294294        uint32_t a = 0;
     
    306306                i++;
    307307
    308                 if (*cur == '\0')
     308                if (*cur != '.')
    309309                        break;
    310 
    311                 if (*cur != '.')
    312                         return EINVAL;
    313310
    314311                if (i < 4)
     
    317314
    318315        if (prefix != NULL) {
     316                if (*cur != '/')
     317                        return EINVAL;
     318                cur++;
     319
    319320                *prefix = strtoul(cur, &cur, 10);
    320321                if (*prefix > 32)
     
    322323        }
    323324
    324         if (i != 4 || (*cur != '\0'))
     325        if (i != 4)
     326                return EINVAL;
     327
     328        if (endptr == NULL && *cur != '\0')
    325329                return EINVAL;
    326330
     
    328332        raddr->addr = a;
    329333
     334        if (endptr != NULL)
     335                *endptr = cur;
     336
    330337        return EOK;
    331338}
    332339
    333 static int inet_addr_parse_v6(const char *str, inet_addr_t *raddr, int *prefix)
     340static int inet_addr_parse_v6(const char *str, inet_addr_t *raddr, int *prefix,
     341    char **endptr)
    334342{
    335343        uint8_t data[16];
     344        int explicit_groups;
    336345
    337346        memset(data, 0, 16);
     
    347356                wildcard_pos = 0;
    348357                wildcard_size = 16;
    349 
    350                 /* Handle the unspecified address */
    351                 if (*cur == '\0')
    352                         goto success;
    353358        }
    354359
    355360        while (i < 16) {
    356361                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);
    358364                if (rc != EOK)
    359                         return rc;
     365                        break;
    360366
    361367                data[i] = (bioctet >> 8) & 0xff;
     
    371377                i += 2;
    372378
    373                 if (*cur != ':')
     379                if (*gend != ':') {
     380                        cur = gend;
    374381                        break;
     382                }
    375383
    376384                if (i < 16) {
    377                         cur++;
    378 
    379385                        /* Handle wildcard */
    380                         if (*cur == ':') {
     386                        if (gend[1] == ':') {
    381387                                if (wildcard_pos != (size_t) -1)
    382388                                        return EINVAL;
     
    384390                                wildcard_pos = i;
    385391                                wildcard_size = 16 - i;
    386                                 cur++;
    387 
    388                                 if (*cur == '\0' || *cur == '/')
    389                                         break;
     392                                cur = gend + 2;
    390393                        }
    391394                }
    392395        }
     396
     397        /* Number of explicitly specified groups */
     398        explicit_groups = i;
    393399
    394400        if (prefix != NULL) {
     
    402408        }
    403409
    404         if (*cur != '\0')
     410        if (endptr == NULL && *cur != '\0')
    405411                return EINVAL;
    406412
     
    414420                        data[j] = 0;
    415421                }
    416         }
    417 
    418 success:
     422        } else {
     423                /* Verify that all groups have been specified */
     424                if (explicit_groups != 16)
     425                        return EINVAL;
     426        }
     427
    419428        raddr->version = ip_v6;
    420429        memcpy(raddr->addr6, data, 16);
     430        if (endptr != NULL)
     431                *endptr = (char *)cur;
    421432        return EOK;
    422433}
    423434
    424435/** Parse node address.
     436 *
     437 * Will fail if @a text contains extra characters at the and and @a endptr
     438 * is @c NULL.
    425439 *
    426440 * @param text Network address in common notation.
    427441 * @param addr Place to store node address.
     442 * @param endptr Place to store pointer to next character oc @c NULL
    428443 *
    429444 * @return EOK on success, EINVAL if input is not in valid format.
    430445 *
    431446 */
    432 int inet_addr_parse(const char *text, inet_addr_t *addr)
     447int inet_addr_parse(const char *text, inet_addr_t *addr, char **endptr)
    433448{
    434449        int rc;
    435450
    436         rc = inet_addr_parse_v4(text, addr, NULL);
     451        rc = inet_addr_parse_v4(text, addr, NULL, endptr);
    437452        if (rc == EOK)
    438453                return EOK;
    439454
    440         rc = inet_addr_parse_v6(text, addr, NULL);
     455        rc = inet_addr_parse_v6(text, addr, NULL, endptr);
    441456        if (rc == EOK)
    442457                return EOK;
     
    447462/** Parse network address.
    448463 *
     464 * Will fail if @a text contains extra characters at the and and @a endptr
     465 * is @c NULL.
     466 *
    449467 * @param text  Network address in common notation.
    450468 * @param naddr Place to store network address.
     469 * @param endptr Place to store pointer to next character oc @c NULL
    451470 *
    452471 * @return EOK on success, EINVAL if input is not in valid format.
    453472 *
    454473 */
    455 int inet_naddr_parse(const char *text, inet_naddr_t *naddr)
     474int inet_naddr_parse(const char *text, inet_naddr_t *naddr, char **endptr)
    456475{
    457476        int rc;
     
    459478        int prefix;
    460479
    461         rc = inet_addr_parse_v4(text, &addr, &prefix);
     480        rc = inet_addr_parse_v4(text, &addr, &prefix, endptr);
    462481        if (rc == EOK) {
    463482                inet_addr_naddr(&addr, prefix, naddr);
     
    465484        }
    466485
    467         rc = inet_addr_parse_v6(text, &addr, &prefix);
     486        rc = inet_addr_parse_v6(text, &addr, &prefix, endptr);
    468487        if (rc == EOK) {
    469488                inet_addr_naddr(&addr, prefix, naddr);
Note: See TracChangeset for help on using the changeset viewer.