Skip to content

Commit

Permalink
universal_address: replaced returned literals by defined constants
Browse files Browse the repository at this point in the history
* added and adjusted doxy for the new defined return values
* stripped whitespaces for statements in the compare functions
  • Loading branch information
BytesGalore authored and BytesGalore committed Mar 30, 2016
1 parent 64cb2cd commit 79809ff
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
31 changes: 23 additions & 8 deletions sys/include/universal_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ extern "C" {
#define UNIVERSAL_ADDRESS_SIZE (IPV6_ADDR_BIT_LEN >> 3)
#endif

/** @brief return value indicating the compared addresses are equal */
#define UNIVERSAL_ADDRESS_EQUAL (0)

/** @brief return value indicating the compared addresses match up to a certain prefix */
#define UNIVERSAL_ADDRESS_MATCHING_PREFIX (1)

/** @brief return value indicating all address bits of the entry are `0`.
* Its considered as default route address that matches any other prefix.
*/
#define UNIVERSAL_ADDRESS_IS_ALL_ZERO_ADDRESS (2)

/**
* @brief The container descriptor used to identify a universal address entry
*/
Expand Down Expand Up @@ -69,7 +80,7 @@ void universal_address_reset(void);
* @param[in] addr_size the number of bytes required for the address entry
*
* @return pointer to the universal_address_container_t containing the address on success
* NULL if the address could not be inserted
* @return NULL if the address could not be inserted
*/
universal_address_container_t *universal_address_add(uint8_t *addr, size_t addr_size);

Expand All @@ -90,7 +101,7 @@ void universal_address_rem(universal_address_container_t *entry);
* this value is overwritten with the actual size required
*
* @return addr if the address is copied to the addr destination
* NULL if the size is unsufficient for copy
* @return NULL if the size is unsufficient for copy
*/
uint8_t* universal_address_get_address(universal_address_container_t *entry,
uint8_t *addr, size_t *addr_size);
Expand All @@ -107,9 +118,12 @@ uint8_t* universal_address_get_address(universal_address_container_t *entry,
* with the number of matching bits till the
* first of trailing `0`s
*
* @return 0 if the entries are equal
* 1 if the entry match to a certain prefix (trailing '0's in *entry)
* -ENOENT if the given adresses do not match
* @return UNIVERSAL_ADDRESS_EQUAL if the entries are equal
* @return UNIVERSAL_ADDRESS_MATCHING_PREFIX if the entry matches to a certain prefix
* (trailing '0's in *entry)
* @return UNIVERSAL_ADDRESS_IS_ALL_ZERO_ADDRESS if the entry address is all `0`s
* and considered as default route
* @return -ENOENT if the given adresses do not match
*/
int universal_address_compare(universal_address_container_t *entry,
uint8_t *addr, size_t *addr_size_in_bits);
Expand All @@ -128,9 +142,10 @@ int universal_address_compare(universal_address_container_t *entry,
* e.g. for an ipv6_addr_t it would be sizeof(ipv6_addr_t)
* regardless if the stored prefix is < ::/128
*
* @return 0 if the entries are equal
* 1 if the entry match to a certain prefix (trailing '0's in *prefix)
* -ENOENT if the given adresses do not match
* @return UNIVERSAL_ADDRESS_EQUAL if the entries are equal
* @return UNIVERSAL_ADDRESS_MATCHING_PREFIX if the entry matches to a certain prefix
* (trailing '0's in *prefix)
* @return -ENOENT if the given adresses do not match
*/
int universal_address_compare_prefix(universal_address_container_t *entry,
uint8_t *prefix, size_t prefix_size_in_bits);
Expand Down
26 changes: 13 additions & 13 deletions sys/universal_address/universal_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,13 @@ int universal_address_compare(universal_address_container_t *entry,
if (test_all_zeros) {
*addr_size_in_bits = 0;
mutex_unlock(&mtx_access);
return 2;
return UNIVERSAL_ADDRESS_IS_ALL_ZERO_ADDRESS;
}

/* if we have no distinct bytes the addresses are equal */
if (idx == -1) {
mutex_unlock(&mtx_access);
return 0;
return UNIVERSAL_ADDRESS_EQUAL;
}

/* count equal bits */
Expand All @@ -235,7 +235,7 @@ int universal_address_compare(universal_address_container_t *entry,

/* get the total number of matching bits */
*addr_size_in_bits = (idx << 3) + j;
ret = 1;
ret = UNIVERSAL_ADDRESS_MATCHING_PREFIX;

mutex_unlock(&mtx_access);
return ret;
Expand All @@ -254,31 +254,31 @@ int universal_address_compare_prefix(universal_address_container_t *entry,

/* Get the index of the first trailing `0` */
int i = 0;
for( i = entry->address_size-1; i >= 0; --i) {
if( prefix[i] != 0 ) {
for (i = entry->address_size-1; i >= 0; --i) {
if (prefix[i] != 0) {
break;
}
}

if( memcmp(entry->address, prefix, i) == 0 ) {
if (memcmp(entry->address, prefix, i) == 0) {
/* if the bytes-1 equals we check the bits of the lowest byte */
uint8_t bitmask = 0x00;
/* get a bitmask for the trailing 0b */
for( uint8_t j = 0; j < 8; ++j ) {
if ( (prefix[i] >> j) & 0x01 ) {
for (uint8_t j = 0; j < 8; ++j) {
if ((prefix[i] >> j) & 0x01) {
bitmask = 0xff << j;
break;
}
}

if( (entry->address[i] & bitmask) == (prefix[i] & bitmask) ) {
if ((entry->address[i] & bitmask) == (prefix[i] & bitmask)) {
ret = entry->address[i] != prefix[i];
if( ret == 0 ) {
if (ret == UNIVERSAL_ADDRESS_EQUAL) {
/* check if the remaining bits from entry are significant */
i++;
for(; i < entry->address_size; ++i) {
if( entry->address[i] != 0 ) {
ret = 1;
for ( ; i < entry->address_size; ++i) {
if (entry->address[i] != 0) {
ret = UNIVERSAL_ADDRESS_MATCHING_PREFIX;
break;
}
}
Expand Down

0 comments on commit 79809ff

Please sign in to comment.