bgpd: Use COMMUNITY_SIZE instead of just 4

Easier to maintain and read.

Signed-off-by: Donatas Abraitis <donatas.abraitis@gmail.com>
This commit is contained in:
Donatas Abraitis 2020-04-08 18:09:25 +03:00
parent c0d4a6d852
commit 11400e739e
2 changed files with 16 additions and 11 deletions

View File

@ -146,7 +146,7 @@ uint32_t community_val_get(struct community *com, int i)
uint32_t val;
p = (uint8_t *)com->val;
p += (i * 4);
p += (i * COMMUNITY_SIZE);
memcpy(&val, p, sizeof(uint32_t));
@ -514,11 +514,11 @@ struct community *community_parse(uint32_t *pnt, unsigned short length)
struct community *new;
/* If length is malformed return NULL. */
if (length % 4)
if (length % COMMUNITY_SIZE)
return NULL;
/* Make temporary community for hash look up. */
tmp.size = length / 4;
tmp.size = length / COMMUNITY_SIZE;
tmp.val = pnt;
new = community_uniq_sort(&tmp);
@ -533,8 +533,9 @@ struct community *community_dup(struct community *com)
new = XCALLOC(MTYPE_COMMUNITY, sizeof(struct community));
new->size = com->size;
if (new->size) {
new->val = XMALLOC(MTYPE_COMMUNITY_VAL, com->size * 4);
memcpy(new->val, com->val, com->size * 4);
new->val = XMALLOC(MTYPE_COMMUNITY_VAL,
com->size * COMMUNITY_SIZE);
memcpy(new->val, com->val, com->size * COMMUNITY_SIZE);
} else
new->val = NULL;
return new;
@ -600,7 +601,8 @@ bool community_cmp(const struct community *com1, const struct community *com2)
return false;
if (com1->size == com2->size)
if (memcmp(com1->val, com2->val, com1->size * 4) == 0)
if (memcmp(com1->val, com2->val, com1->size * COMMUNITY_SIZE)
== 0)
return true;
return false;
}
@ -610,13 +612,14 @@ struct community *community_merge(struct community *com1,
struct community *com2)
{
if (com1->val)
com1->val = XREALLOC(MTYPE_COMMUNITY_VAL, com1->val,
(com1->size + com2->size) * 4);
com1->val =
XREALLOC(MTYPE_COMMUNITY_VAL, com1->val,
(com1->size + com2->size) * COMMUNITY_SIZE);
else
com1->val = XMALLOC(MTYPE_COMMUNITY_VAL,
(com1->size + com2->size) * 4);
(com1->size + com2->size) * COMMUNITY_SIZE);
memcpy(com1->val + com1->size, com2->val, com2->size * 4);
memcpy(com1->val + com1->size, com2->val, com2->size * COMMUNITY_SIZE);
com1->size += com2->size;
return com1;

View File

@ -61,8 +61,10 @@ struct community {
#define COMMUNITY_LOCAL_AS 0xFFFFFF03
#define COMMUNITY_NO_PEER 0xFFFFFF04
#define COMMUNITY_SIZE 4
/* Macros of community attribute. */
#define com_length(X) ((X)->size * 4)
#define com_length(X) ((X)->size * COMMUNITY_SIZE)
#define com_lastval(X) ((X)->val + (X)->size - 1)
#define com_nthval(X,n) ((X)->val + (n))