From 340eaaebcb1cd861d88505a067cdf43e2664e7be Mon Sep 17 00:00:00 2001 From: Sayan Nandan Date: Thu, 3 Sep 2020 10:33:54 +0530 Subject: [PATCH] Add support for IPv6 addressing --- examples/config-files/ipv6.toml | 4 ++++ server/src/config/mod.rs | 29 ++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 examples/config-files/ipv6.toml diff --git a/examples/config-files/ipv6.toml b/examples/config-files/ipv6.toml new file mode 100644 index 00000000..c98fe3bc --- /dev/null +++ b/examples/config-files/ipv6.toml @@ -0,0 +1,4 @@ +# This makes use of an IPv6 address +[server] +host = "::1" +port = 2003 diff --git a/server/src/config/mod.rs b/server/src/config/mod.rs index 8ac3f50a..00599e6c 100644 --- a/server/src/config/mod.rs +++ b/server/src/config/mod.rs @@ -23,6 +23,7 @@ use libtdb::TResult; use serde::Deserialize; +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use toml; #[derive(Deserialize, Debug, PartialEq)] @@ -32,7 +33,7 @@ struct Config { #[derive(Deserialize, Debug, PartialEq)] struct ServerConfig { - host: String, + host: IpAddr, port: u16, noart: Option, } @@ -59,7 +60,7 @@ fn test_config_toml_okayport() { Config { server: ServerConfig { port: 2003, - host: "127.0.0.1".to_owned(), + host: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), noart: None, } } @@ -89,7 +90,7 @@ fn test_config_file_ok() { Config { server: ServerConfig { port: 2003, - host: "127.0.0.1".to_owned(), + host: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), noart: None, } } @@ -128,7 +129,7 @@ fn test_args() { Config { server: ServerConfig { port: 2003, - host: "127.0.0.1".to_owned(), + host: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), noart: None, } } @@ -146,9 +147,27 @@ fn test_config_file_noart() { Config { server: ServerConfig { port: 2003, - host: "127.0.0.1".to_owned(), + host: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), noart: Some(true), } } ); } + +#[test] +#[cfg(test)] +fn test_config_file_ipv6() { + let fileloc = "../examples/config-files/ipv6.toml"; + let file = std::fs::read_to_string(fileloc).unwrap(); + let cfg: Config = Config::new(file).unwrap(); + assert_eq!( + cfg, + Config { + server: ServerConfig { + port: 2003, + host: IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1)), + noart: None, + } + } + ); +}