Skip to content

Commit

Permalink
rust/bindings: add jb_ funcs to bindings
Browse files Browse the repository at this point in the history
This is an example of how we can use cbindgen -> C for "C" style
functions written in Rust, and then back to Rust with bindgen. The idea
is to build one Rust module that contains all the C bindings, whether or
not the functions are written in Rust or C.
  • Loading branch information
jasonish committed Jan 22, 2025
1 parent 007870d commit fd1da84
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 13 deletions.
3 changes: 2 additions & 1 deletion rust/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ if HAVE_BINDGEN
--allowlist-type 'AppProto' \
--allowlist-type 'AppProtoEnum' \
--rustified-enum 'AppProtoEnum' \
--allowlist-function 'jb_.*' \
$(abs_top_srcdir)/src/bindgen.h \
-- \
-DHAVE_CONFIG_H -I../src $(CPPFLAGS)
-DHAVE_CONFIG_H -I../src -I../rust/gen $(CPPFLAGS)
else
@echo "error: bindgen not installed, can't update bindings"
exit 1
Expand Down
152 changes: 140 additions & 12 deletions rust/src/_sys.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
#[repr(u32)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum SCAppLayerEventType {
APP_LAYER_EVENT_TYPE_TRANSACTION = 1,
APP_LAYER_EVENT_TYPE_PACKET = 2,
}
pub type SCAppLayerStateGetEventInfoByIdFn = ::std::option::Option<
unsafe extern "C" fn(
event_id: u8,
event_name: *mut *const ::std::os::raw::c_char,
event_type: *mut SCAppLayerEventType,
) -> ::std::os::raw::c_int,
>;
#[repr(u32)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum AppProtoEnum {
ALPROTO_UNKNOWN = 0,
ALPROTO_FAILED = 1,
Expand Down Expand Up @@ -43,16 +56,131 @@ pub enum AppProtoEnum {
ALPROTO_MAX_STATIC = 39,
}
pub type AppProto = u16;
#[repr(u32)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum SCAppLayerEventType {
APP_LAYER_EVENT_TYPE_TRANSACTION = 1,
APP_LAYER_EVENT_TYPE_PACKET = 2,
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct JsonBuilder {
_unused: [u8; 0],
}
#[doc = " A \"mark\" or saved state for a JsonBuilder object.\n\n The name is full, and the types are u64 as this object is used\n directly in C as well."]
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct JsonBuilderMark {
pub position: u64,
pub state_index: u64,
pub state: u64,
}
#[allow(clippy::unnecessary_operation, clippy::identity_op)]
const _: () = {
["Size of JsonBuilderMark"][::std::mem::size_of::<JsonBuilderMark>() - 24usize];
["Alignment of JsonBuilderMark"][::std::mem::align_of::<JsonBuilderMark>() - 8usize];
["Offset of field: JsonBuilderMark::position"]
[::std::mem::offset_of!(JsonBuilderMark, position) - 0usize];
["Offset of field: JsonBuilderMark::state_index"]
[::std::mem::offset_of!(JsonBuilderMark, state_index) - 8usize];
["Offset of field: JsonBuilderMark::state"]
[::std::mem::offset_of!(JsonBuilderMark, state) - 16usize];
};
unsafe extern "C" {
pub fn jb_new_object() -> *mut JsonBuilder;
}
unsafe extern "C" {
pub fn jb_new_array() -> *mut JsonBuilder;
}
unsafe extern "C" {
pub fn jb_clone(js: *mut JsonBuilder) -> *mut JsonBuilder;
}
unsafe extern "C" {
pub fn jb_free(js: *mut JsonBuilder);
}
unsafe extern "C" {
pub fn jb_capacity(jb: *mut JsonBuilder) -> usize;
}
unsafe extern "C" {
pub fn jb_reset(jb: *mut JsonBuilder);
}
unsafe extern "C" {
pub fn jb_open_object(js: *mut JsonBuilder, key: *const ::std::os::raw::c_char) -> bool;
}
unsafe extern "C" {
pub fn jb_start_object(js: *mut JsonBuilder) -> bool;
}
unsafe extern "C" {
pub fn jb_open_array(js: *mut JsonBuilder, key: *const ::std::os::raw::c_char) -> bool;
}
unsafe extern "C" {
pub fn jb_set_string(
js: *mut JsonBuilder, key: *const ::std::os::raw::c_char,
val: *const ::std::os::raw::c_char,
) -> bool;
}
unsafe extern "C" {
pub fn jb_set_string_from_bytes(
js: *mut JsonBuilder, key: *const ::std::os::raw::c_char, bytes: *const u8, len: u32,
) -> bool;
}
unsafe extern "C" {
pub fn jb_set_base64(
js: *mut JsonBuilder, key: *const ::std::os::raw::c_char, bytes: *const u8, len: u32,
) -> bool;
}
unsafe extern "C" {
pub fn jb_set_hex(
js: *mut JsonBuilder, key: *const ::std::os::raw::c_char, bytes: *const u8, len: u32,
) -> bool;
}
unsafe extern "C" {
pub fn jb_set_formatted(js: *mut JsonBuilder, formatted: *const ::std::os::raw::c_char)
-> bool;
}
unsafe extern "C" {
pub fn jb_append_object(jb: *mut JsonBuilder, obj: *const JsonBuilder) -> bool;
}
unsafe extern "C" {
pub fn jb_set_object(
js: *mut JsonBuilder, key: *const ::std::os::raw::c_char, val: *mut JsonBuilder,
) -> bool;
}
unsafe extern "C" {
pub fn jb_append_string(js: *mut JsonBuilder, val: *const ::std::os::raw::c_char) -> bool;
}
unsafe extern "C" {
pub fn jb_append_string_from_bytes(js: *mut JsonBuilder, bytes: *const u8, len: u32) -> bool;
}
unsafe extern "C" {
pub fn jb_append_base64(js: *mut JsonBuilder, bytes: *const u8, len: u32) -> bool;
}
unsafe extern "C" {
pub fn jb_append_uint(js: *mut JsonBuilder, val: u64) -> bool;
}
unsafe extern "C" {
pub fn jb_append_float(js: *mut JsonBuilder, val: f64) -> bool;
}
unsafe extern "C" {
pub fn jb_set_uint(js: *mut JsonBuilder, key: *const ::std::os::raw::c_char, val: u64) -> bool;
}
unsafe extern "C" {
pub fn jb_set_int(js: *mut JsonBuilder, key: *const ::std::os::raw::c_char, val: i64) -> bool;
}
unsafe extern "C" {
pub fn jb_set_float(js: *mut JsonBuilder, key: *const ::std::os::raw::c_char, val: f64)
-> bool;
}
unsafe extern "C" {
pub fn jb_set_bool(js: *mut JsonBuilder, key: *const ::std::os::raw::c_char, val: bool)
-> bool;
}
unsafe extern "C" {
pub fn jb_close(js: *mut JsonBuilder) -> bool;
}
unsafe extern "C" {
pub fn jb_len(js: *const JsonBuilder) -> usize;
}
unsafe extern "C" {
pub fn jb_ptr(js: *mut JsonBuilder) -> *const u8;
}
unsafe extern "C" {
pub fn jb_get_mark(js: *mut JsonBuilder, mark: *mut JsonBuilderMark);
}
unsafe extern "C" {
pub fn jb_restore_mark(js: *mut JsonBuilder, mark: *mut JsonBuilderMark) -> bool;
}
pub type SCAppLayerStateGetEventInfoByIdFn = ::std::option::Option<
unsafe extern "C" fn(
event_id: u8,
event_name: *mut *const ::std::os::raw::c_char,
event_type: *mut SCAppLayerEventType,
) -> ::std::os::raw::c_int,
>;
1 change: 1 addition & 0 deletions src/bindgen.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <stdint.h>
#include <stdbool.h>

#include "rust.h"
#include "app-layer-protos.h"
#include "app-layer-events.h"

Expand Down

0 comments on commit fd1da84

Please sign in to comment.