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
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// Copyright (C) 2018 Kubos Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Example client program for interacting with a Kubos Service using UDP
//

use nix::{convert_ioctl_res, ioctl};
use serde_derive::Deserialize;
use std::env;
use std::fs::File;
use std::io::prelude::*;
use std::net::{SocketAddr, UdpSocket};
use std::os::unix::io::AsRawFd;
use std::time::Duration;

const FIONREAD: u16 = 0x541B;
ioctl!(bad read available with FIONREAD; usize);

#[derive(Debug, Deserialize)]
pub struct Address {
    ip: String,
    port: u16,
}

#[derive(Debug, Deserialize)]
pub struct Config {
    host: Address,
    service: Address,
    query_file: String,
}

fn configure_client() -> (SocketAddr, SocketAddr, String) {
    let args: Vec<String> = env::args().collect();

    let config: Config = if args.len() == 2 {
        let mut raw = String::new();
        let filename = &args[1];
        match File::open(filename)
            .map(|mut f| f.read_to_string(&mut raw))
            .map(|_| toml::from_str(&raw))
        {
            Ok(toml) => toml.unwrap(),
            _ => {
                panic!("Couldn't open config file");
            }
        }
    } else {
        panic!("No config file");
    };

    let listen_addr = format!("{}:{}", config.host.ip, config.host.port)
        .parse::<SocketAddr>()
        .unwrap();

    let send_addr = format!("{}:{}", config.service.ip, config.service.port)
        .parse::<SocketAddr>()
        .unwrap();

    let query: String = {
        let mut raw = String::new();
        match File::open(config.query_file).map(|mut f| f.read_to_string(&mut raw)) {
            Ok(_) => raw,
            _ => {
                println!("Using default query");
                "{ping}".to_owned()
            }
        }
    };

    (listen_addr, send_addr, query)
}

fn main() {
    // Get the host IP, remote IP, and query string from the configuration file
    let (client_addr, service_addr, query) = configure_client();

    // Get a socket for the host IP
    let socket = match UdpSocket::bind(client_addr) {
        Ok(sock) => {
            println!("Bound socket to {}", client_addr);
            sock
        }
        Err(err) => {
            println!("Could not bind: {}", err);
            return;
        }
    };

    // Send our query to the requested service IP
    let result = socket.send_to(&query.as_bytes(), &service_addr);
    match result {
        Ok(amt) => println!("Sent {} bytes", amt),
        Err(err) => {
            println!("Write error: {}", err);
            return;
        }
    }

    // Wait for a response, but don't actually read it yet.
    // If we don't get a reply within one second, the service probably
    // isn't actually running.
    socket.set_read_timeout(Some(Duration::new(1, 0))).unwrap();
    let mut buf: [u8; 1] = [0];
    let result = socket.peek_from(&mut buf);
    if let Err(err) = result {
        println!("Read error: {}", err);
        return;
    }

    // Get the number of bytes in the response so we know how big to make our buffer
    let mut len: usize = 0;
    unsafe {
        match available(socket.as_raw_fd(), &mut len) {
            Ok(_) => {}
            Err(err) => {
                println!("Failed to read bytes available: {}", err);
                return;
            }
        }
    }

    // Allocate the buffer and read the response
    let mut buf = vec![0u8; len].into_boxed_slice();
    let result = socket.recv_from(&mut buf);
    match result {
        Ok((amt, src)) => {
            println!("Received {} bytes from {}", amt, src);
            let mut v: serde_json::Value = serde_json::from_slice(&buf[0..(amt)]).unwrap();
            // If there's  'data' field in the returned JSON, then our query went (atleast mostly) successfully.
            // Extract the query response JSON from the field
            match serde_json::from_value::<String>(v["data"].clone()) {
                Ok(msg) => {
                    v = serde_json::from_str(&msg).unwrap_or_default();
                }
                // Otherwise, we'll just print the raw returned JSON
                _ => println!("Errors returned from query"),
            }
            println!("{}", serde_json::to_string_pretty(&v).unwrap());
        }
        Err(err) => {
            println!("Read error: {}", err);
            return;
        }
    }
}