1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

sys/net/ipv6: add ipv6_prefix_from_str()

This commit is contained in:
Benjamin Valentin 2022-08-19 12:21:41 +02:00
parent 2dd59236c8
commit 15c8ad2e8e
2 changed files with 45 additions and 0 deletions

View File

@ -737,6 +737,22 @@ char *ipv6_addr_to_str(char *result, const ipv6_addr_t *addr, uint8_t result_len
*/
ipv6_addr_t *ipv6_addr_from_str(ipv6_addr_t *result, const char *addr);
/**
* @brief Converts an IPv6 prefix string representation to a byte-represented
* IPv6 address
*
* @see <a href="https://tools.ietf.org/html/rfc5952">
* RFC 5952
* </a>
*
* @param[out] result The resulting byte representation
* @param[in] prefix An IPv6 prefix string representation
*
* @return prefix length in bits, on success
* @return <0 on error
*/
int ipv6_prefix_from_str(ipv6_addr_t *result, const char *prefix);
/**
* @brief Converts an IPv6 address from a buffer of characters to a
* byte-represented IPv6 address

View File

@ -26,6 +26,7 @@
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
@ -163,6 +164,34 @@ ipv6_addr_t *ipv6_addr_from_str(ipv6_addr_t *result, const char *addr)
return ipv6_addr_from_buf(result, addr, strlen(addr));
}
int ipv6_prefix_from_str(ipv6_addr_t *result, const char *addr)
{
size_t str_len;
int pfx_len;
const char *separator;
if ((result == NULL) || (addr == NULL)) {
return -EINVAL;
}
if ((separator = strrchr(addr, '/')) == NULL) {
return -EINVAL;
}
str_len = separator - addr;
pfx_len = strtol(separator + 1, NULL, 10);
if (pfx_len <= 0) {
return pfx_len;
}
if (ipv6_addr_from_buf(result, addr, str_len) == NULL) {
return -EINVAL;
}
return pfx_len;
}
/**
* @}
*/