1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
4 // This file is licensed under the license available in the LICENSE or LICENSE.md
5 // file in the root of this repository or, if no such file exists, the same
6 // license as that which applies to the original source files from which this
7 // source was automatically generated.
9 //! Wire encoding/decoding for Lightning messages according to [BOLT #1], and for
10 //! custom message through the [`CustomMessageReader`] trait.
12 //! [BOLT #1]: https://github.com/lightning/bolts/blob/master/01-messaging.md
14 use alloc::str::FromStr;
15 use alloc::string::String;
16 use core::ffi::c_void;
17 use core::convert::Infallible;
18 use bitcoin::hashes::Hash;
19 use crate::c_types::*;
20 #[cfg(feature="no-std")]
21 use alloc::{vec::Vec, boxed::Box};
23 /// Trait to be implemented by custom message (unrelated to the channel/gossip LN layers)
26 pub struct CustomMessageReader {
27 /// An opaque pointer which is passed to your function implementations as an argument.
28 /// This has no meaning in the LDK, and can be NULL or any other value.
29 pub this_arg: *mut c_void,
30 /// Decodes a custom message to `CustomMessageType`. If the given message type is known to the
31 /// implementation and the message could be decoded, must return `Ok(Some(message))`. If the
32 /// message type is unknown to the implementation, must return `Ok(None)`. If a decoding error
33 /// occur, must return `Err(DecodeError::X)` where `X` details the encountered error.
34 pub read: extern "C" fn (this_arg: *const c_void, message_type: u16, buffer: crate::c_types::u8slice) -> crate::c_types::derived::CResult_COption_TypeZDecodeErrorZ,
35 /// Frees any resources associated with this object given its this_arg pointer.
36 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
37 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
39 unsafe impl Send for CustomMessageReader {}
40 unsafe impl Sync for CustomMessageReader {}
42 pub(crate) fn CustomMessageReader_clone_fields(orig: &CustomMessageReader) -> CustomMessageReader {
44 this_arg: orig.this_arg,
45 read: Clone::clone(&orig.read),
46 free: Clone::clone(&orig.free),
50 use lightning::ln::wire::CustomMessageReader as rustCustomMessageReader;
51 impl rustCustomMessageReader for CustomMessageReader {
52 type CustomMessage = crate::lightning::ln::wire::Type;
53 fn read<R:crate::c_types::io::Read>(&self, mut message_type: u16, mut buffer: &mut R) -> Result<Option<crate::lightning::ln::wire::Type>, lightning::ln::msgs::DecodeError> {
54 let mut ret = (self.read)(self.this_arg, message_type, crate::c_types::u8slice::from_vec(&crate::c_types::reader_to_vec(buffer)));
55 let mut local_ret = match ret.result_ok { true => Ok( { let mut local_ret_0 = { /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) })*/ let ret_0_opt = (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }); if ret_0_opt.is_none() { None } else { Some({ { { ret_0_opt.take() } }})} }; local_ret_0 }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).into_native() })};
60 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
61 // directly as a Deref trait in higher-level structs:
62 impl core::ops::Deref for CustomMessageReader {
64 fn deref(&self) -> &Self {
68 impl core::ops::DerefMut for CustomMessageReader {
69 fn deref_mut(&mut self) -> &mut Self {
73 /// Calls the free function if one is set
75 pub extern "C" fn CustomMessageReader_free(this_ptr: CustomMessageReader) { }
76 impl Drop for CustomMessageReader {
78 if let Some(f) = self.free {
85 use alloc::str::FromStr;
86 use alloc::string::String;
87 use core::ffi::c_void;
88 use core::convert::Infallible;
89 use bitcoin::hashes::Hash;
90 use crate::c_types::*;
91 #[cfg(feature="no-std")]
92 use alloc::{vec::Vec, boxed::Box};
95 /// Defines a type identifier for sending messages over the wire.
97 /// Messages implementing this trait specify a type and must be [`Writeable`].
100 /// An opaque pointer which is passed to your function implementations as an argument.
101 /// This has no meaning in the LDK, and can be NULL or any other value.
102 pub this_arg: *mut c_void,
103 /// Returns the type identifying the message payload.
104 pub type_id: extern "C" fn (this_arg: *const c_void) -> u16,
105 /// Return a human-readable "debug" string describing this object
106 pub debug_str: extern "C" fn (this_arg: *const c_void) -> crate::c_types::Str,
107 /// Serialize the object into a byte array
108 pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
109 /// Called, if set, after this Type has been cloned into a duplicate object.
110 /// The new Type is provided, and should be mutated as needed to perform a
111 /// deep copy of the object pointed to by this_arg or avoid any double-freeing.
112 pub cloned: Option<extern "C" fn (new_Type: &mut Type)>,
113 /// Frees any resources associated with this object given its this_arg pointer.
114 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
115 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
117 unsafe impl Send for Type {}
118 unsafe impl Sync for Type {}
120 pub(crate) fn Type_clone_fields(orig: &Type) -> Type {
122 this_arg: orig.this_arg,
123 type_id: Clone::clone(&orig.type_id),
124 debug_str: Clone::clone(&orig.debug_str),
125 write: Clone::clone(&orig.write),
126 cloned: Clone::clone(&orig.cloned),
127 free: Clone::clone(&orig.free),
130 impl core::fmt::Debug for Type {
131 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
132 f.write_str((self.debug_str)(self.this_arg).into_str())
135 impl lightning::util::ser::Writeable for Type {
136 fn write<W: lightning::util::ser::Writer>(&self, w: &mut W) -> Result<(), crate::c_types::io::Error> {
137 let vec = (self.write)(self.this_arg);
138 w.write_all(vec.as_slice())
142 /// Creates a copy of a Type
143 pub extern "C" fn Type_clone(orig: &Type) -> Type {
144 let mut res = Type_clone_fields(orig);
145 if let Some(f) = orig.cloned { (f)(&mut res) };
148 impl Clone for Type {
149 fn clone(&self) -> Self {
154 use lightning::ln::wire::Type as rustType;
155 impl rustType for Type {
156 fn type_id(&self) -> u16 {
157 let mut ret = (self.type_id)(self.this_arg);
162 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
163 // directly as a Deref trait in higher-level structs:
164 impl core::ops::Deref for Type {
166 fn deref(&self) -> &Self {
170 impl core::ops::DerefMut for Type {
171 fn deref_mut(&mut self) -> &mut Self {
175 /// Calls the free function if one is set
177 pub extern "C" fn Type_free(this_ptr: Type) { }
180 if let Some(f) = self.free {