30#define _COMPONENT_ "db"
38#define INIT_VEC_CAPACITY 16
41#define VEC_ENSURE_CAPACITY(vec, size, capacity, type) \
42 if ((capacity) < (size) + 1) { \
44 (vec) = realloc((vec), (capacity) * sizeof(type)); \
47static pib_t g_pib = { 0 };
54 memset(p, 0,
sizeof(
pib_t));
56 p->acls_capacity = INIT_VEC_CAPACITY;
58 p->acls = malloc(p->acls_capacity *
sizeof(
acl_t));
60 p->routemaps_capacity = INIT_VEC_CAPACITY;
61 p->routemaps_size = 0;
62 p->routemaps = malloc(p->routemaps_capacity *
sizeof(
routemap_t));
70 for (
size_t i = 0; i < pib->acls_size; i++) {
71 for (
size_t j = 0; j < pib->acls[i].entries_size; j++)
72 free(pib->acls[i].entries[j].expression);
73 free(pib->acls[i].entries);
74 free(pib->acls[i].name);
78 for (
size_t i = 0; i < pib->routemaps_size; i++) {
79 for (
size_t j = 0; j < pib->routemaps[i].size; j++) {
80 for (
size_t k = 0; k < pib->routemaps[i].statements[j].matchers_size; k++)
81 free(pib->routemaps[i].statements[j].matchers[k].acls);
83 for (
size_t k = 0; k < pib->routemaps[i].statements[j].actions_size; k++)
85 &pib->routemaps[i].statements[j].actions[k]);
86 free(pib->routemaps[i].statements[j].matchers);
87 free(pib->routemaps[i].statements[j].actions);
89 free(pib->routemaps[i].name);
90 free(pib->routemaps[i].statements);
100 acl_t *a = &pib->acls[pib->acls_size++];
102 a->name = strdup(name);
104 a->entries_capacity = INIT_VEC_CAPACITY;
106 a->entries = malloc(a->entries_capacity *
sizeof(
acl_entry_t));
117 routemap_t *r = &pib->routemaps[pib->routemaps_size++];
119 r->name = strdup(name);
131 for (
size_t i = 0; i < pib->acls_size; i++)
132 if (strcmp(pib->acls[i].name, name) == 0)
133 return &pib->acls[i];
140 for (
size_t i = 0; i < pib->routemaps_size; i++)
141 if (strcmp(pib->routemaps[i].name, name) == 0)
142 return &pib->routemaps[i];
153 acl_entry_t *e = &acl->entries[acl->entries_size++];
156 e->expression = strdup(expression);
162 for (
size_t i = 0; i < acl->entries_size; i++)
163 if (strcmp(acl->entries[i].expression, expression) == 0)
164 return &acl->entries[i];
171 return (c >=
'0' && c <=
'9') || (c >=
'A' && c <=
'E');
188pattern_check(
const char *pat,
const char *target)
193 if (!*pat && !*target)
197 if (*pat ==
'.' && !*target)
201 if (!*pat || !*target)
206 if (ispfxdigit(*target)) {
215 if (ispfxdigit(*pat) && *pat != *target)
217 if (*pat ==
'X' && !isdigit(*target))
219 if (*pat ==
'Z' && !(*target >=
'1' && *target <=
'9'))
221 if (*pat ==
'N' && !(*target >=
'2' && *target <=
'9'))
245 && (strlen(expr) <= strlen(target))
246 && (strncmp(expr, target, strlen(expr)) == 0)) ||
248 (pattern_check(expr, target));
259 for (
size_t i = 0; i < acl->entries_size; i++) {
260 const char *expr = acl->entries[i].expression;
262 return !acl->entries[i].
deny;
298 free(action->valstr1);
300 free(action->valstr2);
309 matcher->acls[matcher->size++] = (
acl_t *)acl;
323 for (
size_t i = 0; i < statement->actions_size; i++)
324 if (statement->actions[i].attribute == attribute)
325 return &statement->actions[i];
338 for (; i < routemap->size && routemap->statements[i].seq < seq; i++);
340 memmove(&routemap->statements[i + 1], &routemap->statements[i],
349 s->matchers_size = s->actions_size = 0;
350 s->matchers_capacity = s->actions_capacity = INIT_VEC_CAPACITY;
360 for (
size_t i = 0; i < routemap->size; i++)
361 if (routemap->statements[i].seq == seq)
362 return &routemap->statements[i];
369 for (
size_t i = 0; i < routemap->size; i++) {
370 for (
size_t j = 0; j < routemap->statements[i].matchers_size; j++) {
372 for (
size_t k = 0; t && k < routemap->statements[i].matchers[j].size; k++)
373 t &=
acl_check(routemap->statements[i].matchers[j].acls[k], route);
375 return &routemap->statements[i];
pib_t * pib_new()
Initialize PIB.
Definition pib.c:50
int acl_entry_match(const char *expr, const char *target)
Match target against ACL expression.
Definition pib.c:238
routemap_t * pib_routemap_find(const pib_t *pib, const char *name)
Find a route map by name.
Definition pib.c:138
routemap_t * pib_routemap_new(pib_t *pib, const char *name, int deny)
Create and insert route map into PIB.
Definition pib.c:112
void routemap_statement_insert_action(routemap_statement_t *statement, const routemap_action_t *action)
Insert action (copy) into route map.
Definition pib.c:284
void routemap_statement_action_deinit(routemap_action_t *action)
Deinit route map action.
Definition pib.c:295
acl_entry_t * acl_find(const acl_t *acl, const char *expression)
Find entry in ACL.
Definition pib.c:160
#define VEC_ENSURE_CAPACITY(vec, size, capacity, type)
Ensure vector has at least 1 free slot for new element.
Definition pib.c:41
acl_t * pib_acl_new(pib_t *pib, const char *name)
Create and insert ACL into PIB.
Definition pib.c:96
int acl_check(const acl_t *acl, const char *target)
Check target against ACL.
Definition pib.c:257
void pib_destroy(pib_t *pib)
Deinitialize PIB.
Definition pib.c:68
routemap_statement_t * routemap_statement_new(routemap_t *routemap, uint32_t seq, int deny)
Allocate statement in route map.
Definition pib.c:331
const routemap_statement_t * routemap_match(const routemap_t *routemap, const char *route)
Match route against route map matchers.
Definition pib.c:367
routemap_statement_t * routemap_statement_find(routemap_t *routemap, uint32_t seq)
Find statement in route map by sequence number.
Definition pib.c:358
routemap_action_t * routemap_statement_action_find(const routemap_statement_t *statement, routemap_set_attr_t attribute)
Find action by attribute.
Definition pib.c:320
void routemap_matcher_insert(routemap_matcher_t *matcher, const acl_t *acl)
Insert ACL into route map matcher.
Definition pib.c:304
routemap_matcher_t * routemap_statement_matcher_new(routemap_statement_t *statement, int af)
Allocate a matcher in a route map statement.
Definition pib.c:268
void acl_insert(acl_t *acl, int deny, const char *expression)
Create and insert entry into ACL.
Definition pib.c:148
void routemap_matcher_deinit(routemap_matcher_t *matcher)
Deinitialize a route map matcher.
Definition pib.c:313
acl_t * pib_acl_find(const pib_t *pib, const char *name)
Find an ACL by name.
Definition pib.c:129
routemap_set_attr_t
Attributes that can be set.
Definition pib.h:47
af
Address families.
Definition protocol.h:206
Access Control List entry.
Definition pib.h:33
int deny
Definition pib.h:34
Access Control List.
Definition pib.h:39
Policy Information Base.
Definition pib.h:87
Route map Action.
Definition pib.h:62
Route map ACL matcher.
Definition pib.h:55
size_t capacity
Definition pib.h:58
Route map statement.
Definition pib.h:69
Route map.
Definition pib.h:79
size_t capacity
Definition pib.h:82