summaryrefslogtreecommitdiff
path: root/demo5/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'demo5/main.c')
-rw-r--r--demo5/main.c137
1 files changed, 137 insertions, 0 deletions
diff --git a/demo5/main.c b/demo5/main.c
new file mode 100644
index 0000000..64aac7b
--- /dev/null
+++ b/demo5/main.c
@@ -0,0 +1,137 @@
+#include "maat.h"
+#include "stdio.h"
+#include <arpa/inet.h>
+#include <assert.h>
+#include <stddef.h>
+#include <string.h>
+#include <stdlib.h> // Include the header file for 'calloc'
+
+#define ARRAY_SIZE 16
+#define ALLOC(type, number) ((type *)calloc(sizeof(type), number))
+
+const char *json_filename = "/root/Git/demo/demo5/wgw_scan.json";
+const char *table_info_path = "/root/Git/demo/demo5/table_info_wgw_scan.conf";
+
+struct dyn_ue_id_ip
+{
+ int id;
+ int addr_type;
+ char ip[40];
+ char ue_id[11];
+ int is_valid;
+};
+
+void ip_plugin_ex_new_cb(const char *table_name, int table_id, const char *key, const char *table_line, void **ad,
+ long argl, void *argp)
+{
+ int *counter = (int *)argp;
+
+ char op_time[20] = {0};
+
+ struct dyn_ue_id_ip *ud = ALLOC(struct dyn_ue_id_ip, 1);
+
+ int ret = sscanf(table_line, "%d\t%d\t%10s\t%39s\t%d\t%20s", &(ud->id), &(ud->addr_type), ud->ip, ud->ue_id,
+ &(ud->is_valid), &op_time);
+
+ *ad = ud;
+ (*counter)++;
+}
+
+void ip_plugin_ex_free_cb(int table_id, void **ad, long argl, void *argp)
+{
+ struct dyn_ue_id_ip *ud = (struct dyn_ue_id_ip *)(*ad);
+
+ memset(ud, 0, sizeof(struct dyn_ue_id_ip));
+ free(ud);
+ *ad = NULL;
+}
+
+void ip_plugin_ex_dup_cb(int table_id, void **to, void **from, long argl, void *argp)
+{
+ struct dyn_ue_id_ip *ud = (struct dyn_ue_id_ip *)(*from);
+
+ *to = ud;
+}
+
+int main()
+{
+ /* initialize maat options which will be used by maat_new() */
+ struct maat_options *opts = maat_options_new();
+ maat_options_set_json_file(opts, json_filename);
+ maat_options_set_logger(opts, "/root/Git/demo/demo5/sample_test.log", LOG_LEVEL_TRACE);
+
+ /* create maat instance, rules in table_info.conf will be loaded. */
+ struct maat *maat_instance = maat_new(opts, table_info_path);
+ assert(maat_instance != NULL);
+ maat_options_free(opts);
+
+ const char *table_name = "UE_ID"; // ่™šๆ‹Ÿ่กจ 5
+ int table_id = maat_get_table_id(maat_instance, table_name);
+ assert(table_id == 5); /* defined in table_info.conf */
+
+ int thread_id = 0;
+ long long results[ARRAY_SIZE] = {0};
+ size_t n_hit_result = 0;
+ int ret = 0;
+
+ /* store scanning intermediate state */
+ struct maat_state *state = maat_state_new(maat_instance, thread_id);
+ assert(state != NULL);
+
+ const char *ue_id = "abcdefghij";
+
+ ret = maat_scan_string(maat_instance, table_id, ue_id, strlen(ue_id), results, ARRAY_SIZE, &n_hit_result, state);
+ assert(ret == MAAT_SCAN_HIT);
+ assert(n_hit_result == 1);
+ assert(results[0] == 301);
+
+ maat_state_free(state);
+
+ struct ip_addr ipv4_src;
+ ipv4_src.ip_type = 4;
+ ret = inet_pton(AF_INET, "20.20.20.20", &ipv4_src.ipv4);
+
+ struct maat_state *state2 = maat_state_new(maat_instance, thread_id);
+ assert(state2 != NULL);
+
+ uint32_t a = ipv4_src.ipv4;
+ ret = maat_scan_ipv4_port(maat_instance, 6, a, 443, results, ARRAY_SIZE, &n_hit_result, state2);
+ assert(ret == MAAT_SCAN_HALF_HIT);
+ assert(n_hit_result == 0);
+
+ struct ip_addr ipv4_dst;
+ ipv4_dst.ip_type = 4;
+ ret = inet_pton(AF_INET, "8.8.8.8", &ipv4_dst.ipv4);
+
+ uint32_t b = ipv4_dst.ipv4;
+ ret = maat_scan_ipv4_port(maat_instance, 7, b, 443, results, ARRAY_SIZE, &n_hit_result, state2);
+ assert(ret == MAAT_SCAN_HIT);
+ assert(n_hit_result == 1);
+ assert(results[0] == 302);
+
+ maat_state_free(state2);
+
+ const char *table_name_ueidip = "WANNAT_DYN_UE_ID_IP"; /* maat_json.json has TEST_IP_PLUGIN_WITH_EXDATA rule */
+ int table_id_ueidip = maat_get_table_id(maat_instance, table_name_ueidip);
+ assert(table_id_ueidip == 8); /* defined in table_info.conf */
+
+ int plugin_ex_data_counter = 0;
+ ret = maat_plugin_table_ex_schema_register(maat_instance, table_name_ueidip, ip_plugin_ex_new_cb, ip_plugin_ex_free_cb,
+ ip_plugin_ex_dup_cb, 0, &plugin_ex_data_counter);
+
+ assert(ret == 0);
+ assert(plugin_ex_data_counter == 2);
+
+ uint32_t ipv4_addr1;
+ ret = inet_pton(AF_INET, "10.0.0.1", &ipv4_addr1);
+ assert(ret == 1);
+
+ struct dyn_ue_id_ip *ud = NULL;
+ ud = (struct dyn_ue_id_ip *)maat_plugin_table_get_ex_data(maat_instance, table_id_ueidip, (char *)&ipv4_addr1,
+ sizeof(ipv4_addr1));
+
+ assert(ud != NULL);
+ assert(ud->id == 1);
+
+ return 0;
+}