aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/encoding/headermap.rs3
-rw-r--r--src/encoding/headers.rs36
-rw-r--r--src/transaction/auth.rs51
-rw-r--r--src/transaction/mod.rs23
-rw-r--r--src/transport/udp.rs2
5 files changed, 110 insertions, 5 deletions
diff --git a/src/encoding/headermap.rs b/src/encoding/headermap.rs
index 5d1fa0a..01e1962 100644
--- a/src/encoding/headermap.rs
+++ b/src/encoding/headermap.rs
@@ -25,6 +25,9 @@ impl HeaderMap {
pub fn get<H: Header>(&self) -> Option<Result<H>> {
self.get_raw(H::NAME).map(H::from_str)
}
+ pub fn get_res<H: Header>(&self) -> Result<H> {
+ self.get().ok_or(anyhow!("{} header missing", H::NAME))?
+ }
pub fn insert_raw(&mut self, key: String, value: String) {
self.0.push((key, value))
}
diff --git a/src/encoding/headers.rs b/src/encoding/headers.rs
index 9e785f0..afcfef1 100644
--- a/src/encoding/headers.rs
+++ b/src/encoding/headers.rs
@@ -1,4 +1,4 @@
-use super::{headermap::HeaderMap, method::Method};
+use super::{headermap::HeaderMap, method::Method, uri::Uri};
use anyhow::{anyhow, bail, Result};
use std::{fmt::Display, str::FromStr};
@@ -31,7 +31,6 @@ header!("Content-Length", struct ContentLength(usize));
header!("Content-Type", struct ContentType(String));
header!("Call-ID", struct CallID(String));
header!("Via", struct Via(String));
-header!("Contact", struct Contact(String));
header!("Max-Forwards", struct MaxForwards(usize));
header!("From", struct From(String));
header!("To", struct To(String));
@@ -100,7 +99,7 @@ impl FromStr for WWWAuthenticate {
}
}
-#[derive(Debug)]
+#[derive(Debug, Clone)]
pub struct Authorization {
pub username: String,
pub realm: String,
@@ -140,3 +139,34 @@ pub fn unquote(v: &str) -> Result<String> {
.ok_or(anyhow!("end quote missing"))?
.to_string())
}
+
+#[derive(Debug)]
+pub struct Contact {
+ pub display_name: Option<String>,
+ pub uri: Uri,
+ pub params: String,
+}
+
+impl Header for Contact {
+ const NAME: &'static str = "Contact";
+}
+impl Display for Contact {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ let Self {
+ display_name,
+ uri,
+ params,
+ } = self;
+ if let Some(display_name) = display_name {
+ write!(f, "{display_name} <{uri}>{params}")
+ } else {
+ write!(f, "<{uri}>{params}")
+ }
+ }
+}
+impl FromStr for Contact {
+ type Err = anyhow::Error;
+ fn from_str(_s: &str) -> Result<Self, Self::Err> {
+ todo!()
+ }
+}
diff --git a/src/transaction/auth.rs b/src/transaction/auth.rs
new file mode 100644
index 0000000..f64bb4b
--- /dev/null
+++ b/src/transaction/auth.rs
@@ -0,0 +1,51 @@
+use crate::encoding::{
+ headers::{Authorization, WWWAuthenticate},
+ method::Method,
+ request::Request,
+ response::Response,
+ uri::Uri,
+};
+use anyhow::Result;
+
+impl Authorization {
+ pub fn construct(
+ request: &Request,
+ failed_response: &Response,
+ username: &str,
+ password: &str,
+ ) -> Result<Authorization> {
+ let challenge = failed_response.headers.get::<WWWAuthenticate>().unwrap()?;
+
+ Ok(Authorization {
+ response: response_digest(
+ username.to_string(),
+ challenge.realm.clone(),
+ password.to_string(),
+ request.method,
+ challenge.nonce.clone(),
+ request.uri.clone(),
+ ),
+ nonce: challenge.nonce,
+ realm: challenge.realm,
+ uri: request.uri.content.clone(),
+ username: username.to_string(),
+ })
+ }
+}
+
+fn response_digest(
+ username: String,
+ realm: String,
+ password: String,
+ method: Method,
+ nonce: String,
+ uri: Uri,
+) -> String {
+ let h = |s: String| hex::encode(md5::compute(s.as_bytes()).0);
+ let kd = |secret, data| h(format!("{secret}:{data}"));
+
+ let a1 = format!("{username}:{realm}:{password}");
+ let a2 = format!("{method}:{uri}");
+ let response_digest = kd(h(a1), format!("{nonce}:{}", h(a2)));
+ return response_digest;
+}
diff --git a/src/transaction/mod.rs b/src/transaction/mod.rs
index 3be9544..601b134 100644
--- a/src/transaction/mod.rs
+++ b/src/transaction/mod.rs
@@ -1,5 +1,12 @@
+pub mod auth;
+
use crate::{
- encoding::{headers::CSeq, request::Request, response::Response, Message},
+ encoding::{
+ headers::{CSeq, CallID},
+ request::Request,
+ response::Response,
+ Message,
+ },
transport::Transport,
};
use anyhow::{anyhow, Result};
@@ -48,6 +55,20 @@ impl<T: Transport> TransactionUser<T> {
}
}
}
+ pub async fn respond(&self, req: &Request, mut resp: Response) -> Result<()> {
+ resp.headers.insert(
+ req.headers
+ .get::<CSeq>()
+ .ok_or(anyhow!("cseq is mandatory"))??,
+ );
+ resp.headers.insert(
+ req.headers
+ .get::<CallID>()
+ .ok_or(anyhow!("call-id is mandatory"))??,
+ );
+ self.transport.send(Message::Response(resp)).await?;
+ Ok(())
+ }
pub async fn transact(&self, mut request: Request) -> Result<mpsc::Receiver<Response>> {
let seq = self.sequence.fetch_add(1, Ordering::Relaxed);
diff --git a/src/transport/udp.rs b/src/transport/udp.rs
index 391e2b3..78ca4f2 100644
--- a/src/transport/udp.rs
+++ b/src/transport/udp.rs
@@ -19,7 +19,7 @@ impl Transport for UdpTransport {
let mut buf = [0; 1024];
let size = self.sock.recv(&mut buf).await?;
let message = String::from_utf8(buf[..size].to_vec())?;
- debug!("{message}");
+ debug!("<- {message}");
Message::from_str(message.trim_end())
}
async fn send(&self, request: Message) -> Result<()> {