|
| 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 | +} |
0 commit comments