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/lightningnetwork/lightning-rfc/blob/master/01-messaging.md
14 use std::str::FromStr;
16 use core::convert::Infallible;
17 use bitcoin::hashes::Hash;
18 use crate::c_types::*;
20 /// Trait to be implemented by custom message (unrelated to the channel/gossip LN layers)
23 pub struct CustomMessageReader {
24 /// An opaque pointer which is passed to your function implementations as an argument.
25 /// This has no meaning in the LDK, and can be NULL or any other value.
26 pub this_arg: *mut c_void,
27 /// Decodes a custom message to `CustomMessageType`. If the given message type is known to the
28 /// implementation and the message could be decoded, must return `Ok(Some(message))`. If the
29 /// message type is unknown to the implementation, must return `Ok(None)`. If a decoding error
30 /// occur, must return `Err(DecodeError::X)` where `X` details the encountered error.
32 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,
33 /// Frees any resources associated with this object given its this_arg pointer.
34 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
35 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
37 unsafe impl Send for CustomMessageReader {}
38 unsafe impl Sync for CustomMessageReader {}
40 pub(crate) extern "C" fn CustomMessageReader_clone_fields(orig: &CustomMessageReader) -> CustomMessageReader {
42 this_arg: orig.this_arg,
43 read: Clone::clone(&orig.read),
44 free: Clone::clone(&orig.free),
48 use lightning::ln::wire::CustomMessageReader as rustCustomMessageReader;
49 impl rustCustomMessageReader for CustomMessageReader {
50 type CustomMessage = crate::lightning::ln::wire::Type;
51 fn read<R:std::io::Read>(&self, mut message_type: u16, mut buffer: &mut R) -> Result<Option<crate::lightning::ln::wire::Type>, lightning::ln::msgs::DecodeError> {
52 let mut ret = (self.read)(self.this_arg, message_type, crate::c_types::u8slice::from_vec(&crate::c_types::reader_to_vec(buffer)));
53 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((*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).take_inner()) } })};
58 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
59 // directly as a Deref trait in higher-level structs:
60 impl std::ops::Deref for CustomMessageReader {
62 fn deref(&self) -> &Self {
66 /// Calls the free function if one is set
68 pub extern "C" fn CustomMessageReader_free(this_ptr: CustomMessageReader) { }
69 impl Drop for CustomMessageReader {
71 if let Some(f) = self.free {
78 use std::str::FromStr;
80 use core::convert::Infallible;
81 use bitcoin::hashes::Hash;
82 use crate::c_types::*;
85 /// Defines a type identifier for sending messages over the wire.
87 /// Messages implementing this trait specify a type and must be [`Writeable`].
90 /// An opaque pointer which is passed to your function implementations as an argument.
91 /// This has no meaning in the LDK, and can be NULL or any other value.
92 pub this_arg: *mut c_void,
93 /// Returns the type identifying the message payload.
95 pub type_id: extern "C" fn (this_arg: *const c_void) -> u16,
96 /// Return a human-readable "debug" string describing this object
97 pub debug_str: extern "C" fn (this_arg: *const c_void) -> crate::c_types::Str,
98 /// Serialize the object into a byte array
99 pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
100 /// Frees any resources associated with this object given its this_arg pointer.
101 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
102 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
104 unsafe impl Send for Type {}
105 unsafe impl Sync for Type {}
107 pub(crate) extern "C" fn Type_clone_fields(orig: &Type) -> Type {
109 this_arg: orig.this_arg,
110 type_id: Clone::clone(&orig.type_id),
111 debug_str: Clone::clone(&orig.debug_str),
112 write: Clone::clone(&orig.write),
113 free: Clone::clone(&orig.free),
116 impl core::fmt::Debug for Type {
117 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
118 f.write_str((self.debug_str)(self.this_arg).into_str())
121 impl lightning::util::ser::Writeable for Type {
122 fn write<W: lightning::util::ser::Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
123 let vec = (self.write)(self.this_arg);
124 w.write_all(vec.as_slice())
128 use lightning::ln::wire::Type as rustType;
129 impl rustType for Type {
130 fn type_id(&self) -> u16 {
131 let mut ret = (self.type_id)(self.this_arg);
136 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
137 // directly as a Deref trait in higher-level structs:
138 impl std::ops::Deref for Type {
140 fn deref(&self) -> &Self {
144 /// Calls the free function if one is set
146 pub extern "C" fn Type_free(this_ptr: Type) { }
149 if let Some(f) = self.free {