-
Couldn't load subscription status.
- Fork 4
tokio based async support #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
3c83311
ebb9d4f
1a6e704
f9259ca
1524fa3
9c5881b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| use std::time::Duration; | ||
| use futures::pin_mut; | ||
| use tokio::{net, time, io::{AsyncReadExt, AsyncWriteExt}}; | ||
| use udp_dtls::{Certificate, DtlsAcceptor, DtlsConnector, CertificateIdentity, SrtpProfile, AsyncUdpChannel}; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() { | ||
| let buffer = include_bytes!("../test/identity.p12"); | ||
| let identity = CertificateIdentity::from_pkcs12(buffer, "mypass").unwrap(); | ||
|
|
||
| let root_ca = include_bytes!("../test/root-ca.der"); | ||
| let root_ca = Certificate::from_der(root_ca).unwrap(); | ||
|
|
||
| let acceptor = DtlsAcceptor::builder(identity).build().unwrap(); | ||
| let connector = DtlsConnector::builder() | ||
| .add_srtp_profile(SrtpProfile::Aes128CmSha180) | ||
| .add_srtp_profile(SrtpProfile::AeadAes256Gcm) | ||
| .add_root_certificate(root_ca) | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| let server = net::UdpSocket::bind("127.0.0.1:0").await.unwrap(); | ||
| let client = net::UdpSocket::bind("127.0.0.1:0").await.unwrap(); | ||
|
|
||
| let server_addr = server.local_addr().unwrap(); | ||
| let client_addr = client.local_addr().unwrap(); | ||
|
|
||
| let server_channel = AsyncUdpChannel { | ||
| socket: server, | ||
| remote_addr: client_addr, | ||
| }; | ||
|
|
||
| let client_channel = AsyncUdpChannel { | ||
| socket: client, | ||
| remote_addr: server_addr, | ||
| }; | ||
|
|
||
| let connector_task = async move { | ||
| let stream = connector.async_connect("foobar.com", client_channel).await.unwrap(); | ||
|
|
||
| pin_mut!(stream); | ||
| loop { | ||
| let buf = b"hello"; | ||
| stream.write_all(buf).await.unwrap(); | ||
|
|
||
| time::delay_for(Duration::from_millis(30)).await; | ||
| } | ||
| }; | ||
|
|
||
| let acceptor_task = async move { | ||
| let stream = acceptor.async_accept(server_channel).await.unwrap(); | ||
| let mut count = 0; | ||
|
|
||
| pin_mut!(stream); | ||
| loop { | ||
| let mut received = [0; 5]; | ||
|
|
||
| stream.read_exact(&mut received).await.unwrap(); | ||
|
|
||
| println!( | ||
| "{:?} {:?}", | ||
| count, | ||
| String::from_utf8_lossy(received.as_ref()) | ||
| ); | ||
|
|
||
| count = count + 1; | ||
|
|
||
| time::delay_for(Duration::from_millis(2)).await; | ||
| } | ||
| }; | ||
|
|
||
| tokio::spawn(connector_task); | ||
| tokio::spawn(acceptor_task).await.unwrap(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,15 @@ | ||
| use crate::openssl::try_set_supported_protocols; | ||
| use crate::{DtlsAcceptorBuilder, DtlsStream, HandshakeError, CertificateIdentity, Protocol, Result}; | ||
| use crate::{DtlsAcceptorBuilder, SyncDtlsStream, HandshakeError, CertificateIdentity, Protocol, Result}; | ||
| use openssl::ssl::{SslAcceptor, SslMethod}; | ||
| use std::{fmt, io, result}; | ||
|
|
||
| #[cfg(feature="async")] | ||
| use crate::{AsyncDtlsStream, AsyncConnectError}; | ||
| #[cfg(feature="async")] | ||
| use tokio_openssl; | ||
| #[cfg(feature="async")] | ||
| use tokio::io::{AsyncRead, AsyncWrite}; | ||
|
|
||
| /// Acceptor for incoming UDP sessions secured with DTLS. | ||
| #[derive(Clone)] | ||
| pub struct DtlsAcceptor(SslAcceptor); | ||
|
|
@@ -75,12 +82,25 @@ impl DtlsAcceptor { | |
| pub fn accept<S: fmt::Debug>( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we support this builder to work for async? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is in progress There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done and pushed to the same branch |
||
| &self, | ||
| stream: S, | ||
| ) -> result::Result<DtlsStream<S>, HandshakeError<S>> | ||
| ) -> result::Result<SyncDtlsStream<S>, HandshakeError<S>> | ||
| where | ||
| S: io::Read + io::Write, | ||
| { | ||
| let stream = self.0.accept(stream)?; | ||
| Ok(DtlsStream::from(stream)) | ||
| Ok(SyncDtlsStream::from(stream)) | ||
| } | ||
|
|
||
| #[cfg(feature="async")] | ||
| pub async fn async_accept<S: fmt::Debug>( | ||
| &self, | ||
| stream: S | ||
| ) -> std::result::Result<AsyncDtlsStream<S>, AsyncConnectError<S>> | ||
| where | ||
| S: AsyncRead + AsyncWrite + Unpin | ||
| { | ||
| let stream = tokio_openssl::accept(&self.0, stream).await.map_err(AsyncConnectError::TokioOpenSsl)?; | ||
|
|
||
| Ok(AsyncDtlsStream::from(stream)) | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love this to be an option. Great!