universal_address: replaced returned literals by defined constants

* added and adjusted doxy for the new defined return values
* stripped whitespaces for statements in the compare functions
This commit is contained in:
BytesGalore 2016-03-30 08:03:23 +02:00
parent 1bc9c3bfd2
commit 38d5fc2476
2 changed files with 37 additions and 22 deletions

View File

@ -42,6 +42,17 @@ extern "C" {
#define UNIVERSAL_ADDRESS_SIZE (IPV6_ADDR_BIT_LEN >> 3) #define UNIVERSAL_ADDRESS_SIZE (IPV6_ADDR_BIT_LEN >> 3)
#endif #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 * @brief The container descriptor used to identify a universal address entry
*/ */
@ -69,7 +80,7 @@ void universal_address_reset(void);
* @param[in] addr_size the number of bytes required for the address entry * @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 * @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); universal_address_container_t *universal_address_add(uint8_t *addr, size_t addr_size);
@ -90,7 +101,7 @@ void universal_address_rem(universal_address_container_t *entry);
* this value is overwritten with the actual size required * this value is overwritten with the actual size required
* *
* @return addr if the address is copied to the addr destination * @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* universal_address_get_address(universal_address_container_t *entry,
uint8_t *addr, size_t *addr_size); uint8_t *addr, size_t *addr_size);
@ -107,9 +118,12 @@ uint8_t* universal_address_get_address(universal_address_container_t *entry,
* with the number of matching bits till the * with the number of matching bits till the
* first of trailing `0`s * first of trailing `0`s
* *
* @return 0 if the entries are equal * @return UNIVERSAL_ADDRESS_EQUAL if the entries are equal
* 1 if the entry match to a certain prefix (trailing '0's in *entry) * @return UNIVERSAL_ADDRESS_MATCHING_PREFIX if the entry matches to a certain prefix
* -ENOENT if the given adresses do not match * (trailing '0's in @p 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, int universal_address_compare(universal_address_container_t *entry,
uint8_t *addr, size_t *addr_size_in_bits); uint8_t *addr, size_t *addr_size_in_bits);
@ -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) * e.g. for an ipv6_addr_t it would be sizeof(ipv6_addr_t)
* regardless if the stored prefix is < ::/128 * regardless if the stored prefix is < ::/128
* *
* @return 0 if the entries are equal * @return UNIVERSAL_ADDRESS_EQUAL if the entries are equal
* 1 if the entry match to a certain prefix (trailing '0's in *prefix) * @return UNIVERSAL_ADDRESS_MATCHING_PREFIX if the entry matches to a certain prefix
* -ENOENT if the given adresses do not match * (trailing '0's in @p prefix)
* @return -ENOENT if the given adresses do not match
*/ */
int universal_address_compare_prefix(universal_address_container_t *entry, int universal_address_compare_prefix(universal_address_container_t *entry,
uint8_t *prefix, size_t prefix_size_in_bits); uint8_t *prefix, size_t prefix_size_in_bits);

View File

@ -215,13 +215,13 @@ int universal_address_compare(universal_address_container_t *entry,
if (test_all_zeros) { if (test_all_zeros) {
*addr_size_in_bits = 0; *addr_size_in_bits = 0;
mutex_unlock(&mtx_access); mutex_unlock(&mtx_access);
return 2; return UNIVERSAL_ADDRESS_IS_ALL_ZERO_ADDRESS;
} }
/* if we have no distinct bytes the addresses are equal */ /* if we have no distinct bytes the addresses are equal */
if (idx == -1) { if (idx == -1) {
mutex_unlock(&mtx_access); mutex_unlock(&mtx_access);
return 0; return UNIVERSAL_ADDRESS_EQUAL;
} }
/* count equal bits */ /* count equal bits */
@ -235,7 +235,7 @@ int universal_address_compare(universal_address_container_t *entry,
/* get the total number of matching bits */ /* get the total number of matching bits */
*addr_size_in_bits = (idx << 3) + j; *addr_size_in_bits = (idx << 3) + j;
ret = 1; ret = UNIVERSAL_ADDRESS_MATCHING_PREFIX;
mutex_unlock(&mtx_access); mutex_unlock(&mtx_access);
return ret; return ret;
@ -254,31 +254,31 @@ int universal_address_compare_prefix(universal_address_container_t *entry,
/* Get the index of the first trailing `0` */ /* Get the index of the first trailing `0` */
int i = 0; int i = 0;
for( i = entry->address_size-1; i >= 0; --i) { for (i = entry->address_size-1; i >= 0; --i) {
if( prefix[i] != 0 ) { if (prefix[i] != 0) {
break; 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 */ /* if the bytes-1 equals we check the bits of the lowest byte */
uint8_t bitmask = 0x00; uint8_t bitmask = 0x00;
/* get a bitmask for the trailing 0b */ /* get a bitmask for the trailing 0b */
for( uint8_t j = 0; j < 8; ++j ) { for (uint8_t j = 0; j < 8; ++j) {
if ( (prefix[i] >> j) & 0x01 ) { if ((prefix[i] >> j) & 0x01) {
bitmask = 0xff << j; bitmask = 0xff << j;
break; break;
} }
} }
if( (entry->address[i] & bitmask) == (prefix[i] & bitmask) ) { if ((entry->address[i] & bitmask) == (prefix[i] & bitmask)) {
ret = entry->address[i] != prefix[i]; ret = entry->address[i] != prefix[i];
if( ret == 0 ) { if (ret == UNIVERSAL_ADDRESS_EQUAL) {
/* check if the remaining bits from entry are significant */ /* check if the remaining bits from entry are significant */
i++; i++;
for(; i < entry->address_size; ++i) { for ( ; i < entry->address_size; ++i) {
if( entry->address[i] != 0 ) { if (entry->address[i] != 0) {
ret = 1; ret = UNIVERSAL_ADDRESS_MATCHING_PREFIX;
break; break;
} }
} }