summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 13cf682b4104038a9c652ed8800752bfaa5d3277 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
**********************************************************************************************
*	File: main.cpp
*   Description: stellar main entry
*
*   Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
***********************************************************************************************
*/

#include <limits.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <netinet/ether.h>
#include <netinet/ip.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/syscall.h>

#include "logger.h"
#include "packet_io.h"
#include "packet_io_util.h"
#include "session_manager.h"
#include "plugin_manager.h"
#include "http.h"
#include "utils.h"
#include "util_errors.h"
#include "packet_io_internal.h"

struct worker_thread_ctx
{
	pthread_t tid;
	struct packet_io_device *dev;
	struct session_manager *session_mgr;
	struct plugin_manager *plugin_mgr;
	int thread_id;
};

void *worker_thread_cycle(void *arg)
{
	struct stellar_packet *rx_pkt;
	struct stellar_session *session;
	struct worker_thread_ctx *thread_arg = (struct worker_thread_ctx *)arg;
	while (1)
	{
		if (packet_io_device_rx(thread_arg->dev, thread_arg->thread_id, &rx_pkt, 1) > 0)
		{
			session = session_manager_commit(thread_arg->session_mgr, rx_pkt, thread_arg->thread_id);
			while (session)
			{
				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);
			}
			// clean session_manager event queue
			packet_io_device_tx(thread_arg->dev, thread_arg->thread_id, &rx_pkt, 1);
		}
		else
		{
			printf("no fetch num\n");
			// dispatch to time event
			// dispatch to trigger polling event
		}
#endif
		}
	}
	return nullptr;
}

struct packet_io_device *packet_io_init(const char *instance_name, const enum packet_io_run_mode mode, const int thread_num)
{
	struct packet_io_instance *ppio_inst = packet_io_instance_create(instance_name, mode);
	if (nullptr == ppio_inst)
	{
		log_error(ST_ERR_PIO_INSTANCE, "packet_io instance init failed.");
		return nullptr;
	}

	struct packet_io_device *ppio_dev = packet_io_device_open(ppio_inst, "eth1", thread_num, thread_num);
	if (nullptr == ppio_dev)
	{
		log_error(ST_ERR_PIO_DEVICE, "packet_io device open failed.");
	}
	return ppio_dev;
}

int main(int argc, char **argv)
{
	int thread_num = 1;
	char file_path[] = "./plugs/plugins.inf";

	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);

	plugin_manager_register(plugin_mgr, "HTTP", SESSION_STATE_ALL, http_entry);
	plugin_manager_load(plugin_mgr, file_path);

	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]);
	}

	while (1)
	{
		/* main loop code */
		usleep(1);
	}

	plugin_manager_unload(plugin_mgr);
	plugin_manager_destory(plugin_mgr);

	return 0;
}