Skip to content
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

feat: create tftpc binary #23

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ jobs:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Build
run: cargo build --features client --verbose
- name: Run tests
run: cargo test --verbose
- name: test client
run: cargo test --features client --verbose
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ license = "MIT"
keywords = ["tftp", "server"]
categories = ["command-line-utilities"]

[[bin]]
name = "tftpc"
path = "src/client_main.rs"
required-features = ["client"]

[[bin]]
name = "tftpd"
path = "src/main.rs"

[features]
integration = []
client = []
14 changes: 4 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,22 @@ tftpd -i 0.0.0.0 -p 1234 -d "/home/user/tftp" -r

## Usage (Client)

Client code is protected by a feature flag names `client`.
To install the client and server using Cargo:

```bash
cargo install --features client tftpd
tftpd client --help
tftpd server --help
```

To run the server on the IP address `0.0.0.0`, read-only, on port `1234` in the `/home/user/tftp` directory:

```bash
tftpd server -i 0.0.0.0 -p 1234 -d "/home/user/tftp" -r
tftpc --help
```

To connect the client to a tftp server running on IP address `127.0.0.1`, read-only, on port `1234` and download a file named `example.file`
```bash
tftpd client example.file -i 0.0.0.0 -p 1234 -d
tftpc example.file -i 0.0.0.0 -p 1234 -d
```

To connect the client to a tftp server running on IP address `127.0.0.1`, read-only, on port `1234` and upload a file named `example.file`
```bash
tftpd client ./example.file -i 0.0.0.0 -p 1234 -u
tftpc ./example.file -i 0.0.0.0 -p 1234 -u
```

## License
Expand Down
4 changes: 2 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ impl Client {
})
}

/// Starts the Client depending on the [`Mode`] the client is in
pub fn start(&mut self) -> Result<(), Box<dyn Error>> {
/// Run the Client depending on the [`Mode`] the client is in
pub fn run(&mut self) -> Result<(), Box<dyn Error>> {
match self.mode {
Mode::Upload => self.upload(),
Mode::Download => self.download(),
Expand Down
13 changes: 3 additions & 10 deletions src/client_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,6 @@ impl ClientConfig {
pub fn new<T: Iterator<Item = String>>(mut args: T) -> Result<ClientConfig, Box<dyn Error>> {
let mut config = ClientConfig::default();

args.next();

if let Some(file_name) = args.next() {
config.filename = PathBuf::from(file_name);
} else {
return Err("Missing file to upload or download".into());
}

while let Some(arg) = args.next() {
match arg.as_str() {
"-i" | "--ip-address" => {
Expand Down Expand Up @@ -139,8 +131,9 @@ impl ClientConfig {
println!(" -h, --help\t\t\tPrint help information");
process::exit(0);
}

invalid => return Err(format!("Invalid flag: {invalid}").into()),
file_name => {
config.filename = PathBuf::from(file_name);
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions src/client_main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::error::Error;
use std::{env, net::SocketAddr, process};
use tftpd::{Client, ClientConfig, Mode};

fn main() {
client(env::args()).unwrap_or_else(|err| {
eprintln!("{err}");
})
}

fn client<T: Iterator<Item = String>>(args: T) -> Result<(), Box<dyn Error>> {
let config = ClientConfig::new(args).unwrap_or_else(|err| {
eprintln!("Problem parsing arguments: {err}");
process::exit(1)
});

let mut client = Client::new(&config).unwrap_or_else(|err| {
eprintln!("Problem creating client: {err}");
process::exit(1)
});

if config.mode == Mode::Upload {
println!(
"Starting TFTP Client, uploading {} to {}",
config.filename.display(),
SocketAddr::new(config.remote_ip_address, config.port),
);
} else {
println!(
"Starting TFTP Client, downloading {} to {}",
config.filename.display(),
SocketAddr::new(config.remote_ip_address, config.port),
);
}

client.run()
}
57 changes: 1 addition & 56 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,63 +1,8 @@
#[cfg(feature = "client")]
use std::error::Error;
use std::{env, net::SocketAddr, process};
#[cfg(not(feature = "client"))]
use tftpd::{Config, Server};
#[cfg(feature = "client")]
use tftpd::{Client, ClientConfig, Config, Mode, Server};

#[cfg(feature = "client")]
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
eprintln!("{}: incorrect usage", args[0]);
eprintln!("{} <client | server> [args]", args[0]);
} else if args[1] == "client" {
client(args[1..].iter().map(|s| s.to_string())).unwrap_or_else(|err| {
eprintln!("{err}");
})
} else if args[1] == "server" {
server(args[1..].iter().map(|s| s.to_string()));
} else {
eprintln!("{}: incorrect usage", args[0]);
eprintln!("{} (client | server) [args]", args[0]);
}
}


#[cfg(not(feature = "client"))]
fn main() {
let args: Vec<String> = env::args().collect();
server(args[0..].iter().map(|s| s.to_string()));
}

#[cfg(feature = "client")]
fn client<T: Iterator<Item = String>>(args: T) -> Result<(), Box<dyn Error>> {
let config = ClientConfig::new(args).unwrap_or_else(|err| {
eprintln!("Problem parsing arguments: {err}");
process::exit(1)
});

let mut server = Client::new(&config).unwrap_or_else(|err| {
eprintln!("Problem creating client: {err}");
process::exit(1)
});

if config.mode == Mode::Upload {
println!(
"Starting TFTP Client, uploading {} to {}",
config.filename.display(),
SocketAddr::new(config.remote_ip_address, config.port),
);
} else {
println!(
"Starting TFTP Client, downloading {} to {}",
config.filename.display(),
SocketAddr::new(config.remote_ip_address, config.port),
);
}

server.start()
server(env::args());
}

fn server<T: Iterator<Item = String>>(args: T) {
Expand Down
Loading