X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning%2Fonion_message%2Fpacket.rs;h=6a6714e76cd977f749e286e474d95b27545dd8f1;hb=HEAD;hp=94d6d656bd11c79b51c8793ca22d61ecf0c8a940;hpb=f7105f5bb3fdf379d58286eacd8e44edba686954;p=ldk-c-bindings diff --git a/lightning-c-bindings/src/lightning/onion_message/packet.rs b/lightning-c-bindings/src/lightning/onion_message/packet.rs index 94d6d65..7506842 100644 --- a/lightning-c-bindings/src/lightning/onion_message/packet.rs +++ b/lightning-c-bindings/src/lightning/onion_message/packet.rs @@ -140,13 +140,26 @@ impl Clone for Packet { #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method pub(crate) extern "C" fn Packet_clone_void(this_ptr: *const c_void) -> *mut c_void { - Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativePacket)).clone() })) as *mut c_void + Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativePacket)).clone() })) as *mut c_void } #[no_mangle] /// Creates a copy of the Packet pub extern "C" fn Packet_clone(orig: &Packet) -> Packet { orig.clone() } +/// Get a string which allows debug introspection of a Packet object +pub extern "C" fn Packet_debug_str_void(o: *const c_void) -> Str { + alloc::format!("{:?}", unsafe { o as *const crate::lightning::onion_message::packet::Packet }).into()} +/// Generates a non-cryptographic 64-bit hash of the Packet. +#[no_mangle] +pub extern "C" fn Packet_hash(o: &Packet) -> u64 { + if o.inner.is_null() { return 0; } + // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core + #[allow(deprecated)] + let mut hasher = core::hash::SipHasher::new(); + core::hash::Hash::hash(o.get_native_ref(), &mut hasher); + core::hash::Hasher::finish(&hasher) +} /// Checks if two Packets contain equal inner contents. /// This ignores pointers and is_owned flags and looks at the values in fields. /// Two objects with NULL inner values will be considered "equal" here. @@ -161,113 +174,171 @@ pub extern "C" fn Packet_eq(a: &Packet, b: &Packet) -> bool { pub extern "C" fn Packet_write(obj: &crate::lightning::onion_message::packet::Packet) -> crate::c_types::derived::CVec_u8Z { crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref()) } -#[no_mangle] +#[allow(unused)] pub(crate) extern "C" fn Packet_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z { crate::c_types::serialize_obj(unsafe { &*(obj as *const nativePacket) }) } -/// The contents of an onion message. In the context of offers, this would be the invoice, invoice -/// request, or invoice error. +/// The contents of an [`OnionMessage`] as read from the wire. +/// +/// [`OnionMessage`]: crate::ln::msgs::OnionMessage #[derive(Clone)] #[must_use] #[repr(C)] -pub enum OnionMessageContents { +pub enum ParsedOnionMessageContents { /// A message related to BOLT 12 Offers. Offers( crate::lightning::onion_message::offers::OffersMessage), /// A custom onion message specified by the user. Custom( - crate::lightning::onion_message::packet::CustomOnionMessageContents), + crate::lightning::onion_message::packet::OnionMessageContents), } -use lightning::onion_message::packet::OnionMessageContents as OnionMessageContentsImport; -pub(crate) type nativeOnionMessageContents = OnionMessageContentsImport; +use lightning::onion_message::packet::ParsedOnionMessageContents as ParsedOnionMessageContentsImport; +pub(crate) type nativeParsedOnionMessageContents = ParsedOnionMessageContentsImport; -impl OnionMessageContents { +impl ParsedOnionMessageContents { #[allow(unused)] - pub(crate) fn to_native(&self) -> nativeOnionMessageContents { + pub(crate) fn to_native(&self) -> nativeParsedOnionMessageContents { match self { - OnionMessageContents::Offers (ref a, ) => { + ParsedOnionMessageContents::Offers (ref a, ) => { let mut a_nonref = Clone::clone(a); - nativeOnionMessageContents::Offers ( + nativeParsedOnionMessageContents::Offers ( a_nonref.into_native(), ) }, - OnionMessageContents::Custom (ref a, ) => { + ParsedOnionMessageContents::Custom (ref a, ) => { let mut a_nonref = Clone::clone(a); - nativeOnionMessageContents::Custom ( + nativeParsedOnionMessageContents::Custom ( a_nonref, ) }, } } #[allow(unused)] - pub(crate) fn into_native(self) -> nativeOnionMessageContents { + pub(crate) fn into_native(self) -> nativeParsedOnionMessageContents { match self { - OnionMessageContents::Offers (mut a, ) => { - nativeOnionMessageContents::Offers ( + ParsedOnionMessageContents::Offers (mut a, ) => { + nativeParsedOnionMessageContents::Offers ( a.into_native(), ) }, - OnionMessageContents::Custom (mut a, ) => { - nativeOnionMessageContents::Custom ( + ParsedOnionMessageContents::Custom (mut a, ) => { + nativeParsedOnionMessageContents::Custom ( a, ) }, } } #[allow(unused)] - pub(crate) fn from_native(native: &nativeOnionMessageContents) -> Self { + pub(crate) fn from_native(native: &ParsedOnionMessageContentsImport) -> Self { + let native = unsafe { &*(native as *const _ as *const c_void as *const nativeParsedOnionMessageContents) }; match native { - nativeOnionMessageContents::Offers (ref a, ) => { + nativeParsedOnionMessageContents::Offers (ref a, ) => { let mut a_nonref = Clone::clone(a); - OnionMessageContents::Offers ( + ParsedOnionMessageContents::Offers ( crate::lightning::onion_message::offers::OffersMessage::native_into(a_nonref), ) }, - nativeOnionMessageContents::Custom (ref a, ) => { + nativeParsedOnionMessageContents::Custom (ref a, ) => { let mut a_nonref = Clone::clone(a); - OnionMessageContents::Custom ( + ParsedOnionMessageContents::Custom ( Into::into(a_nonref), ) }, } } #[allow(unused)] - pub(crate) fn native_into(native: nativeOnionMessageContents) -> Self { + pub(crate) fn native_into(native: nativeParsedOnionMessageContents) -> Self { match native { - nativeOnionMessageContents::Offers (mut a, ) => { - OnionMessageContents::Offers ( + nativeParsedOnionMessageContents::Offers (mut a, ) => { + ParsedOnionMessageContents::Offers ( crate::lightning::onion_message::offers::OffersMessage::native_into(a), ) }, - nativeOnionMessageContents::Custom (mut a, ) => { - OnionMessageContents::Custom ( + nativeParsedOnionMessageContents::Custom (mut a, ) => { + ParsedOnionMessageContents::Custom ( Into::into(a), ) }, } } } -/// Frees any resources used by the OnionMessageContents +/// Frees any resources used by the ParsedOnionMessageContents #[no_mangle] -pub extern "C" fn OnionMessageContents_free(this_ptr: OnionMessageContents) { } -/// Creates a copy of the OnionMessageContents +pub extern "C" fn ParsedOnionMessageContents_free(this_ptr: ParsedOnionMessageContents) { } +/// Creates a copy of the ParsedOnionMessageContents #[no_mangle] -pub extern "C" fn OnionMessageContents_clone(orig: &OnionMessageContents) -> OnionMessageContents { +pub extern "C" fn ParsedOnionMessageContents_clone(orig: &ParsedOnionMessageContents) -> ParsedOnionMessageContents { orig.clone() } +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn ParsedOnionMessageContents_clone_void(this_ptr: *const c_void) -> *mut c_void { + Box::into_raw(Box::new(unsafe { (*(this_ptr as *const ParsedOnionMessageContents)).clone() })) as *mut c_void +} +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn ParsedOnionMessageContents_free_void(this_ptr: *mut c_void) { + let _ = unsafe { Box::from_raw(this_ptr as *mut ParsedOnionMessageContents) }; +} +#[no_mangle] +/// Utility method to constructs a new Offers-variant ParsedOnionMessageContents +pub extern "C" fn ParsedOnionMessageContents_offers(a: crate::lightning::onion_message::offers::OffersMessage) -> ParsedOnionMessageContents { + ParsedOnionMessageContents::Offers(a, ) +} +#[no_mangle] +/// Utility method to constructs a new Custom-variant ParsedOnionMessageContents +pub extern "C" fn ParsedOnionMessageContents_custom(a: crate::lightning::onion_message::packet::OnionMessageContents) -> ParsedOnionMessageContents { + ParsedOnionMessageContents::Custom(a, ) +} +/// Get a string which allows debug introspection of a ParsedOnionMessageContents object +pub extern "C" fn ParsedOnionMessageContents_debug_str_void(o: *const c_void) -> Str { + alloc::format!("{:?}", unsafe { o as *const crate::lightning::onion_message::packet::ParsedOnionMessageContents }).into()} +impl From for crate::lightning::onion_message::packet::OnionMessageContents { + fn from(obj: nativeParsedOnionMessageContents) -> Self { + let rust_obj = crate::lightning::onion_message::packet::ParsedOnionMessageContents::native_into(obj); + let mut ret = ParsedOnionMessageContents_as_OnionMessageContents(&rust_obj); + // We want to free rust_obj when ret gets drop()'d, not rust_obj, so forget it and set ret's free() fn + core::mem::forget(rust_obj); + ret.free = Some(ParsedOnionMessageContents_free_void); + ret + } +} +/// Constructs a new OnionMessageContents which calls the relevant methods on this_arg. +/// This copies the `inner` pointer in this_arg and thus the returned OnionMessageContents must be freed before this_arg is #[no_mangle] -/// Utility method to constructs a new Offers-variant OnionMessageContents -pub extern "C" fn OnionMessageContents_offers(a: crate::lightning::onion_message::offers::OffersMessage) -> OnionMessageContents { - OnionMessageContents::Offers(a, ) +pub extern "C" fn ParsedOnionMessageContents_as_OnionMessageContents(this_arg: &ParsedOnionMessageContents) -> crate::lightning::onion_message::packet::OnionMessageContents { + crate::lightning::onion_message::packet::OnionMessageContents { + this_arg: unsafe { ObjOps::untweak_ptr(this_arg as *const ParsedOnionMessageContents as *mut ParsedOnionMessageContents) as *mut c_void }, + free: None, + tlv_type: ParsedOnionMessageContents_OnionMessageContents_tlv_type, + write: ParsedOnionMessageContents_write_void, + debug_str: ParsedOnionMessageContents_debug_str_void, + cloned: Some(OnionMessageContents_ParsedOnionMessageContents_cloned), + } +} + +#[must_use] +extern "C" fn ParsedOnionMessageContents_OnionMessageContents_tlv_type(this_arg: *const c_void) -> u64 { + let mut ret = ::tlv_type(unsafe { &mut *(this_arg as *mut nativeParsedOnionMessageContents) }, ); + ret +} +extern "C" fn OnionMessageContents_ParsedOnionMessageContents_cloned(new_obj: &mut crate::lightning::onion_message::packet::OnionMessageContents) { + new_obj.this_arg = ParsedOnionMessageContents_clone_void(new_obj.this_arg); + new_obj.free = Some(ParsedOnionMessageContents_free_void); } + #[no_mangle] -/// Utility method to constructs a new Custom-variant OnionMessageContents -pub extern "C" fn OnionMessageContents_custom(a: crate::lightning::onion_message::packet::CustomOnionMessageContents) -> OnionMessageContents { - OnionMessageContents::Custom(a, ) +/// Serialize the ParsedOnionMessageContents object into a byte array which can be read by ParsedOnionMessageContents_read +pub extern "C" fn ParsedOnionMessageContents_write(obj: &crate::lightning::onion_message::packet::ParsedOnionMessageContents) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(&unsafe { &*obj }.to_native()) +} +#[allow(unused)] +pub(crate) extern "C" fn ParsedOnionMessageContents_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z { + ParsedOnionMessageContents_write(unsafe { &*(obj as *const ParsedOnionMessageContents) }) } -/// The contents of a custom onion message. +/// The contents of an onion message. #[repr(C)] -pub struct CustomOnionMessageContents { +pub struct OnionMessageContents { /// 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, @@ -275,46 +346,55 @@ pub struct CustomOnionMessageContents { 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, - /// Called, if set, after this CustomOnionMessageContents has been cloned into a duplicate object. - /// The new CustomOnionMessageContents is provided, and should be mutated as needed to perform a + /// Return a human-readable "debug" string describing this object + pub debug_str: extern "C" fn (this_arg: *const c_void) -> crate::c_types::Str, + /// Called, if set, after this OnionMessageContents has been cloned into a duplicate object. + /// The new OnionMessageContents is provided, and should be mutated as needed to perform a /// deep copy of the object pointed to by this_arg or avoid any double-freeing. - pub cloned: Option, + pub cloned: Option, /// 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 {} -pub(crate) fn CustomOnionMessageContents_clone_fields(orig: &CustomOnionMessageContents) -> CustomOnionMessageContents { - CustomOnionMessageContents { +unsafe impl Send for OnionMessageContents {} +unsafe impl Sync for OnionMessageContents {} +#[allow(unused)] +pub(crate) fn OnionMessageContents_clone_fields(orig: &OnionMessageContents) -> OnionMessageContents { + OnionMessageContents { this_arg: orig.this_arg, tlv_type: Clone::clone(&orig.tlv_type), write: Clone::clone(&orig.write), + debug_str: Clone::clone(&orig.debug_str), cloned: Clone::clone(&orig.cloned), free: Clone::clone(&orig.free), } } -impl lightning::util::ser::Writeable for CustomOnionMessageContents { +impl lightning::util::ser::Writeable for OnionMessageContents { 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()) } } +impl core::fmt::Debug for OnionMessageContents { + fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> { + f.write_str((self.debug_str)(self.this_arg).into_str()) + } +} #[no_mangle] -/// Creates a copy of a CustomOnionMessageContents -pub extern "C" fn CustomOnionMessageContents_clone(orig: &CustomOnionMessageContents) -> CustomOnionMessageContents { - let mut res = CustomOnionMessageContents_clone_fields(orig); +/// Creates a copy of a OnionMessageContents +pub extern "C" fn OnionMessageContents_clone(orig: &OnionMessageContents) -> OnionMessageContents { + let mut res = OnionMessageContents_clone_fields(orig); if let Some(f) = orig.cloned { (f)(&mut res) }; res } -impl Clone for CustomOnionMessageContents { +impl Clone for OnionMessageContents { fn clone(&self) -> Self { - CustomOnionMessageContents_clone(self) + OnionMessageContents_clone(self) } } -use lightning::onion_message::packet::CustomOnionMessageContents as rustCustomOnionMessageContents; -impl rustCustomOnionMessageContents for CustomOnionMessageContents { +use lightning::onion_message::packet::OnionMessageContents as rustOnionMessageContents; +impl rustOnionMessageContents for OnionMessageContents { fn tlv_type(&self) -> u64 { let mut ret = (self.tlv_type)(self.this_arg); ret @@ -323,21 +403,21 @@ impl rustCustomOnionMessageContents for CustomOnionMessageContents { // 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 { +impl core::ops::Deref for OnionMessageContents { type Target = Self; fn deref(&self) -> &Self { self } } -impl core::ops::DerefMut for CustomOnionMessageContents { +impl core::ops::DerefMut for OnionMessageContents { fn deref_mut(&mut self) -> &mut 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 { +pub extern "C" fn OnionMessageContents_free(this_ptr: OnionMessageContents) { } +impl Drop for OnionMessageContents { fn drop(&mut self) { if let Some(f) = self.free { f(self.this_arg);