Skip to content

Commit

Permalink
bittorrent: no_mangle, pub and naming cleanups
Browse files Browse the repository at this point in the history
- Remove rs_prefix
- Remove no_mangle and pub when not needed

Related to ticket: #7498
  • Loading branch information
jasonish authored and victorjulien committed Feb 28, 2025
1 parent 05dd607 commit f0116c3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 39 deletions.
62 changes: 26 additions & 36 deletions rust/src/bittorrent_dht/bittorrent_dht.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,53 +142,47 @@ impl BitTorrentDHTState {

// C exports.

export_tx_data_get!(bittorrent_dht_get_tx_data, BitTorrentDHTTransaction);
export_state_data_get!(bittorrent_dht_get_state_data, BitTorrentDHTState);
export_tx_data_get!(get_tx_data, BitTorrentDHTTransaction);
export_state_data_get!(get_state_data, BitTorrentDHTState);

#[no_mangle]
pub extern "C" fn rs_bittorrent_dht_state_new(
extern "C" fn state_new(
_orig_state: *mut std::os::raw::c_void, _orig_proto: AppProto,
) -> *mut std::os::raw::c_void {
let state = BitTorrentDHTState::new();
let boxed = Box::new(state);
return Box::into_raw(boxed) as *mut std::os::raw::c_void;
}

#[no_mangle]
pub unsafe extern "C" fn rs_bittorrent_dht_state_free(state: *mut std::os::raw::c_void) {
unsafe extern "C" fn state_free(state: *mut std::os::raw::c_void) {
std::mem::drop(Box::from_raw(state as *mut BitTorrentDHTState));
}

#[no_mangle]
pub unsafe extern "C" fn rs_bittorrent_dht_state_tx_free(
unsafe extern "C" fn state_tx_free(
state: *mut std::os::raw::c_void, tx_id: u64,
) {
let state = cast_pointer!(state, BitTorrentDHTState);
state.free_tx(tx_id);
}

#[no_mangle]
pub unsafe extern "C" fn rs_bittorrent_dht_parse_ts(
unsafe extern "C" fn parse_ts(
_flow: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void,
stream_slice: StreamSlice, _data: *const std::os::raw::c_void,
) -> AppLayerResult {
return rs_bittorrent_dht_parse(
return parse(
_flow, state, _pstate, stream_slice,
_data, Direction::ToServer);
}

#[no_mangle]
pub unsafe extern "C" fn rs_bittorrent_dht_parse_tc(
unsafe extern "C" fn parse_tc(
_flow: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void,
stream_slice: StreamSlice, _data: *const std::os::raw::c_void,
) -> AppLayerResult {
return rs_bittorrent_dht_parse(
return parse(
_flow, state, _pstate, stream_slice,
_data, Direction::ToClient);
}

#[no_mangle]
pub unsafe extern "C" fn rs_bittorrent_dht_parse(
unsafe extern "C" fn parse(
_flow: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void,
stream_slice: StreamSlice, _data: *const std::os::raw::c_void,
direction: Direction,
Expand All @@ -198,8 +192,7 @@ pub unsafe extern "C" fn rs_bittorrent_dht_parse(
state.parse(buf, direction).into()
}

#[no_mangle]
pub unsafe extern "C" fn rs_bittorrent_dht_state_get_tx(
unsafe extern "C" fn state_get_tx(
state: *mut std::os::raw::c_void, tx_id: u64,
) -> *mut std::os::raw::c_void {
let state = cast_pointer!(state, BitTorrentDHTState);
Expand All @@ -213,16 +206,14 @@ pub unsafe extern "C" fn rs_bittorrent_dht_state_get_tx(
}
}

#[no_mangle]
pub unsafe extern "C" fn rs_bittorrent_dht_state_get_tx_count(
unsafe extern "C" fn state_get_tx_count(
state: *mut std::os::raw::c_void,
) -> u64 {
let state = cast_pointer!(state, BitTorrentDHTState);
return state.tx_id;
}

#[no_mangle]
pub unsafe extern "C" fn rs_bittorrent_dht_tx_get_alstate_progress(
unsafe extern "C" fn tx_get_alstate_progress(
tx: *mut std::os::raw::c_void, _direction: u8,
) -> std::os::raw::c_int {
let tx = cast_pointer!(tx, BitTorrentDHTTransaction);
Expand All @@ -235,8 +226,7 @@ pub unsafe extern "C" fn rs_bittorrent_dht_tx_get_alstate_progress(
return 0;
}

#[no_mangle]
pub unsafe extern "C" fn rs_bittorrent_dht_state_get_tx_iterator(
unsafe extern "C" fn state_get_tx_iterator(
_ipproto: u8, _alproto: AppProto, state: *mut std::os::raw::c_void, min_tx_id: u64,
_max_tx_id: u64, istate: &mut u64,
) -> applayer::AppLayerGetTxIterTuple {
Expand All @@ -257,7 +247,7 @@ pub unsafe extern "C" fn rs_bittorrent_dht_state_get_tx_iterator(
const PARSER_NAME: &[u8] = b"bittorrent-dht\0";

#[no_mangle]
pub unsafe extern "C" fn rs_bittorrent_dht_udp_register_parser() {
pub unsafe extern "C" fn SCRegisterBittorrentDhtUdpParser() {
let parser = RustParser {
name: PARSER_NAME.as_ptr() as *const std::os::raw::c_char,
default_port: std::ptr::null(),
Expand All @@ -266,24 +256,24 @@ pub unsafe extern "C" fn rs_bittorrent_dht_udp_register_parser() {
probe_tc: None,
min_depth: 0,
max_depth: 16,
state_new: rs_bittorrent_dht_state_new,
state_free: rs_bittorrent_dht_state_free,
tx_free: rs_bittorrent_dht_state_tx_free,
parse_ts: rs_bittorrent_dht_parse_ts,
parse_tc: rs_bittorrent_dht_parse_tc,
get_tx_count: rs_bittorrent_dht_state_get_tx_count,
get_tx: rs_bittorrent_dht_state_get_tx,
state_new,
state_free,
tx_free: state_tx_free,
parse_ts,
parse_tc,
get_tx_count: state_get_tx_count,
get_tx: 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: tx_get_alstate_progress,
get_eventinfo: Some(BitTorrentDHTEvent::get_event_info),
get_eventinfo_byid: Some(BitTorrentDHTEvent::get_event_info_by_id),
localstorage_new: None,
localstorage_free: None,
get_tx_files: None,
get_tx_iterator: Some(rs_bittorrent_dht_state_get_tx_iterator),
get_tx_data: bittorrent_dht_get_tx_data,
get_state_data: bittorrent_dht_get_state_data,
get_tx_iterator: Some(state_get_tx_iterator),
get_tx_data,
get_state_data,
apply_tx_config: None,
flags: 0,
get_frame_id_by_name: None,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/bittorrent_dht/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn log_bittorrent_dht(
}

#[no_mangle]
pub unsafe extern "C" fn rs_bittorrent_dht_logger_log(
pub unsafe extern "C" fn SCBittorrentDhtLogger(
tx: *mut std::os::raw::c_void, js: &mut JsonBuilder,
) -> bool {
let tx = cast_pointer!(tx, BitTorrentDHTTransaction);
Expand Down
2 changes: 1 addition & 1 deletion src/app-layer-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1738,7 +1738,7 @@ void AppLayerParserRegisterProtocolParsers(void)
RegisterSMTPParsers();
SCRegisterDnsUdpParser();
SCRegisterDnsTcpParser();
rs_bittorrent_dht_udp_register_parser();
SCRegisterBittorrentDhtUdpParser();
RegisterModbusParsers();
SCEnipRegisterParsers();
RegisterDNP3Parsers();
Expand Down
2 changes: 1 addition & 1 deletion src/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ void OutputRegisterRootLoggers(void)
RegisterSimpleJsonApplayerLogger(ALPROTO_HTTP2, rs_http2_log_json, "http");
// underscore instead of dash for bittorrent_dht
RegisterSimpleJsonApplayerLogger(
ALPROTO_BITTORRENT_DHT, rs_bittorrent_dht_logger_log, "bittorrent_dht");
ALPROTO_BITTORRENT_DHT, SCBittorrentDhtLogger, "bittorrent_dht");

OutputPacketLoggerRegister();
OutputFiledataLoggerRegister();
Expand Down

0 comments on commit f0116c3

Please sign in to comment.