2022-07-28 15:12:46 +08:00
|
|
|
/*
|
|
|
|
|
**********************************************************************************************
|
|
|
|
|
* File: main.cpp
|
|
|
|
|
* Description: stellar main entry
|
|
|
|
|
*
|
|
|
|
|
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
|
|
|
|
|
***********************************************************************************************
|
|
|
|
|
*/
|
2022-07-26 15:05:14 +08:00
|
|
|
|
2022-07-24 23:59:00 +08:00
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <unistd.h>
|
2022-08-12 17:23:27 +08:00
|
|
|
#include <netinet/ether.h>
|
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#include <arpa/inet.h>
|
2022-07-06 23:18:13 +08:00
|
|
|
|
2022-08-11 09:38:38 +08:00
|
|
|
#include "global_var.h"
|
|
|
|
|
#include "logger.h"
|
2022-07-06 23:18:13 +08:00
|
|
|
#include "packet_io.h"
|
2022-08-12 17:23:27 +08:00
|
|
|
#include "packet_io_util.h"
|
2022-07-06 23:18:13 +08:00
|
|
|
#include "session_manager.h"
|
|
|
|
|
#include "plugin_manager.h"
|
|
|
|
|
#include "http.h"
|
2022-08-12 17:23:27 +08:00
|
|
|
#include "util_errors.h"
|
2022-07-06 23:18:13 +08:00
|
|
|
|
2022-08-26 17:33:03 +08:00
|
|
|
struct worker_thread_ctx
|
2022-07-06 23:18:13 +08:00
|
|
|
{
|
2022-08-26 17:33:03 +08:00
|
|
|
pthread_t tid;
|
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;
|
2022-08-26 17:33:03 +08:00
|
|
|
struct plugin_manager *plugin_mgr;
|
|
|
|
|
int thread_id;
|
2022-07-06 23:18:13 +08:00
|
|
|
};
|
|
|
|
|
|
2022-08-26 17:33:03 +08:00
|
|
|
void *worker_thread_cycle(void *arg)
|
2022-07-06 23:18:13 +08:00
|
|
|
{
|
2022-07-20 20:20:31 +08:00
|
|
|
struct stellar_packet *rx_pkt;
|
2022-08-25 18:13:46 +08:00
|
|
|
struct stellar_session *session;
|
2022-08-26 17:33:03 +08:00
|
|
|
struct worker_thread_ctx *thread_arg = (struct worker_thread_ctx *)arg;
|
|
|
|
|
while (1)
|
2022-07-06 23:18:13 +08:00
|
|
|
{
|
2022-08-26 17:33:03 +08:00
|
|
|
if (packet_io_device_rx(thread_arg->dev, thread_arg->thread_id, &rx_pkt, 1) > 0)
|
2022-07-06 23:18:13 +08:00
|
|
|
{
|
2022-08-26 17:33:03 +08:00
|
|
|
session = session_manager_commit(thread_arg->session_mgr, rx_pkt, thread_arg->thread_id);
|
|
|
|
|
while (session)
|
2022-07-06 23:18:13 +08:00
|
|
|
{
|
2022-08-26 17:33:03 +08:00
|
|
|
plugin_manager_dispatch(thread_arg->plugin_mgr, session, thread_arg->thread_id);
|
|
|
|
|
session = session_manager_fetch_session(thread_arg->session_mgr, session, thread_arg->thread_id);
|
2022-07-06 23:18:13 +08:00
|
|
|
}
|
2022-08-26 17:33:03 +08:00
|
|
|
// clean session_manager event queue
|
|
|
|
|
packet_io_device_tx(thread_arg->dev, thread_arg->thread_id, &rx_pkt, 1);
|
2022-07-06 23:18:13 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-08-09 15:58:46 +08:00
|
|
|
printf("no fetch num\n");
|
2022-08-26 17:33:03 +08:00
|
|
|
// dispatch to time event
|
2022-07-06 23:18:13 +08:00
|
|
|
|
2022-08-26 17:33:03 +08:00
|
|
|
// dispatch to trigger polling event
|
2022-07-06 23:18:13 +08:00
|
|
|
}
|
2022-08-12 17:23:27 +08:00
|
|
|
#endif
|
2022-07-06 23:18:13 +08:00
|
|
|
}
|
2022-07-24 23:59:00 +08:00
|
|
|
return nullptr;
|
2022-07-06 23:18:13 +08:00
|
|
|
}
|
|
|
|
|
|
2022-08-26 17:33:03 +08:00
|
|
|
struct packet_io_device *packet_io_init(const char *instance_name, const enum packet_io_run_mode mode, const int thread_num)
|
2022-07-06 23:18:13 +08:00
|
|
|
{
|
2022-08-10 10:20:12 +08:00
|
|
|
struct packet_io_instance *ppio_inst = packet_io_instance_create(instance_name, mode);
|
2022-08-26 17:33:03 +08:00
|
|
|
if (nullptr == ppio_inst)
|
|
|
|
|
{
|
2022-07-26 15:05:14 +08:00
|
|
|
log_error(ST_ERR_PIO_INSTANCE, "packet_io instance init failed.");
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 17:33:03 +08:00
|
|
|
struct packet_io_device *ppio_dev = packet_io_device_open(ppio_inst, "eth1", thread_num, thread_num);
|
|
|
|
|
if (nullptr == ppio_dev)
|
|
|
|
|
{
|
2022-07-26 15:05:14 +08:00
|
|
|
log_error(ST_ERR_PIO_DEVICE, "packet_io device open failed.");
|
|
|
|
|
}
|
2022-07-28 20:30:48 +08:00
|
|
|
return ppio_dev;
|
2022-07-06 23:18:13 +08:00
|
|
|
}
|
|
|
|
|
|
2022-08-26 17:33:03 +08:00
|
|
|
int main(int argc, char **argv)
|
2022-07-06 23:18:13 +08:00
|
|
|
{
|
2022-08-26 17:33:03 +08:00
|
|
|
int thread_num = 1;
|
|
|
|
|
char file_path[] = "./plugs/plugins.inf";
|
2022-07-26 15:05:14 +08:00
|
|
|
|
2022-08-26 17:33:03 +08:00
|
|
|
struct packet_io_device *dev = packet_io_init("stellar", PACKET_IO_RUN_MODE_PCAP_LIVE, thread_num);
|
|
|
|
|
struct session_manager *session_mgr = session_manager_create(thread_num);
|
|
|
|
|
struct plugin_manager *plugin_mgr = plugin_manager_create(thread_num);
|
2022-07-06 23:18:13 +08:00
|
|
|
|
2022-08-26 17:33:03 +08:00
|
|
|
plugin_manager_register(plugin_mgr, "HTTP", SESSION_STATE_ALL, http_entry);
|
|
|
|
|
plugin_manager_load(plugin_mgr, file_path);
|
2022-07-28 15:12:46 +08:00
|
|
|
|
2022-08-26 17:33:03 +08:00
|
|
|
struct worker_thread_ctx *workers = (struct worker_thread_ctx *)calloc(sizeof(struct worker_thread_ctx), thread_num);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < thread_num; i++)
|
|
|
|
|
{
|
|
|
|
|
workers[i].dev = dev;
|
|
|
|
|
workers[i].session_mgr = session_mgr;
|
|
|
|
|
workers[i].plugin_mgr = plugin_mgr;
|
|
|
|
|
workers[i].thread_id = i;
|
|
|
|
|
pthread_create(&workers[i].tid, nullptr, worker_thread_cycle, (void *)&workers[i]);
|
|
|
|
|
}
|
2022-07-06 23:18:13 +08:00
|
|
|
|
2022-07-24 23:59:00 +08:00
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
/* main loop code */
|
|
|
|
|
usleep(1);
|
|
|
|
|
}
|
2022-07-27 15:51:07 +08:00
|
|
|
|
2022-08-26 17:33:03 +08:00
|
|
|
plugin_manager_unload(plugin_mgr);
|
|
|
|
|
plugin_manager_destory(plugin_mgr);
|
|
|
|
|
|
2022-07-06 23:18:13 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|