/
msgmod_plugin.h
327 lines (276 loc) · 9.7 KB
/
msgmod_plugin.h
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* Copyright (c) 2008, 2009, 2013, 2015 by Farsight Security, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NMSG_MSGMOD_PLUGIN_H
#define NMSG_MSGMOD_PLUGIN_H
#include <nmsg.h>
#include <protobuf-c/protobuf-c.h>
#include <nmsg/nmsg.pb-c.h>
struct nmsg_msgmod_field;
/** Version number of the nmsg msgmod ABI. */
#define NMSG_MSGMOD_VERSION 9
/** \see nmsg_msgmod_init() */
typedef nmsg_res (*nmsg_msgmod_init_fp)(void **clos);
/** \see nmsg_msgmod_fini() */
typedef nmsg_res (*nmsg_msgmod_fini_fp)(void **clos);
/** \see nmsg_msgmod_payload_to_pres() */
typedef nmsg_res (*nmsg_msgmod_payload_to_pres_fp)(Nmsg__NmsgPayload *np,
char **pres,
const char *endline);
/** \see nmsg_msgmod_ipdg_to_payload() */
typedef nmsg_res (*nmsg_msgmod_ipdg_to_payload_fp)(void *clos,
const struct nmsg_ipdg *dg,
uint8_t **pbuf, size_t *sz);
typedef nmsg_res (*nmsg_msgmod_pkt_to_payload_fp)(void *clos,
nmsg_pcap_t pcap,
nmsg_message_t *m);
typedef nmsg_res (*nmsg_msgmod_pcap_init_fp)(void *clos, nmsg_pcap_t pcap);
/** Per-message load function. */
typedef nmsg_res (*nmsg_msgmod_msg_load_fp)(nmsg_message_t m, void **msg_clos);
/** Per-message finalization function. */
typedef nmsg_res (*nmsg_msgmod_msg_fini_fp)(nmsg_message_t m, void *msg_clos);
/** Custom field printer function. */
typedef nmsg_res (*nmsg_msgmod_field_print_fp)(nmsg_message_t m,
struct nmsg_msgmod_field *field,
void *ptr,
struct nmsg_strbuf *sb,
const char *endline);
/** Custom field accessor function. */
typedef nmsg_res (*nmsg_msgmod_field_get_fp)(nmsg_message_t m,
struct nmsg_msgmod_field *field,
unsigned val_idx,
void **data,
size_t *len,
void *msg_clos);
/** Custom field formatter function. */
typedef nmsg_res (*nmsg_msgmod_field_format_fp)(nmsg_message_t m,
struct nmsg_msgmod_field *field,
void *ptr,
struct nmsg_strbuf *sb,
const char *endline);
/** Custom field parser function. */
typedef nmsg_res (*nmsg_msgmod_field_parse_fp)(nmsg_message_t m,
struct nmsg_msgmod_field *field,
const char *value,
void **ptr,
size_t *len,
const char *endline);
/** Convenience macro. */
#define NMSG_MSGMOD_FIELD_PRINTER(funcname) \
nmsg_res funcname(nmsg_message_t m, \
struct nmsg_msgmod_field *field, \
void *ptr, \
struct nmsg_strbuf *sb, \
const char *endline)
/** Convenience macro. */
#define NMSG_MSGMOD_FIELD_GETTER(funcname) \
nmsg_res funcname(nmsg_message_t m, \
struct nmsg_msgmod_field *field, \
unsigned val_idx, \
void **data, \
size_t *len, \
void *msg_clos)
/** Convenience macro. */
#define NMSG_MSGMOD_FIELD_FORMATTER(funcname) \
nmsg_res funcname(nmsg_message_t m, \
struct nmsg_msgmod_field *field, \
void *ptr, \
struct nmsg_strbuf *sb, \
const char *endline)
/** Convenience macro. */
#define NMSG_MSGMOD_FIELD_PARSER(funcname) \
nmsg_res funcname(nmsg_message_t m, \
struct nmsg_msgmod_field *field, \
const char *value, \
void **ptr, \
size_t *len, \
const char *endline)
/** Convenience macro. */
#define NMSG_MSGMOD_REQUIRED_INIT \
.msgver = NMSG_MSGMOD_VERSION, \
.protobuf_c_version_number = PROTOBUF_C_VERSION_NUMBER
/**
* Structure mapping protocol buffer schema fields to nmsg_msgmod_field_type
* values for "transparent" modules.
*
* In order to map a protocol buffer schema into a transparent message module
* the module must export (in a struct nmsg_msgmod) an array of these
* structures indicating the intended nmsg field types of each field.
*
* For presentation output, you may implement either the print or the format
* functions. The print function produces human-readable output that does not
* need to be reversible.
*
* The format function is intended for use by serializable and deserializable
* io formats (such as JSON) and must be reversible by the parse function.
* It will be used for presentation mode formatting as well if a print
* function is not implemented.
*/
struct nmsg_msgmod_field {
/** Intended (nmsg) type of this protobuf field. */
nmsg_msgmod_field_type type;
/** Name of the field. */
const char *name;
/** Flags for this field. */
unsigned flags;
/** Optional custom field printer function. */
nmsg_msgmod_field_print_fp print;
/** Optional custom field getter function. */
nmsg_msgmod_field_get_fp get;
/** \private, must be initialized to NULL. */
const ProtobufCFieldDescriptor *descr;
/* Optional custom field formatter function. */
nmsg_msgmod_field_format_fp format;
/* Optional custom field parser function. */
nmsg_msgmod_field_parse_fp parse;
/** \private Reserved fields. */
void *_reserved1;
void *_reserved0;
};
/** Element ending a struct nmsg_msgmod_field array. */
#define NMSG_MSGMOD_FIELD_END { 0, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
/**
* Type of message module.
*
* libnmsg provides a "transparent" type of module for module developers that
* requires only a simple structure to provide glue for a "simple" protocol
* buffers schema (in particular, a transparent module message type schema
* can only use fundamental protobuf data types and cannot embed other message
* definitions). libnmsg will use generic functions to encode and decode the
* message fields.
*
* "Opaque" modules must provide functions to encode and decode the message
* payload.
*/
typedef enum {
nmsg_msgmod_type_transparent,
nmsg_msgmod_type_opaque
} nmsg_msgmod_type;
/**
* Structure exported by message modules to implement a new message type.
*
* A module developer may choose to make a module "transparent" or "opaque" by
* setting the type field to the appropriate value and setting certain fields
* and leaving other fields unset. The transparent module interface is intended
* for modules that do not implement IP datagram parsing and whose structure
* can be restricted (in particular, a transparent module message type cannot
* embed other message types). A transparent module developer must provide a
* mapping between protobuf field types and nmsg msgmod field types and generic
* functions will be provided to convert to and from presentation form.
*/
struct nmsg_msgmod_plugin {
/**
* Module interface version.
* Must be set to #NMSG_MSGMOD_VERSION or the
* module will be rejected at load time.
*/
int msgver;
/**
* Module type.
*/
nmsg_msgmod_type type;
/**
* Vendor ID and name.
* Must always be <b>set</b>.
*/
struct nmsg_idname vendor;
/**
* Message type and name.
* Must always be <b>set</b>.
*/
struct nmsg_idname msgtype;
/**
* Module initialization function. May be NULL.
*/
nmsg_msgmod_init_fp init;
/**
* Module finalization function. May be NULL.
*/
nmsg_msgmod_fini_fp fini;
/**
* Per-message load function.
* This function is called when loading serialized messages, but not
* when initializing messages from scratch.
*/
nmsg_msgmod_msg_load_fp msg_load;
/**
* Per-message finalization function.
*/
nmsg_msgmod_msg_fini_fp msg_fini;
/**
* Module function to convert protobuf payloads to presentation form.
* May be <b>set</b>.
*
* If not set for transparent modules, a generic function will be used.
* If not set for opaque modules, an error will be returned to the
* caller.
*/
nmsg_msgmod_payload_to_pres_fp payload_to_pres;
/**
* Module function to convert reassembled IP datagrams to NMSG
* payloads.
* Must be <b>unset</b> for transparent modules.
* May be <b>set</b> for opaque modules.
*/
nmsg_msgmod_ipdg_to_payload_fp ipdg_to_payload;
/**
* Pointer to the ProtobufCMessageDescriptor for the protocol buffer
* schema. This is generated by the protobuf-c compiler and usually ends
* in "__descriptor".
* Must be <b>set</b> for transparent modules.
* Must be <b>unset</b> for opaque modules.
*/
const ProtobufCMessageDescriptor *pbdescr;
/**
* Array mapping protobuf fields to nmsg types.
* Must be <b>set</b> for transparent modules.
* Must be <b>unset</b> for opaque modules.
*/
struct nmsg_msgmod_field *fields;
/**
* Module function to convert raw IP packets to NMSG payloads.
* Must be <b>unset</b> for transparent modules.
* May be <b>set</b> for opaque modules.
* If set, the ipdg_to_payload field will be ignored.
*/
nmsg_msgmod_pkt_to_payload_fp pkt_to_payload;
/**
* protobuf-c version number. Compared at runtime to avoid mismatches
* between libnmsg and the message module. This is the
* PROTOBUF_C_VERSION_NUMBER value used to compile the message module.
*
* This field is only checked if .type is nmsg_msgmod_type_transparent.
*/
uint32_t protobuf_c_version_number;
/**
* Optional module function to perform further initialization of
* pcap inputs (e.g., setting up custom filters).
*/
nmsg_msgmod_pcap_init_fp pcap_init;
/**
* \private Reserved fields.
*/
void *_reserved9;
void *_reserved8;
void *_reserved7;
void *_reserved6;
void *_reserved5;
void *_reserved4;
void *_reserved3;
void *_reserved2;
void *_reserved1;
void *_reserved0;
};
#endif /* NMSG_MSGMOD_PLUGIN_H */