falco_plugin 0.5.1

High level bindings for the Falco plugin API
Documentation
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use crate::plugin::error::ffi_result::FfiResult;
use crate::plugin::exported_tables::entry::table_metadata::traits::TableMetadata;
use crate::plugin::exported_tables::entry::traits::Entry;
use crate::plugin::exported_tables::field_descriptor::FieldDescriptor;
use crate::plugin::exported_tables::table::{Table, TableEntryType};
use crate::plugin::tables::data::{FieldTypeId, Key};
use falco_plugin_api::{
    ss_plugin_bool, ss_plugin_rc, ss_plugin_rc_SS_PLUGIN_FAILURE, ss_plugin_rc_SS_PLUGIN_SUCCESS,
    ss_plugin_state_data, ss_plugin_state_type, ss_plugin_table_entry_t, ss_plugin_table_field_t,
    ss_plugin_table_fieldinfo, ss_plugin_table_fields_vtable_ext, ss_plugin_table_iterator_func_t,
    ss_plugin_table_iterator_state_t, ss_plugin_table_reader_vtable_ext, ss_plugin_table_t,
    ss_plugin_table_writer_vtable_ext,
};
use num_traits::FromPrimitive;
use std::borrow::Borrow;
use std::ffi::{c_char, CStr};

// SAFETY: `table` must be a valid pointer to Table<K,E>
unsafe extern "C-unwind" fn get_table_name<K, E>(table: *mut ss_plugin_table_t) -> *const c_char
where
    K: Key + Ord,
    K: Borrow<<K as Key>::Borrowed>,
    <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
    E: Entry,
    E::Metadata: TableMetadata,
{
    unsafe {
        let Some(table) = (table as *mut Table<K, E>).as_mut() else {
            return std::ptr::null_mut();
        };
        table.name().as_ptr()
    }
}

// SAFETY: `table` must be a valid pointer to Table<K,E>
unsafe extern "C-unwind" fn get_table_size<K, E>(table: *mut ss_plugin_table_t) -> u64
where
    K: Key + Ord,
    K: Borrow<<K as Key>::Borrowed>,
    <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
    E: Entry,
    E::Metadata: TableMetadata,
{
    unsafe {
        let Some(table) = (table as *mut Table<K, E>).as_mut() else {
            return 0;
        };
        table.size() as u64
    }
}

// SAFETY: `table` must be a valid pointer to Table<K,E>
// SAFETY: `key` must be a valid pointer to ss_plugin_state_data
unsafe extern "C-unwind" fn get_table_entry<K, E>(
    table: *mut ss_plugin_table_t,
    key: *const ss_plugin_state_data,
) -> *mut ss_plugin_table_entry_t
where
    K: Key + Ord,
    K: Borrow<<K as Key>::Borrowed>,
    <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
    E: Entry,
    E::Metadata: TableMetadata,
{
    unsafe {
        let Some(table) = (table as *mut Table<K, E>).as_mut() else {
            return std::ptr::null_mut();
        };
        let Some(key) = key.as_ref() else {
            return std::ptr::null_mut();
        };

        let key = K::from_data(key);
        match table.lookup(key) {
            Some(entry) => Box::into_raw(Box::new(entry)) as *mut _,
            None => std::ptr::null_mut(),
        }
    }
}

// SAFETY: all pointers must be valid
unsafe extern "C-unwind" fn read_entry_field<K, E>(
    table: *mut ss_plugin_table_t,
    entry: *mut ss_plugin_table_entry_t,
    field: *const ss_plugin_table_field_t,
    out: *mut ss_plugin_state_data,
) -> ss_plugin_rc
where
    K: Key + Ord,
    K: Borrow<<K as Key>::Borrowed>,
    <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
    E: Entry,
    E::Metadata: TableMetadata,
{
    unsafe {
        let Some(table) = (table as *mut Table<K, E>).as_mut() else {
            return ss_plugin_rc_SS_PLUGIN_FAILURE;
        };
        let Some(entry) = (entry as *mut TableEntryType<E>).as_mut() else {
            return ss_plugin_rc_SS_PLUGIN_FAILURE;
        };
        let Some(field) = (field as *const FieldDescriptor).as_ref() else {
            return ss_plugin_rc_SS_PLUGIN_FAILURE;
        };
        let Some(out) = out.as_mut() else {
            return ss_plugin_rc_SS_PLUGIN_FAILURE;
        };

        table.get_field_value(entry, field, out).status_code()
    }
}

// SAFETY: all pointers must be valid
unsafe extern "C-unwind" fn release_table_entry<E>(
    _table: *mut ss_plugin_table_t,
    entry: *mut ss_plugin_table_entry_t,
) where
    E: Entry,
    E::Metadata: TableMetadata,
{
    if !entry.is_null() {
        unsafe {
            drop(Box::from_raw(entry as *mut TableEntryType<E>));
        }
    }
}

// SAFETY: all pointers must be valid
unsafe extern "C-unwind" fn iterate_entries<K, E>(
    table: *mut ss_plugin_table_t,
    func: ss_plugin_table_iterator_func_t,
    state: *mut ss_plugin_table_iterator_state_t,
) -> ss_plugin_bool
where
    K: Key + Ord,
    K: Borrow<<K as Key>::Borrowed>,
    <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
    E: Entry,
    E::Metadata: TableMetadata,
{
    let Some(func) = func else {
        return 0;
    };
    unsafe {
        let Some(table) = (table as *mut Table<K, E>).as_mut() else {
            return 0;
        };

        table.iterate_entries(|e| {
            let entry = e as *mut _ as *mut ss_plugin_table_entry_t;
            func(state, entry) != 0
        });
    }

    1
}

// SAFETY: `table` must be a valid pointer to Table<K,E>
unsafe extern "C-unwind" fn clear_table<K, E>(table: *mut ss_plugin_table_t) -> ss_plugin_rc
where
    K: Key + Ord,
    K: Borrow<<K as Key>::Borrowed>,
    <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
    E: Entry,
    E::Metadata: TableMetadata,
{
    unsafe {
        let Some(table) = (table as *mut Table<K, E>).as_mut() else {
            return ss_plugin_rc_SS_PLUGIN_FAILURE;
        };
        table.clear();
    }
    ss_plugin_rc_SS_PLUGIN_SUCCESS
}

// SAFETY: all pointers must be valid
unsafe extern "C-unwind" fn erase_table_entry<K, E>(
    table: *mut ss_plugin_table_t,
    key: *const ss_plugin_state_data,
) -> ss_plugin_rc
where
    K: Key + Ord,
    K: Borrow<<K as Key>::Borrowed>,
    <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
    E: Entry,
    E::Metadata: TableMetadata,
{
    unsafe {
        let Some(table) = (table as *mut Table<K, E>).as_mut() else {
            return ss_plugin_rc_SS_PLUGIN_FAILURE;
        };
        let Some(key) = key.as_ref() else {
            return ss_plugin_rc_SS_PLUGIN_FAILURE;
        };
        let key = K::from_data(key);
        match table.erase(key) {
            None => ss_plugin_rc_SS_PLUGIN_FAILURE,
            Some(_) => ss_plugin_rc_SS_PLUGIN_SUCCESS,
        }
    }
}

// SAFETY: `table` must be a valid pointer to Table<K,E>
unsafe extern "C-unwind" fn create_table_entry<K, E>(
    table: *mut ss_plugin_table_t,
) -> *mut ss_plugin_table_entry_t
where
    K: Key + Ord,
    K: Borrow<<K as Key>::Borrowed>,
    <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
    E: Entry,
    E::Metadata: TableMetadata,
{
    unsafe {
        let Some(table) = (table as *mut Table<K, E>).as_mut() else {
            return std::ptr::null_mut();
        };

        match table.create_entry() {
            Ok(e) => Box::into_raw(Box::new(e)).cast(),
            Err(_) => std::ptr::null_mut(), // todo report error
        }
    }
}

// SAFETY: all pointers must be valid
unsafe extern "C-unwind" fn add_table_entry<K, E>(
    table: *mut ss_plugin_table_t,
    key: *const ss_plugin_state_data,
    entry: *mut ss_plugin_table_entry_t,
) -> *mut ss_plugin_table_entry_t
where
    K: Key + Ord,
    K: Borrow<<K as Key>::Borrowed>,
    <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
    E: Entry,
    E::Metadata: TableMetadata,
{
    if entry.is_null() {
        return std::ptr::null_mut();
    }

    unsafe {
        let Some(table) = (table as *mut Table<K, E>).as_mut() else {
            return std::ptr::null_mut();
        };
        let Some(key) = key.as_ref() else {
            return std::ptr::null_mut();
        };
        let key = K::from_data(key);
        let entry = Box::from_raw(entry as *mut TableEntryType<E>);
        let entry = table.insert(key, *entry);

        entry
            .map(|e| Box::into_raw(Box::new(e)).cast())
            .unwrap_or_else(std::ptr::null_mut)
    }
}

// SAFETY: all pointers must be valid
unsafe extern "C-unwind" fn write_entry_field<K, E>(
    table: *mut ss_plugin_table_t,
    entry: *mut ss_plugin_table_entry_t,
    field: *const ss_plugin_table_field_t,
    value: *const ss_plugin_state_data,
) -> ss_plugin_rc
where
    K: Key + Ord,
    K: Borrow<<K as Key>::Borrowed>,
    <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
    E: Entry,
    E::Metadata: TableMetadata,
{
    unsafe {
        let Some(table) = (table as *mut Table<K, E>).as_mut() else {
            return ss_plugin_rc_SS_PLUGIN_FAILURE;
        };
        let Some(entry) = (entry as *mut TableEntryType<E>).as_mut() else {
            return ss_plugin_rc_SS_PLUGIN_FAILURE;
        };
        let Some(field) = (field as *const FieldDescriptor).as_ref() else {
            return ss_plugin_rc_SS_PLUGIN_FAILURE;
        };
        let Some(value) = value.as_ref() else {
            return ss_plugin_rc_SS_PLUGIN_FAILURE;
        };
        table.write(entry, field, value).status_code()
    }
}

// SAFETY: all pointers must be valid
unsafe extern "C-unwind" fn list_table_fields<K, E>(
    table: *mut ss_plugin_table_t,
    nfields: *mut u32,
) -> *const ss_plugin_table_fieldinfo
where
    K: Key + Ord,
    K: Borrow<<K as Key>::Borrowed>,
    <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
    E: Entry,
    E::Metadata: TableMetadata,
{
    unsafe {
        let Some(table) = (table as *mut Table<K, E>).as_mut() else {
            return std::ptr::null_mut();
        };
        let fields = table.list_fields();
        *nfields = fields.len() as u32;
        fields.as_ptr()
    }
}

// SAFETY: all pointers must be valid
unsafe extern "C-unwind" fn get_table_field<K, E>(
    table: *mut ss_plugin_table_t,
    name: *const c_char,
    data_type: ss_plugin_state_type,
) -> *mut ss_plugin_table_field_t
where
    K: Key + Ord,
    K: Borrow<<K as Key>::Borrowed>,
    <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
    E: Entry,
    E::Metadata: TableMetadata,
{
    unsafe {
        let Some(table) = (table as *mut Table<K, E>).as_mut() else {
            return std::ptr::null_mut();
        };
        let Some(data_type) = FieldTypeId::from_usize(data_type as usize) else {
            return std::ptr::null_mut();
        };
        let name = if name.is_null() {
            return std::ptr::null_mut();
        } else {
            CStr::from_ptr(name)
        };
        match table.get_field(name, data_type) {
            Some(field) => field.as_ref() as *const _ as *mut _,
            None => std::ptr::null_mut(),
        }
    }
}

// SAFETY: all pointers must be valid
unsafe extern "C-unwind" fn add_table_field<K, E>(
    table: *mut ss_plugin_table_t,
    name: *const c_char,
    data_type: ss_plugin_state_type,
) -> *mut ss_plugin_table_field_t
where
    K: Key + Ord,
    K: Borrow<<K as Key>::Borrowed>,
    <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
    E: Entry,
    E::Metadata: TableMetadata,
{
    unsafe {
        let Some(table) = (table as *mut Table<K, E>).as_mut() else {
            return std::ptr::null_mut();
        };
        let Some(data_type) = FieldTypeId::from_usize(data_type as usize) else {
            return std::ptr::null_mut();
        };
        let name = if name.is_null() {
            return std::ptr::null_mut();
        } else {
            CStr::from_ptr(name)
        };
        match table.add_field(name, data_type, false) {
            Some(field) => field.as_ref() as *const _ as *mut _,
            None => std::ptr::null_mut(),
        }
    }
}

pub(crate) fn reader_vtable<K, E>() -> ss_plugin_table_reader_vtable_ext
where
    K: Key + Ord,
    K: Borrow<<K as Key>::Borrowed>,
    <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
    E: Entry,
    E::Metadata: TableMetadata,
{
    ss_plugin_table_reader_vtable_ext {
        get_table_name: Some(get_table_name::<K, E>),
        get_table_size: Some(get_table_size::<K, E>),
        get_table_entry: Some(get_table_entry::<K, E>),
        read_entry_field: Some(read_entry_field::<K, E>),
        release_table_entry: Some(release_table_entry::<E>),
        iterate_entries: Some(iterate_entries::<K, E>),
    }
}

pub(crate) fn writer_vtable<K, E>() -> ss_plugin_table_writer_vtable_ext
where
    K: Key + Ord,
    K: Borrow<<K as Key>::Borrowed>,
    <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
    E: Entry,
    E::Metadata: TableMetadata,
{
    ss_plugin_table_writer_vtable_ext {
        clear_table: Some(clear_table::<K, E>),
        erase_table_entry: Some(erase_table_entry::<K, E>),
        create_table_entry: Some(create_table_entry::<K, E>),
        destroy_table_entry: Some(release_table_entry::<E>), // same as release_table_entry
        add_table_entry: Some(add_table_entry::<K, E>),
        write_entry_field: Some(write_entry_field::<K, E>),
    }
}

pub(crate) fn fields_vtable<K, E>() -> ss_plugin_table_fields_vtable_ext
where
    K: Key + Ord,
    K: Borrow<<K as Key>::Borrowed>,
    <K as Key>::Borrowed: Ord + ToOwned<Owned = K>,
    E: Entry,
    E::Metadata: TableMetadata,
{
    ss_plugin_table_fields_vtable_ext {
        list_table_fields: Some(list_table_fields::<K, E>),
        get_table_field: Some(get_table_field::<K, E>),
        add_table_field: Some(add_table_field::<K, E>),
    }
}