Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bindgen: use bindgen to provide Rust bindings to C - v8 #12466

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

jasonish
Copy link
Member

@jasonish jasonish commented Jan 23, 2025

A simpler, more focused version of #12461.

  • Straight to suricata_sys crate, remove intermediate commits.
  • Only export app-layer-protos.h and update Rust code to use the generated bindings.
  • Don't both sorting out other circular dependencies.
  • Code changes to use AppProto could have been more more minimal by re-exporting from core, but I don't like the idea of add re-exports.

Follow Rust convention of using a "sys" crate for bindings to C
functions. The bindings don't exist yet, but will be generated by
bindgen and put into this crate.
Bindgen works by processing a header file which includes all other
header files it should generate bindings for. For this I've created
bindgen.h which just includes app-layer-protos.h for now as an
example.

These bindings are then generated and saved in the "suricata-sys"
crate and become availale as "suricata_sys::sys".
Have bindgen generate bindings for app-layer-protos.h, then use the
generated definitions of AppProto/AppProtoEnum instead if defining
them ourselves.

This header was chosen as its used by Rust, and its a simple header
with no circular dependencies.
Comment on lines +1 to +7
#[repr(u32)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum AppProtoEnum {
ALPROTO_UNKNOWN = 0,
ALPROTO_FAILED = 1,
ALPROTO_HTTP1 = 2,
ALPROTO_FTP = 3,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also do:

pub const AppProtoEnum_ALPROTO_UNKNOWN: AppProtoEnum = 0;
pub const AppProtoEnum_ALPROTO_FAILED: AppProtoEnum = 1;
pub const AppProtoEnum_ALPROTO_HTTP1: AppProtoEnum = 2;
pub const AppProtoEnum_ALPROTO_FTP: AppProtoEnum = 3;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it right to have a rust AppProtoEnum ? when we want dynamic AppProto cf #12420 and rejected https://redmine.openinfosecfoundation.org/issues/3524 ?

Copy link

codecov bot commented Jan 23, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.64%. Comparing base (5d9fad5) to head (770cd3c).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #12466   +/-   ##
=======================================
  Coverage   80.63%   80.64%           
=======================================
  Files         920      920           
  Lines      258739   258739           
=======================================
+ Hits       208643   208648    +5     
+ Misses      50096    50091    -5     
Flag Coverage Δ
fuzzcorpus 56.84% <100.00%> (+0.02%) ⬆️
livemode 19.40% <0.00%> (ø)
pcap 44.32% <100.00%> (+0.04%) ⬆️
suricata-verify 63.23% <100.00%> (-0.01%) ⬇️
unittests 58.52% <33.33%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

@suricata-qa
Copy link

Information: QA ran without warnings.

Pipeline 24340

}
pub type AppProto = u16;
extern "C" {
#[doc = " \\brief Maps the ALPROTO_*, to its string equivalent.\n\n \\param alproto App layer protocol id.\n\n \\retval String equivalent for the alproto."]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: We do not need to export the comment I guess

@@ -79,14 +79,49 @@ clean-local:
distclean-local:
rm -rf vendor dist

check-bindgen-bindings:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a CI test with bindgen

pub const ALPROTO_UNKNOWN : AppProto = 0;
pub const ALPROTO_FAILED : AppProto = 1;
pub const ALPROTO_UNKNOWN : AppProto = AppProtoEnum::ALPROTO_UNKNOWN as AppProto;
pub const ALPROTO_FAILED : AppProto = AppProtoEnum::ALPROTO_FAILED as AppProto;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh AppProtoEnum. as AppProto...

@catenacyber
Copy link
Contributor

This looks nice overall.

What is the remaining work to do ?

  • list all C headers that should get into bindgen ?
  • add some pure rust stuff into suricata-sys crate ? like AppLayerTxData (including the JsonBuilder C API but feel-like-rust)
  • Add CI test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants