Repository navigation
Expand file tree
/
Copy pathpacket_plugin.c
More file actions
152 lines (135 loc) · 5.02 KB
/
Copy pathpacket_plugin.c
File metadata and controls
152 lines (135 loc) · 5.02 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "linkedlist.h"
#include "packet_plugin.h"
#include "logging.h"
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#define TRUE 1
#define FALSE 0
/* content of this list is PACKET_PLUGIN_INFO* */
static LIST_ELEMENT* g_packet_plugin_list;
/* We need to specify the order of plugins */
static LIST_ELEMENT* g_active_packet_plugin_list;
PACKET_PLUGIN* packet_plugin_rjv3_new();
PACKET_PLUGIN* packet_plugin_printer_new();
int init_packet_plugin_list() {
PACKET_PLUGIN* (*list[])() = {
//#include "packet_pluginl_list_gen.h" TODO autogen
packet_plugin_rjv3_new,
packet_plugin_printer_new
};
int i = 0;
for (; i < sizeof(list) / sizeof(PACKET_PLUGIN*); ++i) {
PACKET_PLUGIN* (*func)() = list[i];
insert_data(&g_packet_plugin_list, func());
}
return i;
}
static int plugin_name_cmp(void* to_find, void* curr) {
return memcmp(to_find, ((PACKET_PLUGIN*)curr)->name, strlen(to_find));
}
RESULT select_packet_plugin(const char* name) {
PACKET_PLUGIN* _info;
_info = (PACKET_PLUGIN*)lookup_data(g_packet_plugin_list, (void*)name, plugin_name_cmp);
if (_info != NULL) {
insert_data(&g_active_packet_plugin_list, _info);
return SUCCESS;
}
return FAILURE;
}
/*
* I know this is silly, but is there better way to do it
* since list_traverse takes none extra parameters?
* Even if it takes user-defined extra params, I'd make
* lots of useless structs to pass variable number of actual params
* via the extra param in list_traverse - it would take twice as many
* functions as current implementation. This is more unacceptable.
*/
#define PLUGIN ((PACKET_PLUGIN*)(plugin_info->content))
#define CHK_FUNC(func) \
if (func == NULL) continue;
void packet_plugin_destroy() {
LIST_ELEMENT *plugin_info = g_packet_plugin_list; // Different from below
if (g_packet_plugin_list == NULL) return;
do {
CHK_FUNC(PLUGIN->destroy);
PLUGIN->destroy(PLUGIN);
} while ((plugin_info = plugin_info->next));
list_destroy(&g_packet_plugin_list, FALSE);
list_destroy(&g_active_packet_plugin_list, FALSE);
}
RESULT packet_plugin_process_cmdline_opts(int argc, char* argv[]) {
LIST_ELEMENT *plugin_info = g_active_packet_plugin_list;
if (g_active_packet_plugin_list == NULL) return SUCCESS;
do {
CHK_FUNC(PLUGIN->process_cmdline_opts);
optind = 1; /* Reset the pointer to make getopt works from start */
if (PLUGIN->process_cmdline_opts(PLUGIN, argc, argv) == FAILURE)
return FAILURE;
} while ((plugin_info = plugin_info->next));
return SUCCESS;
}
RESULT packet_plugin_process_config_file(char* filepath) {
LIST_ELEMENT *plugin_info = g_active_packet_plugin_list;
if (g_active_packet_plugin_list == NULL) return SUCCESS;
do {
CHK_FUNC(PLUGIN->process_config_file);
if (PLUGIN->process_config_file(PLUGIN, filepath) == FAILURE)
return FAILURE;
} while ((plugin_info = plugin_info->next));
return SUCCESS;
}
RESULT packet_plugin_validate_params() {
LIST_ELEMENT *plugin_info = g_active_packet_plugin_list;
if (g_active_packet_plugin_list == NULL) return SUCCESS;
do {
CHK_FUNC(PLUGIN->validate_params);
if (PLUGIN->validate_params(PLUGIN) == FAILURE)
return FAILURE;
} while ((plugin_info = plugin_info->next));
return SUCCESS;
}
void packet_plugin_load_default_params() {
LIST_ELEMENT *plugin_info = g_active_packet_plugin_list;
if (g_active_packet_plugin_list == NULL) return;
do {
CHK_FUNC(PLUGIN->load_default_params);
PLUGIN->load_default_params(PLUGIN);
} while ((plugin_info = plugin_info->next));
}
void packet_plugin_print_cmdline_help() {
LIST_ELEMENT *plugin_info = g_active_packet_plugin_list;
if (g_active_packet_plugin_list == NULL) return;
do {
CHK_FUNC(PLUGIN->print_cmdline_help);
PLUGIN->print_cmdline_help(PLUGIN);
} while ((plugin_info = plugin_info->next));
}
RESULT packet_plugin_prepare_frame(ETH_EAP_FRAME* frame) {
LIST_ELEMENT *plugin_info = g_active_packet_plugin_list;
if (g_active_packet_plugin_list == NULL) return SUCCESS;
do {
CHK_FUNC(PLUGIN->prepare_frame);
if (PLUGIN->prepare_frame(PLUGIN, frame) == FAILURE)
return FAILURE;
} while ((plugin_info = plugin_info->next));
return SUCCESS;
}
RESULT packet_plugin_on_frame_received(ETH_EAP_FRAME* frame) {
LIST_ELEMENT *plugin_info = g_active_packet_plugin_list;
if (g_active_packet_plugin_list == NULL) return SUCCESS;
do {
CHK_FUNC(PLUGIN->on_frame_received);
if (PLUGIN->on_frame_received(PLUGIN, frame) == FAILURE)
return FAILURE;
} while ((plugin_info = plugin_info->next));
return SUCCESS;
}
void packet_plugin_set_auth_round(int round) {
LIST_ELEMENT *plugin_info = g_active_packet_plugin_list;
if (g_active_packet_plugin_list == NULL) return;
do {
CHK_FUNC(PLUGIN->set_auth_round);
PLUGIN->set_auth_round(PLUGIN, round);
} while ((plugin_info = plugin_info->next));
}