Skip to content

Commit

Permalink
rust: example use of bindgen for a C function
Browse files Browse the repository at this point in the history
Use bindgen to generate Rust bindings for "app-layer-ext.h", currently
just one function, SCAppLayerStateGetProgressFn.

We use bindgen as a build dependency and dynamically (re)build the
bindings as needed. The alternative would be to require bindgen-cli as
a build tool.

Ticket: OISF#7341
  • Loading branch information
jasonish committed Oct 23, 2024
1 parent 0a5ca35 commit 1e5ab1a
Show file tree
Hide file tree
Showing 31 changed files with 71 additions and 32 deletions.
9 changes: 9 additions & 0 deletions rust/Cargo.toml.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ license = "GPL-2.0-only"
description = "Suricata Rust components"
edition = "2021"
rust-version = "1.67.1"
build = "src/build.rs"

[workspace]
members = [".", "./derive"]
Expand Down Expand Up @@ -72,3 +73,11 @@ suricata-lua-sys = { version = "0.1.0-alpha.1" }
[dev-dependencies]
test-case = "~3.3.1"
hex = "~0.4.3"

[build-dependencies]
# Pin as bindgen 0.70.1 requires Rust 1.70.0+
bindgen = "=0.69.4"

# Most recent version to support Rust 1.67. 0.5.9 requires 1.70.0.
# - Not used directly but Suricata, but by bindgen.
home = "=0.5.5"
3 changes: 1 addition & 2 deletions rust/src/applayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ pub struct RustParser {
pub tx_comp_st_ts: c_int,
pub tx_comp_st_tc: c_int,
/// Function returning the current transaction progress
pub tx_get_progress: StateGetProgressFn,
pub tx_get_progress: crate::sys::SCAppLayerStateGetProgressFn,

/// Function to get an event id from a description
pub get_eventinfo: Option<GetEventInfoFn>,
Expand Down Expand Up @@ -443,7 +443,6 @@ pub type StateFreeFn = unsafe extern "C" fn (*mut c_void);
pub type StateTxFreeFn = unsafe extern "C" fn (*mut c_void, u64);
pub type StateGetTxFn = unsafe extern "C" fn (*mut c_void, u64) -> *mut c_void;
pub type StateGetTxCntFn = unsafe extern "C" fn (*mut c_void) -> u64;
pub type StateGetProgressFn = unsafe extern "C" fn (*mut c_void, u8) -> c_int;
pub type GetEventInfoFn = unsafe extern "C" fn (*const c_char, *mut c_int, *mut AppLayerEventType) -> c_int;
pub type GetEventInfoByIdFn = unsafe extern "C" fn (c_int, *mut *const c_char, *mut AppLayerEventType) -> i8;
pub type LocalStorageNewFn = extern "C" fn () -> *mut c_void;
Expand Down
2 changes: 1 addition & 1 deletion rust/src/applayertemplate/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ pub unsafe extern "C" fn rs_template_register_parser() {
get_tx: rs_template_state_get_tx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: rs_template_tx_get_alstate_progress,
tx_get_progress: Some(rs_template_tx_get_alstate_progress),
get_eventinfo: Some(TemplateEvent::get_event_info),
get_eventinfo_byid: Some(TemplateEvent::get_event_info_by_id),
localstorage_new: None,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/bittorrent_dht/bittorrent_dht.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ pub unsafe extern "C" fn rs_bittorrent_dht_udp_register_parser() {
get_tx: rs_bittorrent_dht_state_get_tx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: rs_bittorrent_dht_tx_get_alstate_progress,
tx_get_progress: Some(rs_bittorrent_dht_tx_get_alstate_progress),
get_eventinfo: Some(BitTorrentDHTEvent::get_event_info),
get_eventinfo_byid: Some(BitTorrentDHTEvent::get_event_info_by_id),
localstorage_new: None,
Expand Down
12 changes: 12 additions & 0 deletions rust/src/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
fn main() {
let bindings = bindgen::Builder::default()
.header("../src/app-layer-ext.h")
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.allowlist_item("SC.*")
.generate()
.unwrap();
let out_path = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.unwrap();
}
2 changes: 1 addition & 1 deletion rust/src/dcerpc/dcerpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,7 @@ pub unsafe extern "C" fn rs_dcerpc_register_parser() {
get_tx: rs_dcerpc_get_tx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: rs_dcerpc_get_alstate_progress,
tx_get_progress: Some(rs_dcerpc_get_alstate_progress),
get_eventinfo: None,
get_eventinfo_byid : None,
localstorage_new: None,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/dcerpc/dcerpc_udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ pub unsafe extern "C" fn rs_dcerpc_udp_register_parser() {
get_tx: rs_dcerpc_udp_get_tx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: rs_dcerpc_get_alstate_progress,
tx_get_progress: Some(rs_dcerpc_get_alstate_progress),
get_eventinfo: None,
get_eventinfo_byid: None,
localstorage_new: None,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/dhcp/dhcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ pub unsafe extern "C" fn rs_dhcp_register_parser() {
get_tx : rs_dhcp_state_get_tx,
tx_comp_st_ts : 1,
tx_comp_st_tc : 1,
tx_get_progress : rs_dhcp_tx_get_alstate_progress,
tx_get_progress : Some(rs_dhcp_tx_get_alstate_progress),
get_eventinfo : Some(DHCPEvent::get_event_info),
get_eventinfo_byid : Some(DHCPEvent::get_event_info_by_id),
localstorage_new : None,
Expand Down
4 changes: 2 additions & 2 deletions rust/src/dns/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ pub unsafe extern "C" fn SCRegisterDnsUdpParser() {
get_tx: state_get_tx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: tx_get_alstate_progress,
tx_get_progress: Some(tx_get_alstate_progress),
get_eventinfo: Some(DNSEvent::get_event_info),
get_eventinfo_byid: Some(DNSEvent::get_event_info_by_id),
localstorage_new: None,
Expand Down Expand Up @@ -1076,7 +1076,7 @@ pub unsafe extern "C" fn SCRegisterDnsTcpParser() {
get_tx: state_get_tx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: tx_get_alstate_progress,
tx_get_progress: Some(tx_get_alstate_progress),
get_eventinfo: Some(DNSEvent::get_event_info),
get_eventinfo_byid: Some(DNSEvent::get_event_info_by_id),
localstorage_new: None,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/enip/enip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ pub unsafe extern "C" fn SCEnipRegisterParsers() {
get_tx: rs_enip_state_get_tx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: rs_enip_tx_get_alstate_progress,
tx_get_progress: Some(rs_enip_tx_get_alstate_progress),
get_eventinfo: Some(EnipEvent::get_event_info),
get_eventinfo_byid: Some(EnipEvent::get_event_info_by_id),
localstorage_new: None,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/http2/http2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1539,7 +1539,7 @@ pub unsafe extern "C" fn rs_http2_register_parser() {
get_tx: rs_http2_state_get_tx,
tx_comp_st_ts: HTTP2TransactionState::HTTP2StateClosed as i32,
tx_comp_st_tc: HTTP2TransactionState::HTTP2StateClosed as i32,
tx_get_progress: rs_http2_tx_get_alstate_progress,
tx_get_progress: Some(rs_http2_tx_get_alstate_progress),
get_eventinfo: Some(HTTP2Event::get_event_info),
get_eventinfo_byid: Some(HTTP2Event::get_event_info_by_id),
localstorage_new: None,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/ike/ike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ pub unsafe extern "C" fn rs_ike_register_parser() {
get_tx: rs_ike_state_get_tx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: rs_ike_tx_get_alstate_progress,
tx_get_progress: Some(rs_ike_tx_get_alstate_progress),
get_eventinfo: Some(IkeEvent::get_event_info),
get_eventinfo_byid: Some(IkeEvent::get_event_info_by_id),
localstorage_new: None,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/krb/krb5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ pub unsafe extern "C" fn rs_register_krb5_parser() {
get_tx : rs_krb5_state_get_tx,
tx_comp_st_ts : 1,
tx_comp_st_tc : 1,
tx_get_progress : rs_krb5_tx_get_alstate_progress,
tx_get_progress : Some(rs_krb5_tx_get_alstate_progress),
get_eventinfo : Some(KRB5Event::get_event_info),
get_eventinfo_byid : Some(KRB5Event::get_event_info_by_id),
localstorage_new : None,
Expand Down
4 changes: 2 additions & 2 deletions rust/src/ldap/ldap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ pub unsafe extern "C" fn SCRegisterLdapTcpParser() {
get_tx: SCLdapStateGetTx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: SCLdapTxGetAlstateProgress,
tx_get_progress: Some(SCLdapTxGetAlstateProgress),
get_eventinfo: Some(LdapEvent::get_event_info),
get_eventinfo_byid: Some(LdapEvent::get_event_info_by_id),
localstorage_new: None,
Expand Down Expand Up @@ -692,7 +692,7 @@ pub unsafe extern "C" fn SCRegisterLdapUdpParser() {
get_tx: SCLdapStateGetTx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: SCLdapTxGetAlstateProgress,
tx_get_progress: Some(SCLdapTxGetAlstateProgress),
get_eventinfo: Some(LdapEvent::get_event_info),
get_eventinfo_byid: Some(LdapEvent::get_event_info_by_id),
localstorage_new: None,
Expand Down
3 changes: 3 additions & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,6 @@ pub mod ldap;

#[allow(unused_imports)]
pub use suricata_lua_sys;

// Generating Rust bindings from C.
pub mod sys;
2 changes: 1 addition & 1 deletion rust/src/modbus/modbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ pub unsafe extern "C" fn rs_modbus_register_parser() {
get_tx: rs_modbus_state_get_tx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: rs_modbus_tx_get_alstate_progress,
tx_get_progress: Some(rs_modbus_tx_get_alstate_progress),
get_eventinfo: Some(ModbusEvent::get_event_info),
get_eventinfo_byid: Some(ModbusEvent::get_event_info_by_id),
localstorage_new: None,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/mqtt/mqtt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ pub unsafe extern "C" fn SCMqttRegisterParser() {
get_tx: rs_mqtt_state_get_tx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: rs_mqtt_tx_get_alstate_progress,
tx_get_progress: Some(rs_mqtt_tx_get_alstate_progress),
get_eventinfo: Some(MQTTEvent::get_event_info),
get_eventinfo_byid: Some(MQTTEvent::get_event_info_by_id),
localstorage_new: None,
Expand Down
4 changes: 2 additions & 2 deletions rust/src/nfs/nfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1978,7 +1978,7 @@ pub unsafe extern "C" fn rs_nfs_register_parser() {
get_tx: rs_nfs_state_get_tx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: rs_nfs_tx_get_alstate_progress,
tx_get_progress: Some(rs_nfs_tx_get_alstate_progress),
get_eventinfo: Some(NFSEvent::get_event_info),
get_eventinfo_byid : Some(NFSEvent::get_event_info_by_id),
localstorage_new: None,
Expand Down Expand Up @@ -2055,7 +2055,7 @@ pub unsafe extern "C" fn rs_nfs_udp_register_parser() {
get_tx: rs_nfs_state_get_tx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: rs_nfs_tx_get_alstate_progress,
tx_get_progress: Some(rs_nfs_tx_get_alstate_progress),
get_eventinfo: Some(NFSEvent::get_event_info),
get_eventinfo_byid : Some(NFSEvent::get_event_info_by_id),
localstorage_new: None,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/ntp/ntp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ pub unsafe extern "C" fn rs_register_ntp_parser() {
get_tx : rs_ntp_state_get_tx,
tx_comp_st_ts : 1,
tx_comp_st_tc : 1,
tx_get_progress : rs_ntp_tx_get_alstate_progress,
tx_get_progress : Some(rs_ntp_tx_get_alstate_progress),
get_eventinfo : Some(NTPEvent::get_event_info),
get_eventinfo_byid : Some(NTPEvent::get_event_info_by_id),
localstorage_new : None,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/pgsql/pgsql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ pub unsafe extern "C" fn SCRegisterPgsqlParser() {
get_tx: SCPgsqlStateGetTx,
tx_comp_st_ts: PgsqlTxProgress::TxDone as i32,
tx_comp_st_tc: PgsqlTxProgress::TxDone as i32,
tx_get_progress: SCPgsqlTxGetALStateProgress,
tx_get_progress: Some(SCPgsqlTxGetALStateProgress),
get_eventinfo: None,
get_eventinfo_byid: None,
localstorage_new: None,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/quic/quic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ pub unsafe extern "C" fn rs_quic_register_parser() {
get_tx: rs_quic_state_get_tx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: rs_quic_tx_get_alstate_progress,
tx_get_progress: Some(rs_quic_tx_get_alstate_progress),
get_eventinfo: Some(QuicEvent::get_event_info),
get_eventinfo_byid: Some(QuicEvent::get_event_info_by_id),
localstorage_new: None,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/rdp/rdp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ pub unsafe extern "C" fn rs_rdp_register_parser() {
get_tx: rs_rdp_state_get_tx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: rs_rdp_tx_get_progress,
tx_get_progress: Some(rs_rdp_tx_get_progress),
get_eventinfo: None,
get_eventinfo_byid: None,
localstorage_new: None,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/rfb/rfb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ pub unsafe extern "C" fn SCRfbRegisterParser() {
get_tx: rs_rfb_state_get_tx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: rs_rfb_tx_get_alstate_progress,
tx_get_progress: Some(rs_rfb_tx_get_alstate_progress),
get_eventinfo: Some(RFBEvent::get_event_info),
get_eventinfo_byid: Some(RFBEvent::get_event_info_by_id),
localstorage_new: None,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/sip/sip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ pub unsafe extern "C" fn rs_sip_register_parser() {
get_tx: rs_sip_state_get_tx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: rs_sip_tx_get_alstate_progress,
tx_get_progress: Some(rs_sip_tx_get_alstate_progress),
get_eventinfo: Some(SIPEvent::get_event_info),
get_eventinfo_byid: Some(SIPEvent::get_event_info_by_id),
localstorage_new: None,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/smb/smb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2299,7 +2299,7 @@ pub unsafe extern "C" fn rs_smb_register_parser() {
get_tx: rs_smb_state_get_tx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: rs_smb_tx_get_alstate_progress,
tx_get_progress: Some(rs_smb_tx_get_alstate_progress),
get_eventinfo: Some(rs_smb_state_get_event_info),
get_eventinfo_byid : Some(rs_smb_state_get_event_info_by_id),
localstorage_new: None,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/snmp/snmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ pub unsafe extern "C" fn rs_register_snmp_parser() {
get_tx : rs_snmp_state_get_tx,
tx_comp_st_ts : 1,
tx_comp_st_tc : 1,
tx_get_progress : rs_snmp_tx_get_alstate_progress,
tx_get_progress : Some(rs_snmp_tx_get_alstate_progress),
get_eventinfo : Some(SNMPEvent::get_event_info),
get_eventinfo_byid : Some(SNMPEvent::get_event_info_by_id),
localstorage_new : None,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/ssh/ssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ pub unsafe extern "C" fn rs_ssh_register_parser() {
get_tx: rs_ssh_state_get_tx,
tx_comp_st_ts: SSHConnectionState::SshStateFinished as i32,
tx_comp_st_tc: SSHConnectionState::SshStateFinished as i32,
tx_get_progress: rs_ssh_tx_get_alstate_progress,
tx_get_progress: Some(rs_ssh_tx_get_alstate_progress),
get_eventinfo: Some(SSHEvent::get_event_info),
get_eventinfo_byid: Some(SSHEvent::get_event_info_by_id),
localstorage_new: None,
Expand Down
18 changes: 18 additions & 0 deletions rust/src/sys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Copyright (C) 2024 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
2 changes: 1 addition & 1 deletion rust/src/telnet/telnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ pub unsafe extern "C" fn rs_telnet_register_parser() {
get_tx: rs_telnet_state_get_tx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: rs_telnet_tx_get_alstate_progress,
tx_get_progress: Some(rs_telnet_tx_get_alstate_progress),
get_eventinfo: Some(TelnetEvent::get_event_info),
get_eventinfo_byid : Some(TelnetEvent::get_event_info_by_id),
localstorage_new: None,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/websocket/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ pub unsafe extern "C" fn rs_websocket_register_parser() {
get_tx: rs_websocket_state_get_tx,
tx_comp_st_ts: 1,
tx_comp_st_tc: 1,
tx_get_progress: rs_websocket_tx_get_alstate_progress,
tx_get_progress: Some(rs_websocket_tx_get_alstate_progress),
get_eventinfo: Some(WebSocketEvent::get_event_info),
get_eventinfo_byid: Some(WebSocketEvent::get_event_info_by_id),
localstorage_new: None,
Expand Down
2 changes: 0 additions & 2 deletions src/app-layer-ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@
typedef int (*SCAppLayerStateGetProgressFn)(void *alstate, uint8_t direction);

#endif /* SURICATA_APP_LAYER_EXT_H */

int foo(void *alstate, uint8_t direction);

0 comments on commit 1e5ab1a

Please sign in to comment.