Skip to content

Commit ce9edae

Browse files
committed
vcc_acl: change compare functions to differentiate cases
This is in preparation of a follow-up commit to merge acl entries and detect supersedes from supernets, but these changes are backwards compatible with the previous CMP() if being used as a comparison function for which only negative, zero and positive result are relevant. The A in CMPA() stands for "adjacent". CMPA() returns -3/3 for left of/right of.
1 parent 2cf57b2 commit ce9edae

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

lib/libvcc/vcc_acl.c

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,29 @@ struct acl_e {
5959
struct token *t_mask;
6060
};
6161

62-
/* Compare two acl rules for ordering */
62+
/*
63+
* Compare two acl rules for ordering
64+
* returns:
65+
* 0 same
66+
* -1/1 strictly less/greater
67+
* -2/2 b contains a / a contains b
68+
* -3/3 a left of b / b left of a
69+
*/
6370

64-
#define CMP(a, b) \
71+
#define CMP(n, a, b) \
6572
do { \
6673
if ((a) < (b)) \
67-
return (-1); \
74+
return (-n); \
6875
else if ((b) < (a)) \
69-
return (1); \
76+
return (n); \
77+
} while (0)
78+
79+
#define CMPA(a, b) \
80+
do { \
81+
if ((a) + 1 == (b)) \
82+
return (-3); \
83+
else if ((b) + 1 == (a)) \
84+
return (3); \
7085
} while (0)
7186

7287
static void
@@ -84,6 +99,7 @@ vcl_acl_cmp(const struct acl_e *ae1, const struct acl_e *ae2)
8499
{
85100
const unsigned char *p1, *p2;
86101
unsigned m;
102+
unsigned char a1, a2;
87103

88104
CHECK_OBJ_NOTNULL(ae1, VCC_ACL_E_MAGIC);
89105
CHECK_OBJ_NOTNULL(ae2, VCC_ACL_E_MAGIC);
@@ -94,17 +110,22 @@ vcl_acl_cmp(const struct acl_e *ae1, const struct acl_e *ae2)
94110
if (ae2->mask < m)
95111
m = ae2->mask;
96112
for (; m >= 8; m -= 8) {
97-
CMP(*p1, *p2);
113+
CMP(1, *p1, *p2);
98114
p1++;
99115
p2++;
100116
}
101117
if (m) {
102-
m = 0xff00 >> m;
103-
m &= 0xff;
104-
CMP(*p1 & m, *p2 & m);
118+
assert (m < 8);
119+
a1 = *p1 >> (8 - m);
120+
a2 = *p2 >> (8 - m);
121+
if (ae1->mask == ae2->mask)
122+
CMPA(a1, a2);
123+
CMP(1, a1, a2);
124+
} else if (ae1->mask == ae2->mask) {
125+
CMPA(*p1, *p2);
105126
}
106127
/* Long mask is less than short mask */
107-
CMP(ae2->mask, ae1->mask);
128+
CMP(2, ae2->mask, ae1->mask);
108129

109130
return (0);
110131
}

0 commit comments

Comments
 (0)