forked from CESNET/ipfixprobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_plugin.sh
executable file
·240 lines (197 loc) · 6 KB
/
create_plugin.sh
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/bin/bash
echo "Enter new plugin name (will be converted to lowercase): "
read PLUGIN
echo "Enter your name and email address (format: NAME SURNAME <EMAIL-ADDRESS>): "
read AUTHOR
PLUGIN="$(tr '[:upper:]' '[:lower:]' <<<"$PLUGIN")"
PLUGIN_UPPER="$(tr '[:lower:]' '[:upper:]' <<<"$PLUGIN")"
# Usage: print_basic_info <FILE-EXTENSION>
print_basic_info() {
echo "/**
* \\file ${PLUGIN}.${1}
* \\brief Plugin for parsing ${PLUGIN} traffic.
* \\author ${AUTHOR}
* \\date $(date +%Y)
*/"
}
print_license() {
echo "/*
* Copyright (C) $(date +%Y) CESNET
*
* LICENSE TERMS
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of the Company nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
*
*
*/
"
}
print_hpp_code() {
echo "#ifndef IPXP_PROCESS_${PLUGIN_UPPER}_HPP
#define IPXP_PROCESS_${PLUGIN_UPPER}_HPP
#include <cstring>
#ifdef WITH_NEMEA
#include \"fields.h\"
#endif
#include <ipfixprobe/process.hpp>
#include <ipfixprobe/flowifc.hpp>
#include <ipfixprobe/packet.hpp>
#include <ipfixprobe/ipfix-elements.hpp>
namespace ipxp {
#define ${PLUGIN_UPPER}_UNIREC_TEMPLATE \"\" /* TODO: unirec template */
UR_FIELDS (
/* TODO: unirec fields definition */
)
/**
* \\brief Flow record extension header for storing parsed ${PLUGIN_UPPER} data.
*/
struct RecordExt${PLUGIN_UPPER} : public RecordExt {
static int REGISTERED_ID;
RecordExt${PLUGIN_UPPER}() : RecordExt(REGISTERED_ID)
{
}
#ifdef WITH_NEMEA
void fill_unirec(ur_template_t *tmplt, void *record) override
{
}
const char *get_unirec_tmplt() const
{
return ${PLUGIN_UPPER}_UNIREC_TEMPLATE;
}
#endif
int fill_ipfix(uint8_t *buffer, int size) override
{
return 0;
}
const char **get_ipfix_tmplt() const
{
static const char *ipfix_template[] = {
IPFIX_${PLUGIN_UPPER}_TEMPLATE(IPFIX_FIELD_NAMES)
NULL
};
return ipfix_template;
}
};
/**
* \\brief Process plugin for parsing ${PLUGIN_UPPER} packets.
*/
class ${PLUGIN_UPPER}Plugin : public ProcessPlugin
{
public:
${PLUGIN_UPPER}Plugin();
~${PLUGIN_UPPER}Plugin();
void init(const char *params);
void close();
OptionsParser *get_parser() const { return new OptionsParser(\"${PLUGIN}\", \"Parse ${PLUGIN_UPPER} traffic\"); }
std::string get_name() const { return \"${PLUGIN}\"; }
RecordExt *get_ext() const { return new RecordExt${PLUGIN_UPPER}(); }
ProcessPlugin *copy();
int pre_create(Packet &pkt);
int post_create(Flow &rec, const Packet &pkt);
int pre_update(Flow &rec, Packet &pkt);
int post_update(Flow &rec, const Packet &pkt);
void pre_export(Flow &rec);
};
}
#endif /* IPXP_PROCESS_${PLUGIN_UPPER}_HPP */
"
}
print_cpp_code() {
echo "#include <iostream>
#include \"${PLUGIN}.hpp\"
namespace ipxp {
int RecordExt${PLUGIN_UPPER}::REGISTERED_ID = -1;
__attribute__((constructor)) static void register_this_plugin()
{
static PluginRecord rec = PluginRecord(\"${PLUGIN}\", [](){return new ${PLUGIN_UPPER}Plugin();});
register_plugin(&rec);
RecordExt${PLUGIN_UPPER}::REGISTERED_ID = register_extension();
}
${PLUGIN_UPPER}Plugin::${PLUGIN_UPPER}Plugin()
{
}
${PLUGIN_UPPER}Plugin::~${PLUGIN_UPPER}Plugin()
{
}
void ${PLUGIN_UPPER}Plugin::init(const char *params)
{
}
void ${PLUGIN_UPPER}Plugin::close()
{
}
ProcessPlugin *${PLUGIN_UPPER}Plugin::copy()
{
return new ${PLUGIN_UPPER}Plugin(*this);
}
int ${PLUGIN_UPPER}Plugin::pre_create(Packet &pkt)
{
return 0;
}
int ${PLUGIN_UPPER}Plugin::post_create(Flow &rec, const Packet &pkt)
{
return 0;
}
int ${PLUGIN_UPPER}Plugin::pre_update(Flow &rec, Packet &pkt)
{
return 0;
}
int ${PLUGIN_UPPER}Plugin::post_update(Flow &rec, const Packet &pkt)
{
return 0;
}
void ${PLUGIN_UPPER}Plugin::pre_export(Flow &rec)
{
}
}
"
}
print_todo() {
echo "Generated ${PLUGIN}.cpp and ${PLUGIN}.hpp files"
echo
echo "TODO:"
echo "1) Add '${PLUGIN}.hpp' and '${PLUGIN}.cpp' files to ipfixprobe_process_src variable in Makefile.am"
echo "2) Do the main work in ${PLUGIN}.cpp and ${PLUGIN}.hpp files - implement pre_create, post_create, pre_update, post_update and pre_export functions (also read and understand when these functions are called, info in ipfixprobe/process.hpp file)"
echo "3.1) Add unirec fields to the UR_FIELDS and ${PLUGIN_UPPER}_UNIREC_TEMPLATE macro in ${PLUGIN}.hpp"
echo "3.2) Add IPFIX template macro 'IPFIX_${PLUGIN_UPPER}_TEMPLATE' to ipfixprobe/ipfix-elements.hpp"
echo "3.3) Define IPFIX fields"
echo "3.4) Write function 'fill_ipfix' in ${PLUGIN}.hpp to fill fields to IPFIX message"
echo "3.5) Write function 'fill_unirec' in ${PLUGIN}.hpp to fill fields to UNIREC message"
echo "4) Update README.md"
echo "5) Be happy with your new awesome ${PLUGIN} plugin!"
echo
echo "Optional work:"
echo "1) Add pcap traffic sample for ${PLUGIN} plugin to pcaps directory"
echo "2) Add test for ${PLUGIN} to tests directory"
echo
echo "NOTE: If you didn't modify pre_create, post_create, pre_update, post_update, pre_export functions, please remove them from ${PLUGIN}.cpp and ${PLUGIN}.hpp"
}
create_hpp_file() {
FILE="${PLUGIN}.hpp"
echo "Creating ${FILE} file..."
print_basic_info hpp >"${FILE}"
print_license >>"${FILE}"
print_hpp_code >>"${FILE}"
}
create_cpp_file() {
FILE="${PLUGIN}.cpp"
echo "Creating ${FILE} file..."
print_basic_info cpp >"${FILE}"
print_license >>"${FILE}"
print_cpp_code >>"${FILE}"
}
create_hpp_file
create_cpp_file
echo
print_todo