-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for DTLS and other transport layers, make client async
features: - Client now uses async and tokio - Observe now takes ownership of the client to avoid missed packets - DTLS is supported using webrtc-rs - DTLS is a feature, can be turned off if needed - Other backends for transport can be provided by users. They must be able to provide a SockAddr for use with observe, butthis could be theoretically hardcoded if you want to use some non-ip protocol - Server is reworked to support multiple listeners at the same time - Server should no longer block on a single long async request handlers - Added tests for DTLS using PSK and PKI - Added example for DTLS using PSK - Moved all tests to use `tokio::test`
- Loading branch information
Jose Ignacio Biehl
authored and
Jose Ignacio Biehl
committed
Jan 5, 2024
1 parent
773aacf
commit 3c82e50
Showing
19 changed files
with
1,661 additions
and
872 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/// This example shows how to use DTLS with coap-rs. If you want to use PKI, please take | ||
/// a look at the test in dtls.rs | ||
extern crate coap; | ||
use coap::client::CoAPClient; | ||
use coap::dtls::DTLSConfig; | ||
use coap::Server; | ||
use coap_lite::{CoapRequest, RequestType as Method}; | ||
use std::future::Future; | ||
use std::net::{SocketAddr, ToSocketAddrs}; | ||
use std::sync::Arc; | ||
use tokio::sync::mpsc; | ||
use webrtc_dtls::cipher_suite::CipherSuiteId; | ||
use webrtc_dtls::config::Config; | ||
use webrtc_dtls::listener::listen; | ||
use webrtc_util::conn::Listener as WebRtcListener; | ||
|
||
pub fn spawn_dtls_server< | ||
F: Fn(Box<CoapRequest<SocketAddr>>) -> HandlerRet + Send + Sync + 'static, | ||
HandlerRet, | ||
>( | ||
ip: &'static str, | ||
request_handler: F, | ||
config: webrtc_dtls::config::Config, | ||
) -> mpsc::UnboundedReceiver<u16> | ||
where | ||
HandlerRet: Future<Output = Box<CoapRequest<SocketAddr>>> + Send, | ||
{ | ||
let (tx, rx) = mpsc::unbounded_channel(); | ||
tokio::spawn(async move { | ||
let listener = listen(ip, config).await.unwrap(); | ||
let listen_port = listener.addr().await.unwrap().port(); | ||
let listener = Box::new(listener); | ||
let server = Server::from_listeners(vec![listener]); | ||
tx.send(listen_port).unwrap(); | ||
server.run(request_handler).await.unwrap(); | ||
}); | ||
|
||
rx | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let config = Config { | ||
psk: Some(Arc::new(|_| Ok(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 0]))), | ||
psk_identity_hint: Some("webrtc-rs DTLS Server".as_bytes().to_vec()), | ||
cipher_suites: vec![CipherSuiteId::Tls_Psk_With_Aes_128_Ccm_8], | ||
server_name: "localhost".to_string(), | ||
..Default::default() | ||
}; | ||
let server_port = spawn_dtls_server( | ||
"127.0.0.1:0", | ||
|mut req| async { | ||
req.response.as_mut().unwrap().message.payload = b"hello dtls".to_vec(); | ||
req | ||
}, | ||
config.clone(), | ||
) | ||
.recv() | ||
.await | ||
.unwrap(); | ||
|
||
let dtls_config = DTLSConfig { | ||
config, | ||
dest_addr: ("127.0.0.1", server_port) | ||
.to_socket_addrs() | ||
.unwrap() | ||
.next() | ||
.unwrap(), | ||
}; | ||
|
||
let mut client = CoAPClient::from_dtls_config(dtls_config) | ||
.await | ||
.expect("could not create client"); | ||
let domain = format!("127.0.0.1:{}", server_port); | ||
let resp = client | ||
.request_path("/hello", Method::Get, None, None, Some(domain.to_string())) | ||
.await | ||
.unwrap(); | ||
println!( | ||
"receive on client: {}", | ||
std::str::from_utf8(&resp.message.payload).unwrap() | ||
); | ||
assert_eq!(resp.message.payload, b"hello dtls".to_vec()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.