Skip to content

Commit 5420c9a

Browse files
committed
fix: update libcoap to development state, allow setting mbedtls path
1 parent b5f0fd2 commit 5420c9a

File tree

9 files changed

+69
-32
lines changed

9 files changed

+69
-32
lines changed

.idea/libcoap-rs.iml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/Test.xml

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libcoap-sys/build.rs

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ fn main() {
6161
if dtls_backend.is_some() {
6262
multiple_backends = true;
6363
}
64+
println!("cargo:rerun-if-env-changed=MBEDTLS_LIBRARY_PATH");
6465
dtls_backend = Some(DtlsBackend::MbedTls);
6566
}
6667
if cfg!(feature = "dtls_backend_openssl") {
@@ -187,8 +188,28 @@ fn main() {
187188
.unwrap()
188189
.join("build")
189190
.join("library");
190-
build_config.env("CFLAGS", format!("-I{}", mbedtls_include.to_str().unwrap()));
191-
build_config.env("LDFLAGS", format!("-L{}", mbedtls_library_path.to_str().unwrap()));
191+
build_config.env("MbedTLS_CFLAGS", format!("-I{}", mbedtls_include.to_str().unwrap()));
192+
build_config.env("MbedTLS_LIBS", format!("-lmbedtls -lmbedcrypto -lmbedx509 -L{}", mbedtls_library_path.to_str().unwrap()));
193+
} else {
194+
// If we're not vendoring mbedtls, allow manually setting the mbedtls
195+
// library path (required if linking statically, which is always the case
196+
// when vendoring libcoap).
197+
if let Some(mbedtls_lib_path) = env::var_os("MBEDTLS_LIBRARY_PATH") {
198+
build_config.env("MbedTLS_LIBS", format!("-lmbedtls -lmbedcrypto -lmbedx509 -L{}", mbedtls_lib_path.to_str().unwrap()));
199+
if let Some(mbedtls_include_path) = env::var_os("MBEDTLS_INCLUDE_PATH") {
200+
build_config.env("MbedTLS_CFLAGS", format!("-I{}", mbedtls_include_path.to_str().unwrap()));
201+
}
202+
} else {
203+
// mbedtls will get pkg-config support in the near future, prepare for that
204+
if let Ok(lib) = &pkg_config::Config::new().cargo_metadata(false).probe("mbedtls") {
205+
let mut lib_flags = "-lmbedtls -lmbedcrypto -lmbedx509".to_string();
206+
lib_flags.push_str(lib.link_paths.iter().map(|x| format!("-L{} ", x.display())).collect::<String>().as_str());
207+
build_config.env("MbedTLS_LIBS", lib_flags);
208+
build_config.env("MbedTLS_CFLAGS", lib.link_paths.iter().map(|x| format!("-I{}", x.display())).collect::<String>());
209+
} else {
210+
println!("cargo:warning=You have enabled libcoap vendoring with mbedtls, but haven't provided a static library path for mbedtls (MBEDTLS_LIBRARY_PATH environment variable is unset). Building might fail because of that.");
211+
}
212+
}
192213
}
193214
},
194215
DtlsBackend::GnuTls => {
@@ -214,7 +235,9 @@ fn main() {
214235
// Disable tests and examples as well as test coverage
215236
.disable("tests", None)
216237
.disable("examples", None)
217-
.disable("gcov", None);
238+
.disable("gcov", None)
239+
// TODO allow multithreaded access
240+
.disable("thread-safe", None);
218241

219242
// Enable debug symbols if enabled in Rust
220243
match std::env::var_os("DEBUG").unwrap().to_str().unwrap() {
@@ -255,7 +278,7 @@ fn main() {
255278
// Tell cargo to link libcoap.
256279
println!(
257280
"cargo:rustc-link-lib={}{}",
258-
cfg!(feature = "static").then(|| "static=").unwrap_or(""),
281+
cfg!(feature = "static").then(|| "static=").unwrap_or("dylib="),
259282
format!(
260283
"coap-3-{}",
261284
&dtls_backend
@@ -265,10 +288,10 @@ fn main() {
265288
)
266289
.as_str()
267290
);
268-
291+
269292
// For the DTLS libraries, we need to tell cargo which external libraries to link.
270293
// Note that these linker instructions have to be added *after* the linker instruction
271-
// for libcoap itself, as some linkers require dependencies to be in reverse order.
294+
// for libcoap itself, as some linkers require dependencies to be in reverse order.
272295
if let Some(dtls_backend) = dtls_backend {
273296
match dtls_backend {
274297
DtlsBackend::TinyDtls => {
@@ -280,15 +303,25 @@ fn main() {
280303
DtlsBackend::MbedTls => {
281304
// If mbedtls is vendored, mbedtls-sys-auto already takes care of linking.
282305
if env::var_os("DEP_MBEDTLS_INCLUDE").is_none() {
283-
// We aren't using mbedtls-sys-auto if we aren't vendoring (as it doesn't support
306+
// We aren't using mbedtls-sys-auto if we aren't vendoring (as it doesn't support
284307
// mbedtls >= 3.0.0), so we need to tell cargo to link to mbedtls ourselves.
285-
286-
// Try to find mbedtls using pkg-config
287-
if probe_library("mbedtls").is_err() {
288-
// couldn't find using pkg-config, just try linking with default library search path
289-
println!("cargo:rustc-link-lib=mbedtls");
290-
println!("cargo:rustc-link-lib=mbedx509");
291-
println!("cargo:rustc-link-lib=mbedcrypto");
308+
309+
if let Some(mbedtls_lib_path) = env::var_os("MBEDTLS_LIBRARY_PATH") {
310+
println!("cargo:rustc-link-search=native={}", mbedtls_lib_path.to_str().unwrap())
311+
}
312+
// Try to find mbedtls using pkg-config, will emit cargo link statements if successful
313+
if env::var_os("MBEDTLS_LIBRARY_PATH").is_some() || pkg_config::Config::new().statik(cfg!(feature = "static")).probe("mbedtls").is_err() {
314+
// couldn't find using pkg-config or MBEDTLS_LIBRARY_PATH was set, just try
315+
// linking with given library search path
316+
println!("cargo:rustc-link-lib={}mbedtls",
317+
cfg!(feature = "static").then(|| "static=").unwrap_or("dylib=")
318+
);
319+
println!("cargo:rustc-link-lib={}mbedx509",
320+
cfg!(feature = "static").then(|| "static=").unwrap_or("dylib=")
321+
);
322+
println!("cargo:rustc-link-lib={}mbedcrypto",
323+
cfg!(feature = "static").then(|| "static=").unwrap_or("dylib=")
324+
);
292325
}
293326
}
294327
},

libcoap-sys/src/lib.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,6 @@ include!(concat!(env!("OUT_DIR"), "\\bindings.rs"));
104104
#[cfg(not(target_family = "windows"))]
105105
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
106106

107-
#[inline]
108-
pub unsafe fn coap_send_rst(
109-
session: *mut coap_session_t,
110-
request: *const coap_pdu_t,
111-
_type_: coap_pdu_type_t,
112-
) -> coap_mid_t {
113-
coap_send_message_type(session, request, COAP_MESSAGE_RST)
114-
}
115-
116107
#[cfg(test)]
117108
mod tests {
118109
use std::{

libcoap-sys/src/libcoap

Submodule libcoap updated 251 files

libcoap/src/context.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//! Module containing context-internal types and traits.
1111
1212
use std::{any::Any, ffi::c_void, fmt::Debug, marker::PhantomData, net::SocketAddr, ops::Sub, time::Duration};
13+
use std::sync::Once;
1314

1415
use libc::c_uint;
1516

@@ -22,8 +23,11 @@ use libcoap_sys::{
2223
coap_dtls_spsk_info_t, coap_dtls_spsk_t, coap_event_t, coap_free_context, coap_get_app_data, coap_io_process,
2324
coap_new_context, coap_proto_t, coap_register_event_handler, coap_register_response_handler, coap_set_app_data,
2425
COAP_BLOCK_SINGLE_BODY, COAP_BLOCK_USE_LIBCOAP, COAP_DTLS_SPSK_SETUP_VERSION, COAP_IO_WAIT,
26+
coap_startup
2527
};
2628

29+
static COAP_STARTUP_ONCE: Once = Once::new();
30+
2731
#[cfg(feature = "dtls")]
2832
use crate::crypto::{dtls_server_id_callback, dtls_server_sni_callback, CoapServerCryptoProvider};
2933
#[cfg(feature = "dtls")]
@@ -94,6 +98,10 @@ impl<'a> CoapContext<'a> {
9498
/// Returns an error if the underlying libcoap library was unable to create a new context
9599
/// (probably an allocation error?).
96100
pub fn new() -> Result<CoapContext<'a>, ContextCreationError> {
101+
// TODO this should actually be done before calling _any_ libcoap function, not just the
102+
// context initialization. Maybe we need to make sure to call this in other places too
103+
// (e.g. if a resource is initialized before a context is created).
104+
COAP_STARTUP_ONCE.call_once(|| unsafe { coap_startup(); });
97105
// SAFETY: Providing null here is fine, the context will just not be bound to an endpoint
98106
// yet.
99107
let raw_context = unsafe { coap_new_context(std::ptr::null()) };
@@ -102,7 +110,7 @@ impl<'a> CoapContext<'a> {
102110
}
103111
// SAFETY: We checked that raw_context is not null.
104112
unsafe {
105-
coap_context_set_block_mode(raw_context, (COAP_BLOCK_USE_LIBCOAP | COAP_BLOCK_SINGLE_BODY) as u8);
113+
coap_context_set_block_mode(raw_context, (COAP_BLOCK_USE_LIBCOAP | COAP_BLOCK_SINGLE_BODY) as u32);
106114
coap_register_response_handler(raw_context, Some(session_response_handler));
107115
}
108116
let inner = CoapLendableFfiRcCell::new(CoapContextInner {

libcoap/src/resource.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub unsafe fn prepare_resource_handler_data<'a, D: Any + ?Sized + Debug>(
9595
match (request, response) {
9696
(Ok(request), Ok(response)) => Ok((resource, session, request, response)),
9797
(v1, v2) => {
98-
coap_send_rst(raw_session, raw_incoming_pdu, COAP_MESSAGE_RST);
98+
coap_send_rst(raw_session, raw_incoming_pdu);
9999
Err(v1.and(v2).err().unwrap())
100100
},
101101
}

libcoap/tests/dtls_client_server_test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use libcoap_rs::{
2323
session::CoapSessionCommon,
2424
CoapContext,
2525
};
26+
use libcoap_sys::coap_startup;
2627

2728
mod common;
2829

0 commit comments

Comments
 (0)