aboutsummaryrefslogtreecommitdiff
path: root/src/query.rs
blob: 1f3b4c865447cf5216bd6323e66e5f7949368589 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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:?}"),
    }
}