Skip to content

Commit

Permalink
add: flag --tsig was added to client in main.rs and README
Browse files Browse the repository at this point in the history
  • Loading branch information
Jotape24 committed Jan 31, 2025
1 parent 556fe5f commit f74a68b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 9 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ tokio-rustls = "0.26.0"
rustls-native-certs = "0.8.0"
ipconfig = "0.3.2"
data-encoding = "2.7.0"
hex = "0.4.3"
[lib]
doctest = false
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,15 @@ Here it can be specified whether to run a *client* or a *resolver* :

- Six options:

| Option | Description |
|-------------------------|-----------------------------------------------------------------|
| `--qtype <QTYPE>` | Query type [default: A] |
| `--qclass <QCLASS>` | Query class [default: IN] |
| `--norecursive` | Disables the use of recursion when specified |
| `--payload <PAYLOAD>` | Maximum payload for EDNS [default: 512] |
| `--noedns` | Disables the use of EDNS when specified |
| `--protocol <PROTOCOL>` | Transport protocol, options: "UDP", "TCP", "TLS" [default: UDP] |
| Option | Description |
|-------------------------|--------------------------------------------------------------------------|
| `--qtype <QTYPE>` | Query type [default: A] |
| `--qclass <QCLASS>` | Query class [default: IN] |
| `--norecursive` | Disables the use of recursion when specified |
| `--payload <PAYLOAD>` | Maximum payload for EDNS [default: 512] |
| `--noedns` | Disables the use of EDNS when specified |
| `--protocol <PROTOCOL>` | Transport protocol, options: "UDP", "TCP", "TLS" [default: UDP] |
| `--tsig <TSIG>` | TSIG arguments key, algorithm, fudge, time_signed, key_name, mac_request |


- And four EDNS0 options
Expand Down
53 changes: 53 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use dns_rust::message::DnsMessage;
use dns_rust::message::rclass::Rclass;
use dns_rust::message::rcode::Rcode;
use dns_rust::message::rrtype::Rrtype;
use dns_rust::tsig::tsig_algorithm::TsigAlgorithm;

#[derive(Parser, Debug)]
struct Cli {
Expand Down Expand Up @@ -76,6 +77,47 @@ struct ClientArgs {
/// Transport protocol, options: "UDP", "TCP", "TLS".
#[arg(long, default_value_t = String::from("UDP"))]
protocol: String,

/// TSIG arguments key, algorithm, fudge, time_signed, key_name, mac_request
#[arg(long, value_parser = TsigArgs::from_str)]
tsig: Option<TsigArgs>,
}

/// Represents the arguments required for TSIG.
#[derive(Debug, Clone)]
pub struct TsigArgs {
pub key: Vec<u8>,
pub alg_name: TsigAlgorithm,
pub fudge: u16,
pub time_signed: u64,
pub key_name: String,
pub mac_request: Vec<u8>,
}

impl TsigArgs {
/// Parses a string into a `TsigArgs` instance.
pub fn from_str(value: &str) -> Result<Self, String> {
let parts: Vec<&str> = value.split(',').collect();
if parts.len() != 6 {
return Err("Expected 6 values for TSIG args".to_string());
}

let key = hex::decode(parts[0].trim()).map_err(|e| e.to_string())?;
let alg_name = TsigAlgorithm::from(parts[1].trim().to_string());
let fudge = parts[2].trim().parse::<u16>().map_err(|e| e.to_string())?;
let time_signed = parts[3].trim().parse::<u64>().map_err(|e| e.to_string())?;
let key_name = parts[4].trim().to_string();
let mac_request = hex::decode(parts[5].trim()).map_err(|e| e.to_string())?;

Ok(Self {
key,
alg_name,
fudge,
time_signed,
key_name,
mac_request,
})
}
}


Expand Down Expand Up @@ -162,6 +204,17 @@ pub async fn main() {
dns_query_message.add_edns0(max_payload, Rcode::NOERROR, 0, false, some_options);
}

if !client_args.tsig.is_none() {
if let Some(tsig_args) = &client_args.tsig {
dns_query_message.sign_message(&*tsig_args.key,
tsig_args.alg_name.clone(),
tsig_args.fudge,
tsig_args.time_signed,
tsig_args.key_name.clone(),
tsig_args.mac_request.clone());
}
}

// match tcp to set a client
let response = match client_args.protocol.as_str() {
"UDP" => {
Expand Down
2 changes: 1 addition & 1 deletion src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ impl DnsMessage {
///
/// dns_message.sign_message(key, algorithm, fudge, time_signed, key_name, mac_request);
/// ```
fn sign_message(&mut self, key: &[u8], alg_name: TsigAlgorithm,
pub fn sign_message(&mut self, key: &[u8], alg_name: TsigAlgorithm,
fudge: u16, time_signed: u64, key_name: String, mac_request: Vec<u8>) {
tsig::sign_tsig(self, key, alg_name, fudge, time_signed, key_name, mac_request);
}
Expand Down

0 comments on commit f74a68b

Please sign in to comment.