Skip to content

Commit

Permalink
Merge pull request #4753 from kaspar030/add_some_ipv6_addr_tools
Browse files Browse the repository at this point in the history
sys: net: ipv6: add some ipv6 utility functions
  • Loading branch information
cgundogan committed Feb 5, 2016
2 parents 5165e48 + 44e985b commit 79c68e0
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
47 changes: 47 additions & 0 deletions sys/include/net/ipv6/addr.h
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,53 @@ 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 split IPv6 address string representation
*
* @note Will change @p seperator position in @p addr_str to '\0'
*
* @param[in,out] addr_str Address to split
* @param[in] seperator Seperator char to use
* @param[in] _default Default value
*
* @return atoi(string after split)
* @return @p _default if no string after @p seperator
*/
int ipv6_addr_split(char *addr_str, char seperator, int _default);

/**
* @brief split IPv6 prefix string representation
*
* E.g., "2001:db8::1/64" returns "64", changes @p addr_str to "2001:db8::1"
*
* @param[in,out] addr_str Address to split
* @return prefix length or 128 if none specified
*/
static inline int ipv6_addr_split_prefix(char *addr_str)
{
return ipv6_addr_split(addr_str, '/', 128);
}

/**
* @brief split IPv6 address + interface specifier
*
* E.g., "fe80::1%5" returns "5", changes @p addr_str to "fe80::1"
*
* @param[in,out] addr_str Address to split
* @return interface number or -1 if none specified
*/
static inline int ipv6_addr_split_iface(char *addr_str)
{
return ipv6_addr_split(addr_str, '%', -1);
}

/**
* @brief Print IPv6 address to stdout
*
* @param[in] addr Pointer to ipv6_addr_t to print
*/
void ipv6_addr_print(const ipv6_addr_t *addr);

#ifdef __cplusplus
}
#endif
Expand Down
35 changes: 35 additions & 0 deletions sys/net/network_layer/ipv6/addr/ipv6_addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "net/ipv6/addr.h"

#ifdef MODULE_FMT
#include "fmt.h"
#else
#include <stdio.h>
#endif

bool ipv6_addr_equal(const ipv6_addr_t *a, const ipv6_addr_t *b)
{
return (a->u64[0].u64 == b->u64[0].u64) &&
Expand Down Expand Up @@ -106,6 +113,34 @@ void ipv6_addr_init_iid(ipv6_addr_t *out, const uint8_t *iid, uint8_t bits)
memcpy(&(out->u8[pos]), iid, bytes);
}

int ipv6_addr_split(char *addr_str, char seperator, int _default)
{
char *sep = addr_str;
while(*++sep) {
if (*sep == seperator) {
*sep++ = '\0';
if (*sep) {
_default = atoi(sep);
}
break;
}
}

return _default;
}

void ipv6_addr_print(const ipv6_addr_t *addr)
{
assert(addr);
char addr_str[IPV6_ADDR_MAX_STR_LEN];
ipv6_addr_to_str(addr_str, addr, sizeof(addr_str));
#ifdef MODULE_FMT
print_str(addr_str);
#else
printf("%s", addr_str);
#endif
}

/**
* @}
*/

0 comments on commit 79c68e0

Please sign in to comment.