Skip to content

Commit

Permalink
initial commit for DiameterServer
Browse files Browse the repository at this point in the history
  • Loading branch information
lwlee2608 committed Jan 15, 2024
1 parent 776a614 commit 4ab8ff7
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use crate::diameter::DiameterMessage;
use std::io::Cursor;

use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::TcpStream,
};
use tokio::io::AsyncReadExt;
use tokio::io::AsyncWriteExt;
use tokio::net::TcpStream;

#[derive(Debug)]
pub struct DiameterClient {
stream: Option<TcpStream>,
}
Expand Down Expand Up @@ -64,17 +62,15 @@ mod tests {
use crate::avp::unsigned32::Unsigned32Avp;
use crate::avp::utf8string::UTF8StringAvp;
use crate::avp::Avp;
use crate::diameter::{
ApplicationId, CommandCode, DiameterMessage, PROXYABLE_FLAG, REQUEST_FLAG,
};
use crate::diameter::{ApplicationId, CommandCode, DiameterMessage, REQUEST_FLAG};

#[ignore]
#[tokio::test]
async fn test_diameter_client() {
let mut ccr = DiameterMessage::new(
CommandCode::CreditControl,
ApplicationId::CreditControl,
REQUEST_FLAG | PROXYABLE_FLAG,
REQUEST_FLAG,
1123158610,
3102381851,
);
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod client;
pub mod diameter;
pub mod dictionary;
pub mod error;
pub mod server;
85 changes: 85 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use crate::avp;
use crate::avp::enumerated::EnumeratedAvp;
use crate::avp::identity::IdentityAvp;
use crate::avp::unsigned32::Unsigned32Avp;
use crate::avp::utf8string::UTF8StringAvp;
use crate::avp::Avp;
use crate::diameter::{ApplicationId, CommandCode, DiameterMessage};
use std::io::Cursor;
use tokio::io::AsyncReadExt;
use tokio::io::AsyncWriteExt;
use tokio::net::TcpListener;

pub struct DiameterServer {
listener: TcpListener,
}

impl DiameterServer {
pub async fn new(addr: &str) -> Result<DiameterServer, Box<dyn std::error::Error>> {
let listener = TcpListener::bind(addr).await?;
Ok(DiameterServer { listener })
}

pub async fn handle(&mut self) -> Result<(), Box<dyn std::error::Error>> {
loop {
// Accept new connection
let (mut socket, _) = self.listener.accept().await?;
tokio::spawn(async move {
let mut buf = [0; 1024];
loop {
let n = match socket.read(&mut buf).await {
Ok(n) if n == 0 => return,
Ok(n) => n,
Err(e) => {
eprintln!("failed to read from socket; err = {:?}", e);
return;
}
};

// Decode Request
let request = &buf[..n];
let mut cursor = Cursor::new(request);
let req = DiameterMessage::decode_from(&mut cursor).unwrap();
println!("Received request: {}", req);

// Return Dummy Value
let mut res = DiameterMessage::new(
CommandCode::CreditControl,
ApplicationId::CreditControl,
0,
1123158610,
3102381851,
);
res.add_avp(avp!(264, None, IdentityAvp::new("host.example.com"), true));
res.add_avp(avp!(296, None, IdentityAvp::new("realm.example.com"), true));
res.add_avp(avp!(263, None, UTF8StringAvp::new("ses;12345888"), true));
res.add_avp(avp!(416, None, EnumeratedAvp::new(1), true));
res.add_avp(avp!(415, None, Unsigned32Avp::new(1000), true));
res.add_avp(avp!(268, None, Unsigned32Avp::new(2001), true));

// Encode Response
let mut response = Vec::new();
res.encode_to(&mut response).unwrap();

// Send Response
if let Err(e) = socket.write_all(&response).await {
eprintln!("failed to write to socket; err = {:?}", e);
return;
}
}
});
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[ignore]
#[tokio::test]
async fn test_server() {
let mut server = DiameterServer::new("0.0.0.0:3868").await.unwrap();
server.handle().await.unwrap();
}
}

0 comments on commit 4ab8ff7

Please sign in to comment.