X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=ldk-c-bindings;a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning%2Fonion_message%2Fpacket.rs;fp=lightning-c-bindings%2Fsrc%2Flightning%2Fonion_message%2Fpacket.rs;h=90c0238577a3f86ca8d6215f963fb8a2b33b97ba;hp=7def568ad1ea0c3d115ed0172d82726d6828d4e1;hb=a21177846ed75d66f68a1603f768410f07a65791;hpb=6a576b303a7b0de3fa2e009a23c1ffbf54dc679e diff --git a/lightning-c-bindings/src/lightning/onion_message/packet.rs b/lightning-c-bindings/src/lightning/onion_message/packet.rs index 7def568..90c0238 100644 --- a/lightning-c-bindings/src/lightning/onion_message/packet.rs +++ b/lightning-c-bindings/src/lightning/onion_message/packet.rs @@ -16,3 +16,62 @@ use crate::c_types::*; #[cfg(feature="no-std")] use alloc::{vec::Vec, boxed::Box}; +/// The contents of a custom onion message. +#[repr(C)] +pub struct CustomOnionMessageContents { + /// An opaque pointer which is passed to your function implementations as an argument. + /// This has no meaning in the LDK, and can be NULL or any other value. + pub this_arg: *mut c_void, + /// Returns the TLV type identifying the message contents. MUST be >= 64. + #[must_use] + pub tlv_type: extern "C" fn (this_arg: *const c_void) -> u64, + /// Serialize the object into a byte array + pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z, + /// Frees any resources associated with this object given its this_arg pointer. + /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed. + pub free: Option, +} +unsafe impl Send for CustomOnionMessageContents {} +unsafe impl Sync for CustomOnionMessageContents {} +#[no_mangle] +pub(crate) extern "C" fn CustomOnionMessageContents_clone_fields(orig: &CustomOnionMessageContents) -> CustomOnionMessageContents { + CustomOnionMessageContents { + this_arg: orig.this_arg, + tlv_type: Clone::clone(&orig.tlv_type), + write: Clone::clone(&orig.write), + free: Clone::clone(&orig.free), + } +} +impl lightning::util::ser::Writeable for CustomOnionMessageContents { + fn write(&self, w: &mut W) -> Result<(), crate::c_types::io::Error> { + let vec = (self.write)(self.this_arg); + w.write_all(vec.as_slice()) + } +} + +use lightning::onion_message::packet::CustomOnionMessageContents as rustCustomOnionMessageContents; +impl rustCustomOnionMessageContents for CustomOnionMessageContents { + fn tlv_type(&self) -> u64 { + let mut ret = (self.tlv_type)(self.this_arg); + ret + } +} + +// We're essentially a pointer already, or at least a set of pointers, so allow us to be used +// directly as a Deref trait in higher-level structs: +impl core::ops::Deref for CustomOnionMessageContents { + type Target = Self; + fn deref(&self) -> &Self { + self + } +} +/// Calls the free function if one is set +#[no_mangle] +pub extern "C" fn CustomOnionMessageContents_free(this_ptr: CustomOnionMessageContents) { } +impl Drop for CustomOnionMessageContents { + fn drop(&mut self) { + if let Some(f) = self.free { + f(self.this_arg); + } + } +}