2022-07-24 23:59:00 +08:00
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2022-07-06 23:18:13 +08:00
|
|
|
#include "packet_io.h"
|
|
|
|
|
#include "session_manager.h"
|
|
|
|
|
#include "plugin_manager.h"
|
|
|
|
|
#include "http.h"
|
|
|
|
|
|
2022-07-20 20:20:31 +08:00
|
|
|
struct stellar_event_base_loop_arg
|
2022-07-06 23:18:13 +08:00
|
|
|
{
|
2022-07-14 14:46:16 +08:00
|
|
|
struct packet_io_device *dev;
|
2022-07-27 14:11:29 +08:00
|
|
|
struct session_manager *session_mgr;
|
|
|
|
|
struct plugin_manager *plug_mgr;
|
2022-07-24 23:59:00 +08:00
|
|
|
int tid;
|
2022-07-06 23:18:13 +08:00
|
|
|
};
|
|
|
|
|
|
2022-07-24 23:59:00 +08:00
|
|
|
void *stellar_event_base_loop(void *arg)
|
2022-07-06 23:18:13 +08:00
|
|
|
{
|
2022-07-20 20:20:31 +08:00
|
|
|
struct stellar_packet *rx_pkt;
|
|
|
|
|
struct stellar_event *event;
|
2022-07-24 23:59:00 +08:00
|
|
|
struct stellar_event_base_loop_arg *thread_arg = (struct stellar_event_base_loop_arg *)arg;
|
2022-07-06 23:18:13 +08:00
|
|
|
while(1)
|
|
|
|
|
{
|
2022-07-24 23:59:00 +08:00
|
|
|
int fetch_num = packet_io_rx(thread_arg->dev, thread_arg->tid, &rx_pkt, 1);
|
2022-07-06 23:18:13 +08:00
|
|
|
if(fetch_num > 0)
|
|
|
|
|
{
|
2022-07-24 23:59:00 +08:00
|
|
|
event = session_manager_commit(thread_arg->session_mgr, rx_pkt);
|
2022-07-06 23:18:13 +08:00
|
|
|
while(event)
|
|
|
|
|
{
|
2022-07-24 23:59:00 +08:00
|
|
|
plugin_manager_dispatch(thread_arg->plug_mgr ,event);
|
|
|
|
|
event = session_manager_fetch_event(thread_arg->session_mgr);
|
2022-07-06 23:18:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//clean session_manager event queue
|
2022-07-24 23:59:00 +08:00
|
|
|
packet_io_tx(thread_arg->dev, thread_arg->tid, &rx_pkt, 1);
|
2022-07-06 23:18:13 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//dispatch to time event
|
|
|
|
|
|
|
|
|
|
//dispatch to trigger polling event
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-24 23:59:00 +08:00
|
|
|
return nullptr;
|
2022-07-06 23:18:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-07-14 14:46:16 +08:00
|
|
|
struct packet_io_device *packet_io_init(int worker_thread_num, const char *instance_name, const char *device_name)
|
2022-07-06 23:18:13 +08:00
|
|
|
{
|
2022-07-14 14:46:16 +08:00
|
|
|
struct packet_io_instance * instance = packet_io_create(PACKET_IO_PCAP, instance_name);
|
|
|
|
|
struct packet_io_device *dev = packet_io_open_device(instance, device_name, worker_thread_num, worker_thread_num);
|
2022-07-06 23:18:13 +08:00
|
|
|
return dev;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char ** argv)
|
|
|
|
|
{
|
2022-07-24 23:59:00 +08:00
|
|
|
//manager_init
|
2022-07-27 14:11:29 +08:00
|
|
|
struct session_manager *session_mgr = session_manager_init();
|
2022-07-27 15:51:07 +08:00
|
|
|
struct plugin_manager *plug_mgr = plugin_manager_create();
|
|
|
|
|
|
|
|
|
|
// register build-in plugin
|
2022-08-03 19:46:43 +08:00
|
|
|
plugin_manager_register(plug_mgr, "HTTP", SESSION_EVENT_ALL, http_decoder, http_error_cb);
|
2022-07-06 23:18:13 +08:00
|
|
|
|
2022-07-24 23:59:00 +08:00
|
|
|
// load external plugins
|
2022-07-27 18:32:22 +08:00
|
|
|
char file_path[] = "./plugs/plugins.inf";
|
2022-08-03 19:46:43 +08:00
|
|
|
plugin_manager_load(plug_mgr, file_path);
|
2022-07-06 23:18:13 +08:00
|
|
|
|
2022-07-24 23:59:00 +08:00
|
|
|
//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);
|
2022-07-06 23:18:13 +08:00
|
|
|
//main_loop
|
2022-07-24 23:59:00 +08:00
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
/* main loop code */
|
|
|
|
|
usleep(1);
|
|
|
|
|
}
|
2022-07-27 15:51:07 +08:00
|
|
|
|
2022-07-27 18:32:22 +08:00
|
|
|
plugin_manager_unload(plug_mgr);
|
2022-07-27 15:51:07 +08:00
|
|
|
plugin_manager_destory(plug_mgr);
|
2022-07-24 23:59:00 +08:00
|
|
|
|
2022-07-06 23:18:13 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|