aboutsummaryrefslogtreecommitdiff
path: root/src/query.rs
diff options
context:
space:
mode:
authorevuez <julien@mulga.net>2022-11-26 15:38:06 -0500
committerevuez <julien@mulga.net>2024-04-03 22:44:12 +0200
commit86098797034cbc7eb6db0cee54e17f8dcaedbc5d (patch)
tree29b6225ead843eb9022296a54657bbadfa1c4da0 /src/query.rs
downloadblom-86098797034cbc7eb6db0cee54e17f8dcaedbc5d.tar.gz
Initial commitHEADmain
Diffstat (limited to 'src/query.rs')
-rw-r--r--src/query.rs97
1 files changed, 97 insertions, 0 deletions
diff --git a/src/query.rs b/src/query.rs
new file mode 100644
index 0000000..1f3b4c8
--- /dev/null
+++ b/src/query.rs
@@ -0,0 +1,97 @@
+use crate::common::expr::Expr;
+use std::io::Read;
+use std::io::Write;
+use std::net::TcpStream;
+use std::process::ExitCode;
+use std::str;
+
+pub const USAGE: &str = "
+Usage: blom query <query>
+
+ Sends <query> to the blom server and prints the response
+ to stdout.
+
+Examples:
+
+ $ blom query 'CHILDREN 1.2.3.0/24'
+ 1.2.3.4/26
+ 1.2.3.4/32
+
+ $ blom query --raw 'CHILDREN 1.2.3.0/24'
+ +1.2.3.4/26\n
+ +1.2.3.4/32\n
+
+ $ blom query 'INFO 1.2.3.0/24'
+ Start: 1.2.3.0
+ End: 1.2.3.255
+ Size: 256
+ Parents: 3
+ Children: 364
+ Meta: \"{\\\"owner\\\": \\\"acme\\\"}\"
+
+Options:
+
+ -r, --raw Do not parse the response and prints the raw
+ output from the server instead.
+
+ -v, --verbose Print the request send to the server in addition
+ to the response.
+
+ -b, --bind Bind to the given ADDRESS:PORT. Default is
+ 0.0.0.0:4902
+
+";
+
+pub fn cmd(args: &[String]) -> ExitCode {
+ let mut addr = "0.0.0.0:4902";
+ let mut query: Option<&str> = None;
+
+ let mut arg_index: usize = 0;
+ while arg_index < args.len() {
+ match args[arg_index].as_str() {
+ "--bind" | "-b" => {
+ addr = args
+ .get(arg_index + 1)
+ .expect("Missing address after --bind.")
+ .as_str();
+ arg_index += 1;
+ }
+ q => {
+ if query.is_some() {
+ println!("Too many arguments.");
+ return ExitCode::FAILURE;
+ }
+ query = Some(q);
+ }
+ }
+
+ arg_index += 1;
+ }
+
+ if let Some(q) = query {
+ handle_query(addr, q);
+ return ExitCode::SUCCESS;
+ }
+
+ println!("Missing query.");
+ ExitCode::FAILURE
+}
+
+fn handle_query(addr: &str, query: &str) {
+ let expr = Expr::from_query(query).encode();
+
+ match TcpStream::connect(addr) {
+ Ok(mut stream) => {
+ let _ = stream.write(&expr).unwrap();
+ let mut data = [0u8; 64];
+ match stream.read(&mut data) {
+ Ok(_) => {
+ let resp = str::from_utf8(&data).unwrap();
+ println!("{resp:?}");
+ }
+ Err(e) => println!("Read failure: {e:?}"),
+ }
+ }
+ Err(e) => println!("Connection failure: {e:?}"),
+ }
+}