Skip to content

Commit ae228d7

Browse files
authored
Merge pull request #15 from douzzer/20220425-decision_event
wolfsentry_event.decision_event
2 parents 9d7f97c + d567407 commit ae228d7

File tree

11 files changed

+142
-32
lines changed

11 files changed

+142
-32
lines changed

src/events.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ wolfsentry_errcode_t wolfsentry_event_clone_bare(
133133
(*new_event)->insert_event = NULL;
134134
(*new_event)->match_event = NULL;
135135
(*new_event)->delete_event = NULL;
136+
(*new_event)->decision_event = NULL;
136137
WOLFSENTRY_LIST_HEADER_RESET((*new_event)->action_list.header);
137138

138139
if (src_event->config) {
@@ -192,6 +193,15 @@ wolfsentry_errcode_t wolfsentry_event_clone_resolve(
192193
WOLFSENTRY_REFCOUNT_INCREMENT(new_event->delete_event->header.refcount);
193194
}
194195

196+
if (src_event->decision_event) {
197+
new_event->decision_event = src_event->decision_event;
198+
if ((ret = wolfsentry_table_ent_get(&dest_context->events->header, (struct wolfsentry_table_ent_header **)&new_event->decision_event)) < 0) {
199+
new_event->decision_event = NULL;
200+
WOLFSENTRY_ERROR_RERETURN(ret);
201+
}
202+
WOLFSENTRY_REFCOUNT_INCREMENT(new_event->decision_event->header.refcount);
203+
}
204+
195205
WOLFSENTRY_RETURN_OK;
196206
}
197207

@@ -317,6 +327,8 @@ wolfsentry_errcode_t wolfsentry_event_drop_reference(struct wolfsentry_context *
317327
wolfsentry_event_drop_reference(wolfsentry, event->match_event, NULL);
318328
if (event->delete_event)
319329
wolfsentry_event_drop_reference(wolfsentry, event->delete_event, NULL);
330+
if (event->decision_event)
331+
wolfsentry_event_drop_reference(wolfsentry, event->decision_event, NULL);
320332
wolfsentry_event_free(wolfsentry, event);
321333
if (action_results)
322334
WOLFSENTRY_SET_BITS(*action_results, WOLFSENTRY_ACTION_RES_DEALLOCATED);
@@ -353,6 +365,11 @@ wolfsentry_errcode_t wolfsentry_event_delete(
353365
return ret;
354366
old->delete_event = NULL;
355367
}
368+
if (old->decision_event) {
369+
if ((ret = wolfsentry_event_drop_reference(wolfsentry, old->decision_event, NULL /* action_results */)) < 0)
370+
return ret;
371+
old->decision_event = NULL;
372+
}
356373

357374
if ((ret = wolfsentry_table_ent_delete_1(wolfsentry, &old->header)) < 0)
358375
return ret;
@@ -526,6 +543,12 @@ wolfsentry_errcode_t wolfsentry_event_set_subevent(
526543
event->delete_event = subevent;
527544
ret = WOLFSENTRY_ERROR_ENCODE(OK);
528545
break;
546+
case WOLFSENTRY_ACTION_TYPE_DECISION:
547+
if (event->decision_event)
548+
WOLFSENTRY_WARN_ON_FAILURE(wolfsentry_event_drop_reference(wolfsentry, event->decision_event, NULL /* action_results */));
549+
event->decision_event = subevent;
550+
ret = WOLFSENTRY_ERROR_ENCODE(OK);
551+
break;
529552
case WOLFSENTRY_ACTION_TYPE_POST:
530553
case WOLFSENTRY_ACTION_TYPE_NONE:
531554
break;

src/json/load_config.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"insert-event" : string,
7070
"match-event" : string,
7171
"delete-event" : string
72+
"decision-event" : string
7273
7374
}
7475
],
@@ -863,6 +864,8 @@ static wolfsentry_errcode_t handle_event_clause(struct wolfsentry_json_process_s
863864
subevent_type = WOLFSENTRY_ACTION_TYPE_MATCH;
864865
else if (! strcmp(jps->cur_keyname, "delete-event"))
865866
subevent_type = WOLFSENTRY_ACTION_TYPE_DELETE;
867+
else if (! strcmp(jps->cur_keyname, "decision-event"))
868+
subevent_type = WOLFSENTRY_ACTION_TYPE_DECISION;
866869

867870
if (subevent_type != WOLFSENTRY_ACTION_TYPE_NONE) {
868871
if (data_size > WOLFSENTRY_MAX_LABEL_BYTES)

src/kv.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,12 @@ static wolfsentry_errcode_t wolfsentry_kv_get_1(
197197
struct wolfsentry_kv_pair_internal *ret_kv = (struct wolfsentry_kv_pair_internal *)kv_template;
198198
if ((ret = wolfsentry_table_ent_get(&kv_table->header, (struct wolfsentry_table_ent_header **)&ret_kv)) < 0)
199199
return ret;
200-
if ((WOLFSENTRY_KV_TYPE(&kv_template->kv) != WOLFSENTRY_KV_NONE) &&
200+
/* special-case request for uint with object sint that is >= 0. */
201+
if ((WOLFSENTRY_KV_TYPE(&kv_template->kv) == WOLFSENTRY_KV_UINT) &&
202+
(WOLFSENTRY_KV_TYPE(&ret_kv->kv) == WOLFSENTRY_KV_SINT) &&
203+
(WOLFSENTRY_KV_V_SINT(&ret_kv->kv) >= 0))
204+
;
205+
else if ((WOLFSENTRY_KV_TYPE(&kv_template->kv) != WOLFSENTRY_KV_NONE) &&
201206
(WOLFSENTRY_KV_TYPE(&kv_template->kv) != WOLFSENTRY_KV_TYPE(&ret_kv->kv)))
202207
WOLFSENTRY_ERROR_RETURN(WRONG_TYPE);
203208
*kv = ret_kv;

src/routes.c

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,7 @@ static wolfsentry_errcode_t wolfsentry_route_event_dispatch_0(
931931
)
932932
{
933933
struct wolfsentry_eventconfig_internal *config = (route->parent_event && route->parent_event->config) ? route->parent_event->config : &wolfsentry->config;
934+
wolfsentry_errcode_t ret;
934935

935936
if (! (route->flags & WOLFSENTRY_ROUTE_FLAG_DONT_COUNT_HITS))
936937
WOLFSENTRY_ATOMIC_INCREMENT(route->header.hitcount, 1);
@@ -969,12 +970,14 @@ static wolfsentry_errcode_t wolfsentry_route_event_dispatch_0(
969970
if (*action_results & WOLFSENTRY_ACTION_RES_CONNECT) {
970971
if (route->meta.connection_count >= config->config.max_connection_count) {
971972
*action_results |= WOLFSENTRY_ACTION_RES_REJECT;
972-
WOLFSENTRY_RETURN_OK;
973+
ret = WOLFSENTRY_ERROR_ENCODE(OK);
974+
goto done;
973975
}
974976
if (WOLFSENTRY_ATOMIC_INCREMENT_BY_ONE(route->meta.connection_count) > config->config.max_connection_count) {
975977
WOLFSENTRY_ATOMIC_DECREMENT_BY_ONE(route->meta.connection_count);
976978
*action_results |= WOLFSENTRY_ACTION_RES_REJECT;
977-
WOLFSENTRY_RETURN_OK;
979+
ret = WOLFSENTRY_ERROR_ENCODE(OK);
980+
goto done;
978981
}
979982
} else if (*action_results & WOLFSENTRY_ACTION_RES_DISCONNECT)
980983
WOLFSENTRY_ATOMIC_DECREMENT_BY_ONE(route->meta.connection_count);
@@ -987,9 +990,11 @@ static wolfsentry_errcode_t wolfsentry_route_event_dispatch_0(
987990
if ((route->flags & WOLFSENTRY_ROUTE_FLAG_PENALTYBOXED)) {
988991
if ((config->config.penaltybox_duration > 0) && (route->meta.last_penaltybox_time != 0)) {
989992
wolfsentry_time_t now;
990-
wolfsentry_errcode_t ret = WOLFSENTRY_GET_TIME(&now);
991-
if (ret < 0)
992-
return ret;
993+
ret = WOLFSENTRY_GET_TIME(&now);
994+
if (ret < 0) {
995+
*action_results |= WOLFSENTRY_ACTION_RES_ERROR;
996+
goto done;
997+
}
993998
if (WOLFSENTRY_DIFF_TIME(now, route->meta.last_penaltybox_time) > config->config.penaltybox_duration) {
994999
wolfsentry_route_flags_t flags_before, flags_after;
9951000
WOLFSENTRY_WARN_ON_FAILURE(
@@ -1004,18 +1009,36 @@ static wolfsentry_errcode_t wolfsentry_route_event_dispatch_0(
10041009
*action_results |= WOLFSENTRY_ACTION_RES_REJECT;
10051010
} else
10061011
*action_results |= WOLFSENTRY_ACTION_RES_REJECT;
1007-
WOLFSENTRY_RETURN_OK;
1012+
ret = WOLFSENTRY_ERROR_ENCODE(OK);
1013+
goto done;
10081014
} else if ((route->flags & WOLFSENTRY_ROUTE_FLAG_GREENLISTED)) {
10091015
*action_results |= WOLFSENTRY_ACTION_RES_ACCEPT;
1010-
WOLFSENTRY_RETURN_OK;
1016+
ret = WOLFSENTRY_ERROR_ENCODE(OK);
1017+
goto done;
10111018
}
10121019

10131020
if (! WOLFSENTRY_MASKIN_BITS(*action_results, WOLFSENTRY_ACTION_RES_ACCEPT|WOLFSENTRY_ACTION_RES_REJECT))
10141021
*action_results |= wolfsentry->routes_static->default_policy;
10151022
if (! WOLFSENTRY_MASKIN_BITS(*action_results, WOLFSENTRY_ACTION_RES_ACCEPT|WOLFSENTRY_ACTION_RES_REJECT))
10161023
*action_results |= wolfsentry->routes_dynamic->default_policy;
10171024

1018-
WOLFSENTRY_RETURN_OK;
1025+
ret = WOLFSENTRY_ERROR_ENCODE(OK);
1026+
1027+
done:
1028+
1029+
if (route->parent_event && route->parent_event->decision_event && route->parent_event->decision_event->action_list.header.head) {
1030+
WOLFSENTRY_WARN_ON_FAILURE(wolfsentry_action_list_dispatch(
1031+
wolfsentry,
1032+
caller_arg,
1033+
route->parent_event->decision_event,
1034+
trigger_event,
1035+
WOLFSENTRY_ACTION_TYPE_DECISION,
1036+
route_table,
1037+
route,
1038+
action_results));
1039+
}
1040+
1041+
return ret;
10191042
}
10201043

10211044
static wolfsentry_errcode_t wolfsentry_route_event_dispatch_1(
@@ -1094,8 +1117,11 @@ static wolfsentry_errcode_t wolfsentry_route_event_dispatch_1(
10941117
*inexact_matches = WOLFSENTRY_ROUTE_FLAG_NONE;
10951118
} else {
10961119
WOLFSENTRY_WARN_ON_FAILURE(wolfsentry_route_drop_reference_1(wolfsentry, route, NULL /* action_results */));
1097-
if (ret >= 0)
1098-
ret = WOLFSENTRY_ERROR_ENCODE(NOT_INSERTED); /* not an error */
1120+
if (ret >= 0) {
1121+
/* inform caller that no entry was found or added. */
1122+
WOLFSENTRY_SET_BITS(*action_results, WOLFSENTRY_ACTION_RES_INSERT|WOLFSENTRY_ACTION_RES_DELETE);
1123+
ret = WOLFSENTRY_ERROR_ENCODE(OK);
1124+
}
10991125
return ret;
11001126
}
11011127
} else {

src/util.c

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ const char *wolfsentry_errcode_error_string(wolfsentry_errcode_t e)
130130
return "Operation attempted with object already marked as stopped";
131131
case WOLFSENTRY_ERROR_ID_WRONG_OBJECT:
132132
return "Operation attempted on wrong type of object";
133-
case WOLFSENTRY_ERROR_ID_NOT_INSERTED:
134-
return "Object was not inserted in table (informational, not an error)";
135133
case WOLFSENTRY_ERROR_ID_DATA_MISSING:
136134
return "Requested data or buffer is not present";
137135
case WOLFSENTRY_ERROR_ID_NOT_PERMITTED:
@@ -168,6 +166,44 @@ const char *wolfsentry_errcode_error_string(wolfsentry_errcode_t e)
168166
return "unknown error code";
169167
}
170168

169+
const char *wolfsentry_action_res_decode(wolfsentry_action_res_t res, unsigned int bit) {
170+
if (bit > 31)
171+
return "<out-of-range>";
172+
if (res & (1U << bit)) {
173+
switch(1U << bit) {
174+
case WOLFSENTRY_ACTION_RES_NONE: /* not reachable */
175+
return "none";
176+
case WOLFSENTRY_ACTION_RES_ACCEPT:
177+
return "accept";
178+
case WOLFSENTRY_ACTION_RES_REJECT:
179+
return "reject";
180+
case WOLFSENTRY_ACTION_RES_CONNECT:
181+
return "connect";
182+
case WOLFSENTRY_ACTION_RES_DISCONNECT:
183+
return "disconnect";
184+
case WOLFSENTRY_ACTION_RES_DEROGATORY:
185+
return "derogatory";
186+
case WOLFSENTRY_ACTION_RES_COMMENDABLE:
187+
return "commendable";
188+
case WOLFSENTRY_ACTION_RES_CONTINUE:
189+
return "continue";
190+
case WOLFSENTRY_ACTION_RES_STOP:
191+
return "stop";
192+
case WOLFSENTRY_ACTION_RES_INSERT:
193+
return "insert";
194+
case WOLFSENTRY_ACTION_RES_DELETE:
195+
return "delete";
196+
case WOLFSENTRY_ACTION_RES_DEALLOCATED:
197+
return "deallocated";
198+
case WOLFSENTRY_ACTION_RES_ERROR:
199+
return "error";
200+
default:
201+
return "<user>";
202+
}
203+
} else
204+
return NULL;
205+
}
206+
171207
#endif /* WOLFSENTRY_ERROR_STRINGS */
172208

173209
#ifdef WOLFSENTRY_MALLOC_BUILTINS

src/wolfsentry_internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ struct wolfsentry_table_header {
167167
#define WOLFSENTRY_TABLE_HEADER_RESET(table) do { \
168168
(table).head = (table).tail = NULL; \
169169
(table).n_ents = 0; \
170+
(table).n_inserts = 0; \
171+
(table).n_deletes = 0; \
170172
} while (0)
171173

172174
struct wolfsentry_cursor {
@@ -215,6 +217,7 @@ struct wolfsentry_event {
215217
struct wolfsentry_event *insert_event; /* child event with setup routines (if any) for routes inserted with this as parent_event. */
216218
struct wolfsentry_event *match_event; /* child event with state management for routes inserted with this as parent_event. */
217219
struct wolfsentry_event *delete_event; /* child event with cleanup routines (if any) for routes inserted with this as parent_event. */
220+
struct wolfsentry_event *decision_event; /* child event with logic for notifications and logging around decisions for routes inserted with this as parent_event. */
218221

219222
wolfsentry_priority_t priority;
220223

tests/test-config-numeric.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
},
1717
{
1818
"label" : "event-on-match",
19-
"actions" : [ "handle-match", "notify-on-match" ]
19+
"actions" : [ "handle-match" ]
20+
},
21+
{
22+
"label" : "event-on-decision",
23+
"actions" : [ "notify-on-decision" ]
2024
},
2125
{
2226
"label" : "static-route-parent",
@@ -28,7 +32,8 @@
2832
"actions" : [ "handle-connect", "handle-connect2" ],
2933
"insert-event" : "event-on-insert",
3034
"match-event" : "event-on-match",
31-
"delete-event" : "event-on-delete"
35+
"delete-event" : "event-on-delete",
36+
"decision-event" : "event-on-decision"
3237
},
3338
{
3439
"label" : "call-in-from-unit-test"

tests/test-config.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
},
1717
{
1818
"label" : "event-on-match",
19-
"actions" : [ "handle-match", "notify-on-match" ]
19+
"actions" : [ "handle-match" ]
20+
},
21+
{
22+
"label" : "event-on-decision",
23+
"actions" : [ "notify-on-decision" ]
2024
},
2125
{
2226
"label" : "static-route-parent",
@@ -28,7 +32,8 @@
2832
"actions" : [ "handle-connect", "handle-connect2" ],
2933
"insert-event" : "event-on-insert",
3034
"match-event" : "event-on-match",
31-
"delete-event" : "event-on-delete"
35+
"delete-event" : "event-on-delete",
36+
"decision-event" : "event-on-decision"
3237
},
3338
{
3439
"label" : "call-in-from-unit-test"

tests/unittests.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2279,7 +2279,7 @@ static int test_json(const char *fname) {
22792279

22802280
WOLFSENTRY_EXIT_ON_FAILURE(wolfsentry_action_insert(
22812281
wolfsentry,
2282-
"notify-on-match",
2282+
"notify-on-decision",
22832283
WOLFSENTRY_LENGTH_NULL_TERMINATED,
22842284
WOLFSENTRY_ACTION_FLAG_NONE,
22852285
test_action,

wolfsentry/wolfsentry.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ typedef enum {
292292
WOLFSENTRY_ACTION_TYPE_POST = 1, /* called when an event is posted. */
293293
WOLFSENTRY_ACTION_TYPE_INSERT = 2, /* called when a route is added to the route table for this event. */
294294
WOLFSENTRY_ACTION_TYPE_MATCH = 3, /* called by wolfsentry_route_dispatch() for a route match. */
295-
WOLFSENTRY_ACTION_TYPE_DELETE = 4 /* called when a route associated with this event expires or is otherwise deleted. */
295+
WOLFSENTRY_ACTION_TYPE_DELETE = 4, /* called when a route associated with this event expires or is otherwise deleted. */
296+
WOLFSENTRY_ACTION_TYPE_DECISION = 5 /* called after final decision has been made by wolfsentry_route_event_dispatch*(). */
296297
} wolfsentry_action_type_t;
297298

298299
#define WOLFSENTRY_ACTION_RES_USER_SHIFT 16U
@@ -510,6 +511,10 @@ WOLFSENTRY_API wolfsentry_errcode_t wolfsentry_from_epoch_time(struct wolfsentry
510511
WOLFSENTRY_API wolfsentry_errcode_t wolfsentry_interval_to_seconds(struct wolfsentry_context *wolfsentry, wolfsentry_time_t howlong, long *howlong_secs, long *howlong_nsecs);
511512
WOLFSENTRY_API wolfsentry_errcode_t wolfsentry_interval_from_seconds(struct wolfsentry_context *wolfsentry, long howlong_secs, long howlong_nsecs, wolfsentry_time_t *howlong);
512513

514+
#ifdef WOLFSENTRY_ERROR_STRINGS
515+
WOLFSENTRY_API const char *wolfsentry_action_res_decode(wolfsentry_action_res_t res, unsigned int bit);
516+
#endif
517+
513518
struct wolfsentry_host_platform_interface {
514519
struct wolfsentry_allocator *allocator;
515520
struct wolfsentry_timecbs *timecbs;

0 commit comments

Comments
 (0)