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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//! Utility library for creating integrations against the Data Diode
//! Middleware.
//!
//! This library provides low level functions to perform arbitrary calls
//! to the server nodes as well as a few high-level helper functions that are
//! built on top of the low level functions.

pub mod auth;
pub mod err;
pub mod mgmt;
pub mod msg;

mod utils;

use futures::sink::SinkExt;

use tokio::io::{AsyncRead, AsyncWrite};

use tokio_stream::StreamExt;

use tokio_util::codec::Framed;

use blather::{codec, Telegram};

pub use err::Error;


/// Reference an account; with the option to implicitly reference self.
pub enum OptObjRef {
  Current,
  Id(i64),
  Name(String)
}

/// Explicitly reference an account, either by numeric identifier or name.
pub enum ObjRef {
  Id(i64),
  Name(String)
}


/// Send a telegram and wait for a reply.
pub async fn sendrecv<T: AsyncRead + AsyncWrite + Unpin>(
  conn: &mut Framed<T, blather::Codec>,
  tg: &Telegram
) -> Result<blather::Params, Error> {
  conn.send(tg).await?;
  crate::expect_okfail(conn).await
}


/// Waits for a message and ensures that it's Ok or Fail.
/// Converts Fail state to an Error::ServerError.
/// Returns a Params buffer containig the Ok parameters on success.
pub async fn expect_okfail<T: AsyncRead + AsyncWrite + Unpin>(
  conn: &mut Framed<T, blather::Codec>
) -> Result<blather::Params, Error> {
  if let Some(o) = conn.next().await {
    let o = o?;
    match o {
      codec::Input::Telegram(tg) => {
        if let Some(topic) = tg.get_topic() {
          if topic == "Ok" {
            return Ok(tg.into_params());
          } else if topic == "Fail" {
            return Err(Error::ServerError(tg.into_params()));
          }
        }
      }
      _ => {
        println!("unexpected reply");
      }
    }
    return Err(Error::BadState("Unexpected reply from server.".to_string()));
  }

  Err(Error::Disconnected)
}


#[derive(Debug)]
pub struct DDLinkInfo {
  pub engine: String,
  pub protocol: ddmw_types::node::ddlnk::Protocol,
  pub protimpl: ddmw_types::node::ddlnk::ProtImpl
}


#[derive(Debug)]
pub struct NodeInfo {
  pub version: String,
  pub os_name: String,
  pub nodetype: ddmw_types::node::Type,
  pub ddlnk: DDLinkInfo
}


pub async fn get_nodeinfo<T: AsyncRead + AsyncWrite + Unpin>(
  conn: &mut Framed<T, blather::Codec>
) -> Result<NodeInfo, Error> {
  let mut tg = Telegram::new();
  tg.set_topic("GetNodeInfo")?;
  let params = sendrecv(conn, &tg).await?;

  let nodetype = match params.get_str("ddmw.node") {
    Some(s) => s.parse::<ddmw_types::node::Type>(),
    None => return Err(Error::MissingData("ddmw.node not found".to_string()))
  };
  let nodetype = match nodetype {
    Ok(nt) => nt,
    Err(_) => return Err(Error::UnknownData("Unknown node type".to_string()))
  };

  let version = match params.get_str("ddmw.version") {
    Some(s) => s.to_string(),
    None => {
      return Err(Error::MissingData("ddmw.version not found".to_string()))
    }
  };

  let os_name = match params.get_str("os.name") {
    Some(s) => s.to_string(),
    None => return Err(Error::MissingData("os.name not found".to_string()))
  };

  let engine = match params.get_str("ddmw.ddlink.engine") {
    Some(s) => s.to_string(),
    None => {
      return Err(Error::MissingData(
        "ddmw.ddlink.engine not found".to_string()
      ))
    }
  };
  let protocol = match params.get_str("ddmw.ddlink.protocol") {
    Some(s) => s.parse::<ddmw_types::node::ddlnk::Protocol>(),
    None => {
      return Err(Error::MissingData(
        "ddmw.ddlnk.protocol not found".to_string()
      ))
    }
  };
  let protocol = match protocol {
    Ok(s) => s,
    Err(_) => {
      return Err(Error::UnknownData("Unknown protocol type".to_string()))
    }
  };
  let protimpl = match params.get_str("ddmw.ddlink.protimpl") {
    Some(s) => s.parse::<ddmw_types::node::ddlnk::ProtImpl>(),
    None => {
      return Err(Error::MissingData(
        "ddmw.ddlnk.protimpl not found".to_string()
      ))
    }
  };
  let protimpl = match protimpl {
    Ok(s) => s,
    Err(_) => {
      return Err(Error::UnknownData("Unknown protimpl type".to_string()))
    }
  };

  Ok(NodeInfo {
    version,
    os_name,
    nodetype,
    ddlnk: DDLinkInfo {
      engine,
      protocol,
      protimpl
    }
  })
}

// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :