From 6f558132a1ed376af73782298e7783ef751b2f01 Mon Sep 17 00:00:00 2001 From: yangwei Date: Wed, 20 Jul 2022 20:20:31 +0800 Subject: [PATCH 01/17] rename packet_io_loop to stellar_event_base_loop --- sdk/example/custom_event_plugin.cpp | 19 ++++--- sdk/example/http_event_plugin.cpp | 9 +++- sdk/example/plugin.inf | 0 .../custom_event_plugin.inf | 13 +++++ .../http_event_plugin/http_event_plugin.inf | 9 ++++ sdk/example/plugins/plugins.inf | 2 + sdk/include/event.h | 18 +++++++ sdk/include/http.h | 2 +- sdk/include/packet.h | 2 +- sdk/include/plugin.h | 8 +-- sdk/include/session.h | 28 ++++------ src/main.cpp | 10 ++-- src/packet_io/packet_io.cpp | 4 +- src/packet_io/packet_io.h | 4 +- src/packet_io/test/gtest_packet_io.cpp | 2 +- src/plugin_manager/CMakeLists.txt | 4 +- src/plugin_manager/plugin_manager.cpp | 24 ++++++--- src/plugin_manager/plugin_manager.h | 23 +++++++- src/protocol_decoder/http/CMakeLists.txt | 3 +- src/protocol_decoder/http/http.cpp | 8 +-- src/session_manager/CMakeLists.txt | 4 +- src/session_manager/session_manager.cpp | 29 +++------- src/session_manager/session_manager.h | 53 ++++++------------- 23 files changed, 160 insertions(+), 118 deletions(-) delete mode 100644 sdk/example/plugin.inf create mode 100644 sdk/example/plugins/custom_event_plugin/custom_event_plugin.inf create mode 100644 sdk/example/plugins/http_event_plugin/http_event_plugin.inf create mode 100644 sdk/example/plugins/plugins.inf create mode 100644 sdk/include/event.h diff --git a/sdk/example/custom_event_plugin.cpp b/sdk/example/custom_event_plugin.cpp index 7decbc4..7bf014e 100644 --- a/sdk/example/custom_event_plugin.cpp +++ b/sdk/example/custom_event_plugin.cpp @@ -9,17 +9,17 @@ void *custom_decode(const char *payload, uint32_t len, void **pme) return nullptr; } -int custom_plugin_entry(const struct session *s, int what, struct packet *p, const char *payload, uint32_t len, void **pme) +int custom_plugin_entry(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint32_t len, void **pme) { - void *info= custom_decode(payload, len, pme); - struct session *new_session=session_manager_custom_session_derive(s, _event); + struct stellar_session_event_extras *info= (struct stellar_session_event_extras *)custom_decode(payload, len, pme); + struct stellar_session *new_session=session_manager_session_derive(s, "CUSTOM_A"); session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info); session_manager_trigger_event(new_session, SESSION_EVENT_META, info); return 0; } -int custom_event_plugin_entry(const struct session *s, int what, struct packet *p, const char *payload, uint32_t len, void **pme) +int custom_event_plugin_entry(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint32_t len, void **pme) { return 0; @@ -27,9 +27,12 @@ int custom_event_plugin_entry(const struct session *s, int what, struct packet * int custom_plugin_init() { - _event = session_manager_custom_session_event_register("SESSION_TYPE_CUSTOM", (int)(SESSION_EVENT_OPENING|SESSION_EVENT_RAW_PKT|SESSION_EVENT_META|SESSION_EVENT_CLOSING)); - plugin_session_event_register(SESSION_TYPE_TCP, custom_plugin_entry, nullptr); - - plugin_custom_session_event_register("SESSION_TYPE_CUSTOM", custom_event_plugin_entry, nullptr); + plugin_session_event_register("TCP", custom_plugin_entry, nullptr); + plugin_session_event_register("CUSTOM_A", custom_event_plugin_entry, nullptr); return 0; +} + +void custom_plugin_destroy() +{ + return ; } \ No newline at end of file diff --git a/sdk/example/http_event_plugin.cpp b/sdk/example/http_event_plugin.cpp index 98fffd9..e8ac0bb 100644 --- a/sdk/example/http_event_plugin.cpp +++ b/sdk/example/http_event_plugin.cpp @@ -5,13 +5,18 @@ -int http_event_plugin_entry(const struct session *s, int what, struct packet *p, const char *payload, uint32_t len, void **pme) +int http_event_plugin_entry(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint32_t len, void **pme) { return 0; } int http_event_plugin_init() { - plugin_session_event_register(SESSION_TYPE_HTTP, http_event_plugin_entry, nullptr); + plugin_session_event_register("HTTP", http_event_plugin_entry, nullptr); return 0; +} + +void http_event_plugin_destroy() +{ + return ; } \ No newline at end of file diff --git a/sdk/example/plugin.inf b/sdk/example/plugin.inf deleted file mode 100644 index e69de29..0000000 diff --git a/sdk/example/plugins/custom_event_plugin/custom_event_plugin.inf b/sdk/example/plugins/custom_event_plugin/custom_event_plugin.inf new file mode 100644 index 0000000..78107e1 --- /dev/null +++ b/sdk/example/plugins/custom_event_plugin/custom_event_plugin.inf @@ -0,0 +1,13 @@ +[PLUGINFO] +INIT_FUNC=custom_plugin_init +DESTROY_FUNC=custom_plugin_destroy +LIBARY_PATH=./plugins/custom_event_plugin/custom_event_plugin.so + + +[SESSION_TYPE.TCP] +SESSION_EVENT_TYPE=ALL +SESSION_EVENT_CALLBACK=custom_plugin_entry + +[SESSION_TYPE.CUSTOM_A] +SESSION_EVENT_TYPE=ALL +SESSION_EVENT_CALLBACK=custom_event_plugin_entry \ No newline at end of file diff --git a/sdk/example/plugins/http_event_plugin/http_event_plugin.inf b/sdk/example/plugins/http_event_plugin/http_event_plugin.inf new file mode 100644 index 0000000..62b7a4a --- /dev/null +++ b/sdk/example/plugins/http_event_plugin/http_event_plugin.inf @@ -0,0 +1,9 @@ +[PLUGINFO] +INIT_FUNC=http_event_plugin_init +DESTROY_FUNC=http_event_plugin_destroy +LIBARY_PATH=./plugins/http_event_plugin/http_event_plugin.so + + +[SESSION_TYPE_HTTP] +SESSION_EVENT_TYPE=ALL +SESSION_EVENT_CALLBACK=http_event_plugin_entry \ No newline at end of file diff --git a/sdk/example/plugins/plugins.inf b/sdk/example/plugins/plugins.inf new file mode 100644 index 0000000..50e40af --- /dev/null +++ b/sdk/example/plugins/plugins.inf @@ -0,0 +1,2 @@ +./http_event_plugin/http_event_plugin.inf +./custom_event_plugin/custom_event_plugin.inf \ No newline at end of file diff --git a/sdk/include/event.h b/sdk/include/event.h new file mode 100644 index 0000000..9f374db --- /dev/null +++ b/sdk/include/event.h @@ -0,0 +1,18 @@ + +enum stellar_event_type +{ + STELLAR_SESSION_EVENT, +}; + +struct stellar_session_event_data; + +struct stellar_event +{ + stellar_event_type type; + union + { + struct stellar_session_event_data *session_event_data; + void *event_data; + }; +}; + diff --git a/sdk/include/http.h b/sdk/include/http.h index abef6c7..62b1fbd 100644 --- a/sdk/include/http.h +++ b/sdk/include/http.h @@ -3,4 +3,4 @@ #include "packet.h" #include "session.h" -int http_decoder(const struct session *s, int what, struct packet *p, const char *payload, uint32_t len, void **pme); +int http_decoder(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint32_t len, void **pme); diff --git a/sdk/include/packet.h b/sdk/include/packet.h index 5121457..5e4c102 100644 --- a/sdk/include/packet.h +++ b/sdk/include/packet.h @@ -1,4 +1,4 @@ #pragma once -struct packet; +struct stellar_packet; diff --git a/sdk/include/plugin.h b/sdk/include/plugin.h index 10cacd1..e8d0f98 100644 --- a/sdk/include/plugin.h +++ b/sdk/include/plugin.h @@ -3,7 +3,9 @@ #include "session.h" #include -int plugin_session_event_register(int session_type, fn_session_event_callback cb, void *entry_arg); -int plugin_session_timer_register(struct session *s, fn_session_timer cb, void *timer_arg, const struct timeval *timeout); +int plugin_session_event_register(const char *session_name, + fn_session_event_callback call_back, + const struct timeval *timeout); -int plugin_custom_session_event_register(const char *event_name, fn_session_event_callback cb, void *entry_arg); +void plugin_session_event_delete(struct stellar_event *ev, + struct stellar_session *s); \ No newline at end of file diff --git a/sdk/include/session.h b/sdk/include/session.h index 2671589..17bea1d 100644 --- a/sdk/include/session.h +++ b/sdk/include/session.h @@ -1,12 +1,12 @@ #pragma once #include "packet.h" +#include "event.h" #include -struct session; -struct session_event; +struct stellar_session; -enum session_type +enum stellar_session_type { SESSION_TYPE_CUSTOM, SESSION_TYPE_IP, @@ -23,7 +23,7 @@ enum session_type SESSION_TYPE_MAX, }; -enum session_event_type +enum stellar_session_event_type { SESSION_EVENT_OPENING=0x01, SESSION_EVENT_RAW_PKT=0x02, @@ -32,19 +32,13 @@ enum session_event_type SESSION_EVENT_CLOSING=0x10, }; +struct stellar_session_event_extras; -typedef int (*fn_session_event_callback)(const struct session *s, int what, struct packet *p, const char *payload, uint32_t len, void **pme); -typedef int (*fn_session_timer)(const struct session *s, void **pme); +typedef int (*fn_session_event_callback)(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint32_t len, void **pme); -void session_manager_trigger_event(struct session *s, session_event_type event, void *event_info); +void session_manager_trigger_event(struct stellar_session *s, + stellar_session_event_type type, + struct stellar_session_event_extras *info); - -struct session_event *session_manager_commit(struct packet *p); - -struct custom_session_event; - -struct custom_session_event *session_manager_custom_session_event_register(const char *event_name, int available_event); -struct session *session_manager_custom_session_derive(const struct session *cur_session, struct custom_session_event *event); - - -struct session_event *session_manager_fetch_event(); +struct stellar_session *session_manager_session_derive(const struct stellar_session *this_session, + const char *new_session_name); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index b607270..3031933 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,16 +3,16 @@ #include "plugin_manager.h" #include "http.h" -struct packet_io_loop_arg +struct stellar_event_base_loop_arg { struct packet_io_device *dev; int thread_id; }; -void packet_io_loop(struct packet_io_loop_arg *arg) +void stellar_event_base_loop(struct stellar_event_base_loop_arg *arg) { - struct packet *rx_pkt; - struct session_event *event; + struct stellar_packet *rx_pkt; + struct stellar_event *event; while(1) { int fetch_num = packet_io_rx(arg->dev, arg->thread_id, &rx_pkt, 1); @@ -50,7 +50,7 @@ int main(int argc, char ** argv) { //config_manager_init - session_manager_session_event_register(http_decoder, SESSION_TYPE_HTTP); + plugin_session_event_register("HTTP", http_decoder, nullptr); //packet_io_init //create_worker_thread diff --git a/src/packet_io/packet_io.cpp b/src/packet_io/packet_io.cpp index 43b9e7d..1b786c1 100644 --- a/src/packet_io/packet_io.cpp +++ b/src/packet_io/packet_io.cpp @@ -26,12 +26,12 @@ void packet_io_close_device(struct packet_io_device *dev) return; } -int packet_io_rx(struct packet_io_device *dev, uint32_t rx_queue_id, struct packet *p[], int nr_p) +int packet_io_rx(struct packet_io_device *dev, uint32_t rx_queue_id, struct stellar_packet *p[], int nr_p) { return 0; } -int packet_io_tx(struct packet_io_device *dev, uint32_t rx_queue_id, struct packet *p[], int nr_p) +int packet_io_tx(struct packet_io_device *dev, uint32_t rx_queue_id, struct stellar_packet *p[], int nr_p) { return 0; } \ No newline at end of file diff --git a/src/packet_io/packet_io.h b/src/packet_io/packet_io.h index 85cc16f..39414ca 100644 --- a/src/packet_io/packet_io.h +++ b/src/packet_io/packet_io.h @@ -19,5 +19,5 @@ void packet_io_destory(struct packet_io_instance *instance); struct packet_io_device *packet_io_open_device(struct packet_io_instance *instance, const char *dev_sym, uint32_t nr_rxqueue, uint32_t nr_txqueue); void packet_io_close_device(struct packet_io_device *dev); -int packet_io_rx(struct packet_io_device *dev, uint32_t rx_queue_id, struct packet *p[], int nr_p); -int packet_io_tx(struct packet_io_device *dev, uint32_t rx_queue_id, struct packet *p[], int nr_p); \ No newline at end of file +int packet_io_rx(struct packet_io_device *dev, uint32_t rx_queue_id, struct stellar_packet *p[], int nr_p); +int packet_io_tx(struct packet_io_device *dev, uint32_t rx_queue_id, struct stellar_packet *p[], int nr_p); \ No newline at end of file diff --git a/src/packet_io/test/gtest_packet_io.cpp b/src/packet_io/test/gtest_packet_io.cpp index ff0ea7f..8618e2e 100644 --- a/src/packet_io/test/gtest_packet_io.cpp +++ b/src/packet_io/test/gtest_packet_io.cpp @@ -1,6 +1,6 @@ #include -#include "packet_io.h" +#include "../packet_io.h" TEST(PACKET_IO_Test, packet_io_create) { EXPECT_EQ(packet_io_create(PACKET_IO_PCAP, "TEST"), nullptr); diff --git a/src/plugin_manager/CMakeLists.txt b/src/plugin_manager/CMakeLists.txt index 212664d..5850d1f 100644 --- a/src/plugin_manager/CMakeLists.txt +++ b/src/plugin_manager/CMakeLists.txt @@ -1,4 +1,4 @@ -include_directories(${CMAKE_SOURCE_DIR}/session_manager/) add_library(plugin_manager plugin_manager.cpp -) \ No newline at end of file +) +target_include_directories(plugin_manager PUBLIC ${CMAKE_SOURCE_DIR}) \ No newline at end of file diff --git a/src/plugin_manager/plugin_manager.cpp b/src/plugin_manager/plugin_manager.cpp index c5b7fe1..4fe54ce 100644 --- a/src/plugin_manager/plugin_manager.cpp +++ b/src/plugin_manager/plugin_manager.cpp @@ -1,13 +1,16 @@ -#include "plugin.h" +#include "deps/uthash/uthash.h" + +#include "sdk/include/session.h" + + #include "session_manager.h" -#include "utils.h" - +#if 0 struct session_event_callback { const char *session_name; fn_session_event_callback callback; - TAILQ_ENTRY(session_event_callback) session_event_cb_tq_entries; + TAILQ_ENTRY(session_event_callback) entries; UT_hash_handle hh; }; @@ -26,9 +29,18 @@ struct plugin_manager }; struct plugin_manager g_plug_mgr; +#endif -void plugin_manager_dispatch(struct session_event *event) +int plugin_session_event_register(const char *session_name, + fn_session_event_callback call_back, + const struct timeval *timeout) { + return 0; +} + +void plugin_manager_dispatch(struct stellar_event *event) +{ +#if 0 struct session *s = container_of(event, struct session, cur_ev); struct session_event_callback_map *t_map; struct session_event_callback *session_ev_cb; @@ -55,6 +67,6 @@ void plugin_manager_dispatch(struct session_event *event) default: break; } - +#endif return; } \ No newline at end of file diff --git a/src/plugin_manager/plugin_manager.h b/src/plugin_manager/plugin_manager.h index 6159957..d2b9672 100644 --- a/src/plugin_manager/plugin_manager.h +++ b/src/plugin_manager/plugin_manager.h @@ -1,6 +1,25 @@ #pragma once -#include "plugin.h" +#include -void plugin_manager_dispatch(struct session_event *event); \ No newline at end of file +#include "sdk/include/session.h" +#include "sdk/include/plugin.h" + +struct stellar_plugin_ctx +{ + void *call_back_arg; + const struct timeval timeout; + fn_session_event_callback call_back; + TAILQ_ENTRY(stellar_plugin_ctx) tqe; +}; + +TAILQ_HEAD(stellar_plugin_ctx_list, stellar_plugin_ctx); + +struct stellar_plugin_data +{ + stellar_plugin_ctx_list plugin_ctx_list; +}; + + +void plugin_manager_dispatch(struct stellar_event *ev); \ No newline at end of file diff --git a/src/protocol_decoder/http/CMakeLists.txt b/src/protocol_decoder/http/CMakeLists.txt index 50ed35d..dd8482f 100644 --- a/src/protocol_decoder/http/CMakeLists.txt +++ b/src/protocol_decoder/http/CMakeLists.txt @@ -2,4 +2,5 @@ add_library(http http.cpp -) \ No newline at end of file +) +target_include_directories(http PUBLIC ${CMAKE_SOURCE_DIR}) \ No newline at end of file diff --git a/src/protocol_decoder/http/http.cpp b/src/protocol_decoder/http/http.cpp index e8b19b5..13fd4d6 100644 --- a/src/protocol_decoder/http/http.cpp +++ b/src/protocol_decoder/http/http.cpp @@ -1,16 +1,16 @@ #include "http.h" #include "session_manager.h" -int http_decoder(const struct session *s, int what, struct packet *p, const char *payload, uint32_t len, void **pme) +int http_decoder(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint32_t len, void **pme) { - void *info; - struct session *new_session=session_manager_session_derive(s, SESSION_TYPE_HTTP); + struct stellar_session_event_extras *info; + struct stellar_session *new_session=session_manager_session_derive(s, "HTTP"); session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info); session_manager_trigger_event(new_session, SESSION_EVENT_META, info); return 0; } -int http_peek(const struct session *s, const char *payload, uint32_t len) +int http_peek(const struct stellar_session *s, const char *payload, uint32_t len) { return 0; } \ No newline at end of file diff --git a/src/session_manager/CMakeLists.txt b/src/session_manager/CMakeLists.txt index 23fe595..b78722f 100644 --- a/src/session_manager/CMakeLists.txt +++ b/src/session_manager/CMakeLists.txt @@ -1,4 +1,4 @@ - add_library(session_manager session_manager.cpp -) \ No newline at end of file +) +target_include_directories(session_manager PUBLIC ${CMAKE_SOURCE_DIR}) \ No newline at end of file diff --git a/src/session_manager/session_manager.cpp b/src/session_manager/session_manager.cpp index a5592c8..10d41bd 100644 --- a/src/session_manager/session_manager.cpp +++ b/src/session_manager/session_manager.cpp @@ -2,42 +2,29 @@ struct session_manager { - struct session **tcp_table, **udp_table; + struct stellar_session **tcp_table, **udp_table; }; -void session_manager_session_event_register(fn_session_event_callback decoder, session_type type) +void session_manager_trigger_event(struct stellar_session *s, + stellar_session_event_type type, + struct stellar_session_event_extras *info) { return; } -struct session *session_manager_custom_session_derive(const struct session *cur_session, struct custom_session_event *event) +struct stellar_session *session_manager_session_derive(const struct stellar_session *this_session, + const char *new_session_name) { return nullptr; } -void session_manager_trigger_event(struct session *s, session_event_type event, void *event_info) -{ - return; -} - - -struct session_event *session_manager_commit(struct packet *p) +struct stellar_event *session_manager_commit(struct stellar_packet *p) { return nullptr; }; -struct custom_session_event *session_manager_custom_session_event_register(const char *event_name, int available_event) -{ - return nullptr; -} - -struct session *session_manager_session_derive(const struct session *cur_session, session_type type) -{ - return nullptr; -} - -struct session_event *session_manager_fetch_event() +struct stellar_event *session_manager_fetch_event() { return nullptr; } diff --git a/src/session_manager/session_manager.h b/src/session_manager/session_manager.h index 17f2e0d..3bac9f0 100644 --- a/src/session_manager/session_manager.h +++ b/src/session_manager/session_manager.h @@ -1,47 +1,24 @@ #pragma once -#include "session.h" -#include "uthash/uthash.h" +#include "sdk/include/session.h" -#include /* TAILQ(3) */ +struct stellar_plugin_data; -struct session_data +struct stellar_session_event_data { - /* data */ + struct stellar_session *s; + stellar_session_event_type type; + long last_active; + struct stellar_plugin_data *plugin_data; }; -enum session_state +struct stellar_event *session_manager_commit(struct stellar_packet *pkt); +struct stellar_event *session_manager_fetch_event(); + +struct stellar_session { - SESSION_STATE_OPENING, - SESSION_STATE_ACTIVE, - SESSION_STATE_CLOSING, - SESSION_STATE_CLOSED + stellar_session_type type; + const char *name; + void *addr; + void *data; }; - -struct session_event -{ - enum session_state state; - void *cb_pme; - fn_session_event_callback callback; - TAILQ_ENTRY(session_event) session_event_tq_entries; -}; - -TAILQ_HEAD(session_event_list, session_event); - -struct session -{ - const char *name; - enum session_type type; - enum session_state state; - struct session_data data; - struct session_event cur_ev; - struct session_event_list ev_list; - TAILQ_ENTRY(session) session_tq_entries; - UT_hash_handle hh; -}; - - - - -void session_manager_session_event_register(fn_session_event_callback session_ev_cb, session_type type); -struct session *session_manager_session_derive(const struct session *cur_session, session_type type); From ba866fef10cdbfd94618e4364756ac76f91fe43f Mon Sep 17 00:00:00 2001 From: yangwei Date: Sun, 24 Jul 2022 23:22:52 +0800 Subject: [PATCH 02/17] =?UTF-8?q?=E2=9C=A8=20feat(session=5Fmanager):=20in?= =?UTF-8?q?it=20return=20with=20handle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sdk/include/event.h | 6 ------ src/main.cpp | 9 +++++---- src/session_manager/session_manager.cpp | 10 ++++++++-- src/session_manager/session_manager.h | 7 +++++-- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/sdk/include/event.h b/sdk/include/event.h index 9f374db..0653f69 100644 --- a/sdk/include/event.h +++ b/sdk/include/event.h @@ -1,14 +1,8 @@ -enum stellar_event_type -{ - STELLAR_SESSION_EVENT, -}; - struct stellar_session_event_data; struct stellar_event { - stellar_event_type type; union { struct stellar_session_event_data *session_event_data; diff --git a/src/main.cpp b/src/main.cpp index 3031933..184abbe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,7 @@ struct stellar_event_base_loop_arg { struct packet_io_device *dev; + struct session_manager_handle *s_mgr; int thread_id; }; @@ -18,11 +19,11 @@ void stellar_event_base_loop(struct stellar_event_base_loop_arg *arg) int fetch_num = packet_io_rx(arg->dev, arg->thread_id, &rx_pkt, 1); if(fetch_num > 0) { - event = session_manager_commit(rx_pkt); + event = session_manager_commit(arg->s_mgr, rx_pkt); while(event) { plugin_manager_dispatch(event); - event = session_manager_fetch_event(); + event = session_manager_fetch_event(arg->s_mgr); } //clean session_manager event queue @@ -48,8 +49,8 @@ struct packet_io_device *packet_io_init(int worker_thread_num, const char *insta int main(int argc, char ** argv) { - //config_manager_init - + //config_init + struct session_manager_handle *h = session_manager_init(); plugin_session_event_register("HTTP", http_decoder, nullptr); //packet_io_init diff --git a/src/session_manager/session_manager.cpp b/src/session_manager/session_manager.cpp index 10d41bd..b374920 100644 --- a/src/session_manager/session_manager.cpp +++ b/src/session_manager/session_manager.cpp @@ -5,6 +5,12 @@ struct session_manager struct stellar_session **tcp_table, **udp_table; }; +struct session_manager_handle *session_manager_init() +{ + return nullptr; +} + + void session_manager_trigger_event(struct stellar_session *s, stellar_session_event_type type, struct stellar_session_event_extras *info) @@ -18,13 +24,13 @@ struct stellar_session *session_manager_session_derive(const struct stellar_sess return nullptr; } -struct stellar_event *session_manager_commit(struct stellar_packet *p) +struct stellar_event *session_manager_commit(struct session_manager_handle *h, struct stellar_packet *p) { return nullptr; }; -struct stellar_event *session_manager_fetch_event() +struct stellar_event *session_manager_fetch_event(struct session_manager_handle *h) { return nullptr; } diff --git a/src/session_manager/session_manager.h b/src/session_manager/session_manager.h index 3bac9f0..93c13da 100644 --- a/src/session_manager/session_manager.h +++ b/src/session_manager/session_manager.h @@ -12,8 +12,11 @@ struct stellar_session_event_data struct stellar_plugin_data *plugin_data; }; -struct stellar_event *session_manager_commit(struct stellar_packet *pkt); -struct stellar_event *session_manager_fetch_event(); +struct session_manager_handle; +struct session_manager_handle *session_manager_init(); + +struct stellar_event *session_manager_commit(struct session_manager_handle *s_mgr, struct stellar_packet *pkt); +struct stellar_event *session_manager_fetch_event(struct session_manager_handle *s_mgr); struct stellar_session { From 7a3bc7a888d878c3fa06fc26460fced84d369e26 Mon Sep 17 00:00:00 2001 From: yangwei Date: Sun, 24 Jul 2022 23:59:00 +0800 Subject: [PATCH 03/17] =?UTF-8?q?=F0=9F=A6=84=20refactor(plugin=20manager)?= =?UTF-8?q?:=20update=20interface=20definition?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sdk/example/custom_event_plugin.cpp | 4 +-- sdk/example/http_event_plugin.cpp | 2 +- sdk/include/plugin.h | 7 +--- sdk/include/session.h | 2 +- src/CMakeLists.txt | 2 +- src/main.cpp | 47 +++++++++++++++++-------- src/plugin_manager/plugin_manager.cpp | 16 +++++++-- src/plugin_manager/plugin_manager.h | 12 ++++++- src/session_manager/session_manager.cpp | 2 +- src/session_manager/session_manager.h | 4 +-- 10 files changed, 66 insertions(+), 32 deletions(-) diff --git a/sdk/example/custom_event_plugin.cpp b/sdk/example/custom_event_plugin.cpp index 7bf014e..d5b95e9 100644 --- a/sdk/example/custom_event_plugin.cpp +++ b/sdk/example/custom_event_plugin.cpp @@ -27,8 +27,8 @@ int custom_event_plugin_entry(const struct stellar_session *s, int what, struct int custom_plugin_init() { - plugin_session_event_register("TCP", custom_plugin_entry, nullptr); - plugin_session_event_register("CUSTOM_A", custom_event_plugin_entry, nullptr); + //plugin_manager_event_register("TCP", custom_plugin_entry, nullptr); + //plugin_manager_event_register("CUSTOM_A", custom_event_plugin_entry, nullptr); return 0; } diff --git a/sdk/example/http_event_plugin.cpp b/sdk/example/http_event_plugin.cpp index e8ac0bb..68dcf06 100644 --- a/sdk/example/http_event_plugin.cpp +++ b/sdk/example/http_event_plugin.cpp @@ -12,7 +12,7 @@ int http_event_plugin_entry(const struct stellar_session *s, int what, struct st int http_event_plugin_init() { - plugin_session_event_register("HTTP", http_event_plugin_entry, nullptr); + //plugin_manager_event_register("HTTP", http_event_plugin_entry, nullptr); return 0; } diff --git a/sdk/include/plugin.h b/sdk/include/plugin.h index e8d0f98..11d55cc 100644 --- a/sdk/include/plugin.h +++ b/sdk/include/plugin.h @@ -1,11 +1,6 @@ #pragma once #include "session.h" -#include -int plugin_session_event_register(const char *session_name, - fn_session_event_callback call_back, - const struct timeval *timeout); - -void plugin_session_event_delete(struct stellar_event *ev, +void plugin_remove_from_session_event(struct stellar_event *ev, struct stellar_session *s); \ No newline at end of file diff --git a/sdk/include/session.h b/sdk/include/session.h index 17bea1d..ea3af84 100644 --- a/sdk/include/session.h +++ b/sdk/include/session.h @@ -41,4 +41,4 @@ void session_manager_trigger_event(struct stellar_session *s, struct stellar_session_event_extras *info); struct stellar_session *session_manager_session_derive(const struct stellar_session *this_session, - const char *new_session_name); \ No newline at end of file + const char *new_session_type_name); \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2742da1..ede3dd3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -16,4 +16,4 @@ add_executable(stellar main.cpp ) -target_link_libraries(stellar packet_io plugin_manager session_manager http) \ No newline at end of file +target_link_libraries(stellar pthread packet_io plugin_manager session_manager http) \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 184abbe..e09573f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,6 @@ +#include +#include + #include "packet_io.h" #include "session_manager.h" #include "plugin_manager.h" @@ -6,28 +9,30 @@ struct stellar_event_base_loop_arg { struct packet_io_device *dev; - struct session_manager_handle *s_mgr; - int thread_id; + struct session_manager_handle *session_mgr; + struct plugin_manager_handle *plug_mgr; + int tid; }; -void stellar_event_base_loop(struct stellar_event_base_loop_arg *arg) +void *stellar_event_base_loop(void *arg) { struct stellar_packet *rx_pkt; struct stellar_event *event; + struct stellar_event_base_loop_arg *thread_arg = (struct stellar_event_base_loop_arg *)arg; while(1) { - int fetch_num = packet_io_rx(arg->dev, arg->thread_id, &rx_pkt, 1); + int fetch_num = packet_io_rx(thread_arg->dev, thread_arg->tid, &rx_pkt, 1); if(fetch_num > 0) { - event = session_manager_commit(arg->s_mgr, rx_pkt); + event = session_manager_commit(thread_arg->session_mgr, rx_pkt); while(event) { - plugin_manager_dispatch(event); - event = session_manager_fetch_event(arg->s_mgr); + plugin_manager_dispatch(thread_arg->plug_mgr ,event); + event = session_manager_fetch_event(thread_arg->session_mgr); } //clean session_manager event queue - packet_io_tx(arg->dev, arg->thread_id, &rx_pkt, 1); + packet_io_tx(thread_arg->dev, thread_arg->tid, &rx_pkt, 1); } else { @@ -36,7 +41,7 @@ void stellar_event_base_loop(struct stellar_event_base_loop_arg *arg) //dispatch to trigger polling event } } - return; + return nullptr; } @@ -49,13 +54,27 @@ struct packet_io_device *packet_io_init(int worker_thread_num, const char *insta int main(int argc, char ** argv) { - //config_init - struct session_manager_handle *h = session_manager_init(); - plugin_session_event_register("HTTP", http_decoder, nullptr); + //manager_init + struct session_manager_handle *session_mgr = session_manager_init(); + struct plugin_manager_handle *plug_mgr = plugin_manager_init(); + + //register build-in plugin + plugin_manager_event_register(plug_mgr, "HTTP", http_decoder, nullptr); + // load external plugins + plugin_manager_load("./plugins.inf"); + //packet_io_init - + struct packet_io_device *dev = packet_io_init(1, "stellar", "cap0"); //create_worker_thread - + stellar_event_base_loop_arg arg = {dev, session_mgr, plug_mgr, 0}; + pthread_t worker_pid; + pthread_create(&worker_pid, NULL, stellar_event_base_loop, (void *)&arg); //main_loop + while (1) + { + /* main loop code */ + usleep(1); + } + return 0; } diff --git a/src/plugin_manager/plugin_manager.cpp b/src/plugin_manager/plugin_manager.cpp index 4fe54ce..267d75c 100644 --- a/src/plugin_manager/plugin_manager.cpp +++ b/src/plugin_manager/plugin_manager.cpp @@ -28,17 +28,27 @@ struct plugin_manager struct session_event_callback_map *session_ev_cb_map; }; -struct plugin_manager g_plug_mgr; #endif -int plugin_session_event_register(const char *session_name, +struct plugin_manager_handle *plugin_manager_init() +{ + return nullptr; +} + +int plugin_manager_load(const char *plugin_conf_path) +{ + return 0; +} + +int plugin_manager_event_register(struct plugin_manager_handle *h, + const char *session_type_name, fn_session_event_callback call_back, const struct timeval *timeout) { return 0; } -void plugin_manager_dispatch(struct stellar_event *event) +void plugin_manager_dispatch(struct plugin_manager_handle *h, struct stellar_event *event) { #if 0 struct session *s = container_of(event, struct session, cur_ev); diff --git a/src/plugin_manager/plugin_manager.h b/src/plugin_manager/plugin_manager.h index d2b9672..65961bb 100644 --- a/src/plugin_manager/plugin_manager.h +++ b/src/plugin_manager/plugin_manager.h @@ -2,6 +2,7 @@ #pragma once #include +#include #include "sdk/include/session.h" #include "sdk/include/plugin.h" @@ -21,5 +22,14 @@ struct stellar_plugin_data stellar_plugin_ctx_list plugin_ctx_list; }; +struct plugin_manager_handle; +struct plugin_manager_handle *plugin_manager_init(); -void plugin_manager_dispatch(struct stellar_event *ev); \ No newline at end of file +int plugin_manager_event_register(struct plugin_manager_handle *h, + const char *session_type_name, + fn_session_event_callback call_back, + const struct timeval *timeout); + +int plugin_manager_load(const char *plugin_conf_path); + +void plugin_manager_dispatch(struct plugin_manager_handle *h, struct stellar_event *ev); \ No newline at end of file diff --git a/src/session_manager/session_manager.cpp b/src/session_manager/session_manager.cpp index b374920..ec8521e 100644 --- a/src/session_manager/session_manager.cpp +++ b/src/session_manager/session_manager.cpp @@ -19,7 +19,7 @@ void session_manager_trigger_event(struct stellar_session *s, } struct stellar_session *session_manager_session_derive(const struct stellar_session *this_session, - const char *new_session_name) + const char *new_session_type_name) { return nullptr; } diff --git a/src/session_manager/session_manager.h b/src/session_manager/session_manager.h index 93c13da..d18def7 100644 --- a/src/session_manager/session_manager.h +++ b/src/session_manager/session_manager.h @@ -15,8 +15,8 @@ struct stellar_session_event_data struct session_manager_handle; struct session_manager_handle *session_manager_init(); -struct stellar_event *session_manager_commit(struct session_manager_handle *s_mgr, struct stellar_packet *pkt); -struct stellar_event *session_manager_fetch_event(struct session_manager_handle *s_mgr); +struct stellar_event *session_manager_commit(struct session_manager_handle *session_mgr, struct stellar_packet *pkt); +struct stellar_event *session_manager_fetch_event(struct session_manager_handle *session_mgr); struct stellar_session { From 717f52e4c1f96f49475722f4dd747ad532d7c034 Mon Sep 17 00:00:00 2001 From: zhengchao Date: Wed, 27 Jul 2022 11:14:49 +0800 Subject: [PATCH 04/17] Add readme.md --- docs/imgs/stellar-high-level-design.png | Bin 0 -> 16669 bytes readme.md | 16 ++++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 docs/imgs/stellar-high-level-design.png create mode 100644 readme.md diff --git a/docs/imgs/stellar-high-level-design.png b/docs/imgs/stellar-high-level-design.png new file mode 100644 index 0000000000000000000000000000000000000000..b9815d9c7454faca326672162b8068e3a0a6576d GIT binary patch literal 16669 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∓pkb;_>p4VgI|FEPD`psWNIb-?WT=mI54ueUUJ(@On=f zMpxK1^fo}>UDThZc&hat8<7q;Z-+EVE;ioTD!6!@u3b6v%SpP}_pPpzS)R`JTg%MA z?n|<{n`!t*{YvmzJ}@SFCTbbiol3Lv{=SF_(3eUmKS?kE&;Q9_CriFcvqkEqq4awD zckCpyN2{t8sJNZ4#PSdI6EZWY%yuRVXXiJl)|c4Lml+nXUyEfiAY-_Hl=MDE8~5I& zJQu%#)~f>JZJqX))D@u%xohjLc8|0-6$P`@Zl-L`oioYsac}U|PE8$_odanRG2-7E z(Hk<^Ew`Z$4<&GMffzSF@O;Po-lKxJbD3z$C9TrX{ov7#iE2gnm)jK^vL>!*TW3Ly zt&8>{FfS|8!9J~$gyEAGi=Jt+Th?X!ePr%Q8v2m=jELq7An z@yB7zML0Z3RZh~^iV_ja6p5C6&-_jt<&R%Sxqj4N-jE6`75$f}_)`1vcHWNy^Tg)~ zn{S)}BLXr2K)5JkLoXlY-CqtZuKNaqlvrFRNm)IUjetykodDS|Hn$*f70$h5~F(dcpL_?O2E%S z;x3%jeLI*3$Q*n9hYS4MKvuDy3Ua)me-%Oiy9ks_c&0{kHA<1t5zDl8fYOu)L|5}i z&^Vb-@rR%a`XqPU{=0e^NCQ=#1SRVMI}Y>k*aW*1fALN%#5R1Urltn`4nPnn&H^6C zfSOAcK(+wscYyI@fNbUE#}$e@bJ_3=QN!|z<_eYa+?$4#-{(KzC9fguWz~H8DvaCR z-Thq?7BV?({|*36v;5OHMMU@ByO|3Df=jnS^E0U3CW&4Xh43m+NJXqtf1&`UNq?~& z_5Ryu_}&61oHv}ZBXO>Klju*Y<8T4|kzW!ZJSRrx+FKWb%@;CO_U2hDf>3x^_(O`|f=3qMx%$ZIu&sux^ z2Aq|X!8(@kK=p%g2KlN}(2dzNo> literal 0 HcmV?d00001 diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..49278c0 --- /dev/null +++ b/readme.md @@ -0,0 +1,16 @@ +# Stellar: A stateful network functions development platform + +A stateful network function could be a firewall, a load balancer, or an IDS. + +## Architecture + +The stellar components are: + +- **Packet IO** built an abstraction of network IO devices. +- **Session Manager** has a hash table for tracking sessions. The caller feeds packets to the session manager and may return triggered session events. +- **Plugin Manager** loads C/Lua plugins and manages per-plugin, per-session context. When the caller feeds an event to the plugin manager, it invokes plugin callbacks. +- **Protocol Decoders** are libraries that parse and extract information from the packet payload. +- **Active Queue Management** is queue management algorithm libraries that schedule packets by buffering, forwarding, marking, or dropping. A plugin creates a queue instance and enqueues packets as its needs. + - Question: Who consumes the dequeue events? + +![stellar-high-level-design](./docs/imgs/stellar-high-level-design.png) \ No newline at end of file From 4735a8073a00604636ac8bc4bec8e0667783cec7 Mon Sep 17 00:00:00 2001 From: yangwei Date: Wed, 27 Jul 2022 14:11:29 +0800 Subject: [PATCH 05/17] =?UTF-8?q?=F0=9F=A6=84=20refactor(plugin=20manager)?= =?UTF-8?q?:=20Updating=20interface=20definitions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sdk/include/session.h | 11 ++++++----- src/main.cpp | 8 ++++---- src/plugin_manager/plugin_manager.cpp | 25 ++++++++++++++++++++----- src/plugin_manager/plugin_manager.h | 25 +++++-------------------- src/session_manager/session_manager.cpp | 6 +++--- src/session_manager/session_manager.h | 8 ++++---- 6 files changed, 42 insertions(+), 41 deletions(-) diff --git a/sdk/include/session.h b/sdk/include/session.h index ea3af84..e2ed19e 100644 --- a/sdk/include/session.h +++ b/sdk/include/session.h @@ -25,11 +25,12 @@ enum stellar_session_type enum stellar_session_event_type { - SESSION_EVENT_OPENING=0x01, - SESSION_EVENT_RAW_PKT=0x02, - SESSION_EVENT_ORDERED_PKT=0x04, - SESSION_EVENT_META=0x08, - SESSION_EVENT_CLOSING=0x10, + SESSION_EVENT_OPENING = 1<<1, + SESSION_EVENT_RAW_PKT = 1<<2, + SESSION_EVENT_ORDERED_PKT = 1<<3, + SESSION_EVENT_META = 1<<4, + SESSION_EVENT_TIMEOUT = 1<<5, + SESSION_EVENT_CLOSING = 1<<6, }; struct stellar_session_event_extras; diff --git a/src/main.cpp b/src/main.cpp index e09573f..891300a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,8 +9,8 @@ struct stellar_event_base_loop_arg { struct packet_io_device *dev; - struct session_manager_handle *session_mgr; - struct plugin_manager_handle *plug_mgr; + struct session_manager *session_mgr; + struct plugin_manager *plug_mgr; int tid; }; @@ -55,8 +55,8 @@ struct packet_io_device *packet_io_init(int worker_thread_num, const char *insta int main(int argc, char ** argv) { //manager_init - struct session_manager_handle *session_mgr = session_manager_init(); - struct plugin_manager_handle *plug_mgr = plugin_manager_init(); + struct session_manager *session_mgr = session_manager_init(); + struct plugin_manager *plug_mgr = plugin_manager_init(); //register build-in plugin plugin_manager_event_register(plug_mgr, "HTTP", http_decoder, nullptr); diff --git a/src/plugin_manager/plugin_manager.cpp b/src/plugin_manager/plugin_manager.cpp index 267d75c..4fba201 100644 --- a/src/plugin_manager/plugin_manager.cpp +++ b/src/plugin_manager/plugin_manager.cpp @@ -1,8 +1,8 @@ +#include +#include + #include "deps/uthash/uthash.h" - #include "sdk/include/session.h" - - #include "session_manager.h" #if 0 @@ -30,6 +30,21 @@ struct plugin_manager #endif +struct stellar_plugin_ctx +{ + void *call_back_arg; + const struct timeval timeout; + fn_session_event_callback call_back; + TAILQ_ENTRY(stellar_plugin_ctx) tqe; +}; + +TAILQ_HEAD(stellar_plugin_ctx_list, stellar_plugin_ctx); + +struct stellar_plugin_data +{ + stellar_plugin_ctx_list plugin_ctx_list; +}; + struct plugin_manager_handle *plugin_manager_init() { return nullptr; @@ -40,7 +55,7 @@ int plugin_manager_load(const char *plugin_conf_path) return 0; } -int plugin_manager_event_register(struct plugin_manager_handle *h, +int plugin_manager_event_register(struct plugin_manager *h, const char *session_type_name, fn_session_event_callback call_back, const struct timeval *timeout) @@ -48,7 +63,7 @@ int plugin_manager_event_register(struct plugin_manager_handle *h, return 0; } -void plugin_manager_dispatch(struct plugin_manager_handle *h, struct stellar_event *event) +void plugin_manager_dispatch(struct plugin_manager *h, struct stellar_event *event) { #if 0 struct session *s = container_of(event, struct session, cur_ev); diff --git a/src/plugin_manager/plugin_manager.h b/src/plugin_manager/plugin_manager.h index 65961bb..cfe2905 100644 --- a/src/plugin_manager/plugin_manager.h +++ b/src/plugin_manager/plugin_manager.h @@ -1,35 +1,20 @@ #pragma once -#include -#include + #include "sdk/include/session.h" #include "sdk/include/plugin.h" -struct stellar_plugin_ctx -{ - void *call_back_arg; - const struct timeval timeout; - fn_session_event_callback call_back; - TAILQ_ENTRY(stellar_plugin_ctx) tqe; -}; -TAILQ_HEAD(stellar_plugin_ctx_list, stellar_plugin_ctx); +struct plugin_manager; +struct plugin_manager *plugin_manager_init(); -struct stellar_plugin_data -{ - stellar_plugin_ctx_list plugin_ctx_list; -}; - -struct plugin_manager_handle; -struct plugin_manager_handle *plugin_manager_init(); - -int plugin_manager_event_register(struct plugin_manager_handle *h, +int plugin_manager_event_register(struct plugin_manager *h, const char *session_type_name, fn_session_event_callback call_back, const struct timeval *timeout); int plugin_manager_load(const char *plugin_conf_path); -void plugin_manager_dispatch(struct plugin_manager_handle *h, struct stellar_event *ev); \ No newline at end of file +void plugin_manager_dispatch(struct plugin_manager *h, struct stellar_event *ev); \ No newline at end of file diff --git a/src/session_manager/session_manager.cpp b/src/session_manager/session_manager.cpp index ec8521e..ff10c3e 100644 --- a/src/session_manager/session_manager.cpp +++ b/src/session_manager/session_manager.cpp @@ -5,7 +5,7 @@ struct session_manager struct stellar_session **tcp_table, **udp_table; }; -struct session_manager_handle *session_manager_init() +struct session_manager *session_manager_init() { return nullptr; } @@ -24,13 +24,13 @@ struct stellar_session *session_manager_session_derive(const struct stellar_sess return nullptr; } -struct stellar_event *session_manager_commit(struct session_manager_handle *h, struct stellar_packet *p) +struct stellar_event *session_manager_commit(struct session_manager *h, struct stellar_packet *p) { return nullptr; }; -struct stellar_event *session_manager_fetch_event(struct session_manager_handle *h) +struct stellar_event *session_manager_fetch_event(struct session_manager *h) { return nullptr; } diff --git a/src/session_manager/session_manager.h b/src/session_manager/session_manager.h index d18def7..047d8c5 100644 --- a/src/session_manager/session_manager.h +++ b/src/session_manager/session_manager.h @@ -12,11 +12,11 @@ struct stellar_session_event_data struct stellar_plugin_data *plugin_data; }; -struct session_manager_handle; -struct session_manager_handle *session_manager_init(); +struct session_manager; +struct session_manager *session_manager_init(); -struct stellar_event *session_manager_commit(struct session_manager_handle *session_mgr, struct stellar_packet *pkt); -struct stellar_event *session_manager_fetch_event(struct session_manager_handle *session_mgr); +struct stellar_event *session_manager_commit(struct session_manager *session_mgr, struct stellar_packet *pkt); +struct stellar_event *session_manager_fetch_event(struct session_manager *session_mgr); struct stellar_session { From dd54381cb00def84782511c4feb21ad570b4cb1a Mon Sep 17 00:00:00 2001 From: yangwei Date: Wed, 27 Jul 2022 14:48:50 +0800 Subject: [PATCH 06/17] =?UTF-8?q?=F0=9F=93=83=20docs(readme):=20update=20s?= =?UTF-8?q?tellar-high-level-design.svg?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/imgs/stellar-high-level-design.png | Bin 16669 -> 0 bytes docs/imgs/stellar-high-level-design.svg | 4 ++++ readme.md | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) delete mode 100644 docs/imgs/stellar-high-level-design.png create mode 100644 docs/imgs/stellar-high-level-design.svg diff --git a/docs/imgs/stellar-high-level-design.png b/docs/imgs/stellar-high-level-design.png deleted file mode 100644 index b9815d9c7454faca326672162b8068e3a0a6576d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16669 zcmeIZXIN8Pw>BI_z=9xxg0zST2uKl>DnzAA6A@79(xe5XOG!|X-cjjA5Ky{w2t}HJ z^j<;;y%PvEKnU*&8~1iU=RM!`e&yf!esFQE%&b|*9OJ(4G2;J7RsQ^0hO;0L=)9tW z%wrJf6gLQT!v6G0;FHRp>}25I35Un>4?wvcObfunDf9a(_d%fVq37^Mq`>nTJB4Qs zAkale;@^oeGq5WNbgw{B=Kd2`{S{P>Gm~QM&sX=~k?NZjn0uTd*`d9XDtUhHVo(6I zp4FY~H2uw&s5kfDk&#erGu@##>`H#~=H5FpQijv?r%wZaZE}^=F}pwI&Ezz>Qn^zJ zkS;yyD+ZeOrd4V>mU;*0H1A4D!N~HX5eSJxGsc}2qzHuJ$4S`1miD0;iPx)!(1!%V zPEC#H{x-9hX{FoN;?iIq;Xrt@-j}NK{F|BR(g`peKY_Ld3B1Lr`}>1^Y(cIT9NKLI zl0dWU$IA03ym0DgoT&0|Op@E#H&B7t{lFg|?SKNW!9bkyc+ex@wg0KjEsINu$d&&7 z{_TO%uwulpuAU9lx!OP04_tg_rzv4)73&8EJ-h(n7Q2sPd&8Qq<`$;*VoSXej`!0Z zQgXq$Hf^ZhPn%KNOB3sk6koJ*G|OxZozHS9fit)(d4A+1lx+}lCMnQ0$J<`1fGSLy zkA!uqWxC=BPW9^G-~)1x+clrpXP6QjuYBSs&!UD1=0zPmPh0^jn19P#ZveZD)o8{~ z>__2FB%i{jMi?(8l3Y&;PXp&Z!&;t$pG3@*ZYS0H?+iIl(9FiW?ApibWj&LEHJ`(+ z7mr!i@Ht@L?W_hqZ%U$>B=@=qSW`%rJ7t8&^YX(d&f|01l_PaY?_s;s&F-L1llni{~jiMGK1)8CA1iOyDLGQGFY+yLB`AN%J!- zaLHgJt8&9wOD{yUqigN0KJCskAxygGFW=hqZ@X#XqgK|tdV|X6R;^+X-q{UoM5o{q zcr^N{PPyEeL=4FOR{UxPd2hUlF|W9HS`)Hq+o<$AHKlOh9T8DCj`|O{>)k)rf`oLI z$|o8;f3H{u-4=50THJ3!Jx}wN{hQP$PBe1 z@JsrO6wQ1hMX1@ILzGY_Pp2>@mB3Joc`OuVJEWzNbhETF zyt2Zu`CdqVB$H=oM86M_`P!Wo2D)({|8?50V(t;jH#i+q`A~|(7ppF0VcwQ4Q0`NG zJ=9ZrYi^J+@sXRP$4|lM{noX3$AZ&tvcbr3_+Tvlym=HUW_p`g{H{5d1!gSK7D1))q z?Chp24L7$3+fVhOgP-}~>B%nkR##%4eU80^Uy&zlslTf%YFc!U4DLx-8T!nI&>xMt za($_SZ!16QbzOqttt)e6ln1@YD?%j8#$z?o1_b5%tJ4F5f`xU}za2#iM(oM#Mzd$- zUiI5h#XFMvyw*xtb9C+50(%p5fwSA>;JtcY@`)H9cJFL9&+Twj#lDMc_2_86h`aM( zIcl#yM)-7g9R`;9yrAu8=5RER0s$yHQmItjmqA=yE0#!2^TuMl#)B+D= zqgX-sp$lcrlb9_z;dZ-sf=bF&*Sqa@+Em=?{2ro62xd#BlhfS0iNsYgyjU7IP_G3V*U?z2|#*`NF-$TS{~^8{n4yD@?WAa_W4O4#}DH znUQkwP;eWzm74mjY`rkb+zI_C!qq>jV)IIE`j=tiSy4sy@o70nC^ z)lwJxEUJjC0_#EJJfD3s`Ix8TE{a?2y4yMaIpl1p*5~3dZv&tB=}Sy;)YVckbg(_E z@XE@CC1%*Sl$o9ht?JS!7DbMrHhM5X^kJT{ui#QCIa+I(HInw>UO50kAJj z)-+G}7bb_DE8V&l%uJH>F~eN;V!CGZ{2AvjpAVhof*i@X1dF5_twVw=* zQOA!#V*fk7Q%z@ThXal4W z6;upO9-|7q@a1_bVKz&K`cac-oP~AGz6RW=j)<{j^p0z{H~x&LZGnPYm>u?QVN(M1 z&9p@sp~zhzUiYVBZ)mB5c}R*m`91|*#ROI{J#l?=AT@5_F&pgZgc7Z!<^gx~xgJN7 zbp51>SlDa%n#+Q^p~~VZ{2#NYODQ^8e~O$Bz0y`3Ri}5mYU&4n?%uglr9^A9@Xf0e zlM`E;>_U^OrT5lv>%vDQ{q%2G>|vcTZR(rLhDj3-MD9wp^l2oHxjFVqQvVgm)-O6y zd8rozK~eobFp&sW7{9lb1wS=DNe$utj5%>x77tS8+ZVnqxr(k4iSl6l(Z~jS@&HA0 zGXQrYUUyU2v9|u;-WlLFh5iAh4v>$?seFN`Esy`|T|xeo7E2;3wjqq4i84qTpQwDj zF}raRgTdg(Km+gocn!YSn?zWuA+(Z*bY*Ghjq(1t*-BMRk|q5|FFV4O2*m!9!QrB@ ze3S0_JN+jujNl*Ttp7|Z9>yrg>izG*%QtO?d@leCX2GUjTuc;lL{wFbj4pdvQIV@P z*!zK-bcVspsP^==%58r=8(YsM`pTV*;HvW~r*{eatDaM`)DW*b*N9^{IDc^#%fI%S zObpTxYRtS5BI>WvKsC1CyXbpjl007yKp9`%JKA3rU%t~k%nD<&a9WtU4d4*M^|;M;#Q51~{DKx5 z-=&c~8-(A4GGeE0Y^p%xPg6$_LM&bA8nsEvc}IgS3RqYDg9Wa8O4;D zx8cCcR-+-Kg&}#@c7E>L2_fGiYlH6UDB>vt*bLaz!i!pLr0M4_$Df#Nx;0C<9jf1{ z7gimye%DPW&FWj?W{T2P*;`lRE76FU9fH(9>)~;aqWW;vJQVjTXc*Eic$GOKNqcXs zxgm5-MVKN?zB?kuWyWvq-hNfw4x{J7AMdfxynDiY;$fJB9A5Ni;;J9a1UA-SsbR|_ z!RrxuJ`E0SQ5K-5?&xT1r8un|?DOC^-tT-C!wWX?;ubRlCd+`GqIbtzY!{a^#wUrC zjI<$t=~JRj>b*vf#ggH&8&%j7q4%ss`$?fHll zIK$r6)5v*#pVg50s)(YG^N-g{3YaH!9-86!MDC6bG*^?O`t3|TIV@bF_R~y$jIy5h zcqux8X@MQf{UU${E()ap5g9_V?%Jewm znGzm$mdGn`x|N(ya#=W&Y~CsKv02ocws+P6&>!v5g>oG*i&Dxu zasA5qfLB>3o3OZdl7Ue#8IA*TpX84eKk$#1V^{Ut(=h!{X-18X0;G6P?$Vaa zDZ9G>>`R}2eMmSk2erCx%&I8j$8Aoxr6iLBj8>jKNy^iW-0&9Ka|dPJ-hjMtCB_H^e*OwKFo*!~E zfqyWmSm~l`pIVjy0apr?JX|2AQ-p@5p44GkVyJOn=C*}AV1UXvzvojZYuZ+KIqAth zWBg$IYl;rXXI0wU)1Re2v7~AR@V5kE?xVZ)G-*!IAN4ao=@!JDwY7$M^~5O-b6s{* zS`gV%%(xM1qSe_kVqMC?=V;ucBFGacO0y|cf6lUY{H@J=!rm+Ans$yddRtO2+zAlq zq_HM~C!HJ?#4;usl8G@=Fe7zPr`ynia94L-xm*-FJ<-+Q>gZoq1F>2JT6&+YHBTEjwS-->MG=!SM4+?Jp4r#w)R%EadLsfaLGiV|jMpX4bjDrphGOYgJp&BwNR1=vS*$xZz&cx1Eoy z2Wilcxol7Sw@0=%d;Q5N<3Mzw#9aaDB>V)k}Ag{vh-3{}@Zc@-WfGn@O zOfUjp{2@z>ns^YXXk~VG7K`l`$^2Tw<~0SN3ji{LMz*(CkPrf{{y#pJJNDIp0d@%= zM=W41UM0WX0Ix;&;?P^I2!qX$`C&S zP|&>GTwxIjw8wn}>A)$wYk%*RUG8GZbMhGHVoSngMu? zoYfOVXL2N-_<=>WbKjz^tAg|TL^7Wk+<=uOga(}Q#794EYGMk+ow(`$_knb1#_~qg ze0s_Ot32f*NE6#uBUd0>djZ7%_K&ad=_sJPUSqIRvsx!Lw!LcsXp(=7lA<r&5bW7~uhqo?6ewsJT9AHWg&G^#dVs!>`4b5BbIXE#wX_h|uQI0N!ew0M> zi5i&Y)v>kb3LS|1xt=-b6+zwF(})(G;Xg4)1cj%tQ|vJ~*5X=Ch6ltIsh_^kBSO2> z9X1-I`+8ODd)ns8N-$5n$hNt1#fTVUc|fIL%0rB0g_izPGxNg)ZpoyjaAx@(Zm}M` zc)_oRrLNF<-{$CveEVT#JL6>zV7Bak?N?M^6O}v>b9QJxFGai%<%9dOE(BrM^|!9~ z+PC^hk1}*?#Xhf78RWFhc|{Hv&Iz)m&&%6mo6mUUH-*HA}asaaq)8n zfJ4r)0t4`9BP$M_ZHJx@_w?I65s+62ryW*ovS!9mrkGD_ompUy*xj2PeIRGn%qky1 z_N9J`#j{PnYaxW1>O==6a2@=&8beoW4#!{@^V$sv>vX^rBeHDB*Lm;J6x&tnt7b0m z79c!^v2_yBG&TZGHu=aEDD#e|dTp3w$n6r*G@cyz34IHP@NsIn`!zxIMLkECoYg)3y-1+_RR*m(8x~QtGDW>6iE! zowz5MB*jOhB$EiQ?AhrbJAR3Aib!x!bN5dAC_!fDgF~DqinheX^SPEO5sl&Ph_JSR zypa7&mE(Jey`<)7D8B@(;$?@xT`4ttsTd1N5xlIL;Fo!Y4PYDRm%@gHKM{NS>+b76 z216Xz?1(2%T5NN>L37loHlDV-+8AdP!B(ANg?GfpL-8se4LFaOxv;zMBLGX9(xJ(d zhDLVbPmkaHOBk!X?vQ`mRrGi^43pfJnhvYF)GQw+dhQ(PNTirLke{(Scp{!5@vx8C z>@74pR=9Lk#_oD}Ny(SH;lKP=fkf+#3hYM@SB7-CFCt zy+iE*IheNWBcCS7MI#^Y!x_kPVVAIjsV2LUWRVcC-ow+??7v2YU~PFqhvJS_LrUAE zD}?kfXf_v$af-kFD1RqY47(;GzgI`! z%}eWudGcJKsnQAVOGCjW-m@uAn)i-9&@l_=w8w1U1nRi(wk;d_3Y< zD13nbVcK#?zVC5sOi-U(7l;nWE~88!w{Fb3L(h;l^hIvUA}}Z^qo5IYjlQ2rkM1h8?`vdY-U5mvoRE!bNvbEYn__-ejP3Q)H;}5$w_2 zAz}3pU)*r(T{~T~M-0ALMtlhWT9^t*o*czjD>m5OyLZdTXUn7AjFq44s8I-Cdiq5A za2sh$cz%r;|F{XaHE3{<+|h6=Xr7+KVE3F@w_&6v{=JM_kbkY*?k!!tES2ndN?}Rh z>p2~qWEnhh3xww{%8jGxeo-wdGu0Dk(R4KO?IDjh`1kSi^s}8AJU;&a%s|(`0F6fP zlX*oIPU5f!JLn7}ES?CUY=G%SQ!SAtlG^!bF+sE*_K*`{#7)2^&oqRNNO|B4F|Dg0 z(91qRW2lB5EJC)mh|$X%r;aSOMUSdYfIwYe?!c@Z0g2 zn>+}ZYFZhH+T~ZDM>Z1Us`!WVU=+7=<8ry85|s(t+mECn^Cr~C+0P1MkPgTf6WhjR_g?{ zUAb}Vvdc2aU1fm&%thkj9Vz);=N?R|%cW3K7D>phP5K{zD7==_MyUPx+pk z0-HE_>{(9>u>PY5=1$HS!*xXv~{&;Z1qdyZdb47d`SlQ%cpa5nE?jhttj4oNs|y?xaC$bkzSL% zO%=pqW>VJ>0vwL%>R6TgA)$)D-D{u^Ax-FnA*8+rT7AW*(O=CG}hp9UfY zCoD7GTNgp3km~j!resLn8eGxk=DW--1>Rw_} zl4mNQv(wEqi%0+p(Gg6yX$Y;qaI`|A;Sw?Y1h5r;;Q#)K(!CG@fuMoqlmF4bGlCi7 zMyV{Z+zw4Idl3zYh{QWyB0Sl&wY7Dk6NsdN5AaXPkH@6iFjB#T(y1W>_kTMBUt3TP z8!Y?X7b@a+o;6URf7nAywY9a8I%shNL|l7+|E$?uTs!N(z9VY>xY~;6#!Rt4F8pq> zhLqT(JS8?&SP@Lzi$Xte^{bA$b-jRTP0bbuthAoJ8b+ow1Q!knm@5Aa9AD@W44Xz@ zR5p1VRU(hi|FTNU<@H~`^hFNtFTU$BCEs6Y(GVKHzwfS1K?C6q-@FfO-0eVM)4>SUx-)4vQi-*T7BXSkv`u8DPDjTa5)kmF-Vr84Sh&~Z|w^T!U}i#5X2>plM& z8v?!Ok?!EQ7KgfiVMFcrK>~V!i;D~I;lM^2J=L%2YH!MOhB2uR7s?WDibL3~k)8*e zDgL{A9@CHW-@5x8B){Cs|E6jxBc1VS#o(tAZo2vax4CA%^dD!PeH*4=^|C_zY5$Ox zX#JViIg{p5+F{NSxwh4hj1$4SJ#lH$)}5o@f<{MrIH!8Ex_QLPZ3(6j2f(h0hBqRI zUki5Nc%OffwyON(-JJNGd=UmA5!0d${vjTSOptB_v{c8;J6^UzPR(>W3ghN`hpg5z za$j}TBll0Mn!9PB;9f57TCv_yY?cx92}$Zc%B z-1oDagwSLEs9P7whX-V{5ch0JyWW6WFNP6Mvd?;sXShO@eJP(6>#c zMUT#bhBJMZ|9$7nijJhGZnK69n#14FmK+9cx>8EO9crgNI>{p%Iv|HHtFD$Be|DP* z;=v`^=8ued?VvGPBKFa$?R>mA++I<}3uApYN@LlYT|tjZU-KCi*xrx@?#MsJd!i@=_nq>8=Mn6NowN3`OhNAd@(be>%TU{rI6|DG2ofnC z^8>D99lQ!%si|?cFGS|Rsn^jm6o$6I)dD@2KOQLl`C;L!xSSaVP1v~2+=KhU&kYV- z(euuWzNh@bKhClK>2}5&bd6XV&TIJQEgzQNQc6r~(H1rgG6{Uk3flhP`=j_1J4tvQ zCB`#RyijOUk_}K6a-uTqd%!dh*dYg-k+$)YfahBpKO9(FTQejm0#YRIjnH__)XS2w zF%%_)*}iyq%p1b;B5%)XHj``8v*y;TZ#=t^X`9~jCO9RR=1}`jz zx~09=*yO6uehLzB7!^QJVM#L5c_O4-had=TZJq$IFDo#O3y^^~xHlX54o*3yO2~Wx zkn1UuPrGE1$(BQO?sCx2IG;&p?^A`oAAeNv77@!d?C1?AiL}0Z8;*fOvh5b``G=M}AI7+zJutZWl!wK>E4=Q+!rC$imgkB~ zMPx&&mj*FOvDRzkAe07uC{zM^zt0a`c7u)%(uPE$owVKqWGWR_l+5p; z$6Oh{n#fRQOSS$^w?FP6E7D&{?vYfe=CTo7_mvKcnllsgEg?V^n}>o%xnq8^$TR3% zN8MRm7}zMRiiiYdm~uxP9-t5J9NvbDvMDu$))2PxfOOc7dlH~p)6L}Luy{OvqZ1NG zz7z=NnZbOlJ`sOTdtqBM`$G|%&*IYqg(8233PM5|?Wa~Ko8DUqBla5koV@qFo2Pa} zGdJF8oPrDW#qsS$vbEogux8|4)Q_)R0LJatz79Z{pa*^y6}R}CmkVU@qRTZ4!iLYb8G2!YU)9UUZ&y_?ZIEm<%O&C9pv~($=YXUk1Bfg25?_wexOAQ$<527`kM7T z7`(08iwd3|qeaD9qi1L3pzd5kLnvwuXJ$D6@+H*vhc(K1?Q@s?L$XEUk!`<1I{9C8 zei{v5>5{YUJ@Vn>%H_z{F6TaK?Y8%z-WQ5sfdEp3h^^Ah!i-wTnOS0spaJ_{PviqS z>8mealQ3CVC=A36lpHcfOe6a3+I9{O zo_lh%%BRz!>RL8L?nu0#AIGG4NX|so-4531`yK- z5#-oCo|U2}B$wAzEyfjBy-VNeLgny>*_PIxg;vB#zEW)dbx;SJGECL0d@b7eie`$0 ztj7Xs#4{=&p30csG0H5&Aov=vdy{*6arbVQ%Hgl?=e=!CI3=3nA28jUp+@L#Wrf}B z7&G@l#EiCXwzuaKC)#oPOO$?5Fn?*V=wLZJUNoUPFFeK0i>>!=n-9t*b5BV&S3G?w|1#`n>PTFXav2 zKsVI$#f5z#QuHO$^U`NSZX*=dQ&}0=@QfE~e5$VVtIm?P6K7Y?9r2DD^2uyfW|5Cg zB>n4lh);6@7ekLCe`Dm7b6f5OJ#9Uhv)w~fSln#$uzkUlJY7q?O3+r`Z*XX5Ibl0h z(wM{NR+YdTn|lA?M!BeNw@AOglFw*SeO!KGn1r%fz`j6CbYUvY zLC%b*?<~lU+PQy@?0n-K4!u+pGA8VDal8mXPTgnSTKNoWIyz#V$|1$2o?9UFC_+(bVy26N9h%fOV=piI%l=JyJuzJ}IAl$r^ z(G_#~=%TER+NKn-=Tzb!Cf#o#b*WYM&+8D)7Iw^R4M8@>j(&?`PFi9L1Z(6q$Nq+f zWv<()Hx9xg#x6rrh&a5u`?QYfQk>@FRz_^J{|kw#+aIvkbgHbJ-@gnSF#yX#znK+| zhM%v&Hs9A6#u}N=T6Z+?6qx;<+f{JjcHB4UDF5Zkh3b~fTB;km^pn1}x3A@*9EvMf zdNp@sz9;AhaCWwQz*%-1-Vs}xofUR4;oLDvH)+c1Xd90A*EVSfATU664Gaw8jv;X6 ze3Q5`tv@`U?Vatm zQGkW|c`_|yg17Qe;kM3yz(!vSL72h{)4p;UPlbBhJA4EuJj4XWhzWDDGf)ceqOB4{ZWqJwU~1yGXztMu=B@{n)Zuz`v9O zKpY@RBn3S8`~L>*{I~q)_p$y3zmIqe@umV=)nx!IdJX)xBBI*xUkL{2IdQfC$-7h! zFquDzR>eE?GRK=}_{HPg1ON(gfTD4bO7v6eAs&SQ?*EMzMJbMFPNW<_MgyQP-Q#}eJ%GN)!}u06ai`_uM2o-`W432Rcn)r+mRMfitJdP64O} zU`JlZ3@VBV(sncQp4T}n$^}hiosfe@pq6xa?dZ&wBBscRWcA4P9_=qE`rhBHDEk+7 z3MlRMJy5gHh#|7m{8N+iHnTIZ9ZW*-Rp7|yx0VLa(_&`*0Z73l`!giKTm*r=1wIvrmDQZXp15D& zCnV!jra^jE;tqviA|ls)GX!Mm>gKlB1R)r)5ZUt&E>dj0*{Q(_lyjA|L72d(7a#1@ zu=I016ZPKx?o`Em%IQ-Ex>&~ zIH)>uXvf-2{}F}E1w))g;&`mwmn0~s2Zn*-CdQ{fYi+lEeaVkW{`d?Ex|>YeA&d03`$oR5t05Yi))2-sfufhTM^*uw%dHER?i>B1%B=gps?~yOlO)N5T zDJHb`&kjGzPpSvljY(b2@(`*57I0FR1@NuQ8EW7u($AYN%TDmaD~_cZ(D$RLThAq* zIR-FTYZt4OR>WT)y)n|nfXXu5n;%u!5zid_8LTQ!Ry(qoxl+pOBpG`$B8mwRy#T9vJ)KU6>KzTXTV+J~jZ(VXi%hJ+62q6LZO3-~`S)+W zDZUEB{nVc}>p}5N&r+kb>yV+$81f*>63>a7N>rg3j|D?sre#|g3O9~ZbX?pu5$Na6*3A903 zH`L6ra3y0u{gPAp>p~I3;>eHjLbJ{>^q|{*fc!SoBk{=5C49CyM+TS{N@=G>_i$)? zr?rgn3Aa0E+e(V+M=?VIW3#-L1@{CYJ#IZDUv$uETo(4n_kI$LrGP+U@oHAMBL!!i z`>WZcx7lHE-nrihg02z89v8d>I6)PWZE$MR;r(1@LKQAFIw zWf6L@3thK?3qk8p8WE4dP)7yDj+&<+w7WPX(~J3Qek%7_uwDUQiRi}+H1!h z>C!qJNPNxvMhkcZ+yHQrR53_ zk5eS*M<8gohSM#E;om&h_d^s*&!|T0I%^{m%d67US6w*C@RzU{)klcgq%;> z)aYghud8SQ%Hu`+;=_#kzsdHjJ#q#&Iq8y_ccOeR#aqzW|xKUJDK z-C-_K$4c_7ux8QLw!R~#m!bZJkh_X%QVe6*_q`bfag#i|EEw<3bwrk1GR;llTZ52+ zx9?NF<)&JZ#XVK@l^ULY?4VYwK!+wdMo)I?(|mVdTRH4l987^HR{Hd`MFtgW-Z9dL zDr4HOup0;A%F8TL)u5R1d%#v#t8-N6+~{U^sbBai(Q5aZ@utvLRTC=MzLX8#7*}nu zKocF_)0UKnV8|6IA8gy~agg)<<=Ff&}6=%KNJJsGJwP@M;?}eOxB1}NJaYtg( ze5WAdM%NKhQG?p|?hKqWHHeviA5NE5-bwQJ#c8{InCm_0GE-)^inI*JAMR#?8Lj7? z{W_Di)(X>Axq9X%OV4$hEsrK{D4hg!be!DAG@(g_kDXtc_Rh4>8yqVO3kxwD2Vf=u z-Hxw+VRz3~qCoM_A=rpCIZD0q0kOqIN582Ig&*|E;os$oCaU(-E$`4F>8 zS$E+AVPL%l?KZ}KvdYW4yVC`6%6#M-TTp`Ya=CX+p`b^5UAW)TS)`<$);1g zWsiOmvIZ)ck3%E?OLKy8ucBK6z-6v~&>0|4@_+U8U!3`0X^a2+cw;a05Eq&R+iQ6; z;e=nYg#+-8^ZxNE189K&C{_nVnM{RMKUh%$wi;zDl?`aDxBp4%Xzs@4U49{z%`XWX zLUW5AwyrPd4eIRX^=0kT{?Z=+c-Wu$k*8vMALMK5bPm%uWUY1#(Qoxq94-?cZp$V; z_%$1<1=GI!FWT_4g7i+g&E}=TK3r{@tfkAa+{cA-u?hN z_ArL%vH(R(}FLU#?Kh>Z)hz zQz0x{X7f9HvFwDc=C&nM3uK=V}OIdUC8ee8=Kn>%S zOa0P9i4ZG~2VhROZle^71J<8W%f3MjNsI=u^xxY5?!su#nHx+lx4pf{Pv-IxO&3~f z#XX@?g=}EpNO#$?3ttX@;bxKbvWI1Pk2LAg4+bE4XxyH{_|0Zf=hNsll#`^OQ0tw3 z+yiV#l_OWNm7r2E3q;y+ImmwlsbtyeAO3Lo>m}eUNw6dEk(S$TN4@2;S2ys3;#EDS?$``QoICiCY|2mRKw^ zJekfvJrElcATc9wtF-IYB(s-U5jCXRV|S7;ch}v519r;g@nBD1I}g)WIjHSH0xHry zPoyoWl5L5L4dGoni;KtB?F!9j2*48Yx~b^fPwO{41=9@%C{KTSn)9bp(Umz5U5vA* zp@dE^)(MQ3n0z2yxKJX^n-x3JuB&b(Q&_e6=-ZmdeH~%vJ=rG)uFrcTRwk9?2zEdA zj*>w@4m2eJ$6ER|hH>WH*FvRo?m2∓pkb;_>p4VgI|FEPD`psWNIb-?WT=mI54ueUUJ(@On=f zMpxK1^fo}>UDThZc&hat8<7q;Z-+EVE;ioTD!6!@u3b6v%SpP}_pPpzS)R`JTg%MA z?n|<{n`!t*{YvmzJ}@SFCTbbiol3Lv{=SF_(3eUmKS?kE&;Q9_CriFcvqkEqq4awD zckCpyN2{t8sJNZ4#PSdI6EZWY%yuRVXXiJl)|c4Lml+nXUyEfiAY-_Hl=MDE8~5I& zJQu%#)~f>JZJqX))D@u%xohjLc8|0-6$P`@Zl-L`oioYsac}U|PE8$_odanRG2-7E z(Hk<^Ew`Z$4<&GMffzSF@O;Po-lKxJbD3z$C9TrX{ov7#iE2gnm)jK^vL>!*TW3Ly zt&8>{FfS|8!9J~$gyEAGi=Jt+Th?X!ePr%Q8v2m=jELq7An z@yB7zML0Z3RZh~^iV_ja6p5C6&-_jt<&R%Sxqj4N-jE6`75$f}_)`1vcHWNy^Tg)~ zn{S)}BLXr2K)5JkLoXlY-CqtZuKNaqlvrFRNm)IUjetykodDS|Hn$*f70$h5~F(dcpL_?O2E%S z;x3%jeLI*3$Q*n9hYS4MKvuDy3Ua)me-%Oiy9ks_c&0{kHA<1t5zDl8fYOu)L|5}i z&^Vb-@rR%a`XqPU{=0e^NCQ=#1SRVMI}Y>k*aW*1fALN%#5R1Urltn`4nPnn&H^6C zfSOAcK(+wscYyI@fNbUE#}$e@bJ_3=QN!|z<_eYa+?$4#-{(KzC9fguWz~H8DvaCR z-Thq?7BV?({|*36v;5OHMMU@ByO|3Df=jnS^E0U3CW&4Xh43m+NJXqtf1&`UNq?~& z_5Ryu_}&61oHv}ZBXO>Klju*Y<8T4|kzW!ZJSRrx+FKWb%@;CO_U2hDf>3x^_(O`|f=3qMx%$ZIu&sux^ z2Aq|X!8(@kK=p%g2KlN}(2dzNo> diff --git a/docs/imgs/stellar-high-level-design.svg b/docs/imgs/stellar-high-level-design.svg new file mode 100644 index 0000000..2d1810e --- /dev/null +++ b/docs/imgs/stellar-high-level-design.svg @@ -0,0 +1,4 @@ + + + +

Packet IO
(MARSIO, Pcap)

Packet IO...
Session Manager
Session Manager
Plugin Manager
Plugin Manager
Built-in Network Functions
(.a)
Built-in Network Functions...
Customized Network Functions
(.so and .lua)
Customized Network Functions...

Protocol Decoders
(HTTP, DNS, SSL, etc.)

Protocol Decoders...

Active queue management
(Blue, CoDel, etc.)

Active queue management...
Callback & Context
Callback & Context
Session & Events
Session & Events
Packet
Packet
Text is not SVG - cannot display
\ No newline at end of file diff --git a/readme.md b/readme.md index 49278c0..0e1b1ff 100644 --- a/readme.md +++ b/readme.md @@ -13,4 +13,4 @@ The stellar components are: - **Active Queue Management** is queue management algorithm libraries that schedule packets by buffering, forwarding, marking, or dropping. A plugin creates a queue instance and enqueues packets as its needs. - Question: Who consumes the dequeue events? -![stellar-high-level-design](./docs/imgs/stellar-high-level-design.png) \ No newline at end of file +![stellar-high-level-design](./docs/imgs/stellar-high-level-design.svg) \ No newline at end of file From 61e3c264f319123ac329289c44958a9b7bb5a6ab Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Wed, 27 Jul 2022 15:51:07 +0800 Subject: [PATCH 07/17] Refactored plugin management interface --- .../custom_event_plugin.inf | 9 +- .../http_event_plugin/http_event_plugin.inf | 7 +- sdk/include/http.h | 2 +- sdk/include/plugin.h | 3 + sdk/include/session.h | 18 +- src/main.cpp | 12 +- src/plugin_manager/plugin_manager.cpp | 182 ++++++++++++------ src/plugin_manager/plugin_manager.h | 20 +- src/protocol_decoder/http/http.cpp | 3 +- src/session_manager/session_manager.cpp | 2 +- src/session_manager/session_manager.h | 10 +- 11 files changed, 168 insertions(+), 100 deletions(-) diff --git a/sdk/example/plugins/custom_event_plugin/custom_event_plugin.inf b/sdk/example/plugins/custom_event_plugin/custom_event_plugin.inf index 78107e1..479d474 100644 --- a/sdk/example/plugins/custom_event_plugin/custom_event_plugin.inf +++ b/sdk/example/plugins/custom_event_plugin/custom_event_plugin.inf @@ -1,13 +1,16 @@ [PLUGINFO] INIT_FUNC=custom_plugin_init -DESTROY_FUNC=custom_plugin_destroy -LIBARY_PATH=./plugins/custom_event_plugin/custom_event_plugin.so +EXIT_FUNC=custom_plugin_exit +LIBRARY_PATH=./plugins/custom_event_plugin/custom_event_plugin.so +# Support SESSION_EVENT_TYPE +# OPENING,RAWPKT,ORDPKT,META,CLOSING,ALL + [SESSION_TYPE.TCP] SESSION_EVENT_TYPE=ALL SESSION_EVENT_CALLBACK=custom_plugin_entry [SESSION_TYPE.CUSTOM_A] -SESSION_EVENT_TYPE=ALL +SESSION_EVENT_TYPE=OPENING,ORDPKT,CLOSING SESSION_EVENT_CALLBACK=custom_event_plugin_entry \ No newline at end of file diff --git a/sdk/example/plugins/http_event_plugin/http_event_plugin.inf b/sdk/example/plugins/http_event_plugin/http_event_plugin.inf index 62b7a4a..4609300 100644 --- a/sdk/example/plugins/http_event_plugin/http_event_plugin.inf +++ b/sdk/example/plugins/http_event_plugin/http_event_plugin.inf @@ -1,9 +1,8 @@ [PLUGINFO] INIT_FUNC=http_event_plugin_init -DESTROY_FUNC=http_event_plugin_destroy -LIBARY_PATH=./plugins/http_event_plugin/http_event_plugin.so +EXIT_FUNC=http_event_plugin_exit +LIBRARY_PATH=./plugins/http_event_plugin/http_event_plugin.so - -[SESSION_TYPE_HTTP] +[SESSION_TYPE.HTTP] SESSION_EVENT_TYPE=ALL SESSION_EVENT_CALLBACK=http_event_plugin_entry \ No newline at end of file diff --git a/sdk/include/http.h b/sdk/include/http.h index 62b1fbd..d7f3fbf 100644 --- a/sdk/include/http.h +++ b/sdk/include/http.h @@ -3,4 +3,4 @@ #include "packet.h" #include "session.h" -int http_decoder(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint32_t len, void **pme); +void http_decoder(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint16_t len, void **pme); \ No newline at end of file diff --git a/sdk/include/plugin.h b/sdk/include/plugin.h index 11d55cc..ac3b279 100644 --- a/sdk/include/plugin.h +++ b/sdk/include/plugin.h @@ -2,5 +2,8 @@ #include "session.h" +typedef int plugin_init_callback(void **pme); +typedef void plugin_exit_callback(void *pme); + void plugin_remove_from_session_event(struct stellar_event *ev, struct stellar_session *s); \ No newline at end of file diff --git a/sdk/include/session.h b/sdk/include/session.h index e2ed19e..b497fab 100644 --- a/sdk/include/session.h +++ b/sdk/include/session.h @@ -23,22 +23,22 @@ enum stellar_session_type SESSION_TYPE_MAX, }; -enum stellar_session_event_type +enum session_event_type { - SESSION_EVENT_OPENING = 1<<1, - SESSION_EVENT_RAW_PKT = 1<<2, - SESSION_EVENT_ORDERED_PKT = 1<<3, - SESSION_EVENT_META = 1<<4, - SESSION_EVENT_TIMEOUT = 1<<5, - SESSION_EVENT_CLOSING = 1<<6, + SESSION_EVENT_OPENING = (0x01 << 1), + SESSION_EVENT_RAWPKT = (0x01 << 2), + SESSION_EVENT_ORDPKT = (0x01 << 3), + SESSION_EVENT_META = (0x01 << 4), + SESSION_EVENT_CLOSING = (0x01 << 5), + SESSION_EVENT_ALL = (0x01 << 1 | 0x01 << 2 | 0x01 << 3 | 0x01 << 4 | 0x01 << 5), }; struct stellar_session_event_extras; -typedef int (*fn_session_event_callback)(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint32_t len, void **pme); +typedef void (fn_session_event_callback)(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint16_t len, void **pme); void session_manager_trigger_event(struct stellar_session *s, - stellar_session_event_type type, + enum session_event_type type, struct stellar_session_event_extras *info); struct stellar_session *session_manager_session_derive(const struct stellar_session *this_session, diff --git a/src/main.cpp b/src/main.cpp index 891300a..5071278 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -56,12 +56,14 @@ int main(int argc, char ** argv) { //manager_init struct session_manager *session_mgr = session_manager_init(); - struct plugin_manager *plug_mgr = plugin_manager_init(); + struct plugin_manager *plug_mgr = plugin_manager_create(); + + // register build-in plugin + plugin_manager_register(plug_mgr, "HTTP", http_decoder); - //register build-in plugin - plugin_manager_event_register(plug_mgr, "HTTP", http_decoder, nullptr); // load external plugins - plugin_manager_load("./plugins.inf"); + char absolute_plugin_file[] = "/op/tsg/sapp/plug/plugins.inf"; + plugin_manager_load(plug_mgr, absolute_plugin_file); //packet_io_init struct packet_io_device *dev = packet_io_init(1, "stellar", "cap0"); @@ -75,6 +77,8 @@ int main(int argc, char ** argv) /* main loop code */ usleep(1); } + + plugin_manager_destory(plug_mgr); return 0; } diff --git a/src/plugin_manager/plugin_manager.cpp b/src/plugin_manager/plugin_manager.cpp index 4fba201..1755cc4 100644 --- a/src/plugin_manager/plugin_manager.cpp +++ b/src/plugin_manager/plugin_manager.cpp @@ -1,97 +1,153 @@ -#include -#include - #include "deps/uthash/uthash.h" #include "sdk/include/session.h" +#include "sdk/include/plugin.h" #include "session_manager.h" +#include "plugin_manager.h" -#if 0 -struct session_event_callback +#include +#include + +/****************************************************************************** + * CallBack Static Hash Table (For Global) + ******************************************************************************/ + +struct session_event_callback_static { - const char *session_name; - fn_session_event_callback callback; - TAILQ_ENTRY(session_event_callback) entries; - UT_hash_handle hh; + enum session_event_type event; + fn_session_event_callback *callback; }; -TAILQ_HEAD(session_event_callback_list, session_event_callback); - -struct session_event_callback_map +/* + * Hast table + * + * key: string -> session_type + * val: array -> event_cbs + */ +struct managed_session_event_callback { - struct session_event_callback_list session_ev_cb_list; - UT_hash_handle hh; + char session_type[16]; + struct session_event_callback_static event_cbs[0]; // dynamic array + // Record the number of callback functions that want to process the current session + int event_cbs_num; + UT_hash_handle hh; +}; + +/****************************************************************************** + * CallBack Runtime Array (For Per Session) + ******************************************************************************/ + +struct session_event_callback_runtime +{ + int skip; // for skip this plugin callback + void *callback_args; + + enum session_event_type event; + fn_session_event_callback *callback; +}; + +struct session_plugin_ctx +{ + int event_cbs_num; + struct session_event_callback_runtime event_cbs[0]; // dynamic array +}; + +/****************************************************************************** + * struct plugin_manager + ******************************************************************************/ + +/* + * Each plugin has an init_cb and an exit_cb, but may have multiple entry_cb. + * entry_cb is stored in the global_session_callback_ctx. + */ +struct plugin_ctx +{ + char *name; + char *library; + /* + * Stores the context generated by the plugin initialization, + * which is used to release resources when the plugin ends. + */ + void *pme; + plugin_init_callback *init_cb; + plugin_exit_callback *exit_cb; }; struct plugin_manager { - struct session_event_callback_list fixed_event_cbs[SESSION_TYPE_MAX]; - struct session_event_callback_map *session_ev_cb_map; + struct managed_session_event_callback *cb_table; + struct plugin_ctx plugins[MAX_PLUGIN_NUM]; }; -#endif +/****************************************************************************** + * Private API + ******************************************************************************/ -struct stellar_plugin_ctx +static void plugin_manager_handle_opening_event(struct plugin_manager *plug_mgr, struct stellar_event *event) { - void *call_back_arg; - const struct timeval timeout; - fn_session_event_callback call_back; - TAILQ_ENTRY(stellar_plugin_ctx) tqe; -}; - -TAILQ_HEAD(stellar_plugin_ctx_list, stellar_plugin_ctx); - -struct stellar_plugin_data -{ - stellar_plugin_ctx_list plugin_ctx_list; -}; - -struct plugin_manager_handle *plugin_manager_init() -{ - return nullptr; } -int plugin_manager_load(const char *plugin_conf_path) +static void plugin_manager_handle_data_event(struct plugin_manager *plug_mgr, struct stellar_event *event) +{ +} + +static void plugin_manager_handle_closing_event(struct plugin_manager *plug_mgr, struct stellar_event *event) +{ +} + +/****************************************************************************** + * Public API + ******************************************************************************/ + +struct plugin_manager *plugin_manager_create() +{ + struct plugin_manager *plug_mgr = NULL; + + return plug_mgr; +} + +void plugin_manager_destory(struct plugin_manager *plug_mgr) +{ + if (plug_mgr) + { + } +} + +int plugin_manager_load(struct plugin_manager *plug_mgr, const char *plugin_file) { return 0; } -int plugin_manager_event_register(struct plugin_manager *h, - const char *session_type_name, - fn_session_event_callback call_back, - const struct timeval *timeout) +void plugin_manager_unload(struct plugin_manager *plug_mgr) +{ +} + +int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_type, fn_session_event_callback *callback) { return 0; } -void plugin_manager_dispatch(struct plugin_manager *h, struct stellar_event *event) +void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_event *event) { -#if 0 - struct session *s = container_of(event, struct session, cur_ev); - struct session_event_callback_map *t_map; - struct session_event_callback *session_ev_cb; - HASH_FIND(hh, g_plug_mgr.session_ev_cb_map, s->name, strlen(s->name), t_map); - if(!t_map) return; - switch (s->state) + assert(event); + struct stellar_session_event_data *event_data = event->session_event_data; + assert(event_data); + session_event_type type = event_data->type; + + switch (type) { case SESSION_EVENT_OPENING: - TAILQ_FOREACH(session_ev_cb, &t_map->session_ev_cb_list, session_event_cb_tq_entries) - { - struct session_event *ev = ALLOC(struct session_event, 1); - ev->callback = session_ev_cb->callback; - ev->callback(s, SESSION_EVENT_OPENING, NULL, NULL, 0, &ev->cb_pme); - if(ev->state == 1) - { - TAILQ_INSERT_TAIL(&s->ev_list, ev, session_event_tq_entries); - } - else - { - FREE(&ev);// TODO; - } - } + plugin_manager_handle_opening_event(plug_mgr, event); + break; + case SESSION_EVENT_RAWPKT: /* fall through */ + case SESSION_EVENT_ORDPKT: /* fall through */ + case SESSION_EVENT_META: /* fall through */ + plugin_manager_handle_data_event(plug_mgr, event); + break; + case SESSION_EVENT_CLOSING: + plugin_manager_handle_closing_event(plug_mgr, event); break; default: + // TODO log error break; } -#endif - return; } \ No newline at end of file diff --git a/src/plugin_manager/plugin_manager.h b/src/plugin_manager/plugin_manager.h index cfe2905..f6a12a3 100644 --- a/src/plugin_manager/plugin_manager.h +++ b/src/plugin_manager/plugin_manager.h @@ -1,20 +1,20 @@ #pragma once - - #include "sdk/include/session.h" -#include "sdk/include/plugin.h" +#define MAX_PLUGIN_NUM 256 +struct per_session_event_cbs; struct plugin_manager; -struct plugin_manager *plugin_manager_init(); -int plugin_manager_event_register(struct plugin_manager *h, - const char *session_type_name, - fn_session_event_callback call_back, - const struct timeval *timeout); +struct plugin_manager *plugin_manager_create(); +void plugin_manager_destory(struct plugin_manager *plug_mgr); -int plugin_manager_load(const char *plugin_conf_path); +// return 0: success +// return -1: error +int plugin_manager_load(struct plugin_manager *plug_mgr, const char *plugin_file); +void plugin_manager_unload(struct plugin_manager *plug_mgr); -void plugin_manager_dispatch(struct plugin_manager *h, struct stellar_event *ev); \ No newline at end of file +int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_type, fn_session_event_callback *callback); +void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_event *event); \ No newline at end of file diff --git a/src/protocol_decoder/http/http.cpp b/src/protocol_decoder/http/http.cpp index 13fd4d6..408f149 100644 --- a/src/protocol_decoder/http/http.cpp +++ b/src/protocol_decoder/http/http.cpp @@ -1,13 +1,12 @@ #include "http.h" #include "session_manager.h" -int http_decoder(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint32_t len, void **pme) +void http_decoder(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) { struct stellar_session_event_extras *info; struct stellar_session *new_session=session_manager_session_derive(s, "HTTP"); session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info); session_manager_trigger_event(new_session, SESSION_EVENT_META, info); - return 0; } int http_peek(const struct stellar_session *s, const char *payload, uint32_t len) diff --git a/src/session_manager/session_manager.cpp b/src/session_manager/session_manager.cpp index ff10c3e..ab70071 100644 --- a/src/session_manager/session_manager.cpp +++ b/src/session_manager/session_manager.cpp @@ -12,7 +12,7 @@ struct session_manager *session_manager_init() void session_manager_trigger_event(struct stellar_session *s, - stellar_session_event_type type, + enum session_event_type type, struct stellar_session_event_extras *info) { return; diff --git a/src/session_manager/session_manager.h b/src/session_manager/session_manager.h index 047d8c5..6d17e79 100644 --- a/src/session_manager/session_manager.h +++ b/src/session_manager/session_manager.h @@ -2,14 +2,14 @@ #include "sdk/include/session.h" -struct stellar_plugin_data; +struct per_session_evcb_runtime_ctx; struct stellar_session_event_data { struct stellar_session *s; - stellar_session_event_type type; + enum session_event_type type; long last_active; - struct stellar_plugin_data *plugin_data; + struct session_plugin_ctx *plugin_ctx; }; struct session_manager; @@ -18,6 +18,10 @@ struct session_manager *session_manager_init(); struct stellar_event *session_manager_commit(struct session_manager *session_mgr, struct stellar_packet *pkt); struct stellar_event *session_manager_fetch_event(struct session_manager *session_mgr); +// interfaces for data interaction between stellar event and plugin manager +struct session_plugin_ctx *stellar_event_get_plugin_ctx(struct stellar_event *event); +void stellar_event_set_plugin_ctx(struct stellar_event *event, struct session_plugin_ctx *plugin_ctx); + struct stellar_session { stellar_session_type type; From 649d29e53887dc122a828d13696d356310ae86d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E8=B6=85?= Date: Tue, 2 Aug 2022 03:15:32 +0000 Subject: [PATCH 08/17] Update readme.md --- readme.md | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 0e1b1ff..aed4fcc 100644 --- a/readme.md +++ b/readme.md @@ -13,4 +13,68 @@ The stellar components are: - **Active Queue Management** is queue management algorithm libraries that schedule packets by buffering, forwarding, marking, or dropping. A plugin creates a queue instance and enqueues packets as its needs. - Question: Who consumes the dequeue events? -![stellar-high-level-design](./docs/imgs/stellar-high-level-design.svg) \ No newline at end of file +![stellar-high-level-design](./docs/imgs/stellar-high-level-design.svg) + +## Packet IO Library +``` +struct packet +{ + enum io_type type; + void *raw_pkt; +} +packet_io_loop() +{ + packet_io_rx(&rx_pkt) + //ingress processing: Tunnel decoding, IP defragmentation + session_manager(); + plugin_manager(); + //egress processing: AMQ + rl_group_id=pkt_get_group_id(rx_pkt); + void *raw_pkt=pkt_get_raw(rx_pkt); + AMQ_enqueue(group_id[], raw_pkt, pkt_sz); + +} +``` + +## Plugin Manager + +Plugin Management APIs +``` +pm_session_dettach_me(pm, session); +pm_session_dettach_others(pm, session); +``` +## Session Manager +Session Management APIs +``` +session_drop_current_packet(session); +session_set_ratelimit_group(session, rl_group_id); +session_set_metadata(session, const char *key, void *value, size_t val_sz, free_callback); +session_get_metadata(session, const char *key, void **value, size_t *val_sz); +session_del_metadata(session, key) +session_lock(session, plug_id); +session_unlock(session, plug_id); + +``` +Plugin Example +``` +plugin_entry(session, pme) +{ + session_get_metadata(session, "fw_action", value); + if(value==INTERCEPT) + { + //pm_session_dettach_me(session); + return; + } + ret=check_security_policy(session); + if(ret==INTERCEPT) + { + pm_session_dettach_others(session); + } + else if(ret==RATE_LIMIT) + { + group_id=security_policy_id; + amq_group_create(group_id, CIR, CBS); + session_set_ratelimit_group(session, group_id); + } +} +``` From 50111e7cd09ff83ecbd53017d0edf6d33b84a801 Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Wed, 27 Jul 2022 18:32:22 +0800 Subject: [PATCH 09/17] Plugin Management support C plugin --- CMakeLists.txt | 1 + deps/toml/CMakeLists.txt | 3 + deps/toml/toml.c | 2379 +++++++++++++++++ deps/toml/toml.h | 175 ++ sdk/example/CMakeLists.txt | 13 +- sdk/example/custom_event_plugin.cpp | 50 +- sdk/example/http_event_plugin.cpp | 78 +- .../custom_event_plugin.inf | 22 +- .../http_event_plugin/http_event_plugin.inf | 14 +- sdk/example/plugins/plugins.inf | 6 +- sdk/include/http.h | 3 +- sdk/include/plugin.h | 14 +- sdk/include/session.h | 11 +- src/CMakeLists.txt | 13 +- src/main.cpp | 8 +- src/plugin_manager/CMakeLists.txt | 12 +- src/plugin_manager/plugin_manager.cpp | 480 +++- src/plugin_manager/plugin_manager.h | 8 +- src/plugin_manager/plugin_manager_config.cpp | 358 +++ src/plugin_manager/plugin_manager_config.h | 34 + src/plugin_manager/plugin_manager_module.cpp | 168 ++ src/plugin_manager/plugin_manager_module.h | 12 + src/plugin_manager/plugin_manager_util.cpp | 40 + src/plugin_manager/plugin_manager_util.h | 61 + src/plugin_manager/test/CMakeLists.txt | 18 + .../test/gtest_plugin_manager.cpp | 159 ++ .../custom_event_plugin.inf | 14 + .../http_event_plugin/http_event_plugin.inf | 10 + .../test_plugins/plugins_config/plugins.inf | 4 + .../plugins_library/CMakeLists.txt | 9 + .../plugins_library/custom_event_plugin.cpp | 58 + .../plugins_library/http_event_plugin.cpp | 82 + src/protocol_decoder/http/http.cpp | 13 +- src/session_manager/session_manager.cpp | 62 +- src/session_manager/session_manager.h | 21 +- 35 files changed, 4220 insertions(+), 193 deletions(-) create mode 100644 deps/toml/CMakeLists.txt create mode 100644 deps/toml/toml.c create mode 100644 deps/toml/toml.h create mode 100644 src/plugin_manager/plugin_manager_config.cpp create mode 100644 src/plugin_manager/plugin_manager_config.h create mode 100644 src/plugin_manager/plugin_manager_module.cpp create mode 100644 src/plugin_manager/plugin_manager_module.h create mode 100644 src/plugin_manager/plugin_manager_util.cpp create mode 100644 src/plugin_manager/plugin_manager_util.h create mode 100644 src/plugin_manager/test/CMakeLists.txt create mode 100644 src/plugin_manager/test/gtest_plugin_manager.cpp create mode 100644 src/plugin_manager/test/test_plugins/plugins_config/custom_event_plugin/custom_event_plugin.inf create mode 100644 src/plugin_manager/test/test_plugins/plugins_config/http_event_plugin/http_event_plugin.inf create mode 100644 src/plugin_manager/test/test_plugins/plugins_config/plugins.inf create mode 100644 src/plugin_manager/test/test_plugins/plugins_library/CMakeLists.txt create mode 100644 src/plugin_manager/test/test_plugins/plugins_library/custom_event_plugin.cpp create mode 100644 src/plugin_manager/test/test_plugins/plugins_library/http_event_plugin.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 29429de..fdb093f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ include_directories(${CMAKE_SOURCE_DIR}/sdk/include) #add vendor and source directory add_subdirectory(vendor) +add_subdirectory(deps/toml) add_subdirectory(src) add_subdirectory(sdk/example) diff --git a/deps/toml/CMakeLists.txt b/deps/toml/CMakeLists.txt new file mode 100644 index 0000000..504c0bf --- /dev/null +++ b/deps/toml/CMakeLists.txt @@ -0,0 +1,3 @@ +set(CMAKE_C_FLAGS "-std=c99") +add_definitions(-fPIC) +add_library(toml STATIC toml.c) \ No newline at end of file diff --git a/deps/toml/toml.c b/deps/toml/toml.c new file mode 100644 index 0000000..fafe0da --- /dev/null +++ b/deps/toml/toml.c @@ -0,0 +1,2379 @@ +/* + + MIT License + + Copyright (c) CK Tan + https://github.com/cktan/tomlc99 + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + +*/ +#define _POSIX_C_SOURCE 200809L +#include "toml.h" +#include +#include +#include +#include +#include +#include +#include +#include + +static void *(*ppmalloc)(size_t) = malloc; +static void (*ppfree)(void *) = free; + +void toml_set_memutil(void *(*xxmalloc)(size_t), void (*xxfree)(void *)) { + if (xxmalloc) + ppmalloc = xxmalloc; + if (xxfree) + ppfree = xxfree; +} + +#define MALLOC(a) ppmalloc(a) +#define FREE(a) ppfree(a) + +#define malloc(x) error - forbidden - use MALLOC instead +#define free(x) error - forbidden - use FREE instead +#define calloc(x, y) error - forbidden - use CALLOC instead + +static void *CALLOC(size_t nmemb, size_t sz) { + int nb = sz * nmemb; + void *p = MALLOC(nb); + if (p) { + memset(p, 0, nb); + } + return p; +} + +// some old platforms define strdup macro -- drop it. +#undef strdup +#define strdup(x) error - forbidden - use STRDUP instead + +static char *STRDUP(const char *s) { + int len = strlen(s); + char *p = MALLOC(len + 1); + if (p) { + memcpy(p, s, len); + p[len] = 0; + } + return p; +} + +// some old platforms define strndup macro -- drop it. +#undef strndup +#define strndup(x) error - forbiden - use STRNDUP instead + +static char *STRNDUP(const char *s, size_t n) { + size_t len = strnlen(s, n); + char *p = MALLOC(len + 1); + if (p) { + memcpy(p, s, len); + p[len] = 0; + } + return p; +} + +/** + * Convert a char in utf8 into UCS, and store it in *ret. + * Return #bytes consumed or -1 on failure. + */ +int toml_utf8_to_ucs(const char *orig, int len, int64_t *ret) { + const unsigned char *buf = (const unsigned char *)orig; + unsigned i = *buf++; + int64_t v; + + /* 0x00000000 - 0x0000007F: + 0xxxxxxx + */ + if (0 == (i >> 7)) { + if (len < 1) + return -1; + v = i; + return *ret = v, 1; + } + /* 0x00000080 - 0x000007FF: + 110xxxxx 10xxxxxx + */ + if (0x6 == (i >> 5)) { + if (len < 2) + return -1; + v = i & 0x1f; + for (int j = 0; j < 1; j++) { + i = *buf++; + if (0x2 != (i >> 6)) + return -1; + v = (v << 6) | (i & 0x3f); + } + return *ret = v, (const char *)buf - orig; + } + + /* 0x00000800 - 0x0000FFFF: + 1110xxxx 10xxxxxx 10xxxxxx + */ + if (0xE == (i >> 4)) { + if (len < 3) + return -1; + v = i & 0x0F; + for (int j = 0; j < 2; j++) { + i = *buf++; + if (0x2 != (i >> 6)) + return -1; + v = (v << 6) | (i & 0x3f); + } + return *ret = v, (const char *)buf - orig; + } + + /* 0x00010000 - 0x001FFFFF: + 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx + */ + if (0x1E == (i >> 3)) { + if (len < 4) + return -1; + v = i & 0x07; + for (int j = 0; j < 3; j++) { + i = *buf++; + if (0x2 != (i >> 6)) + return -1; + v = (v << 6) | (i & 0x3f); + } + return *ret = v, (const char *)buf - orig; + } + + /* 0x00200000 - 0x03FFFFFF: + 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx + */ + if (0x3E == (i >> 2)) { + if (len < 5) + return -1; + v = i & 0x03; + for (int j = 0; j < 4; j++) { + i = *buf++; + if (0x2 != (i >> 6)) + return -1; + v = (v << 6) | (i & 0x3f); + } + return *ret = v, (const char *)buf - orig; + } + + /* 0x04000000 - 0x7FFFFFFF: + 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx + */ + if (0x7e == (i >> 1)) { + if (len < 6) + return -1; + v = i & 0x01; + for (int j = 0; j < 5; j++) { + i = *buf++; + if (0x2 != (i >> 6)) + return -1; + v = (v << 6) | (i & 0x3f); + } + return *ret = v, (const char *)buf - orig; + } + return -1; +} + +/** + * Convert a UCS char to utf8 code, and return it in buf. + * Return #bytes used in buf to encode the char, or + * -1 on error. + */ +int toml_ucs_to_utf8(int64_t code, char buf[6]) { + /* http://stackoverflow.com/questions/6240055/manually-converting-unicode-codepoints-into-utf-8-and-utf-16 + */ + /* The UCS code values 0xd800–0xdfff (UTF-16 surrogates) as well + * as 0xfffe and 0xffff (UCS noncharacters) should not appear in + * conforming UTF-8 streams. + */ + if (0xd800 <= code && code <= 0xdfff) + return -1; + if (0xfffe <= code && code <= 0xffff) + return -1; + + /* 0x00000000 - 0x0000007F: + 0xxxxxxx + */ + if (code < 0) + return -1; + if (code <= 0x7F) { + buf[0] = (unsigned char)code; + return 1; + } + + /* 0x00000080 - 0x000007FF: + 110xxxxx 10xxxxxx + */ + if (code <= 0x000007FF) { + buf[0] = (unsigned char) (0xc0 | (code >> 6)); + buf[1] = (unsigned char) (0x80 | (code & 0x3f)); + return 2; + } + + /* 0x00000800 - 0x0000FFFF: + 1110xxxx 10xxxxxx 10xxxxxx + */ + if (code <= 0x0000FFFF) { + buf[0] = (unsigned char) (0xe0 | (code >> 12)); + buf[1] = (unsigned char) (0x80 | ((code >> 6) & 0x3f)); + buf[2] = (unsigned char) (0x80 | (code & 0x3f)); + return 3; + } + + /* 0x00010000 - 0x001FFFFF: + 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx + */ + if (code <= 0x001FFFFF) { + buf[0] = (unsigned char) (0xf0 | (code >> 18)); + buf[1] = (unsigned char) (0x80 | ((code >> 12) & 0x3f)); + buf[2] = (unsigned char) (0x80 | ((code >> 6) & 0x3f)); + buf[3] = (unsigned char) (0x80 | (code & 0x3f)); + return 4; + } + + /* 0x00200000 - 0x03FFFFFF: + 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx + */ + if (code <= 0x03FFFFFF) { + buf[0] = (unsigned char) (0xf8 | (code >> 24)); + buf[1] = (unsigned char) (0x80 | ((code >> 18) & 0x3f)); + buf[2] = (unsigned char) (0x80 | ((code >> 12) & 0x3f)); + buf[3] = (unsigned char) (0x80 | ((code >> 6) & 0x3f)); + buf[4] = (unsigned char) (0x80 | (code & 0x3f)); + return 5; + } + + /* 0x04000000 - 0x7FFFFFFF: + 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx + */ + if (code <= 0x7FFFFFFF) { + buf[0] = (unsigned char) (0xfc | (code >> 30)); + buf[1] = (unsigned char) (0x80 | ((code >> 24) & 0x3f)); + buf[2] = (unsigned char) (0x80 | ((code >> 18) & 0x3f)); + buf[3] = (unsigned char) (0x80 | ((code >> 12) & 0x3f)); + buf[4] = (unsigned char) (0x80 | ((code >> 6) & 0x3f)); + buf[5] = (unsigned char) (0x80 | (code & 0x3f)); + return 6; + } + + return -1; +} + +/* + * TOML has 3 data structures: value, array, table. + * Each of them can have identification key. + */ +typedef struct toml_keyval_t toml_keyval_t; +struct toml_keyval_t { + const char *key; /* key to this value */ + const char *val; /* the raw value */ +}; + +typedef struct toml_arritem_t toml_arritem_t; +struct toml_arritem_t { + int valtype; /* for value kind: 'i'nt, 'd'ouble, 'b'ool, 's'tring, 't'ime, + 'D'ate, 'T'imestamp */ + char *val; + toml_array_t *arr; + toml_table_t *tab; +}; + +struct toml_array_t { + const char *key; /* key to this array */ + int kind; /* element kind: 'v'alue, 'a'rray, or 't'able, 'm'ixed */ + int type; /* for value kind: 'i'nt, 'd'ouble, 'b'ool, 's'tring, 't'ime, + 'D'ate, 'T'imestamp, 'm'ixed */ + + int nitem; /* number of elements */ + toml_arritem_t *item; +}; + +struct toml_table_t { + const char *key; /* key to this table */ + bool implicit; /* table was created implicitly */ + bool readonly; /* no more modification allowed */ + + /* key-values in the table */ + int nkval; + toml_keyval_t **kval; + + /* arrays in the table */ + int narr; + toml_array_t **arr; + + /* tables in the table */ + int ntab; + toml_table_t **tab; +}; + +static inline void xfree(const void *x) { + if (x) + FREE((void *)(intptr_t)x); +} + +enum tokentype_t { + INVALID, + DOT, + COMMA, + EQUAL, + LBRACE, + RBRACE, + NEWLINE, + LBRACKET, + RBRACKET, + STRING, +}; +typedef enum tokentype_t tokentype_t; + +typedef struct token_t token_t; +struct token_t { + tokentype_t tok; + int lineno; + char *ptr; /* points into context->start */ + int len; + int eof; +}; + +typedef struct context_t context_t; +struct context_t { + char *start; + char *stop; + char *errbuf; + int errbufsz; + + token_t tok; + toml_table_t *root; + toml_table_t *curtab; + + struct { + int top; + char *key[10]; + token_t tok[10]; + } tpath; +}; + +#define STRINGIFY(x) #x +#define TOSTRING(x) STRINGIFY(x) +#define FLINE __FILE__ ":" TOSTRING(__LINE__) + +static int next_token(context_t *ctx, int dotisspecial); + +/* + Error reporting. Call when an error is detected. Always return -1. +*/ +static int e_outofmemory(context_t *ctx, const char *fline) { + snprintf(ctx->errbuf, ctx->errbufsz, "ERROR: out of memory (%s)", fline); + return -1; +} + +static int e_internal(context_t *ctx, const char *fline) { + snprintf(ctx->errbuf, ctx->errbufsz, "internal error (%s)", fline); + return -1; +} + +static int e_syntax(context_t *ctx, int lineno, const char *msg) { + snprintf(ctx->errbuf, ctx->errbufsz, "line %d: %s", lineno, msg); + return -1; +} + +static int e_badkey(context_t *ctx, int lineno) { + snprintf(ctx->errbuf, ctx->errbufsz, "line %d: bad key", lineno); + return -1; +} + +static int e_keyexists(context_t *ctx, int lineno) { + snprintf(ctx->errbuf, ctx->errbufsz, "line %d: key exists", lineno); + return -1; +} + +static int e_forbid(context_t *ctx, int lineno, const char *msg) { + snprintf(ctx->errbuf, ctx->errbufsz, "line %d: %s", lineno, msg); + return -1; +} + +static void *expand(void *p, int sz, int newsz) { + void *s = MALLOC(newsz); + if (!s) + return 0; + + memcpy(s, p, sz); + FREE(p); + return s; +} + +static void **expand_ptrarr(void **p, int n) { + void **s = MALLOC((n + 1) * sizeof(void *)); + if (!s) + return 0; + + s[n] = 0; + memcpy(s, p, n * sizeof(void *)); + FREE(p); + return s; +} + +static toml_arritem_t *expand_arritem(toml_arritem_t *p, int n) { + toml_arritem_t *pp = expand(p, n * sizeof(*p), (n + 1) * sizeof(*p)); + if (!pp) + return 0; + + memset(&pp[n], 0, sizeof(pp[n])); + return pp; +} + +static char *norm_lit_str(const char *src, int srclen, int multiline, + char *errbuf, int errbufsz) { + char *dst = 0; /* will write to dst[] and return it */ + int max = 0; /* max size of dst[] */ + int off = 0; /* cur offset in dst[] */ + const char *sp = src; + const char *sq = src + srclen; + int ch; + + /* scan forward on src */ + for (;;) { + if (off >= max - 10) { /* have some slack for misc stuff */ + int newmax = max + 50; + char *x = expand(dst, max, newmax); + if (!x) { + xfree(dst); + snprintf(errbuf, errbufsz, "out of memory"); + return 0; + } + dst = x; + max = newmax; + } + + /* finished? */ + if (sp >= sq) + break; + + ch = *sp++; + /* control characters other than tab is not allowed */ + if ((0 <= ch && ch <= 0x08) || (0x0a <= ch && ch <= 0x1f) || (ch == 0x7f)) { + if (!(multiline && (ch == '\r' || ch == '\n'))) { + xfree(dst); + snprintf(errbuf, errbufsz, "invalid char U+%04x", ch); + return 0; + } + } + + // a plain copy suffice + dst[off++] = ch; + } + + dst[off++] = 0; + return dst; +} + +/* + * Convert src to raw unescaped utf-8 string. + * Returns NULL if error with errmsg in errbuf. + */ +static char *norm_basic_str(const char *src, int srclen, int multiline, + char *errbuf, int errbufsz) { + char *dst = 0; /* will write to dst[] and return it */ + int max = 0; /* max size of dst[] */ + int off = 0; /* cur offset in dst[] */ + const char *sp = src; + const char *sq = src + srclen; + int ch; + + /* scan forward on src */ + for (;;) { + if (off >= max - 10) { /* have some slack for misc stuff */ + int newmax = max + 50; + char *x = expand(dst, max, newmax); + if (!x) { + xfree(dst); + snprintf(errbuf, errbufsz, "out of memory"); + return 0; + } + dst = x; + max = newmax; + } + + /* finished? */ + if (sp >= sq) + break; + + ch = *sp++; + if (ch != '\\') { + /* these chars must be escaped: U+0000 to U+0008, U+000A to U+001F, U+007F + */ + if ((0 <= ch && ch <= 0x08) || (0x0a <= ch && ch <= 0x1f) || + (ch == 0x7f)) { + if (!(multiline && (ch == '\r' || ch == '\n'))) { + xfree(dst); + snprintf(errbuf, errbufsz, "invalid char U+%04x", ch); + return 0; + } + } + + // a plain copy suffice + dst[off++] = ch; + continue; + } + + /* ch was backslash. we expect the escape char. */ + if (sp >= sq) { + snprintf(errbuf, errbufsz, "last backslash is invalid"); + xfree(dst); + return 0; + } + + /* for multi-line, we want to kill line-ending-backslash ... */ + if (multiline) { + + // if there is only whitespace after the backslash ... + if (sp[strspn(sp, " \t\r")] == '\n') { + /* skip all the following whitespaces */ + sp += strspn(sp, " \t\r\n"); + continue; + } + } + + /* get the escaped char */ + ch = *sp++; + switch (ch) { + case 'u': + case 'U': { + int64_t ucs = 0; + int nhex = (ch == 'u' ? 4 : 8); + for (int i = 0; i < nhex; i++) { + if (sp >= sq) { + snprintf(errbuf, errbufsz, "\\%c expects %d hex chars", ch, nhex); + xfree(dst); + return 0; + } + ch = *sp++; + int v = ('0' <= ch && ch <= '9') + ? ch - '0' + : (('A' <= ch && ch <= 'F') ? ch - 'A' + 10 : -1); + if (-1 == v) { + snprintf(errbuf, errbufsz, "invalid hex chars for \\u or \\U"); + xfree(dst); + return 0; + } + ucs = ucs * 16 + v; + } + int n = toml_ucs_to_utf8(ucs, &dst[off]); + if (-1 == n) { + snprintf(errbuf, errbufsz, "illegal ucs code in \\u or \\U"); + xfree(dst); + return 0; + } + off += n; + } + continue; + + case 'b': + ch = '\b'; + break; + case 't': + ch = '\t'; + break; + case 'n': + ch = '\n'; + break; + case 'f': + ch = '\f'; + break; + case 'r': + ch = '\r'; + break; + case '"': + ch = '"'; + break; + case '\\': + ch = '\\'; + break; + default: + snprintf(errbuf, errbufsz, "illegal escape char \\%c", ch); + xfree(dst); + return 0; + } + + dst[off++] = ch; + } + + // Cap with NUL and return it. + dst[off++] = 0; + return dst; +} + +/* Normalize a key. Convert all special chars to raw unescaped utf-8 chars. */ +static char *normalize_key(context_t *ctx, token_t strtok) { + const char *sp = strtok.ptr; + const char *sq = strtok.ptr + strtok.len; + int lineno = strtok.lineno; + char *ret; + int ch = *sp; + char ebuf[80]; + + /* handle quoted string */ + if (ch == '\'' || ch == '\"') { + /* if ''' or """, take 3 chars off front and back. Else, take 1 char off. */ + int multiline = 0; + if (sp[1] == ch && sp[2] == ch) { + sp += 3, sq -= 3; + multiline = 1; + } else + sp++, sq--; + + if (ch == '\'') { + /* for single quote, take it verbatim. */ + if (!(ret = STRNDUP(sp, sq - sp))) { + e_outofmemory(ctx, FLINE); + return 0; + } + } else { + /* for double quote, we need to normalize */ + ret = norm_basic_str(sp, sq - sp, multiline, ebuf, sizeof(ebuf)); + if (!ret) { + e_syntax(ctx, lineno, ebuf); + return 0; + } + } + + /* newlines are not allowed in keys */ + if (strchr(ret, '\n')) { + xfree(ret); + e_badkey(ctx, lineno); + return 0; + } + return ret; + } + + /* for bare-key allow only this regex: [A-Za-z0-9_-]+ */ + const char *xp; + for (xp = sp; xp != sq; xp++) { + int k = *xp; + if (isalnum(k)) + continue; + if (k == '_' || k == '-') + continue; + e_badkey(ctx, lineno); + return 0; + } + + /* dup and return it */ + if (!(ret = STRNDUP(sp, sq - sp))) { + e_outofmemory(ctx, FLINE); + return 0; + } + return ret; +} + +/* + * Look up key in tab. Return 0 if not found, or + * 'v'alue, 'a'rray or 't'able depending on the element. + */ +static int check_key(toml_table_t *tab, const char *key, + toml_keyval_t **ret_val, toml_array_t **ret_arr, + toml_table_t **ret_tab) { + int i; + void *dummy; + + if (!ret_tab) + ret_tab = (toml_table_t **)&dummy; + if (!ret_arr) + ret_arr = (toml_array_t **)&dummy; + if (!ret_val) + ret_val = (toml_keyval_t **)&dummy; + + *ret_tab = 0; + *ret_arr = 0; + *ret_val = 0; + + for (i = 0; i < tab->nkval; i++) { + if (0 == strcmp(key, tab->kval[i]->key)) { + *ret_val = tab->kval[i]; + return 'v'; + } + } + for (i = 0; i < tab->narr; i++) { + if (0 == strcmp(key, tab->arr[i]->key)) { + *ret_arr = tab->arr[i]; + return 'a'; + } + } + for (i = 0; i < tab->ntab; i++) { + if (0 == strcmp(key, tab->tab[i]->key)) { + *ret_tab = tab->tab[i]; + return 't'; + } + } + return 0; +} + +static int key_kind(toml_table_t *tab, const char *key) { + return check_key(tab, key, 0, 0, 0); +} + +/* Create a keyval in the table. + */ +static toml_keyval_t *create_keyval_in_table(context_t *ctx, toml_table_t *tab, + token_t keytok) { + /* first, normalize the key to be used for lookup. + * remember to free it if we error out. + */ + char *newkey = normalize_key(ctx, keytok); + if (!newkey) + return 0; + + /* if key exists: error out. */ + toml_keyval_t *dest = 0; + if (key_kind(tab, newkey)) { + xfree(newkey); + e_keyexists(ctx, keytok.lineno); + return 0; + } + + /* make a new entry */ + int n = tab->nkval; + toml_keyval_t **base; + if (0 == (base = (toml_keyval_t **)expand_ptrarr((void **)tab->kval, n))) { + xfree(newkey); + e_outofmemory(ctx, FLINE); + return 0; + } + tab->kval = base; + + if (0 == (base[n] = (toml_keyval_t *)CALLOC(1, sizeof(*base[n])))) { + xfree(newkey); + e_outofmemory(ctx, FLINE); + return 0; + } + dest = tab->kval[tab->nkval++]; + + /* save the key in the new value struct */ + dest->key = newkey; + return dest; +} + +/* Create a table in the table. + */ +static toml_table_t *create_keytable_in_table(context_t *ctx, toml_table_t *tab, + token_t keytok) { + /* first, normalize the key to be used for lookup. + * remember to free it if we error out. + */ + char *newkey = normalize_key(ctx, keytok); + if (!newkey) + return 0; + + /* if key exists: error out */ + toml_table_t *dest = 0; + if (check_key(tab, newkey, 0, 0, &dest)) { + xfree(newkey); /* don't need this anymore */ + + /* special case: if table exists, but was created implicitly ... */ + if (dest && dest->implicit) { + /* we make it explicit now, and simply return it. */ + dest->implicit = false; + return dest; + } + e_keyexists(ctx, keytok.lineno); + return 0; + } + + /* create a new table entry */ + int n = tab->ntab; + toml_table_t **base; + if (0 == (base = (toml_table_t **)expand_ptrarr((void **)tab->tab, n))) { + xfree(newkey); + e_outofmemory(ctx, FLINE); + return 0; + } + tab->tab = base; + + if (0 == (base[n] = (toml_table_t *)CALLOC(1, sizeof(*base[n])))) { + xfree(newkey); + e_outofmemory(ctx, FLINE); + return 0; + } + dest = tab->tab[tab->ntab++]; + + /* save the key in the new table struct */ + dest->key = newkey; + return dest; +} + +/* Create an array in the table. + */ +static toml_array_t *create_keyarray_in_table(context_t *ctx, toml_table_t *tab, + token_t keytok, char kind) { + /* first, normalize the key to be used for lookup. + * remember to free it if we error out. + */ + char *newkey = normalize_key(ctx, keytok); + if (!newkey) + return 0; + + /* if key exists: error out */ + if (key_kind(tab, newkey)) { + xfree(newkey); /* don't need this anymore */ + e_keyexists(ctx, keytok.lineno); + return 0; + } + + /* make a new array entry */ + int n = tab->narr; + toml_array_t **base; + if (0 == (base = (toml_array_t **)expand_ptrarr((void **)tab->arr, n))) { + xfree(newkey); + e_outofmemory(ctx, FLINE); + return 0; + } + tab->arr = base; + + if (0 == (base[n] = (toml_array_t *)CALLOC(1, sizeof(*base[n])))) { + xfree(newkey); + e_outofmemory(ctx, FLINE); + return 0; + } + toml_array_t *dest = tab->arr[tab->narr++]; + + /* save the key in the new array struct */ + dest->key = newkey; + dest->kind = kind; + return dest; +} + +static toml_arritem_t *create_value_in_array(context_t *ctx, + toml_array_t *parent) { + const int n = parent->nitem; + toml_arritem_t *base = expand_arritem(parent->item, n); + if (!base) { + e_outofmemory(ctx, FLINE); + return 0; + } + parent->item = base; + parent->nitem++; + return &parent->item[n]; +} + +/* Create an array in an array + */ +static toml_array_t *create_array_in_array(context_t *ctx, + toml_array_t *parent) { + const int n = parent->nitem; + toml_arritem_t *base = expand_arritem(parent->item, n); + if (!base) { + e_outofmemory(ctx, FLINE); + return 0; + } + toml_array_t *ret = (toml_array_t *)CALLOC(1, sizeof(toml_array_t)); + if (!ret) { + e_outofmemory(ctx, FLINE); + return 0; + } + base[n].arr = ret; + parent->item = base; + parent->nitem++; + return ret; +} + +/* Create a table in an array + */ +static toml_table_t *create_table_in_array(context_t *ctx, + toml_array_t *parent) { + int n = parent->nitem; + toml_arritem_t *base = expand_arritem(parent->item, n); + if (!base) { + e_outofmemory(ctx, FLINE); + return 0; + } + toml_table_t *ret = (toml_table_t *)CALLOC(1, sizeof(toml_table_t)); + if (!ret) { + e_outofmemory(ctx, FLINE); + return 0; + } + base[n].tab = ret; + parent->item = base; + parent->nitem++; + return ret; +} + +static int skip_newlines(context_t *ctx, int isdotspecial) { + while (ctx->tok.tok == NEWLINE) { + if (next_token(ctx, isdotspecial)) + return -1; + if (ctx->tok.eof) + break; + } + return 0; +} + +static int parse_keyval(context_t *ctx, toml_table_t *tab); + +static inline int eat_token(context_t *ctx, tokentype_t typ, int isdotspecial, + const char *fline) { + if (ctx->tok.tok != typ) + return e_internal(ctx, fline); + + if (next_token(ctx, isdotspecial)) + return -1; + + return 0; +} + +/* We are at '{ ... }'. + * Parse the table. + */ +static int parse_inline_table(context_t *ctx, toml_table_t *tab) { + if (eat_token(ctx, LBRACE, 1, FLINE)) + return -1; + + for (;;) { + if (ctx->tok.tok == NEWLINE) + return e_syntax(ctx, ctx->tok.lineno, + "newline not allowed in inline table"); + + /* until } */ + if (ctx->tok.tok == RBRACE) + break; + + if (ctx->tok.tok != STRING) + return e_syntax(ctx, ctx->tok.lineno, "expect a string"); + + if (parse_keyval(ctx, tab)) + return -1; + + if (ctx->tok.tok == NEWLINE) + return e_syntax(ctx, ctx->tok.lineno, + "newline not allowed in inline table"); + + /* on comma, continue to scan for next keyval */ + if (ctx->tok.tok == COMMA) { + if (eat_token(ctx, COMMA, 1, FLINE)) + return -1; + continue; + } + break; + } + + if (eat_token(ctx, RBRACE, 1, FLINE)) + return -1; + + tab->readonly = 1; + + return 0; +} + +static int valtype(const char *val) { + toml_timestamp_t ts; + if (*val == '\'' || *val == '"') + return 's'; + if (0 == toml_rtob(val, 0)) + return 'b'; + if (0 == toml_rtoi(val, 0)) + return 'i'; + if (0 == toml_rtod(val, 0)) + return 'd'; + if (0 == toml_rtots(val, &ts)) { + if (ts.year && ts.hour) + return 'T'; /* timestamp */ + if (ts.year) + return 'D'; /* date */ + return 't'; /* time */ + } + return 'u'; /* unknown */ +} + +/* We are at '[...]' */ +static int parse_array(context_t *ctx, toml_array_t *arr) { + if (eat_token(ctx, LBRACKET, 0, FLINE)) + return -1; + + for (;;) { + if (skip_newlines(ctx, 0)) + return -1; + + /* until ] */ + if (ctx->tok.tok == RBRACKET) + break; + + switch (ctx->tok.tok) { + case STRING: { + /* set array kind if this will be the first entry */ + if (arr->kind == 0) + arr->kind = 'v'; + else if (arr->kind != 'v') + arr->kind = 'm'; + + char *val = ctx->tok.ptr; + int vlen = ctx->tok.len; + + /* make a new value in array */ + toml_arritem_t *newval = create_value_in_array(ctx, arr); + if (!newval) + return e_outofmemory(ctx, FLINE); + + if (!(newval->val = STRNDUP(val, vlen))) + return e_outofmemory(ctx, FLINE); + + newval->valtype = valtype(newval->val); + + /* set array type if this is the first entry */ + if (arr->nitem == 1) + arr->type = newval->valtype; + else if (arr->type != newval->valtype) + arr->type = 'm'; /* mixed */ + + if (eat_token(ctx, STRING, 0, FLINE)) + return -1; + break; + } + + case LBRACKET: { /* [ [array], [array] ... ] */ + /* set the array kind if this will be the first entry */ + if (arr->kind == 0) + arr->kind = 'a'; + else if (arr->kind != 'a') + arr->kind = 'm'; + + toml_array_t *subarr = create_array_in_array(ctx, arr); + if (!subarr) + return -1; + if (parse_array(ctx, subarr)) + return -1; + break; + } + + case LBRACE: { /* [ {table}, {table} ... ] */ + /* set the array kind if this will be the first entry */ + if (arr->kind == 0) + arr->kind = 't'; + else if (arr->kind != 't') + arr->kind = 'm'; + + toml_table_t *subtab = create_table_in_array(ctx, arr); + if (!subtab) + return -1; + if (parse_inline_table(ctx, subtab)) + return -1; + break; + } + + default: + return e_syntax(ctx, ctx->tok.lineno, "syntax error"); + } + + if (skip_newlines(ctx, 0)) + return -1; + + /* on comma, continue to scan for next element */ + if (ctx->tok.tok == COMMA) { + if (eat_token(ctx, COMMA, 0, FLINE)) + return -1; + continue; + } + break; + } + + if (eat_token(ctx, RBRACKET, 1, FLINE)) + return -1; + return 0; +} + +/* handle lines like these: + key = "value" + key = [ array ] + key = { table } +*/ +static int parse_keyval(context_t *ctx, toml_table_t *tab) { + if (tab->readonly) { + return e_forbid(ctx, ctx->tok.lineno, + "cannot insert new entry into existing table"); + } + + token_t key = ctx->tok; + if (eat_token(ctx, STRING, 1, FLINE)) + return -1; + + if (ctx->tok.tok == DOT) { + /* handle inline dotted key. + e.g. + physical.color = "orange" + physical.shape = "round" + */ + toml_table_t *subtab = 0; + { + char *subtabstr = normalize_key(ctx, key); + if (!subtabstr) + return -1; + + subtab = toml_table_in(tab, subtabstr); + xfree(subtabstr); + } + if (!subtab) { + subtab = create_keytable_in_table(ctx, tab, key); + if (!subtab) + return -1; + } + if (next_token(ctx, 1)) + return -1; + if (parse_keyval(ctx, subtab)) + return -1; + return 0; + } + + if (ctx->tok.tok != EQUAL) { + return e_syntax(ctx, ctx->tok.lineno, "missing ="); + } + + if (next_token(ctx, 0)) + return -1; + + switch (ctx->tok.tok) { + case STRING: { /* key = "value" */ + toml_keyval_t *keyval = create_keyval_in_table(ctx, tab, key); + if (!keyval) + return -1; + token_t val = ctx->tok; + + assert(keyval->val == 0); + if (!(keyval->val = STRNDUP(val.ptr, val.len))) + return e_outofmemory(ctx, FLINE); + + if (next_token(ctx, 1)) + return -1; + + return 0; + } + + case LBRACKET: { /* key = [ array ] */ + toml_array_t *arr = create_keyarray_in_table(ctx, tab, key, 0); + if (!arr) + return -1; + if (parse_array(ctx, arr)) + return -1; + return 0; + } + + case LBRACE: { /* key = { table } */ + toml_table_t *nxttab = create_keytable_in_table(ctx, tab, key); + if (!nxttab) + return -1; + if (parse_inline_table(ctx, nxttab)) + return -1; + return 0; + } + + default: + return e_syntax(ctx, ctx->tok.lineno, "syntax error"); + } + return 0; +} + +typedef struct tabpath_t tabpath_t; +struct tabpath_t { + int cnt; + token_t key[10]; +}; + +/* at [x.y.z] or [[x.y.z]] + * Scan forward and fill tabpath until it enters ] or ]] + * There will be at least one entry on return. + */ +static int fill_tabpath(context_t *ctx) { + int lineno = ctx->tok.lineno; + int i; + + /* clear tpath */ + for (i = 0; i < ctx->tpath.top; i++) { + char **p = &ctx->tpath.key[i]; + xfree(*p); + *p = 0; + } + ctx->tpath.top = 0; + + for (;;) { + if (ctx->tpath.top >= 10) + return e_syntax(ctx, lineno, + "table path is too deep; max allowed is 10."); + + if (ctx->tok.tok != STRING) + return e_syntax(ctx, lineno, "invalid or missing key"); + + char *key = normalize_key(ctx, ctx->tok); + if (!key) + return -1; + ctx->tpath.tok[ctx->tpath.top] = ctx->tok; + ctx->tpath.key[ctx->tpath.top] = key; + ctx->tpath.top++; + + if (next_token(ctx, 1)) + return -1; + + if (ctx->tok.tok == RBRACKET) + break; + + if (ctx->tok.tok != DOT) + return e_syntax(ctx, lineno, "invalid key"); + + if (next_token(ctx, 1)) + return -1; + } + + if (ctx->tpath.top <= 0) + return e_syntax(ctx, lineno, "empty table selector"); + + return 0; +} + +/* Walk tabpath from the root, and create new tables on the way. + * Sets ctx->curtab to the final table. + */ +static int walk_tabpath(context_t *ctx) { + /* start from root */ + toml_table_t *curtab = ctx->root; + + for (int i = 0; i < ctx->tpath.top; i++) { + const char *key = ctx->tpath.key[i]; + + toml_keyval_t *nextval = 0; + toml_array_t *nextarr = 0; + toml_table_t *nexttab = 0; + switch (check_key(curtab, key, &nextval, &nextarr, &nexttab)) { + case 't': + /* found a table. nexttab is where we will go next. */ + break; + + case 'a': + /* found an array. nexttab is the last table in the array. */ + if (nextarr->kind != 't') + return e_internal(ctx, FLINE); + + if (nextarr->nitem == 0) + return e_internal(ctx, FLINE); + + nexttab = nextarr->item[nextarr->nitem - 1].tab; + break; + + case 'v': + return e_keyexists(ctx, ctx->tpath.tok[i].lineno); + + default: { /* Not found. Let's create an implicit table. */ + int n = curtab->ntab; + toml_table_t **base = + (toml_table_t **)expand_ptrarr((void **)curtab->tab, n); + if (0 == base) + return e_outofmemory(ctx, FLINE); + + curtab->tab = base; + + if (0 == (base[n] = (toml_table_t *)CALLOC(1, sizeof(*base[n])))) + return e_outofmemory(ctx, FLINE); + + if (0 == (base[n]->key = STRDUP(key))) + return e_outofmemory(ctx, FLINE); + + nexttab = curtab->tab[curtab->ntab++]; + + /* tabs created by walk_tabpath are considered implicit */ + nexttab->implicit = true; + } break; + } + + /* switch to next tab */ + curtab = nexttab; + } + + /* save it */ + ctx->curtab = curtab; + + return 0; +} + +/* handle lines like [x.y.z] or [[x.y.z]] */ +static int parse_select(context_t *ctx) { + assert(ctx->tok.tok == LBRACKET); + + /* true if [[ */ + int llb = (ctx->tok.ptr + 1 < ctx->stop && ctx->tok.ptr[1] == '['); + /* need to detect '[[' on our own because next_token() will skip whitespace, + and '[ [' would be taken as '[[', which is wrong. */ + + /* eat [ or [[ */ + if (eat_token(ctx, LBRACKET, 1, FLINE)) + return -1; + if (llb) { + assert(ctx->tok.tok == LBRACKET); + if (eat_token(ctx, LBRACKET, 1, FLINE)) + return -1; + } + + if (fill_tabpath(ctx)) + return -1; + + /* For [x.y.z] or [[x.y.z]], remove z from tpath. + */ + token_t z = ctx->tpath.tok[ctx->tpath.top - 1]; + xfree(ctx->tpath.key[ctx->tpath.top - 1]); + ctx->tpath.top--; + + /* set up ctx->curtab */ + if (walk_tabpath(ctx)) + return -1; + + if (!llb) { + /* [x.y.z] -> create z = {} in x.y */ + toml_table_t *curtab = create_keytable_in_table(ctx, ctx->curtab, z); + if (!curtab) + return -1; + ctx->curtab = curtab; + } else { + /* [[x.y.z]] -> create z = [] in x.y */ + toml_array_t *arr = 0; + { + char *zstr = normalize_key(ctx, z); + if (!zstr) + return -1; + arr = toml_array_in(ctx->curtab, zstr); + xfree(zstr); + } + if (!arr) { + arr = create_keyarray_in_table(ctx, ctx->curtab, z, 't'); + if (!arr) + return -1; + } + if (arr->kind != 't') + return e_syntax(ctx, z.lineno, "array mismatch"); + + /* add to z[] */ + toml_table_t *dest; + { + toml_table_t *t = create_table_in_array(ctx, arr); + if (!t) + return -1; + + if (0 == (t->key = STRDUP("__anon__"))) + return e_outofmemory(ctx, FLINE); + + dest = t; + } + + ctx->curtab = dest; + } + + if (ctx->tok.tok != RBRACKET) { + return e_syntax(ctx, ctx->tok.lineno, "expects ]"); + } + if (llb) { + if (!(ctx->tok.ptr + 1 < ctx->stop && ctx->tok.ptr[1] == ']')) { + return e_syntax(ctx, ctx->tok.lineno, "expects ]]"); + } + if (eat_token(ctx, RBRACKET, 1, FLINE)) + return -1; + } + + if (eat_token(ctx, RBRACKET, 1, FLINE)) + return -1; + + if (ctx->tok.tok != NEWLINE) + return e_syntax(ctx, ctx->tok.lineno, "extra chars after ] or ]]"); + + return 0; +} + +toml_table_t *toml_parse(char *conf, char *errbuf, int errbufsz) { + context_t ctx; + + // clear errbuf + if (errbufsz <= 0) + errbufsz = 0; + if (errbufsz > 0) + errbuf[0] = 0; + + // init context + memset(&ctx, 0, sizeof(ctx)); + ctx.start = conf; + ctx.stop = ctx.start + strlen(conf); + ctx.errbuf = errbuf; + ctx.errbufsz = errbufsz; + + // start with an artificial newline of length 0 + ctx.tok.tok = NEWLINE; + ctx.tok.lineno = 1; + ctx.tok.ptr = conf; + ctx.tok.len = 0; + + // make a root table + if (0 == (ctx.root = CALLOC(1, sizeof(*ctx.root)))) { + e_outofmemory(&ctx, FLINE); + // Do not goto fail, root table not set up yet + return 0; + } + + // set root as default table + ctx.curtab = ctx.root; + + /* Scan forward until EOF */ + for (token_t tok = ctx.tok; !tok.eof; tok = ctx.tok) { + switch (tok.tok) { + + case NEWLINE: + if (next_token(&ctx, 1)) + goto fail; + break; + + case STRING: + if (parse_keyval(&ctx, ctx.curtab)) + goto fail; + + if (ctx.tok.tok != NEWLINE) { + e_syntax(&ctx, ctx.tok.lineno, "extra chars after value"); + goto fail; + } + + if (eat_token(&ctx, NEWLINE, 1, FLINE)) + goto fail; + break; + + case LBRACKET: /* [ x.y.z ] or [[ x.y.z ]] */ + if (parse_select(&ctx)) + goto fail; + break; + + default: + e_syntax(&ctx, tok.lineno, "syntax error"); + goto fail; + } + } + + /* success */ + for (int i = 0; i < ctx.tpath.top; i++) + xfree(ctx.tpath.key[i]); + return ctx.root; + +fail: + // Something bad has happened. Free resources and return error. + for (int i = 0; i < ctx.tpath.top; i++) + xfree(ctx.tpath.key[i]); + toml_free(ctx.root); + return 0; +} + +toml_table_t *toml_parse_file(FILE *fp, char *errbuf, int errbufsz) { + int bufsz = 0; + char *buf = 0; + int off = 0; + + /* read from fp into buf */ + while (!feof(fp)) { + + if (off == bufsz) { + int xsz = bufsz + 1000; + char *x = expand(buf, bufsz, xsz); + if (!x) { + snprintf(errbuf, errbufsz, "out of memory"); + xfree(buf); + return 0; + } + buf = x; + bufsz = xsz; + } + + errno = 0; + int n = fread(buf + off, 1, bufsz - off, fp); + if (ferror(fp)) { + snprintf(errbuf, errbufsz, "%s", + errno ? strerror(errno) : "Error reading file"); + xfree(buf); + return 0; + } + off += n; + } + + /* tag on a NUL to cap the string */ + if (off == bufsz) { + int xsz = bufsz + 1; + char *x = expand(buf, bufsz, xsz); + if (!x) { + snprintf(errbuf, errbufsz, "out of memory"); + xfree(buf); + return 0; + } + buf = x; + bufsz = xsz; + } + buf[off] = 0; + + /* parse it, cleanup and finish */ + toml_table_t *ret = toml_parse(buf, errbuf, errbufsz); + xfree(buf); + return ret; +} + +static void xfree_kval(toml_keyval_t *p) { + if (!p) + return; + xfree(p->key); + xfree(p->val); + xfree(p); +} + +static void xfree_tab(toml_table_t *p); + +static void xfree_arr(toml_array_t *p) { + if (!p) + return; + + xfree(p->key); + const int n = p->nitem; + for (int i = 0; i < n; i++) { + toml_arritem_t *a = &p->item[i]; + if (a->val) + xfree(a->val); + else if (a->arr) + xfree_arr(a->arr); + else if (a->tab) + xfree_tab(a->tab); + } + xfree(p->item); + xfree(p); +} + +static void xfree_tab(toml_table_t *p) { + int i; + + if (!p) + return; + + xfree(p->key); + + for (i = 0; i < p->nkval; i++) + xfree_kval(p->kval[i]); + xfree(p->kval); + + for (i = 0; i < p->narr; i++) + xfree_arr(p->arr[i]); + xfree(p->arr); + + for (i = 0; i < p->ntab; i++) + xfree_tab(p->tab[i]); + xfree(p->tab); + + xfree(p); +} + +void toml_free(toml_table_t *tab) { xfree_tab(tab); } + +static void set_token(context_t *ctx, tokentype_t tok, int lineno, char *ptr, + int len) { + token_t t; + t.tok = tok; + t.lineno = lineno; + t.ptr = ptr; + t.len = len; + t.eof = 0; + ctx->tok = t; +} + +static void set_eof(context_t *ctx, int lineno) { + set_token(ctx, NEWLINE, lineno, ctx->stop, 0); + ctx->tok.eof = 1; +} + +/* Scan p for n digits compositing entirely of [0-9] */ +static int scan_digits(const char *p, int n) { + int ret = 0; + for (; n > 0 && isdigit(*p); n--, p++) { + ret = 10 * ret + (*p - '0'); + } + return n ? -1 : ret; +} + +static int scan_date(const char *p, int *YY, int *MM, int *DD) { + int year, month, day; + year = scan_digits(p, 4); + month = (year >= 0 && p[4] == '-') ? scan_digits(p + 5, 2) : -1; + day = (month >= 0 && p[7] == '-') ? scan_digits(p + 8, 2) : -1; + if (YY) + *YY = year; + if (MM) + *MM = month; + if (DD) + *DD = day; + return (year >= 0 && month >= 0 && day >= 0) ? 0 : -1; +} + +static int scan_time(const char *p, int *hh, int *mm, int *ss) { + int hour, minute, second; + hour = scan_digits(p, 2); + minute = (hour >= 0 && p[2] == ':') ? scan_digits(p + 3, 2) : -1; + second = (minute >= 0 && p[5] == ':') ? scan_digits(p + 6, 2) : -1; + if (hh) + *hh = hour; + if (mm) + *mm = minute; + if (ss) + *ss = second; + return (hour >= 0 && minute >= 0 && second >= 0) ? 0 : -1; +} + +static int scan_string(context_t *ctx, char *p, int lineno, int dotisspecial) { + char *orig = p; + if (0 == strncmp(p, "'''", 3)) { + char *q = p + 3; + + while (1) { + q = strstr(q, "'''"); + if (0 == q) { + return e_syntax(ctx, lineno, "unterminated triple-s-quote"); + } + while (q[3] == '\'') + q++; + break; + } + + set_token(ctx, STRING, lineno, orig, q + 3 - orig); + return 0; + } + + if (0 == strncmp(p, "\"\"\"", 3)) { + char *q = p + 3; + + while (1) { + q = strstr(q, "\"\"\""); + if (0 == q) { + return e_syntax(ctx, lineno, "unterminated triple-d-quote"); + } + if (q[-1] == '\\') { + q++; + continue; + } + while (q[3] == '\"') + q++; + break; + } + + // the string is [p+3, q-1] + + int hexreq = 0; /* #hex required */ + int escape = 0; + for (p += 3; p < q; p++) { + if (escape) { + escape = 0; + if (strchr("btnfr\"\\", *p)) + continue; + if (*p == 'u') { + hexreq = 4; + continue; + } + if (*p == 'U') { + hexreq = 8; + continue; + } + if (p[strspn(p, " \t\r")] == '\n') + continue; /* allow for line ending backslash */ + return e_syntax(ctx, lineno, "bad escape char"); + } + if (hexreq) { + hexreq--; + if (strchr("0123456789ABCDEF", *p)) + continue; + return e_syntax(ctx, lineno, "expect hex char"); + } + if (*p == '\\') { + escape = 1; + continue; + } + } + if (escape) + return e_syntax(ctx, lineno, "expect an escape char"); + if (hexreq) + return e_syntax(ctx, lineno, "expected more hex char"); + + set_token(ctx, STRING, lineno, orig, q + 3 - orig); + return 0; + } + + if ('\'' == *p) { + for (p++; *p && *p != '\n' && *p != '\''; p++) + ; + if (*p != '\'') { + return e_syntax(ctx, lineno, "unterminated s-quote"); + } + + set_token(ctx, STRING, lineno, orig, p + 1 - orig); + return 0; + } + + if ('\"' == *p) { + int hexreq = 0; /* #hex required */ + int escape = 0; + for (p++; *p; p++) { + if (escape) { + escape = 0; + if (strchr("btnfr\"\\", *p)) + continue; + if (*p == 'u') { + hexreq = 4; + continue; + } + if (*p == 'U') { + hexreq = 8; + continue; + } + return e_syntax(ctx, lineno, "bad escape char"); + } + if (hexreq) { + hexreq--; + if (strchr("0123456789ABCDEF", *p)) + continue; + return e_syntax(ctx, lineno, "expect hex char"); + } + if (*p == '\\') { + escape = 1; + continue; + } + if (*p == '\'') { + if (p[1] == '\'' && p[2] == '\'') { + return e_syntax(ctx, lineno, "triple-s-quote inside string lit"); + } + continue; + } + if (*p == '\n') + break; + if (*p == '"') + break; + } + if (*p != '"') { + return e_syntax(ctx, lineno, "unterminated quote"); + } + + set_token(ctx, STRING, lineno, orig, p + 1 - orig); + return 0; + } + + /* check for timestamp without quotes */ + if (0 == scan_date(p, 0, 0, 0) || 0 == scan_time(p, 0, 0, 0)) { + // forward thru the timestamp + p += strspn(p, "0123456789.:+-T Z"); + // squeeze out any spaces at end of string + for (; p[-1] == ' '; p--) + ; + // tokenize + set_token(ctx, STRING, lineno, orig, p - orig); + return 0; + } + + /* literals */ + for (; *p && *p != '\n'; p++) { + int ch = *p; + if (ch == '.' && dotisspecial) + break; + if ('A' <= ch && ch <= 'Z') + continue; + if ('a' <= ch && ch <= 'z') + continue; + if (strchr("0123456789+-_.", ch)) + continue; + break; + } + + set_token(ctx, STRING, lineno, orig, p - orig); + return 0; +} + +static int next_token(context_t *ctx, int dotisspecial) { + int lineno = ctx->tok.lineno; + char *p = ctx->tok.ptr; + int i; + + /* eat this tok */ + for (i = 0; i < ctx->tok.len; i++) { + if (*p++ == '\n') + lineno++; + } + + /* make next tok */ + while (p < ctx->stop) { + /* skip comment. stop just before the \n. */ + if (*p == '#') { + for (p++; p < ctx->stop && *p != '\n'; p++) + ; + continue; + } + + if (dotisspecial && *p == '.') { + set_token(ctx, DOT, lineno, p, 1); + return 0; + } + + switch (*p) { + case ',': + set_token(ctx, COMMA, lineno, p, 1); + return 0; + case '=': + set_token(ctx, EQUAL, lineno, p, 1); + return 0; + case '{': + set_token(ctx, LBRACE, lineno, p, 1); + return 0; + case '}': + set_token(ctx, RBRACE, lineno, p, 1); + return 0; + case '[': + set_token(ctx, LBRACKET, lineno, p, 1); + return 0; + case ']': + set_token(ctx, RBRACKET, lineno, p, 1); + return 0; + case '\n': + set_token(ctx, NEWLINE, lineno, p, 1); + return 0; + case '\r': + case ' ': + case '\t': + /* ignore white spaces */ + p++; + continue; + } + + return scan_string(ctx, p, lineno, dotisspecial); + } + + set_eof(ctx, lineno); + return 0; +} + +const char *toml_key_in(const toml_table_t *tab, int keyidx) { + if (keyidx < tab->nkval) + return tab->kval[keyidx]->key; + + keyidx -= tab->nkval; + if (keyidx < tab->narr) + return tab->arr[keyidx]->key; + + keyidx -= tab->narr; + if (keyidx < tab->ntab) + return tab->tab[keyidx]->key; + + return 0; +} + +int toml_key_exists(const toml_table_t *tab, const char *key) { + int i; + for (i = 0; i < tab->nkval; i++) { + if (0 == strcmp(key, tab->kval[i]->key)) + return 1; + } + for (i = 0; i < tab->narr; i++) { + if (0 == strcmp(key, tab->arr[i]->key)) + return 1; + } + for (i = 0; i < tab->ntab; i++) { + if (0 == strcmp(key, tab->tab[i]->key)) + return 1; + } + return 0; +} + +toml_raw_t toml_raw_in(const toml_table_t *tab, const char *key) { + int i; + for (i = 0; i < tab->nkval; i++) { + if (0 == strcmp(key, tab->kval[i]->key)) + return tab->kval[i]->val; + } + return 0; +} + +toml_array_t *toml_array_in(const toml_table_t *tab, const char *key) { + int i; + for (i = 0; i < tab->narr; i++) { + if (0 == strcmp(key, tab->arr[i]->key)) + return tab->arr[i]; + } + return 0; +} + +toml_table_t *toml_table_in(const toml_table_t *tab, const char *key) { + int i; + for (i = 0; i < tab->ntab; i++) { + if (0 == strcmp(key, tab->tab[i]->key)) + return tab->tab[i]; + } + return 0; +} + +toml_raw_t toml_raw_at(const toml_array_t *arr, int idx) { + return (0 <= idx && idx < arr->nitem) ? arr->item[idx].val : 0; +} + +char toml_array_kind(const toml_array_t *arr) { return arr->kind; } + +char toml_array_type(const toml_array_t *arr) { + if (arr->kind != 'v') + return 0; + + if (arr->nitem == 0) + return 0; + + return arr->type; +} + +int toml_array_nelem(const toml_array_t *arr) { return arr->nitem; } + +const char *toml_array_key(const toml_array_t *arr) { + return arr ? arr->key : (const char *)NULL; +} + +int toml_table_nkval(const toml_table_t *tab) { return tab->nkval; } + +int toml_table_narr(const toml_table_t *tab) { return tab->narr; } + +int toml_table_ntab(const toml_table_t *tab) { return tab->ntab; } + +const char *toml_table_key(const toml_table_t *tab) { + return tab ? tab->key : (const char *)NULL; +} + +toml_array_t *toml_array_at(const toml_array_t *arr, int idx) { + return (0 <= idx && idx < arr->nitem) ? arr->item[idx].arr : 0; +} + +toml_table_t *toml_table_at(const toml_array_t *arr, int idx) { + return (0 <= idx && idx < arr->nitem) ? arr->item[idx].tab : 0; +} + +static int parse_millisec(const char *p, const char **endp); + +int toml_rtots(toml_raw_t src_, toml_timestamp_t *ret) { + if (!src_) + return -1; + + const char *p = src_; + int must_parse_time = 0; + + memset(ret, 0, sizeof(*ret)); + + int *year = &ret->__buffer.year; + int *month = &ret->__buffer.month; + int *day = &ret->__buffer.day; + int *hour = &ret->__buffer.hour; + int *minute = &ret->__buffer.minute; + int *second = &ret->__buffer.second; + int *millisec = &ret->__buffer.millisec; + + /* parse date YYYY-MM-DD */ + if (0 == scan_date(p, year, month, day)) { + ret->year = year; + ret->month = month; + ret->day = day; + + p += 10; + if (*p) { + // parse the T or space separator + if (*p != 'T' && *p != ' ') + return -1; + must_parse_time = 1; + p++; + } + } + + /* parse time HH:MM:SS */ + if (0 == scan_time(p, hour, minute, second)) { + ret->hour = hour; + ret->minute = minute; + ret->second = second; + + /* optionally, parse millisec */ + p += 8; + if (*p == '.') { + p++; /* skip '.' */ + const char *qq; + *millisec = parse_millisec(p, &qq); + ret->millisec = millisec; + p = qq; + } + + if (*p) { + /* parse and copy Z */ + char *z = ret->__buffer.z; + ret->z = z; + if (*p == 'Z' || *p == 'z') { + *z++ = 'Z'; + p++; + *z = 0; + + } else if (*p == '+' || *p == '-') { + *z++ = *p++; + + if (!(isdigit(p[0]) && isdigit(p[1]))) + return -1; + *z++ = *p++; + *z++ = *p++; + + if (*p == ':') { + *z++ = *p++; + + if (!(isdigit(p[0]) && isdigit(p[1]))) + return -1; + *z++ = *p++; + *z++ = *p++; + } + + *z = 0; + } + } + } + if (*p != 0) + return -1; + + if (must_parse_time && !ret->hour) + return -1; + + return 0; +} + +/* Raw to boolean */ +int toml_rtob(toml_raw_t src, int *ret_) { + if (!src) + return -1; + int dummy; + int *ret = ret_ ? ret_ : &dummy; + + if (0 == strcmp(src, "true")) { + *ret = 1; + return 0; + } + if (0 == strcmp(src, "false")) { + *ret = 0; + return 0; + } + return -1; +} + +/* Raw to integer */ +int toml_rtoi(toml_raw_t src, int64_t *ret_) { + if (!src) + return -1; + + char buf[100]; + char *p = buf; + char *q = p + sizeof(buf); + const char *s = src; + int base = 0; + int64_t dummy; + int64_t *ret = ret_ ? ret_ : &dummy; + + /* allow +/- */ + if (s[0] == '+' || s[0] == '-') + *p++ = *s++; + + /* disallow +_100 */ + if (s[0] == '_') + return -1; + + /* if 0* ... */ + if ('0' == s[0]) { + switch (s[1]) { + case 'x': + base = 16; + s += 2; + break; + case 'o': + base = 8; + s += 2; + break; + case 'b': + base = 2; + s += 2; + break; + case '\0': + return *ret = 0, 0; + default: + /* ensure no other digits after it */ + if (s[1]) + return -1; + } + } + + /* just strip underscores and pass to strtoll */ + while (*s && p < q) { + int ch = *s++; + if (ch == '_') { + // disallow '__' + if (s[0] == '_') + return -1; + // numbers cannot end with '_' + if (s[0] == '\0') + return -1; + continue; /* skip _ */ + } + *p++ = ch; + } + + // if not at end-of-string or we ran out of buffer ... + if (*s || p == q) + return -1; + + /* cap with NUL */ + *p = 0; + + /* Run strtoll on buf to get the integer */ + char *endp; + errno = 0; + *ret = strtoll(buf, &endp, base); + return (errno || *endp) ? -1 : 0; +} + +int toml_rtod_ex(toml_raw_t src, double *ret_, char *buf, int buflen) { + if (!src) + return -1; + + char *p = buf; + char *q = p + buflen; + const char *s = src; + double dummy; + double *ret = ret_ ? ret_ : &dummy; + + /* allow +/- */ + if (s[0] == '+' || s[0] == '-') + *p++ = *s++; + + /* disallow +_1.00 */ + if (s[0] == '_') + return -1; + + /* decimal point, if used, must be surrounded by at least one digit on each + * side */ + { + char *dot = strchr(s, '.'); + if (dot) { + if (dot == s || !isdigit(dot[-1]) || !isdigit(dot[1])) + return -1; + } + } + + /* zero must be followed by . or 'e', or NUL */ + if (s[0] == '0' && s[1] && !strchr("eE.", s[1])) + return -1; + + /* just strip underscores and pass to strtod */ + while (*s && p < q) { + int ch = *s++; + if (ch == '_') { + // disallow '__' + if (s[0] == '_') + return -1; + // disallow last char '_' + if (s[0] == 0) + return -1; + continue; /* skip _ */ + } + *p++ = ch; + } + if (*s || p == q) + return -1; /* reached end of string or buffer is full? */ + + /* cap with NUL */ + *p = 0; + + /* Run strtod on buf to get the value */ + char *endp; + errno = 0; + *ret = strtod(buf, &endp); + return (errno || *endp) ? -1 : 0; +} + +int toml_rtod(toml_raw_t src, double *ret_) { + char buf[100]; + return toml_rtod_ex(src, ret_, buf, sizeof(buf)); +} + +int toml_rtos(toml_raw_t src, char **ret) { + int multiline = 0; + const char *sp; + const char *sq; + + *ret = 0; + if (!src) + return -1; + + int qchar = src[0]; + int srclen = strlen(src); + if (!(qchar == '\'' || qchar == '"')) { + return -1; + } + + // triple quotes? + if (qchar == src[1] && qchar == src[2]) { + multiline = 1; + sp = src + 3; + sq = src + srclen - 3; + /* last 3 chars in src must be qchar */ + if (!(sp <= sq && sq[0] == qchar && sq[1] == qchar && sq[2] == qchar)) + return -1; + + /* skip new line immediate after qchar */ + if (sp[0] == '\n') + sp++; + else if (sp[0] == '\r' && sp[1] == '\n') + sp += 2; + + } else { + sp = src + 1; + sq = src + srclen - 1; + /* last char in src must be qchar */ + if (!(sp <= sq && *sq == qchar)) + return -1; + } + + if (qchar == '\'') { + *ret = norm_lit_str(sp, sq - sp, multiline, 0, 0); + } else { + *ret = norm_basic_str(sp, sq - sp, multiline, 0, 0); + } + + return *ret ? 0 : -1; +} + +toml_datum_t toml_string_at(const toml_array_t *arr, int idx) { + toml_datum_t ret; + memset(&ret, 0, sizeof(ret)); + ret.ok = (0 == toml_rtos(toml_raw_at(arr, idx), &ret.u.s)); + return ret; +} + +toml_datum_t toml_bool_at(const toml_array_t *arr, int idx) { + toml_datum_t ret; + memset(&ret, 0, sizeof(ret)); + ret.ok = (0 == toml_rtob(toml_raw_at(arr, idx), &ret.u.b)); + return ret; +} + +toml_datum_t toml_int_at(const toml_array_t *arr, int idx) { + toml_datum_t ret; + memset(&ret, 0, sizeof(ret)); + ret.ok = (0 == toml_rtoi(toml_raw_at(arr, idx), &ret.u.i)); + return ret; +} + +toml_datum_t toml_double_at(const toml_array_t *arr, int idx) { + toml_datum_t ret; + memset(&ret, 0, sizeof(ret)); + ret.ok = (0 == toml_rtod(toml_raw_at(arr, idx), &ret.u.d)); + return ret; +} + +toml_datum_t toml_timestamp_at(const toml_array_t *arr, int idx) { + toml_timestamp_t ts; + toml_datum_t ret; + memset(&ret, 0, sizeof(ret)); + ret.ok = (0 == toml_rtots(toml_raw_at(arr, idx), &ts)); + if (ret.ok) { + ret.ok = !!(ret.u.ts = MALLOC(sizeof(*ret.u.ts))); + if (ret.ok) { + *ret.u.ts = ts; + if (ret.u.ts->year) + ret.u.ts->year = &ret.u.ts->__buffer.year; + if (ret.u.ts->month) + ret.u.ts->month = &ret.u.ts->__buffer.month; + if (ret.u.ts->day) + ret.u.ts->day = &ret.u.ts->__buffer.day; + if (ret.u.ts->hour) + ret.u.ts->hour = &ret.u.ts->__buffer.hour; + if (ret.u.ts->minute) + ret.u.ts->minute = &ret.u.ts->__buffer.minute; + if (ret.u.ts->second) + ret.u.ts->second = &ret.u.ts->__buffer.second; + if (ret.u.ts->millisec) + ret.u.ts->millisec = &ret.u.ts->__buffer.millisec; + if (ret.u.ts->z) + ret.u.ts->z = ret.u.ts->__buffer.z; + } + } + return ret; +} + +toml_datum_t toml_string_in(const toml_table_t *arr, const char *key) { + toml_datum_t ret; + memset(&ret, 0, sizeof(ret)); + toml_raw_t raw = toml_raw_in(arr, key); + if (raw) { + ret.ok = (0 == toml_rtos(raw, &ret.u.s)); + } + return ret; +} + +toml_datum_t toml_bool_in(const toml_table_t *arr, const char *key) { + toml_datum_t ret; + memset(&ret, 0, sizeof(ret)); + ret.ok = (0 == toml_rtob(toml_raw_in(arr, key), &ret.u.b)); + return ret; +} + +toml_datum_t toml_int_in(const toml_table_t *arr, const char *key) { + toml_datum_t ret; + memset(&ret, 0, sizeof(ret)); + ret.ok = (0 == toml_rtoi(toml_raw_in(arr, key), &ret.u.i)); + return ret; +} + +toml_datum_t toml_double_in(const toml_table_t *arr, const char *key) { + toml_datum_t ret; + memset(&ret, 0, sizeof(ret)); + ret.ok = (0 == toml_rtod(toml_raw_in(arr, key), &ret.u.d)); + return ret; +} + +toml_datum_t toml_timestamp_in(const toml_table_t *arr, const char *key) { + toml_timestamp_t ts; + toml_datum_t ret; + memset(&ret, 0, sizeof(ret)); + ret.ok = (0 == toml_rtots(toml_raw_in(arr, key), &ts)); + if (ret.ok) { + ret.ok = !!(ret.u.ts = MALLOC(sizeof(*ret.u.ts))); + if (ret.ok) { + *ret.u.ts = ts; + if (ret.u.ts->year) + ret.u.ts->year = &ret.u.ts->__buffer.year; + if (ret.u.ts->month) + ret.u.ts->month = &ret.u.ts->__buffer.month; + if (ret.u.ts->day) + ret.u.ts->day = &ret.u.ts->__buffer.day; + if (ret.u.ts->hour) + ret.u.ts->hour = &ret.u.ts->__buffer.hour; + if (ret.u.ts->minute) + ret.u.ts->minute = &ret.u.ts->__buffer.minute; + if (ret.u.ts->second) + ret.u.ts->second = &ret.u.ts->__buffer.second; + if (ret.u.ts->millisec) + ret.u.ts->millisec = &ret.u.ts->__buffer.millisec; + if (ret.u.ts->z) + ret.u.ts->z = ret.u.ts->__buffer.z; + } + } + return ret; +} + +static int parse_millisec(const char *p, const char **endp) { + int ret = 0; + int unit = 100; /* unit in millisec */ + for (; '0' <= *p && *p <= '9'; p++, unit /= 10) { + ret += (*p - '0') * unit; + } + *endp = p; + return ret; +} diff --git a/deps/toml/toml.h b/deps/toml/toml.h new file mode 100644 index 0000000..19dc3d2 --- /dev/null +++ b/deps/toml/toml.h @@ -0,0 +1,175 @@ +/* + MIT License + + Copyright (c) CK Tan + https://github.com/cktan/tomlc99 + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ +#ifndef TOML_H +#define TOML_H + +#ifdef _MSC_VER +#pragma warning(disable: 4996) +#endif + +#include +#include + +#ifdef __cplusplus +#define TOML_EXTERN extern "C" +#else +#define TOML_EXTERN extern +#endif + +typedef struct toml_timestamp_t toml_timestamp_t; +typedef struct toml_table_t toml_table_t; +typedef struct toml_array_t toml_array_t; +typedef struct toml_datum_t toml_datum_t; + +/* Parse a file. Return a table on success, or 0 otherwise. + * Caller must toml_free(the-return-value) after use. + */ +TOML_EXTERN toml_table_t *toml_parse_file(FILE *fp, char *errbuf, int errbufsz); + +/* Parse a string containing the full config. + * Return a table on success, or 0 otherwise. + * Caller must toml_free(the-return-value) after use. + */ +TOML_EXTERN toml_table_t *toml_parse(char *conf, /* NUL terminated, please. */ + char *errbuf, int errbufsz); + +/* Free the table returned by toml_parse() or toml_parse_file(). Once + * this function is called, any handles accessed through this tab + * directly or indirectly are no longer valid. + */ +TOML_EXTERN void toml_free(toml_table_t *tab); + +/* Timestamp types. The year, month, day, hour, minute, second, z + * fields may be NULL if they are not relevant. e.g. In a DATE + * type, the hour, minute, second and z fields will be NULLs. + */ +struct toml_timestamp_t { + struct { /* internal. do not use. */ + int year, month, day; + int hour, minute, second, millisec; + char z[10]; + } __buffer; + int *year, *month, *day; + int *hour, *minute, *second, *millisec; + char *z; +}; + +/*----------------------------------------------------------------- + * Enhanced access methods + */ +struct toml_datum_t { + int ok; + union { + toml_timestamp_t *ts; /* ts must be freed after use */ + char *s; /* string value. s must be freed after use */ + int b; /* bool value */ + int64_t i; /* int value */ + double d; /* double value */ + } u; +}; + +/* on arrays: */ +/* ... retrieve size of array. */ +TOML_EXTERN int toml_array_nelem(const toml_array_t *arr); +/* ... retrieve values using index. */ +TOML_EXTERN toml_datum_t toml_string_at(const toml_array_t *arr, int idx); +TOML_EXTERN toml_datum_t toml_bool_at(const toml_array_t *arr, int idx); +TOML_EXTERN toml_datum_t toml_int_at(const toml_array_t *arr, int idx); +TOML_EXTERN toml_datum_t toml_double_at(const toml_array_t *arr, int idx); +TOML_EXTERN toml_datum_t toml_timestamp_at(const toml_array_t *arr, int idx); +/* ... retrieve array or table using index. */ +TOML_EXTERN toml_array_t *toml_array_at(const toml_array_t *arr, int idx); +TOML_EXTERN toml_table_t *toml_table_at(const toml_array_t *arr, int idx); + +/* on tables: */ +/* ... retrieve the key in table at keyidx. Return 0 if out of range. */ +TOML_EXTERN const char *toml_key_in(const toml_table_t *tab, int keyidx); +/* ... returns 1 if key exists in tab, 0 otherwise */ +TOML_EXTERN int toml_key_exists(const toml_table_t *tab, const char *key); +/* ... retrieve values using key. */ +TOML_EXTERN toml_datum_t toml_string_in(const toml_table_t *arr, + const char *key); +TOML_EXTERN toml_datum_t toml_bool_in(const toml_table_t *arr, const char *key); +TOML_EXTERN toml_datum_t toml_int_in(const toml_table_t *arr, const char *key); +TOML_EXTERN toml_datum_t toml_double_in(const toml_table_t *arr, + const char *key); +TOML_EXTERN toml_datum_t toml_timestamp_in(const toml_table_t *arr, + const char *key); +/* .. retrieve array or table using key. */ +TOML_EXTERN toml_array_t *toml_array_in(const toml_table_t *tab, + const char *key); +TOML_EXTERN toml_table_t *toml_table_in(const toml_table_t *tab, + const char *key); + +/*----------------------------------------------------------------- + * lesser used + */ +/* Return the array kind: 't'able, 'a'rray, 'v'alue, 'm'ixed */ +TOML_EXTERN char toml_array_kind(const toml_array_t *arr); + +/* For array kind 'v'alue, return the type of values + i:int, d:double, b:bool, s:string, t:time, D:date, T:timestamp, 'm'ixed + 0 if unknown +*/ +TOML_EXTERN char toml_array_type(const toml_array_t *arr); + +/* Return the key of an array */ +TOML_EXTERN const char *toml_array_key(const toml_array_t *arr); + +/* Return the number of key-values in a table */ +TOML_EXTERN int toml_table_nkval(const toml_table_t *tab); + +/* Return the number of arrays in a table */ +TOML_EXTERN int toml_table_narr(const toml_table_t *tab); + +/* Return the number of sub-tables in a table */ +TOML_EXTERN int toml_table_ntab(const toml_table_t *tab); + +/* Return the key of a table*/ +TOML_EXTERN const char *toml_table_key(const toml_table_t *tab); + +/*-------------------------------------------------------------- + * misc + */ +TOML_EXTERN int toml_utf8_to_ucs(const char *orig, int len, int64_t *ret); +TOML_EXTERN int toml_ucs_to_utf8(int64_t code, char buf[6]); +TOML_EXTERN void toml_set_memutil(void *(*xxmalloc)(size_t), + void (*xxfree)(void *)); + +/*-------------------------------------------------------------- + * deprecated + */ +/* A raw value, must be processed by toml_rto* before using. */ +typedef const char *toml_raw_t; +TOML_EXTERN toml_raw_t toml_raw_in(const toml_table_t *tab, const char *key); +TOML_EXTERN toml_raw_t toml_raw_at(const toml_array_t *arr, int idx); +TOML_EXTERN int toml_rtos(toml_raw_t s, char **ret); +TOML_EXTERN int toml_rtob(toml_raw_t s, int *ret); +TOML_EXTERN int toml_rtoi(toml_raw_t s, int64_t *ret); +TOML_EXTERN int toml_rtod(toml_raw_t s, double *ret); +TOML_EXTERN int toml_rtod_ex(toml_raw_t s, double *ret, char *buf, int buflen); +TOML_EXTERN int toml_rtots(toml_raw_t s, toml_timestamp_t *ret); + +#endif /* TOML_H */ diff --git a/sdk/example/CMakeLists.txt b/sdk/example/CMakeLists.txt index a11a7e3..e45be02 100644 --- a/sdk/example/CMakeLists.txt +++ b/sdk/example/CMakeLists.txt @@ -1,12 +1,9 @@ - -add_definitions(-fPIC) - -add_library(custom_event_plugin +add_library(custom_event_plugin SHARED custom_event_plugin.cpp ) -set_target_properties(custom_event_plugin PROPERTIES PREFIX "") +set_target_properties(custom_event_plugin PROPERTIES PREFIX "") -add_library(http_event_plugin - http_event_plugin.cpp +add_library(http_event_plugin SHARED + http_event_plugin.cpp ) -set_target_properties(http_event_plugin PROPERTIES PREFIX "") \ No newline at end of file +set_target_properties(http_event_plugin PROPERTIES PREFIX "") \ No newline at end of file diff --git a/sdk/example/custom_event_plugin.cpp b/sdk/example/custom_event_plugin.cpp index d5b95e9..0d7f1c4 100644 --- a/sdk/example/custom_event_plugin.cpp +++ b/sdk/example/custom_event_plugin.cpp @@ -2,37 +2,57 @@ #include "packet.h" #include "plugin.h" -struct custom_session_event *_event = nullptr; +#include +#include +#include -void *custom_decode(const char *payload, uint32_t len, void **pme) +static char *g_handler = NULL; + +static void *custom_decode(const char *payload, uint16_t len, void **pme) { - return nullptr; + return NULL; } -int custom_plugin_entry(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint32_t len, void **pme) +extern "C" void custom_plugin_tcp_entry(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) { - struct stellar_session_event_extras *info= (struct stellar_session_event_extras *)custom_decode(payload, len, pme); - struct stellar_session *new_session=session_manager_session_derive(s, "CUSTOM_A"); + char **per_session_pme = (char **)pme; + + printf("RUN custom_plugin_tcp_entry, event: %d\n", event); + + struct stellar_session_event_extras *info = (struct stellar_session_event_extras *)custom_decode(payload, len, pme); + struct stellar_session *new_session = session_manager_session_derive(s, "CUSTOM"); + session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info); session_manager_trigger_event(new_session, SESSION_EVENT_META, info); - return 0; - } -int custom_event_plugin_entry(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint32_t len, void **pme) +extern "C" void custom_plugin_custom_entry(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) { - return 0; + char **per_session_pme = (char **)pme; + printf("RUN custom_plugin_custom_entry, event: %d\n", event); } -int custom_plugin_init() +extern "C" int custom_plugin_init(void) { - //plugin_manager_event_register("TCP", custom_plugin_entry, nullptr); - //plugin_manager_event_register("CUSTOM_A", custom_event_plugin_entry, nullptr); + printf("RUN custom_plugin_init\n"); + + if (g_handler == NULL) + { + g_handler = (char *)malloc(1024); + snprintf(g_handler, 1024, "222222"); + } + return 0; } -void custom_plugin_destroy() +extern "C" void custom_plugin_exit(void) { - return ; + printf("RUN custom_plugin_exit\n"); + + if (g_handler) + { + free(g_handler); + g_handler = NULL; + } } \ No newline at end of file diff --git a/sdk/example/http_event_plugin.cpp b/sdk/example/http_event_plugin.cpp index 68dcf06..e41d8b6 100644 --- a/sdk/example/http_event_plugin.cpp +++ b/sdk/example/http_event_plugin.cpp @@ -1,22 +1,82 @@ #include "session.h" #include "packet.h" #include "plugin.h" -#include "http.h" +#include +#include +static char *g_handler = NULL; -int http_event_plugin_entry(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint32_t len, void **pme) +struct per_session_pme_info { + int flag; + char data[16]; +}; + +extern "C" void http_event_plugin_entry(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) +{ + struct per_session_pme_info **per_session_pme = (struct per_session_pme_info **)pme; + + printf("RUN http_event_plugin_entry, event: %d\n", event); + + if (event & SESSION_EVENT_OPENING) + { + if (*per_session_pme == NULL) + { + struct per_session_pme_info *cur_ctx = (struct per_session_pme_info *)malloc(sizeof(struct per_session_pme_info)); + snprintf(cur_ctx->data, 6, "******"); + *per_session_pme = *&cur_ctx; + printf("http_event_plugin_entry->opening_handler\n"); + } + } + + if (event & SESSION_EVENT_RAWPKT) + { + printf("http_event_plugin_entry->rawpkt_handler\n"); + } + + if (event & SESSION_EVENT_ORDPKT) + { + printf("http_event_plugin_entry->ordpkt_handler\n"); + } + + if (event & SESSION_EVENT_META) + { + printf("http_event_plugin_entry->meta_handler\n"); + } + + if (event & SESSION_EVENT_CLOSING) + { + if (*per_session_pme) + { + printf("http_event_plugin_entry->closing_hanler\n"); + + free(*per_session_pme); + *per_session_pme = NULL; + } + } +} + +extern "C" int http_event_plugin_init(void) +{ + printf("RUN http_event_plugin_init\n"); + + if (g_handler == NULL) + { + g_handler = (char *)malloc(1024); + snprintf(g_handler, 1024, "111111"); + } + return 0; } -int http_event_plugin_init() +extern "C" void http_event_plugin_exit(void) { - //plugin_manager_event_register("HTTP", http_event_plugin_entry, nullptr); - return 0; -} + printf("RUN http_event_plugin_exit\n"); -void http_event_plugin_destroy() -{ - return ; + if (g_handler) + { + free(g_handler); + g_handler = NULL; + } } \ No newline at end of file diff --git a/sdk/example/plugins/custom_event_plugin/custom_event_plugin.inf b/sdk/example/plugins/custom_event_plugin/custom_event_plugin.inf index 479d474..c7d3fe3 100644 --- a/sdk/example/plugins/custom_event_plugin/custom_event_plugin.inf +++ b/sdk/example/plugins/custom_event_plugin/custom_event_plugin.inf @@ -1,16 +1,14 @@ [PLUGINFO] -INIT_FUNC=custom_plugin_init -EXIT_FUNC=custom_plugin_exit -LIBRARY_PATH=./plugins/custom_event_plugin/custom_event_plugin.so +INIT_FUNC="custom_plugin_init" +EXIT_FUNC="custom_plugin_exit" +LIBRARY_PATH="./plugins/custom_event_plugin/custom_event_plugin.so" +# Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL" -# Support SESSION_EVENT_TYPE -# OPENING,RAWPKT,ORDPKT,META,CLOSING,ALL +[SESSION_NAME.TCP] +SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"] +SESSION_EVENT_CALLBACK="custom_plugin_tcp_entry" -[SESSION_TYPE.TCP] -SESSION_EVENT_TYPE=ALL -SESSION_EVENT_CALLBACK=custom_plugin_entry - -[SESSION_TYPE.CUSTOM_A] -SESSION_EVENT_TYPE=OPENING,ORDPKT,CLOSING -SESSION_EVENT_CALLBACK=custom_event_plugin_entry \ No newline at end of file +[SESSION_NAME.CUSTOM] +SESSION_EVENT_TYPE=["SESSION_EVENT_OPENING","SESSION_EVENT_ORDPKT","SESSION_EVENT_CLOSING"] +SESSION_EVENT_CALLBACK="custom_plugin_custom_entry" \ No newline at end of file diff --git a/sdk/example/plugins/http_event_plugin/http_event_plugin.inf b/sdk/example/plugins/http_event_plugin/http_event_plugin.inf index 4609300..a58a405 100644 --- a/sdk/example/plugins/http_event_plugin/http_event_plugin.inf +++ b/sdk/example/plugins/http_event_plugin/http_event_plugin.inf @@ -1,8 +1,10 @@ [PLUGINFO] -INIT_FUNC=http_event_plugin_init -EXIT_FUNC=http_event_plugin_exit -LIBRARY_PATH=./plugins/http_event_plugin/http_event_plugin.so +INIT_FUNC="http_event_plugin_init" +EXIT_FUNC="http_event_plugin_exit" +LIBRARY_PATH="./plugins/http_event_plugin/http_event_plugin.so" -[SESSION_TYPE.HTTP] -SESSION_EVENT_TYPE=ALL -SESSION_EVENT_CALLBACK=http_event_plugin_entry \ No newline at end of file +# Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL" + +[SESSION_NAME.HTTP] +SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"] +SESSION_EVENT_CALLBACK="http_event_plugin_entry" \ No newline at end of file diff --git a/sdk/example/plugins/plugins.inf b/sdk/example/plugins/plugins.inf index 50e40af..ad3f27e 100644 --- a/sdk/example/plugins/plugins.inf +++ b/sdk/example/plugins/plugins.inf @@ -1,2 +1,4 @@ -./http_event_plugin/http_event_plugin.inf -./custom_event_plugin/custom_event_plugin.inf \ No newline at end of file +# Relative path, relative to the installation path of stellar + +./plugins/http_event_plugin/http_event_plugin.inf +./plugins/custom_event_plugin/custom_event_plugin.inf \ No newline at end of file diff --git a/sdk/include/http.h b/sdk/include/http.h index d7f3fbf..6250055 100644 --- a/sdk/include/http.h +++ b/sdk/include/http.h @@ -1,6 +1,5 @@ #pragma once -#include "packet.h" #include "session.h" -void http_decoder(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint16_t len, void **pme); \ No newline at end of file +void http_decoder(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme); \ No newline at end of file diff --git a/sdk/include/plugin.h b/sdk/include/plugin.h index ac3b279..d213f7c 100644 --- a/sdk/include/plugin.h +++ b/sdk/include/plugin.h @@ -2,8 +2,14 @@ #include "session.h" -typedef int plugin_init_callback(void **pme); -typedef void plugin_exit_callback(void *pme); +typedef int plugin_init_callback(void); +typedef void plugin_exit_callback(void); -void plugin_remove_from_session_event(struct stellar_event *ev, - struct stellar_session *s); \ No newline at end of file +void plugin_remove_from_session_event(struct stellar_event *ev, struct stellar_session *s); + +/****************************************************************************** + * Public API: between plugin and plugin_manager + ******************************************************************************/ + +// TODO +// TODO \ No newline at end of file diff --git a/sdk/include/session.h b/sdk/include/session.h index b497fab..19bb566 100644 --- a/sdk/include/session.h +++ b/sdk/include/session.h @@ -25,6 +25,7 @@ enum stellar_session_type enum session_event_type { + SESSION_EVENT_UNKNOWN = (0x00), SESSION_EVENT_OPENING = (0x01 << 1), SESSION_EVENT_RAWPKT = (0x01 << 2), SESSION_EVENT_ORDPKT = (0x01 << 3), @@ -35,11 +36,7 @@ enum session_event_type struct stellar_session_event_extras; -typedef void (fn_session_event_callback)(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint16_t len, void **pme); +typedef void(fn_session_event_callback)(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme); -void session_manager_trigger_event(struct stellar_session *s, - enum session_event_type type, - struct stellar_session_event_extras *info); - -struct stellar_session *session_manager_session_derive(const struct stellar_session *this_session, - const char *new_session_type_name); \ No newline at end of file +void session_manager_trigger_event(struct stellar_session *s, enum session_event_type type, struct stellar_session_event_extras *info); +struct stellar_session *session_manager_session_derive(const struct stellar_session *this_session, const char *session_name); \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ede3dd3..6b4ad06 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,11 +1,9 @@ - include_directories(${CMAKE_SOURCE_DIR}/sdk/include) include_directories(${CMAKE_SOURCE_DIR}/deps/) include_directories(${CMAKE_SOURCE_DIR}/src/packet_io/) include_directories(${CMAKE_SOURCE_DIR}/src/session_manager/) include_directories(${CMAKE_SOURCE_DIR}/src/plugin_manager/) - enable_testing() add_subdirectory(packet_io) add_subdirectory(session_manager) @@ -16,4 +14,13 @@ add_executable(stellar main.cpp ) -target_link_libraries(stellar pthread packet_io plugin_manager session_manager http) \ No newline at end of file +target_link_libraries( + stellar + pthread + packet_io + plugin_manager + session_manager + http + toml + dl +) \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 5071278..8d1655d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -59,11 +59,12 @@ int main(int argc, char ** argv) struct plugin_manager *plug_mgr = plugin_manager_create(); // register build-in plugin - plugin_manager_register(plug_mgr, "HTTP", http_decoder); + plugin_manager_register(plug_mgr, "HTTP", SESSION_EVENT_ALL, http_decoder); // load external plugins - char absolute_plugin_file[] = "/op/tsg/sapp/plug/plugins.inf"; - plugin_manager_load(plug_mgr, absolute_plugin_file); + char prefix_path[] = "/op/tsg/stellar/"; + char file_path[] = "./plugs/plugins.inf"; + plugin_manager_load(plug_mgr, prefix_path, file_path); //packet_io_init struct packet_io_device *dev = packet_io_init(1, "stellar", "cap0"); @@ -78,6 +79,7 @@ int main(int argc, char ** argv) usleep(1); } + plugin_manager_unload(plug_mgr); plugin_manager_destory(plug_mgr); return 0; diff --git a/src/plugin_manager/CMakeLists.txt b/src/plugin_manager/CMakeLists.txt index 5850d1f..1901671 100644 --- a/src/plugin_manager/CMakeLists.txt +++ b/src/plugin_manager/CMakeLists.txt @@ -1,4 +1,10 @@ -add_library(plugin_manager - plugin_manager.cpp +add_library(plugin_manager + plugin_manager_config.cpp + plugin_manager_module.cpp + plugin_manager_util.cpp + plugin_manager.cpp ) -target_include_directories(plugin_manager PUBLIC ${CMAKE_SOURCE_DIR}) \ No newline at end of file + +target_include_directories(plugin_manager PUBLIC ${CMAKE_SOURCE_DIR}) + +add_subdirectory(test) \ No newline at end of file diff --git a/src/plugin_manager/plugin_manager.cpp b/src/plugin_manager/plugin_manager.cpp index 1755cc4..7b6492a 100644 --- a/src/plugin_manager/plugin_manager.cpp +++ b/src/plugin_manager/plugin_manager.cpp @@ -1,106 +1,313 @@ #include "deps/uthash/uthash.h" -#include "sdk/include/session.h" -#include "sdk/include/plugin.h" #include "session_manager.h" -#include "plugin_manager.h" +#include "plugin_manager_util.h" +#include "plugin_manager_config.h" +#include "plugin_manager_module.h" -#include #include +#include /****************************************************************************** - * CallBack Static Hash Table (For Global) + * CallBack Runtime (For Per Session) ******************************************************************************/ -struct session_event_callback_static +struct eventcb_runtime { - enum session_event_type event; - fn_session_event_callback *callback; -}; - -/* - * Hast table - * - * key: string -> session_type - * val: array -> event_cbs - */ -struct managed_session_event_callback -{ - char session_type[16]; - struct session_event_callback_static event_cbs[0]; // dynamic array - // Record the number of callback functions that want to process the current session - int event_cbs_num; - UT_hash_handle hh; -}; - -/****************************************************************************** - * CallBack Runtime Array (For Per Session) - ******************************************************************************/ - -struct session_event_callback_runtime -{ - int skip; // for skip this plugin callback - void *callback_args; + int skip; + void *cb_args; enum session_event_type event; - fn_session_event_callback *callback; + fn_session_event_callback *cb; }; struct session_plugin_ctx { - int event_cbs_num; - struct session_event_callback_runtime event_cbs[0]; // dynamic array + int current_plugin_index; + int eventcb_num; + struct eventcb_runtime *eventcbs; }; /****************************************************************************** - * struct plugin_manager + * CallBack Static (For Per Plugin Manager) ******************************************************************************/ -/* - * Each plugin has an init_cb and an exit_cb, but may have multiple entry_cb. - * entry_cb is stored in the global_session_callback_ctx. - */ -struct plugin_ctx +struct eventcb_static { - char *name; - char *library; - /* - * Stores the context generated by the plugin initialization, - * which is used to release resources when the plugin ends. - */ - void *pme; - plugin_init_callback *init_cb; - plugin_exit_callback *exit_cb; + enum session_event_type event; + fn_session_event_callback *cb; }; +struct plugin_manager_eventcb +{ + char session_name[MAX_SESSION_NAME_LENGTH]; // key + + int eventcb_num; // val size + struct eventcb_static *eventcbs; // val: dynamic array + + UT_hash_handle hh; +}; + +/****************************************************************************** + * Struct plugin_manager + ******************************************************************************/ + struct plugin_manager { - struct managed_session_event_callback *cb_table; - struct plugin_ctx plugins[MAX_PLUGIN_NUM]; + int used_module_num; + int used_config_num; + int used_evencb_num; // only plugin register eventcb numbers + + struct plugin_manager_module *modules[MAX_PLUGIN_NUM]; + struct plugin_manager_config *configs[MAX_PLUGIN_NUM]; + struct plugin_manager_eventcb *evcb_htable; }; /****************************************************************************** - * Private API + * Create/Destory plugin ctx (per session) ******************************************************************************/ -static void plugin_manager_handle_opening_event(struct plugin_manager *plug_mgr, struct stellar_event *event) +static struct session_plugin_ctx *plugin_manager_create_plugin_ctx(struct plugin_manager *plug_mgr, const char *session_name) { + if (session_name == NULL || strlen(session_name) == 0) + { + plugin_manager_log(ERROR, "invalid parameter, session name is empty"); + return NULL; + } + + struct plugin_manager_eventcb *elem; + HASH_FIND_STR(plug_mgr->evcb_htable, session_name, elem); + if (elem == NULL) + { + plugin_manager_log(ERROR, "can't find event callback for session name '%s'", session_name); + return NULL; + } + else + { + struct session_plugin_ctx *plug_ctx = safe_alloc(struct session_plugin_ctx, 1); + plug_ctx->eventcb_num = elem->eventcb_num; + plug_ctx->eventcbs = safe_alloc(struct eventcb_runtime, plug_ctx->eventcb_num); + + for (int i = 0; i < plug_ctx->eventcb_num; i++) + { + plug_ctx->eventcbs[i].skip = 0; + plug_ctx->eventcbs[i].event = elem->eventcbs[i].event; + plug_ctx->eventcbs[i].cb = elem->eventcbs[i].cb; + plug_ctx->eventcbs[i].cb_args = NULL; + } + + return plug_ctx; + } } -static void plugin_manager_handle_data_event(struct plugin_manager *plug_mgr, struct stellar_event *event) -{ -} - -static void plugin_manager_handle_closing_event(struct plugin_manager *plug_mgr, struct stellar_event *event) +static void plugin_manager_destory_plugin_ctx(struct session_plugin_ctx *plug_ctx) { + if (plug_ctx) + { + safe_free(plug_ctx->eventcbs); + safe_free(plug_ctx); + } } /****************************************************************************** - * Public API + * Tools for managing plugins ******************************************************************************/ +static int plugin_manager_parse_plugins(struct plugin_manager *plug_mgr, const char *prefix, const char *file) +{ + char plugin_inf[4096] = {0}; + char line_buffer[4096] = {0}; + if (strlen(prefix) <= 0) + { + plugin_manager_log(ERROR, "Invalid parameter, plugin config file prefix cannot be empty"); + return -1; + } + if (strlen(file) <= 0) + { + plugin_manager_log(ERROR, "Invalid parameter, plugin config file name cannot be empty"); + return -1; + } + strcat_prefix_and_file(prefix, file, plugin_inf, sizeof(plugin_inf)); + + FILE *fp = fopen(plugin_inf, "r"); + if (fp == NULL) + { + plugin_manager_log(ERROR, "can't open %s, %s", plugin_inf, strerror(errno)); + return -1; + } + + while (fgets(line_buffer, sizeof(line_buffer), fp)) + { + if ('#' == line_buffer[0] || '\n' == line_buffer[0]) + { + memset(line_buffer, 0, sizeof(line_buffer)); + continue; + } + + if (plug_mgr->used_config_num >= MAX_PLUGIN_NUM) + { + plugin_manager_log(ERROR, "the number of registered plugins exceeds the limit and cannot exceed %d", MAX_PLUGIN_NUM); + goto err; + } + + struct plugin_manager_config *config = plugin_mangager_config_create(); + if (plugin_mangager_config_parse(config, prefix, line_buffer) == -1) + { + plugin_mangager_config_destory(config); + goto err; + } + + plug_mgr->configs[plug_mgr->used_config_num] = config; + plug_mgr->used_config_num++; + memset(line_buffer, 0, sizeof(line_buffer)); + } + + fclose(fp); + + return 0; + +err: + + if (fp) + { + fclose(fp); + fp = NULL; + } + + return -1; +} + +static int plugin_manager_open_plugins(struct plugin_manager *plug_mgr) +{ + for (int i = 0; i < plug_mgr->used_config_num; i++) + { + struct plugin_manager_config *config = plug_mgr->configs[i]; + struct plugin_manager_module *module = plugin_manager_module_open(config); + if (module == NULL) + { + return -1; + } + plug_mgr->modules[plug_mgr->used_module_num] = module; + plug_mgr->used_module_num++; + } + + return 0; +} + +static int plugin_manager_register_plugins(struct plugin_manager *plug_mgr) +{ + for (int i = 0; i < plug_mgr->used_module_num; i++) + { + struct plugin_manager_module *module = plug_mgr->modules[i]; + if (plugin_manager_module_register(plug_mgr, module) == -1) + { + return -1; + } + plug_mgr->used_evencb_num++; + } + + return 0; +} + +static int plugin_manager_init_plugins(struct plugin_manager *plug_mgr) +{ + for (int i = 0; i < plug_mgr->used_module_num; i++) + { + struct plugin_manager_module *module = plug_mgr->modules[i]; + if (plugin_manager_module_init(module) == -1) + { + return -1; + } + double percentage = ((double)(i + 1)) / ((double)plug_mgr->used_module_num) * ((double)100); + plugin_manager_log(INFO, "Plugin initialization progress: [%.2f%]", percentage); + } + + return 0; +} + +static void plugin_manager_exit_plugins(struct plugin_manager *plug_mgr) +{ + if (plug_mgr && plug_mgr->used_module_num) + { + for (int i = 0; i < plug_mgr->used_module_num; i++) + { + struct plugin_manager_module *module = plug_mgr->modules[i]; + plugin_manager_module_exit(module); + } + } +} + +static void plugin_manager_close_plugins(struct plugin_manager *plug_mgr) +{ + if (plug_mgr && plug_mgr->used_module_num) + { + for (int i = 0; i < plug_mgr->used_module_num; i++) + { + struct plugin_manager_module *module = plug_mgr->modules[i]; + plugin_manager_module_close(module); + } + plug_mgr->used_module_num = 0; + } +} + +// deparse for destory parse +static void plugin_manager_deparse_plugins(struct plugin_manager *plug_mgr) +{ + if (plug_mgr && plug_mgr->used_config_num) + { + for (int i = 0; i < plug_mgr->used_config_num; i++) + { + struct plugin_manager_config *config = plug_mgr->configs[i]; + plugin_mangager_config_destory(config); + } + plug_mgr->used_config_num = 0; + } +} + +/****************************************************************************** + * Public API for managing plugins + ******************************************************************************/ + +int plugin_manager_load(struct plugin_manager *plug_mgr, const char *prefix, const char *file) +{ + if (plugin_manager_parse_plugins(plug_mgr, prefix, file) == -1) + { + return -1; + } + + if (plugin_manager_open_plugins(plug_mgr) == -1) + { + return -1; + } + + if (plugin_manager_register_plugins(plug_mgr) == -1) + { + return -1; + } + + if (plugin_manager_init_plugins(plug_mgr) == -1) + { + return -1; + } + + return 0; +} + +void plugin_manager_unload(struct plugin_manager *plug_mgr) +{ + plugin_manager_exit_plugins(plug_mgr); + plugin_manager_close_plugins(plug_mgr); + plugin_manager_deparse_plugins(plug_mgr); +} + struct plugin_manager *plugin_manager_create() { - struct plugin_manager *plug_mgr = NULL; + struct plugin_manager *plug_mgr = safe_alloc(struct plugin_manager, 1); + + plug_mgr->used_module_num = 0; + plug_mgr->used_config_num = 0; + plug_mgr->used_evencb_num = 0; + + plug_mgr->evcb_htable = NULL; return plug_mgr; } @@ -109,45 +316,138 @@ void plugin_manager_destory(struct plugin_manager *plug_mgr) { if (plug_mgr) { + if (plug_mgr->evcb_htable) + { + struct plugin_manager_eventcb *elem; + struct plugin_manager_eventcb *tmp; + HASH_ITER(hh, plug_mgr->evcb_htable, elem, tmp) + { + HASH_DEL(plug_mgr->evcb_htable, elem); + + safe_free(elem->eventcbs); + safe_free(elem); + } + + plug_mgr->evcb_htable = NULL; + plug_mgr->used_evencb_num = 0; + } + + plugin_manager_unload(plug_mgr); + safe_free(plug_mgr); } } -int plugin_manager_load(struct plugin_manager *plug_mgr, const char *plugin_file) +int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_name, enum session_event_type event, fn_session_event_callback *cb) { - return 0; -} + if (strlen(session_name) <= 0) + { + plugin_manager_log(ERROR, "invalid parameter, session name is empty"); + return -1; + } -void plugin_manager_unload(struct plugin_manager *plug_mgr) -{ -} + if (strlen(session_name) > MAX_SESSION_NAME_LENGTH) + { + plugin_manager_log(ERROR, "invalid parameter, session name '%s' is too long and exceeds '%d' bytes", session_name, MAX_SESSION_NAME_LENGTH); + return -1; + } + + if (cb == NULL) + { + plugin_manager_log(ERROR, "invalid parameter, the callback corresponding to the session name '%s' is null", session_name); + return -1; + } + + struct plugin_manager_eventcb *elem; + HASH_FIND_STR(plug_mgr->evcb_htable, session_name, elem); + // session_name exists, add a new cb to the end of the eventcbs dynamic array + if (elem) + { + elem->eventcbs = (struct eventcb_static *)realloc(elem->eventcbs, (elem->eventcb_num + 1) * sizeof(struct eventcb_static)); + + elem->eventcbs[elem->eventcb_num].event = event; + elem->eventcbs[elem->eventcb_num].cb = cb; + + elem->eventcb_num++; + } + // session_name does not exist, allocate a new node elem, and add elem to the hash table + else + { + elem = safe_alloc(struct plugin_manager_eventcb, 1); + memcpy(elem->session_name, session_name, strlen(session_name)); + + elem->eventcbs = (struct eventcb_static *)realloc(elem->eventcbs, (elem->eventcb_num + 1) * sizeof(struct eventcb_static)); + + elem->eventcbs[elem->eventcb_num].event = event; + elem->eventcbs[elem->eventcb_num].cb = cb; + + elem->eventcb_num++; + + HASH_ADD_STR(plug_mgr->evcb_htable, session_name, elem); + } -int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_type, fn_session_event_callback *callback) -{ return 0; } void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_event *event) { - assert(event); - struct stellar_session_event_data *event_data = event->session_event_data; - assert(event_data); - session_event_type type = event_data->type; + const struct stellar_session *seesion = stellar_event_get_session(event); + struct session_plugin_ctx *plug_ctx = stellar_event_get_plugin_ctx(event); + enum session_event_type event_type = stellar_event_get_type(event); + const char *session_name = stellar_event_get_session_name(event); + struct stellar_packet *packet = stellar_event_get_packet(event); + uint16_t payload_len = stellar_event_get_payload_length(event); + const char *payload = stellar_event_get_payload(event); - switch (type) + assert(seesion); + assert(session_name); + + // the same session may trigger multi times opening events + if (event_type & SESSION_EVENT_OPENING) { - case SESSION_EVENT_OPENING: - plugin_manager_handle_opening_event(plug_mgr, event); - break; - case SESSION_EVENT_RAWPKT: /* fall through */ - case SESSION_EVENT_ORDPKT: /* fall through */ - case SESSION_EVENT_META: /* fall through */ - plugin_manager_handle_data_event(plug_mgr, event); - break; - case SESSION_EVENT_CLOSING: - plugin_manager_handle_closing_event(plug_mgr, event); - break; - default: - // TODO log error - break; + if (plug_ctx == NULL) + { + plug_ctx = plugin_manager_create_plugin_ctx(plug_mgr, session_name); + if (plug_ctx == NULL) + { + plugin_manager_log(ERROR, "can't create runtime plugin ctx for session '%s', Please check whether the callback is registered in the current session"); + return; + } + stellar_event_set_plugin_ctx(event, plug_ctx); + } } -} \ No newline at end of file + + if (plug_ctx) + { + for (int i = 0; i < plug_ctx->eventcb_num; i++) + { + plug_ctx->current_plugin_index = i; + struct eventcb_runtime *runtime = &plug_ctx->eventcbs[i]; + if (runtime->skip) + { + continue; + } + + if (runtime->event & event_type) + { + runtime->cb(seesion, event_type, packet, payload, payload_len, &runtime->cb_args); + } + } + } + else + { + plugin_manager_log(ERROR, "session '%s' runtime plugin ctx is null when running event callback", session_name); + abort(); + } + + if (event_type & SESSION_EVENT_CLOSING) + { + plugin_manager_destory_plugin_ctx(plug_ctx); + stellar_event_set_plugin_ctx(event, NULL); + } +} + +/****************************************************************************** + * Suppport LUA plugins + ******************************************************************************/ + +// TODO \ No newline at end of file diff --git a/src/plugin_manager/plugin_manager.h b/src/plugin_manager/plugin_manager.h index f6a12a3..4e96b06 100644 --- a/src/plugin_manager/plugin_manager.h +++ b/src/plugin_manager/plugin_manager.h @@ -1,11 +1,7 @@ - #pragma once #include "sdk/include/session.h" -#define MAX_PLUGIN_NUM 256 - -struct per_session_event_cbs; struct plugin_manager; struct plugin_manager *plugin_manager_create(); @@ -13,8 +9,8 @@ void plugin_manager_destory(struct plugin_manager *plug_mgr); // return 0: success // return -1: error -int plugin_manager_load(struct plugin_manager *plug_mgr, const char *plugin_file); +int plugin_manager_load(struct plugin_manager *plug_mgr, const char *prefix, const char *file); void plugin_manager_unload(struct plugin_manager *plug_mgr); -int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_type, fn_session_event_callback *callback); +int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_name, enum session_event_type event, fn_session_event_callback *callback); void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_event *event); \ No newline at end of file diff --git a/src/plugin_manager/plugin_manager_config.cpp b/src/plugin_manager/plugin_manager_config.cpp new file mode 100644 index 0000000..f36512c --- /dev/null +++ b/src/plugin_manager/plugin_manager_config.cpp @@ -0,0 +1,358 @@ +#include +#include +#include + +#include "toml/toml.h" +#include "sdk/include/session.h" +#include "plugin_manager_util.h" +#include "plugin_manager_config.h" + +struct event_type_map +{ + const char *type_str; + enum session_event_type type_int; +}; + +static struct event_type_map evtype_map[] = + { + {"SESSION_EVENT_OPENING", SESSION_EVENT_OPENING}, + {"SESSION_EVENT_RAWPKT", SESSION_EVENT_RAWPKT}, + {"SESSION_EVENT_ORDPKT", SESSION_EVENT_ORDPKT}, + {"SESSION_EVENT_META", SESSION_EVENT_META}, + {"SESSION_EVENT_CLOSING", SESSION_EVENT_CLOSING}, + {"SESSION_EVENT_ALL", SESSION_EVENT_ALL}, +}; + +/****************************************************************************** + * Private API (For Event Type) + ******************************************************************************/ + +static enum session_event_type event_type_str2int(const char *evtype_str) +{ + int num = sizeof(evtype_map) / sizeof(evtype_map[0]); + + for (int i = 0; i < num; i++) + { + if (strcmp(evtype_str, evtype_map[i].type_str) == 0) + { + return evtype_map[i].type_int; + } + } + + return SESSION_EVENT_UNKNOWN; +} + +static void event_type_int2str(enum session_event_type evtype_int, char *buffer, int size) +{ + int used = 0; + int num = sizeof(evtype_map) / sizeof(evtype_map[0]); + + for (int i = 0; i < num; i++) + { + if (evtype_map[i].type_int & evtype_int) + { + if (evtype_map[i].type_int == SESSION_EVENT_ALL && evtype_int != SESSION_EVENT_ALL) + { + continue; + } + used += snprintf(buffer + used, size - used, "%s ", evtype_map[i].type_str); + } + } +} + +/****************************************************************************** + * Private API (For Parse) + ******************************************************************************/ + +static int toml_parse_string(toml_table_t *table, const char *string_key, const char *file, char **out) +{ + toml_datum_t string_val = toml_string_in(table, string_key); + if (!string_val.ok) + { + plugin_manager_log(ERROR, "can't find '%s' configuration iterm in %s", string_key, file); + return -1; + } + + if (strlen(string_val.u.s) <= 0) + { + plugin_manager_log(ERROR, "invalid value for '%s' configuration item in %s", string_key, file); + safe_free(string_val.u.s); + return -1; + } + + *out = safe_dup(string_val.u.s); + safe_free(string_val.u.s); + + return 0; +} + +static int toml_parse_table(toml_table_t *table, const char *string_key, const char *file, toml_table_t **out) +{ + *out = toml_table_in(table, string_key); + if (*out == NULL) + { + plugin_manager_log(ERROR, "can't find '%s' section in %s", string_key, file); + return -1; + } + + return 0; +} + +static int toml_parse_plugin_section(toml_table_t *root, struct plugin_manager_config *config, const char *file) +{ + toml_table_t *plugin_section = NULL; + + if (toml_parse_table(root, "PLUGINFO", file, &plugin_section) == -1) + { + return -1; + } + + if (toml_parse_string(plugin_section, "INIT_FUNC", file, &config->plugin_section.init_func_name) == -1) + { + return -1; + } + + if (toml_parse_string(plugin_section, "EXIT_FUNC", file, &config->plugin_section.exit_func_name) == -1) + { + return -1; + } + + if (toml_parse_string(plugin_section, "LIBRARY_PATH", file, &config->plugin_section.lib_path) == -1) + { + return -1; + } + + return 0; +} + +static int toml_parse_session_section(toml_table_t *root, struct plugin_manager_config *config, const char *file) +{ + toml_table_t *session_section = NULL; + toml_table_t *sub_session_section = NULL; + const char *session_name = NULL; + toml_array_t *event_type_array = NULL; + toml_datum_t type_str; + enum session_event_type type_int; + + if (toml_parse_table(root, "SESSION_NAME", file, &session_section) == -1) + { + return -1; + } + + for (int i = 0; i < toml_table_ntab(session_section); i++) + { + session_name = NULL; + sub_session_section = NULL; + event_type_array = NULL; + + config->session_section = (struct session_section_config *)realloc(config->session_section, sizeof(struct session_section_config) * (config->session_section_num + 1)); + config->session_section[config->session_section_num].event = SESSION_EVENT_UNKNOWN; + config->session_section[config->session_section_num].cb_func_name = NULL; + memset(config->session_section[config->session_section_num].session_name, 0, MAX_SESSION_NAME_LENGTH); + + // parse session name + session_name = toml_key_in(session_section, i); + int session_name_len = strlen(session_name); + if (session_name_len <= 0) + { + plugin_manager_log(ERROR, "invalid value for 'SESSION_NAME' configuration item in %s", file); + return -1; + } + if (session_name_len > MAX_SESSION_NAME_LENGTH) + { + plugin_manager_log(ERROR, "invalid value for 'SESSION_NAME' configuration item in %s, '%s' is too long and exceeds %d bytes", file, session_name, MAX_SESSION_NAME_LENGTH); + return -1; + } + if (toml_parse_table(session_section, session_name, file, &sub_session_section) == -1) + { + return -1; + } + strncpy(config->session_section[config->session_section_num].session_name, session_name, session_name_len); + + // parse session event callback + if (toml_parse_string(sub_session_section, "SESSION_EVENT_CALLBACK", file, &config->session_section[config->session_section_num].cb_func_name) == -1) + { + return -1; + } + + event_type_array = toml_array_in(sub_session_section, "SESSION_EVENT_TYPE"); + if (event_type_array == NULL) + { + plugin_manager_log(ERROR, "can't find 'SESSION_EVENT_TYPE' configuration iterm in '[SESSION_NAME.%s]' section of %s", session_name, file); + return -1; + } + + for (int i = 0; i < toml_array_nelem(event_type_array); i++) + { + type_int = SESSION_EVENT_UNKNOWN; + + type_str = toml_string_at(event_type_array, i); + if (!type_str.ok) + { + plugin_manager_log(ERROR, "can't parse 'SESSION_EVENT_TYPE' configuration iterm in '[SESSION_NAME.%s]' section of %s", session_name, file); + return -1; + } + + type_int = event_type_str2int(type_str.u.s); + if (type_int == SESSION_EVENT_UNKNOWN) + { + plugin_manager_log(ERROR, "invalid value '%s' for 'SESSION_EVENT_TYPE' configuration item in '[SESSION_NAME.%s]' section of %s", type_str.u.s, session_name, file); + safe_free(type_str.u.s); + return -1; + } + + (config->session_section[config->session_section_num].event) = (enum session_event_type)((config->session_section[config->session_section_num].event) | type_int); + safe_free(type_str.u.s); + } + + config->session_section_num++; + } + + return 0; +} + +/****************************************************************************** + * Public API + ******************************************************************************/ + +struct plugin_manager_config *plugin_mangager_config_create() +{ + struct plugin_manager_config *config = safe_alloc(struct plugin_manager_config, 1); + + config->prefix_path = NULL; + config->file_path = NULL; + config->session_section_num = 0; + config->session_section = NULL; + config->plugin_section.init_func_name = NULL; + config->plugin_section.exit_func_name = NULL; + config->plugin_section.lib_path = NULL; + + return config; +} + +void plugin_mangager_config_destory(struct plugin_manager_config *config) +{ + if (config) + { + safe_free(config->prefix_path); + safe_free(config->file_path); + + safe_free(config->plugin_section.init_func_name); + safe_free(config->plugin_section.exit_func_name); + safe_free(config->plugin_section.lib_path); + + if (config->session_section) + { + for (int i = 0; i < config->session_section_num; i++) + { + struct session_section_config *temp = &config->session_section[i]; + safe_free(temp->cb_func_name); + } + config->session_section_num = 0; + safe_free(config->session_section); + } + + safe_free(config); + } +} + +void plugin_mangager_config_dump(struct plugin_manager_config *config) +{ + if (config) + { + plugin_manager_log(DEBUG, "[CONFIG_PREFIX] : %s", config->prefix_path); + plugin_manager_log(DEBUG, "[CONFIG_FILE] : %s", config->file_path); + plugin_manager_log(DEBUG, "[PLUGINFO]->INIT_FUNC : %s", config->plugin_section.init_func_name); + plugin_manager_log(DEBUG, "[PLUGINFO]->EXIT_FUNC : %s", config->plugin_section.exit_func_name); + plugin_manager_log(DEBUG, "[PLUGINFO]->LIBRARY_PATH : %s", config->plugin_section.lib_path); + + if (config->session_section) + { + for (int i = 0; i < config->session_section_num; i++) + { + char tmp_buffer[1024] = {0}; + struct session_section_config *temp = &config->session_section[i]; + event_type_int2str(temp->event, tmp_buffer, 1024); + plugin_manager_log(DEBUG, "[SESSION_NAME.%s]->SESSION_EVENT_TYPE : %d, %s", temp->session_name, temp->event, tmp_buffer); + plugin_manager_log(DEBUG, "[SESSION_NAME.%s]->SESSION_EVENT_CALLBACK : %s", temp->session_name, temp->cb_func_name); + } + } + } +} + +int plugin_mangager_config_parse(struct plugin_manager_config *config, const char *prefix, const char *file) +{ + FILE *fp = NULL; + toml_table_t *root = NULL; + char errbuf[200] = {0}; + char tmp_buffer[4096] = {0}; + + config->prefix_path = safe_dup(prefix); + config->file_path = safe_dup(file); + strcat_prefix_and_file(prefix, file, tmp_buffer, sizeof(tmp_buffer)); + + fp = fopen(tmp_buffer, "r"); + if (fp == NULL) + { + plugin_manager_log(ERROR, "can't open %s, %s", tmp_buffer, strerror(errno)); + return -1; + } + + root = toml_parse_file(fp, errbuf, sizeof(errbuf)); + if (root == NULL) + { + plugin_manager_log(ERROR, "toml parsing %s failed, %s", file, errbuf); + goto err; + } + + if (toml_parse_plugin_section(root, config, file) == -1) + { + goto err; + } + + if (toml_parse_session_section(root, config, file) == -1) + { + goto err; + } + + toml_free(root); + fclose(fp); + + return 0; + +err: + if (root) + { + toml_free(root); + } + + if (fp) + { + fclose(fp); + fp = NULL; + } + + return -1; +} + +/****************************************************************************** + * Test + ******************************************************************************/ + +#ifdef PLUGIN_MANAGER_TEST +int main(int argc, char *argv[]) +{ + struct plugin_manager_config *config = plugin_mangager_config_create(); + + if (plugin_mangager_config_parse(config, argv[1], argv[2]) == -1) + { + plugin_manager_log(ERROR, "can't parser %s %s", argv[1], argv[2]); + } + + plugin_mangager_config_dump(config); + + plugin_mangager_config_destory(config); + + return 0; +} +#endif \ No newline at end of file diff --git a/src/plugin_manager/plugin_manager_config.h b/src/plugin_manager/plugin_manager_config.h new file mode 100644 index 0000000..fa9164d --- /dev/null +++ b/src/plugin_manager/plugin_manager_config.h @@ -0,0 +1,34 @@ +#pragma once + +#include "plugin_manager_util.h" +#include "sdk/include/session.h" + +struct session_section_config +{ + char session_name[MAX_SESSION_NAME_LENGTH]; + char *cb_func_name; + enum session_event_type event; +}; + +struct plugin_section_config +{ + char *init_func_name; + char *exit_func_name; + char *lib_path; +}; + +struct plugin_manager_config +{ + char *prefix_path; // absolute path to stellar installation + char *file_path; // relative path to stellar installation + + int session_section_num; + struct session_section_config *session_section; // array + struct plugin_section_config plugin_section; +}; + +struct plugin_manager_config *plugin_mangager_config_create(); +void plugin_mangager_config_destory(struct plugin_manager_config *config); + +int plugin_mangager_config_parse(struct plugin_manager_config *config, const char *prefix, const char *file); +void plugin_mangager_config_dump(struct plugin_manager_config *config); \ No newline at end of file diff --git a/src/plugin_manager/plugin_manager_module.cpp b/src/plugin_manager/plugin_manager_module.cpp new file mode 100644 index 0000000..1e3011b --- /dev/null +++ b/src/plugin_manager/plugin_manager_module.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +#include + +#include "sdk/include/plugin.h" +#include "plugin_manager_util.h" +#include "plugin_manager_config.h" +#include "plugin_manager.h" + +struct plugin_manager_module_evcb +{ + char session_name[MAX_SESSION_NAME_LENGTH]; + fn_session_event_callback *event_cb_ptr; + enum session_event_type event; +}; + +struct plugin_manager_module +{ + char *prefix; + char *lib_path; + void *dl_handle; + + plugin_init_callback *init_cb_ptr; + plugin_exit_callback *exit_cb_ptr; + + struct plugin_manager_module_evcb *evcbs; + int evcbs_num; +}; + +void plugin_manager_module_close(struct plugin_manager_module *module) +{ + if (module) + { + if (module->exit_cb_ptr) + { + module->exit_cb_ptr(); + } + + if (module->dl_handle) + { + dlclose(module->dl_handle); + module->dl_handle = NULL; + } + + safe_free(module->prefix); + safe_free(module->lib_path); + safe_free(module->evcbs); + module->evcbs_num = 0; + + safe_free(module); + } +} + +struct plugin_manager_module *plugin_manager_module_open(struct plugin_manager_config *config) +{ + if (config == NULL) + { + return NULL; + } + + char dynamic_lib_path[4096] = {0}; + struct plugin_manager_module *module = safe_alloc(struct plugin_manager_module, 1); + + module->prefix = safe_dup(config->prefix_path); + module->lib_path = safe_dup(config->plugin_section.lib_path); + strcat_prefix_and_file(module->prefix, module->lib_path, dynamic_lib_path, sizeof(dynamic_lib_path)); + + module->evcbs_num = 0; + module->dl_handle = dlopen(dynamic_lib_path, RTLD_LAZY | RTLD_GLOBAL | RTLD_DEEPBIND); + if (module->dl_handle == NULL) + { + plugin_manager_log(ERROR, "can't dlopen %s, %s", dynamic_lib_path, dlerror()); + goto err; + } + + module->init_cb_ptr = (plugin_init_callback *)(dlsym(module->dl_handle, config->plugin_section.init_func_name)); + if (module->init_cb_ptr == NULL) + { + plugin_manager_log(ERROR, "can't find symbol name of '%s' in dynamic library %s, %s", + config->plugin_section.init_func_name, dynamic_lib_path, dlerror()); + goto err; + } + + module->exit_cb_ptr = (plugin_exit_callback *)(dlsym(module->dl_handle, config->plugin_section.exit_func_name)); + if (module->exit_cb_ptr == NULL) + { + plugin_manager_log(ERROR, "can't find symbol name of '%s' in dynamic library %s, %s", + config->plugin_section.exit_func_name, dynamic_lib_path, dlerror()); + goto err; + } + + if (config->session_section) + { + module->evcbs = safe_alloc(struct plugin_manager_module_evcb, config->session_section_num); + module->evcbs_num = config->session_section_num; + + for (int i = 0; i < config->session_section_num; i++) + { + struct session_section_config *session_config = &config->session_section[i]; + struct plugin_manager_module_evcb *event_cb = &module->evcbs[i]; + + strncpy(event_cb->session_name, session_config->session_name, strlen(session_config->session_name)); + event_cb->event = session_config->event; + event_cb->event_cb_ptr = (fn_session_event_callback *)(dlsym(module->dl_handle, session_config->cb_func_name)); + if (event_cb->event_cb_ptr == NULL) + { + plugin_manager_log(ERROR, "can't find symbol name of '%s' in dynamic library %s, %s", + session_config->cb_func_name, dynamic_lib_path, dlerror()); + goto err; + } + } + } + + return module; + +err: + plugin_manager_module_close(module); + + return NULL; +} + +int plugin_manager_module_register(struct plugin_manager *plug_mgr, struct plugin_manager_module *module) +{ + if (module && module->evcbs) + { + for (int i = 0; i < module->evcbs_num; i++) + { + struct plugin_manager_module_evcb *event_cb = &module->evcbs[i]; + + if (plugin_manager_register(plug_mgr, event_cb->session_name, event_cb->event, event_cb->event_cb_ptr) == -1) + { + plugin_manager_log(ERROR, "dynamic library '%s' failed to register the event callback function of session '%s'", module->lib_path, event_cb->session_name); + return -1; + } + } + } + + return 0; +} + +int plugin_manager_module_init(struct plugin_manager_module *module) +{ + struct timespec start; + struct timespec end; + + clock_gettime(CLOCK_MONOTONIC, &start); + int ret = module->init_cb_ptr(); + if (ret == -1) + { + plugin_manager_log(ERROR, "dynamic library '%s' initialization failed", module->lib_path); + return -1; + } + clock_gettime(CLOCK_MONOTONIC, &end); + + long elapsed = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000; + plugin_manager_log(INFO, "plugin '%s' init success, using '%lld' us", module->lib_path, elapsed); + + return 0; +} + +void plugin_manager_module_exit(struct plugin_manager_module *module) +{ + if (module && module->exit_cb_ptr) + { + module->exit_cb_ptr(); + } +} diff --git a/src/plugin_manager/plugin_manager_module.h b/src/plugin_manager/plugin_manager_module.h new file mode 100644 index 0000000..d765b4a --- /dev/null +++ b/src/plugin_manager/plugin_manager_module.h @@ -0,0 +1,12 @@ +#pragma once + +#include "plugin_manager_config.h" + +struct plugin_manager_module; + +struct plugin_manager_module *plugin_manager_module_open(struct plugin_manager_config *config); +int plugin_manager_module_register(struct plugin_manager *plug_mgr, struct plugin_manager_module *module); +int plugin_manager_module_init(struct plugin_manager_module *module); + +void plugin_manager_module_exit(struct plugin_manager_module *module); +void plugin_manager_module_close(struct plugin_manager_module *module); \ No newline at end of file diff --git a/src/plugin_manager/plugin_manager_util.cpp b/src/plugin_manager/plugin_manager_util.cpp new file mode 100644 index 0000000..1c4337d --- /dev/null +++ b/src/plugin_manager/plugin_manager_util.cpp @@ -0,0 +1,40 @@ +#include +#include + +#include "plugin_manager_util.h" + +char *safe_dup(const char *str) +{ + if (str == NULL) + { + return NULL; + } + + char *dup = safe_alloc(char, strlen(str) + 1); + memcpy(dup, str, strlen(str)); + + return dup; +} + +void strcat_prefix_and_file(const char *prefix, const char *file, char *buff, int size) +{ + char prefix_buffer[4096] = {0}; + char file_buffer[4096] = {0}; + char *ptr = NULL; + + memcpy(prefix_buffer, prefix, strlen(prefix)); + memcpy(file_buffer, file, strlen(file)); + + if (prefix_buffer[strlen(prefix_buffer) - 1] != '/') + { + prefix_buffer[strlen(prefix_buffer)] = '/'; + } + file_buffer[strcspn(file_buffer, "\r\n")] = 0; + + ptr = file_buffer; + if (file_buffer[0] == '.' && file_buffer[1] == '/') + { + ptr += 2; + } + snprintf(buff, size, "%s%s", prefix_buffer, ptr); +} \ No newline at end of file diff --git a/src/plugin_manager/plugin_manager_util.h b/src/plugin_manager/plugin_manager_util.h new file mode 100644 index 0000000..9c6c0fb --- /dev/null +++ b/src/plugin_manager/plugin_manager_util.h @@ -0,0 +1,61 @@ +#pragma once + +#include + +#define MAX_PLUGIN_NUM 512 +#define MAX_SESSION_NAME_LENGTH 32 + +/****************************************************************************** + * Malloc + ******************************************************************************/ + +#define safe_alloc(type, number) ((type *)calloc(number, sizeof(type))) + +#define safe_free(ptr) \ + { \ + if (ptr) \ + { \ + free(ptr); \ + ptr = NULL; \ + } \ + } + +char *safe_dup(const char *str); + +/****************************************************************************** + * Logger + ******************************************************************************/ + +enum plugin_manager_log_level +{ + DEBUG = 0x11, + INFO = 0x12, + ERROR = 0x13, +}; + +#ifndef plugin_manager_log +#define plugin_manager_log(level, format, ...) \ + { \ + switch (level) \ + { \ + case DEBUG: \ + fprintf(stdout, "PLUGIN_MANAGER [DEBUG] " format "\n", ##__VA_ARGS__); \ + fflush(stdout); \ + break; \ + case INFO: \ + fprintf(stdout, "PLUGIN_MANAGER [INFO] " format "\n", ##__VA_ARGS__); \ + fflush(stdout); \ + break; \ + case ERROR: \ + fprintf(stderr, "PLUGIN_MANAGER [ERROR] " format "\n", ##__VA_ARGS__); \ + fflush(stderr); \ + break; \ + } \ + } +#endif + +/****************************************************************************** + * Str + ******************************************************************************/ + +void strcat_prefix_and_file(const char *prefix, const char *file, char *buff, int size); diff --git a/src/plugin_manager/test/CMakeLists.txt b/src/plugin_manager/test/CMakeLists.txt new file mode 100644 index 0000000..7a1a880 --- /dev/null +++ b/src/plugin_manager/test/CMakeLists.txt @@ -0,0 +1,18 @@ +add_executable(gtest_plugin_manager + gtest_plugin_manager.cpp +) + +target_link_libraries( + gtest_plugin_manager + gtest_main + plugin_manager + session_manager + toml + dl +) + +include(GoogleTest) +gtest_discover_tests(gtest_plugin_manager) + +add_subdirectory(test_plugins/plugins_library) +file(COPY test_plugins/plugins_config DESTINATION ./) \ No newline at end of file diff --git a/src/plugin_manager/test/gtest_plugin_manager.cpp b/src/plugin_manager/test/gtest_plugin_manager.cpp new file mode 100644 index 0000000..8107b41 --- /dev/null +++ b/src/plugin_manager/test/gtest_plugin_manager.cpp @@ -0,0 +1,159 @@ +#include + +#include "../plugin_manager_config.h" +#include "../plugin_manager.h" +#include "session_manager.h" + +TEST(PLUGIN_MANAGER_TEST, plugin_manager_config_HTTP) +{ + char prefix_path[] = "./"; + char file_path[] = "./plugins_config/http_event_plugin/http_event_plugin.inf"; + + struct plugin_manager_config *config = plugin_mangager_config_create(); + EXPECT_TRUE(config != nullptr); + + EXPECT_TRUE(plugin_mangager_config_parse(config, prefix_path, file_path) == 0); + + EXPECT_STREQ(config->prefix_path, "./"); + EXPECT_STREQ(config->file_path, "./plugins_config/http_event_plugin/http_event_plugin.inf"); + EXPECT_STREQ(config->plugin_section.init_func_name, "http_event_plugin_init"); + EXPECT_STREQ(config->plugin_section.exit_func_name, "http_event_plugin_exit"); + EXPECT_STREQ(config->plugin_section.lib_path, "./test_plugins/plugins_library/http_event_plugin_test.so"); + + EXPECT_TRUE(config->session_section_num == 1); + EXPECT_STREQ(config->session_section[0].session_name, "HTTP"); + EXPECT_STREQ(config->session_section[0].cb_func_name, "http_event_plugin_entry"); + EXPECT_TRUE(config->session_section[0].event == (0x01 << 1 | 0x01 << 2 | 0x01 << 3 | 0x01 << 4 | 0x01 << 5)); + + plugin_mangager_config_dump(config); + plugin_mangager_config_destory(config); +} + +TEST(PLUGIN_MANAGER_TEST, plugin_manager_config_CUSTOM) +{ + char prefix_path[] = "./"; + char file_path[] = "./plugins_config/custom_event_plugin/custom_event_plugin.inf"; + + struct plugin_manager_config *config = plugin_mangager_config_create(); + EXPECT_TRUE(config != nullptr); + + EXPECT_TRUE(plugin_mangager_config_parse(config, prefix_path, file_path) == 0); + + EXPECT_STREQ(config->prefix_path, "./"); + EXPECT_STREQ(config->file_path, "./plugins_config/custom_event_plugin/custom_event_plugin.inf"); + EXPECT_STREQ(config->plugin_section.init_func_name, "custom_plugin_init"); + EXPECT_STREQ(config->plugin_section.exit_func_name, "custom_plugin_exit"); + EXPECT_STREQ(config->plugin_section.lib_path, "./test_plugins/plugins_library/custom_event_plugin_test.so"); + + EXPECT_TRUE(config->session_section_num == 2); + EXPECT_STREQ(config->session_section[0].session_name, "TCP"); + EXPECT_STREQ(config->session_section[0].cb_func_name, "custom_plugin_tcp_entry"); + EXPECT_TRUE(config->session_section[0].event == (0x01 << 1 | 0x01 << 2 | 0x01 << 3 | 0x01 << 4 | 0x01 << 5)); + EXPECT_STREQ(config->session_section[1].session_name, "CUSTOM"); + EXPECT_STREQ(config->session_section[1].cb_func_name, "custom_plugin_custom_entry"); + EXPECT_TRUE(config->session_section[1].event == (0x01 << 1) | (0x01 << 3) | (0x01 << 5)); + + plugin_mangager_config_dump(config); + plugin_mangager_config_destory(config); +} + +TEST(PLUGIN_MANAGER_TEST, plugin_manager_load) +{ + char prefix_path[] = "./"; + char file_path[] = "./plugins_config/plugins.inf"; + + struct plugin_manager *plug_mgr = plugin_manager_create(); + EXPECT_TRUE(plug_mgr != nullptr); + EXPECT_TRUE(plugin_manager_load(plug_mgr, prefix_path, file_path) == 0); + + plugin_manager_unload(plug_mgr); + plugin_manager_destory(plug_mgr); +} + +TEST(PLUGIN_MANAGER_TEST, plugin_manager_dispatch_HTTP) +{ + char prefix_path[] = "./"; + char file_path[] = "./plugins_config/plugins.inf"; + + const char *session_name = "HTTP"; + struct stellar_session session; + session.name = session_name; + struct stellar_session_event_data event_data; + event_data.s = &session; + event_data.plugin_ctx = NULL; // must be init to NULL + struct stellar_event event; + event.session_event_data = &event_data; + + struct plugin_manager *plug_mgr = plugin_manager_create(); + EXPECT_TRUE(plug_mgr != nullptr); + EXPECT_TRUE(plugin_manager_load(plug_mgr, prefix_path, file_path) == 0); + + event_data.type = SESSION_EVENT_OPENING; + plugin_manager_dispatch(plug_mgr, &event); + + event_data.type = SESSION_EVENT_RAWPKT; + plugin_manager_dispatch(plug_mgr, &event); + + event_data.type = SESSION_EVENT_ORDPKT; + plugin_manager_dispatch(plug_mgr, &event); + + event_data.type = SESSION_EVENT_META; + plugin_manager_dispatch(plug_mgr, &event); + + event_data.type = SESSION_EVENT_CLOSING; + plugin_manager_dispatch(plug_mgr, &event); + + event_data.type = SESSION_EVENT_ALL; + plugin_manager_dispatch(plug_mgr, &event); + + plugin_manager_unload(plug_mgr); + plugin_manager_destory(plug_mgr); +} + +TEST(PLUGIN_MANAGER_TEST, plugin_manager_dispatch_CUSTOM) +{ + char prefix_path[] = "./"; + char file_path[] = "./plugins_config/plugins.inf"; + + const char *session_name = "CUSTOM"; + struct stellar_session session; + session.name = session_name; + struct stellar_session_event_data event_data; + event_data.s = &session; + event_data.plugin_ctx = NULL; // must be init to NULL + struct stellar_event event; + event.session_event_data = &event_data; + + struct plugin_manager *plug_mgr = plugin_manager_create(); + EXPECT_TRUE(plug_mgr != nullptr); + EXPECT_TRUE(plugin_manager_load(plug_mgr, prefix_path, file_path) == 0); + + event_data.type = SESSION_EVENT_OPENING; + plugin_manager_dispatch(plug_mgr, &event); + + event_data.type = SESSION_EVENT_RAWPKT; + plugin_manager_dispatch(plug_mgr, &event); + + event_data.type = SESSION_EVENT_ORDPKT; + plugin_manager_dispatch(plug_mgr, &event); + + event_data.type = SESSION_EVENT_META; + plugin_manager_dispatch(plug_mgr, &event); + + event_data.type = SESSION_EVENT_CLOSING; + plugin_manager_dispatch(plug_mgr, &event); + + event_data.type = SESSION_EVENT_ALL; + plugin_manager_dispatch(plug_mgr, &event); + + plugin_manager_unload(plug_mgr); + plugin_manager_destory(plug_mgr); +} + +int main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + int ret = RUN_ALL_TESTS(); + + return ret; +} \ No newline at end of file diff --git a/src/plugin_manager/test/test_plugins/plugins_config/custom_event_plugin/custom_event_plugin.inf b/src/plugin_manager/test/test_plugins/plugins_config/custom_event_plugin/custom_event_plugin.inf new file mode 100644 index 0000000..26ba914 --- /dev/null +++ b/src/plugin_manager/test/test_plugins/plugins_config/custom_event_plugin/custom_event_plugin.inf @@ -0,0 +1,14 @@ +[PLUGINFO] +INIT_FUNC="custom_plugin_init" +EXIT_FUNC="custom_plugin_exit" +LIBRARY_PATH="./test_plugins/plugins_library/custom_event_plugin_test.so" + +# Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL" + +[SESSION_NAME.TCP] +SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"] +SESSION_EVENT_CALLBACK="custom_plugin_tcp_entry" + +[SESSION_NAME.CUSTOM] +SESSION_EVENT_TYPE=["SESSION_EVENT_OPENING","SESSION_EVENT_ORDPKT","SESSION_EVENT_CLOSING"] +SESSION_EVENT_CALLBACK="custom_plugin_custom_entry" \ No newline at end of file diff --git a/src/plugin_manager/test/test_plugins/plugins_config/http_event_plugin/http_event_plugin.inf b/src/plugin_manager/test/test_plugins/plugins_config/http_event_plugin/http_event_plugin.inf new file mode 100644 index 0000000..1dd124b --- /dev/null +++ b/src/plugin_manager/test/test_plugins/plugins_config/http_event_plugin/http_event_plugin.inf @@ -0,0 +1,10 @@ +[PLUGINFO] +INIT_FUNC="http_event_plugin_init" +EXIT_FUNC="http_event_plugin_exit" +LIBRARY_PATH="./test_plugins/plugins_library/http_event_plugin_test.so" + +# Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL" + +[SESSION_NAME.HTTP] +SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"] +SESSION_EVENT_CALLBACK="http_event_plugin_entry" \ No newline at end of file diff --git a/src/plugin_manager/test/test_plugins/plugins_config/plugins.inf b/src/plugin_manager/test/test_plugins/plugins_config/plugins.inf new file mode 100644 index 0000000..9382f91 --- /dev/null +++ b/src/plugin_manager/test/test_plugins/plugins_config/plugins.inf @@ -0,0 +1,4 @@ +# Relative path, relative to the installation path of stellar + +./plugins_config/http_event_plugin/http_event_plugin.inf +./plugins_config/custom_event_plugin/custom_event_plugin.inf \ No newline at end of file diff --git a/src/plugin_manager/test/test_plugins/plugins_library/CMakeLists.txt b/src/plugin_manager/test/test_plugins/plugins_library/CMakeLists.txt new file mode 100644 index 0000000..df47c82 --- /dev/null +++ b/src/plugin_manager/test/test_plugins/plugins_library/CMakeLists.txt @@ -0,0 +1,9 @@ +add_library(custom_event_plugin_test SHARED + custom_event_plugin.cpp +) +set_target_properties(custom_event_plugin_test PROPERTIES PREFIX "") + +add_library(http_event_plugin_test SHARED + http_event_plugin.cpp +) +set_target_properties(http_event_plugin_test PROPERTIES PREFIX "") \ No newline at end of file diff --git a/src/plugin_manager/test/test_plugins/plugins_library/custom_event_plugin.cpp b/src/plugin_manager/test/test_plugins/plugins_library/custom_event_plugin.cpp new file mode 100644 index 0000000..0d7f1c4 --- /dev/null +++ b/src/plugin_manager/test/test_plugins/plugins_library/custom_event_plugin.cpp @@ -0,0 +1,58 @@ +#include "session.h" +#include "packet.h" +#include "plugin.h" + +#include +#include +#include + +static char *g_handler = NULL; + +static void *custom_decode(const char *payload, uint16_t len, void **pme) +{ + return NULL; +} + +extern "C" void custom_plugin_tcp_entry(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) +{ + char **per_session_pme = (char **)pme; + + printf("RUN custom_plugin_tcp_entry, event: %d\n", event); + + struct stellar_session_event_extras *info = (struct stellar_session_event_extras *)custom_decode(payload, len, pme); + struct stellar_session *new_session = session_manager_session_derive(s, "CUSTOM"); + + session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info); + session_manager_trigger_event(new_session, SESSION_EVENT_META, info); +} + +extern "C" void custom_plugin_custom_entry(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) +{ + char **per_session_pme = (char **)pme; + + printf("RUN custom_plugin_custom_entry, event: %d\n", event); +} + +extern "C" int custom_plugin_init(void) +{ + printf("RUN custom_plugin_init\n"); + + if (g_handler == NULL) + { + g_handler = (char *)malloc(1024); + snprintf(g_handler, 1024, "222222"); + } + + return 0; +} + +extern "C" void custom_plugin_exit(void) +{ + printf("RUN custom_plugin_exit\n"); + + if (g_handler) + { + free(g_handler); + g_handler = NULL; + } +} \ No newline at end of file diff --git a/src/plugin_manager/test/test_plugins/plugins_library/http_event_plugin.cpp b/src/plugin_manager/test/test_plugins/plugins_library/http_event_plugin.cpp new file mode 100644 index 0000000..e41d8b6 --- /dev/null +++ b/src/plugin_manager/test/test_plugins/plugins_library/http_event_plugin.cpp @@ -0,0 +1,82 @@ +#include "session.h" +#include "packet.h" +#include "plugin.h" + +#include +#include + +static char *g_handler = NULL; + +struct per_session_pme_info +{ + int flag; + char data[16]; +}; + +extern "C" void http_event_plugin_entry(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) +{ + struct per_session_pme_info **per_session_pme = (struct per_session_pme_info **)pme; + + printf("RUN http_event_plugin_entry, event: %d\n", event); + + if (event & SESSION_EVENT_OPENING) + { + if (*per_session_pme == NULL) + { + struct per_session_pme_info *cur_ctx = (struct per_session_pme_info *)malloc(sizeof(struct per_session_pme_info)); + snprintf(cur_ctx->data, 6, "******"); + *per_session_pme = *&cur_ctx; + printf("http_event_plugin_entry->opening_handler\n"); + } + } + + if (event & SESSION_EVENT_RAWPKT) + { + printf("http_event_plugin_entry->rawpkt_handler\n"); + } + + if (event & SESSION_EVENT_ORDPKT) + { + printf("http_event_plugin_entry->ordpkt_handler\n"); + } + + if (event & SESSION_EVENT_META) + { + printf("http_event_plugin_entry->meta_handler\n"); + } + + if (event & SESSION_EVENT_CLOSING) + { + if (*per_session_pme) + { + printf("http_event_plugin_entry->closing_hanler\n"); + + free(*per_session_pme); + *per_session_pme = NULL; + } + } +} + +extern "C" int http_event_plugin_init(void) +{ + printf("RUN http_event_plugin_init\n"); + + if (g_handler == NULL) + { + g_handler = (char *)malloc(1024); + snprintf(g_handler, 1024, "111111"); + } + + return 0; +} + +extern "C" void http_event_plugin_exit(void) +{ + printf("RUN http_event_plugin_exit\n"); + + if (g_handler) + { + free(g_handler); + g_handler = NULL; + } +} \ No newline at end of file diff --git a/src/protocol_decoder/http/http.cpp b/src/protocol_decoder/http/http.cpp index 408f149..b3a9205 100644 --- a/src/protocol_decoder/http/http.cpp +++ b/src/protocol_decoder/http/http.cpp @@ -1,15 +1,10 @@ -#include "http.h" -#include "session_manager.h" +#include "sdk/include/session.h" -void http_decoder(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) +void http_decoder(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) { struct stellar_session_event_extras *info; - struct stellar_session *new_session=session_manager_session_derive(s, "HTTP"); + struct stellar_session *new_session = session_manager_session_derive(s, "HTTP"); + session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info); session_manager_trigger_event(new_session, SESSION_EVENT_META, info); -} - -int http_peek(const struct stellar_session *s, const char *payload, uint32_t len) -{ - return 0; } \ No newline at end of file diff --git a/src/session_manager/session_manager.cpp b/src/session_manager/session_manager.cpp index ab70071..7ea5643 100644 --- a/src/session_manager/session_manager.cpp +++ b/src/session_manager/session_manager.cpp @@ -1,8 +1,12 @@ +#include + +#include "sdk/include/session.h" #include "session_manager.h" struct session_manager { - struct stellar_session **tcp_table, **udp_table; + struct stellar_session **tcp_table; + struct stellar_session **udp_table; }; struct session_manager *session_manager_init() @@ -10,16 +14,12 @@ struct session_manager *session_manager_init() return nullptr; } - -void session_manager_trigger_event(struct stellar_session *s, - enum session_event_type type, - struct stellar_session_event_extras *info) +void session_manager_trigger_event(struct stellar_session *s, enum session_event_type type, struct stellar_session_event_extras *info) { return; } -struct stellar_session *session_manager_session_derive(const struct stellar_session *this_session, - const char *new_session_type_name) +struct stellar_session *session_manager_session_derive(const struct stellar_session *this_session, const char *session_name) { return nullptr; } @@ -29,8 +29,54 @@ struct stellar_event *session_manager_commit(struct session_manager *h, struct s return nullptr; }; - struct stellar_event *session_manager_fetch_event(struct session_manager *h) { return nullptr; } + +/****************************************************************************** + * API: between stellar_event and plugin_manager + ******************************************************************************/ + +struct session_plugin_ctx *stellar_event_get_plugin_ctx(struct stellar_event *event) +{ + return event->session_event_data->plugin_ctx; +} + +void stellar_event_set_plugin_ctx(struct stellar_event *event, struct session_plugin_ctx *plugin_ctx) +{ + event->session_event_data->plugin_ctx = plugin_ctx; +} + +enum session_event_type stellar_event_get_type(struct stellar_event *event) +{ + return event->session_event_data->type; +} + +const char *stellar_event_get_session_name(struct stellar_event *event) +{ + return event->session_event_data->s->name; +} + +const struct stellar_session *stellar_event_get_session(struct stellar_event *event) +{ + return event->session_event_data->s; +} + +// TODO +struct stellar_packet *stellar_event_get_packet(struct stellar_event *event) +{ + return NULL; +} + +// TODO +const char *stellar_event_get_payload(struct stellar_event *event) +{ + return NULL; +} + +// TODO +uint16_t stellar_event_get_payload_length(struct stellar_event *event) +{ + return 0; +} diff --git a/src/session_manager/session_manager.h b/src/session_manager/session_manager.h index 6d17e79..1bef25f 100644 --- a/src/session_manager/session_manager.h +++ b/src/session_manager/session_manager.h @@ -2,8 +2,6 @@ #include "sdk/include/session.h" -struct per_session_evcb_runtime_ctx; - struct stellar_session_event_data { struct stellar_session *s; @@ -18,10 +16,6 @@ struct session_manager *session_manager_init(); struct stellar_event *session_manager_commit(struct session_manager *session_mgr, struct stellar_packet *pkt); struct stellar_event *session_manager_fetch_event(struct session_manager *session_mgr); -// interfaces for data interaction between stellar event and plugin manager -struct session_plugin_ctx *stellar_event_get_plugin_ctx(struct stellar_event *event); -void stellar_event_set_plugin_ctx(struct stellar_event *event, struct session_plugin_ctx *plugin_ctx); - struct stellar_session { stellar_session_type type; @@ -29,3 +23,18 @@ struct stellar_session void *addr; void *data; }; + +/****************************************************************************** + * API: between stellar_event and plugin_manager + ******************************************************************************/ + +struct session_plugin_ctx *stellar_event_get_plugin_ctx(struct stellar_event *event); +void stellar_event_set_plugin_ctx(struct stellar_event *event, struct session_plugin_ctx *plugin_ctx); + +enum session_event_type stellar_event_get_type(struct stellar_event *event); +const char *stellar_event_get_session_name(struct stellar_event *event); +const struct stellar_session *stellar_event_get_session(struct stellar_event *event); +struct stellar_packet *stellar_event_get_packet(struct stellar_event *event); + +const char *stellar_event_get_payload(struct stellar_event *event); +uint16_t stellar_event_get_payload_length(struct stellar_event *event); \ No newline at end of file From 5c790085ebbf5812a34f6fcbc9c2e4398a723667 Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Wed, 3 Aug 2022 19:46:43 +0800 Subject: [PATCH 10/17] Plugin management support pm_session_dettach_me and pm_session_dettach_others Add test cases for pm_session_dettach_me and pm_session_dettach_others --- readme.md | 18 +- sdk/example/custom_event_plugin.cpp | 125 +++- sdk/example/http_event_plugin.cpp | 59 +- .../custom_event_plugin.inf | 9 +- .../http_event_plugin/http_event_plugin.inf | 1 + sdk/include/http.h | 3 +- sdk/include/plugin.h | 22 +- sdk/include/session.h | 24 +- src/main.cpp | 5 +- src/plugin_manager/plugin_manager.cpp | 169 ++++-- src/plugin_manager/plugin_manager.h | 25 +- src/plugin_manager/plugin_manager_config.cpp | 115 +--- src/plugin_manager/plugin_manager_config.h | 22 +- src/plugin_manager/plugin_manager_module.cpp | 111 ++-- src/plugin_manager/plugin_manager_module.h | 22 +- src/plugin_manager/plugin_manager_util.cpp | 93 ++- src/plugin_manager/plugin_manager_util.h | 22 +- src/plugin_manager/test/CMakeLists.txt | 3 + .../test/gtest_plugin_manager.cpp | 539 ++++++++++++++++-- .../custom_event_plugin.inf | 13 +- .../http_event_plugin/http_event_plugin.inf | 1 + .../plugins_library/custom_event_plugin.cpp | 216 ++++++- .../plugins_library/http_event_plugin.cpp | 77 ++- src/protocol_decoder/http/http.cpp | 4 + src/session_manager/session_manager.cpp | 21 +- src/session_manager/session_manager.h | 11 +- 26 files changed, 1348 insertions(+), 382 deletions(-) diff --git a/readme.md b/readme.md index aed4fcc..5b375b8 100644 --- a/readme.md +++ b/readme.md @@ -39,12 +39,26 @@ packet_io_loop() ## Plugin Manager Plugin Management APIs + ``` -pm_session_dettach_me(pm, session); -pm_session_dettach_others(pm, session); +/* + * The plugin manager just set the skip flag and don't call this event callback next. + * Before calling pm_session_dettach_me, the current plugin must release related resources for the current session. + */ +pm_session_dettach_me(session); + +/* + * The plugin manager uses ERROR_EVENT_DETTACH to call other plugin error callbacks, + * and when the plugin error callback handler is called, + * the error callback handler must release the relevant resources for the current session. + */ +pm_session_dettach_others(session); ``` + ## Session Manager + Session Management APIs + ``` session_drop_current_packet(session); session_set_ratelimit_group(session, rl_group_id); diff --git a/sdk/example/custom_event_plugin.cpp b/sdk/example/custom_event_plugin.cpp index 0d7f1c4..a1d4d03 100644 --- a/sdk/example/custom_event_plugin.cpp +++ b/sdk/example/custom_event_plugin.cpp @@ -5,6 +5,7 @@ #include #include #include +#include static char *g_handler = NULL; @@ -13,29 +14,121 @@ static void *custom_decode(const char *payload, uint16_t len, void **pme) return NULL; } -extern "C" void custom_plugin_tcp_entry(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) +struct tcp_session_pme { - char **per_session_pme = (char **)pme; + char data[16]; + /* data */ +}; - printf("RUN custom_plugin_tcp_entry, event: %d\n", event); +struct custom_session_pme +{ + char data[16]; + /* data */ +}; - struct stellar_session_event_extras *info = (struct stellar_session_event_extras *)custom_decode(payload, len, pme); - struct stellar_session *new_session = session_manager_session_derive(s, "CUSTOM"); - - session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info); - session_manager_trigger_event(new_session, SESSION_EVENT_META, info); +static struct tcp_session_pme *tcp_session_pme_create() +{ + struct tcp_session_pme *pme = (struct tcp_session_pme *)calloc(1, sizeof(struct tcp_session_pme)); + return pme; } -extern "C" void custom_plugin_custom_entry(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) +static void tcp_session_pme_destory(struct tcp_session_pme *pme) { - char **per_session_pme = (char **)pme; - - printf("RUN custom_plugin_custom_entry, event: %d\n", event); + if (pme) + { + free(pme); + pme = NULL; + } } -extern "C" int custom_plugin_init(void) +static struct custom_session_pme *custom_session_pme_create() { - printf("RUN custom_plugin_init\n"); + struct custom_session_pme *pme = (struct custom_session_pme *)calloc(1, sizeof(struct custom_session_pme)); + return pme; +} + +static void custom_session_pme_destory(struct custom_session_pme *pme) +{ + if (pme) + { + free(pme); + pme = NULL; + } +} + +extern "C" void custom_event_plugin_error(const struct stellar_session *session, enum error_event_type event, void **pme) +{ + if (strcmp(stellar_session_get_name(session), "TCP") == 0) + { + struct tcp_session_pme **per_tcp_session_pme = (struct tcp_session_pme **)pme; + printf("RUN custom_event_plugin_error, session_name: 'TCP', error_event_type: %d, pme->data: %s\n", event, (*per_tcp_session_pme) == NULL ? "NULL" : (*per_tcp_session_pme)->data); + tcp_session_pme_destory(*per_tcp_session_pme); + } + + if (strcmp(stellar_session_get_name(session), "CUSTOM") == 0) + { + struct custom_session_pme **per_custom_session_pme = (struct custom_session_pme **)pme; + printf("RUN custom_event_plugin_error, session_name: 'CUSTOM', error_event_type: %d, pme->data: %s\n", event, (*per_custom_session_pme) == NULL ? "NULL" : (*per_custom_session_pme)->data); + custom_session_pme_destory(*per_custom_session_pme); + } +} + +extern "C" void custom_event_plugin_tcp_entry(const struct stellar_session *session, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) +{ + struct tcp_session_pme **per_tcp_session_pme = (struct tcp_session_pme **)pme; + + printf("RUN custom_event_plugin_tcp_entry, event: %d\n", event); + + if (event & SESSION_EVENT_OPENING) + { + if (*per_tcp_session_pme == NULL) + { + struct tcp_session_pme *cur_ctx = tcp_session_pme_create(); + snprintf(cur_ctx->data, 6, "tcp******"); + *per_tcp_session_pme = *&cur_ctx; + } + } + + if (event & SESSION_EVENT_ORDPKT) + { + struct stellar_session_event_extras *info = (struct stellar_session_event_extras *)custom_decode(payload, len, pme); + struct stellar_session *new_session = session_manager_session_derive(session, "CUSTOM"); + + session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info); + session_manager_trigger_event(new_session, SESSION_EVENT_META, info); + } + + if (event & SESSION_EVENT_CLOSING) + { + tcp_session_pme_destory(*per_tcp_session_pme); + } +} + +extern "C" void custom_event_plugin_custom_entry(const struct stellar_session *session, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) +{ + struct custom_session_pme **per_custom_session_pme = (struct custom_session_pme **)pme; + + printf("RUN custom_event_plugin_custom_entry, event: %d\n", event); + + if (event & SESSION_EVENT_OPENING) + { + if (*per_custom_session_pme == NULL) + { + struct custom_session_pme *cur_ctx = custom_session_pme_create(); + snprintf(cur_ctx->data, 6, "custom******"); + *per_custom_session_pme = *&cur_ctx; + } + } + + if (event & SESSION_EVENT_CLOSING) + { + custom_session_pme_destory(*per_custom_session_pme); + } +} + +extern "C" int custom_event_plugin_init(void) +{ + printf("RUN custom_event_plugin_init\n"); if (g_handler == NULL) { @@ -46,9 +139,9 @@ extern "C" int custom_plugin_init(void) return 0; } -extern "C" void custom_plugin_exit(void) +extern "C" void custom_event_plugin_exit(void) { - printf("RUN custom_plugin_exit\n"); + printf("RUN custom_event_plugin_exit\n"); if (g_handler) { diff --git a/sdk/example/http_event_plugin.cpp b/sdk/example/http_event_plugin.cpp index e41d8b6..0cf5364 100644 --- a/sdk/example/http_event_plugin.cpp +++ b/sdk/example/http_event_plugin.cpp @@ -4,56 +4,77 @@ #include #include +#include static char *g_handler = NULL; -struct per_session_pme_info +struct http_session_pme { - int flag; char data[16]; + /* data */; }; -extern "C" void http_event_plugin_entry(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) +static struct http_session_pme *http_session_pme_create() { - struct per_session_pme_info **per_session_pme = (struct per_session_pme_info **)pme; + struct http_session_pme *pme = (struct http_session_pme *)calloc(1, sizeof(struct http_session_pme)); + return pme; +} + +static void http_session_pme_destory(struct http_session_pme *pme) +{ + if (pme) + { + free(pme); + pme = NULL; + } +} + +extern "C" void http_event_plugin_error(const struct stellar_session *session, enum error_event_type event, void **pme) +{ + if (strcmp(stellar_session_get_name(session), "HTTP") == 0) + { + struct http_session_pme **per_http_session_pme = (struct http_session_pme **)pme; + printf("RUN http_event_plugin_error, session_name: 'HTTP', error_event_type: %d, pme->data: %s\n", event, (*per_http_session_pme) == NULL ? "NULL" : (*per_http_session_pme)->data); + http_session_pme_destory(*per_http_session_pme); + } +} + +extern "C" void http_event_plugin_entry(const struct stellar_session *session, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) +{ + struct http_session_pme **per_http_session_pme = (struct http_session_pme **)pme; printf("RUN http_event_plugin_entry, event: %d\n", event); if (event & SESSION_EVENT_OPENING) { - if (*per_session_pme == NULL) + if (*per_http_session_pme == NULL) { - struct per_session_pme_info *cur_ctx = (struct per_session_pme_info *)malloc(sizeof(struct per_session_pme_info)); - snprintf(cur_ctx->data, 6, "******"); - *per_session_pme = *&cur_ctx; - printf("http_event_plugin_entry->opening_handler\n"); + struct http_session_pme *cur_ctx = http_session_pme_create(); + snprintf(cur_ctx->data, 6, "http******"); + *per_http_session_pme = *&cur_ctx; } } if (event & SESSION_EVENT_RAWPKT) { - printf("http_event_plugin_entry->rawpkt_handler\n"); + // TODO + pm_session_dettach_me(session); } if (event & SESSION_EVENT_ORDPKT) { - printf("http_event_plugin_entry->ordpkt_handler\n"); + // TODO + pm_session_dettach_others(session); } if (event & SESSION_EVENT_META) { - printf("http_event_plugin_entry->meta_handler\n"); + // TODO } if (event & SESSION_EVENT_CLOSING) { - if (*per_session_pme) - { - printf("http_event_plugin_entry->closing_hanler\n"); - - free(*per_session_pme); - *per_session_pme = NULL; - } + http_session_pme_destory(*per_http_session_pme); } } diff --git a/sdk/example/plugins/custom_event_plugin/custom_event_plugin.inf b/sdk/example/plugins/custom_event_plugin/custom_event_plugin.inf index c7d3fe3..3718be0 100644 --- a/sdk/example/plugins/custom_event_plugin/custom_event_plugin.inf +++ b/sdk/example/plugins/custom_event_plugin/custom_event_plugin.inf @@ -1,14 +1,15 @@ [PLUGINFO] -INIT_FUNC="custom_plugin_init" -EXIT_FUNC="custom_plugin_exit" +INIT_FUNC="custom_event_plugin_init" +EXIT_FUNC="custom_event_plugin_exit" +ERROR_FUNC="custom_event_plugin_error" LIBRARY_PATH="./plugins/custom_event_plugin/custom_event_plugin.so" # Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL" [SESSION_NAME.TCP] SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"] -SESSION_EVENT_CALLBACK="custom_plugin_tcp_entry" +SESSION_EVENT_CALLBACK="custom_event_plugin_tcp_entry" [SESSION_NAME.CUSTOM] SESSION_EVENT_TYPE=["SESSION_EVENT_OPENING","SESSION_EVENT_ORDPKT","SESSION_EVENT_CLOSING"] -SESSION_EVENT_CALLBACK="custom_plugin_custom_entry" \ No newline at end of file +SESSION_EVENT_CALLBACK="custom_event_plugin_custom_entry" \ No newline at end of file diff --git a/sdk/example/plugins/http_event_plugin/http_event_plugin.inf b/sdk/example/plugins/http_event_plugin/http_event_plugin.inf index a58a405..e0f2704 100644 --- a/sdk/example/plugins/http_event_plugin/http_event_plugin.inf +++ b/sdk/example/plugins/http_event_plugin/http_event_plugin.inf @@ -1,6 +1,7 @@ [PLUGINFO] INIT_FUNC="http_event_plugin_init" EXIT_FUNC="http_event_plugin_exit" +ERROR_FUNC="http_event_plugin_error" LIBRARY_PATH="./plugins/http_event_plugin/http_event_plugin.so" # Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL" diff --git a/sdk/include/http.h b/sdk/include/http.h index 6250055..4db1126 100644 --- a/sdk/include/http.h +++ b/sdk/include/http.h @@ -2,4 +2,5 @@ #include "session.h" -void http_decoder(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme); \ No newline at end of file +void http_decoder(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme); +void http_error_cb(const struct stellar_session *s, enum error_event_type event, void **pme); \ No newline at end of file diff --git a/sdk/include/plugin.h b/sdk/include/plugin.h index d213f7c..0d6c346 100644 --- a/sdk/include/plugin.h +++ b/sdk/include/plugin.h @@ -1,15 +1,25 @@ -#pragma once +#ifndef _PLUGIN_H +#define _PLUGIN_H + +#ifdef __cpluscplus +extern "C" +{ +#endif #include "session.h" typedef int plugin_init_callback(void); typedef void plugin_exit_callback(void); -void plugin_remove_from_session_event(struct stellar_event *ev, struct stellar_session *s); - /****************************************************************************** - * Public API: between plugin and plugin_manager + * Public API For Plugin ******************************************************************************/ -// TODO -// TODO \ No newline at end of file +void pm_session_dettach_me(const struct stellar_session *session); +void pm_session_dettach_others(const struct stellar_session *session); + +#ifdef __cpluscplus +} +#endif + +#endif \ No newline at end of file diff --git a/sdk/include/session.h b/sdk/include/session.h index 19bb566..8c4b25f 100644 --- a/sdk/include/session.h +++ b/sdk/include/session.h @@ -1,4 +1,10 @@ -#pragma once +#ifndef _SESSION_H +#define _SESSION_H + +#ifdef __cpluscplus +extern "C" +{ +#endif #include "packet.h" #include "event.h" @@ -34,9 +40,23 @@ enum session_event_type SESSION_EVENT_ALL = (0x01 << 1 | 0x01 << 2 | 0x01 << 3 | 0x01 << 4 | 0x01 << 5), }; +enum error_event_type +{ + ERROR_EVENT_DETTACH = (0x1 << 10), +}; + struct stellar_session_event_extras; +typedef void(fn_session_error_callback)(const struct stellar_session *s, enum error_event_type event, void **pme); typedef void(fn_session_event_callback)(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme); void session_manager_trigger_event(struct stellar_session *s, enum session_event_type type, struct stellar_session_event_extras *info); -struct stellar_session *session_manager_session_derive(const struct stellar_session *this_session, const char *session_name); \ No newline at end of file +struct stellar_session *session_manager_session_derive(const struct stellar_session *this_session, const char *session_name); + +const char *stellar_session_get_name(const struct stellar_session *session); + +#ifdef __cpluscplus +} +#endif + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 8d1655d..2b1d541 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -59,12 +59,11 @@ int main(int argc, char ** argv) struct plugin_manager *plug_mgr = plugin_manager_create(); // register build-in plugin - plugin_manager_register(plug_mgr, "HTTP", SESSION_EVENT_ALL, http_decoder); + plugin_manager_register(plug_mgr, "HTTP", SESSION_EVENT_ALL, http_decoder, http_error_cb); // load external plugins - char prefix_path[] = "/op/tsg/stellar/"; char file_path[] = "./plugs/plugins.inf"; - plugin_manager_load(plug_mgr, prefix_path, file_path); + plugin_manager_load(plug_mgr, file_path); //packet_io_init struct packet_io_device *dev = packet_io_init(1, "stellar", "cap0"); diff --git a/src/plugin_manager/plugin_manager.cpp b/src/plugin_manager/plugin_manager.cpp index 7b6492a..6baaad6 100644 --- a/src/plugin_manager/plugin_manager.cpp +++ b/src/plugin_manager/plugin_manager.cpp @@ -1,48 +1,49 @@ -#include "deps/uthash/uthash.h" -#include "session_manager.h" -#include "plugin_manager_util.h" -#include "plugin_manager_config.h" -#include "plugin_manager_module.h" - #include #include +#include "uthash/uthash.h" + +#include "session_manager.h" +#include "plugin_manager_module.h" + /****************************************************************************** * CallBack Runtime (For Per Session) ******************************************************************************/ -struct eventcb_runtime +struct callback_runtime { int skip; void *cb_args; enum session_event_type event; - fn_session_event_callback *cb; + fn_session_event_callback *event_cb; + fn_session_error_callback *error_cb; }; struct session_plugin_ctx { - int current_plugin_index; - int eventcb_num; - struct eventcb_runtime *eventcbs; + int callback_index; + int callback_num; + struct callback_runtime *callbacks; }; /****************************************************************************** * CallBack Static (For Per Plugin Manager) ******************************************************************************/ -struct eventcb_static +struct callback_static { enum session_event_type event; - fn_session_event_callback *cb; + fn_session_event_callback *event_cb; + fn_session_error_callback *error_cb; }; struct plugin_manager_eventcb { char session_name[MAX_SESSION_NAME_LENGTH]; // key - int eventcb_num; // val size - struct eventcb_static *eventcbs; // val: dynamic array + int callback_num; // val size + struct callback_static *callbacks; // val: dynamic array UT_hash_handle hh; }; @@ -84,15 +85,16 @@ static struct session_plugin_ctx *plugin_manager_create_plugin_ctx(struct plugin else { struct session_plugin_ctx *plug_ctx = safe_alloc(struct session_plugin_ctx, 1); - plug_ctx->eventcb_num = elem->eventcb_num; - plug_ctx->eventcbs = safe_alloc(struct eventcb_runtime, plug_ctx->eventcb_num); + plug_ctx->callback_num = elem->callback_num; + plug_ctx->callbacks = safe_alloc(struct callback_runtime, plug_ctx->callback_num); - for (int i = 0; i < plug_ctx->eventcb_num; i++) + for (int i = 0; i < plug_ctx->callback_num; i++) { - plug_ctx->eventcbs[i].skip = 0; - plug_ctx->eventcbs[i].event = elem->eventcbs[i].event; - plug_ctx->eventcbs[i].cb = elem->eventcbs[i].cb; - plug_ctx->eventcbs[i].cb_args = NULL; + plug_ctx->callbacks[i].skip = 0; + plug_ctx->callbacks[i].event = elem->callbacks[i].event; + plug_ctx->callbacks[i].event_cb = elem->callbacks[i].event_cb; + plug_ctx->callbacks[i].error_cb = elem->callbacks[i].error_cb; + plug_ctx->callbacks[i].cb_args = NULL; } return plug_ctx; @@ -103,7 +105,7 @@ static void plugin_manager_destory_plugin_ctx(struct session_plugin_ctx *plug_ct { if (plug_ctx) { - safe_free(plug_ctx->eventcbs); + safe_free(plug_ctx->callbacks); safe_free(plug_ctx); } } @@ -112,26 +114,20 @@ static void plugin_manager_destory_plugin_ctx(struct session_plugin_ctx *plug_ct * Tools for managing plugins ******************************************************************************/ -static int plugin_manager_parse_plugins(struct plugin_manager *plug_mgr, const char *prefix, const char *file) +static int plugin_manager_parse_plugins(struct plugin_manager *plug_mgr, const char *file) { - char plugin_inf[4096] = {0}; char line_buffer[4096] = {0}; - if (strlen(prefix) <= 0) - { - plugin_manager_log(ERROR, "Invalid parameter, plugin config file prefix cannot be empty"); - return -1; - } + if (strlen(file) <= 0) { plugin_manager_log(ERROR, "Invalid parameter, plugin config file name cannot be empty"); return -1; } - strcat_prefix_and_file(prefix, file, plugin_inf, sizeof(plugin_inf)); - FILE *fp = fopen(plugin_inf, "r"); + FILE *fp = fopen(file, "r"); if (fp == NULL) { - plugin_manager_log(ERROR, "can't open %s, %s", plugin_inf, strerror(errno)); + plugin_manager_log(ERROR, "can't open %s, %s", file, strerror(errno)); return -1; } @@ -142,6 +138,7 @@ static int plugin_manager_parse_plugins(struct plugin_manager *plug_mgr, const c memset(line_buffer, 0, sizeof(line_buffer)); continue; } + line_buffer[strcspn(line_buffer, "\r\n")] = 0; if (plug_mgr->used_config_num >= MAX_PLUGIN_NUM) { @@ -150,7 +147,7 @@ static int plugin_manager_parse_plugins(struct plugin_manager *plug_mgr, const c } struct plugin_manager_config *config = plugin_mangager_config_create(); - if (plugin_mangager_config_parse(config, prefix, line_buffer) == -1) + if (plugin_mangager_config_parse(config, line_buffer) == -1) { plugin_mangager_config_destory(config); goto err; @@ -186,6 +183,8 @@ static int plugin_manager_open_plugins(struct plugin_manager *plug_mgr) { return -1; } + plugin_manager_module_dump(module, config); + plug_mgr->modules[plug_mgr->used_module_num] = module; plug_mgr->used_module_num++; } @@ -267,9 +266,9 @@ static void plugin_manager_deparse_plugins(struct plugin_manager *plug_mgr) * Public API for managing plugins ******************************************************************************/ -int plugin_manager_load(struct plugin_manager *plug_mgr, const char *prefix, const char *file) +int plugin_manager_load(struct plugin_manager *plug_mgr, const char *file) { - if (plugin_manager_parse_plugins(plug_mgr, prefix, file) == -1) + if (plugin_manager_parse_plugins(plug_mgr, file) == -1) { return -1; } @@ -324,7 +323,7 @@ void plugin_manager_destory(struct plugin_manager *plug_mgr) { HASH_DEL(plug_mgr->evcb_htable, elem); - safe_free(elem->eventcbs); + safe_free(elem->callbacks); safe_free(elem); } @@ -337,7 +336,7 @@ void plugin_manager_destory(struct plugin_manager *plug_mgr) } } -int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_name, enum session_event_type event, fn_session_event_callback *cb) +int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_name, enum session_event_type event, fn_session_event_callback *event_cb, fn_session_error_callback *error_cb) { if (strlen(session_name) <= 0) { @@ -351,23 +350,30 @@ int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session return -1; } - if (cb == NULL) + if (event_cb == NULL) { - plugin_manager_log(ERROR, "invalid parameter, the callback corresponding to the session name '%s' is null", session_name); + plugin_manager_log(ERROR, "invalid parameter, the event callback corresponding to the session name '%s' is null", session_name); + return -1; + } + + if (error_cb == NULL) + { + plugin_manager_log(ERROR, "invalid parameter, the error callback corresponding to the session name '%s' is null", session_name); return -1; } struct plugin_manager_eventcb *elem; HASH_FIND_STR(plug_mgr->evcb_htable, session_name, elem); - // session_name exists, add a new cb to the end of the eventcbs dynamic array + // session_name exists, add a new cb to the end of the callbacks dynamic array if (elem) { - elem->eventcbs = (struct eventcb_static *)realloc(elem->eventcbs, (elem->eventcb_num + 1) * sizeof(struct eventcb_static)); + elem->callbacks = (struct callback_static *)realloc(elem->callbacks, (elem->callback_num + 1) * sizeof(struct callback_static)); - elem->eventcbs[elem->eventcb_num].event = event; - elem->eventcbs[elem->eventcb_num].cb = cb; + elem->callbacks[elem->callback_num].event = event; + elem->callbacks[elem->callback_num].event_cb = event_cb; + elem->callbacks[elem->callback_num].error_cb = error_cb; - elem->eventcb_num++; + elem->callback_num++; } // session_name does not exist, allocate a new node elem, and add elem to the hash table else @@ -375,12 +381,13 @@ int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session elem = safe_alloc(struct plugin_manager_eventcb, 1); memcpy(elem->session_name, session_name, strlen(session_name)); - elem->eventcbs = (struct eventcb_static *)realloc(elem->eventcbs, (elem->eventcb_num + 1) * sizeof(struct eventcb_static)); + elem->callbacks = (struct callback_static *)realloc(elem->callbacks, (elem->callback_num + 1) * sizeof(struct callback_static)); - elem->eventcbs[elem->eventcb_num].event = event; - elem->eventcbs[elem->eventcb_num].cb = cb; + elem->callbacks[elem->callback_num].event = event; + elem->callbacks[elem->callback_num].event_cb = event_cb; + elem->callbacks[elem->callback_num].error_cb = error_cb; - elem->eventcb_num++; + elem->callback_num++; HASH_ADD_STR(plug_mgr->evcb_htable, session_name, elem); } @@ -401,6 +408,9 @@ void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_eve assert(seesion); assert(session_name); + char event_str_buffer[1024] = {0}; + session_event_type_int2str(event_type, event_str_buffer, 1024); + // the same session may trigger multi times opening events if (event_type & SESSION_EVENT_OPENING) { @@ -418,18 +428,20 @@ void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_eve if (plug_ctx) { - for (int i = 0; i < plug_ctx->eventcb_num; i++) + for (int i = 0; i < plug_ctx->callback_num; i++) { - plug_ctx->current_plugin_index = i; - struct eventcb_runtime *runtime = &plug_ctx->eventcbs[i]; - if (runtime->skip) + struct callback_runtime *runtime = &plug_ctx->callbacks[i]; + if (runtime->skip == 1) { + plugin_manager_log(DEBUG, "dispatch, skip event_cb: %p, session: %s, event: (%d, %s)", runtime->event_cb, session_name, event_type, event_str_buffer); continue; } if (runtime->event & event_type) { - runtime->cb(seesion, event_type, packet, payload, payload_len, &runtime->cb_args); + plug_ctx->callback_index = i; + plugin_manager_log(DEBUG, "dispatch, run event_cb: %p, session: %s, event: (%d, %s)", runtime->event_cb, session_name, event_type, event_str_buffer); + runtime->event_cb(seesion, event_type, packet, payload, payload_len, &runtime->cb_args); } } } @@ -446,6 +458,55 @@ void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_eve } } +/****************************************************************************** + * Public API For Plugin + ******************************************************************************/ + +void pm_session_dettach_me(const struct stellar_session *session) +{ + struct session_plugin_ctx *plugin_ctx = stellar_session_get_plugin_ctx(session); + assert(plugin_ctx); + struct callback_runtime *runtime_me = &plugin_ctx->callbacks[plugin_ctx->callback_index]; + + /* + * Just set the skip flag and don't call this event callback next. + * The plugin is closed before calling pm_session_dettach_me. + */ + runtime_me->skip = 1; + plugin_manager_log(DEBUG, "%p dettach me, disable event_cb: %p, session: %s", runtime_me->event_cb, runtime_me->event_cb, stellar_session_get_name(session)); +} + +void pm_session_dettach_others(const struct stellar_session *session) +{ + struct session_plugin_ctx *plugin_ctx = stellar_session_get_plugin_ctx(session); + assert(plugin_ctx); + struct callback_runtime *runtime_me = &plugin_ctx->callbacks[plugin_ctx->callback_index]; + + for (int i = 0; i < plugin_ctx->callback_num; i++) + { + if (i != plugin_ctx->callback_index) + { + struct callback_runtime *runtime_other = &plugin_ctx->callbacks[i]; + runtime_other->skip = 1; + plugin_manager_log(DEBUG, "%p dettach others, run error_cb: %p, session: %s", runtime_me->event_cb, runtime_other->error_cb, stellar_session_get_name(session)); + runtime_other->error_cb(session, ERROR_EVENT_DETTACH, &runtime_other->cb_args); + } + } +} + +/****************************************************************************** + * Util For Gtest + ******************************************************************************/ + +void *pm_session_get_plugin_pme(const struct stellar_session *session) +{ + struct session_plugin_ctx *plugin_ctx = stellar_session_get_plugin_ctx(session); + assert(plugin_ctx); + struct callback_runtime *runtime_me = &plugin_ctx->callbacks[plugin_ctx->callback_index]; + + return runtime_me->cb_args; +} + /****************************************************************************** * Suppport LUA plugins ******************************************************************************/ diff --git a/src/plugin_manager/plugin_manager.h b/src/plugin_manager/plugin_manager.h index 4e96b06..2827483 100644 --- a/src/plugin_manager/plugin_manager.h +++ b/src/plugin_manager/plugin_manager.h @@ -1,4 +1,10 @@ -#pragma once +#ifndef _PLUGIN_MANAGER_H +#define _PLUGIN_MANAGER_H + +#ifdef __cpluscplus +extern "C" +{ +#endif #include "sdk/include/session.h" @@ -7,10 +13,17 @@ struct plugin_manager; struct plugin_manager *plugin_manager_create(); void plugin_manager_destory(struct plugin_manager *plug_mgr); -// return 0: success -// return -1: error -int plugin_manager_load(struct plugin_manager *plug_mgr, const char *prefix, const char *file); +int plugin_manager_load(struct plugin_manager *plug_mgr, const char *file); void plugin_manager_unload(struct plugin_manager *plug_mgr); -int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_name, enum session_event_type event, fn_session_event_callback *callback); -void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_event *event); \ No newline at end of file +int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_name, enum session_event_type event, fn_session_event_callback *event_cb, fn_session_error_callback *error_cb); +void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_event *event); + +// only use for gtest +void *pm_session_get_plugin_pme(const struct stellar_session *session); + +#ifdef __cpluscplus +} +#endif + +#endif \ No newline at end of file diff --git a/src/plugin_manager/plugin_manager_config.cpp b/src/plugin_manager/plugin_manager_config.cpp index f36512c..89dd39b 100644 --- a/src/plugin_manager/plugin_manager_config.cpp +++ b/src/plugin_manager/plugin_manager_config.cpp @@ -1,65 +1,10 @@ #include -#include #include #include "toml/toml.h" -#include "sdk/include/session.h" -#include "plugin_manager_util.h" + #include "plugin_manager_config.h" -struct event_type_map -{ - const char *type_str; - enum session_event_type type_int; -}; - -static struct event_type_map evtype_map[] = - { - {"SESSION_EVENT_OPENING", SESSION_EVENT_OPENING}, - {"SESSION_EVENT_RAWPKT", SESSION_EVENT_RAWPKT}, - {"SESSION_EVENT_ORDPKT", SESSION_EVENT_ORDPKT}, - {"SESSION_EVENT_META", SESSION_EVENT_META}, - {"SESSION_EVENT_CLOSING", SESSION_EVENT_CLOSING}, - {"SESSION_EVENT_ALL", SESSION_EVENT_ALL}, -}; - -/****************************************************************************** - * Private API (For Event Type) - ******************************************************************************/ - -static enum session_event_type event_type_str2int(const char *evtype_str) -{ - int num = sizeof(evtype_map) / sizeof(evtype_map[0]); - - for (int i = 0; i < num; i++) - { - if (strcmp(evtype_str, evtype_map[i].type_str) == 0) - { - return evtype_map[i].type_int; - } - } - - return SESSION_EVENT_UNKNOWN; -} - -static void event_type_int2str(enum session_event_type evtype_int, char *buffer, int size) -{ - int used = 0; - int num = sizeof(evtype_map) / sizeof(evtype_map[0]); - - for (int i = 0; i < num; i++) - { - if (evtype_map[i].type_int & evtype_int) - { - if (evtype_map[i].type_int == SESSION_EVENT_ALL && evtype_int != SESSION_EVENT_ALL) - { - continue; - } - used += snprintf(buffer + used, size - used, "%s ", evtype_map[i].type_str); - } - } -} - /****************************************************************************** * Private API (For Parse) ******************************************************************************/ @@ -117,6 +62,11 @@ static int toml_parse_plugin_section(toml_table_t *root, struct plugin_manager_c return -1; } + if (toml_parse_string(plugin_section, "ERROR_FUNC", file, &config->plugin_section.error_func_name) == -1) + { + return -1; + } + if (toml_parse_string(plugin_section, "LIBRARY_PATH", file, &config->plugin_section.lib_path) == -1) { return -1; @@ -193,7 +143,7 @@ static int toml_parse_session_section(toml_table_t *root, struct plugin_manager_ return -1; } - type_int = event_type_str2int(type_str.u.s); + type_int = session_event_type_str2int(type_str.u.s); if (type_int == SESSION_EVENT_UNKNOWN) { plugin_manager_log(ERROR, "invalid value '%s' for 'SESSION_EVENT_TYPE' configuration item in '[SESSION_NAME.%s]' section of %s", type_str.u.s, session_name, file); @@ -219,12 +169,12 @@ struct plugin_manager_config *plugin_mangager_config_create() { struct plugin_manager_config *config = safe_alloc(struct plugin_manager_config, 1); - config->prefix_path = NULL; config->file_path = NULL; config->session_section_num = 0; config->session_section = NULL; config->plugin_section.init_func_name = NULL; config->plugin_section.exit_func_name = NULL; + config->plugin_section.error_func_name = NULL; config->plugin_section.lib_path = NULL; return config; @@ -234,11 +184,11 @@ void plugin_mangager_config_destory(struct plugin_manager_config *config) { if (config) { - safe_free(config->prefix_path); safe_free(config->file_path); safe_free(config->plugin_section.init_func_name); safe_free(config->plugin_section.exit_func_name); + safe_free(config->plugin_section.error_func_name); safe_free(config->plugin_section.lib_path); if (config->session_section) @@ -260,10 +210,10 @@ void plugin_mangager_config_dump(struct plugin_manager_config *config) { if (config) { - plugin_manager_log(DEBUG, "[CONFIG_PREFIX] : %s", config->prefix_path); - plugin_manager_log(DEBUG, "[CONFIG_FILE] : %s", config->file_path); - plugin_manager_log(DEBUG, "[PLUGINFO]->INIT_FUNC : %s", config->plugin_section.init_func_name); - plugin_manager_log(DEBUG, "[PLUGINFO]->EXIT_FUNC : %s", config->plugin_section.exit_func_name); + plugin_manager_log(DEBUG, "[CONFIG_FILE] : %s", config->file_path); + plugin_manager_log(DEBUG, "[PLUGINFO]->INIT_FUNC : %s", config->plugin_section.init_func_name); + plugin_manager_log(DEBUG, "[PLUGINFO]->EXIT_FUNC : %s", config->plugin_section.exit_func_name); + plugin_manager_log(DEBUG, "[PLUGINFO]->ERROR_FUNC : %s", config->plugin_section.error_func_name); plugin_manager_log(DEBUG, "[PLUGINFO]->LIBRARY_PATH : %s", config->plugin_section.lib_path); if (config->session_section) @@ -272,29 +222,26 @@ void plugin_mangager_config_dump(struct plugin_manager_config *config) { char tmp_buffer[1024] = {0}; struct session_section_config *temp = &config->session_section[i]; - event_type_int2str(temp->event, tmp_buffer, 1024); - plugin_manager_log(DEBUG, "[SESSION_NAME.%s]->SESSION_EVENT_TYPE : %d, %s", temp->session_name, temp->event, tmp_buffer); + session_event_type_int2str(temp->event, tmp_buffer, 1024); + plugin_manager_log(DEBUG, "[SESSION_NAME.%s]->SESSION_EVENT_TYPE : %d, %s", temp->session_name, temp->event, tmp_buffer); plugin_manager_log(DEBUG, "[SESSION_NAME.%s]->SESSION_EVENT_CALLBACK : %s", temp->session_name, temp->cb_func_name); } } } } -int plugin_mangager_config_parse(struct plugin_manager_config *config, const char *prefix, const char *file) +int plugin_mangager_config_parse(struct plugin_manager_config *config, const char *file) { FILE *fp = NULL; toml_table_t *root = NULL; char errbuf[200] = {0}; - char tmp_buffer[4096] = {0}; - config->prefix_path = safe_dup(prefix); config->file_path = safe_dup(file); - strcat_prefix_and_file(prefix, file, tmp_buffer, sizeof(tmp_buffer)); - fp = fopen(tmp_buffer, "r"); + fp = fopen(config->file_path, "r"); if (fp == NULL) { - plugin_manager_log(ERROR, "can't open %s, %s", tmp_buffer, strerror(errno)); + plugin_manager_log(ERROR, "can't open %s, %s", config->file_path, strerror(errno)); return -1; } @@ -318,6 +265,8 @@ int plugin_mangager_config_parse(struct plugin_manager_config *config, const cha toml_free(root); fclose(fp); + plugin_manager_log(INFO, "plugin config file '%s' parse success", config->file_path); + return 0; err: @@ -333,26 +282,4 @@ err: } return -1; -} - -/****************************************************************************** - * Test - ******************************************************************************/ - -#ifdef PLUGIN_MANAGER_TEST -int main(int argc, char *argv[]) -{ - struct plugin_manager_config *config = plugin_mangager_config_create(); - - if (plugin_mangager_config_parse(config, argv[1], argv[2]) == -1) - { - plugin_manager_log(ERROR, "can't parser %s %s", argv[1], argv[2]); - } - - plugin_mangager_config_dump(config); - - plugin_mangager_config_destory(config); - - return 0; -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/plugin_manager/plugin_manager_config.h b/src/plugin_manager/plugin_manager_config.h index fa9164d..16a62ba 100644 --- a/src/plugin_manager/plugin_manager_config.h +++ b/src/plugin_manager/plugin_manager_config.h @@ -1,4 +1,10 @@ -#pragma once +#ifndef _PLUGIN_MANAGER_CONFIG_H +#define _PLUGIN_MANAGER_CONFIG_H + +#ifdef __cpluscplus +extern "C" +{ +#endif #include "plugin_manager_util.h" #include "sdk/include/session.h" @@ -14,13 +20,13 @@ struct plugin_section_config { char *init_func_name; char *exit_func_name; + char *error_func_name; char *lib_path; }; struct plugin_manager_config { - char *prefix_path; // absolute path to stellar installation - char *file_path; // relative path to stellar installation + char *file_path; int session_section_num; struct session_section_config *session_section; // array @@ -30,5 +36,11 @@ struct plugin_manager_config struct plugin_manager_config *plugin_mangager_config_create(); void plugin_mangager_config_destory(struct plugin_manager_config *config); -int plugin_mangager_config_parse(struct plugin_manager_config *config, const char *prefix, const char *file); -void plugin_mangager_config_dump(struct plugin_manager_config *config); \ No newline at end of file +int plugin_mangager_config_parse(struct plugin_manager_config *config, const char *file); +void plugin_mangager_config_dump(struct plugin_manager_config *config); + +#ifdef __cpluscplus +} +#endif + +#endif \ No newline at end of file diff --git a/src/plugin_manager/plugin_manager_module.cpp b/src/plugin_manager/plugin_manager_module.cpp index 1e3011b..def9343 100644 --- a/src/plugin_manager/plugin_manager_module.cpp +++ b/src/plugin_manager/plugin_manager_module.cpp @@ -1,11 +1,9 @@ #include #include -#include #include #include "sdk/include/plugin.h" -#include "plugin_manager_util.h" -#include "plugin_manager_config.h" +#include "plugin_manager_module.h" #include "plugin_manager.h" struct plugin_manager_module_evcb @@ -17,12 +15,12 @@ struct plugin_manager_module_evcb struct plugin_manager_module { - char *prefix; char *lib_path; void *dl_handle; plugin_init_callback *init_cb_ptr; plugin_exit_callback *exit_cb_ptr; + fn_session_error_callback *error_cb_ptr; struct plugin_manager_module_evcb *evcbs; int evcbs_num; @@ -43,7 +41,6 @@ void plugin_manager_module_close(struct plugin_manager_module *module) module->dl_handle = NULL; } - safe_free(module->prefix); safe_free(module->lib_path); safe_free(module->evcbs); module->evcbs_num = 0; @@ -59,18 +56,16 @@ struct plugin_manager_module *plugin_manager_module_open(struct plugin_manager_c return NULL; } - char dynamic_lib_path[4096] = {0}; struct plugin_manager_module *module = safe_alloc(struct plugin_manager_module, 1); - module->prefix = safe_dup(config->prefix_path); module->lib_path = safe_dup(config->plugin_section.lib_path); - strcat_prefix_and_file(module->prefix, module->lib_path, dynamic_lib_path, sizeof(dynamic_lib_path)); + // RTLD_NOW | RTLD_GLOBAL module->evcbs_num = 0; - module->dl_handle = dlopen(dynamic_lib_path, RTLD_LAZY | RTLD_GLOBAL | RTLD_DEEPBIND); + module->dl_handle = dlopen(module->lib_path, RTLD_NOW | RTLD_GLOBAL | RTLD_DEEPBIND); if (module->dl_handle == NULL) { - plugin_manager_log(ERROR, "can't dlopen %s, %s", dynamic_lib_path, dlerror()); + plugin_manager_log(ERROR, "can't dlopen %s, %s", module->lib_path, dlerror()); goto err; } @@ -78,7 +73,7 @@ struct plugin_manager_module *plugin_manager_module_open(struct plugin_manager_c if (module->init_cb_ptr == NULL) { plugin_manager_log(ERROR, "can't find symbol name of '%s' in dynamic library %s, %s", - config->plugin_section.init_func_name, dynamic_lib_path, dlerror()); + config->plugin_section.init_func_name, module->lib_path, dlerror()); goto err; } @@ -86,7 +81,15 @@ struct plugin_manager_module *plugin_manager_module_open(struct plugin_manager_c if (module->exit_cb_ptr == NULL) { plugin_manager_log(ERROR, "can't find symbol name of '%s' in dynamic library %s, %s", - config->plugin_section.exit_func_name, dynamic_lib_path, dlerror()); + config->plugin_section.exit_func_name, module->lib_path, dlerror()); + goto err; + } + + module->error_cb_ptr = (fn_session_error_callback *)(dlsym(module->dl_handle, config->plugin_section.error_func_name)); + if (module->error_cb_ptr == NULL) + { + plugin_manager_log(ERROR, "can't find symbol name of '%s' in dynamic library %s, %s", + config->plugin_section.error_func_name, module->lib_path, dlerror()); goto err; } @@ -106,12 +109,14 @@ struct plugin_manager_module *plugin_manager_module_open(struct plugin_manager_c if (event_cb->event_cb_ptr == NULL) { plugin_manager_log(ERROR, "can't find symbol name of '%s' in dynamic library %s, %s", - session_config->cb_func_name, dynamic_lib_path, dlerror()); + session_config->cb_func_name, module->lib_path, dlerror()); goto err; } } } + plugin_manager_log(INFO, "plugin dynamic library '%s' dlopen success", module->lib_path); + return module; err: @@ -120,6 +125,43 @@ err: return NULL; } +int plugin_manager_module_init(struct plugin_manager_module *module) +{ + struct timespec start; + struct timespec end; + + clock_gettime(CLOCK_MONOTONIC, &start); + int ret = module->init_cb_ptr(); + clock_gettime(CLOCK_MONOTONIC, &end); + if (ret == -1) + { + plugin_manager_log(ERROR, "dynamic library '%s' initialization failed", module->lib_path); + return -1; + } + + long elapsed = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000; + plugin_manager_log(INFO, "plugin dynamic library '%s' init success, using '%lld' us", module->lib_path, elapsed); + + return 0; +} + +void plugin_manager_module_exit(struct plugin_manager_module *module) +{ + struct timespec start; + struct timespec end; + + if (module && module->exit_cb_ptr) + { + clock_gettime(CLOCK_MONOTONIC, &start); + module->exit_cb_ptr(); + clock_gettime(CLOCK_MONOTONIC, &end); + module->exit_cb_ptr = NULL; + + long elapsed = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000; + plugin_manager_log(INFO, "plugin dynamic library '%s' exit success, using '%lld' us", module->lib_path, elapsed); + } +} + int plugin_manager_module_register(struct plugin_manager *plug_mgr, struct plugin_manager_module *module) { if (module && module->evcbs) @@ -128,7 +170,7 @@ int plugin_manager_module_register(struct plugin_manager *plug_mgr, struct plugi { struct plugin_manager_module_evcb *event_cb = &module->evcbs[i]; - if (plugin_manager_register(plug_mgr, event_cb->session_name, event_cb->event, event_cb->event_cb_ptr) == -1) + if (plugin_manager_register(plug_mgr, event_cb->session_name, event_cb->event, event_cb->event_cb_ptr, module->error_cb_ptr) == -1) { plugin_manager_log(ERROR, "dynamic library '%s' failed to register the event callback function of session '%s'", module->lib_path, event_cb->session_name); return -1; @@ -139,30 +181,23 @@ int plugin_manager_module_register(struct plugin_manager *plug_mgr, struct plugi return 0; } -int plugin_manager_module_init(struct plugin_manager_module *module) +void plugin_manager_module_dump(struct plugin_manager_module *module, struct plugin_manager_config *config) { - struct timespec start; - struct timespec end; - - clock_gettime(CLOCK_MONOTONIC, &start); - int ret = module->init_cb_ptr(); - if (ret == -1) + if (module) { - plugin_manager_log(ERROR, "dynamic library '%s' initialization failed", module->lib_path); - return -1; + plugin_manager_log(DEBUG, "[LIBRARY] : %s, %p", config->plugin_section.lib_path, module->dl_handle); + plugin_manager_log(DEBUG, "[INIT_FUNC] : %s, %p", config->plugin_section.init_func_name, module->init_cb_ptr); + plugin_manager_log(DEBUG, "[EXIT_FUNC] : %s, %p", config->plugin_section.exit_func_name, module->exit_cb_ptr); + plugin_manager_log(DEBUG, "[ERROR_FUNC] : %s, %p", config->plugin_section.error_func_name, module->error_cb_ptr); + + for (int i = 0; i < module->evcbs_num; i++) + { + struct session_section_config *session_config = &config->session_section[i]; + struct plugin_manager_module_evcb *event_cb = &module->evcbs[i]; + + char event_str_buffer[1024] = {0}; + session_event_type_int2str(event_cb->event, event_str_buffer, 1024); + plugin_manager_log(DEBUG, "[EVENT_FUNC] : %s, %p, %s, (%d: %s)", session_config->cb_func_name, event_cb->event_cb_ptr, event_cb->session_name, event_cb->event, event_str_buffer); + } } - clock_gettime(CLOCK_MONOTONIC, &end); - - long elapsed = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000; - plugin_manager_log(INFO, "plugin '%s' init success, using '%lld' us", module->lib_path, elapsed); - - return 0; -} - -void plugin_manager_module_exit(struct plugin_manager_module *module) -{ - if (module && module->exit_cb_ptr) - { - module->exit_cb_ptr(); - } -} +} \ No newline at end of file diff --git a/src/plugin_manager/plugin_manager_module.h b/src/plugin_manager/plugin_manager_module.h index d765b4a..0bc468c 100644 --- a/src/plugin_manager/plugin_manager_module.h +++ b/src/plugin_manager/plugin_manager_module.h @@ -1,12 +1,26 @@ -#pragma once +#ifndef _PLUGIN_MANAGER_MODULE_H +#define _PLUGIN_MANAGER_MODULE_H + +#ifdef __cpluscplus +extern "C" +{ +#endif #include "plugin_manager_config.h" struct plugin_manager_module; struct plugin_manager_module *plugin_manager_module_open(struct plugin_manager_config *config); -int plugin_manager_module_register(struct plugin_manager *plug_mgr, struct plugin_manager_module *module); -int plugin_manager_module_init(struct plugin_manager_module *module); +void plugin_manager_module_close(struct plugin_manager_module *module); +int plugin_manager_module_init(struct plugin_manager_module *module); void plugin_manager_module_exit(struct plugin_manager_module *module); -void plugin_manager_module_close(struct plugin_manager_module *module); \ No newline at end of file + +void plugin_manager_module_dump(struct plugin_manager_module *module, struct plugin_manager_config *config); +int plugin_manager_module_register(struct plugin_manager *plug_mgr, struct plugin_manager_module *module); + +#ifdef __cpluscplus +} +#endif + +#endif \ No newline at end of file diff --git a/src/plugin_manager/plugin_manager_util.cpp b/src/plugin_manager/plugin_manager_util.cpp index 1c4337d..08f903f 100644 --- a/src/plugin_manager/plugin_manager_util.cpp +++ b/src/plugin_manager/plugin_manager_util.cpp @@ -1,8 +1,11 @@ #include -#include #include "plugin_manager_util.h" +/****************************************************************************** + * Malloc + ******************************************************************************/ + char *safe_dup(const char *str) { if (str == NULL) @@ -16,25 +19,81 @@ char *safe_dup(const char *str) return dup; } -void strcat_prefix_and_file(const char *prefix, const char *file, char *buff, int size) +/****************************************************************************** + * Session Event Type + ******************************************************************************/ + +struct event_type_map { - char prefix_buffer[4096] = {0}; - char file_buffer[4096] = {0}; - char *ptr = NULL; + const char *type_str; + enum session_event_type type_int; +}; - memcpy(prefix_buffer, prefix, strlen(prefix)); - memcpy(file_buffer, file, strlen(file)); - - if (prefix_buffer[strlen(prefix_buffer) - 1] != '/') +static struct event_type_map evtype_map[] = { - prefix_buffer[strlen(prefix_buffer)] = '/'; - } - file_buffer[strcspn(file_buffer, "\r\n")] = 0; + {"SESSION_EVENT_UNKNOWN", SESSION_EVENT_UNKNOWN}, + {"SESSION_EVENT_OPENING", SESSION_EVENT_OPENING}, + {"SESSION_EVENT_RAWPKT", SESSION_EVENT_RAWPKT}, + {"SESSION_EVENT_ORDPKT", SESSION_EVENT_ORDPKT}, + {"SESSION_EVENT_META", SESSION_EVENT_META}, + {"SESSION_EVENT_CLOSING", SESSION_EVENT_CLOSING}, + {"SESSION_EVENT_ALL", SESSION_EVENT_ALL}, +}; - ptr = file_buffer; - if (file_buffer[0] == '.' && file_buffer[1] == '/') +enum session_event_type session_event_type_str2int(const char *evtype_str) +{ + enum session_event_type evtype_int = SESSION_EVENT_UNKNOWN; + int num = sizeof(evtype_map) / sizeof(evtype_map[0]); + + char *buffer = safe_dup(evtype_str); + char *token = strtok(buffer, "|"); + while (token) { - ptr += 2; + for (int i = 0; i < num; i++) + { + if (strcmp(token, evtype_map[i].type_str) == 0) + { + evtype_int = (enum session_event_type)(evtype_int | evtype_map[i].type_int); + } + } + token = strtok(NULL, "|"); } - snprintf(buff, size, "%s%s", prefix_buffer, ptr); -} \ No newline at end of file + safe_free(buffer); + + return evtype_int; +} + +void session_event_type_int2str(enum session_event_type evtype_int, char *buffer, int size) +{ + int used = 0; + int num = sizeof(evtype_map) / sizeof(evtype_map[0]); + + if (evtype_int == SESSION_EVENT_UNKNOWN) + { + snprintf(buffer, size, "%s", "SESSION_EVENT_UNKNOWN"); + return; + } + + if (evtype_int == SESSION_EVENT_ALL) + { + snprintf(buffer, size, "%s", "SESSION_EVENT_ALL"); + return; + } + + for (int i = 0; i < num; i++) + { + if (evtype_map[i].type_int & evtype_int) + { + if (evtype_map[i].type_int == SESSION_EVENT_ALL && evtype_int != SESSION_EVENT_ALL) + { + continue; + } + used += snprintf(buffer + used, size - used, "%s|", evtype_map[i].type_str); + } + } + + if (used) + { + buffer[used - 1] = '\0'; + } +} diff --git a/src/plugin_manager/plugin_manager_util.h b/src/plugin_manager/plugin_manager_util.h index 9c6c0fb..13b85db 100644 --- a/src/plugin_manager/plugin_manager_util.h +++ b/src/plugin_manager/plugin_manager_util.h @@ -1,6 +1,15 @@ -#pragma once +#ifndef _PLUGIN_MANAGER_UTIL_H +#define _PLUGIN_MANAGER_UTIL_H + +#ifdef __cpluscplus +extern "C" +{ +#endif #include +#include + +#include "sdk/include/session.h" #define MAX_PLUGIN_NUM 512 #define MAX_SESSION_NAME_LENGTH 32 @@ -55,7 +64,14 @@ enum plugin_manager_log_level #endif /****************************************************************************** - * Str + * Session Event Type ******************************************************************************/ -void strcat_prefix_and_file(const char *prefix, const char *file, char *buff, int size); +enum session_event_type session_event_type_str2int(const char *evtype_str); +void session_event_type_int2str(enum session_event_type evtype_int, char *buffer, int size); + +#ifdef __cpluscplus +} +#endif + +#endif \ No newline at end of file diff --git a/src/plugin_manager/test/CMakeLists.txt b/src/plugin_manager/test/CMakeLists.txt index 7a1a880..82bc6d6 100644 --- a/src/plugin_manager/test/CMakeLists.txt +++ b/src/plugin_manager/test/CMakeLists.txt @@ -11,6 +11,9 @@ target_link_libraries( dl ) +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--export-dynamic") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--export-dynamic") + include(GoogleTest) gtest_discover_tests(gtest_plugin_manager) diff --git a/src/plugin_manager/test/gtest_plugin_manager.cpp b/src/plugin_manager/test/gtest_plugin_manager.cpp index 8107b41..efc95b2 100644 --- a/src/plugin_manager/test/gtest_plugin_manager.cpp +++ b/src/plugin_manager/test/gtest_plugin_manager.cpp @@ -1,23 +1,150 @@ #include +#include "sdk/include/session.h" + +#include "../plugin_manager_util.h" #include "../plugin_manager_config.h" +#include "../plugin_manager_module.h" #include "../plugin_manager.h" #include "session_manager.h" -TEST(PLUGIN_MANAGER_TEST, plugin_manager_config_HTTP) +/****************************************************************************** + * Test plugin_mangager_util API + ******************************************************************************/ + +#if 1 +TEST(PLUGIN_MANAGER_TEST, plugin_mangager_util_int2str) +{ + enum session_event_type type_int; + char buffer[1024] = {0}; + + type_int = SESSION_EVENT_UNKNOWN; + memset(buffer, 0, sizeof(buffer)); + session_event_type_int2str(type_int, buffer, sizeof(buffer)); + EXPECT_STREQ(buffer, "SESSION_EVENT_UNKNOWN"); + + type_int = SESSION_EVENT_OPENING; + memset(buffer, 0, sizeof(buffer)); + session_event_type_int2str(type_int, buffer, sizeof(buffer)); + EXPECT_STREQ(buffer, "SESSION_EVENT_OPENING"); + + type_int = SESSION_EVENT_RAWPKT; + memset(buffer, 0, sizeof(buffer)); + session_event_type_int2str(type_int, buffer, sizeof(buffer)); + EXPECT_STREQ(buffer, "SESSION_EVENT_RAWPKT"); + + type_int = SESSION_EVENT_ORDPKT; + memset(buffer, 0, sizeof(buffer)); + session_event_type_int2str(type_int, buffer, sizeof(buffer)); + EXPECT_STREQ(buffer, "SESSION_EVENT_ORDPKT"); + + type_int = SESSION_EVENT_META; + memset(buffer, 0, sizeof(buffer)); + session_event_type_int2str(type_int, buffer, sizeof(buffer)); + EXPECT_STREQ(buffer, "SESSION_EVENT_META"); + + type_int = SESSION_EVENT_CLOSING; + memset(buffer, 0, sizeof(buffer)); + session_event_type_int2str(type_int, buffer, sizeof(buffer)); + EXPECT_STREQ(buffer, "SESSION_EVENT_CLOSING"); + + type_int = SESSION_EVENT_ALL; + memset(buffer, 0, sizeof(buffer)); + session_event_type_int2str(type_int, buffer, sizeof(buffer)); + EXPECT_STREQ(buffer, "SESSION_EVENT_ALL"); + + type_int = (enum session_event_type)(SESSION_EVENT_OPENING | SESSION_EVENT_RAWPKT); + memset(buffer, 0, sizeof(buffer)); + session_event_type_int2str(type_int, buffer, sizeof(buffer)); + EXPECT_STREQ(buffer, "SESSION_EVENT_OPENING|SESSION_EVENT_RAWPKT"); + + type_int = (enum session_event_type)(SESSION_EVENT_OPENING | SESSION_EVENT_RAWPKT | SESSION_EVENT_ORDPKT); + memset(buffer, 0, sizeof(buffer)); + session_event_type_int2str(type_int, buffer, sizeof(buffer)); + EXPECT_STREQ(buffer, "SESSION_EVENT_OPENING|SESSION_EVENT_RAWPKT|SESSION_EVENT_ORDPKT"); + + type_int = (enum session_event_type)(SESSION_EVENT_OPENING | SESSION_EVENT_RAWPKT | SESSION_EVENT_ORDPKT | SESSION_EVENT_META); + memset(buffer, 0, sizeof(buffer)); + session_event_type_int2str(type_int, buffer, sizeof(buffer)); + EXPECT_STREQ(buffer, "SESSION_EVENT_OPENING|SESSION_EVENT_RAWPKT|SESSION_EVENT_ORDPKT|SESSION_EVENT_META"); + + type_int = (enum session_event_type)(SESSION_EVENT_OPENING | SESSION_EVENT_RAWPKT | SESSION_EVENT_ORDPKT | SESSION_EVENT_META | SESSION_EVENT_CLOSING); + memset(buffer, 0, sizeof(buffer)); + session_event_type_int2str(type_int, buffer, sizeof(buffer)); + EXPECT_STREQ(buffer, "SESSION_EVENT_ALL"); +} +#endif + +#if 1 +TEST(PLUGIN_MANAGER_TEST, plugin_mangager_util_str2int) +{ + enum session_event_type type_int; + + const char *type_str1 = "SESSION_EVENT_UNKNOWN"; + const char *type_str2 = "SESSION_EVENT_OPENING"; + const char *type_str3 = "SESSION_EVENT_RAWPKT"; + const char *type_str4 = "SESSION_EVENT_ORDPKT"; + const char *type_str5 = "SESSION_EVENT_META"; + const char *type_str6 = "SESSION_EVENT_CLOSING"; + const char *type_str7 = "SESSION_EVENT_ALL"; + const char *type_str8 = "SESSION_EVENT_OPENING|SESSION_EVENT_RAWPKT"; + const char *type_str9 = "SESSION_EVENT_OPENING|SESSION_EVENT_RAWPKT|SESSION_EVENT_ORDPKT"; + const char *type_str10 = "SESSION_EVENT_OPENING|SESSION_EVENT_RAWPKT|SESSION_EVENT_ORDPKT|SESSION_EVENT_META"; + const char *type_str11 = "SESSION_EVENT_OPENING|SESSION_EVENT_RAWPKT|SESSION_EVENT_ORDPKT|SESSION_EVENT_META|SESSION_EVENT_CLOSING"; + + type_int = session_event_type_str2int(type_str1); + EXPECT_TRUE(type_int == SESSION_EVENT_UNKNOWN); + + type_int = session_event_type_str2int(type_str2); + EXPECT_TRUE(type_int == SESSION_EVENT_OPENING); + + type_int = session_event_type_str2int(type_str3); + EXPECT_TRUE(type_int == SESSION_EVENT_RAWPKT); + + type_int = session_event_type_str2int(type_str4); + EXPECT_TRUE(type_int == SESSION_EVENT_ORDPKT); + + type_int = session_event_type_str2int(type_str5); + EXPECT_TRUE(type_int == SESSION_EVENT_META); + + type_int = session_event_type_str2int(type_str6); + EXPECT_TRUE(type_int == SESSION_EVENT_CLOSING); + + type_int = session_event_type_str2int(type_str7); + EXPECT_TRUE(type_int == SESSION_EVENT_ALL); + + type_int = session_event_type_str2int(type_str8); + EXPECT_TRUE(type_int == (SESSION_EVENT_OPENING | SESSION_EVENT_RAWPKT)); + + type_int = session_event_type_str2int(type_str9); + EXPECT_TRUE(type_int == (SESSION_EVENT_OPENING | SESSION_EVENT_RAWPKT | SESSION_EVENT_ORDPKT)); + + type_int = session_event_type_str2int(type_str10); + EXPECT_TRUE(type_int == (SESSION_EVENT_OPENING | SESSION_EVENT_RAWPKT | SESSION_EVENT_ORDPKT | SESSION_EVENT_META)); + + type_int = session_event_type_str2int(type_str11); + EXPECT_TRUE(type_int == SESSION_EVENT_ALL); +} +#endif + +/****************************************************************************** + * Test plugin_mangager_config API + ******************************************************************************/ + +#if 1 +TEST(PLUGIN_MANAGER_TEST, plugin_mangager_config_HTTP) { - char prefix_path[] = "./"; char file_path[] = "./plugins_config/http_event_plugin/http_event_plugin.inf"; struct plugin_manager_config *config = plugin_mangager_config_create(); EXPECT_TRUE(config != nullptr); - EXPECT_TRUE(plugin_mangager_config_parse(config, prefix_path, file_path) == 0); + EXPECT_TRUE(plugin_mangager_config_parse(config, file_path) == 0); - EXPECT_STREQ(config->prefix_path, "./"); EXPECT_STREQ(config->file_path, "./plugins_config/http_event_plugin/http_event_plugin.inf"); EXPECT_STREQ(config->plugin_section.init_func_name, "http_event_plugin_init"); EXPECT_STREQ(config->plugin_section.exit_func_name, "http_event_plugin_exit"); + EXPECT_STREQ(config->plugin_section.error_func_name, "http_event_plugin_error"); EXPECT_STREQ(config->plugin_section.lib_path, "./test_plugins/plugins_library/http_event_plugin_test.so"); EXPECT_TRUE(config->session_section_num == 1); @@ -28,127 +155,421 @@ TEST(PLUGIN_MANAGER_TEST, plugin_manager_config_HTTP) plugin_mangager_config_dump(config); plugin_mangager_config_destory(config); } +#endif -TEST(PLUGIN_MANAGER_TEST, plugin_manager_config_CUSTOM) +#if 1 +TEST(PLUGIN_MANAGER_TEST, plugin_mangager_config_CUSTOM) { - char prefix_path[] = "./"; char file_path[] = "./plugins_config/custom_event_plugin/custom_event_plugin.inf"; struct plugin_manager_config *config = plugin_mangager_config_create(); EXPECT_TRUE(config != nullptr); - EXPECT_TRUE(plugin_mangager_config_parse(config, prefix_path, file_path) == 0); + EXPECT_TRUE(plugin_mangager_config_parse(config, file_path) == 0); - EXPECT_STREQ(config->prefix_path, "./"); EXPECT_STREQ(config->file_path, "./plugins_config/custom_event_plugin/custom_event_plugin.inf"); - EXPECT_STREQ(config->plugin_section.init_func_name, "custom_plugin_init"); - EXPECT_STREQ(config->plugin_section.exit_func_name, "custom_plugin_exit"); + EXPECT_STREQ(config->plugin_section.init_func_name, "custom_event_plugin_init"); + EXPECT_STREQ(config->plugin_section.exit_func_name, "custom_event_plugin_exit"); + EXPECT_STREQ(config->plugin_section.error_func_name, "custom_event_plugin_error"); EXPECT_STREQ(config->plugin_section.lib_path, "./test_plugins/plugins_library/custom_event_plugin_test.so"); - EXPECT_TRUE(config->session_section_num == 2); + EXPECT_TRUE(config->session_section_num == 3); EXPECT_STREQ(config->session_section[0].session_name, "TCP"); - EXPECT_STREQ(config->session_section[0].cb_func_name, "custom_plugin_tcp_entry"); + EXPECT_STREQ(config->session_section[0].cb_func_name, "custom_event_plugin_tcp_entry"); EXPECT_TRUE(config->session_section[0].event == (0x01 << 1 | 0x01 << 2 | 0x01 << 3 | 0x01 << 4 | 0x01 << 5)); - EXPECT_STREQ(config->session_section[1].session_name, "CUSTOM"); - EXPECT_STREQ(config->session_section[1].cb_func_name, "custom_plugin_custom_entry"); - EXPECT_TRUE(config->session_section[1].event == (0x01 << 1) | (0x01 << 3) | (0x01 << 5)); + + EXPECT_STREQ(config->session_section[1].session_name, "HTTP"); + EXPECT_STREQ(config->session_section[1].cb_func_name, "custom_event_plugin_http_entry"); + EXPECT_TRUE(config->session_section[1].event == (0x01 << 1 | 0x01 << 2 | 0x01 << 3 | 0x01 << 4 | 0x01 << 5)); + + EXPECT_STREQ(config->session_section[2].session_name, "CUSTOM"); + EXPECT_STREQ(config->session_section[2].cb_func_name, "custom_event_plugin_custom_entry"); + EXPECT_TRUE(config->session_section[2].event == (0x01 << 1) | (0x01 << 3) | (0x01 << 5)); plugin_mangager_config_dump(config); plugin_mangager_config_destory(config); } +#endif +/****************************************************************************** + * Test plugin_mangager_module API + ******************************************************************************/ + +#if 1 +TEST(PLUGIN_MANAGER_TEST, plugin_mangager_module_HTTP) +{ + char file_path[] = "./plugins_config/http_event_plugin/http_event_plugin.inf"; + + struct plugin_manager_config *config = plugin_mangager_config_create(); + EXPECT_TRUE(config != nullptr); + EXPECT_TRUE(plugin_mangager_config_parse(config, file_path) == 0); + + struct plugin_manager_module *module = plugin_manager_module_open(config); + EXPECT_TRUE(module != nullptr); + EXPECT_TRUE(plugin_manager_module_init(module) == 0); + plugin_manager_module_dump(module, config); + plugin_manager_module_exit(module); + plugin_manager_module_close(module); + + plugin_mangager_config_destory(config); +} +#endif + +#if 1 +TEST(PLUGIN_MANAGER_TEST, plugin_mangager_module_CUSTOM) +{ + char file_path[] = "./plugins_config/custom_event_plugin/custom_event_plugin.inf"; + + struct plugin_manager_config *config = plugin_mangager_config_create(); + EXPECT_TRUE(config != nullptr); + EXPECT_TRUE(plugin_mangager_config_parse(config, file_path) == 0); + + struct plugin_manager_module *module = plugin_manager_module_open(config); + EXPECT_TRUE(module != nullptr); + EXPECT_TRUE(plugin_manager_module_init(module) == 0); + plugin_manager_module_dump(module, config); + plugin_manager_module_exit(module); + plugin_manager_module_close(module); + + plugin_mangager_config_destory(config); +} +#endif + +/****************************************************************************** + * Test plugin_mangager API + ******************************************************************************/ + +#if 1 TEST(PLUGIN_MANAGER_TEST, plugin_manager_load) { - char prefix_path[] = "./"; char file_path[] = "./plugins_config/plugins.inf"; struct plugin_manager *plug_mgr = plugin_manager_create(); EXPECT_TRUE(plug_mgr != nullptr); - EXPECT_TRUE(plugin_manager_load(plug_mgr, prefix_path, file_path) == 0); - - plugin_manager_unload(plug_mgr); - plugin_manager_destory(plug_mgr); -} - -TEST(PLUGIN_MANAGER_TEST, plugin_manager_dispatch_HTTP) -{ - char prefix_path[] = "./"; - char file_path[] = "./plugins_config/plugins.inf"; - - const char *session_name = "HTTP"; - struct stellar_session session; - session.name = session_name; - struct stellar_session_event_data event_data; - event_data.s = &session; - event_data.plugin_ctx = NULL; // must be init to NULL - struct stellar_event event; - event.session_event_data = &event_data; - - struct plugin_manager *plug_mgr = plugin_manager_create(); - EXPECT_TRUE(plug_mgr != nullptr); - EXPECT_TRUE(plugin_manager_load(plug_mgr, prefix_path, file_path) == 0); - - event_data.type = SESSION_EVENT_OPENING; - plugin_manager_dispatch(plug_mgr, &event); - - event_data.type = SESSION_EVENT_RAWPKT; - plugin_manager_dispatch(plug_mgr, &event); - - event_data.type = SESSION_EVENT_ORDPKT; - plugin_manager_dispatch(plug_mgr, &event); - - event_data.type = SESSION_EVENT_META; - plugin_manager_dispatch(plug_mgr, &event); - - event_data.type = SESSION_EVENT_CLOSING; - plugin_manager_dispatch(plug_mgr, &event); - - event_data.type = SESSION_EVENT_ALL; - plugin_manager_dispatch(plug_mgr, &event); + EXPECT_TRUE(plugin_manager_load(plug_mgr, file_path) == 0); plugin_manager_unload(plug_mgr); plugin_manager_destory(plug_mgr); } +#endif +#if 1 +// only SESSION_EVENT_OPENING | SESSION_EVENT_ORDPKT | SESSION_EVENT_CLOSING can trigger event callback TEST(PLUGIN_MANAGER_TEST, plugin_manager_dispatch_CUSTOM) { - char prefix_path[] = "./"; + /* + * [SESSION_NAME.CUSTOM] + * SESSION_EVENT_TYPE=["SESSION_EVENT_OPENING","SESSION_EVENT_ORDPKT","SESSION_EVENT_CLOSING"] + * SESSION_EVENT_CALLBACK="custom_event_plugin_custom_entry" + */ + + struct custom_session_pme + { + char data[64]; + int flags; + /* data */ + }; + + struct custom_session_pme *pme; char file_path[] = "./plugins_config/plugins.inf"; const char *session_name = "CUSTOM"; + struct stellar_session session; session.name = session_name; + struct stellar_session_event_data event_data; event_data.s = &session; event_data.plugin_ctx = NULL; // must be init to NULL + struct stellar_event event; event.session_event_data = &event_data; + session.event_data = &event_data; // must be set + struct plugin_manager *plug_mgr = plugin_manager_create(); EXPECT_TRUE(plug_mgr != nullptr); - EXPECT_TRUE(plugin_manager_load(plug_mgr, prefix_path, file_path) == 0); + EXPECT_TRUE(plugin_manager_load(plug_mgr, file_path) == 0); + // run evencb event_data.type = SESSION_EVENT_OPENING; plugin_manager_dispatch(plug_mgr, &event); + pme = (struct custom_session_pme *)pm_session_get_plugin_pme(&session); + EXPECT_TRUE(pme->flags == SESSION_EVENT_OPENING); + EXPECT_STREQ(pme->data, "custom_event_plugin_custom_entry"); + // unrun evencb event_data.type = SESSION_EVENT_RAWPKT; plugin_manager_dispatch(plug_mgr, &event); + pme = (struct custom_session_pme *)pm_session_get_plugin_pme(&session); + EXPECT_TRUE(pme->flags == SESSION_EVENT_OPENING); + EXPECT_STREQ(pme->data, "custom_event_plugin_custom_entry"); + // run evencb event_data.type = SESSION_EVENT_ORDPKT; plugin_manager_dispatch(plug_mgr, &event); + pme = (struct custom_session_pme *)pm_session_get_plugin_pme(&session); + EXPECT_TRUE(pme->flags == SESSION_EVENT_ORDPKT); + EXPECT_STREQ(pme->data, "custom_event_plugin_custom_entry"); + // unrun evencb event_data.type = SESSION_EVENT_META; plugin_manager_dispatch(plug_mgr, &event); + pme = (struct custom_session_pme *)pm_session_get_plugin_pme(&session); + EXPECT_TRUE(pme->flags == SESSION_EVENT_ORDPKT); + EXPECT_STREQ(pme->data, "custom_event_plugin_custom_entry"); + // run evencb event_data.type = SESSION_EVENT_CLOSING; plugin_manager_dispatch(plug_mgr, &event); + EXPECT_TRUE(stellar_session_get_plugin_ctx(&session) == nullptr); + // run evencb event_data.type = SESSION_EVENT_ALL; plugin_manager_dispatch(plug_mgr, &event); + EXPECT_TRUE(stellar_session_get_plugin_ctx(&session) == nullptr); plugin_manager_unload(plug_mgr); plugin_manager_destory(plug_mgr); } +#endif + +#if 1 +// ALL SESSION_EVENT can trigger event callback +TEST(PLUGIN_MANAGER_TEST, plugin_manager_dispatch_TCP) +{ + /* + * [SESSION_NAME.TCP] + * SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"] + * SESSION_EVENT_CALLBACK="custom_event_plugin_tcp_entry" + */ + + struct tcp_session_pme + { + char data[64]; + int flags; + /* data */ + }; + struct tcp_session_pme *pme; + + char file_path[] = "./plugins_config/plugins.inf"; + + const char *session_name = "TCP"; + + struct stellar_session session; + session.name = session_name; + + struct stellar_session_event_data event_data; + event_data.s = &session; + event_data.plugin_ctx = NULL; // must be init to NULL + + struct stellar_event event; + event.session_event_data = &event_data; + + session.event_data = &event_data; // must be set + + struct plugin_manager *plug_mgr = plugin_manager_create(); + EXPECT_TRUE(plug_mgr != nullptr); + EXPECT_TRUE(plugin_manager_load(plug_mgr, file_path) == 0); + + // run evencb + event_data.type = SESSION_EVENT_OPENING; + plugin_manager_dispatch(plug_mgr, &event); + pme = (struct tcp_session_pme *)pm_session_get_plugin_pme(&session); + EXPECT_TRUE(pme->flags == SESSION_EVENT_OPENING); + EXPECT_STREQ(pme->data, "tcp***"); + + // run evencb + event_data.type = SESSION_EVENT_RAWPKT; + plugin_manager_dispatch(plug_mgr, &event); + pme = (struct tcp_session_pme *)pm_session_get_plugin_pme(&session); + EXPECT_TRUE(pme->flags == SESSION_EVENT_RAWPKT); + EXPECT_STREQ(pme->data, "tcp***"); + + // run evencb + event_data.type = SESSION_EVENT_ORDPKT; + plugin_manager_dispatch(plug_mgr, &event); + pme = (struct tcp_session_pme *)pm_session_get_plugin_pme(&session); + EXPECT_TRUE(pme->flags == SESSION_EVENT_ORDPKT); + EXPECT_STREQ(pme->data, "tcp***"); + + // run evencb + event_data.type = SESSION_EVENT_META; + plugin_manager_dispatch(plug_mgr, &event); + pme = (struct tcp_session_pme *)pm_session_get_plugin_pme(&session); + EXPECT_TRUE(pme->flags == SESSION_EVENT_META); + EXPECT_STREQ(pme->data, "tcp***"); + + // run evencb + event_data.type = SESSION_EVENT_CLOSING; + plugin_manager_dispatch(plug_mgr, &event); + EXPECT_TRUE(stellar_session_get_plugin_ctx(&session) == nullptr); + + // run evencb + event_data.type = SESSION_EVENT_ALL; + plugin_manager_dispatch(plug_mgr, &event); + EXPECT_TRUE(stellar_session_get_plugin_ctx(&session) == nullptr); + + plugin_manager_unload(plug_mgr); + plugin_manager_destory(plug_mgr); +} +#endif + +#if 1 +// http_event_plugin_entry + SESSION_EVENT_RAWPKT ==> pm_session_dettach_me +TEST(PLUGIN_MANAGER_TEST, plugin_manager_dispatch_HTTP_dettach_me) +{ + /* + * [SESSION_NAME.HTTP] + * SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"] + * SESSION_EVENT_CALLBACK="http_event_plugin_entry" + * + * [SESSION_NAME.HTTP] + * SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"] + * SESSION_EVENT_CALLBACK="custom_event_plugin_http_entry" + */ + + struct http_session_pme + { + char data[64]; + int flags; + /* data */; + }; + + struct http_session_pme *pme = NULL; + char file_path[] = "./plugins_config/plugins.inf"; + + const char *session_name = "HTTP"; + + struct stellar_session session; + session.name = session_name; + + struct stellar_session_event_data event_data; + event_data.s = &session; + event_data.plugin_ctx = NULL; // must be init to NULL + + struct stellar_event event; + event.session_event_data = &event_data; + + session.event_data = &event_data; // must be set + + struct plugin_manager *plug_mgr = plugin_manager_create(); + EXPECT_TRUE(plug_mgr != nullptr); + EXPECT_TRUE(plugin_manager_load(plug_mgr, file_path) == 0); + + // run evencb + event_data.type = SESSION_EVENT_OPENING; + plugin_manager_dispatch(plug_mgr, &event); + pme = (struct http_session_pme *)pm_session_get_plugin_pme(&session); + EXPECT_TRUE(pme->flags == SESSION_EVENT_OPENING); + EXPECT_STREQ(pme->data, "custom_event_plugin_http_entry"); + + // http_event_plugin_entry + SESSION_EVENT_RAWPKT ==> pm_session_dettach_me + event_data.type = SESSION_EVENT_RAWPKT; + plugin_manager_dispatch(plug_mgr, &event); + pme = (struct http_session_pme *)pm_session_get_plugin_pme(&session); + EXPECT_TRUE(pme->flags == SESSION_EVENT_RAWPKT); + EXPECT_STREQ(pme->data, "custom_event_plugin_http_entry"); + + // run evencb + event_data.type = SESSION_EVENT_META; + plugin_manager_dispatch(plug_mgr, &event); + pme = (struct http_session_pme *)pm_session_get_plugin_pme(&session); + EXPECT_TRUE(pme->flags == SESSION_EVENT_META); + EXPECT_STREQ(pme->data, "custom_event_plugin_http_entry"); + + // run evencb + event_data.type = SESSION_EVENT_CLOSING; + plugin_manager_dispatch(plug_mgr, &event); + EXPECT_TRUE(stellar_session_get_plugin_ctx(&session) == nullptr); + + // run evencb + event_data.type = SESSION_EVENT_ALL; + plugin_manager_dispatch(plug_mgr, &event); + EXPECT_TRUE(stellar_session_get_plugin_ctx(&session) == nullptr); + + plugin_manager_unload(plug_mgr); + plugin_manager_destory(plug_mgr); +} +#endif + +#if 1 +// http_event_plugin_entry + SESSION_EVENT_ORDPKT ==> pm_session_dettach_others +TEST(PLUGIN_MANAGER_TEST, plugin_manager_dispatch_HTTP_dettach_other) +{ + /* + * [SESSION_NAME.HTTP] + * SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"] + * SESSION_EVENT_CALLBACK="http_event_plugin_entry" + * + * [SESSION_NAME.HTTP] + * SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"] + * SESSION_EVENT_CALLBACK="custom_event_plugin_http_entry" + */ + + struct http_session_pme + { + char data[64]; + int flags; + /* data */; + }; + + struct http_session_pme *pme = NULL; + char file_path[] = "./plugins_config/plugins.inf"; + + const char *session_name = "HTTP"; + + struct stellar_session session; + session.name = session_name; + + struct stellar_session_event_data event_data; + event_data.s = &session; + event_data.plugin_ctx = NULL; // must be init to NULL + + struct stellar_event event; + event.session_event_data = &event_data; + + session.event_data = &event_data; // must be set + + struct plugin_manager *plug_mgr = plugin_manager_create(); + EXPECT_TRUE(plug_mgr != nullptr); + EXPECT_TRUE(plugin_manager_load(plug_mgr, file_path) == 0); + + // run evencb + event_data.type = SESSION_EVENT_OPENING; + plugin_manager_dispatch(plug_mgr, &event); + pme = (struct http_session_pme *)pm_session_get_plugin_pme(&session); + EXPECT_TRUE(pme->flags == SESSION_EVENT_OPENING); + EXPECT_STREQ(pme->data, "custom_event_plugin_http_entry"); + + // http_event_plugin_entry + SESSION_EVENT_ORDPKT ==> pm_session_dettach_others + event_data.type = SESSION_EVENT_ORDPKT; + plugin_manager_dispatch(plug_mgr, &event); + pme = (struct http_session_pme *)pm_session_get_plugin_pme(&session); + EXPECT_TRUE(pme->flags == SESSION_EVENT_ORDPKT); + EXPECT_STREQ(pme->data, "http_event_plugin_entry"); + + // run evencb + event_data.type = SESSION_EVENT_META; + plugin_manager_dispatch(plug_mgr, &event); + pme = (struct http_session_pme *)pm_session_get_plugin_pme(&session); + EXPECT_TRUE(pme->flags == SESSION_EVENT_META); + EXPECT_STREQ(pme->data, "http_event_plugin_entry"); + + // run evencb + event_data.type = SESSION_EVENT_CLOSING; + plugin_manager_dispatch(plug_mgr, &event); + EXPECT_TRUE(stellar_session_get_plugin_ctx(&session) == nullptr); + + // run evencb + event_data.type = SESSION_EVENT_ALL; + plugin_manager_dispatch(plug_mgr, &event); + EXPECT_TRUE(stellar_session_get_plugin_ctx(&session) == nullptr); + + plugin_manager_unload(plug_mgr); + plugin_manager_destory(plug_mgr); +} +#endif int main(int argc, char **argv) { diff --git a/src/plugin_manager/test/test_plugins/plugins_config/custom_event_plugin/custom_event_plugin.inf b/src/plugin_manager/test/test_plugins/plugins_config/custom_event_plugin/custom_event_plugin.inf index 26ba914..5df88aa 100644 --- a/src/plugin_manager/test/test_plugins/plugins_config/custom_event_plugin/custom_event_plugin.inf +++ b/src/plugin_manager/test/test_plugins/plugins_config/custom_event_plugin/custom_event_plugin.inf @@ -1,14 +1,19 @@ [PLUGINFO] -INIT_FUNC="custom_plugin_init" -EXIT_FUNC="custom_plugin_exit" +INIT_FUNC="custom_event_plugin_init" +EXIT_FUNC="custom_event_plugin_exit" +ERROR_FUNC="custom_event_plugin_error" LIBRARY_PATH="./test_plugins/plugins_library/custom_event_plugin_test.so" # Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL" [SESSION_NAME.TCP] SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"] -SESSION_EVENT_CALLBACK="custom_plugin_tcp_entry" +SESSION_EVENT_CALLBACK="custom_event_plugin_tcp_entry" + +[SESSION_NAME.HTTP] +SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"] +SESSION_EVENT_CALLBACK="custom_event_plugin_http_entry" [SESSION_NAME.CUSTOM] SESSION_EVENT_TYPE=["SESSION_EVENT_OPENING","SESSION_EVENT_ORDPKT","SESSION_EVENT_CLOSING"] -SESSION_EVENT_CALLBACK="custom_plugin_custom_entry" \ No newline at end of file +SESSION_EVENT_CALLBACK="custom_event_plugin_custom_entry" \ No newline at end of file diff --git a/src/plugin_manager/test/test_plugins/plugins_config/http_event_plugin/http_event_plugin.inf b/src/plugin_manager/test/test_plugins/plugins_config/http_event_plugin/http_event_plugin.inf index 1dd124b..cf0d16b 100644 --- a/src/plugin_manager/test/test_plugins/plugins_config/http_event_plugin/http_event_plugin.inf +++ b/src/plugin_manager/test/test_plugins/plugins_config/http_event_plugin/http_event_plugin.inf @@ -1,6 +1,7 @@ [PLUGINFO] INIT_FUNC="http_event_plugin_init" EXIT_FUNC="http_event_plugin_exit" +ERROR_FUNC="http_event_plugin_error" LIBRARY_PATH="./test_plugins/plugins_library/http_event_plugin_test.so" # Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL" diff --git a/src/plugin_manager/test/test_plugins/plugins_library/custom_event_plugin.cpp b/src/plugin_manager/test/test_plugins/plugins_library/custom_event_plugin.cpp index 0d7f1c4..fbeed57 100644 --- a/src/plugin_manager/test/test_plugins/plugins_library/custom_event_plugin.cpp +++ b/src/plugin_manager/test/test_plugins/plugins_library/custom_event_plugin.cpp @@ -2,9 +2,9 @@ #include "packet.h" #include "plugin.h" -#include #include #include +#include static char *g_handler = NULL; @@ -13,30 +13,214 @@ static void *custom_decode(const char *payload, uint16_t len, void **pme) return NULL; } -extern "C" void custom_plugin_tcp_entry(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) +struct tcp_session_pme { - char **per_session_pme = (char **)pme; + char data[64]; + int flags; + /* data */ +}; - printf("RUN custom_plugin_tcp_entry, event: %d\n", event); +struct custom_session_pme +{ + char data[64]; + int flags; + /* data */ +}; - struct stellar_session_event_extras *info = (struct stellar_session_event_extras *)custom_decode(payload, len, pme); - struct stellar_session *new_session = session_manager_session_derive(s, "CUSTOM"); +struct http_session_pme +{ + char data[64]; + int flags; + /* data */; +}; - session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info); - session_manager_trigger_event(new_session, SESSION_EVENT_META, info); +static struct tcp_session_pme *tcp_session_pme_create() +{ + struct tcp_session_pme *pme = (struct tcp_session_pme *)calloc(1, sizeof(struct tcp_session_pme)); + return pme; } -extern "C" void custom_plugin_custom_entry(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) +static void tcp_session_pme_destory(struct tcp_session_pme *pme) { - char **per_session_pme = (char **)pme; - - printf("RUN custom_plugin_custom_entry, event: %d\n", event); + if (pme) + { + free(pme); + pme = NULL; + } } -extern "C" int custom_plugin_init(void) +static struct custom_session_pme *custom_session_pme_create() { - printf("RUN custom_plugin_init\n"); + struct custom_session_pme *pme = (struct custom_session_pme *)calloc(1, sizeof(struct custom_session_pme)); + return pme; +} +static void custom_session_pme_destory(struct custom_session_pme *pme) +{ + if (pme) + { + free(pme); + pme = NULL; + } +} + +static struct http_session_pme *http_session_pme_create() +{ + struct http_session_pme *pme = (struct http_session_pme *)calloc(1, sizeof(struct http_session_pme)); + return pme; +} + +static void http_session_pme_destory(struct http_session_pme *pme) +{ + if (pme) + { + free(pme); + pme = NULL; + } +} + +extern "C" void custom_event_plugin_error(const struct stellar_session *session, enum error_event_type event, void **pme) +{ + if (strcmp(stellar_session_get_name(session), "TCP") == 0) + { + struct tcp_session_pme **per_tcp_session_pme = (struct tcp_session_pme **)pme; + printf("RUN custom_event_plugin_error, session_name: 'TCP', error_event_type: %d, pme->data: %s\n", event, (*per_tcp_session_pme) == NULL ? "NULL" : (*per_tcp_session_pme)->data); + tcp_session_pme_destory(*per_tcp_session_pme); + } + + if (strcmp(stellar_session_get_name(session), "CUSTOM") == 0) + { + struct custom_session_pme **per_custom_session_pme = (struct custom_session_pme **)pme; + printf("RUN custom_event_plugin_error, session_name: 'CUSTOM', error_event_type: %d, pme->data: %s\n", event, (*per_custom_session_pme) == NULL ? "NULL" : (*per_custom_session_pme)->data); + custom_session_pme_destory(*per_custom_session_pme); + } + + if (strcmp(stellar_session_get_name(session), "HTTP") == 0) + { + struct http_session_pme **per_http_session_pme = (struct http_session_pme **)pme; + printf("RUN custom_event_plugin_error, session_name: 'HTTP', error_event_type: %d, pme->data: %s\n", event, (*per_http_session_pme) == NULL ? "NULL" : (*per_http_session_pme)->data); + http_session_pme_destory(*per_http_session_pme); + } +} + +extern "C" void custom_event_plugin_tcp_entry(const struct stellar_session *session, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) +{ + struct tcp_session_pme **per_tcp_session_pme = (struct tcp_session_pme **)pme; + + if (event & SESSION_EVENT_OPENING) + { + if (*per_tcp_session_pme == NULL) + { + struct tcp_session_pme *cur_ctx = tcp_session_pme_create(); + memcpy(cur_ctx->data, "tcp***", 6); + cur_ctx->flags = SESSION_EVENT_OPENING; + *per_tcp_session_pme = *&cur_ctx; + } + } + + if (event & SESSION_EVENT_RAWPKT) + { + (*per_tcp_session_pme)->flags = SESSION_EVENT_RAWPKT; + } + + if (event & SESSION_EVENT_ORDPKT) + { + (*per_tcp_session_pme)->flags = SESSION_EVENT_ORDPKT; + + struct stellar_session_event_extras *info = (struct stellar_session_event_extras *)custom_decode(payload, len, pme); + struct stellar_session *new_session = session_manager_session_derive(session, "CUSTOM"); + + session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info); + session_manager_trigger_event(new_session, SESSION_EVENT_META, info); + } + + if (event & SESSION_EVENT_META) + { + (*per_tcp_session_pme)->flags = SESSION_EVENT_META; + } + + if (event & SESSION_EVENT_CLOSING) + { + tcp_session_pme_destory(*per_tcp_session_pme); + *per_tcp_session_pme = NULL; + } +} + +extern "C" void custom_event_plugin_custom_entry(const struct stellar_session *session, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) +{ + struct custom_session_pme **per_custom_session_pme = (struct custom_session_pme **)pme; + + if (event & SESSION_EVENT_OPENING) + { + if (*per_custom_session_pme == NULL) + { + struct custom_session_pme *cur_ctx = custom_session_pme_create(); + memcpy(cur_ctx->data, "custom_event_plugin_custom_entry", strlen("custom_event_plugin_custom_entry")); + cur_ctx->flags = SESSION_EVENT_OPENING; + *per_custom_session_pme = *&cur_ctx; + } + } + + if (event & SESSION_EVENT_RAWPKT) + { + (*per_custom_session_pme)->flags = SESSION_EVENT_RAWPKT; + } + if (event & SESSION_EVENT_ORDPKT) + { + (*per_custom_session_pme)->flags = SESSION_EVENT_ORDPKT; + } + + if (event & SESSION_EVENT_META) + { + (*per_custom_session_pme)->flags = SESSION_EVENT_META; + } + + if (event & SESSION_EVENT_CLOSING) + { + custom_session_pme_destory(*per_custom_session_pme); + *per_custom_session_pme = NULL; + } +} + +extern "C" void custom_event_plugin_http_entry(const struct stellar_session *session, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) +{ + struct http_session_pme **per_http_session_pme = (struct http_session_pme **)pme; + + if (event & SESSION_EVENT_OPENING) + { + if (*per_http_session_pme == NULL) + { + struct http_session_pme *cur_ctx = http_session_pme_create(); + memcpy(cur_ctx->data, "custom_event_plugin_http_entry", strlen("custom_event_plugin_http_entry")); + cur_ctx->flags = SESSION_EVENT_OPENING; + *per_http_session_pme = *&cur_ctx; + } + } + + if (event & SESSION_EVENT_RAWPKT) + { + (*per_http_session_pme)->flags = SESSION_EVENT_RAWPKT; + } + + if (event & SESSION_EVENT_ORDPKT) + { + (*per_http_session_pme)->flags = SESSION_EVENT_ORDPKT; + } + + if (event & SESSION_EVENT_META) + { + (*per_http_session_pme)->flags = SESSION_EVENT_META; + } + + if (event & SESSION_EVENT_CLOSING) + { + http_session_pme_destory(*per_http_session_pme); + *per_http_session_pme = NULL; + } +} + +extern "C" int custom_event_plugin_init(void) +{ if (g_handler == NULL) { g_handler = (char *)malloc(1024); @@ -46,10 +230,8 @@ extern "C" int custom_plugin_init(void) return 0; } -extern "C" void custom_plugin_exit(void) +extern "C" void custom_event_plugin_exit(void) { - printf("RUN custom_plugin_exit\n"); - if (g_handler) { free(g_handler); diff --git a/src/plugin_manager/test/test_plugins/plugins_library/http_event_plugin.cpp b/src/plugin_manager/test/test_plugins/plugins_library/http_event_plugin.cpp index e41d8b6..487d6bf 100644 --- a/src/plugin_manager/test/test_plugins/plugins_library/http_event_plugin.cpp +++ b/src/plugin_manager/test/test_plugins/plugins_library/http_event_plugin.cpp @@ -4,63 +4,92 @@ #include #include +#include static char *g_handler = NULL; -struct per_session_pme_info +struct http_session_pme { - int flag; - char data[16]; + char data[64]; + int flags; + /* data */; }; -extern "C" void http_event_plugin_entry(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) +static struct http_session_pme *http_session_pme_create() { - struct per_session_pme_info **per_session_pme = (struct per_session_pme_info **)pme; + struct http_session_pme *pme = (struct http_session_pme *)calloc(1, sizeof(struct http_session_pme)); + return pme; +} - printf("RUN http_event_plugin_entry, event: %d\n", event); +static void http_session_pme_destory(struct http_session_pme *pme) +{ + if (pme) + { + free(pme); + pme = NULL; + } +} + +extern "C" void http_event_plugin_error(const struct stellar_session *session, enum error_event_type event, void **pme) +{ + if (strcmp(stellar_session_get_name(session), "HTTP") == 0) + { + struct http_session_pme **per_http_session_pme = (struct http_session_pme **)pme; + printf("RUN http_event_plugin_error, session_name: 'HTTP', error_event_type: %d, pme->data: %s\n", event, (*per_http_session_pme) == NULL ? "NULL" : (*per_http_session_pme)->data); + http_session_pme_destory(*per_http_session_pme); + } +} + +extern "C" void http_event_plugin_entry(const struct stellar_session *session, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) +{ + struct http_session_pme **per_http_session_pme = (struct http_session_pme **)pme; if (event & SESSION_EVENT_OPENING) { - if (*per_session_pme == NULL) + if (*per_http_session_pme == NULL) { - struct per_session_pme_info *cur_ctx = (struct per_session_pme_info *)malloc(sizeof(struct per_session_pme_info)); - snprintf(cur_ctx->data, 6, "******"); - *per_session_pme = *&cur_ctx; - printf("http_event_plugin_entry->opening_handler\n"); + struct http_session_pme *cur_ctx = http_session_pme_create(); + memcpy(cur_ctx->data, "http_event_plugin_entry", strlen("http_event_plugin_entry")); + cur_ctx->flags = SESSION_EVENT_OPENING; + *per_http_session_pme = *&cur_ctx; } } if (event & SESSION_EVENT_RAWPKT) { - printf("http_event_plugin_entry->rawpkt_handler\n"); + /* + * Note: pm_session_dettach_me() + * The plugin manager just set the skip flag and don't call this event callback next. + * Before calling pm_session_dettach_me, the current plugin must release related resources for the current session. + */ + http_session_pme_destory(*per_http_session_pme); + *per_http_session_pme = NULL; + pm_session_dettach_me(session); + return; } if (event & SESSION_EVENT_ORDPKT) { - printf("http_event_plugin_entry->ordpkt_handler\n"); + // TODO + (*per_http_session_pme)->flags = SESSION_EVENT_ORDPKT; + pm_session_dettach_others(session); } if (event & SESSION_EVENT_META) { - printf("http_event_plugin_entry->meta_handler\n"); + // TODO + (*per_http_session_pme)->flags = SESSION_EVENT_META; } if (event & SESSION_EVENT_CLOSING) { - if (*per_session_pme) - { - printf("http_event_plugin_entry->closing_hanler\n"); - - free(*per_session_pme); - *per_session_pme = NULL; - } + http_session_pme_destory(*per_http_session_pme); + *per_http_session_pme = NULL; } } extern "C" int http_event_plugin_init(void) { - printf("RUN http_event_plugin_init\n"); - if (g_handler == NULL) { g_handler = (char *)malloc(1024); @@ -72,8 +101,6 @@ extern "C" int http_event_plugin_init(void) extern "C" void http_event_plugin_exit(void) { - printf("RUN http_event_plugin_exit\n"); - if (g_handler) { free(g_handler); diff --git a/src/protocol_decoder/http/http.cpp b/src/protocol_decoder/http/http.cpp index b3a9205..b24fffd 100644 --- a/src/protocol_decoder/http/http.cpp +++ b/src/protocol_decoder/http/http.cpp @@ -7,4 +7,8 @@ void http_decoder(const struct stellar_session *s, enum session_event_type event session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info); session_manager_trigger_event(new_session, SESSION_EVENT_META, info); +} + +void http_error_cb(const struct stellar_session *s, enum error_event_type event, void **pme) +{ } \ No newline at end of file diff --git a/src/session_manager/session_manager.cpp b/src/session_manager/session_manager.cpp index 7ea5643..91e2405 100644 --- a/src/session_manager/session_manager.cpp +++ b/src/session_manager/session_manager.cpp @@ -35,7 +35,7 @@ struct stellar_event *session_manager_fetch_event(struct session_manager *h) } /****************************************************************************** - * API: between stellar_event and plugin_manager + * stellar_event API For Plugin Manager ******************************************************************************/ struct session_plugin_ctx *stellar_event_get_plugin_ctx(struct stellar_event *event) @@ -80,3 +80,22 @@ uint16_t stellar_event_get_payload_length(struct stellar_event *event) { return 0; } + +/****************************************************************************** + * stellar_session API For Plugin Manager + ******************************************************************************/ + +struct session_plugin_ctx *stellar_session_get_plugin_ctx(const struct stellar_session *session) +{ + struct stellar_session_event_data *evdata = session->event_data; + return evdata->plugin_ctx; +} + +/****************************************************************************** + * stellar_session API For Plugin + ******************************************************************************/ + +const char *stellar_session_get_name(const struct stellar_session *session) +{ + return session->name; +} \ No newline at end of file diff --git a/src/session_manager/session_manager.h b/src/session_manager/session_manager.h index 1bef25f..ec8ac60 100644 --- a/src/session_manager/session_manager.h +++ b/src/session_manager/session_manager.h @@ -22,10 +22,11 @@ struct stellar_session const char *name; void *addr; void *data; + struct stellar_session_event_data *event_data; }; /****************************************************************************** - * API: between stellar_event and plugin_manager + * stellar_event API For Plugin Manager ******************************************************************************/ struct session_plugin_ctx *stellar_event_get_plugin_ctx(struct stellar_event *event); @@ -37,4 +38,10 @@ const struct stellar_session *stellar_event_get_session(struct stellar_event *ev struct stellar_packet *stellar_event_get_packet(struct stellar_event *event); const char *stellar_event_get_payload(struct stellar_event *event); -uint16_t stellar_event_get_payload_length(struct stellar_event *event); \ No newline at end of file +uint16_t stellar_event_get_payload_length(struct stellar_event *event); + +/****************************************************************************** + * stellar_session API For Plugin Manager + ******************************************************************************/ + +struct session_plugin_ctx *stellar_session_get_plugin_ctx(const struct stellar_session *session); \ No newline at end of file From e9e4e6b4bf9db0754068a028190a4fc23f4eb7fb Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Thu, 4 Aug 2022 18:00:25 +0800 Subject: [PATCH 11/17] Add sds open source code to deps directory --- CMakeLists.txt | 1 + deps/sds/CMakeLists.txt | 3 + deps/sds/sds.c | 1300 +++++++++++++++++++++++++++++++++++++++ deps/sds/sds.h | 274 +++++++++ deps/sds/sdsalloc.h | 42 ++ 5 files changed, 1620 insertions(+) create mode 100644 deps/sds/CMakeLists.txt create mode 100644 deps/sds/sds.c create mode 100644 deps/sds/sds.h create mode 100644 deps/sds/sdsalloc.h diff --git a/CMakeLists.txt b/CMakeLists.txt index fdb093f..d5a530f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ include_directories(${CMAKE_SOURCE_DIR}/sdk/include) #add vendor and source directory add_subdirectory(vendor) add_subdirectory(deps/toml) +add_subdirectory(deps/sds) add_subdirectory(src) add_subdirectory(sdk/example) diff --git a/deps/sds/CMakeLists.txt b/deps/sds/CMakeLists.txt new file mode 100644 index 0000000..ecc0931 --- /dev/null +++ b/deps/sds/CMakeLists.txt @@ -0,0 +1,3 @@ +set(CMAKE_C_FLAGS "-std=c99") +add_definitions(-fPIC) +add_library(sds STATIC sds.c) \ No newline at end of file diff --git a/deps/sds/sds.c b/deps/sds/sds.c new file mode 100644 index 0000000..1189716 --- /dev/null +++ b/deps/sds/sds.c @@ -0,0 +1,1300 @@ +/* SDSLib 2.0 -- A C dynamic strings library + * + * Copyright (c) 2006-2015, Salvatore Sanfilippo + * Copyright (c) 2015, Oran Agra + * Copyright (c) 2015, Redis Labs, Inc + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include "sds.h" +#include "sdsalloc.h" + +const char *SDS_NOINIT = "SDS_NOINIT"; + +static inline int sdsHdrSize(char type) { + switch(type&SDS_TYPE_MASK) { + case SDS_TYPE_5: + return sizeof(struct sdshdr5); + case SDS_TYPE_8: + return sizeof(struct sdshdr8); + case SDS_TYPE_16: + return sizeof(struct sdshdr16); + case SDS_TYPE_32: + return sizeof(struct sdshdr32); + case SDS_TYPE_64: + return sizeof(struct sdshdr64); + } + return 0; +} + +static inline char sdsReqType(size_t string_size) { + if (string_size < 1<<5) + return SDS_TYPE_5; + if (string_size < 1<<8) + return SDS_TYPE_8; + if (string_size < 1<<16) + return SDS_TYPE_16; +#if (LONG_MAX == LLONG_MAX) + if (string_size < 1ll<<32) + return SDS_TYPE_32; + return SDS_TYPE_64; +#else + return SDS_TYPE_32; +#endif +} + +/* Create a new sds string with the content specified by the 'init' pointer + * and 'initlen'. + * If NULL is used for 'init' the string is initialized with zero bytes. + * If SDS_NOINIT is used, the buffer is left uninitialized; + * + * The string is always null-termined (all the sds strings are, always) so + * even if you create an sds string with: + * + * mystring = sdsnewlen("abc",3); + * + * You can print the string with printf() as there is an implicit \0 at the + * end of the string. However the string is binary safe and can contain + * \0 characters in the middle, as the length is stored in the sds header. */ +sds sdsnewlen(const void *init, size_t initlen) { + void *sh; + sds s; + char type = sdsReqType(initlen); + /* Empty strings are usually created in order to append. Use type 8 + * since type 5 is not good at this. */ + if (type == SDS_TYPE_5 && initlen == 0) type = SDS_TYPE_8; + int hdrlen = sdsHdrSize(type); + unsigned char *fp; /* flags pointer. */ + + sh = s_malloc(hdrlen+initlen+1); + if (sh == NULL) return NULL; + if (init==SDS_NOINIT) + init = NULL; + else if (!init) + memset(sh, 0, hdrlen+initlen+1); + s = (char*)sh+hdrlen; + fp = ((unsigned char*)s)-1; + switch(type) { + case SDS_TYPE_5: { + *fp = type | (initlen << SDS_TYPE_BITS); + break; + } + case SDS_TYPE_8: { + SDS_HDR_VAR(8,s); + sh->len = initlen; + sh->alloc = initlen; + *fp = type; + break; + } + case SDS_TYPE_16: { + SDS_HDR_VAR(16,s); + sh->len = initlen; + sh->alloc = initlen; + *fp = type; + break; + } + case SDS_TYPE_32: { + SDS_HDR_VAR(32,s); + sh->len = initlen; + sh->alloc = initlen; + *fp = type; + break; + } + case SDS_TYPE_64: { + SDS_HDR_VAR(64,s); + sh->len = initlen; + sh->alloc = initlen; + *fp = type; + break; + } + } + if (initlen && init) + memcpy(s, init, initlen); + s[initlen] = '\0'; + return s; +} + +/* Create an empty (zero length) sds string. Even in this case the string + * always has an implicit null term. */ +sds sdsempty(void) { + return sdsnewlen("",0); +} + +/* Create a new sds string starting from a null terminated C string. */ +sds sdsnew(const char *init) { + size_t initlen = (init == NULL) ? 0 : strlen(init); + return sdsnewlen(init, initlen); +} + +/* Duplicate an sds string. */ +sds sdsdup(const sds s) { + return sdsnewlen(s, sdslen(s)); +} + +/* Free an sds string. No operation is performed if 's' is NULL. */ +void sdsfree(sds s) { + if (s == NULL) return; + s_free((char*)s-sdsHdrSize(s[-1])); +} + +/* Set the sds string length to the length as obtained with strlen(), so + * considering as content only up to the first null term character. + * + * This function is useful when the sds string is hacked manually in some + * way, like in the following example: + * + * s = sdsnew("foobar"); + * s[2] = '\0'; + * sdsupdatelen(s); + * printf("%d\n", sdslen(s)); + * + * The output will be "2", but if we comment out the call to sdsupdatelen() + * the output will be "6" as the string was modified but the logical length + * remains 6 bytes. */ +void sdsupdatelen(sds s) { + size_t reallen = strlen(s); + sdssetlen(s, reallen); +} + +/* Modify an sds string in-place to make it empty (zero length). + * However all the existing buffer is not discarded but set as free space + * so that next append operations will not require allocations up to the + * number of bytes previously available. */ +void sdsclear(sds s) { + sdssetlen(s, 0); + s[0] = '\0'; +} + +/* Enlarge the free space at the end of the sds string so that the caller + * is sure that after calling this function can overwrite up to addlen + * bytes after the end of the string, plus one more byte for nul term. + * + * Note: this does not change the *length* of the sds string as returned + * by sdslen(), but only the free buffer space we have. */ +sds sdsMakeRoomFor(sds s, size_t addlen) { + void *sh, *newsh; + size_t avail = sdsavail(s); + size_t len, newlen; + char type, oldtype = s[-1] & SDS_TYPE_MASK; + int hdrlen; + + /* Return ASAP if there is enough space left. */ + if (avail >= addlen) return s; + + len = sdslen(s); + sh = (char*)s-sdsHdrSize(oldtype); + newlen = (len+addlen); + if (newlen < SDS_MAX_PREALLOC) + newlen *= 2; + else + newlen += SDS_MAX_PREALLOC; + + type = sdsReqType(newlen); + + /* Don't use type 5: the user is appending to the string and type 5 is + * not able to remember empty space, so sdsMakeRoomFor() must be called + * at every appending operation. */ + if (type == SDS_TYPE_5) type = SDS_TYPE_8; + + hdrlen = sdsHdrSize(type); + if (oldtype==type) { + newsh = s_realloc(sh, hdrlen+newlen+1); + if (newsh == NULL) return NULL; + s = (char*)newsh+hdrlen; + } else { + /* Since the header size changes, need to move the string forward, + * and can't use realloc */ + newsh = s_malloc(hdrlen+newlen+1); + if (newsh == NULL) return NULL; + memcpy((char*)newsh+hdrlen, s, len+1); + s_free(sh); + s = (char*)newsh+hdrlen; + s[-1] = type; + sdssetlen(s, len); + } + sdssetalloc(s, newlen); + return s; +} + +/* Reallocate the sds string so that it has no free space at the end. The + * contained string remains not altered, but next concatenation operations + * will require a reallocation. + * + * After the call, the passed sds string is no longer valid and all the + * references must be substituted with the new pointer returned by the call. */ +sds sdsRemoveFreeSpace(sds s) { + void *sh, *newsh; + char type, oldtype = s[-1] & SDS_TYPE_MASK; + int hdrlen, oldhdrlen = sdsHdrSize(oldtype); + size_t len = sdslen(s); + size_t avail = sdsavail(s); + sh = (char*)s-oldhdrlen; + + /* Return ASAP if there is no space left. */ + if (avail == 0) return s; + + /* Check what would be the minimum SDS header that is just good enough to + * fit this string. */ + type = sdsReqType(len); + hdrlen = sdsHdrSize(type); + + /* If the type is the same, or at least a large enough type is still + * required, we just realloc(), letting the allocator to do the copy + * only if really needed. Otherwise if the change is huge, we manually + * reallocate the string to use the different header type. */ + if (oldtype==type || type > SDS_TYPE_8) { + newsh = s_realloc(sh, oldhdrlen+len+1); + if (newsh == NULL) return NULL; + s = (char*)newsh+oldhdrlen; + } else { + newsh = s_malloc(hdrlen+len+1); + if (newsh == NULL) return NULL; + memcpy((char*)newsh+hdrlen, s, len+1); + s_free(sh); + s = (char*)newsh+hdrlen; + s[-1] = type; + sdssetlen(s, len); + } + sdssetalloc(s, len); + return s; +} + +/* Return the total size of the allocation of the specified sds string, + * including: + * 1) The sds header before the pointer. + * 2) The string. + * 3) The free buffer at the end if any. + * 4) The implicit null term. + */ +size_t sdsAllocSize(sds s) { + size_t alloc = sdsalloc(s); + return sdsHdrSize(s[-1])+alloc+1; +} + +/* Return the pointer of the actual SDS allocation (normally SDS strings + * are referenced by the start of the string buffer). */ +void *sdsAllocPtr(sds s) { + return (void*) (s-sdsHdrSize(s[-1])); +} + +/* Increment the sds length and decrements the left free space at the + * end of the string according to 'incr'. Also set the null term + * in the new end of the string. + * + * This function is used in order to fix the string length after the + * user calls sdsMakeRoomFor(), writes something after the end of + * the current string, and finally needs to set the new length. + * + * Note: it is possible to use a negative increment in order to + * right-trim the string. + * + * Usage example: + * + * Using sdsIncrLen() and sdsMakeRoomFor() it is possible to mount the + * following schema, to cat bytes coming from the kernel to the end of an + * sds string without copying into an intermediate buffer: + * + * oldlen = sdslen(s); + * s = sdsMakeRoomFor(s, BUFFER_SIZE); + * nread = read(fd, s+oldlen, BUFFER_SIZE); + * ... check for nread <= 0 and handle it ... + * sdsIncrLen(s, nread); + */ +void sdsIncrLen(sds s, ssize_t incr) { + unsigned char flags = s[-1]; + size_t len; + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: { + unsigned char *fp = ((unsigned char*)s)-1; + unsigned char oldlen = SDS_TYPE_5_LEN(flags); + assert((incr > 0 && oldlen+incr < 32) || (incr < 0 && oldlen >= (unsigned int)(-incr))); + *fp = SDS_TYPE_5 | ((oldlen+incr) << SDS_TYPE_BITS); + len = oldlen+incr; + break; + } + case SDS_TYPE_8: { + SDS_HDR_VAR(8,s); + assert((incr >= 0 && sh->alloc-sh->len >= incr) || (incr < 0 && sh->len >= (unsigned int)(-incr))); + len = (sh->len += incr); + break; + } + case SDS_TYPE_16: { + SDS_HDR_VAR(16,s); + assert((incr >= 0 && sh->alloc-sh->len >= incr) || (incr < 0 && sh->len >= (unsigned int)(-incr))); + len = (sh->len += incr); + break; + } + case SDS_TYPE_32: { + SDS_HDR_VAR(32,s); + assert((incr >= 0 && sh->alloc-sh->len >= (unsigned int)incr) || (incr < 0 && sh->len >= (unsigned int)(-incr))); + len = (sh->len += incr); + break; + } + case SDS_TYPE_64: { + SDS_HDR_VAR(64,s); + assert((incr >= 0 && sh->alloc-sh->len >= (uint64_t)incr) || (incr < 0 && sh->len >= (uint64_t)(-incr))); + len = (sh->len += incr); + break; + } + default: len = 0; /* Just to avoid compilation warnings. */ + } + s[len] = '\0'; +} + +/* Grow the sds to have the specified length. Bytes that were not part of + * the original length of the sds will be set to zero. + * + * if the specified length is smaller than the current length, no operation + * is performed. */ +sds sdsgrowzero(sds s, size_t len) { + size_t curlen = sdslen(s); + + if (len <= curlen) return s; + s = sdsMakeRoomFor(s,len-curlen); + if (s == NULL) return NULL; + + /* Make sure added region doesn't contain garbage */ + memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */ + sdssetlen(s, len); + return s; +} + +/* Append the specified binary-safe string pointed by 't' of 'len' bytes to the + * end of the specified sds string 's'. + * + * After the call, the passed sds string is no longer valid and all the + * references must be substituted with the new pointer returned by the call. */ +sds sdscatlen(sds s, const void *t, size_t len) { + size_t curlen = sdslen(s); + + s = sdsMakeRoomFor(s,len); + if (s == NULL) return NULL; + memcpy(s+curlen, t, len); + sdssetlen(s, curlen+len); + s[curlen+len] = '\0'; + return s; +} + +/* Append the specified null termianted C string to the sds string 's'. + * + * After the call, the passed sds string is no longer valid and all the + * references must be substituted with the new pointer returned by the call. */ +sds sdscat(sds s, const char *t) { + return sdscatlen(s, t, strlen(t)); +} + +/* Append the specified sds 't' to the existing sds 's'. + * + * After the call, the modified sds string is no longer valid and all the + * references must be substituted with the new pointer returned by the call. */ +sds sdscatsds(sds s, const sds t) { + return sdscatlen(s, t, sdslen(t)); +} + +/* Destructively modify the sds string 's' to hold the specified binary + * safe string pointed by 't' of length 'len' bytes. */ +sds sdscpylen(sds s, const char *t, size_t len) { + if (sdsalloc(s) < len) { + s = sdsMakeRoomFor(s,len-sdslen(s)); + if (s == NULL) return NULL; + } + memcpy(s, t, len); + s[len] = '\0'; + sdssetlen(s, len); + return s; +} + +/* Like sdscpylen() but 't' must be a null-termined string so that the length + * of the string is obtained with strlen(). */ +sds sdscpy(sds s, const char *t) { + return sdscpylen(s, t, strlen(t)); +} + +/* Helper for sdscatlonglong() doing the actual number -> string + * conversion. 's' must point to a string with room for at least + * SDS_LLSTR_SIZE bytes. + * + * The function returns the length of the null-terminated string + * representation stored at 's'. */ +#define SDS_LLSTR_SIZE 21 +int sdsll2str(char *s, long long value) { + char *p, aux; + unsigned long long v; + size_t l; + + /* Generate the string representation, this method produces + * an reversed string. */ + v = (value < 0) ? -value : value; + p = s; + do { + *p++ = '0'+(v%10); + v /= 10; + } while(v); + if (value < 0) *p++ = '-'; + + /* Compute length and add null term. */ + l = p-s; + *p = '\0'; + + /* Reverse the string. */ + p--; + while(s < p) { + aux = *s; + *s = *p; + *p = aux; + s++; + p--; + } + return l; +} + +/* Identical sdsll2str(), but for unsigned long long type. */ +int sdsull2str(char *s, unsigned long long v) { + char *p, aux; + size_t l; + + /* Generate the string representation, this method produces + * an reversed string. */ + p = s; + do { + *p++ = '0'+(v%10); + v /= 10; + } while(v); + + /* Compute length and add null term. */ + l = p-s; + *p = '\0'; + + /* Reverse the string. */ + p--; + while(s < p) { + aux = *s; + *s = *p; + *p = aux; + s++; + p--; + } + return l; +} + +/* Create an sds string from a long long value. It is much faster than: + * + * sdscatprintf(sdsempty(),"%lld\n", value); + */ +sds sdsfromlonglong(long long value) { + char buf[SDS_LLSTR_SIZE]; + int len = sdsll2str(buf,value); + + return sdsnewlen(buf,len); +} + +/* Like sdscatprintf() but gets va_list instead of being variadic. */ +sds sdscatvprintf(sds s, const char *fmt, va_list ap) { + va_list cpy; + char staticbuf[1024], *buf = staticbuf, *t; + size_t buflen = strlen(fmt)*2; + + /* We try to start using a static buffer for speed. + * If not possible we revert to heap allocation. */ + if (buflen > sizeof(staticbuf)) { + buf = s_malloc(buflen); + if (buf == NULL) return NULL; + } else { + buflen = sizeof(staticbuf); + } + + /* Try with buffers two times bigger every time we fail to + * fit the string in the current buffer size. */ + while(1) { + buf[buflen-2] = '\0'; + va_copy(cpy,ap); + vsnprintf(buf, buflen, fmt, cpy); + va_end(cpy); + if (buf[buflen-2] != '\0') { + if (buf != staticbuf) s_free(buf); + buflen *= 2; + buf = s_malloc(buflen); + if (buf == NULL) return NULL; + continue; + } + break; + } + + /* Finally concat the obtained string to the SDS string and return it. */ + t = sdscat(s, buf); + if (buf != staticbuf) s_free(buf); + return t; +} + +/* Append to the sds string 's' a string obtained using printf-alike format + * specifier. + * + * After the call, the modified sds string is no longer valid and all the + * references must be substituted with the new pointer returned by the call. + * + * Example: + * + * s = sdsnew("Sum is: "); + * s = sdscatprintf(s,"%d+%d = %d",a,b,a+b). + * + * Often you need to create a string from scratch with the printf-alike + * format. When this is the need, just use sdsempty() as the target string: + * + * s = sdscatprintf(sdsempty(), "... your format ...", args); + */ +sds sdscatprintf(sds s, const char *fmt, ...) { + va_list ap; + char *t; + va_start(ap, fmt); + t = sdscatvprintf(s,fmt,ap); + va_end(ap); + return t; +} + +/* This function is similar to sdscatprintf, but much faster as it does + * not rely on sprintf() family functions implemented by the libc that + * are often very slow. Moreover directly handling the sds string as + * new data is concatenated provides a performance improvement. + * + * However this function only handles an incompatible subset of printf-alike + * format specifiers: + * + * %s - C String + * %S - SDS string + * %i - signed int + * %I - 64 bit signed integer (long long, int64_t) + * %u - unsigned int + * %U - 64 bit unsigned integer (unsigned long long, uint64_t) + * %% - Verbatim "%" character. + */ +sds sdscatfmt(sds s, char const *fmt, ...) { + size_t initlen = sdslen(s); + const char *f = fmt; + long i; + va_list ap; + + /* To avoid continuous reallocations, let's start with a buffer that + * can hold at least two times the format string itself. It's not the + * best heuristic but seems to work in practice. */ + s = sdsMakeRoomFor(s, initlen + strlen(fmt)*2); + va_start(ap,fmt); + f = fmt; /* Next format specifier byte to process. */ + i = initlen; /* Position of the next byte to write to dest str. */ + while(*f) { + char next, *str; + size_t l; + long long num; + unsigned long long unum; + + /* Make sure there is always space for at least 1 char. */ + if (sdsavail(s)==0) { + s = sdsMakeRoomFor(s,1); + } + + switch(*f) { + case '%': + next = *(f+1); + f++; + switch(next) { + case 's': + case 'S': + str = va_arg(ap,char*); + l = (next == 's') ? strlen(str) : sdslen(str); + if (sdsavail(s) < l) { + s = sdsMakeRoomFor(s,l); + } + memcpy(s+i,str,l); + sdsinclen(s,l); + i += l; + break; + case 'i': + case 'I': + if (next == 'i') + num = va_arg(ap,int); + else + num = va_arg(ap,long long); + { + char buf[SDS_LLSTR_SIZE]; + l = sdsll2str(buf,num); + if (sdsavail(s) < l) { + s = sdsMakeRoomFor(s,l); + } + memcpy(s+i,buf,l); + sdsinclen(s,l); + i += l; + } + break; + case 'u': + case 'U': + if (next == 'u') + unum = va_arg(ap,unsigned int); + else + unum = va_arg(ap,unsigned long long); + { + char buf[SDS_LLSTR_SIZE]; + l = sdsull2str(buf,unum); + if (sdsavail(s) < l) { + s = sdsMakeRoomFor(s,l); + } + memcpy(s+i,buf,l); + sdsinclen(s,l); + i += l; + } + break; + default: /* Handle %% and generally %. */ + s[i++] = next; + sdsinclen(s,1); + break; + } + break; + default: + s[i++] = *f; + sdsinclen(s,1); + break; + } + f++; + } + va_end(ap); + + /* Add null-term */ + s[i] = '\0'; + return s; +} + +/* Remove the part of the string from left and from right composed just of + * contiguous characters found in 'cset', that is a null terminted C string. + * + * After the call, the modified sds string is no longer valid and all the + * references must be substituted with the new pointer returned by the call. + * + * Example: + * + * s = sdsnew("AA...AA.a.aa.aHelloWorld :::"); + * s = sdstrim(s,"Aa. :"); + * printf("%s\n", s); + * + * Output will be just "HelloWorld". + */ +sds sdstrim(sds s, const char *cset) { + char *start, *end, *sp, *ep; + size_t len; + + sp = start = s; + ep = end = s+sdslen(s)-1; + while(sp <= end && strchr(cset, *sp)) sp++; + while(ep > sp && strchr(cset, *ep)) ep--; + len = (sp > ep) ? 0 : ((ep-sp)+1); + if (s != sp) memmove(s, sp, len); + s[len] = '\0'; + sdssetlen(s,len); + return s; +} + +/* Turn the string into a smaller (or equal) string containing only the + * substring specified by the 'start' and 'end' indexes. + * + * start and end can be negative, where -1 means the last character of the + * string, -2 the penultimate character, and so forth. + * + * The interval is inclusive, so the start and end characters will be part + * of the resulting string. + * + * The string is modified in-place. + * + * Example: + * + * s = sdsnew("Hello World"); + * sdsrange(s,1,-1); => "ello World" + */ +void sdsrange(sds s, ssize_t start, ssize_t end) { + size_t newlen, len = sdslen(s); + + if (len == 0) return; + if (start < 0) { + start = len+start; + if (start < 0) start = 0; + } + if (end < 0) { + end = len+end; + if (end < 0) end = 0; + } + newlen = (start > end) ? 0 : (end-start)+1; + if (newlen != 0) { + if (start >= (ssize_t)len) { + newlen = 0; + } else if (end >= (ssize_t)len) { + end = len-1; + newlen = (start > end) ? 0 : (end-start)+1; + } + } else { + start = 0; + } + if (start && newlen) memmove(s, s+start, newlen); + s[newlen] = 0; + sdssetlen(s,newlen); +} + +/* Apply tolower() to every character of the sds string 's'. */ +void sdstolower(sds s) { + size_t len = sdslen(s), j; + + for (j = 0; j < len; j++) s[j] = tolower(s[j]); +} + +/* Apply toupper() to every character of the sds string 's'. */ +void sdstoupper(sds s) { + size_t len = sdslen(s), j; + + for (j = 0; j < len; j++) s[j] = toupper(s[j]); +} + +/* Compare two sds strings s1 and s2 with memcmp(). + * + * Return value: + * + * positive if s1 > s2. + * negative if s1 < s2. + * 0 if s1 and s2 are exactly the same binary string. + * + * If two strings share exactly the same prefix, but one of the two has + * additional characters, the longer string is considered to be greater than + * the smaller one. */ +int sdscmp(const sds s1, const sds s2) { + size_t l1, l2, minlen; + int cmp; + + l1 = sdslen(s1); + l2 = sdslen(s2); + minlen = (l1 < l2) ? l1 : l2; + cmp = memcmp(s1,s2,minlen); + if (cmp == 0) return l1>l2? 1: (l1". + * + * After the call, the modified sds string is no longer valid and all the + * references must be substituted with the new pointer returned by the call. */ +sds sdscatrepr(sds s, const char *p, size_t len) { + s = sdscatlen(s,"\"",1); + while(len--) { + switch(*p) { + case '\\': + case '"': + s = sdscatprintf(s,"\\%c",*p); + break; + case '\n': s = sdscatlen(s,"\\n",2); break; + case '\r': s = sdscatlen(s,"\\r",2); break; + case '\t': s = sdscatlen(s,"\\t",2); break; + case '\a': s = sdscatlen(s,"\\a",2); break; + case '\b': s = sdscatlen(s,"\\b",2); break; + default: + if (isprint(*p)) + s = sdscatprintf(s,"%c",*p); + else + s = sdscatprintf(s,"\\x%02x",(unsigned char)*p); + break; + } + p++; + } + return sdscatlen(s,"\"",1); +} + +/* Helper function for sdssplitargs() that returns non zero if 'c' + * is a valid hex digit. */ +int is_hex_digit(char c) { + return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || + (c >= 'A' && c <= 'F'); +} + +/* Helper function for sdssplitargs() that converts a hex digit into an + * integer from 0 to 15 */ +int hex_digit_to_int(char c) { + switch(c) { + case '0': return 0; + case '1': return 1; + case '2': return 2; + case '3': return 3; + case '4': return 4; + case '5': return 5; + case '6': return 6; + case '7': return 7; + case '8': return 8; + case '9': return 9; + case 'a': case 'A': return 10; + case 'b': case 'B': return 11; + case 'c': case 'C': return 12; + case 'd': case 'D': return 13; + case 'e': case 'E': return 14; + case 'f': case 'F': return 15; + default: return 0; + } +} + +/* Split a line into arguments, where every argument can be in the + * following programming-language REPL-alike form: + * + * foo bar "newline are supported\n" and "\xff\x00otherstuff" + * + * The number of arguments is stored into *argc, and an array + * of sds is returned. + * + * The caller should free the resulting array of sds strings with + * sdsfreesplitres(). + * + * Note that sdscatrepr() is able to convert back a string into + * a quoted string in the same format sdssplitargs() is able to parse. + * + * The function returns the allocated tokens on success, even when the + * input string is empty, or NULL if the input contains unbalanced + * quotes or closed quotes followed by non space characters + * as in: "foo"bar or "foo' + */ +sds *sdssplitargs(const char *line, int *argc) { + const char *p = line; + char *current = NULL; + char **vector = NULL; + + *argc = 0; + while(1) { + /* skip blanks */ + while(*p && isspace(*p)) p++; + if (*p) { + /* get a token */ + int inq=0; /* set to 1 if we are in "quotes" */ + int insq=0; /* set to 1 if we are in 'single quotes' */ + int done=0; + + if (current == NULL) current = sdsempty(); + while(!done) { + if (inq) { + if (*p == '\\' && *(p+1) == 'x' && + is_hex_digit(*(p+2)) && + is_hex_digit(*(p+3))) + { + unsigned char byte; + + byte = (hex_digit_to_int(*(p+2))*16)+ + hex_digit_to_int(*(p+3)); + current = sdscatlen(current,(char*)&byte,1); + p += 3; + } else if (*p == '\\' && *(p+1)) { + char c; + + p++; + switch(*p) { + case 'n': c = '\n'; break; + case 'r': c = '\r'; break; + case 't': c = '\t'; break; + case 'b': c = '\b'; break; + case 'a': c = '\a'; break; + default: c = *p; break; + } + current = sdscatlen(current,&c,1); + } else if (*p == '"') { + /* closing quote must be followed by a space or + * nothing at all. */ + if (*(p+1) && !isspace(*(p+1))) goto err; + done=1; + } else if (!*p) { + /* unterminated quotes */ + goto err; + } else { + current = sdscatlen(current,p,1); + } + } else if (insq) { + if (*p == '\\' && *(p+1) == '\'') { + p++; + current = sdscatlen(current,"'",1); + } else if (*p == '\'') { + /* closing quote must be followed by a space or + * nothing at all. */ + if (*(p+1) && !isspace(*(p+1))) goto err; + done=1; + } else if (!*p) { + /* unterminated quotes */ + goto err; + } else { + current = sdscatlen(current,p,1); + } + } else { + switch(*p) { + case ' ': + case '\n': + case '\r': + case '\t': + case '\0': + done=1; + break; + case '"': + inq=1; + break; + case '\'': + insq=1; + break; + default: + current = sdscatlen(current,p,1); + break; + } + } + if (*p) p++; + } + /* add the token to the vector */ + vector = s_realloc(vector,((*argc)+1)*sizeof(char*)); + vector[*argc] = current; + (*argc)++; + current = NULL; + } else { + /* Even on empty input string return something not NULL. */ + if (vector == NULL) vector = s_malloc(sizeof(void*)); + return vector; + } + } + +err: + while((*argc)--) + sdsfree(vector[*argc]); + s_free(vector); + if (current) sdsfree(current); + *argc = 0; + return NULL; +} + +/* Modify the string substituting all the occurrences of the set of + * characters specified in the 'from' string to the corresponding character + * in the 'to' array. + * + * For instance: sdsmapchars(mystring, "ho", "01", 2) + * will have the effect of turning the string "hello" into "0ell1". + * + * The function returns the sds string pointer, that is always the same + * as the input pointer since no resize is needed. */ +sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) { + size_t j, i, l = sdslen(s); + + for (j = 0; j < l; j++) { + for (i = 0; i < setlen; i++) { + if (s[j] == from[i]) { + s[j] = to[i]; + break; + } + } + } + return s; +} + +/* Join an array of C strings using the specified separator (also a C string). + * Returns the result as an sds string. */ +sds sdsjoin(char **argv, int argc, char *sep) { + sds join = sdsempty(); + int j; + + for (j = 0; j < argc; j++) { + join = sdscat(join, argv[j]); + if (j != argc-1) join = sdscat(join,sep); + } + return join; +} + +/* Like sdsjoin, but joins an array of SDS strings. */ +sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen) { + sds join = sdsempty(); + int j; + + for (j = 0; j < argc; j++) { + join = sdscatsds(join, argv[j]); + if (j != argc-1) join = sdscatlen(join,sep,seplen); + } + return join; +} + +/* Wrappers to the allocators used by SDS. Note that SDS will actually + * just use the macros defined into sdsalloc.h in order to avoid to pay + * the overhead of function calls. Here we define these wrappers only for + * the programs SDS is linked to, if they want to touch the SDS internals + * even if they use a different allocator. */ +void *sds_malloc(size_t size) { return s_malloc(size); } +void *sds_realloc(void *ptr, size_t size) { return s_realloc(ptr,size); } +void sds_free(void *ptr) { s_free(ptr); } + +#if defined(SDS_TEST_MAIN) +#include +#include "testhelp.h" +#include "limits.h" + +#define UNUSED(x) (void)(x) +int sdsTest(void) { + { + sds x = sdsnew("foo"), y; + + test_cond("Create a string and obtain the length", + sdslen(x) == 3 && memcmp(x,"foo\0",4) == 0) + + sdsfree(x); + x = sdsnewlen("foo",2); + test_cond("Create a string with specified length", + sdslen(x) == 2 && memcmp(x,"fo\0",3) == 0) + + x = sdscat(x,"bar"); + test_cond("Strings concatenation", + sdslen(x) == 5 && memcmp(x,"fobar\0",6) == 0); + + x = sdscpy(x,"a"); + test_cond("sdscpy() against an originally longer string", + sdslen(x) == 1 && memcmp(x,"a\0",2) == 0) + + x = sdscpy(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk"); + test_cond("sdscpy() against an originally shorter string", + sdslen(x) == 33 && + memcmp(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0) + + sdsfree(x); + x = sdscatprintf(sdsempty(),"%d",123); + test_cond("sdscatprintf() seems working in the base case", + sdslen(x) == 3 && memcmp(x,"123\0",4) == 0) + + sdsfree(x); + x = sdsnew("--"); + x = sdscatfmt(x, "Hello %s World %I,%I--", "Hi!", LLONG_MIN,LLONG_MAX); + test_cond("sdscatfmt() seems working in the base case", + sdslen(x) == 60 && + memcmp(x,"--Hello Hi! World -9223372036854775808," + "9223372036854775807--",60) == 0) + printf("[%s]\n",x); + + sdsfree(x); + x = sdsnew("--"); + x = sdscatfmt(x, "%u,%U--", UINT_MAX, ULLONG_MAX); + test_cond("sdscatfmt() seems working with unsigned numbers", + sdslen(x) == 35 && + memcmp(x,"--4294967295,18446744073709551615--",35) == 0) + + sdsfree(x); + x = sdsnew(" x "); + sdstrim(x," x"); + test_cond("sdstrim() works when all chars match", + sdslen(x) == 0) + + sdsfree(x); + x = sdsnew(" x "); + sdstrim(x," "); + test_cond("sdstrim() works when a single char remains", + sdslen(x) == 1 && x[0] == 'x') + + sdsfree(x); + x = sdsnew("xxciaoyyy"); + sdstrim(x,"xy"); + test_cond("sdstrim() correctly trims characters", + sdslen(x) == 4 && memcmp(x,"ciao\0",5) == 0) + + y = sdsdup(x); + sdsrange(y,1,1); + test_cond("sdsrange(...,1,1)", + sdslen(y) == 1 && memcmp(y,"i\0",2) == 0) + + sdsfree(y); + y = sdsdup(x); + sdsrange(y,1,-1); + test_cond("sdsrange(...,1,-1)", + sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0) + + sdsfree(y); + y = sdsdup(x); + sdsrange(y,-2,-1); + test_cond("sdsrange(...,-2,-1)", + sdslen(y) == 2 && memcmp(y,"ao\0",3) == 0) + + sdsfree(y); + y = sdsdup(x); + sdsrange(y,2,1); + test_cond("sdsrange(...,2,1)", + sdslen(y) == 0 && memcmp(y,"\0",1) == 0) + + sdsfree(y); + y = sdsdup(x); + sdsrange(y,1,100); + test_cond("sdsrange(...,1,100)", + sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0) + + sdsfree(y); + y = sdsdup(x); + sdsrange(y,100,100); + test_cond("sdsrange(...,100,100)", + sdslen(y) == 0 && memcmp(y,"\0",1) == 0) + + sdsfree(y); + sdsfree(x); + x = sdsnew("foo"); + y = sdsnew("foa"); + test_cond("sdscmp(foo,foa)", sdscmp(x,y) > 0) + + sdsfree(y); + sdsfree(x); + x = sdsnew("bar"); + y = sdsnew("bar"); + test_cond("sdscmp(bar,bar)", sdscmp(x,y) == 0) + + sdsfree(y); + sdsfree(x); + x = sdsnew("aar"); + y = sdsnew("bar"); + test_cond("sdscmp(bar,bar)", sdscmp(x,y) < 0) + + sdsfree(y); + sdsfree(x); + x = sdsnewlen("\a\n\0foo\r",7); + y = sdscatrepr(sdsempty(),x,sdslen(x)); + test_cond("sdscatrepr(...data...)", + memcmp(y,"\"\\a\\n\\x00foo\\r\"",15) == 0) + + { + unsigned int oldfree; + char *p; + int step = 10, j, i; + + sdsfree(x); + sdsfree(y); + x = sdsnew("0"); + test_cond("sdsnew() free/len buffers", sdslen(x) == 1 && sdsavail(x) == 0); + + /* Run the test a few times in order to hit the first two + * SDS header types. */ + for (i = 0; i < 10; i++) { + int oldlen = sdslen(x); + x = sdsMakeRoomFor(x,step); + int type = x[-1]&SDS_TYPE_MASK; + + test_cond("sdsMakeRoomFor() len", sdslen(x) == oldlen); + if (type != SDS_TYPE_5) { + test_cond("sdsMakeRoomFor() free", sdsavail(x) >= step); + oldfree = sdsavail(x); + } + p = x+oldlen; + for (j = 0; j < step; j++) { + p[j] = 'A'+j; + } + sdsIncrLen(x,step); + } + test_cond("sdsMakeRoomFor() content", + memcmp("0ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ",x,101) == 0); + test_cond("sdsMakeRoomFor() final length",sdslen(x)==101); + + sdsfree(x); + } + } + test_report() + return 0; +} +#endif + +#ifdef SDS_TEST_MAIN +int main(void) { + return sdsTest(); +} +#endif diff --git a/deps/sds/sds.h b/deps/sds/sds.h new file mode 100644 index 0000000..adcc12c --- /dev/null +++ b/deps/sds/sds.h @@ -0,0 +1,274 @@ +/* SDSLib 2.0 -- A C dynamic strings library + * + * Copyright (c) 2006-2015, Salvatore Sanfilippo + * Copyright (c) 2015, Oran Agra + * Copyright (c) 2015, Redis Labs, Inc + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __SDS_H +#define __SDS_H + +#define SDS_MAX_PREALLOC (1024*1024) +extern const char *SDS_NOINIT; + +#include +#include +#include + +typedef char *sds; + +/* Note: sdshdr5 is never used, we just access the flags byte directly. + * However is here to document the layout of type 5 SDS strings. */ +struct __attribute__ ((__packed__)) sdshdr5 { + unsigned char flags; /* 3 lsb of type, and 5 msb of string length */ + char buf[]; +}; +struct __attribute__ ((__packed__)) sdshdr8 { + uint8_t len; /* used */ + uint8_t alloc; /* excluding the header and null terminator */ + unsigned char flags; /* 3 lsb of type, 5 unused bits */ + char buf[]; +}; +struct __attribute__ ((__packed__)) sdshdr16 { + uint16_t len; /* used */ + uint16_t alloc; /* excluding the header and null terminator */ + unsigned char flags; /* 3 lsb of type, 5 unused bits */ + char buf[]; +}; +struct __attribute__ ((__packed__)) sdshdr32 { + uint32_t len; /* used */ + uint32_t alloc; /* excluding the header and null terminator */ + unsigned char flags; /* 3 lsb of type, 5 unused bits */ + char buf[]; +}; +struct __attribute__ ((__packed__)) sdshdr64 { + uint64_t len; /* used */ + uint64_t alloc; /* excluding the header and null terminator */ + unsigned char flags; /* 3 lsb of type, 5 unused bits */ + char buf[]; +}; + +#define SDS_TYPE_5 0 +#define SDS_TYPE_8 1 +#define SDS_TYPE_16 2 +#define SDS_TYPE_32 3 +#define SDS_TYPE_64 4 +#define SDS_TYPE_MASK 7 +#define SDS_TYPE_BITS 3 +#define SDS_HDR_VAR(T,s) struct sdshdr##T *sh = (void*)((s)-(sizeof(struct sdshdr##T))); +#define SDS_HDR(T,s) ((struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T)))) +#define SDS_TYPE_5_LEN(f) ((f)>>SDS_TYPE_BITS) + +static inline size_t sdslen(const sds s) { + unsigned char flags = s[-1]; + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: + return SDS_TYPE_5_LEN(flags); + case SDS_TYPE_8: + return SDS_HDR(8,s)->len; + case SDS_TYPE_16: + return SDS_HDR(16,s)->len; + case SDS_TYPE_32: + return SDS_HDR(32,s)->len; + case SDS_TYPE_64: + return SDS_HDR(64,s)->len; + } + return 0; +} + +static inline size_t sdsavail(const sds s) { + unsigned char flags = s[-1]; + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: { + return 0; + } + case SDS_TYPE_8: { + SDS_HDR_VAR(8,s); + return sh->alloc - sh->len; + } + case SDS_TYPE_16: { + SDS_HDR_VAR(16,s); + return sh->alloc - sh->len; + } + case SDS_TYPE_32: { + SDS_HDR_VAR(32,s); + return sh->alloc - sh->len; + } + case SDS_TYPE_64: { + SDS_HDR_VAR(64,s); + return sh->alloc - sh->len; + } + } + return 0; +} + +static inline void sdssetlen(sds s, size_t newlen) { + unsigned char flags = s[-1]; + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: + { + unsigned char *fp = ((unsigned char*)s)-1; + *fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS); + } + break; + case SDS_TYPE_8: + SDS_HDR(8,s)->len = newlen; + break; + case SDS_TYPE_16: + SDS_HDR(16,s)->len = newlen; + break; + case SDS_TYPE_32: + SDS_HDR(32,s)->len = newlen; + break; + case SDS_TYPE_64: + SDS_HDR(64,s)->len = newlen; + break; + } +} + +static inline void sdsinclen(sds s, size_t inc) { + unsigned char flags = s[-1]; + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: + { + unsigned char *fp = ((unsigned char*)s)-1; + unsigned char newlen = SDS_TYPE_5_LEN(flags)+inc; + *fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS); + } + break; + case SDS_TYPE_8: + SDS_HDR(8,s)->len += inc; + break; + case SDS_TYPE_16: + SDS_HDR(16,s)->len += inc; + break; + case SDS_TYPE_32: + SDS_HDR(32,s)->len += inc; + break; + case SDS_TYPE_64: + SDS_HDR(64,s)->len += inc; + break; + } +} + +/* sdsalloc() = sdsavail() + sdslen() */ +static inline size_t sdsalloc(const sds s) { + unsigned char flags = s[-1]; + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: + return SDS_TYPE_5_LEN(flags); + case SDS_TYPE_8: + return SDS_HDR(8,s)->alloc; + case SDS_TYPE_16: + return SDS_HDR(16,s)->alloc; + case SDS_TYPE_32: + return SDS_HDR(32,s)->alloc; + case SDS_TYPE_64: + return SDS_HDR(64,s)->alloc; + } + return 0; +} + +static inline void sdssetalloc(sds s, size_t newlen) { + unsigned char flags = s[-1]; + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: + /* Nothing to do, this type has no total allocation info. */ + break; + case SDS_TYPE_8: + SDS_HDR(8,s)->alloc = newlen; + break; + case SDS_TYPE_16: + SDS_HDR(16,s)->alloc = newlen; + break; + case SDS_TYPE_32: + SDS_HDR(32,s)->alloc = newlen; + break; + case SDS_TYPE_64: + SDS_HDR(64,s)->alloc = newlen; + break; + } +} + +sds sdsnewlen(const void *init, size_t initlen); +sds sdsnew(const char *init); +sds sdsempty(void); +sds sdsdup(const sds s); +void sdsfree(sds s); +sds sdsgrowzero(sds s, size_t len); +sds sdscatlen(sds s, const void *t, size_t len); +sds sdscat(sds s, const char *t); +sds sdscatsds(sds s, const sds t); +sds sdscpylen(sds s, const char *t, size_t len); +sds sdscpy(sds s, const char *t); + +sds sdscatvprintf(sds s, const char *fmt, va_list ap); +#ifdef __GNUC__ +sds sdscatprintf(sds s, const char *fmt, ...) + __attribute__((format(printf, 2, 3))); +#else +sds sdscatprintf(sds s, const char *fmt, ...); +#endif + +sds sdscatfmt(sds s, char const *fmt, ...); +sds sdstrim(sds s, const char *cset); +void sdsrange(sds s, ssize_t start, ssize_t end); +void sdsupdatelen(sds s); +void sdsclear(sds s); +int sdscmp(const sds s1, const sds s2); +sds *sdssplitlen(const char *s, ssize_t len, const char *sep, int seplen, int *count); +void sdsfreesplitres(sds *tokens, int count); +void sdstolower(sds s); +void sdstoupper(sds s); +sds sdsfromlonglong(long long value); +sds sdscatrepr(sds s, const char *p, size_t len); +sds *sdssplitargs(const char *line, int *argc); +sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen); +sds sdsjoin(char **argv, int argc, char *sep); +sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen); + +/* Low level functions exposed to the user API */ +sds sdsMakeRoomFor(sds s, size_t addlen); +void sdsIncrLen(sds s, ssize_t incr); +sds sdsRemoveFreeSpace(sds s); +size_t sdsAllocSize(sds s); +void *sdsAllocPtr(sds s); + +/* Export the allocator used by SDS to the program using SDS. + * Sometimes the program SDS is linked to, may use a different set of + * allocators, but may want to allocate or free things that SDS will + * respectively free or allocate. */ +void *sds_malloc(size_t size); +void *sds_realloc(void *ptr, size_t size); +void sds_free(void *ptr); + +#ifdef REDIS_TEST +int sdsTest(int argc, char *argv[]); +#endif + +#endif diff --git a/deps/sds/sdsalloc.h b/deps/sds/sdsalloc.h new file mode 100644 index 0000000..f43023c --- /dev/null +++ b/deps/sds/sdsalloc.h @@ -0,0 +1,42 @@ +/* SDSLib 2.0 -- A C dynamic strings library + * + * Copyright (c) 2006-2015, Salvatore Sanfilippo + * Copyright (c) 2015, Oran Agra + * Copyright (c) 2015, Redis Labs, Inc + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* SDS allocator selection. + * + * This file is used in order to change the SDS allocator at compile time. + * Just define the following defines to what you want to use. Also add + * the include of your alternate allocator if needed (not needed in order + * to use the default libc allocator). */ + +#define s_malloc malloc +#define s_realloc realloc +#define s_free free From 772f4d8bd85d50c9a6910bcafae0221d97ad6408 Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Thu, 4 Aug 2022 18:07:34 +0800 Subject: [PATCH 12/17] Add timeout open source code to deps directory --- CMakeLists.txt | 1 + deps/timeout/CMakeLists.txt | 3 + deps/timeout/timeout-bitops.c | 249 ++++++++++++ deps/timeout/timeout.c | 744 ++++++++++++++++++++++++++++++++++ deps/timeout/timeout.h | 253 ++++++++++++ 5 files changed, 1250 insertions(+) create mode 100644 deps/timeout/CMakeLists.txt create mode 100644 deps/timeout/timeout-bitops.c create mode 100644 deps/timeout/timeout.c create mode 100644 deps/timeout/timeout.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d5a530f..d1f406e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ include_directories(${CMAKE_SOURCE_DIR}/sdk/include) add_subdirectory(vendor) add_subdirectory(deps/toml) add_subdirectory(deps/sds) +add_subdirectory(deps/timeout) add_subdirectory(src) add_subdirectory(sdk/example) diff --git a/deps/timeout/CMakeLists.txt b/deps/timeout/CMakeLists.txt new file mode 100644 index 0000000..e000160 --- /dev/null +++ b/deps/timeout/CMakeLists.txt @@ -0,0 +1,3 @@ +set(CMAKE_C_FLAGS "-std=c99") +add_definitions(-fPIC) +add_library(timeout STATIC timeout.c timeout-bitops.c) \ No newline at end of file diff --git a/deps/timeout/timeout-bitops.c b/deps/timeout/timeout-bitops.c new file mode 100644 index 0000000..d8325db --- /dev/null +++ b/deps/timeout/timeout-bitops.c @@ -0,0 +1,249 @@ +#include +#ifdef _MSC_VER +#include /* _BitScanForward, _BitScanReverse */ +#endif + +/* First define ctz and clz functions; these are compiler-dependent if + * you want them to be fast. */ +#if defined(__GNUC__) && !defined(TIMEOUT_DISABLE_GNUC_BITOPS) + +/* On GCC and clang and some others, we can use __builtin functions. They + * are not defined for n==0, but timeout.s never calls them with n==0. */ + +#define ctz64(n) __builtin_ctzll(n) +#define clz64(n) __builtin_clzll(n) +#if LONG_BITS == 32 +#define ctz32(n) __builtin_ctzl(n) +#define clz32(n) __builtin_clzl(n) +#else +#define ctz32(n) __builtin_ctz(n) +#define clz32(n) __builtin_clz(n) +#endif + +#elif defined(_MSC_VER) && !defined(TIMEOUT_DISABLE_MSVC_BITOPS) + +/* On MSVC, we have these handy functions. We can ignore their return + * values, since we will never supply val == 0. */ + +static __inline int ctz32(unsigned long val) +{ + DWORD zeros = 0; + _BitScanForward(&zeros, val); + return zeros; +} +static __inline int clz32(unsigned long val) +{ + DWORD zeros = 0; + _BitScanReverse(&zeros, val); + return zeros; +} +#ifdef _WIN64 +/* According to the documentation, these only exist on Win64. */ +static __inline int ctz64(uint64_t val) +{ + DWORD zeros = 0; + _BitScanForward64(&zeros, val); + return zeros; +} +static __inline int clz64(uint64_t val) +{ + DWORD zeros = 0; + _BitScanReverse64(&zeros, val); + return zeros; +} +#else +static __inline int ctz64(uint64_t val) +{ + uint32_t lo = (uint32_t) val; + uint32_t hi = (uint32_t) (val >> 32); + return lo ? ctz32(lo) : 32 + ctz32(hi); +} +static __inline int clz64(uint64_t val) +{ + uint32_t lo = (uint32_t) val; + uint32_t hi = (uint32_t) (val >> 32); + return hi ? clz32(hi) : 32 + clz32(lo); +} +#endif + +/* End of MSVC case. */ + +#else + +/* TODO: There are more clever ways to do this in the generic case. */ + + +#define process_(one, cz_bits, bits) \ + if (x < ( one << (cz_bits - bits))) { rv += bits; x <<= bits; } + +#define process64(bits) process_((UINT64_C(1)), 64, (bits)) +static inline int clz64(uint64_t x) +{ + int rv = 0; + + process64(32); + process64(16); + process64(8); + process64(4); + process64(2); + process64(1); + return rv; +} +#define process32(bits) process_((UINT32_C(1)), 32, (bits)) +static inline int clz32(uint32_t x) +{ + int rv = 0; + + process32(16); + process32(8); + process32(4); + process32(2); + process32(1); + return rv; +} + +#undef process_ +#undef process32 +#undef process64 +#define process_(one, bits) \ + if ((x & ((one << (bits))-1)) == 0) { rv += bits; x >>= bits; } + +#define process64(bits) process_((UINT64_C(1)), bits) +static inline int ctz64(uint64_t x) +{ + int rv = 0; + + process64(32); + process64(16); + process64(8); + process64(4); + process64(2); + process64(1); + return rv; +} + +#define process32(bits) process_((UINT32_C(1)), bits) +static inline int ctz32(uint32_t x) +{ + int rv = 0; + + process32(16); + process32(8); + process32(4); + process32(2); + process32(1); + return rv; +} + +#undef process32 +#undef process64 +#undef process_ + +/* End of generic case */ + +#endif /* End of defining ctz */ + +#ifdef TEST_BITOPS +#include +#include + +static uint64_t testcases[] = { + 13371337 * 10, + 100, + 385789752, + 82574, + (((uint64_t)1)<<63) + (((uint64_t)1)<<31) + 10101 +}; + +static int +naive_clz(int bits, uint64_t v) +{ + int r = 0; + uint64_t bit = ((uint64_t)1) << (bits-1); + while (bit && 0 == (v & bit)) { + r++; + bit >>= 1; + } + /* printf("clz(%d,%lx) -> %d\n", bits, v, r); */ + return r; +} + +static int +naive_ctz(int bits, uint64_t v) +{ + int r = 0; + uint64_t bit = 1; + while (bit && 0 == (v & bit)) { + r++; + bit <<= 1; + if (r == bits) + break; + } + /* printf("ctz(%d,%lx) -> %d\n", bits, v, r); */ + return r; +} + +static int +check(uint64_t vv) +{ + uint32_t v32 = (uint32_t) vv; + + if (vv == 0) + return 1; /* c[tl]z64(0) is undefined. */ + + if (ctz64(vv) != naive_ctz(64, vv)) { + printf("mismatch with ctz64: %d\n", ctz64(vv)); + exit(1); + return 0; + } + if (clz64(vv) != naive_clz(64, vv)) { + printf("mismatch with clz64: %d\n", clz64(vv)); + exit(1); + return 0; + } + + if (v32 == 0) + return 1; /* c[lt]z(0) is undefined. */ + + if (ctz32(v32) != naive_ctz(32, v32)) { + printf("mismatch with ctz32: %d\n", ctz32(v32)); + exit(1); + return 0; + } + if (clz32(v32) != naive_clz(32, v32)) { + printf("mismatch with clz32: %d\n", clz32(v32)); + exit(1); + return 0; + } + return 1; +} + +int +main(int c, char **v) +{ + unsigned int i; + const unsigned int n = sizeof(testcases)/sizeof(testcases[0]); + int result = 0; + + for (i = 0; i <= 63; ++i) { + uint64_t x = 1 << i; + if (!check(x)) + result = 1; + --x; + if (!check(x)) + result = 1; + } + + for (i = 0; i < n; ++i) { + if (! check(testcases[i])) + result = 1; + } + if (result) { + puts("FAIL"); + } else { + puts("OK"); + } + return result; +} +#endif + diff --git a/deps/timeout/timeout.c b/deps/timeout/timeout.c new file mode 100644 index 0000000..e78f57d --- /dev/null +++ b/deps/timeout/timeout.c @@ -0,0 +1,744 @@ +/* ========================================================================== + * timeout.c - Tickless hierarchical timing wheel. + * -------------------------------------------------------------------------- + * Copyright (c) 2013, 2014 William Ahern + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + * ========================================================================== + */ +#include /* CHAR_BIT */ + +#include /* NULL */ +#include /* malloc(3) free(3) */ +#include /* FILE fprintf(3) */ + +#include /* UINT64_C uint64_t */ + +#include /* memset(3) */ + +#include /* errno */ + +#include /* TAILQ(3) */ + +#include "timeout.h" + +#if TIMEOUT_DEBUG - 0 +#include "timeout-debug.h" +#endif + +#ifdef TIMEOUT_DISABLE_RELATIVE_ACCESS +#define TO_SET_TIMEOUTS(to, T) ((void)0) +#else +#define TO_SET_TIMEOUTS(to, T) ((to)->timeouts = (T)) +#endif + +/* + * A N C I L L A R Y R O U T I N E S + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#define abstime_t timeout_t /* for documentation purposes */ +#define reltime_t timeout_t /* "" */ + +#if !defined countof +#define countof(a) (sizeof (a) / sizeof *(a)) +#endif + +#if !defined endof +#define endof(a) (&(a)[countof(a)]) +#endif + +#if !defined MIN +#define MIN(a, b) (((a) < (b))? (a) : (b)) +#endif + +#if !defined MAX +#define MAX(a, b) (((a) > (b))? (a) : (b)) +#endif + +#if !defined TAILQ_CONCAT +#define TAILQ_CONCAT(head1, head2, field) do { \ + if (!TAILQ_EMPTY(head2)) { \ + *(head1)->tqh_last = (head2)->tqh_first; \ + (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ + (head1)->tqh_last = (head2)->tqh_last; \ + TAILQ_INIT((head2)); \ + } \ +} while (0) +#endif + +#if !defined TAILQ_FOREACH_SAFE +#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = TAILQ_FIRST(head); \ + (var) && ((tvar) = TAILQ_NEXT(var, field), 1); \ + (var) = (tvar)) +#endif + + +/* + * B I T M A N I P U L A T I O N R O U T I N E S + * + * The macros and routines below implement wheel parameterization. The + * inputs are: + * + * WHEEL_BIT - The number of value bits mapped in each wheel. The + * lowest-order WHEEL_BIT bits index the lowest-order (highest + * resolution) wheel, the next group of WHEEL_BIT bits the + * higher wheel, etc. + * + * WHEEL_NUM - The number of wheels. WHEEL_BIT * WHEEL_NUM = the number of + * value bits used by all the wheels. For the default of 6 and + * 4, only the low 24 bits are processed. Any timeout value + * larger than this will cycle through again. + * + * The implementation uses bit fields to remember which slot in each wheel + * is populated, and to generate masks of expiring slots according to the + * current update interval (i.e. the "tickless" aspect). The slots to + * process in a wheel are (populated-set & interval-mask). + * + * WHEEL_BIT cannot be larger than 6 bits because 2^6 -> 64 is the largest + * number of slots which can be tracked in a uint64_t integer bit field. + * WHEEL_BIT cannot be smaller than 3 bits because of our rotr and rotl + * routines, which only operate on all the value bits in an integer, and + * there's no integer smaller than uint8_t. + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#if !defined WHEEL_BIT +#define WHEEL_BIT 6 +#endif + +#if !defined WHEEL_NUM +#define WHEEL_NUM 4 +#endif + +#define WHEEL_LEN (1U << WHEEL_BIT) +#define WHEEL_MAX (WHEEL_LEN - 1) +#define WHEEL_MASK (WHEEL_LEN - 1) +#define TIMEOUT_MAX ((TIMEOUT_C(1) << (WHEEL_BIT * WHEEL_NUM)) - 1) + +#include "timeout-bitops.c" + +#if WHEEL_BIT == 6 +#define ctz(n) ctz64(n) +#define clz(n) clz64(n) +#define fls(n) ((int)(64 - clz64(n))) +#else +#define ctz(n) ctz32(n) +#define clz(n) clz32(n) +#define fls(n) ((int)(32 - clz32(n))) +#endif + +#if WHEEL_BIT == 6 +#define WHEEL_C(n) UINT64_C(n) +#define WHEEL_PRIu PRIu64 +#define WHEEL_PRIx PRIx64 + +typedef uint64_t wheel_t; + +#elif WHEEL_BIT == 5 + +#define WHEEL_C(n) UINT32_C(n) +#define WHEEL_PRIu PRIu32 +#define WHEEL_PRIx PRIx32 + +typedef uint32_t wheel_t; + +#elif WHEEL_BIT == 4 + +#define WHEEL_C(n) UINT16_C(n) +#define WHEEL_PRIu PRIu16 +#define WHEEL_PRIx PRIx16 + +typedef uint16_t wheel_t; + +#elif WHEEL_BIT == 3 + +#define WHEEL_C(n) UINT8_C(n) +#define WHEEL_PRIu PRIu8 +#define WHEEL_PRIx PRIx8 + +typedef uint8_t wheel_t; + +#else +#error invalid WHEEL_BIT value +#endif + + +static inline wheel_t rotl(const wheel_t v, int c) { + if (!(c &= (sizeof v * CHAR_BIT - 1))) + return v; + + return (v << c) | (v >> (sizeof v * CHAR_BIT - c)); +} /* rotl() */ + + +static inline wheel_t rotr(const wheel_t v, int c) { + if (!(c &= (sizeof v * CHAR_BIT - 1))) + return v; + + return (v >> c) | (v << (sizeof v * CHAR_BIT - c)); +} /* rotr() */ + + +/* + * T I M E R R O U T I N E S + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +TAILQ_HEAD(timeout_list, timeout); + +struct timeouts { + struct timeout_list wheel[WHEEL_NUM][WHEEL_LEN], expired; + + wheel_t pending[WHEEL_NUM]; + + timeout_t curtime; + timeout_t hertz; +}; /* struct timeouts */ + + +static struct timeouts *timeouts_init(struct timeouts *T, timeout_t hz) { + unsigned i, j; + + for (i = 0; i < countof(T->wheel); i++) { + for (j = 0; j < countof(T->wheel[i]); j++) { + TAILQ_INIT(&T->wheel[i][j]); + } + } + + TAILQ_INIT(&T->expired); + + for (i = 0; i < countof(T->pending); i++) { + T->pending[i] = 0; + } + + T->curtime = 0; + T->hertz = (hz)? hz : TIMEOUT_mHZ; + + return T; +} /* timeouts_init() */ + + +TIMEOUT_PUBLIC struct timeouts *timeouts_open(timeout_t hz, int *error) { + struct timeouts *T; + + if ((T = malloc(sizeof *T))) + return timeouts_init(T, hz); + + *error = errno; + + return NULL; +} /* timeouts_open() */ + + +static void timeouts_reset(struct timeouts *T) { + struct timeout_list reset; + struct timeout *to; + unsigned i, j; + + TAILQ_INIT(&reset); + + for (i = 0; i < countof(T->wheel); i++) { + for (j = 0; j < countof(T->wheel[i]); j++) { + TAILQ_CONCAT(&reset, &T->wheel[i][j], tqe); + } + } + + TAILQ_CONCAT(&reset, &T->expired, tqe); + + TAILQ_FOREACH(to, &reset, tqe) { + to->pending = NULL; + TO_SET_TIMEOUTS(to, NULL); + } +} /* timeouts_reset() */ + + +TIMEOUT_PUBLIC void timeouts_close(struct timeouts *T) { + /* + * NOTE: Delete installed timeouts so timeout_pending() and + * timeout_expired() worked as expected. + */ + timeouts_reset(T); + + free(T); +} /* timeouts_close() */ + + +TIMEOUT_PUBLIC timeout_t timeouts_hz(struct timeouts *T) { + return T->hertz; +} /* timeouts_hz() */ + + +TIMEOUT_PUBLIC void timeouts_del(struct timeouts *T, struct timeout *to) { + if (to->pending) { + TAILQ_REMOVE(to->pending, to, tqe); + + if (to->pending != &T->expired && TAILQ_EMPTY(to->pending)) { + ptrdiff_t index = to->pending - &T->wheel[0][0]; + int wheel = index / WHEEL_LEN; + int slot = index % WHEEL_LEN; + + T->pending[wheel] &= ~(WHEEL_C(1) << slot); + } + + to->pending = NULL; + TO_SET_TIMEOUTS(to, NULL); + } +} /* timeouts_del() */ + + +static inline reltime_t timeout_rem(struct timeouts *T, struct timeout *to) { + return to->expires - T->curtime; +} /* timeout_rem() */ + + +static inline int timeout_wheel(timeout_t timeout) { + /* must be called with timeout != 0, so fls input is nonzero */ + return (fls(MIN(timeout, TIMEOUT_MAX)) - 1) / WHEEL_BIT; +} /* timeout_wheel() */ + + +static inline int timeout_slot(int wheel, timeout_t expires) { + return WHEEL_MASK & ((expires >> (wheel * WHEEL_BIT)) - !!wheel); +} /* timeout_slot() */ + + +static void timeouts_sched(struct timeouts *T, struct timeout *to, timeout_t expires) { + timeout_t rem; + int wheel, slot; + + timeouts_del(T, to); + + to->expires = expires; + + TO_SET_TIMEOUTS(to, T); + + if (expires > T->curtime) { + rem = timeout_rem(T, to); + + /* rem is nonzero since: + * rem == timeout_rem(T,to), + * == to->expires - T->curtime + * and above we have expires > T->curtime. + */ + wheel = timeout_wheel(rem); + slot = timeout_slot(wheel, to->expires); + + to->pending = &T->wheel[wheel][slot]; + TAILQ_INSERT_TAIL(to->pending, to, tqe); + + T->pending[wheel] |= WHEEL_C(1) << slot; + } else { + to->pending = &T->expired; + TAILQ_INSERT_TAIL(to->pending, to, tqe); + } +} /* timeouts_sched() */ + + +#ifndef TIMEOUT_DISABLE_INTERVALS +static void timeouts_readd(struct timeouts *T, struct timeout *to) { + to->expires += to->interval; + + if (to->expires <= T->curtime) { + /* If we've missed the next firing of this timeout, reschedule + * it to occur at the next multiple of its interval after + * the last time that it fired. + */ + timeout_t n = T->curtime - to->expires; + timeout_t r = n % to->interval; + to->expires = T->curtime + (to->interval - r); + } + + timeouts_sched(T, to, to->expires); +} /* timeouts_readd() */ +#endif + + +TIMEOUT_PUBLIC void timeouts_add(struct timeouts *T, struct timeout *to, timeout_t timeout) { +#ifndef TIMEOUT_DISABLE_INTERVALS + if (to->flags & TIMEOUT_INT) + to->interval = MAX(1, timeout); +#endif + + if (to->flags & TIMEOUT_ABS) + timeouts_sched(T, to, timeout); + else + timeouts_sched(T, to, T->curtime + timeout); +} /* timeouts_add() */ + + +TIMEOUT_PUBLIC void timeouts_update(struct timeouts *T, abstime_t curtime) { + timeout_t elapsed = curtime - T->curtime; + struct timeout_list todo; + int wheel; + + TAILQ_INIT(&todo); + + /* + * There's no avoiding looping over every wheel. It's best to keep + * WHEEL_NUM smallish. + */ + for (wheel = 0; wheel < WHEEL_NUM; wheel++) { + wheel_t pending; + + /* + * Calculate the slots expiring in this wheel + * + * If the elapsed time is greater than the maximum period of + * the wheel, mark every position as expiring. + * + * Otherwise, to determine the expired slots fill in all the + * bits between the last slot processed and the current + * slot, inclusive of the last slot. We'll bitwise-AND this + * with our pending set below. + * + * If a wheel rolls over, force a tick of the next higher + * wheel. + */ + if ((elapsed >> (wheel * WHEEL_BIT)) > WHEEL_MAX) { + pending = (wheel_t)~WHEEL_C(0); + } else { + wheel_t _elapsed = WHEEL_MASK & (elapsed >> (wheel * WHEEL_BIT)); + int oslot, nslot; + + /* + * TODO: It's likely that at least one of the + * following three bit fill operations is redundant + * or can be replaced with a simpler operation. + */ + oslot = WHEEL_MASK & (T->curtime >> (wheel * WHEEL_BIT)); + pending = rotl(((UINT64_C(1) << _elapsed) - 1), oslot); + + nslot = WHEEL_MASK & (curtime >> (wheel * WHEEL_BIT)); + pending |= rotr(rotl(((WHEEL_C(1) << _elapsed) - 1), nslot), _elapsed); + pending |= WHEEL_C(1) << nslot; + } + + while (pending & T->pending[wheel]) { + /* ctz input cannot be zero: loop condition. */ + int slot = ctz(pending & T->pending[wheel]); + TAILQ_CONCAT(&todo, &T->wheel[wheel][slot], tqe); + T->pending[wheel] &= ~(UINT64_C(1) << slot); + } + + if (!(0x1 & pending)) + break; /* break if we didn't wrap around end of wheel */ + + /* if we're continuing, the next wheel must tick at least once */ + elapsed = MAX(elapsed, (WHEEL_LEN << (wheel * WHEEL_BIT))); + } + + T->curtime = curtime; + + while (!TAILQ_EMPTY(&todo)) { + struct timeout *to = TAILQ_FIRST(&todo); + + TAILQ_REMOVE(&todo, to, tqe); + to->pending = NULL; + + timeouts_sched(T, to, to->expires); + } + + return; +} /* timeouts_update() */ + + +TIMEOUT_PUBLIC void timeouts_step(struct timeouts *T, reltime_t elapsed) { + timeouts_update(T, T->curtime + elapsed); +} /* timeouts_step() */ + + +TIMEOUT_PUBLIC bool timeouts_pending(struct timeouts *T) { + wheel_t pending = 0; + int wheel; + + for (wheel = 0; wheel < WHEEL_NUM; wheel++) { + pending |= T->pending[wheel]; + } + + return !!pending; +} /* timeouts_pending() */ + + +TIMEOUT_PUBLIC bool timeouts_expired(struct timeouts *T) { + return !TAILQ_EMPTY(&T->expired); +} /* timeouts_expired() */ + + +/* + * Calculate the interval before needing to process any timeouts pending on + * any wheel. + * + * (This is separated from the public API routine so we can evaluate our + * wheel invariant assertions irrespective of the expired queue.) + * + * This might return a timeout value sooner than any installed timeout if + * only higher-order wheels have timeouts pending. We can only know when to + * process a wheel, not precisely when a timeout is scheduled. Our timeout + * accuracy could be off by 2^(N*M)-1 units where N is the wheel number and + * M is WHEEL_BIT. Only timeouts which have fallen through to wheel 0 can be + * known exactly. + * + * We should never return a timeout larger than the lowest actual timeout. + */ +static timeout_t timeouts_int(struct timeouts *T) { + timeout_t timeout = ~TIMEOUT_C(0), _timeout; + timeout_t relmask; + int wheel, slot; + + relmask = 0; + + for (wheel = 0; wheel < WHEEL_NUM; wheel++) { + if (T->pending[wheel]) { + slot = WHEEL_MASK & (T->curtime >> (wheel * WHEEL_BIT)); + + /* ctz input cannot be zero: T->pending[wheel] is + * nonzero, so rotr() is nonzero. */ + _timeout = (ctz(rotr(T->pending[wheel], slot)) + !!wheel) << (wheel * WHEEL_BIT); + /* +1 to higher order wheels as those timeouts are one rotation in the future (otherwise they'd be on a lower wheel or expired) */ + + _timeout -= relmask & T->curtime; + /* reduce by how much lower wheels have progressed */ + + timeout = MIN(_timeout, timeout); + } + + relmask <<= WHEEL_BIT; + relmask |= WHEEL_MASK; + } + + return timeout; +} /* timeouts_int() */ + + +/* + * Calculate the interval our caller can wait before needing to process + * events. + */ +TIMEOUT_PUBLIC timeout_t timeouts_timeout(struct timeouts *T) { + if (!TAILQ_EMPTY(&T->expired)) + return 0; + + return timeouts_int(T); +} /* timeouts_timeout() */ + + +TIMEOUT_PUBLIC struct timeout *timeouts_get(struct timeouts *T) { + if (!TAILQ_EMPTY(&T->expired)) { + struct timeout *to = TAILQ_FIRST(&T->expired); + + TAILQ_REMOVE(&T->expired, to, tqe); + to->pending = NULL; + TO_SET_TIMEOUTS(to, NULL); + +#ifndef TIMEOUT_DISABLE_INTERVALS + if ((to->flags & TIMEOUT_INT) && to->interval > 0) + timeouts_readd(T, to); +#endif + + return to; + } else { + return 0; + } +} /* timeouts_get() */ + + +/* + * Use dumb looping to locate the earliest timeout pending on the wheel so + * our invariant assertions can check the result of our optimized code. + */ +static struct timeout *timeouts_min(struct timeouts *T) { + struct timeout *to, *min = NULL; + unsigned i, j; + + for (i = 0; i < countof(T->wheel); i++) { + for (j = 0; j < countof(T->wheel[i]); j++) { + TAILQ_FOREACH(to, &T->wheel[i][j], tqe) { + if (!min || to->expires < min->expires) + min = to; + } + } + } + + return min; +} /* timeouts_min() */ + + +/* + * Check some basic algorithm invariants. If these invariants fail then + * something is definitely broken. + */ +#define report(...) do { \ + if ((fp)) \ + fprintf(fp, __VA_ARGS__); \ +} while (0) + +#define check(expr, ...) do { \ + if (!(expr)) { \ + report(__VA_ARGS__); \ + return 0; \ + } \ +} while (0) + +TIMEOUT_PUBLIC bool timeouts_check(struct timeouts *T, FILE *fp) { + timeout_t timeout; + struct timeout *to; + + if ((to = timeouts_min(T))) { + check(to->expires > T->curtime, "missed timeout (expires:%" TIMEOUT_PRIu " <= curtime:%" TIMEOUT_PRIu ")\n", to->expires, T->curtime); + + timeout = timeouts_int(T); + check(timeout <= to->expires - T->curtime, "wrong soft timeout (soft:%" TIMEOUT_PRIu " > hard:%" TIMEOUT_PRIu ") (expires:%" TIMEOUT_PRIu "; curtime:%" TIMEOUT_PRIu ")\n", timeout, (to->expires - T->curtime), to->expires, T->curtime); + + timeout = timeouts_timeout(T); + check(timeout <= to->expires - T->curtime, "wrong soft timeout (soft:%" TIMEOUT_PRIu " > hard:%" TIMEOUT_PRIu ") (expires:%" TIMEOUT_PRIu "; curtime:%" TIMEOUT_PRIu ")\n", timeout, (to->expires - T->curtime), to->expires, T->curtime); + } else { + timeout = timeouts_timeout(T); + + if (!TAILQ_EMPTY(&T->expired)) + check(timeout == 0, "wrong soft timeout (soft:%" TIMEOUT_PRIu " != hard:%" TIMEOUT_PRIu ")\n", timeout, TIMEOUT_C(0)); + else + check(timeout == ~TIMEOUT_C(0), "wrong soft timeout (soft:%" TIMEOUT_PRIu " != hard:%" TIMEOUT_PRIu ")\n", timeout, ~TIMEOUT_C(0)); + } + + return 1; +} /* timeouts_check() */ + + +#define ENTER \ + do { \ + static const int pc0 = __LINE__; \ + switch (pc0 + it->pc) { \ + case __LINE__: (void)0 + +#define SAVE_AND_DO(do_statement) \ + do { \ + it->pc = __LINE__ - pc0; \ + do_statement; \ + case __LINE__: (void)0; \ + } while (0) + +#define YIELD(rv) \ + SAVE_AND_DO(return (rv)) + +#define LEAVE \ + SAVE_AND_DO(break); \ + } \ + } while (0) + +TIMEOUT_PUBLIC struct timeout *timeouts_next(struct timeouts *T, struct timeouts_it *it) { + struct timeout *to; + + ENTER; + + if (it->flags & TIMEOUTS_EXPIRED) { + if (it->flags & TIMEOUTS_CLEAR) { + while ((to = timeouts_get(T))) { + YIELD(to); + } + } else { + TAILQ_FOREACH_SAFE(to, &T->expired, tqe, it->to) { + YIELD(to); + } + } + } + + if (it->flags & TIMEOUTS_PENDING) { + for (it->i = 0; it->i < countof(T->wheel); it->i++) { + for (it->j = 0; it->j < countof(T->wheel[it->i]); it->j++) { + TAILQ_FOREACH_SAFE(to, &T->wheel[it->i][it->j], tqe, it->to) { + YIELD(to); + } + } + } + } + + LEAVE; + + return NULL; +} /* timeouts_next */ + +#undef LEAVE +#undef YIELD +#undef SAVE_AND_DO +#undef ENTER + + +/* + * T I M E O U T R O U T I N E S + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +TIMEOUT_PUBLIC struct timeout *timeout_init(struct timeout *to, int flags) { + memset(to, 0, sizeof *to); + + to->flags = flags; + + return to; +} /* timeout_init() */ + + +#ifndef TIMEOUT_DISABLE_RELATIVE_ACCESS +TIMEOUT_PUBLIC bool timeout_pending(struct timeout *to) { + return to->pending && to->pending != &to->timeouts->expired; +} /* timeout_pending() */ + + +TIMEOUT_PUBLIC bool timeout_expired(struct timeout *to) { + return to->pending && to->pending == &to->timeouts->expired; +} /* timeout_expired() */ + + +TIMEOUT_PUBLIC void timeout_del(struct timeout *to) { + timeouts_del(to->timeouts, to); +} /* timeout_del() */ +#endif + + +/* + * V E R S I O N I N T E R F A C E S + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +TIMEOUT_PUBLIC int timeout_version(void) { + return TIMEOUT_VERSION; +} /* timeout_version() */ + + +TIMEOUT_PUBLIC const char *timeout_vendor(void) { + return TIMEOUT_VENDOR; +} /* timeout_version() */ + + +TIMEOUT_PUBLIC int timeout_v_rel(void) { + return TIMEOUT_V_REL; +} /* timeout_version() */ + + +TIMEOUT_PUBLIC int timeout_v_abi(void) { + return TIMEOUT_V_ABI; +} /* timeout_version() */ + + +TIMEOUT_PUBLIC int timeout_v_api(void) { + return TIMEOUT_V_API; +} /* timeout_version() */ + diff --git a/deps/timeout/timeout.h b/deps/timeout/timeout.h new file mode 100644 index 0000000..3ef76e9 --- /dev/null +++ b/deps/timeout/timeout.h @@ -0,0 +1,253 @@ +/* ========================================================================== + * timeout.h - Tickless hierarchical timing wheel. + * -------------------------------------------------------------------------- + * Copyright (c) 2013, 2014 William Ahern + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + * ========================================================================== + */ +#ifndef TIMEOUT_H +#define TIMEOUT_H + +#include /* bool */ +#include /* FILE */ + +#include /* PRIu64 PRIx64 PRIX64 uint64_t */ + +#include /* TAILQ(3) */ + + +/* + * V E R S I O N I N T E R F A C E S + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#if !defined TIMEOUT_PUBLIC +#define TIMEOUT_PUBLIC +#endif + +#define TIMEOUT_VERSION TIMEOUT_V_REL +#define TIMEOUT_VENDOR "william@25thandClement.com" + +#define TIMEOUT_V_REL 0x20160226 +#define TIMEOUT_V_ABI 0x20160224 +#define TIMEOUT_V_API 0x20160226 + +TIMEOUT_PUBLIC int timeout_version(void); + +TIMEOUT_PUBLIC const char *timeout_vendor(void); + +TIMEOUT_PUBLIC int timeout_v_rel(void); + +TIMEOUT_PUBLIC int timeout_v_abi(void); + +TIMEOUT_PUBLIC int timeout_v_api(void); + + +/* + * I N T E G E R T Y P E I N T E R F A C E S + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#define TIMEOUT_C(n) UINT64_C(n) +#define TIMEOUT_PRIu PRIu64 +#define TIMEOUT_PRIx PRIx64 +#define TIMEOUT_PRIX PRIX64 + +#define TIMEOUT_mHZ TIMEOUT_C(1000) +#define TIMEOUT_uHZ TIMEOUT_C(1000000) +#define TIMEOUT_nHZ TIMEOUT_C(1000000000) + +typedef uint64_t timeout_t; + +#define timeout_error_t int /* for documentation purposes */ + + +/* + * C A L L B A C K I N T E R F A C E + * + * Callback function parameters unspecified to make embedding into existing + * applications easier. + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef TIMEOUT_CB_OVERRIDE +struct timeout_cb { + void (*fn)(); + void *arg; +}; /* struct timeout_cb */ +#endif + +/* + * T I M E O U T I N T E R F A C E S + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef TIMEOUT_DISABLE_INTERVALS +#define TIMEOUT_INT 0x01 /* interval (repeating) timeout */ +#endif +#define TIMEOUT_ABS 0x02 /* treat timeout values as absolute */ + +#define TIMEOUT_INITIALIZER(flags) { (flags) } + +#define timeout_setcb(to, fn, arg) do { \ + (to)->callback.fn = (fn); \ + (to)->callback.arg = (arg); \ +} while (0) + +struct timeout { + int flags; + + timeout_t expires; + /* absolute expiration time */ + + struct timeout_list *pending; + /* timeout list if pending on wheel or expiry queue */ + + TAILQ_ENTRY(timeout) tqe; + /* entry member for struct timeout_list lists */ + +#ifndef TIMEOUT_DISABLE_CALLBACKS + struct timeout_cb callback; + /* optional callback information */ +#endif + +#ifndef TIMEOUT_DISABLE_INTERVALS + timeout_t interval; + /* timeout interval if periodic */ +#endif + +#ifndef TIMEOUT_DISABLE_RELATIVE_ACCESS + struct timeouts *timeouts; + /* timeouts collection if member of */ +#endif +}; /* struct timeout */ + + +TIMEOUT_PUBLIC struct timeout *timeout_init(struct timeout *, int); +/* initialize timeout structure (same as TIMEOUT_INITIALIZER) */ + +#ifndef TIMEOUT_DISABLE_RELATIVE_ACCESS +TIMEOUT_PUBLIC bool timeout_pending(struct timeout *); +/* true if on timing wheel, false otherwise */ + +TIMEOUT_PUBLIC bool timeout_expired(struct timeout *); +/* true if on expired queue, false otherwise */ + +TIMEOUT_PUBLIC void timeout_del(struct timeout *); +/* remove timeout from any timing wheel (okay if not member of any) */ +#endif + +/* + * T I M I N G W H E E L I N T E R F A C E S + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +struct timeouts; + +TIMEOUT_PUBLIC struct timeouts *timeouts_open(timeout_t, timeout_error_t *); +/* open a new timing wheel, setting optional HZ (for float conversions) */ + +TIMEOUT_PUBLIC void timeouts_close(struct timeouts *); +/* destroy timing wheel */ + +TIMEOUT_PUBLIC timeout_t timeouts_hz(struct timeouts *); +/* return HZ setting (for float conversions) */ + +TIMEOUT_PUBLIC void timeouts_update(struct timeouts *, timeout_t); +/* update timing wheel with current absolute time */ + +TIMEOUT_PUBLIC void timeouts_step(struct timeouts *, timeout_t); +/* step timing wheel by relative time */ + +TIMEOUT_PUBLIC timeout_t timeouts_timeout(struct timeouts *); +/* return interval to next required update */ + +TIMEOUT_PUBLIC void timeouts_add(struct timeouts *, struct timeout *, timeout_t); +/* add timeout to timing wheel */ + +TIMEOUT_PUBLIC void timeouts_del(struct timeouts *, struct timeout *); +/* remove timeout from any timing wheel or expired queue (okay if on neither) */ + +TIMEOUT_PUBLIC struct timeout *timeouts_get(struct timeouts *); +/* return any expired timeout (caller should loop until NULL-return) */ + +TIMEOUT_PUBLIC bool timeouts_pending(struct timeouts *); +/* return true if any timeouts pending on timing wheel */ + +TIMEOUT_PUBLIC bool timeouts_expired(struct timeouts *); +/* return true if any timeouts on expired queue */ + +TIMEOUT_PUBLIC bool timeouts_check(struct timeouts *, FILE *); +/* return true if invariants hold. describes failures to optional file handle. */ + +#define TIMEOUTS_PENDING 0x10 +#define TIMEOUTS_EXPIRED 0x20 +#define TIMEOUTS_ALL (TIMEOUTS_PENDING|TIMEOUTS_EXPIRED) +#define TIMEOUTS_CLEAR 0x40 + +#define TIMEOUTS_IT_INITIALIZER(flags) { (flags), 0, 0, 0, 0 } + +#define TIMEOUTS_IT_INIT(cur, _flags) do { \ + (cur)->flags = (_flags); \ + (cur)->pc = 0; \ +} while (0) + +struct timeouts_it { + int flags; + unsigned pc, i, j; + struct timeout *to; +}; /* struct timeouts_it */ + +TIMEOUT_PUBLIC struct timeout *timeouts_next(struct timeouts *, struct timeouts_it *); +/* return next timeout in pending wheel or expired queue. caller can delete + * the returned timeout, but should not otherwise manipulate the timing + * wheel. in particular, caller SHOULD NOT delete any other timeout as that + * could invalidate cursor state and trigger a use-after-free. + */ + +#define TIMEOUTS_FOREACH(var, T, flags) \ + struct timeouts_it _it = TIMEOUTS_IT_INITIALIZER((flags)); \ + while (((var) = timeouts_next((T), &_it))) + + +/* + * B O N U S W H E E L I N T E R F A C E S + * + * I usually use floating point timeouts in all my code, but it's cleaner to + * separate it to keep the core algorithmic code simple. + * + * Using macros instead of static inline routines where routines + * might be used to keep -lm linking optional. + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include /* ceil(3) */ + +#define timeouts_f2i(T, f) \ + ((timeout_t)ceil((f) * timeouts_hz((T)))) /* prefer late expiration over early */ + +#define timeouts_i2f(T, i) \ + ((double)(i) / timeouts_hz((T))) + +#define timeouts_addf(T, to, timeout) \ + timeouts_add((T), (to), timeouts_f2i((T), (timeout))) + +#endif /* TIMEOUT_H */ From 209d7ea7cd7282ee1a3a962b0b8c28ae2708cc3f Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Fri, 5 Aug 2022 10:24:10 +0800 Subject: [PATCH 13/17] Update uthash v2.3.0 open source code to deps directory --- deps/uthash/utarray.h | 9 ++--- deps/uthash/uthash.h | 68 ++++++++++++++++---------------------- deps/uthash/utlist.h | 4 +-- deps/uthash/utringbuffer.h | 4 +-- deps/uthash/utstack.h | 8 ++--- deps/uthash/utstring.h | 4 +-- 6 files changed, 43 insertions(+), 54 deletions(-) diff --git a/deps/uthash/utarray.h b/deps/uthash/utarray.h index 6b62018..dc1cf8e 100644 --- a/deps/uthash/utarray.h +++ b/deps/uthash/utarray.h @@ -1,5 +1,5 @@ /* -Copyright (c) 2008-2018, Troy D. Hanson http://troydhanson.github.com/uthash/ +Copyright (c) 2008-2022, Troy D. Hanson https://troydhanson.github.io/uthash/ All rights reserved. Redistribution and use in source and binary forms, with or without @@ -26,7 +26,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef UTARRAY_H #define UTARRAY_H -#define UTARRAY_VERSION 2.1.0 +#define UTARRAY_VERSION 2.3.0 #include /* size_t */ #include /* memset, etc */ @@ -232,8 +232,9 @@ typedef struct { /* last we pre-define a few icd for common utarrays of ints and strings */ static void utarray_str_cpy(void *dst, const void *src) { - char **_src = (char**)src, **_dst = (char**)dst; - *_dst = (*_src == NULL) ? NULL : strdup(*_src); + char *const *srcc = (char *const *)src; + char **dstc = (char**)dst; + *dstc = (*srcc == NULL) ? NULL : strdup(*srcc); } static void utarray_str_dtor(void *elt) { char **eltc = (char**)elt; diff --git a/deps/uthash/uthash.h b/deps/uthash/uthash.h index 5e5866a..49c69df 100644 --- a/deps/uthash/uthash.h +++ b/deps/uthash/uthash.h @@ -1,5 +1,5 @@ /* -Copyright (c) 2003-2018, Troy D. Hanson http://troydhanson.github.com/uthash/ +Copyright (c) 2003-2022, Troy D. Hanson https://troydhanson.github.io/uthash/ All rights reserved. Redistribution and use in source and binary forms, with or without @@ -24,12 +24,22 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef UTHASH_H #define UTHASH_H -#define UTHASH_VERSION 2.1.0 +#define UTHASH_VERSION 2.3.0 #include /* memcmp, memset, strlen */ #include /* ptrdiff_t */ #include /* exit */ +#if defined(HASH_DEFINE_OWN_STDINT) && HASH_DEFINE_OWN_STDINT +/* This codepath is provided for backward compatibility, but I plan to remove it. */ +#warning "HASH_DEFINE_OWN_STDINT is deprecated; please use HASH_NO_STDINT instead" +typedef unsigned int uint32_t; +typedef unsigned char uint8_t; +#elif defined(HASH_NO_STDINT) && HASH_NO_STDINT +#else +#include /* uint8_t, uint32_t */ +#endif + /* These macros use decltype or the earlier __typeof GNU extension. As decltype is only available in newer compilers (VS2010 or gcc 4.3+ when compiling c++ source) this code uses whatever method is needed @@ -62,23 +72,6 @@ do { } while (0) #endif -/* a number of the hash function use uint32_t which isn't defined on Pre VS2010 */ -#if defined(_WIN32) -#if defined(_MSC_VER) && _MSC_VER >= 1600 -#include -#elif defined(__WATCOMC__) || defined(__MINGW32__) || defined(__CYGWIN__) -#include -#else -typedef unsigned int uint32_t; -typedef unsigned char uint8_t; -#endif -#elif defined(__GNUC__) && !defined(__VXWORKS__) -#include -#else -typedef unsigned int uint32_t; -typedef unsigned char uint8_t; -#endif - #ifndef uthash_malloc #define uthash_malloc(sz) malloc(sz) /* malloc fcn */ #endif @@ -92,15 +85,12 @@ typedef unsigned char uint8_t; #define uthash_strlen(s) strlen(s) #endif -#ifdef uthash_memcmp -/* This warning will not catch programs that define uthash_memcmp AFTER including uthash.h. */ -#warning "uthash_memcmp is deprecated; please use HASH_KEYCMP instead" -#else -#define uthash_memcmp(a,b,n) memcmp(a,b,n) +#ifndef HASH_FUNCTION +#define HASH_FUNCTION(keyptr,keylen,hashv) HASH_JEN(keyptr, keylen, hashv) #endif #ifndef HASH_KEYCMP -#define HASH_KEYCMP(a,b,n) uthash_memcmp(a,b,n) +#define HASH_KEYCMP(a,b,n) memcmp(a,b,n) #endif #ifndef uthash_noexpand_fyi @@ -158,7 +148,7 @@ do { #define HASH_VALUE(keyptr,keylen,hashv) \ do { \ - HASH_FCN(keyptr, keylen, hashv); \ + HASH_FUNCTION(keyptr, keylen, hashv); \ } while (0) #define HASH_FIND_BYHASHVALUE(hh,head,keyptr,keylen,hashval,out) \ @@ -408,7 +398,7 @@ do { do { \ IF_HASH_NONFATAL_OOM( int _ha_oomed = 0; ) \ (add)->hh.hashv = (hashval); \ - (add)->hh.key = (char*) (keyptr); \ + (add)->hh.key = (const void*) (keyptr); \ (add)->hh.keylen = (unsigned) (keylen_in); \ if (!(head)) { \ (add)->hh.next = NULL; \ @@ -590,13 +580,6 @@ do { #define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) #endif -/* default to Jenkin's hash unless overridden e.g. DHASH_FUNCTION=HASH_SAX */ -#ifdef HASH_FUNCTION -#define HASH_FCN HASH_FUNCTION -#else -#define HASH_FCN HASH_JEN -#endif - /* The Bernstein hash function, used in Perl prior to v5.6. Note (x<<5+x)=x*33. */ #define HASH_BER(key,keylen,hashv) \ do { \ @@ -610,7 +593,9 @@ do { /* SAX/FNV/OAT/JEN hash functions are macro variants of those listed at - * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx */ + * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx + * (archive link: https://archive.is/Ivcan ) + */ #define HASH_SAX(key,keylen,hashv) \ do { \ unsigned _sx_i; \ @@ -695,7 +680,8 @@ do { case 4: _hj_i += ( (unsigned)_hj_key[3] << 24 ); /* FALLTHROUGH */ \ case 3: _hj_i += ( (unsigned)_hj_key[2] << 16 ); /* FALLTHROUGH */ \ case 2: _hj_i += ( (unsigned)_hj_key[1] << 8 ); /* FALLTHROUGH */ \ - case 1: _hj_i += _hj_key[0]; \ + case 1: _hj_i += _hj_key[0]; /* FALLTHROUGH */ \ + default: ; \ } \ HASH_JEN_MIX(_hj_i, _hj_j, hashv); \ } while (0) @@ -743,6 +729,8 @@ do { case 1: hashv += *_sfh_key; \ hashv ^= hashv << 10; \ hashv += hashv >> 1; \ + break; \ + default: ; \ } \ \ /* Force "avalanching" of final 127 bits */ \ @@ -764,7 +752,7 @@ do { } \ while ((out) != NULL) { \ if ((out)->hh.hashv == (hashval) && (out)->hh.keylen == (keylen_in)) { \ - if (HASH_KEYCMP((out)->hh.key, keyptr, keylen_in) == 0) { \ + if (HASH_KEYCMP((out)->hh.key, keyptr, keylen_in) == 0) { \ break; \ } \ } \ @@ -850,12 +838,12 @@ do { struct UT_hash_handle *_he_thh, *_he_hh_nxt; \ UT_hash_bucket *_he_new_buckets, *_he_newbkt; \ _he_new_buckets = (UT_hash_bucket*)uthash_malloc( \ - 2UL * (tbl)->num_buckets * sizeof(struct UT_hash_bucket)); \ + sizeof(struct UT_hash_bucket) * (tbl)->num_buckets * 2U); \ if (!_he_new_buckets) { \ HASH_RECORD_OOM(oomed); \ } else { \ uthash_bzero(_he_new_buckets, \ - 2UL * (tbl)->num_buckets * sizeof(struct UT_hash_bucket)); \ + sizeof(struct UT_hash_bucket) * (tbl)->num_buckets * 2U); \ (tbl)->ideal_chain_maxlen = \ ((tbl)->num_items >> ((tbl)->log2_num_buckets+1U)) + \ ((((tbl)->num_items & (((tbl)->num_buckets*2U)-1U)) != 0U) ? 1U : 0U); \ @@ -1142,7 +1130,7 @@ typedef struct UT_hash_handle { void *next; /* next element in app order */ struct UT_hash_handle *hh_prev; /* previous hh in bucket order */ struct UT_hash_handle *hh_next; /* next hh in bucket order */ - void *key; /* ptr to enclosing struct's key */ + const void *key; /* ptr to enclosing struct's key */ unsigned keylen; /* enclosing struct's key len */ unsigned hashv; /* result of hash-fcn(key) */ } UT_hash_handle; diff --git a/deps/uthash/utlist.h b/deps/uthash/utlist.h index 5bb1ac9..492908c 100644 --- a/deps/uthash/utlist.h +++ b/deps/uthash/utlist.h @@ -1,5 +1,5 @@ /* -Copyright (c) 2007-2018, Troy D. Hanson http://troydhanson.github.com/uthash/ +Copyright (c) 2007-2022, Troy D. Hanson https://troydhanson.github.io/uthash/ All rights reserved. Redistribution and use in source and binary forms, with or without @@ -24,7 +24,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef UTLIST_H #define UTLIST_H -#define UTLIST_VERSION 2.1.0 +#define UTLIST_VERSION 2.3.0 #include diff --git a/deps/uthash/utringbuffer.h b/deps/uthash/utringbuffer.h index ce2890e..6034117 100644 --- a/deps/uthash/utringbuffer.h +++ b/deps/uthash/utringbuffer.h @@ -1,5 +1,5 @@ /* -Copyright (c) 2015-2018, Troy D. Hanson http://troydhanson.github.com/uthash/ +Copyright (c) 2015-2022, Troy D. Hanson https://troydhanson.github.io/uthash/ All rights reserved. Redistribution and use in source and binary forms, with or without @@ -26,7 +26,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef UTRINGBUFFER_H #define UTRINGBUFFER_H -#define UTRINGBUFFER_VERSION 2.1.0 +#define UTRINGBUFFER_VERSION 2.3.0 #include #include diff --git a/deps/uthash/utstack.h b/deps/uthash/utstack.h index 3b0c1a0..94b8c51 100644 --- a/deps/uthash/utstack.h +++ b/deps/uthash/utstack.h @@ -1,5 +1,5 @@ /* -Copyright (c) 2018-2018, Troy D. Hanson http://troydhanson.github.com/uthash/ +Copyright (c) 2018-2022, Troy D. Hanson https://troydhanson.github.io/uthash/ All rights reserved. Redistribution and use in source and binary forms, with or without @@ -24,7 +24,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef UTSTACK_H #define UTSTACK_H -#define UTSTACK_VERSION 2.1.0 +#define UTSTACK_VERSION 2.3.0 /* * This file contains macros to manipulate a singly-linked list as a stack. @@ -35,9 +35,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * struct item { * int id; * struct item *next; - * } + * }; * - * struct item *stack = NULL: + * struct item *stack = NULL; * * int main() { * int count; diff --git a/deps/uthash/utstring.h b/deps/uthash/utstring.h index 4cf5ffd..f0270fb 100644 --- a/deps/uthash/utstring.h +++ b/deps/uthash/utstring.h @@ -1,5 +1,5 @@ /* -Copyright (c) 2008-2018, Troy D. Hanson http://troydhanson.github.com/uthash/ +Copyright (c) 2008-2022, Troy D. Hanson https://troydhanson.github.io/uthash/ All rights reserved. Redistribution and use in source and binary forms, with or without @@ -26,7 +26,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef UTSTRING_H #define UTSTRING_H -#define UTSTRING_VERSION 2.1.0 +#define UTSTRING_VERSION 2.3.0 #include #include From 8c85079603b664a98fb8c65e92ee1c90c780c614 Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Fri, 5 Aug 2022 10:45:28 +0800 Subject: [PATCH 14/17] Add CI autotest and Fix compile warning --- .gitlab-ci.yml | 253 ++++++++++++++++++ CMakeLists.txt | 42 +-- cmake/autorevision.sh => autorevision.sh | 178 ++++++------ ci/envelope_rpm.sh | 12 + ci/get-nprocessors.sh | 48 ++++ ci/perpare_pulp3_netrc.sh | 3 + ci/travis.sh | 66 +++++ ci/upload_enveloped_rpm.sh | 21 ++ cmake/FindSYSTEMD.cmake | 39 +++ cmake/Package.cmake | 35 +++ cmake/Version.cmake | 63 ++--- conf/CMakeLists.txt | 1 + conf/stellar/stellar.conf | 0 sdk/example/custom_event_plugin.cpp | 4 +- sdk/example/http_event_plugin.cpp | 2 +- src/packet_io/test/CMakeLists.txt | 2 +- src/plugin_manager/plugin_manager.cpp | 4 +- src/plugin_manager/plugin_manager_module.cpp | 4 +- src/plugin_manager/test/CMakeLists.txt | 2 +- .../test/gtest_plugin_manager.cpp | 10 +- .../plugins_library/custom_event_plugin.cpp | 2 +- src/protocol_decoder/http/http.cpp | 4 +- test/CMakeLists.txt | 2 +- vendor/CMakeLists.txt | 31 ++- 24 files changed, 654 insertions(+), 174 deletions(-) create mode 100644 .gitlab-ci.yml rename cmake/autorevision.sh => autorevision.sh (93%) create mode 100644 ci/envelope_rpm.sh create mode 100644 ci/get-nprocessors.sh create mode 100644 ci/perpare_pulp3_netrc.sh create mode 100644 ci/travis.sh create mode 100644 ci/upload_enveloped_rpm.sh create mode 100644 cmake/FindSYSTEMD.cmake create mode 100644 cmake/Package.cmake create mode 100644 conf/CMakeLists.txt create mode 100644 conf/stellar/stellar.conf diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..0e52438 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,253 @@ +variables: + GIT_STRATEGY: "clone" + BUILD_PADDING_PREFIX: /tmp/padding_for_CPACK_RPM_BUILD_SOURCE_DIRS_PREFIX_PREFIX_PREFIX_PREFIX_PREFIX_PREFIX/ + INSTALL_PREFIX: "/opt/tsg/stellar" + TESTING_VERSION_BUILD: 0 + BUILD_IMAGE_CENTOS7: "git.mesalab.cn:7443/mesa_platform/build-env:master" + BUILD_IMAGE_CENTOS8: "git.mesalab.cn:7443/mesa_platform/build-env:rockylinux" + +stages: + - build + - envelope + - upload + +.build_before_script: + before_script: + - mkdir -p $BUILD_PADDING_PREFIX/$CI_PROJECT_NAMESPACE/ + - ln -s $CI_PROJECT_DIR $BUILD_PADDING_PREFIX/$CI_PROJECT_PATH + - cd $BUILD_PADDING_PREFIX/$CI_PROJECT_PATH + - chmod +x ./ci/travis.sh + - yum makecache + - yum install -y elfutils-libelf-devel + +.build_by_travis_for_centos7: + stage: build + image: $BUILD_IMAGE_CENTOS7 + extends: .build_before_script + script: + - ./ci/travis.sh + tags: + - share + +.build_by_travis_for_centos8: + stage: build + image: $BUILD_IMAGE_CENTOS8 + extends: .build_before_script + script: + - ./ci/travis.sh + tags: + - share + +############################################################################### +# compile use image: build-env:master +############################################################################### + +branch_build_debug_for_centos7: + extends: .build_by_travis_for_centos7 + variables: + BUILD_TYPE: Debug + except: + - /^develop-.*$/i + - /^release-.*$/i + - tags + +branch_build_release_for_centos7: + variables: + BUILD_TYPE: RelWithDebInfo + extends: .build_by_travis_for_centos7 + except: + - /^develop-.*$/i + - /^release-.*$/i + - tags + +develop_build_debug_for_centos7: + extends: .build_by_travis_for_centos7 + variables: + TESTING_VERSION_BUILD: 1 + UPLOAD_SYMBOL_FILES: 1 + BUILD_TYPE: Debug + # ASAN_OPTION: ADDRESS + PACKAGE: 1 + PULP3_REPO_NAME: stellar-testing-x86_64.el7 + PULP3_DIST_NAME: stellar-testing-x86_64.el7 + artifacts: + name: "stellar-develop-$CI_COMMIT_REF_NAME-debug" + paths: + - build/*.rpm + only: + - /^develop-.*$/i + - /^release-.*$/i + +develop_build_release_for_centos7: + extends: .build_by_travis_for_centos7 + variables: + TESTING_VERSION_BUILD: 1 + UPLOAD_SYMBOL_FILES: 1 + # ASAN_OPTION: ADDRESS + BUILD_TYPE: RelWithDebInfo + PACKAGE: 1 + PULP3_REPO_NAME: stellar-testing-x86_64.el7 + PULP3_DIST_NAME: stellar-testing-x86_64.el7 + artifacts: + name: "stellar-develop-$CI_COMMIT_REF_NAME-release" + paths: + - build/*.rpm + only: + - /^develop-.*$/i + - /^release-.*$/i + +release_build_debug_for_centos7: + variables: + UPLOAD_SYMBOL_FILES: 1 + BUILD_TYPE: Debug + PACKAGE: 1 + PULP3_REPO_NAME: stellar-stable-x86_64.el7 + PULP3_DIST_NAME: stellar-stable-x86_64.el7 + extends: .build_by_travis_for_centos7 + artifacts: + name: "stellar-install-$CI_COMMIT_REF_NAME-debug" + paths: + - build/*.rpm + only: + - tags + +release_build_release_for_centos7: + variables: + BUILD_TYPE: RelWithDebInfo + UPLOAD_SYMBOL_FILES: 1 + PACKAGE: 1 + PULP3_REPO_NAME: stellar-stable-x86_64.el7 + PULP3_DIST_NAME: stellar-stable-x86_64.el7 + extends: .build_by_travis_for_centos7 + artifacts: + name: "stellar-install-$CI_COMMIT_REF_NAME-release" + paths: + - build/*.rpm + only: + - tags + +############################################################################### +# compile use image: build-env:rockylinux +############################################################################### + +branch_build_debug_for_centos8: + extends: .build_by_travis_for_centos8 + variables: + BUILD_TYPE: Debug + except: + - /^develop-.*$/i + - /^release-.*$/i + - tags + +branch_build_release_for_centos8: + variables: + BUILD_TYPE: RelWithDebInfo + extends: .build_by_travis_for_centos8 + except: + - /^develop-.*$/i + - /^release-.*$/i + - tags + +develop_build_debug_for_centos8: + extends: .build_by_travis_for_centos8 + variables: + TESTING_VERSION_BUILD: 1 + UPLOAD_SYMBOL_FILES: 1 + BUILD_TYPE: Debug + # ASAN_OPTION: ADDRESS + PACKAGE: 1 + PULP3_REPO_NAME: stellar-testing-x86_64.el8 + PULP3_DIST_NAME: stellar-testing-x86_64.el8 + artifacts: + name: "stellar-develop-$CI_COMMIT_REF_NAME-debug" + paths: + - build/*.rpm + only: + - /^develop-.*$/i + - /^release-.*$/i + +develop_build_release_for_centos8: + extends: .build_by_travis_for_centos8 + variables: + TESTING_VERSION_BUILD: 1 + UPLOAD_SYMBOL_FILES: 1 + # ASAN_OPTION: ADDRESS + BUILD_TYPE: RelWithDebInfo + PACKAGE: 1 + PULP3_REPO_NAME: stellar-testing-x86_64.el8 + PULP3_DIST_NAME: stellar-testing-x86_64.el8 + artifacts: + name: "stellar-develop-$CI_COMMIT_REF_NAME-release" + paths: + - build/*.rpm + only: + - /^develop-.*$/i + - /^release-.*$/i + +release_build_debug_for_centos8: + variables: + UPLOAD_SYMBOL_FILES: 1 + BUILD_TYPE: Debug + PACKAGE: 1 + PULP3_REPO_NAME: stellar-stable-x86_64.el8 + PULP3_DIST_NAME: stellar-stable-x86_64.el8 + extends: .build_by_travis_for_centos8 + artifacts: + name: "stellar-install-$CI_COMMIT_REF_NAME-debug" + paths: + - build/*.rpm + only: + - tags + +release_build_release_for_centos8: + variables: + BUILD_TYPE: RelWithDebInfo + UPLOAD_SYMBOL_FILES: 1 + PACKAGE: 1 + PULP3_REPO_NAME: stellar-stable-x86_64.el8 + PULP3_DIST_NAME: stellar-stable-x86_64.el8 + extends: .build_by_travis_for_centos8 + artifacts: + name: "stellar-install-$CI_COMMIT_REF_NAME-release" + paths: + - build/*.rpm + only: + - tags + +############################################################################### +# envelope and upload +############################################################################### + +envelope_rpm: + stage: envelope + image: $BUILD_IMAGE_CENTOS7 + variables: + FEATURE_ID: 100 + APP_NAME_IN_RPM_SPEC: stellar + script: + - chmod +x ./ci/envelope_rpm.sh + - ./ci/envelope_rpm.sh + artifacts: + name: "stellar-pr-$CI_COMMIT_REF_NAME-release" + paths: + - build/*-pr-*.rpm + tags: + - envelope + only: + - tags + +upload_enveloped_rpm: + stage: upload + image: $BUILD_IMAGE_CENTOS7 + variables: + PULP3_REPO_NAME_EL7: stellar-stable-x86_64.el7 + PULP3_DIST_NAME_EL7: stellar-stable-x86_64.el7 + PULP3_REPO_NAME_EL8: stellar-stable-x86_64.el8 + PULP3_DIST_NAME_EL8: stellar-stable-x86_64.el8 + script: + - chmod +x ./ci/upload_enveloped_rpm.sh + - ./ci/upload_enveloped_rpm.sh + tags: + - share + only: + - tags \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index d1f406e..9bb7320 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,21 +1,35 @@ -cmake_minimum_required(VERSION 3.14) +cmake_minimum_required(VERSION 3.12) project(stellar) -# GoogleTest requires at least C++11 -set(CMAKE_CXX_STANDARD 11) - -#find required package set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) include(Version) -find_package(PkgConfig REQUIRED) -find_package(Threads REQUIRED) -find_package(PCAP REQUIRED) -pkg_check_modules(SYSTEMD REQUIRED systemd) +include(Package) +add_definitions(-D_GNU_SOURCE) +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_C_STANDARD 11) + +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE RelWithDebInfo) +endif() + +if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) + set (CMAKE_INSTALL_PREFIX "/opt/tsg/stellar" CACHE PATH "default install path" FORCE) +endif() + +option(ENABLE_PIC "Generate position independent code (necessary for shared libraries)" TRUE) +option(ENABLE_WARNING_ALL "Enable all optional warnings which are desirable for normal code" TRUE) + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lelf") + +add_custom_target("install-program" COMMAND ${CMAKE_COMMAND} ARGS -DCOMPONENT=Program -P cmake_install.cmake) +add_custom_target("install-profile" COMMAND ${CMAKE_COMMAND} ARGS -DCOMPONENT=Profile -P cmake_install.cmake) include_directories(${CMAKE_SOURCE_DIR}/sdk/include) -#add vendor and source directory +add_subdirectory(conf) add_subdirectory(vendor) add_subdirectory(deps/toml) add_subdirectory(deps/sds) @@ -23,11 +37,5 @@ add_subdirectory(deps/timeout) add_subdirectory(src) add_subdirectory(sdk/example) - -#add gtest enable_testing() -add_subdirectory(test) - - - - +add_subdirectory(test) \ No newline at end of file diff --git a/cmake/autorevision.sh b/autorevision.sh similarity index 93% rename from cmake/autorevision.sh rename to autorevision.sh index 3baa179..80d0712 100644 --- a/cmake/autorevision.sh +++ b/autorevision.sh @@ -9,7 +9,7 @@ # Usage message. arUsage() { - cat > "/dev/stderr" << EOF + cat >"/dev/stderr" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" < "${TARGETFILE}" << EOF + cat >"${TARGETFILE}" </dev/null)" ]; then local gitPath="$(git rev-parse --show-toplevel)" local gitDepth="$(pathSegment "${gitPath}")" - REPONUM="$((REPONUM+1))" + REPONUM="$((REPONUM + 1))" else local gitDepth="0" fi if [ ! -z "$(hg root 2>/dev/null)" ]; then local hgPath="$(hg root 2>/dev/null)" local hgDepth="$(pathSegment "${hgPath}")" - REPONUM="$((REPONUM+1))" + REPONUM="$((REPONUM + 1))" else local hgDepth="0" fi if [ ! -z "$(bzr root 2>/dev/null)" ]; then local bzrPath="$(bzr root 2>/dev/null)" local bzrDepth="$(pathSegment "${bzrPath}")" - REPONUM="$((REPONUM+1))" + REPONUM="$((REPONUM + 1))" else local bzrDepth="0" fi @@ -1114,12 +1111,12 @@ repoTest() { local svnPath="$(svn info --xml | sed -n -e "s:${stringz}::" -e "s:${stringx}::p")" # An old enough svn will not be able give us a path; default # to 1 for that case. - if [ -z "${svnPath}" ]; then + if [ -z "${svnPath}" ]; then local svnDepth="1" else local svnDepth="$(pathSegment "${svnPath}")" fi - REPONUM="$((REPONUM+1))" + REPONUM="$((REPONUM + 1))" else local svnDepth="0" fi @@ -1142,8 +1139,6 @@ repoTest() { fi } - - # Detect which repos we are in and gather data. # shellcheck source=/dev/null if [ -f "${CACHEFILE}" ] && [ "${CACHEFORCE}" = "1" ]; then @@ -1165,7 +1160,6 @@ else fi fi - # -s output is handled here. if [ ! -z "${VAROUT}" ]; then if [ "${VAROUT}" = "VCS_TYPE" ]; then @@ -1196,7 +1190,6 @@ if [ ! -z "${VAROUT}" ]; then fi fi - # Detect requested output type and use it. if [ ! -z "${AFILETYPE}" ]; then if [ "${AFILETYPE}" = "c" ]; then @@ -1251,7 +1244,6 @@ if [ ! -z "${AFILETYPE}" ]; then fi fi - # If requested, make a cache file. if [ ! -z "${CACHEFILE}" ] && [ ! "${CACHEFORCE}" = "1" ]; then TARGETFILE="${CACHEFILE}.tmp" diff --git a/ci/envelope_rpm.sh b/ci/envelope_rpm.sh new file mode 100644 index 0000000..4aa2978 --- /dev/null +++ b/ci/envelope_rpm.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env sh + +ls $CI_PROJECT_DIR/build/*.rpm + +BIN_TO_PROTECT_IN_RPM="${INSTALL_PREFIX}/bin/stellar" +RPMS_FULL_PATH=$(ls $CI_PROJECT_DIR/build/*.rpm | grep -v debug | grep -v devel) +echo "RPMS_TO_BE_ENVELOPE: " $RPMS_FULL_PATH + +for RPM in ${RPMS_FULL_PATH[@]}; do + echo "ENVELOPE: " $RPM + /root/rebuildrpm_and_envelope.sh $RPM $BIN_TO_PROTECT_IN_RPM $FEATURE_ID $APP_NAME_IN_RPM_SPEC +done diff --git a/ci/get-nprocessors.sh b/ci/get-nprocessors.sh new file mode 100644 index 0000000..8a754cf --- /dev/null +++ b/ci/get-nprocessors.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +# Copyright 2017 Google Inc. +# All Rights Reserved. +# +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following disclaimer +# in the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Google Inc. nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +# This file is typically sourced by another script. +# if possible, ask for the precise number of processors, +# otherwise take 2 processors as reasonable default; see +# https://docs.travis-ci.com/user/speeding-up-the-build/#Makefile-optimization +if [ -x /usr/bin/getconf ]; then + NPROCESSORS=$(/usr/bin/getconf _NPROCESSORS_ONLN) +else + NPROCESSORS=2 +fi + +# as of 2017-09-04 Travis CI reports 32 processors, but GCC build +# crashes if parallelized too much (maybe memory consumption problem), +# so limit to 4 processors for the time being. +if [ $NPROCESSORS -gt 4 ]; then + echo "$0:Note: Limiting processors to use by make from $NPROCESSORS to 4." + NPROCESSORS=4 +fi diff --git a/ci/perpare_pulp3_netrc.sh b/ci/perpare_pulp3_netrc.sh new file mode 100644 index 0000000..0aca8ec --- /dev/null +++ b/ci/perpare_pulp3_netrc.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env sh +set -evx +echo "machine ${PULP3_SERVER_URL}\nlogin ${PULP3_SERVER_LOGIN}\npassword ${PULP3_SERVER_PASSWORD}\n" >~/.netrc diff --git a/ci/travis.sh b/ci/travis.sh new file mode 100644 index 0000000..6f30ffd --- /dev/null +++ b/ci/travis.sh @@ -0,0 +1,66 @@ +#!/usr/bin/env sh +set -evx + +chmod +x ci/get-nprocessors.sh +. ci/get-nprocessors.sh + +# if possible, ask for the precise number of processors, +# otherwise take 2 processors as reasonable default; see +# https://docs.travis-ci.com/user/speeding-up-the-build/#Makefile-optimization +if [ -x /usr/bin/getconf ]; then + NPROCESSORS=$(/usr/bin/getconf _NPROCESSORS_ONLN) +else + NPROCESSORS=2 +fi + +# as of 2017-09-04 Travis CI reports 32 processors, but GCC build +# crashes if parallelized too much (maybe memory consumption problem), +# so limit to 4 processors for the time being. +if [ $NPROCESSORS -gt 4 ]; then + echo "$0:Note: Limiting processors to use by make from $NPROCESSORS to 4." + NPROCESSORS=4 +fi + +# Tell make to use the processors. No preceding '-' required. +MAKEFLAGS="j${NPROCESSORS}" +export MAKEFLAGS + +env | sort + +# Set default values to OFF for these variables if not specified. +: "${NO_EXCEPTION:=OFF}" +: "${NO_RTTI:=OFF}" +: "${COMPILER_IS_GNUCXX:=OFF}" + +# Install dependency from YUM +yum install -y libpcap-devel + +if [ $ASAN_OPTION ]; then + source /opt/rh/devtoolset-7/enable +fi + +mkdir build || true +cd build + +cmake3 -DCMAKE_CXX_FLAGS=$CXX_FLAGS \ + -DCMAKE_BUILD_TYPE=$BUILD_TYPE \ + -DASAN_OPTION=$ASAN_OPTION \ + -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX \ + -DTFE_VERSION_DAILY_BUILD=$TESTING_VERSION_BUILD \ + .. + +make +make test + +if [ -n "${PACKAGE}" ]; then + make package + cp ~/rpm_upload_tools.py ./ + python3 rpm_upload_tools.py ${PULP3_REPO_NAME} ${PULP3_DIST_NAME} *.rpm +fi + +if [ -n "${UPLOAD_SYMBOL_FILES}" ]; then + rpm -i stellar*debuginfo*.rpm + ls -ahl /usr/lib/debug/opt/tsg/stellar/bin/ + cp /usr/lib/debug/opt/tsg/stellar/bin/stellar*debug /tmp/stellar.debuginfo.${CI_COMMIT_SHORT_SHA} + sentry-cli upload-dif -t elf /tmp/stellar.debuginfo.${CI_COMMIT_SHORT_SHA} +fi diff --git a/ci/upload_enveloped_rpm.sh b/ci/upload_enveloped_rpm.sh new file mode 100644 index 0000000..0faae1a --- /dev/null +++ b/ci/upload_enveloped_rpm.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env sh + +cd $CI_PROJECT_DIR/build/ +cp ~/rpm_upload_tools.py ./ + +ls -ahl *.rpm + +RPM_IS_EL7=$(ls -ahl *-pr-*.rpm | grep el7 | wc -l) +RPM_IS_EL8=$(ls -ahl *-pr-*.rpm | grep el8 | wc -l) + +if [ $RPM_IS_EL7 -eq 1 ]; then + echo "====== Upload the packed RPM package for CentOS7 ======" + python3 rpm_upload_tools.py ${PULP3_REPO_NAME_EL7} ${PULP3_DIST_NAME_EL7} *-pr-*el7*.rpm +fi + +if [ $RPM_IS_EL8 -eq 1 ]; then + echo "====== Upload the packed RPM package for CentOS8 ======" + python3 rpm_upload_tools.py ${PULP3_REPO_NAME_EL8} ${PULP3_DIST_NAME_EL8} *-pr-*el8*.rpm +fi + +rm -rf *.rpm diff --git a/cmake/FindSYSTEMD.cmake b/cmake/FindSYSTEMD.cmake new file mode 100644 index 0000000..c7decde --- /dev/null +++ b/cmake/FindSYSTEMD.cmake @@ -0,0 +1,39 @@ +# - Find SystemdDaemon +# Find the systemd daemon library +# +# This module defines the following variables: +# SYSTEMD_FOUND - True if library and include directory are found +# If set to TRUE, the following are also defined: +# SYSTEMD_INCLUDE_DIRS - The directory where to find the header file +# SYSTEMD_LIBRARIES - Where to find the library file +# +# For conveniance, these variables are also set. They have the same values +# than the variables above. The user can thus choose his/her prefered way +# to write them. +# SYSTEMD_LIBRARY +# SYSTEMD_INCLUDE_DIR +# +# This file is in the public domain + +include(FindPkgConfig) +pkg_check_modules(SYSTEMD libsystemd) + +if(NOT SYSTEMD_FOUND) + find_path(SYSTEMD_INCLUDE_DIRS NAMES systemd/sd-daemon.h + DOC "The Systemd include directory") + + find_library(SYSTEMD_LIBRARIES NAMES systemd + DOC "The Systemd library") + + # Use some standard module to handle the QUIETLY and REQUIRED arguments, and + # set SYSTEMD_FOUND to TRUE if these two variables are set. + include(FindPackageHandleStandardArgs) + find_package_handle_standard_args(SYSTEMD REQUIRED_VARS SYSTEMD_LIBRARIES SYSTEMD_INCLUDE_DIRS) + + if(SYSTEMD_FOUND) + set(SYSTEMD_LIBRARY ${SYSTEMD_LIBRARIES}) + set(SYSTEMD_INCLUDE_DIR ${SYSTEMD_INCLUDE_DIRS}) + endif() +endif() + +mark_as_advanced(SYSTEMD_INCLUDE_DIRS SYSTEMD_LIBRARIES) \ No newline at end of file diff --git a/cmake/Package.cmake b/cmake/Package.cmake new file mode 100644 index 0000000..3091159 --- /dev/null +++ b/cmake/Package.cmake @@ -0,0 +1,35 @@ +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + set(CPACK_PACKAGE_NAME "stellar-debug") +else() + set(CPACK_PACKAGE_NAME "stellar") +endif() + +message(STATUS "Package: ${CPACK_PACKAGE_NAME}") + +set(CPACK_PACKAGE_VENDOR "TSG") +set(CPACK_PACKAGE_VERSION_MAJOR "${STELLAR_VERSION_MAJOR}") +set(CPACK_PACKAGE_VERSION_MINOR "${STELLAR_VERSION_MINOR}") +set(CPACK_PACKAGE_VERSION_PATCH "${STELLAR_VERSION_PATCH}.${STELLAR_DESCRIBE}") +set(CPACK_PACKAGING_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}) + +# RPM Build +set(CPACK_GENERATOR "RPM") +set(CPACK_RPM_AUTO_GENERATED_FILE_NAME ON) +set(CPACK_RPM_FILE_NAME "RPM-DEFAULT") +set(CPACK_RPM_PACKAGE_AUTOREQPROV "no") +set(CPACK_RPM_PACKAGE_RELEASE_DIST on) +set(CPACK_RPM_DEBUGINFO_PACKAGE on) +set(CPACK_RPM_POST_INSTALL_SCRIPT_FILE ${CMAKE_SOURCE_DIR}/cmake/PostInstall.in) +set(CPACK_RPM_POST_UNINSTALL_SCRIPT_FILE ${CMAKE_SOURCE_DIR}/cmake/PostUninstall.in) +set(CPACK_RPM_PRE_UNINSTALL_SCRIPT_FILE ${CMAKE_SOURCE_DIR}/cmake/PreUninstall.in) + +# Must uninstall the debug package before install release package +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + set(CPACK_RPM_PACKAGE_CONFLICTS "stellar") +else() + set(CPACK_RPM_PACKAGE_CONFLICTS "stellar-debug") +endif() + +# Setup %config(noreplace) +set(CPACK_RPM_USER_FILELIST "%config(noreplace) ${CMAKE_INSTALL_PREFIX}/conf/stellar/stellar.conf") +include(CPack) diff --git a/cmake/Version.cmake b/cmake/Version.cmake index f16eb71..4bd226e 100644 --- a/cmake/Version.cmake +++ b/cmake/Version.cmake @@ -1,58 +1,47 @@ - # Using autorevision.sh to generate version information - -set(__SOURCE_AUTORESIVISION ${CMAKE_SOURCE_DIR}/cmake/autorevision.sh) +set(__SOURCE_AUTORESIVISION ${CMAKE_SOURCE_DIR}/autorevision.sh) set(__AUTORESIVISION ${CMAKE_BINARY_DIR}/autorevision.sh) -set(__VERSION_CACHE ${CMAKE_BINARY_DIR}/version.txt) +set(__VERSION_CACHE ${CMAKE_SOURCE_DIR}/version.txt) set(__VERSION_CONFIG ${CMAKE_BINARY_DIR}/version.cmake) file(COPY ${__SOURCE_AUTORESIVISION} DESTINATION ${CMAKE_BINARY_DIR} FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) -# execute autorevision.sh to generate version information -execute_process(COMMAND ${__AUTORESIVISION} -t cmake -o ${__VERSION_CACHE} - OUTPUT_FILE ${__VERSION_CONFIG} ERROR_QUIET) +# Execute autorevision.sh to generate version information +execute_process(COMMAND ${__AUTORESIVISION} -t cmake -o ${__VERSION_CACHE} OUTPUT_FILE ${__VERSION_CONFIG}) include(${__VERSION_CONFIG}) -# extract major, minor, patch version from git tag -string(REGEX REPLACE "^v([0-9]+)\\..*" "\\1" VERSION_MAJOR "${VCS_TAG}") -string(REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${VCS_TAG}") -string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${VCS_TAG}") +# Extract major, minor, patch version from git tag +string(REGEX REPLACE "^v([0-9]+)\\..*" "\\1" STELLAR_VERSION_MAJOR "${VCS_TAG}") +string(REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" STELLAR_VERSION_MINOR "${VCS_TAG}") +string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" STELLAR_VERSION_PATCH "${VCS_TAG}") +string(REGEX REPLACE "[T\\:\\+\\-]" "" STELLAR_VERSION_DATE "${VCS_DATE}") -string(REGEX REPLACE "[T\\:\\+\\-]" "" VERSION_DATE "${VCS_DATE}") - -if(VERSION_DAILY_BUILD) - set(VERSION_PATCH ${VERSION_PATCH}.${VERSION_DATE}) +if(STELLAR_VERSION_DAILY_BUILD) + set(STELLAR_VERSION_PATCH ${STELLAR_VERSION_PATCH}.${STELLAR_VERSION_DATE}) endif() - -if(NOT VERSION_MAJOR) - set(VERSION_MAJOR 1) +if(NOT STELLAR_VERSION_MAJOR) + set(STELLAR_VERSION_MAJOR 3) endif() -if(NOT VERSION_MINOR) - set(VERSION_MINOR 0) +if(NOT STELLAR_VERSION_MINOR) + set(STELLAR_VERSION_MINOR 0) endif() -if(NOT VERSION_PATCH) - set(VERSION_PATCH 0) +if(NOT STELLAR_VERSION_PATCH) + set(STELLAR_VERSION_PATCH 0) endif() -set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}") -set(VERSION_BUILD "${VCS_SHORT_HASH}") +set(STELLAR_DESCRIBE "${VCS_SHORT_HASH}") +set(STELLAR_VERSION "${STELLAR_VERSION_MAJOR}.${STELLAR_VERSION_MINOR}.${STELLAR_VERSION_PATCH}") +set(STELLAR_GIT_VERSION "${STELLAR_VERSION_MAJOR}.${STELLAR_VERSION_MINOR}.${STELLAR_VERSION_PATCH}-${STELLAR_DESCRIBE}") -# print information -message(STATUS "Version: ${VERSION}-${VERSION_BUILD}") +# Replace .- with _ +string(REGEX REPLACE "[\\.\\-]" "_" STELLAR_VAR_VERSION "${STELLAR_GIT_VERSION}") -option(DEFINE_GIT_VERSION "Set DEFINE_GIT_VERSION to TRUE or FALSE" TRUE) - -if(DEFINE_GIT_VERSION) - -set(GIT_VERSION "${VERSION}-${CMAKE_BUILD_TYPE}-${VERSION_BUILD}-${VCS_BRANCH}-${VCS_DATE}") -message(STATUS "GIT_VERSION: ${GIT_VERSION}") -string(REGEX REPLACE "[-:+/\\.]" "_" GIT_VERSION ${GIT_VERSION}) - -add_definitions(-DGIT_VERSION=\"${GIT_VERSION}\") - -endif() +# Print information +message(STATUS "Welcome to stateful network function development platform, Version: ${STELLAR_GIT_VERSION}") +add_definitions(-DSTELLAR_GIT_VERSION=\"${STELLAR_GIT_VERSION}\") +add_definitions(-DSTELLAR_VAR_VERSION=${STELLAR_VAR_VERSION}) \ No newline at end of file diff --git a/conf/CMakeLists.txt b/conf/CMakeLists.txt new file mode 100644 index 0000000..d118a46 --- /dev/null +++ b/conf/CMakeLists.txt @@ -0,0 +1 @@ +install(DIRECTORY stellar DESTINATION conf COMPONENT Profile) \ No newline at end of file diff --git a/conf/stellar/stellar.conf b/conf/stellar/stellar.conf new file mode 100644 index 0000000..e69de29 diff --git a/sdk/example/custom_event_plugin.cpp b/sdk/example/custom_event_plugin.cpp index a1d4d03..dc56c0d 100644 --- a/sdk/example/custom_event_plugin.cpp +++ b/sdk/example/custom_event_plugin.cpp @@ -84,7 +84,7 @@ extern "C" void custom_event_plugin_tcp_entry(const struct stellar_session *sess if (*per_tcp_session_pme == NULL) { struct tcp_session_pme *cur_ctx = tcp_session_pme_create(); - snprintf(cur_ctx->data, 6, "tcp******"); + memcpy(cur_ctx->data, "custom_event_plugin_tcp_entry", strlen("custom_event_plugin_tcp_entry")); *per_tcp_session_pme = *&cur_ctx; } } @@ -115,7 +115,7 @@ extern "C" void custom_event_plugin_custom_entry(const struct stellar_session *s if (*per_custom_session_pme == NULL) { struct custom_session_pme *cur_ctx = custom_session_pme_create(); - snprintf(cur_ctx->data, 6, "custom******"); + memcpy(cur_ctx->data, "custom_event_plugin_custom_entry", strlen("custom_event_plugin_custom_entry")); *per_custom_session_pme = *&cur_ctx; } } diff --git a/sdk/example/http_event_plugin.cpp b/sdk/example/http_event_plugin.cpp index 0cf5364..4f795d3 100644 --- a/sdk/example/http_event_plugin.cpp +++ b/sdk/example/http_event_plugin.cpp @@ -50,7 +50,7 @@ extern "C" void http_event_plugin_entry(const struct stellar_session *session, e if (*per_http_session_pme == NULL) { struct http_session_pme *cur_ctx = http_session_pme_create(); - snprintf(cur_ctx->data, 6, "http******"); + memcpy(cur_ctx->data, "http_event_plugin_entry", strlen("http_event_plugin_entry")); *per_http_session_pme = *&cur_ctx; } } diff --git a/src/packet_io/test/CMakeLists.txt b/src/packet_io/test/CMakeLists.txt index 94aca33..3c1503e 100644 --- a/src/packet_io/test/CMakeLists.txt +++ b/src/packet_io/test/CMakeLists.txt @@ -4,7 +4,7 @@ add_executable(gtest_packet_io target_link_libraries( gtest_packet_io - gtest_main + gtest packet_io ) diff --git a/src/plugin_manager/plugin_manager.cpp b/src/plugin_manager/plugin_manager.cpp index 6baaad6..95f63b4 100644 --- a/src/plugin_manager/plugin_manager.cpp +++ b/src/plugin_manager/plugin_manager.cpp @@ -217,7 +217,7 @@ static int plugin_manager_init_plugins(struct plugin_manager *plug_mgr) return -1; } double percentage = ((double)(i + 1)) / ((double)plug_mgr->used_module_num) * ((double)100); - plugin_manager_log(INFO, "Plugin initialization progress: [%.2f%]", percentage); + plugin_manager_log(INFO, "Plugin initialization progress: [%.2f%%]", percentage); } return 0; @@ -419,7 +419,7 @@ void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_eve plug_ctx = plugin_manager_create_plugin_ctx(plug_mgr, session_name); if (plug_ctx == NULL) { - plugin_manager_log(ERROR, "can't create runtime plugin ctx for session '%s', Please check whether the callback is registered in the current session"); + plugin_manager_log(ERROR, "can't create runtime plugin ctx for session '%s', Please check whether the callback is registered in the current session", session_name); return; } stellar_event_set_plugin_ctx(event, plug_ctx); diff --git a/src/plugin_manager/plugin_manager_module.cpp b/src/plugin_manager/plugin_manager_module.cpp index def9343..2ff7179 100644 --- a/src/plugin_manager/plugin_manager_module.cpp +++ b/src/plugin_manager/plugin_manager_module.cpp @@ -139,7 +139,7 @@ int plugin_manager_module_init(struct plugin_manager_module *module) return -1; } - long elapsed = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000; + long long elapsed = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000; plugin_manager_log(INFO, "plugin dynamic library '%s' init success, using '%lld' us", module->lib_path, elapsed); return 0; @@ -157,7 +157,7 @@ void plugin_manager_module_exit(struct plugin_manager_module *module) clock_gettime(CLOCK_MONOTONIC, &end); module->exit_cb_ptr = NULL; - long elapsed = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000; + long long elapsed = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000; plugin_manager_log(INFO, "plugin dynamic library '%s' exit success, using '%lld' us", module->lib_path, elapsed); } } diff --git a/src/plugin_manager/test/CMakeLists.txt b/src/plugin_manager/test/CMakeLists.txt index 82bc6d6..e3ce81a 100644 --- a/src/plugin_manager/test/CMakeLists.txt +++ b/src/plugin_manager/test/CMakeLists.txt @@ -4,7 +4,7 @@ add_executable(gtest_plugin_manager target_link_libraries( gtest_plugin_manager - gtest_main + gtest plugin_manager session_manager toml diff --git a/src/plugin_manager/test/gtest_plugin_manager.cpp b/src/plugin_manager/test/gtest_plugin_manager.cpp index efc95b2..dbe267f 100644 --- a/src/plugin_manager/test/gtest_plugin_manager.cpp +++ b/src/plugin_manager/test/gtest_plugin_manager.cpp @@ -184,7 +184,7 @@ TEST(PLUGIN_MANAGER_TEST, plugin_mangager_config_CUSTOM) EXPECT_STREQ(config->session_section[2].session_name, "CUSTOM"); EXPECT_STREQ(config->session_section[2].cb_func_name, "custom_event_plugin_custom_entry"); - EXPECT_TRUE(config->session_section[2].event == (0x01 << 1) | (0x01 << 3) | (0x01 << 5)); + EXPECT_TRUE(config->session_section[2].event == (enum session_event_type)((0x01 << 1) | (0x01 << 3) | (0x01 << 5))); plugin_mangager_config_dump(config); plugin_mangager_config_destory(config); @@ -377,28 +377,28 @@ TEST(PLUGIN_MANAGER_TEST, plugin_manager_dispatch_TCP) plugin_manager_dispatch(plug_mgr, &event); pme = (struct tcp_session_pme *)pm_session_get_plugin_pme(&session); EXPECT_TRUE(pme->flags == SESSION_EVENT_OPENING); - EXPECT_STREQ(pme->data, "tcp***"); + EXPECT_STREQ(pme->data, "custom_event_plugin_tcp_entry"); // run evencb event_data.type = SESSION_EVENT_RAWPKT; plugin_manager_dispatch(plug_mgr, &event); pme = (struct tcp_session_pme *)pm_session_get_plugin_pme(&session); EXPECT_TRUE(pme->flags == SESSION_EVENT_RAWPKT); - EXPECT_STREQ(pme->data, "tcp***"); + EXPECT_STREQ(pme->data, "custom_event_plugin_tcp_entry"); // run evencb event_data.type = SESSION_EVENT_ORDPKT; plugin_manager_dispatch(plug_mgr, &event); pme = (struct tcp_session_pme *)pm_session_get_plugin_pme(&session); EXPECT_TRUE(pme->flags == SESSION_EVENT_ORDPKT); - EXPECT_STREQ(pme->data, "tcp***"); + EXPECT_STREQ(pme->data, "custom_event_plugin_tcp_entry"); // run evencb event_data.type = SESSION_EVENT_META; plugin_manager_dispatch(plug_mgr, &event); pme = (struct tcp_session_pme *)pm_session_get_plugin_pme(&session); EXPECT_TRUE(pme->flags == SESSION_EVENT_META); - EXPECT_STREQ(pme->data, "tcp***"); + EXPECT_STREQ(pme->data, "custom_event_plugin_tcp_entry"); // run evencb event_data.type = SESSION_EVENT_CLOSING; diff --git a/src/plugin_manager/test/test_plugins/plugins_library/custom_event_plugin.cpp b/src/plugin_manager/test/test_plugins/plugins_library/custom_event_plugin.cpp index fbeed57..dc34f1e 100644 --- a/src/plugin_manager/test/test_plugins/plugins_library/custom_event_plugin.cpp +++ b/src/plugin_manager/test/test_plugins/plugins_library/custom_event_plugin.cpp @@ -112,7 +112,7 @@ extern "C" void custom_event_plugin_tcp_entry(const struct stellar_session *sess if (*per_tcp_session_pme == NULL) { struct tcp_session_pme *cur_ctx = tcp_session_pme_create(); - memcpy(cur_ctx->data, "tcp***", 6); + memcpy(cur_ctx->data, "custom_event_plugin_tcp_entry", strlen("custom_event_plugin_tcp_entry")); cur_ctx->flags = SESSION_EVENT_OPENING; *per_tcp_session_pme = *&cur_ctx; } diff --git a/src/protocol_decoder/http/http.cpp b/src/protocol_decoder/http/http.cpp index b24fffd..c7e3e79 100644 --- a/src/protocol_decoder/http/http.cpp +++ b/src/protocol_decoder/http/http.cpp @@ -1,8 +1,10 @@ +#include + #include "sdk/include/session.h" void http_decoder(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) { - struct stellar_session_event_extras *info; + struct stellar_session_event_extras *info = NULL; struct stellar_session *new_session = session_manager_session_derive(s, "HTTP"); session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7cdc600..56d7b60 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -7,7 +7,7 @@ add_executable(gtest_stellar target_link_libraries( gtest_stellar - gtest_main + gtest ) include(GoogleTest) diff --git a/vendor/CMakeLists.txt b/vendor/CMakeLists.txt index 5653577..6799ed0 100644 --- a/vendor/CMakeLists.txt +++ b/vendor/CMakeLists.txt @@ -1,10 +1,21 @@ -include(FetchContent) -FetchContent_Declare( - googletest - #URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip - URL ${CMAKE_CURRENT_SOURCE_DIR}/googletest-release-1.8.0.tar.gz - URL_MD5 16877098823401d1bf2ed7891d7dce36 -) -# For Windows: Prevent overriding the parent project's compiler/linker settings -set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) -FetchContent_MakeAvailable(googletest) \ No newline at end of file +include(ExternalProject) + +# GoogleTest +ExternalProject_Add(googletest PREFIX googletest + URL ${CMAKE_CURRENT_SOURCE_DIR}/googletest-release-1.8.0.tar.gz + URL_MD5 16877098823401d1bf2ed7891d7dce36 + CMAKE_ARGS -DCMAKE_INSTALL_PREFIX= -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}) + +ExternalProject_Get_Property(googletest INSTALL_DIR) +file(MAKE_DIRECTORY ${INSTALL_DIR}/include) + +add_library(gtest STATIC IMPORTED GLOBAL) +add_dependencies(gtest googletest) +set_property(TARGET gtest PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/libgtest.a) +set_property(TARGET gtest PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include) +set_property(TARGET gtest PROPERTY INTERFACE_LINK_LIBRARIES pthread) + +add_library(gmock STATIC IMPORTED GLOBAL) +add_dependencies(gmock googletest) +set_property(TARGET gmock PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/libgmock.a) +set_property(TARGET gmock PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include) \ No newline at end of file From fd2a67e0eb536f8c963b1035394c538ddaf3160f Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Fri, 5 Aug 2022 18:25:06 +0800 Subject: [PATCH 15/17] bugfix CI --- .gitlab-ci.yml | 16 ++++++++-------- cmake/PostInstall.in | 1 + cmake/PostUninstall.in | 1 + cmake/PreUninstall.in | 1 + src/CMakeLists.txt | 4 +++- 5 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 cmake/PostInstall.in create mode 100644 cmake/PostUninstall.in create mode 100644 cmake/PreUninstall.in diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 0e52438..a3cf3a8 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -64,7 +64,7 @@ develop_build_debug_for_centos7: extends: .build_by_travis_for_centos7 variables: TESTING_VERSION_BUILD: 1 - UPLOAD_SYMBOL_FILES: 1 + # UPLOAD_SYMBOL_FILES: 1 BUILD_TYPE: Debug # ASAN_OPTION: ADDRESS PACKAGE: 1 @@ -82,7 +82,7 @@ develop_build_release_for_centos7: extends: .build_by_travis_for_centos7 variables: TESTING_VERSION_BUILD: 1 - UPLOAD_SYMBOL_FILES: 1 + # UPLOAD_SYMBOL_FILES: 1 # ASAN_OPTION: ADDRESS BUILD_TYPE: RelWithDebInfo PACKAGE: 1 @@ -98,7 +98,7 @@ develop_build_release_for_centos7: release_build_debug_for_centos7: variables: - UPLOAD_SYMBOL_FILES: 1 + # UPLOAD_SYMBOL_FILES: 1 BUILD_TYPE: Debug PACKAGE: 1 PULP3_REPO_NAME: stellar-stable-x86_64.el7 @@ -114,7 +114,7 @@ release_build_debug_for_centos7: release_build_release_for_centos7: variables: BUILD_TYPE: RelWithDebInfo - UPLOAD_SYMBOL_FILES: 1 + # UPLOAD_SYMBOL_FILES: 1 PACKAGE: 1 PULP3_REPO_NAME: stellar-stable-x86_64.el7 PULP3_DIST_NAME: stellar-stable-x86_64.el7 @@ -152,7 +152,7 @@ develop_build_debug_for_centos8: extends: .build_by_travis_for_centos8 variables: TESTING_VERSION_BUILD: 1 - UPLOAD_SYMBOL_FILES: 1 + # UPLOAD_SYMBOL_FILES: 1 BUILD_TYPE: Debug # ASAN_OPTION: ADDRESS PACKAGE: 1 @@ -170,7 +170,7 @@ develop_build_release_for_centos8: extends: .build_by_travis_for_centos8 variables: TESTING_VERSION_BUILD: 1 - UPLOAD_SYMBOL_FILES: 1 + # UPLOAD_SYMBOL_FILES: 1 # ASAN_OPTION: ADDRESS BUILD_TYPE: RelWithDebInfo PACKAGE: 1 @@ -186,7 +186,7 @@ develop_build_release_for_centos8: release_build_debug_for_centos8: variables: - UPLOAD_SYMBOL_FILES: 1 + # UPLOAD_SYMBOL_FILES: 1 BUILD_TYPE: Debug PACKAGE: 1 PULP3_REPO_NAME: stellar-stable-x86_64.el8 @@ -202,7 +202,7 @@ release_build_debug_for_centos8: release_build_release_for_centos8: variables: BUILD_TYPE: RelWithDebInfo - UPLOAD_SYMBOL_FILES: 1 + # UPLOAD_SYMBOL_FILES: 1 PACKAGE: 1 PULP3_REPO_NAME: stellar-stable-x86_64.el8 PULP3_DIST_NAME: stellar-stable-x86_64.el8 diff --git a/cmake/PostInstall.in b/cmake/PostInstall.in new file mode 100644 index 0000000..d601599 --- /dev/null +++ b/cmake/PostInstall.in @@ -0,0 +1 @@ +/sbin/ldconfig \ No newline at end of file diff --git a/cmake/PostUninstall.in b/cmake/PostUninstall.in new file mode 100644 index 0000000..d601599 --- /dev/null +++ b/cmake/PostUninstall.in @@ -0,0 +1 @@ +/sbin/ldconfig \ No newline at end of file diff --git a/cmake/PreUninstall.in b/cmake/PreUninstall.in new file mode 100644 index 0000000..d601599 --- /dev/null +++ b/cmake/PreUninstall.in @@ -0,0 +1 @@ +/sbin/ldconfig \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6b4ad06..bed233d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -23,4 +23,6 @@ target_link_libraries( http toml dl -) \ No newline at end of file +) + +install(TARGETS stellar RUNTIME DESTINATION bin COMPONENT Program) \ No newline at end of file From 0f7468b9946e29e19cd8a6f8c63e6949e29b9b02 Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Mon, 8 Aug 2022 14:19:32 +0800 Subject: [PATCH 16/17] Refactored pm_session_dettach_others to pm_session_take_over --- readme.md | 15 ++-- sdk/example/custom_event_plugin.cpp | 17 ---- sdk/example/http_event_plugin.cpp | 12 +-- .../custom_event_plugin.inf | 1 - .../http_event_plugin/http_event_plugin.inf | 1 - sdk/include/http.h | 3 +- sdk/include/plugin.h | 15 +++- sdk/include/session.h | 6 -- src/main.cpp | 2 +- src/plugin_manager/plugin_manager.cpp | 89 ++++++++++++------- src/plugin_manager/plugin_manager.h | 2 +- src/plugin_manager/plugin_manager_config.cpp | 8 -- src/plugin_manager/plugin_manager_config.h | 1 - src/plugin_manager/plugin_manager_module.cpp | 12 +-- .../test/gtest_plugin_manager.cpp | 8 +- .../custom_event_plugin.inf | 1 - .../http_event_plugin/http_event_plugin.inf | 1 - .../plugins_library/custom_event_plugin.cpp | 24 ----- .../plugins_library/http_event_plugin.cpp | 12 +-- src/protocol_decoder/http/http.cpp | 4 - 20 files changed, 91 insertions(+), 143 deletions(-) diff --git a/readme.md b/readme.md index 5b375b8..b6d1023 100644 --- a/readme.md +++ b/readme.md @@ -42,17 +42,20 @@ Plugin Management APIs ``` /* - * The plugin manager just set the skip flag and don't call this event callback next. + * The pm_session_dettach_me just sets the flag to disable this plugin and no longer call this event callback. * Before calling pm_session_dettach_me, the current plugin must release related resources for the current session. */ pm_session_dettach_me(session); /* - * The plugin manager uses ERROR_EVENT_DETTACH to call other plugin error callbacks, - * and when the plugin error callback handler is called, - * the error callback handler must release the relevant resources for the current session. + * The current plugin takes over the current session, the pm_session_take_over setting flag disables other plugins, + * and the current session does not call other plugins except for the SESSION_EVENT_CLOSING event. + * + * The current plugin may be taken over while the session is open (SESSION_EVENT_OPENING), + * other plugins event_cb is not called, the session pme on other plugins is NULL, + * When the session is closed (SESSION_EVENT_CLOSING), call other plugin even_cb to destroy pme must check if pme is NULL. */ -pm_session_dettach_others(session); +pm_session_take_over(session); ``` ## Session Manager @@ -82,7 +85,7 @@ plugin_entry(session, pme) ret=check_security_policy(session); if(ret==INTERCEPT) { - pm_session_dettach_others(session); + pm_session_take_over(session); } else if(ret==RATE_LIMIT) { diff --git a/sdk/example/custom_event_plugin.cpp b/sdk/example/custom_event_plugin.cpp index dc56c0d..8034d99 100644 --- a/sdk/example/custom_event_plugin.cpp +++ b/sdk/example/custom_event_plugin.cpp @@ -56,23 +56,6 @@ static void custom_session_pme_destory(struct custom_session_pme *pme) } } -extern "C" void custom_event_plugin_error(const struct stellar_session *session, enum error_event_type event, void **pme) -{ - if (strcmp(stellar_session_get_name(session), "TCP") == 0) - { - struct tcp_session_pme **per_tcp_session_pme = (struct tcp_session_pme **)pme; - printf("RUN custom_event_plugin_error, session_name: 'TCP', error_event_type: %d, pme->data: %s\n", event, (*per_tcp_session_pme) == NULL ? "NULL" : (*per_tcp_session_pme)->data); - tcp_session_pme_destory(*per_tcp_session_pme); - } - - if (strcmp(stellar_session_get_name(session), "CUSTOM") == 0) - { - struct custom_session_pme **per_custom_session_pme = (struct custom_session_pme **)pme; - printf("RUN custom_event_plugin_error, session_name: 'CUSTOM', error_event_type: %d, pme->data: %s\n", event, (*per_custom_session_pme) == NULL ? "NULL" : (*per_custom_session_pme)->data); - custom_session_pme_destory(*per_custom_session_pme); - } -} - extern "C" void custom_event_plugin_tcp_entry(const struct stellar_session *session, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) { struct tcp_session_pme **per_tcp_session_pme = (struct tcp_session_pme **)pme; diff --git a/sdk/example/http_event_plugin.cpp b/sdk/example/http_event_plugin.cpp index 4f795d3..ef1e761 100644 --- a/sdk/example/http_event_plugin.cpp +++ b/sdk/example/http_event_plugin.cpp @@ -29,16 +29,6 @@ static void http_session_pme_destory(struct http_session_pme *pme) } } -extern "C" void http_event_plugin_error(const struct stellar_session *session, enum error_event_type event, void **pme) -{ - if (strcmp(stellar_session_get_name(session), "HTTP") == 0) - { - struct http_session_pme **per_http_session_pme = (struct http_session_pme **)pme; - printf("RUN http_event_plugin_error, session_name: 'HTTP', error_event_type: %d, pme->data: %s\n", event, (*per_http_session_pme) == NULL ? "NULL" : (*per_http_session_pme)->data); - http_session_pme_destory(*per_http_session_pme); - } -} - extern "C" void http_event_plugin_entry(const struct stellar_session *session, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) { struct http_session_pme **per_http_session_pme = (struct http_session_pme **)pme; @@ -64,7 +54,7 @@ extern "C" void http_event_plugin_entry(const struct stellar_session *session, e if (event & SESSION_EVENT_ORDPKT) { // TODO - pm_session_dettach_others(session); + pm_session_take_over(session); } if (event & SESSION_EVENT_META) diff --git a/sdk/example/plugins/custom_event_plugin/custom_event_plugin.inf b/sdk/example/plugins/custom_event_plugin/custom_event_plugin.inf index 3718be0..08c84a3 100644 --- a/sdk/example/plugins/custom_event_plugin/custom_event_plugin.inf +++ b/sdk/example/plugins/custom_event_plugin/custom_event_plugin.inf @@ -1,7 +1,6 @@ [PLUGINFO] INIT_FUNC="custom_event_plugin_init" EXIT_FUNC="custom_event_plugin_exit" -ERROR_FUNC="custom_event_plugin_error" LIBRARY_PATH="./plugins/custom_event_plugin/custom_event_plugin.so" # Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL" diff --git a/sdk/example/plugins/http_event_plugin/http_event_plugin.inf b/sdk/example/plugins/http_event_plugin/http_event_plugin.inf index e0f2704..a58a405 100644 --- a/sdk/example/plugins/http_event_plugin/http_event_plugin.inf +++ b/sdk/example/plugins/http_event_plugin/http_event_plugin.inf @@ -1,7 +1,6 @@ [PLUGINFO] INIT_FUNC="http_event_plugin_init" EXIT_FUNC="http_event_plugin_exit" -ERROR_FUNC="http_event_plugin_error" LIBRARY_PATH="./plugins/http_event_plugin/http_event_plugin.so" # Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL" diff --git a/sdk/include/http.h b/sdk/include/http.h index 4db1126..6250055 100644 --- a/sdk/include/http.h +++ b/sdk/include/http.h @@ -2,5 +2,4 @@ #include "session.h" -void http_decoder(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme); -void http_error_cb(const struct stellar_session *s, enum error_event_type event, void **pme); \ No newline at end of file +void http_decoder(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme); \ No newline at end of file diff --git a/sdk/include/plugin.h b/sdk/include/plugin.h index 0d6c346..301363a 100644 --- a/sdk/include/plugin.h +++ b/sdk/include/plugin.h @@ -15,8 +15,21 @@ typedef void plugin_exit_callback(void); * Public API For Plugin ******************************************************************************/ +/* + * The pm_session_dettach_me just sets the flag to disable this plugin and no longer call this event callback. + * Before calling pm_session_dettach_me, the current plugin must release related resources for the current session. + */ void pm_session_dettach_me(const struct stellar_session *session); -void pm_session_dettach_others(const struct stellar_session *session); + +/* + * The current plugin takes over the current session, the pm_session_take_over setting flag disables other plugins, + * and the current session does not call other plugins except for the SESSION_EVENT_CLOSING event. + * + * The current plugin may be taken over while the session is open (SESSION_EVENT_OPENING), + * other plugins event_cb is not called, the session pme on other plugins is NULL, + * When the session is closed (SESSION_EVENT_CLOSING), call other plugin even_cb to destroy pme must check if pme is NULL. + */ +void pm_session_take_over(const struct stellar_session *session); #ifdef __cpluscplus } diff --git a/sdk/include/session.h b/sdk/include/session.h index 8c4b25f..8ce3d26 100644 --- a/sdk/include/session.h +++ b/sdk/include/session.h @@ -40,14 +40,8 @@ enum session_event_type SESSION_EVENT_ALL = (0x01 << 1 | 0x01 << 2 | 0x01 << 3 | 0x01 << 4 | 0x01 << 5), }; -enum error_event_type -{ - ERROR_EVENT_DETTACH = (0x1 << 10), -}; - struct stellar_session_event_extras; -typedef void(fn_session_error_callback)(const struct stellar_session *s, enum error_event_type event, void **pme); typedef void(fn_session_event_callback)(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme); void session_manager_trigger_event(struct stellar_session *s, enum session_event_type type, struct stellar_session_event_extras *info); diff --git a/src/main.cpp b/src/main.cpp index 2b1d541..75f803c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -59,7 +59,7 @@ int main(int argc, char ** argv) struct plugin_manager *plug_mgr = plugin_manager_create(); // register build-in plugin - plugin_manager_register(plug_mgr, "HTTP", SESSION_EVENT_ALL, http_decoder, http_error_cb); + plugin_manager_register(plug_mgr, "HTTP", SESSION_EVENT_ALL, http_decoder); // load external plugins char file_path[] = "./plugs/plugins.inf"; diff --git a/src/plugin_manager/plugin_manager.cpp b/src/plugin_manager/plugin_manager.cpp index 95f63b4..d90122c 100644 --- a/src/plugin_manager/plugin_manager.cpp +++ b/src/plugin_manager/plugin_manager.cpp @@ -10,14 +10,20 @@ * CallBack Runtime (For Per Session) ******************************************************************************/ +enum plugin_status +{ + PLUGIN_STATUS_NORMAL = 0x00, + PLUGIN_STATUS_DETTACH_ME = 0x01, + PLUGIN_STATUS_TAKEN_OVER = 0x02, // Noitce: this is taken over not take over +}; + struct callback_runtime { - int skip; + enum plugin_status status; void *cb_args; enum session_event_type event; fn_session_event_callback *event_cb; - fn_session_error_callback *error_cb; }; struct session_plugin_ctx @@ -35,7 +41,6 @@ struct callback_static { enum session_event_type event; fn_session_event_callback *event_cb; - fn_session_error_callback *error_cb; }; struct plugin_manager_eventcb @@ -90,10 +95,9 @@ static struct session_plugin_ctx *plugin_manager_create_plugin_ctx(struct plugin for (int i = 0; i < plug_ctx->callback_num; i++) { - plug_ctx->callbacks[i].skip = 0; + plug_ctx->callbacks[i].status = PLUGIN_STATUS_NORMAL; plug_ctx->callbacks[i].event = elem->callbacks[i].event; plug_ctx->callbacks[i].event_cb = elem->callbacks[i].event_cb; - plug_ctx->callbacks[i].error_cb = elem->callbacks[i].error_cb; plug_ctx->callbacks[i].cb_args = NULL; } @@ -336,7 +340,7 @@ void plugin_manager_destory(struct plugin_manager *plug_mgr) } } -int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_name, enum session_event_type event, fn_session_event_callback *event_cb, fn_session_error_callback *error_cb) +int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_name, enum session_event_type event, fn_session_event_callback *event_cb) { if (strlen(session_name) <= 0) { @@ -356,12 +360,6 @@ int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session return -1; } - if (error_cb == NULL) - { - plugin_manager_log(ERROR, "invalid parameter, the error callback corresponding to the session name '%s' is null", session_name); - return -1; - } - struct plugin_manager_eventcb *elem; HASH_FIND_STR(plug_mgr->evcb_htable, session_name, elem); // session_name exists, add a new cb to the end of the callbacks dynamic array @@ -371,7 +369,6 @@ int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session elem->callbacks[elem->callback_num].event = event; elem->callbacks[elem->callback_num].event_cb = event_cb; - elem->callbacks[elem->callback_num].error_cb = error_cb; elem->callback_num++; } @@ -385,7 +382,6 @@ int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session elem->callbacks[elem->callback_num].event = event; elem->callbacks[elem->callback_num].event_cb = event_cb; - elem->callbacks[elem->callback_num].error_cb = error_cb; elem->callback_num++; @@ -431,17 +427,43 @@ void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_eve for (int i = 0; i < plug_ctx->callback_num; i++) { struct callback_runtime *runtime = &plug_ctx->callbacks[i]; - if (runtime->skip == 1) + + if (runtime->status == PLUGIN_STATUS_DETTACH_ME) { - plugin_manager_log(DEBUG, "dispatch, skip event_cb: %p, session: %s, event: (%d, %s)", runtime->event_cb, session_name, event_type, event_str_buffer); + plugin_manager_log(DEBUG, "dispatch, skip event_cb: %p, plugin status: 'dettach me', session: %s, event: (%d, %s)", runtime->event_cb, session_name, event_type, event_str_buffer); continue; } - - if (runtime->event & event_type) + else if (runtime->status == PLUGIN_STATUS_TAKEN_OVER) { - plug_ctx->callback_index = i; - plugin_manager_log(DEBUG, "dispatch, run event_cb: %p, session: %s, event: (%d, %s)", runtime->event_cb, session_name, event_type, event_str_buffer); - runtime->event_cb(seesion, event_type, packet, payload, payload_len, &runtime->cb_args); + /* + * This plugin may be taken over when the session is open (SESSION_EVENT_OPENING), + * and the pme may be NULL when event_cb() is run to free memory with the session is closed (SESSION_EVENT_CLOSING). + * So, The plugin must check whether the pme is NULL before freeing memory. + */ + if (event_type & SESSION_EVENT_CLOSING) + { + plug_ctx->callback_index = i; + plugin_manager_log(DEBUG, "dispatch, run event_cb: %p, plugin status: 'taken over', session: %s, event: (%d, %s)", runtime->event_cb, session_name, event_type, event_str_buffer); + runtime->event_cb(seesion, SESSION_EVENT_CLOSING, packet, payload, payload_len, &runtime->cb_args); + continue; + } + else + { + plugin_manager_log(DEBUG, "dispatch, skip event_cb: %p, plugin status: 'taken over', session: %s, event: (%d, %s)", runtime->event_cb, session_name, event_type, event_str_buffer); + } + } + else if (runtime->status == PLUGIN_STATUS_NORMAL) + { + if (runtime->event & event_type) + { + plug_ctx->callback_index = i; + plugin_manager_log(DEBUG, "dispatch, run event_cb: %p, plugin status: 'normal', session: %s, event: (%d, %s)", runtime->event_cb, session_name, event_type, event_str_buffer); + runtime->event_cb(seesion, event_type, packet, payload, payload_len, &runtime->cb_args); + } + else + { + plugin_manager_log(DEBUG, "dispatch, skip event_cb: %p, plugin status: 'normal', session: %s, event: (%d, %s)", runtime->event_cb, session_name, event_type, event_str_buffer); + } } } } @@ -462,21 +484,29 @@ void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_eve * Public API For Plugin ******************************************************************************/ +/* + * pm_session_dettach_me just sets the flag to disable this plugin and no longer call this event callback. + * Before calling pm_session_dettach_me, the current plugin must release related resources for the current session. + */ void pm_session_dettach_me(const struct stellar_session *session) { struct session_plugin_ctx *plugin_ctx = stellar_session_get_plugin_ctx(session); assert(plugin_ctx); struct callback_runtime *runtime_me = &plugin_ctx->callbacks[plugin_ctx->callback_index]; - /* - * Just set the skip flag and don't call this event callback next. - * The plugin is closed before calling pm_session_dettach_me. - */ - runtime_me->skip = 1; + runtime_me->status = PLUGIN_STATUS_DETTACH_ME; plugin_manager_log(DEBUG, "%p dettach me, disable event_cb: %p, session: %s", runtime_me->event_cb, runtime_me->event_cb, stellar_session_get_name(session)); } -void pm_session_dettach_others(const struct stellar_session *session) +/* + * The current plugin takes over the current session, the pm_session_take_over setting flag disables other plugins, + * and the current session does not call other plugins except for the SESSION_EVENT_CLOSING event. + * + * The current plugin may be taken over while the session is open (SESSION_EVENT_OPENING), + * other plugins event_cb is not called, the session pme on other plugins is NULL, + * When the session is closed (SESSION_EVENT_CLOSING), call other plugin even_cb to destroy pme must check if pme is NULL + */ +void pm_session_take_over(const struct stellar_session *session) { struct session_plugin_ctx *plugin_ctx = stellar_session_get_plugin_ctx(session); assert(plugin_ctx); @@ -487,9 +517,8 @@ void pm_session_dettach_others(const struct stellar_session *session) if (i != plugin_ctx->callback_index) { struct callback_runtime *runtime_other = &plugin_ctx->callbacks[i]; - runtime_other->skip = 1; - plugin_manager_log(DEBUG, "%p dettach others, run error_cb: %p, session: %s", runtime_me->event_cb, runtime_other->error_cb, stellar_session_get_name(session)); - runtime_other->error_cb(session, ERROR_EVENT_DETTACH, &runtime_other->cb_args); + runtime_other->status = PLUGIN_STATUS_TAKEN_OVER; + plugin_manager_log(DEBUG, "%p take over, disable event_cb: %p, session: %s", runtime_me->event_cb, runtime_other->event_cb, stellar_session_get_name(session)); } } } diff --git a/src/plugin_manager/plugin_manager.h b/src/plugin_manager/plugin_manager.h index 2827483..b23e874 100644 --- a/src/plugin_manager/plugin_manager.h +++ b/src/plugin_manager/plugin_manager.h @@ -16,7 +16,7 @@ void plugin_manager_destory(struct plugin_manager *plug_mgr); int plugin_manager_load(struct plugin_manager *plug_mgr, const char *file); void plugin_manager_unload(struct plugin_manager *plug_mgr); -int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_name, enum session_event_type event, fn_session_event_callback *event_cb, fn_session_error_callback *error_cb); +int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_name, enum session_event_type event, fn_session_event_callback *event_cb); void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_event *event); // only use for gtest diff --git a/src/plugin_manager/plugin_manager_config.cpp b/src/plugin_manager/plugin_manager_config.cpp index 89dd39b..1772900 100644 --- a/src/plugin_manager/plugin_manager_config.cpp +++ b/src/plugin_manager/plugin_manager_config.cpp @@ -62,11 +62,6 @@ static int toml_parse_plugin_section(toml_table_t *root, struct plugin_manager_c return -1; } - if (toml_parse_string(plugin_section, "ERROR_FUNC", file, &config->plugin_section.error_func_name) == -1) - { - return -1; - } - if (toml_parse_string(plugin_section, "LIBRARY_PATH", file, &config->plugin_section.lib_path) == -1) { return -1; @@ -174,7 +169,6 @@ struct plugin_manager_config *plugin_mangager_config_create() config->session_section = NULL; config->plugin_section.init_func_name = NULL; config->plugin_section.exit_func_name = NULL; - config->plugin_section.error_func_name = NULL; config->plugin_section.lib_path = NULL; return config; @@ -188,7 +182,6 @@ void plugin_mangager_config_destory(struct plugin_manager_config *config) safe_free(config->plugin_section.init_func_name); safe_free(config->plugin_section.exit_func_name); - safe_free(config->plugin_section.error_func_name); safe_free(config->plugin_section.lib_path); if (config->session_section) @@ -213,7 +206,6 @@ void plugin_mangager_config_dump(struct plugin_manager_config *config) plugin_manager_log(DEBUG, "[CONFIG_FILE] : %s", config->file_path); plugin_manager_log(DEBUG, "[PLUGINFO]->INIT_FUNC : %s", config->plugin_section.init_func_name); plugin_manager_log(DEBUG, "[PLUGINFO]->EXIT_FUNC : %s", config->plugin_section.exit_func_name); - plugin_manager_log(DEBUG, "[PLUGINFO]->ERROR_FUNC : %s", config->plugin_section.error_func_name); plugin_manager_log(DEBUG, "[PLUGINFO]->LIBRARY_PATH : %s", config->plugin_section.lib_path); if (config->session_section) diff --git a/src/plugin_manager/plugin_manager_config.h b/src/plugin_manager/plugin_manager_config.h index 16a62ba..e028a15 100644 --- a/src/plugin_manager/plugin_manager_config.h +++ b/src/plugin_manager/plugin_manager_config.h @@ -20,7 +20,6 @@ struct plugin_section_config { char *init_func_name; char *exit_func_name; - char *error_func_name; char *lib_path; }; diff --git a/src/plugin_manager/plugin_manager_module.cpp b/src/plugin_manager/plugin_manager_module.cpp index 2ff7179..7756da5 100644 --- a/src/plugin_manager/plugin_manager_module.cpp +++ b/src/plugin_manager/plugin_manager_module.cpp @@ -20,7 +20,6 @@ struct plugin_manager_module plugin_init_callback *init_cb_ptr; plugin_exit_callback *exit_cb_ptr; - fn_session_error_callback *error_cb_ptr; struct plugin_manager_module_evcb *evcbs; int evcbs_num; @@ -85,14 +84,6 @@ struct plugin_manager_module *plugin_manager_module_open(struct plugin_manager_c goto err; } - module->error_cb_ptr = (fn_session_error_callback *)(dlsym(module->dl_handle, config->plugin_section.error_func_name)); - if (module->error_cb_ptr == NULL) - { - plugin_manager_log(ERROR, "can't find symbol name of '%s' in dynamic library %s, %s", - config->plugin_section.error_func_name, module->lib_path, dlerror()); - goto err; - } - if (config->session_section) { module->evcbs = safe_alloc(struct plugin_manager_module_evcb, config->session_section_num); @@ -170,7 +161,7 @@ int plugin_manager_module_register(struct plugin_manager *plug_mgr, struct plugi { struct plugin_manager_module_evcb *event_cb = &module->evcbs[i]; - if (plugin_manager_register(plug_mgr, event_cb->session_name, event_cb->event, event_cb->event_cb_ptr, module->error_cb_ptr) == -1) + if (plugin_manager_register(plug_mgr, event_cb->session_name, event_cb->event, event_cb->event_cb_ptr) == -1) { plugin_manager_log(ERROR, "dynamic library '%s' failed to register the event callback function of session '%s'", module->lib_path, event_cb->session_name); return -1; @@ -188,7 +179,6 @@ void plugin_manager_module_dump(struct plugin_manager_module *module, struct plu plugin_manager_log(DEBUG, "[LIBRARY] : %s, %p", config->plugin_section.lib_path, module->dl_handle); plugin_manager_log(DEBUG, "[INIT_FUNC] : %s, %p", config->plugin_section.init_func_name, module->init_cb_ptr); plugin_manager_log(DEBUG, "[EXIT_FUNC] : %s, %p", config->plugin_section.exit_func_name, module->exit_cb_ptr); - plugin_manager_log(DEBUG, "[ERROR_FUNC] : %s, %p", config->plugin_section.error_func_name, module->error_cb_ptr); for (int i = 0; i < module->evcbs_num; i++) { diff --git a/src/plugin_manager/test/gtest_plugin_manager.cpp b/src/plugin_manager/test/gtest_plugin_manager.cpp index dbe267f..6afd126 100644 --- a/src/plugin_manager/test/gtest_plugin_manager.cpp +++ b/src/plugin_manager/test/gtest_plugin_manager.cpp @@ -144,7 +144,6 @@ TEST(PLUGIN_MANAGER_TEST, plugin_mangager_config_HTTP) EXPECT_STREQ(config->file_path, "./plugins_config/http_event_plugin/http_event_plugin.inf"); EXPECT_STREQ(config->plugin_section.init_func_name, "http_event_plugin_init"); EXPECT_STREQ(config->plugin_section.exit_func_name, "http_event_plugin_exit"); - EXPECT_STREQ(config->plugin_section.error_func_name, "http_event_plugin_error"); EXPECT_STREQ(config->plugin_section.lib_path, "./test_plugins/plugins_library/http_event_plugin_test.so"); EXPECT_TRUE(config->session_section_num == 1); @@ -170,7 +169,6 @@ TEST(PLUGIN_MANAGER_TEST, plugin_mangager_config_CUSTOM) EXPECT_STREQ(config->file_path, "./plugins_config/custom_event_plugin/custom_event_plugin.inf"); EXPECT_STREQ(config->plugin_section.init_func_name, "custom_event_plugin_init"); EXPECT_STREQ(config->plugin_section.exit_func_name, "custom_event_plugin_exit"); - EXPECT_STREQ(config->plugin_section.error_func_name, "custom_event_plugin_error"); EXPECT_STREQ(config->plugin_section.lib_path, "./test_plugins/plugins_library/custom_event_plugin_test.so"); EXPECT_TRUE(config->session_section_num == 3); @@ -494,8 +492,8 @@ TEST(PLUGIN_MANAGER_TEST, plugin_manager_dispatch_HTTP_dettach_me) #endif #if 1 -// http_event_plugin_entry + SESSION_EVENT_ORDPKT ==> pm_session_dettach_others -TEST(PLUGIN_MANAGER_TEST, plugin_manager_dispatch_HTTP_dettach_other) +// http_event_plugin_entry + SESSION_EVENT_ORDPKT ==> pm_session_take_over +TEST(PLUGIN_MANAGER_TEST, plugin_manager_dispatch_HTTP_take_over) { /* * [SESSION_NAME.HTTP] @@ -542,7 +540,7 @@ TEST(PLUGIN_MANAGER_TEST, plugin_manager_dispatch_HTTP_dettach_other) EXPECT_TRUE(pme->flags == SESSION_EVENT_OPENING); EXPECT_STREQ(pme->data, "custom_event_plugin_http_entry"); - // http_event_plugin_entry + SESSION_EVENT_ORDPKT ==> pm_session_dettach_others + // http_event_plugin_entry + SESSION_EVENT_ORDPKT ==> pm_session_take_over event_data.type = SESSION_EVENT_ORDPKT; plugin_manager_dispatch(plug_mgr, &event); pme = (struct http_session_pme *)pm_session_get_plugin_pme(&session); diff --git a/src/plugin_manager/test/test_plugins/plugins_config/custom_event_plugin/custom_event_plugin.inf b/src/plugin_manager/test/test_plugins/plugins_config/custom_event_plugin/custom_event_plugin.inf index 5df88aa..d3ff370 100644 --- a/src/plugin_manager/test/test_plugins/plugins_config/custom_event_plugin/custom_event_plugin.inf +++ b/src/plugin_manager/test/test_plugins/plugins_config/custom_event_plugin/custom_event_plugin.inf @@ -1,7 +1,6 @@ [PLUGINFO] INIT_FUNC="custom_event_plugin_init" EXIT_FUNC="custom_event_plugin_exit" -ERROR_FUNC="custom_event_plugin_error" LIBRARY_PATH="./test_plugins/plugins_library/custom_event_plugin_test.so" # Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL" diff --git a/src/plugin_manager/test/test_plugins/plugins_config/http_event_plugin/http_event_plugin.inf b/src/plugin_manager/test/test_plugins/plugins_config/http_event_plugin/http_event_plugin.inf index cf0d16b..1dd124b 100644 --- a/src/plugin_manager/test/test_plugins/plugins_config/http_event_plugin/http_event_plugin.inf +++ b/src/plugin_manager/test/test_plugins/plugins_config/http_event_plugin/http_event_plugin.inf @@ -1,7 +1,6 @@ [PLUGINFO] INIT_FUNC="http_event_plugin_init" EXIT_FUNC="http_event_plugin_exit" -ERROR_FUNC="http_event_plugin_error" LIBRARY_PATH="./test_plugins/plugins_library/http_event_plugin_test.so" # Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL" diff --git a/src/plugin_manager/test/test_plugins/plugins_library/custom_event_plugin.cpp b/src/plugin_manager/test/test_plugins/plugins_library/custom_event_plugin.cpp index dc34f1e..3757bbc 100644 --- a/src/plugin_manager/test/test_plugins/plugins_library/custom_event_plugin.cpp +++ b/src/plugin_manager/test/test_plugins/plugins_library/custom_event_plugin.cpp @@ -79,30 +79,6 @@ static void http_session_pme_destory(struct http_session_pme *pme) } } -extern "C" void custom_event_plugin_error(const struct stellar_session *session, enum error_event_type event, void **pme) -{ - if (strcmp(stellar_session_get_name(session), "TCP") == 0) - { - struct tcp_session_pme **per_tcp_session_pme = (struct tcp_session_pme **)pme; - printf("RUN custom_event_plugin_error, session_name: 'TCP', error_event_type: %d, pme->data: %s\n", event, (*per_tcp_session_pme) == NULL ? "NULL" : (*per_tcp_session_pme)->data); - tcp_session_pme_destory(*per_tcp_session_pme); - } - - if (strcmp(stellar_session_get_name(session), "CUSTOM") == 0) - { - struct custom_session_pme **per_custom_session_pme = (struct custom_session_pme **)pme; - printf("RUN custom_event_plugin_error, session_name: 'CUSTOM', error_event_type: %d, pme->data: %s\n", event, (*per_custom_session_pme) == NULL ? "NULL" : (*per_custom_session_pme)->data); - custom_session_pme_destory(*per_custom_session_pme); - } - - if (strcmp(stellar_session_get_name(session), "HTTP") == 0) - { - struct http_session_pme **per_http_session_pme = (struct http_session_pme **)pme; - printf("RUN custom_event_plugin_error, session_name: 'HTTP', error_event_type: %d, pme->data: %s\n", event, (*per_http_session_pme) == NULL ? "NULL" : (*per_http_session_pme)->data); - http_session_pme_destory(*per_http_session_pme); - } -} - extern "C" void custom_event_plugin_tcp_entry(const struct stellar_session *session, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) { struct tcp_session_pme **per_tcp_session_pme = (struct tcp_session_pme **)pme; diff --git a/src/plugin_manager/test/test_plugins/plugins_library/http_event_plugin.cpp b/src/plugin_manager/test/test_plugins/plugins_library/http_event_plugin.cpp index 487d6bf..939ef34 100644 --- a/src/plugin_manager/test/test_plugins/plugins_library/http_event_plugin.cpp +++ b/src/plugin_manager/test/test_plugins/plugins_library/http_event_plugin.cpp @@ -30,16 +30,6 @@ static void http_session_pme_destory(struct http_session_pme *pme) } } -extern "C" void http_event_plugin_error(const struct stellar_session *session, enum error_event_type event, void **pme) -{ - if (strcmp(stellar_session_get_name(session), "HTTP") == 0) - { - struct http_session_pme **per_http_session_pme = (struct http_session_pme **)pme; - printf("RUN http_event_plugin_error, session_name: 'HTTP', error_event_type: %d, pme->data: %s\n", event, (*per_http_session_pme) == NULL ? "NULL" : (*per_http_session_pme)->data); - http_session_pme_destory(*per_http_session_pme); - } -} - extern "C" void http_event_plugin_entry(const struct stellar_session *session, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) { struct http_session_pme **per_http_session_pme = (struct http_session_pme **)pme; @@ -72,7 +62,7 @@ extern "C" void http_event_plugin_entry(const struct stellar_session *session, e { // TODO (*per_http_session_pme)->flags = SESSION_EVENT_ORDPKT; - pm_session_dettach_others(session); + pm_session_take_over(session); } if (event & SESSION_EVENT_META) diff --git a/src/protocol_decoder/http/http.cpp b/src/protocol_decoder/http/http.cpp index c7e3e79..cb8f0d9 100644 --- a/src/protocol_decoder/http/http.cpp +++ b/src/protocol_decoder/http/http.cpp @@ -9,8 +9,4 @@ void http_decoder(const struct stellar_session *s, enum session_event_type event session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info); session_manager_trigger_event(new_session, SESSION_EVENT_META, info); -} - -void http_error_cb(const struct stellar_session *s, enum error_event_type event, void **pme) -{ } \ No newline at end of file From 9df6bf07af341a2878363302408102a6856c6bbc Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Tue, 9 Aug 2022 16:00:06 +0800 Subject: [PATCH 17/17] Modify the implementation of the plugin manager take over A plugin that is taken over, if the plugin was called before being taken over and has a registered SESSION_EVENT_CLOSING event, it will be called again when the SESSION_EVENT_CLOSING event comes. Otherwise, the plugin will not be called. --- readme.md | 14 +++++++--- sdk/include/plugin.h | 14 +++++++--- src/plugin_manager/plugin_manager.cpp | 28 +++++++++++--------- src/plugin_manager/plugin_manager_config.cpp | 4 +++ src/plugin_manager/plugin_manager_util.cpp | 17 ++++++------ src/plugin_manager/plugin_manager_util.h | 9 +++++-- 6 files changed, 55 insertions(+), 31 deletions(-) diff --git a/readme.md b/readme.md index b6d1023..e889e1d 100644 --- a/readme.md +++ b/readme.md @@ -48,12 +48,18 @@ Plugin Management APIs pm_session_dettach_me(session); /* - * The current plugin takes over the current session, the pm_session_take_over setting flag disables other plugins, + * The current plugin(cb2) takes over the current session, the pm_session_take_over setting flag disables other plugins, * and the current session does not call other plugins except for the SESSION_EVENT_CLOSING event. * - * The current plugin may be taken over while the session is open (SESSION_EVENT_OPENING), - * other plugins event_cb is not called, the session pme on other plugins is NULL, - * When the session is closed (SESSION_EVENT_CLOSING), call other plugin even_cb to destroy pme must check if pme is NULL. + * +-----+ +-----+ +-----+ +-----+ + * Plugin runtime callback list: | cb1 |-->| cb2 |-->| cb3 |-->| cb4 | + * +-----+ +-----+ +-----+ +-----+ + * /|\ + * | + * plugin cb2 run pm_session_take_over + * + * A plugin(cb1/cb3/cb4) that is taken over, if the plugin was called before being taken over and has a registered SESSION_EVENT_CLOSING event, + * it will be called again when the SESSION_EVENT_CLOSING event comes. Otherwise, the plugin will not be called. */ pm_session_take_over(session); ``` diff --git a/sdk/include/plugin.h b/sdk/include/plugin.h index 301363a..7042bbe 100644 --- a/sdk/include/plugin.h +++ b/sdk/include/plugin.h @@ -22,12 +22,18 @@ typedef void plugin_exit_callback(void); void pm_session_dettach_me(const struct stellar_session *session); /* - * The current plugin takes over the current session, the pm_session_take_over setting flag disables other plugins, + * The current plugin(cb2) takes over the current session, the pm_session_take_over setting flag disables other plugins, * and the current session does not call other plugins except for the SESSION_EVENT_CLOSING event. * - * The current plugin may be taken over while the session is open (SESSION_EVENT_OPENING), - * other plugins event_cb is not called, the session pme on other plugins is NULL, - * When the session is closed (SESSION_EVENT_CLOSING), call other plugin even_cb to destroy pme must check if pme is NULL. + * +-----+ +-----+ +-----+ +-----+ + * Plugin runtime callback list: | cb1 |-->| cb2 |-->| cb3 |-->| cb4 | + * +-----+ +-----+ +-----+ +-----+ + * /|\ + * | + * plugin cb2 run pm_session_take_over + * + * A plugin(cb1/cb3/cb4) that is taken over, if the plugin was called before being taken over and has a registered SESSION_EVENT_CLOSING event, + * it will be called again when the SESSION_EVENT_CLOSING event comes. Otherwise, the plugin will not be called. */ void pm_session_take_over(const struct stellar_session *session); diff --git a/src/plugin_manager/plugin_manager.cpp b/src/plugin_manager/plugin_manager.cpp index d90122c..88aae1e 100644 --- a/src/plugin_manager/plugin_manager.cpp +++ b/src/plugin_manager/plugin_manager.cpp @@ -19,11 +19,12 @@ enum plugin_status struct callback_runtime { - enum plugin_status status; void *cb_args; + fn_session_event_callback *event_cb; enum session_event_type event; - fn_session_event_callback *event_cb; + enum plugin_status status; + int is_be_called; }; struct session_plugin_ctx @@ -95,6 +96,7 @@ static struct session_plugin_ctx *plugin_manager_create_plugin_ctx(struct plugin for (int i = 0; i < plug_ctx->callback_num; i++) { + plug_ctx->callbacks[i].is_be_called = 0; plug_ctx->callbacks[i].status = PLUGIN_STATUS_NORMAL; plug_ctx->callbacks[i].event = elem->callbacks[i].event; plug_ctx->callbacks[i].event_cb = elem->callbacks[i].event_cb; @@ -435,12 +437,7 @@ void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_eve } else if (runtime->status == PLUGIN_STATUS_TAKEN_OVER) { - /* - * This plugin may be taken over when the session is open (SESSION_EVENT_OPENING), - * and the pme may be NULL when event_cb() is run to free memory with the session is closed (SESSION_EVENT_CLOSING). - * So, The plugin must check whether the pme is NULL before freeing memory. - */ - if (event_type & SESSION_EVENT_CLOSING) + if ((event_type & SESSION_EVENT_CLOSING) && (runtime->event & SESSION_EVENT_CLOSING) && runtime->is_be_called) { plug_ctx->callback_index = i; plugin_manager_log(DEBUG, "dispatch, run event_cb: %p, plugin status: 'taken over', session: %s, event: (%d, %s)", runtime->event_cb, session_name, event_type, event_str_buffer); @@ -459,6 +456,7 @@ void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_eve plug_ctx->callback_index = i; plugin_manager_log(DEBUG, "dispatch, run event_cb: %p, plugin status: 'normal', session: %s, event: (%d, %s)", runtime->event_cb, session_name, event_type, event_str_buffer); runtime->event_cb(seesion, event_type, packet, payload, payload_len, &runtime->cb_args); + runtime->is_be_called = 1; } else { @@ -499,12 +497,18 @@ void pm_session_dettach_me(const struct stellar_session *session) } /* - * The current plugin takes over the current session, the pm_session_take_over setting flag disables other plugins, + * The current plugin(cb2) takes over the current session, the pm_session_take_over setting flag disables other plugins, * and the current session does not call other plugins except for the SESSION_EVENT_CLOSING event. * - * The current plugin may be taken over while the session is open (SESSION_EVENT_OPENING), - * other plugins event_cb is not called, the session pme on other plugins is NULL, - * When the session is closed (SESSION_EVENT_CLOSING), call other plugin even_cb to destroy pme must check if pme is NULL + * +-----+ +-----+ +-----+ +-----+ + * Plugin runtime callback list: | cb1 |-->| cb2 |-->| cb3 |-->| cb4 | + * +-----+ +-----+ +-----+ +-----+ + * /|\ + * | + * plugin cb2 run pm_session_take_over + * + * A plugin(cb1/cb3/cb4) that is taken over, if the plugin was called before being taken over and has a registered SESSION_EVENT_CLOSING event, + * it will be called again when the SESSION_EVENT_CLOSING event comes. Otherwise, the plugin will not be called. */ void pm_session_take_over(const struct stellar_session *session) { diff --git a/src/plugin_manager/plugin_manager_config.cpp b/src/plugin_manager/plugin_manager_config.cpp index 1772900..b73fb8d 100644 --- a/src/plugin_manager/plugin_manager_config.cpp +++ b/src/plugin_manager/plugin_manager_config.cpp @@ -149,6 +149,10 @@ static int toml_parse_session_section(toml_table_t *root, struct plugin_manager_ (config->session_section[config->session_section_num].event) = (enum session_event_type)((config->session_section[config->session_section_num].event) | type_int); safe_free(type_str.u.s); } + if ((config->session_section[config->session_section_num].event & SESSION_EVENT_CLOSING) == 0) + { + plugin_manager_log(WARN, "can't find 'SESSION_EVENT_CLOSING' value for 'SESSION_EVENT_TYPE' configuration item in '[SESSION_NAME.%s]' section of %s", session_name, file); + } config->session_section_num++; } diff --git a/src/plugin_manager/plugin_manager_util.cpp b/src/plugin_manager/plugin_manager_util.cpp index 08f903f..43de099 100644 --- a/src/plugin_manager/plugin_manager_util.cpp +++ b/src/plugin_manager/plugin_manager_util.cpp @@ -29,15 +29,14 @@ struct event_type_map enum session_event_type type_int; }; -static struct event_type_map evtype_map[] = - { - {"SESSION_EVENT_UNKNOWN", SESSION_EVENT_UNKNOWN}, - {"SESSION_EVENT_OPENING", SESSION_EVENT_OPENING}, - {"SESSION_EVENT_RAWPKT", SESSION_EVENT_RAWPKT}, - {"SESSION_EVENT_ORDPKT", SESSION_EVENT_ORDPKT}, - {"SESSION_EVENT_META", SESSION_EVENT_META}, - {"SESSION_EVENT_CLOSING", SESSION_EVENT_CLOSING}, - {"SESSION_EVENT_ALL", SESSION_EVENT_ALL}, +static struct event_type_map evtype_map[] = { + {"SESSION_EVENT_UNKNOWN", SESSION_EVENT_UNKNOWN}, + {"SESSION_EVENT_OPENING", SESSION_EVENT_OPENING}, + {"SESSION_EVENT_RAWPKT", SESSION_EVENT_RAWPKT}, + {"SESSION_EVENT_ORDPKT", SESSION_EVENT_ORDPKT}, + {"SESSION_EVENT_META", SESSION_EVENT_META}, + {"SESSION_EVENT_CLOSING", SESSION_EVENT_CLOSING}, + {"SESSION_EVENT_ALL", SESSION_EVENT_ALL}, }; enum session_event_type session_event_type_str2int(const char *evtype_str) diff --git a/src/plugin_manager/plugin_manager_util.h b/src/plugin_manager/plugin_manager_util.h index 13b85db..890e61d 100644 --- a/src/plugin_manager/plugin_manager_util.h +++ b/src/plugin_manager/plugin_manager_util.h @@ -38,8 +38,9 @@ char *safe_dup(const char *str); enum plugin_manager_log_level { DEBUG = 0x11, - INFO = 0x12, - ERROR = 0x13, + WARN = 0x12, + INFO = 0x13, + ERROR = 0x14, }; #ifndef plugin_manager_log @@ -51,6 +52,10 @@ enum plugin_manager_log_level fprintf(stdout, "PLUGIN_MANAGER [DEBUG] " format "\n", ##__VA_ARGS__); \ fflush(stdout); \ break; \ + case WARN: \ + fprintf(stdout, "PLUGIN_MANAGER [WARN] " format "\n", ##__VA_ARGS__); \ + fflush(stdout); \ + break; \ case INFO: \ fprintf(stdout, "PLUGIN_MANAGER [INFO] " format "\n", ##__VA_ARGS__); \ fflush(stdout); \