Skip to content

Commit 031456e

Browse files
rbranemesare
authored andcommitted
Add more core API's to rust
1 parent 8a5cd4b commit 031456e

File tree

3 files changed

+270
-0
lines changed

3 files changed

+270
-0
lines changed

rust/src/enterprise.rs

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
use std::marker::PhantomData;
2+
use std::time::{Duration, SystemTime, UNIX_EPOCH};
3+
4+
use crate::rc::Array;
5+
use crate::string::{BnStrCompatible, BnString};
6+
7+
pub fn server_username() -> BnString {
8+
unsafe { BnString::from_raw(binaryninjacore_sys::BNGetEnterpriseServerUsername()) }
9+
}
10+
11+
pub fn server_url() -> BnString {
12+
unsafe { BnString::from_raw(binaryninjacore_sys::BNGetEnterpriseServerUrl()) }
13+
}
14+
15+
pub fn set_server_url<S: BnStrCompatible>(url: S) -> Result<(), ()> {
16+
let url = url.into_bytes_with_nul();
17+
let result = unsafe {
18+
binaryninjacore_sys::BNSetEnterpriseServerUrl(url.as_ref().as_ptr() as *const i8)
19+
};
20+
if result {
21+
Ok(())
22+
} else {
23+
Err(())
24+
}
25+
}
26+
27+
pub fn server_name() -> BnString {
28+
unsafe { BnString::from_raw(binaryninjacore_sys::BNGetEnterpriseServerName()) }
29+
}
30+
31+
pub fn server_id() -> BnString {
32+
unsafe { BnString::from_raw(binaryninjacore_sys::BNGetEnterpriseServerId()) }
33+
}
34+
35+
pub fn server_version() -> u64 {
36+
unsafe { binaryninjacore_sys::BNGetEnterpriseServerVersion() }
37+
}
38+
39+
pub fn server_build_id() -> BnString {
40+
unsafe { BnString::from_raw(binaryninjacore_sys::BNGetEnterpriseServerBuildId()) }
41+
}
42+
43+
pub fn server_token() -> BnString {
44+
unsafe { BnString::from_raw(binaryninjacore_sys::BNGetEnterpriseServerToken()) }
45+
}
46+
47+
pub fn license_duration() -> Duration {
48+
Duration::from_secs(unsafe { binaryninjacore_sys::BNGetEnterpriseServerLicenseDuration() })
49+
}
50+
51+
pub fn license_expiration_time() -> SystemTime {
52+
let m = Duration::from_secs(unsafe {
53+
binaryninjacore_sys::BNGetEnterpriseServerLicenseExpirationTime()
54+
});
55+
UNIX_EPOCH + m
56+
}
57+
58+
pub fn server_reservation_time_limit() -> Duration {
59+
Duration::from_secs(unsafe { binaryninjacore_sys::BNGetEnterpriseServerReservationTimeLimit() })
60+
}
61+
62+
pub fn is_server_floating_license() -> bool {
63+
unsafe { binaryninjacore_sys::BNIsEnterpriseServerFloatingLicense() }
64+
}
65+
66+
pub fn is_server_license_still_activated() -> bool {
67+
unsafe { binaryninjacore_sys::BNIsEnterpriseServerLicenseStillActivated() }
68+
}
69+
70+
pub fn authenticate_server_with_credentials<U, P>(username: U, password: P, remember: bool) -> bool
71+
where
72+
U: BnStrCompatible,
73+
P: BnStrCompatible,
74+
{
75+
let username = username.into_bytes_with_nul();
76+
let password = password.into_bytes_with_nul();
77+
unsafe {
78+
binaryninjacore_sys::BNAuthenticateEnterpriseServerWithCredentials(
79+
username.as_ref().as_ptr() as *const i8,
80+
password.as_ref().as_ptr() as *const i8,
81+
remember,
82+
)
83+
}
84+
}
85+
86+
pub fn authenticate_server_with_method<S: BnStrCompatible>(method: S, remember: bool) -> bool {
87+
let method = method.into_bytes_with_nul();
88+
unsafe {
89+
binaryninjacore_sys::BNAuthenticateEnterpriseServerWithMethod(
90+
method.as_ref().as_ptr() as *const i8,
91+
remember,
92+
)
93+
}
94+
}
95+
96+
pub fn connect_server() -> bool {
97+
unsafe { binaryninjacore_sys::BNConnectEnterpriseServer() }
98+
}
99+
100+
pub fn deauthenticate_server() -> bool {
101+
unsafe { binaryninjacore_sys::BNDeauthenticateEnterpriseServer() }
102+
}
103+
104+
pub fn cancel_server_authentication() {
105+
unsafe { binaryninjacore_sys::BNCancelEnterpriseServerAuthentication() }
106+
}
107+
108+
pub fn update_server_license(timeout: Duration) -> bool {
109+
unsafe { binaryninjacore_sys::BNUpdateEnterpriseServerLicense(timeout.as_secs()) }
110+
}
111+
112+
pub fn release_server_license() -> bool {
113+
unsafe { binaryninjacore_sys::BNReleaseEnterpriseServerLicense() }
114+
}
115+
116+
pub fn is_server_connected() -> bool {
117+
unsafe { binaryninjacore_sys::BNIsEnterpriseServerConnected() }
118+
}
119+
120+
pub fn is_server_authenticated() -> bool {
121+
unsafe { binaryninjacore_sys::BNIsEnterpriseServerAuthenticated() }
122+
}
123+
124+
pub fn is_server_initialized() -> bool {
125+
unsafe { binaryninjacore_sys::BNIsEnterpriseServerInitialized() }
126+
}
127+
128+
pub fn server_last_error() -> BnString {
129+
unsafe { BnString::from_raw(binaryninjacore_sys::BNGetEnterpriseServerLastError()) }
130+
}
131+
132+
pub fn server_authentication_methods() -> (Array<BnString>, Array<BnString>) {
133+
let mut methods = core::ptr::null_mut();
134+
let mut names = core::ptr::null_mut();
135+
let count = unsafe {
136+
binaryninjacore_sys::BNGetEnterpriseServerAuthenticationMethods(&mut methods, &mut names)
137+
};
138+
unsafe { (Array::new(methods, count, ()), Array::new(names, count, ())) }
139+
}
140+
141+
// NOTE don't implement Clone, Copy, so each callback can only be
142+
// register/unregistered only once
143+
#[repr(transparent)]
144+
#[derive(Debug)]
145+
pub struct EnterpriseServerCallback<'a> {
146+
handle: binaryninjacore_sys::BNEnterpriseServerCallbacks,
147+
lifetime: PhantomData<&'a ()>,
148+
}
149+
150+
pub fn register_license_changed_callback<'a, F: FnMut(bool) + 'a>(
151+
callback: F,
152+
) -> EnterpriseServerCallback<'a> {
153+
unsafe extern "C" fn cb_license_status_changed<F: FnMut(bool)>(
154+
ctxt: *mut ::std::os::raw::c_void,
155+
still_valid: bool,
156+
) {
157+
let ctxt: &mut F = &mut *(ctxt as *mut F);
158+
ctxt(still_valid)
159+
}
160+
let mut handle = binaryninjacore_sys::BNEnterpriseServerCallbacks {
161+
context: Box::leak(Box::new(callback)) as *mut F as *mut core::ffi::c_void,
162+
licenseStatusChanged: Some(cb_license_status_changed::<F>),
163+
};
164+
unsafe { binaryninjacore_sys::BNRegisterEnterpriseServerNotification(&mut handle) }
165+
EnterpriseServerCallback {
166+
handle,
167+
lifetime: PhantomData,
168+
}
169+
}
170+
171+
pub fn unregister_license_changed_callback(mut callback_handle: EnterpriseServerCallback) {
172+
unsafe {
173+
binaryninjacore_sys::BNUnregisterEnterpriseServerNotification(&mut callback_handle.handle)
174+
}
175+
}
176+
177+
impl<'a> EnterpriseServerCallback<'a> {
178+
/// register the license changed callback
179+
pub fn register<F: FnMut(bool) + 'a>(callback: F) -> Self {
180+
register_license_changed_callback(callback)
181+
}
182+
183+
/// deregister the license changed callback, equivalent to drop the struct
184+
pub fn deregister(self) {
185+
// Nothing, just drop self
186+
}
187+
}
188+
189+
impl Drop for EnterpriseServerCallback<'_> {
190+
fn drop(&mut self) {
191+
unregister_license_changed_callback(EnterpriseServerCallback {
192+
handle: self.handle,
193+
lifetime: PhantomData,
194+
})
195+
}
196+
}

rust/src/headless.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ pub fn shutdown() {
9999
unsafe { binaryninjacore_sys::BNShutdown() };
100100
}
101101

102+
pub fn is_shutdown_requested() {
103+
unsafe { binaryninjacore_sys::BNIsShutdownRequested() };
104+
}
105+
102106
/// Prelued-postlued helper function (calls [`init`] and [`shutdown`] for you)
103107
/// ```no_run
104108
/// # use binaryninja::binaryview::BinaryViewExt;

rust/src/lib.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ pub mod databuffer;
138138
pub mod debuginfo;
139139
pub mod demangle;
140140
pub mod disassembly;
141+
pub mod enterprise;
141142
pub mod component;
142143
pub mod downloadprovider;
143144
pub mod externallibrary;
@@ -434,6 +435,75 @@ pub fn version() -> string::BnString {
434435
unsafe { string::BnString::from_raw(binaryninjacore_sys::BNGetVersionString()) }
435436
}
436437

438+
pub fn build_id() -> u32 {
439+
unsafe { binaryninjacore_sys::BNGetBuildId() }
440+
}
441+
442+
#[derive(Clone, PartialEq, Eq, Hash)]
443+
pub struct VersionInfo {
444+
pub major: u32,
445+
pub minor: u32,
446+
pub build: u32,
447+
pub channel: string::BnString,
448+
}
449+
450+
pub fn version_info() -> VersionInfo {
451+
let info_raw = unsafe { binaryninjacore_sys::BNGetVersionInfo() };
452+
VersionInfo {
453+
major: info_raw.major,
454+
minor: info_raw.minor,
455+
build: info_raw.build,
456+
channel: unsafe { string::BnString::from_raw(info_raw.channel) },
457+
}
458+
}
459+
460+
pub fn serial_number() -> string::BnString {
461+
unsafe { string::BnString::from_raw(binaryninjacore_sys::BNGetSerialNumber()) }
462+
}
463+
464+
pub fn is_license_validated() -> bool {
465+
unsafe { binaryninjacore_sys::BNIsLicenseValidated() }
466+
}
467+
468+
pub fn licensed_user_email() -> string::BnString {
469+
unsafe { string::BnString::from_raw(binaryninjacore_sys::BNGetLicensedUserEmail()) }
470+
}
471+
472+
pub fn license_count() -> i32 {
473+
unsafe { binaryninjacore_sys::BNGetLicenseCount() }
474+
}
475+
476+
pub fn set_license<S: string::BnStrCompatible>(license: S) {
477+
let license = license.into_bytes_with_nul();
478+
let license_slice = license.as_ref();
479+
unsafe { binaryninjacore_sys::BNSetLicense(license_slice.as_ptr() as *const i8) }
480+
}
481+
482+
pub fn product() -> string::BnString {
483+
unsafe { string::BnString::from_raw(binaryninjacore_sys::BNGetProduct()) }
484+
}
485+
486+
pub fn product_type() -> string::BnString {
487+
unsafe { string::BnString::from_raw(binaryninjacore_sys::BNGetProductType()) }
488+
}
489+
490+
pub fn license_expiration_time() -> std::time::SystemTime {
491+
let m = std::time::Duration::from_secs(unsafe {
492+
binaryninjacore_sys::BNGetLicenseExpirationTime()
493+
});
494+
std::time::UNIX_EPOCH + m
495+
}
496+
497+
pub fn is_ui_enabled() -> bool {
498+
unsafe { binaryninjacore_sys::BNIsUIEnabled() }
499+
}
500+
501+
pub fn is_database<S: string::BnStrCompatible>(filename: S) -> bool {
502+
let filename = filename.into_bytes_with_nul();
503+
let filename_slice = filename.as_ref();
504+
unsafe { binaryninjacore_sys::BNIsDatabase(filename_slice.as_ptr() as *const i8) }
505+
}
506+
437507
pub fn plugin_abi_version() -> u32 {
438508
binaryninjacore_sys::BN_CURRENT_CORE_ABI_VERSION
439509
}

0 commit comments

Comments
 (0)