Ignore:
File:
1 edited

Legend:

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

    ra62ceaf r683e584  
    290290
    291291static int inet_addr_parse_v4(const char *str, inet_addr_t *raddr,
    292     int *prefix, char **endptr)
     292    int *prefix)
    293293{
    294294        uint32_t a = 0;
     
    306306                i++;
    307307
     308                if (*cur == '\0')
     309                        break;
     310
    308311                if (*cur != '.')
    309                         break;
     312                        return EINVAL;
    310313
    311314                if (i < 4)
     
    314317
    315318        if (prefix != NULL) {
    316                 if (*cur != '/')
    317                         return EINVAL;
    318                 cur++;
    319 
    320319                *prefix = strtoul(cur, &cur, 10);
    321320                if (*prefix > 32)
     
    323322        }
    324323
    325         if (i != 4)
    326                 return EINVAL;
    327 
    328         if (endptr == NULL && *cur != '\0')
     324        if (i != 4 || (*cur != '\0'))
    329325                return EINVAL;
    330326
     
    332328        raddr->addr = a;
    333329
    334         if (endptr != NULL)
    335                 *endptr = cur;
    336 
    337330        return EOK;
    338331}
    339332
    340 static int inet_addr_parse_v6(const char *str, inet_addr_t *raddr, int *prefix,
    341     char **endptr)
     333static int inet_addr_parse_v6(const char *str, inet_addr_t *raddr, int *prefix)
    342334{
    343335        uint8_t data[16];
    344         int explicit_groups;
    345336
    346337        memset(data, 0, 16);
     
    356347                wildcard_pos = 0;
    357348                wildcard_size = 16;
     349
     350                /* Handle the unspecified address */
     351                if (*cur == '\0')
     352                        goto success;
    358353        }
    359354
    360355        while (i < 16) {
    361356                uint16_t bioctet;
    362                 const char *gend;
    363                 int rc = str_uint16_t(cur, &gend, 16, false, &bioctet);
     357                int rc = str_uint16_t(cur, &cur, 16, false, &bioctet);
    364358                if (rc != EOK)
    365                         break;
     359                        return rc;
    366360
    367361                data[i] = (bioctet >> 8) & 0xff;
     
    377371                i += 2;
    378372
    379                 if (*gend != ':') {
    380                         cur = gend;
     373                if (*cur != ':')
    381374                        break;
    382                 }
    383375
    384376                if (i < 16) {
     377                        cur++;
     378
    385379                        /* Handle wildcard */
    386                         if (gend[1] == ':') {
     380                        if (*cur == ':') {
    387381                                if (wildcard_pos != (size_t) -1)
    388382                                        return EINVAL;
     
    390384                                wildcard_pos = i;
    391385                                wildcard_size = 16 - i;
    392                                 cur = gend + 2;
     386                                cur++;
     387
     388                                if (*cur == '\0' || *cur == '/')
     389                                        break;
    393390                        }
    394391                }
    395392        }
    396 
    397         /* Number of explicitly specified groups */
    398         explicit_groups = i;
    399393
    400394        if (prefix != NULL) {
     
    408402        }
    409403
    410         if (endptr == NULL && *cur != '\0')
     404        if (*cur != '\0')
    411405                return EINVAL;
    412406
     
    420414                        data[j] = 0;
    421415                }
    422         } else {
    423                 /* Verify that all groups have been specified */
    424                 if (explicit_groups != 16)
    425                         return EINVAL;
    426         }
    427 
     416        }
     417
     418success:
    428419        raddr->version = ip_v6;
    429420        memcpy(raddr->addr6, data, 16);
    430         if (endptr != NULL)
    431                 *endptr = (char *)cur;
    432421        return EOK;
    433422}
    434423
    435424/** Parse node address.
    436  *
    437  * Will fail if @a text contains extra characters at the and and @a endptr
    438  * is @c NULL.
    439425 *
    440426 * @param text Network address in common notation.
    441427 * @param addr Place to store node address.
    442  * @param endptr Place to store pointer to next character oc @c NULL
    443428 *
    444429 * @return EOK on success, EINVAL if input is not in valid format.
    445430 *
    446431 */
    447 int inet_addr_parse(const char *text, inet_addr_t *addr, char **endptr)
     432int inet_addr_parse(const char *text, inet_addr_t *addr)
    448433{
    449434        int rc;
    450435
    451         rc = inet_addr_parse_v4(text, addr, NULL, endptr);
     436        rc = inet_addr_parse_v4(text, addr, NULL);
    452437        if (rc == EOK)
    453438                return EOK;
    454439
    455         rc = inet_addr_parse_v6(text, addr, NULL, endptr);
     440        rc = inet_addr_parse_v6(text, addr, NULL);
    456441        if (rc == EOK)
    457442                return EOK;
     
    462447/** Parse network address.
    463448 *
    464  * Will fail if @a text contains extra characters at the and and @a endptr
    465  * is @c NULL.
    466  *
    467449 * @param text  Network address in common notation.
    468450 * @param naddr Place to store network address.
    469  * @param endptr Place to store pointer to next character oc @c NULL
    470451 *
    471452 * @return EOK on success, EINVAL if input is not in valid format.
    472453 *
    473454 */
    474 int inet_naddr_parse(const char *text, inet_naddr_t *naddr, char **endptr)
     455int inet_naddr_parse(const char *text, inet_naddr_t *naddr)
    475456{
    476457        int rc;
     
    478459        int prefix;
    479460
    480         rc = inet_addr_parse_v4(text, &addr, &prefix, endptr);
     461        rc = inet_addr_parse_v4(text, &addr, &prefix);
    481462        if (rc == EOK) {
    482463                inet_addr_naddr(&addr, prefix, naddr);
     
    484465        }
    485466
    486         rc = inet_addr_parse_v6(text, &addr, &prefix, endptr);
     467        rc = inet_addr_parse_v6(text, &addr, &prefix);
    487468        if (rc == EOK) {
    488469                inet_addr_naddr(&addr, prefix, naddr);
Note: See TracChangeset for help on using the changeset viewer.