Skip to content

Commit da34358

Browse files
authored
Merge pull request #69 from TheBlueMatt/main
2 parents 4995ea7 + 4e715a9 commit da34358

File tree

9 files changed

+1161
-294
lines changed

9 files changed

+1161
-294
lines changed

c-bindings-gen/src/main.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,21 +1245,38 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
12451245
writeln!(w, "\torig.clone()").unwrap();
12461246
writeln!(w, "}}").unwrap();
12471247
} else if path_matches_nongeneric(&trait_path.1, &["FromStr"]) {
1248-
if let Some(container) = types.get_c_mangled_container_type(
1249-
vec![&*i.self_ty, &syn::Type::Tuple(syn::TypeTuple { paren_token: Default::default(), elems: syn::punctuated::Punctuated::new() })],
1250-
Some(&gen_types), "Result") {
1248+
let mut err_opt = None;
1249+
for item in i.items.iter() {
1250+
match item {
1251+
syn::ImplItem::Type(ty) if format!("{}", ty.ident) == "Err" => {
1252+
err_opt = Some(&ty.ty);
1253+
},
1254+
_ => {}
1255+
}
1256+
}
1257+
let err_ty = err_opt.unwrap();
1258+
if let Some(container) = types.get_c_mangled_container_type(vec![&*i.self_ty, &err_ty], Some(&gen_types), "Result") {
12511259
writeln!(w, "#[no_mangle]").unwrap();
12521260
writeln!(w, "/// Read a {} object from a string", ident).unwrap();
12531261
writeln!(w, "pub extern \"C\" fn {}_from_str(s: crate::c_types::Str) -> {} {{", ident, container).unwrap();
12541262
writeln!(w, "\tmatch {}::from_str(s.into_str()) {{", resolved_path).unwrap();
1263+
12551264
writeln!(w, "\t\tOk(r) => {{").unwrap();
12561265
let new_var = types.write_to_c_conversion_new_var(w, &format_ident!("r"), &*i.self_ty, Some(&gen_types), false);
12571266
write!(w, "\t\t\tcrate::c_types::CResultTempl::ok(\n\t\t\t\t").unwrap();
12581267
types.write_to_c_conversion_inline_prefix(w, &*i.self_ty, Some(&gen_types), false);
12591268
write!(w, "{}r", if new_var { "local_" } else { "" }).unwrap();
12601269
types.write_to_c_conversion_inline_suffix(w, &*i.self_ty, Some(&gen_types), false);
12611270
writeln!(w, "\n\t\t\t)\n\t\t}},").unwrap();
1262-
writeln!(w, "\t\tErr(e) => crate::c_types::CResultTempl::err(()),").unwrap();
1271+
1272+
writeln!(w, "\t\tErr(e) => {{").unwrap();
1273+
let new_var = types.write_to_c_conversion_new_var(w, &format_ident!("e"), &err_ty, Some(&gen_types), false);
1274+
write!(w, "\t\t\tcrate::c_types::CResultTempl::err(\n\t\t\t\t").unwrap();
1275+
types.write_to_c_conversion_inline_prefix(w, &err_ty, Some(&gen_types), false);
1276+
write!(w, "{}e", if new_var { "local_" } else { "" }).unwrap();
1277+
types.write_to_c_conversion_inline_suffix(w, &err_ty, Some(&gen_types), false);
1278+
writeln!(w, "\n\t\t\t)\n\t\t}},").unwrap();
1279+
12631280
writeln!(w, "\t}}.into()\n}}").unwrap();
12641281
}
12651282
} else if path_matches_nongeneric(&trait_path.1, &["Display"]) {

c-bindings-gen/src/types.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,14 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
883883

884884
"core::convert::Infallible" => Some("crate::c_types::NotConstructable"),
885885

886+
"bitcoin::bech32::Error"|"bech32::Error"
887+
if !is_ref => Some("crate::c_types::Bech32Error"),
888+
"bitcoin::secp256k1::Error"|"secp256k1::Error"
889+
if !is_ref => Some("crate::c_types::Secp256k1Error"),
890+
891+
"core::num::ParseIntError" => Some("crate::c_types::Error"),
892+
"core::str::Utf8Error" => Some("crate::c_types::Error"),
893+
886894
"bitcoin::bech32::u5"|"bech32::u5" => Some("crate::c_types::u5"),
887895
"core::num::NonZeroU8" => Some("u8"),
888896

@@ -894,8 +902,6 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
894902
if is_ref => Some("*const [u8; 32]"),
895903
"bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
896904
if !is_ref => Some("crate::c_types::SecretKey"),
897-
"bitcoin::secp256k1::Error"|"secp256k1::Error"
898-
if !is_ref => Some("crate::c_types::Secp256k1Error"),
899905
"bitcoin::blockdata::script::Script" if is_ref => Some("crate::c_types::u8slice"),
900906
"bitcoin::blockdata::script::Script" if !is_ref => Some("crate::c_types::derived::CVec_u8Z"),
901907
"bitcoin::blockdata::transaction::OutPoint" => Some("crate::lightning::chain::transaction::OutPoint"),
@@ -961,6 +967,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
961967

962968
"core::convert::Infallible" => Some("panic!(\"You must never construct a NotConstructable! : "),
963969

970+
"bitcoin::bech32::Error"|"bech32::Error" if !is_ref => Some(""),
971+
"bitcoin::secp256k1::Error"|"secp256k1::Error" if !is_ref => Some(""),
972+
973+
"core::num::ParseIntError" => Some("u8::from_str_radix(\" a\", 10).unwrap_err() /*"),
974+
"core::str::Utf8Error" => Some("core::str::from_utf8(&[0xff]).unwrap_err() /*"),
975+
964976
"std::time::Duration"|"core::time::Duration" => Some("core::time::Duration::from_secs("),
965977
"std::time::SystemTime" => Some("(::std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs("),
966978

@@ -1043,6 +1055,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
10431055

10441056
"core::convert::Infallible" => Some("\")"),
10451057

1058+
"bitcoin::bech32::Error"|"bech32::Error" if !is_ref => Some(".into_rust()"),
1059+
"bitcoin::secp256k1::Error"|"secp256k1::Error" if !is_ref => Some(".into_rust()"),
1060+
1061+
"core::num::ParseIntError" => Some("*/"),
1062+
"core::str::Utf8Error" => Some("*/"),
1063+
10461064
"std::time::Duration"|"core::time::Duration" => Some(")"),
10471065
"std::time::SystemTime" => Some("))"),
10481066

@@ -1133,6 +1151,14 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
11331151

11341152
"core::convert::Infallible" => Some("panic!(\"Cannot construct an Infallible: "),
11351153

1154+
"bitcoin::bech32::Error"|"bech32::Error"
1155+
if !is_ref => Some("crate::c_types::Bech32Error::from_rust("),
1156+
"bitcoin::secp256k1::Error"|"secp256k1::Error"
1157+
if !is_ref => Some("crate::c_types::Secp256k1Error::from_rust("),
1158+
1159+
"core::num::ParseIntError" => Some("crate::c_types::Error { _dummy: 0 } /*"),
1160+
"core::str::Utf8Error" => Some("crate::c_types::Error { _dummy: 0 } /*"),
1161+
11361162
"bitcoin::bech32::u5"|"bech32::u5" => Some(""),
11371163

11381164
"bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey"
@@ -1143,8 +1169,6 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
11431169
if is_ref => Some(""),
11441170
"bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
11451171
if !is_ref => Some("crate::c_types::SecretKey::from_rust("),
1146-
"bitcoin::secp256k1::Error"|"secp256k1::Error"
1147-
if !is_ref => Some("crate::c_types::Secp256k1Error::from_rust("),
11481172
"bitcoin::blockdata::script::Script" if is_ref => Some("crate::c_types::u8slice::from_slice(&"),
11491173
"bitcoin::blockdata::script::Script" if !is_ref => Some(""),
11501174
"bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" if is_ref => Some("crate::c_types::Transaction::from_bitcoin("),
@@ -1206,6 +1230,14 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
12061230

12071231
"core::convert::Infallible" => Some("\")"),
12081232

1233+
"bitcoin::secp256k1::Error"|"bech32::Error"
1234+
if !is_ref => Some(")"),
1235+
"bitcoin::secp256k1::Error"|"secp256k1::Error"
1236+
if !is_ref => Some(")"),
1237+
1238+
"core::num::ParseIntError" => Some("*/"),
1239+
"core::str::Utf8Error" => Some("*/"),
1240+
12091241
"bitcoin::bech32::u5"|"bech32::u5" => Some(".into()"),
12101242

12111243
"bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey"
@@ -1216,8 +1248,6 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
12161248
if !is_ref => Some(")"),
12171249
"bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
12181250
if is_ref => Some(".as_ref()"),
1219-
"bitcoin::secp256k1::Error"|"secp256k1::Error"
1220-
if !is_ref => Some(")"),
12211251
"bitcoin::blockdata::script::Script" if is_ref => Some("[..])"),
12221252
"bitcoin::blockdata::script::Script" if !is_ref => Some(".into_bytes().into()"),
12231253
"bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" => Some(")"),

ldk-net/ldk_net.c

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,31 @@ static inline LDKSocketDescriptor get_descriptor(struct SocketHandler *handler,
168168
return ret;
169169
}
170170

171+
static LDKCOption_NetAddressZ get_remote_network_address(int fd) {
172+
struct sockaddr_storage sockaddr;
173+
socklen_t remote_addr_len = sizeof(sockaddr);
174+
if (getpeername(fd, (struct sockaddr*)&sockaddr, &remote_addr_len) == -1) {
175+
return COption_NetAddressZ_none();
176+
}
177+
178+
switch (sockaddr.ss_family) {
179+
case AF_INET: {
180+
const struct sockaddr_in *remote_addr = (struct sockaddr_in*)&sockaddr;
181+
LDKFourBytes addr;
182+
memcpy(&addr, &remote_addr->sin_addr.s_addr, 4);
183+
return COption_NetAddressZ_some(NetAddress_ipv4(addr, ntohs(remote_addr->sin_port)));
184+
}
185+
case AF_INET6: {
186+
const struct sockaddr_in6 *remote_addr = (struct sockaddr_in6*)&sockaddr;
187+
LDKSixteenBytes addr;
188+
memcpy(&addr, &remote_addr->sin6_addr.s6_addr, 16);
189+
return COption_NetAddressZ_some(NetAddress_ipv6(addr, ntohs(remote_addr->sin6_port)));
190+
}
191+
default:
192+
return COption_NetAddressZ_none();
193+
}
194+
}
195+
171196
static void *sock_thread_fn(void* arg) {
172197
struct SocketHandler *handler = (struct SocketHandler*) arg;
173198

@@ -214,10 +239,7 @@ static void *sock_thread_fn(void* arg) {
214239
if (newfd >= 0) {
215240
// Received a new connection, register it!
216241
LDKSocketDescriptor new_descriptor = get_descriptor(handler, newfd);
217-
LDKCOption_NetAddressZ remote_network_address = {
218-
.tag = LDKCOption_NetAddressZ_None,
219-
};
220-
LDKCResult_NonePeerHandleErrorZ con_res = PeerManager_new_inbound_connection(&handler->ldk_peer_manager, new_descriptor, remote_network_address);
242+
LDKCResult_NonePeerHandleErrorZ con_res = PeerManager_new_inbound_connection(&handler->ldk_peer_manager, new_descriptor, get_remote_network_address(newfd));
221243
if (con_res.result_ok) {
222244
if (register_socket(handler, newfd, 0))
223245
shutdown(newfd, SHUT_RDWR);
@@ -381,10 +403,7 @@ int socket_connect(void* arg, LDKPublicKey pubkey, struct sockaddr *addr, size_t
381403
if (register_socket(handler, fd, 0)) return -4;
382404

383405
LDKSocketDescriptor descriptor = get_descriptor(handler, fd);
384-
LDKCOption_NetAddressZ remote_network_address = {
385-
.tag = LDKCOption_NetAddressZ_None,
386-
};
387-
LDKCResult_CVec_u8ZPeerHandleErrorZ con_res = PeerManager_new_outbound_connection(&handler->ldk_peer_manager, pubkey, descriptor, remote_network_address);
406+
LDKCResult_CVec_u8ZPeerHandleErrorZ con_res = PeerManager_new_outbound_connection(&handler->ldk_peer_manager, pubkey, descriptor, get_remote_network_address(fd));
388407
if (con_res.result_ok) {
389408
ssize_t write_count = send(fd, con_res.contents.result->data, con_res.contents.result->datalen, MSG_NOSIGNAL);
390409
if (write_count != con_res.contents.result->datalen)

lightning-c-bindings/demo.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,9 @@ class PeersConnection {
331331
t2 = std::thread(&sock_read_data_thread, pipefds_1_to_2[0], &sock2, &net2);
332332

333333
// Note that we have to bind the result to a C++ class to make sure it gets free'd
334-
LDKCOption_NetAddressZ remote_network_address = {
335-
.tag = LDKCOption_NetAddressZ_None,
336-
};
337-
LDK::CResult_CVec_u8ZPeerHandleErrorZ con_res = PeerManager_new_outbound_connection(&net1, ChannelManager_get_our_node_id(&cm2), sock1, remote_network_address);
334+
LDK::CResult_CVec_u8ZPeerHandleErrorZ con_res = PeerManager_new_outbound_connection(&net1, ChannelManager_get_our_node_id(&cm2), sock1, COption_NetAddressZ_none());
338335
assert(con_res->result_ok);
339-
LDK::CResult_NonePeerHandleErrorZ con_res2 = PeerManager_new_inbound_connection(&net2, sock2, remote_network_address);
336+
LDK::CResult_NonePeerHandleErrorZ con_res2 = PeerManager_new_inbound_connection(&net2, sock2, COption_NetAddressZ_none());
340337
assert(con_res2->result_ok);
341338

342339
auto writelen = write(pipefds_1_to_2[1], con_res->contents.result->data, con_res->contents.result->datalen);

0 commit comments

Comments
 (0)