Changeset a62ceaf in mainline for uspace/lib/c/generic/inet/addr.c


Ignore:
Timestamp:
2016-02-29T19:19:19Z (8 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
129b92c6
Parents:
5147ff1
Message:

Need better interfaces for handling internet host and host:port specifications.

File:
1 edited

Legend:

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

    r5147ff1 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' || *cur == '/')
     308                if (*cur != '.')
    309309                        break;
    310 
    311                 if (*cur != '.')
    312                         return EINVAL;
    313310
    314311                if (i < 4)
     
    326323        }
    327324
    328         if (i != 4 || (*cur != '\0'))
     325        if (i != 4)
     326                return EINVAL;
     327
     328        if (endptr == NULL && *cur != '\0')
    329329                return EINVAL;
    330330
     
    332332        raddr->addr = a;
    333333
     334        if (endptr != NULL)
     335                *endptr = cur;
     336
    334337        return EOK;
    335338}
    336339
    337 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)
    338342{
    339343        uint8_t data[16];
     344        int explicit_groups;
    340345
    341346        memset(data, 0, 16);
     
    351356                wildcard_pos = 0;
    352357                wildcard_size = 16;
    353 
    354                 /* Handle the unspecified address */
    355                 if (*cur == '\0')
    356                         goto success;
    357358        }
    358359
    359360        while (i < 16) {
    360361                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);
    362364                if (rc != EOK)
    363                         return rc;
     365                        break;
    364366
    365367                data[i] = (bioctet >> 8) & 0xff;
     
    375377                i += 2;
    376378
    377                 if (*cur != ':')
     379                if (*gend != ':') {
     380                        cur = gend;
    378381                        break;
     382                }
    379383
    380384                if (i < 16) {
    381                         cur++;
    382 
    383385                        /* Handle wildcard */
    384                         if (*cur == ':') {
     386                        if (gend[1] == ':') {
    385387                                if (wildcard_pos != (size_t) -1)
    386388                                        return EINVAL;
     
    388390                                wildcard_pos = i;
    389391                                wildcard_size = 16 - i;
    390                                 cur++;
    391 
    392                                 if (*cur == '\0' || *cur == '/')
    393                                         break;
     392                                cur = gend + 2;
    394393                        }
    395394                }
    396395        }
     396
     397        /* Number of explicitly specified groups */
     398        explicit_groups = i;
    397399
    398400        if (prefix != NULL) {
     
    406408        }
    407409
    408         if (*cur != '\0')
     410        if (endptr == NULL && *cur != '\0')
    409411                return EINVAL;
    410412
     
    418420                        data[j] = 0;
    419421                }
    420         }
    421 
    422 success:
     422        } else {
     423                /* Verify that all groups have been specified */
     424                if (explicit_groups != 16)
     425                        return EINVAL;
     426        }
     427
    423428        raddr->version = ip_v6;
    424429        memcpy(raddr->addr6, data, 16);
     430        if (endptr != NULL)
     431                *endptr = (char *)cur;
    425432        return EOK;
    426433}
    427434
    428435/** Parse node address.
     436 *
     437 * Will fail if @a text contains extra characters at the and and @a endptr
     438 * is @c NULL.
    429439 *
    430440 * @param text Network address in common notation.
    431441 * @param addr Place to store node address.
     442 * @param endptr Place to store pointer to next character oc @c NULL
    432443 *
    433444 * @return EOK on success, EINVAL if input is not in valid format.
    434445 *
    435446 */
    436 int inet_addr_parse(const char *text, inet_addr_t *addr)
     447int inet_addr_parse(const char *text, inet_addr_t *addr, char **endptr)
    437448{
    438449        int rc;
    439450
    440         rc = inet_addr_parse_v4(text, addr, NULL);
     451        rc = inet_addr_parse_v4(text, addr, NULL, endptr);
    441452        if (rc == EOK)
    442453                return EOK;
    443454
    444         rc = inet_addr_parse_v6(text, addr, NULL);
     455        rc = inet_addr_parse_v6(text, addr, NULL, endptr);
    445456        if (rc == EOK)
    446457                return EOK;
     
    451462/** Parse network address.
    452463 *
     464 * Will fail if @a text contains extra characters at the and and @a endptr
     465 * is @c NULL.
     466 *
    453467 * @param text  Network address in common notation.
    454468 * @param naddr Place to store network address.
     469 * @param endptr Place to store pointer to next character oc @c NULL
    455470 *
    456471 * @return EOK on success, EINVAL if input is not in valid format.
    457472 *
    458473 */
    459 int inet_naddr_parse(const char *text, inet_naddr_t *naddr)
     474int inet_naddr_parse(const char *text, inet_naddr_t *naddr, char **endptr)
    460475{
    461476        int rc;
     
    463478        int prefix;
    464479
    465         rc = inet_addr_parse_v4(text, &addr, &prefix);
     480        rc = inet_addr_parse_v4(text, &addr, &prefix, endptr);
    466481        if (rc == EOK) {
    467482                inet_addr_naddr(&addr, prefix, naddr);
     
    469484        }
    470485
    471         rc = inet_addr_parse_v6(text, &addr, &prefix);
     486        rc = inet_addr_parse_v6(text, &addr, &prefix, endptr);
    472487        if (rc == EOK) {
    473488                inet_addr_naddr(&addr, prefix, naddr);
Note: See TracChangeset for help on using the changeset viewer.