Add IPV6 support
This commit is contained in:
parent
e81540f24d
commit
9eef30a58a
2 changed files with 25 additions and 11 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::net::Ipv4Addr;
|
use std::net::{IpAddr, Ipv4Addr};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::{env, process};
|
use std::{env, process};
|
||||||
|
|
||||||
|
|
@ -20,7 +20,7 @@ use std::{env, process};
|
||||||
/// ```
|
/// ```
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
/// Local IP address of the TFTP Server. (default: 127.0.0.1)
|
/// Local IP address of the TFTP Server. (default: 127.0.0.1)
|
||||||
pub ip_address: Ipv4Addr,
|
pub ip_address: IpAddr,
|
||||||
/// Local Port number of the TFTP Server. (default: 69)
|
/// Local Port number of the TFTP Server. (default: 69)
|
||||||
pub port: u16,
|
pub port: u16,
|
||||||
/// Default directory of the TFTP Server. (default: current working directory)
|
/// Default directory of the TFTP Server. (default: current working directory)
|
||||||
|
|
@ -44,7 +44,7 @@ impl Config {
|
||||||
/// intended for use with [`env::args()`].
|
/// intended for use with [`env::args()`].
|
||||||
pub fn new<T: Iterator<Item = String>>(mut args: T) -> Result<Config, Box<dyn Error>> {
|
pub fn new<T: Iterator<Item = String>>(mut args: T) -> Result<Config, Box<dyn Error>> {
|
||||||
let mut config = Config {
|
let mut config = Config {
|
||||||
ip_address: Ipv4Addr::new(127, 0, 0, 1),
|
ip_address: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
|
||||||
port: 69,
|
port: 69,
|
||||||
directory: env::current_dir().unwrap_or_else(|_| env::temp_dir()),
|
directory: env::current_dir().unwrap_or_else(|_| env::temp_dir()),
|
||||||
receive_directory: PathBuf::new(),
|
receive_directory: PathBuf::new(),
|
||||||
|
|
@ -61,7 +61,8 @@ impl Config {
|
||||||
match arg.as_str() {
|
match arg.as_str() {
|
||||||
"-i" | "--ip-address" => {
|
"-i" | "--ip-address" => {
|
||||||
if let Some(ip_str) = args.next() {
|
if let Some(ip_str) = args.next() {
|
||||||
config.ip_address = ip_str.parse::<Ipv4Addr>()?;
|
let ip_addr: IpAddr = ip_str.parse()?;
|
||||||
|
config.ip_address = ip_addr;
|
||||||
} else {
|
} else {
|
||||||
return Err("Missing ip address after flag".into());
|
return Err("Missing ip address after flag".into());
|
||||||
}
|
}
|
||||||
|
|
@ -164,6 +165,8 @@ impl Config {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use std::net::Ipv6Addr;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -186,6 +189,19 @@ mod tests {
|
||||||
assert!(config.read_only);
|
assert!(config.read_only);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parses_config_with_ipv6() {
|
||||||
|
let config = Config::new(
|
||||||
|
["/", "-i", "0:0:0:0:0:0:0:0", "-p", "1234"]
|
||||||
|
.iter()
|
||||||
|
.map(|s| s.to_string()),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(config.ip_address, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
|
||||||
|
assert_eq!(config.port, 1234);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parses_some_config() {
|
fn parses_some_config() {
|
||||||
let config = Config::new(
|
let config = Config::new(
|
||||||
|
|
|
||||||
12
src/main.rs
12
src/main.rs
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{env, process};
|
use std::{env, net::SocketAddr, process};
|
||||||
use tftpd::{Config, Server};
|
use tftpd::{Config, Server};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
@ -17,16 +17,14 @@ fn main() {
|
||||||
|
|
||||||
if config.receive_directory == config.send_directory {
|
if config.receive_directory == config.send_directory {
|
||||||
println!(
|
println!(
|
||||||
"Running TFTP Server on {}:{} in {}",
|
"Running TFTP Server on {} in {}",
|
||||||
config.ip_address,
|
SocketAddr::new(config.ip_address, config.port),
|
||||||
config.port,
|
|
||||||
config.directory.display()
|
config.directory.display()
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
println!(
|
println!(
|
||||||
"Running TFTP Server on {}:{}. Sending from {}, receiving to {}",
|
"Running TFTP Server on {}. Sending from {}, receiving to {}",
|
||||||
config.ip_address,
|
SocketAddr::new(config.ip_address, config.port),
|
||||||
config.port,
|
|
||||||
config.send_directory.display(),
|
config.send_directory.display(),
|
||||||
config.receive_directory.display(),
|
config.receive_directory.display(),
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue