Skip to content

Commit

Permalink
bgpd: Use COMMUNITY_SIZE instead of just 4
Browse files Browse the repository at this point in the history
Easier to maintain and read.

Signed-off-by: Donatas Abraitis <donatas.abraitis@gmail.com>
  • Loading branch information
ton31337 committed Apr 8, 2020
1 parent c0d4a6d commit 11400e7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
23 changes: 13 additions & 10 deletions bgpd/bgp_community.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion bgpd/bgp_community.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down

0 comments on commit 11400e7

Please sign in to comment.