mikrotik_api/
lib.rs

1//! # Library and minimal client for RouterOS' proprietary API
2//!
3//! Mikrotik RouterOS exposes on port TCP/8278 a semi-textual API composed of words and sentences.
4//! This API allow power-users to request data about the router state, set configuration, listen to some events, ... using a syntax similar to the one used in the CLI.
5//!
6//! For a more in-depth description of the API, please see the [official Mikrotik wiki](https://wiki.mikrotik.com/wiki/Manual:API).  
7//!
8//! ## The library
9
10//! Based on tokio and fully asynchronous, the library allows to deal with all the API's uses cases :
11//! - simple requests with one-off answers, like `/system/identity/print`,
12//! - simple requests with array-like answers, like `/interfaces/print`,
13//! - simple requests to listen to events, like `/user/active/listen`,
14//! - cancel streaming commands with `/cancel`
15//!
16//! ### Usage
17//!
18//! The library exposes only one function: `connect`, that makes a TCP connection to the provided address.
19//! If successful, a `MikrotikAPI<Disconnected>` object is returned.
20//! It is then necessary to `authenticate` to get a `MikrotikAPI<Authenticated>` object.
21
22//! Eight functions are then available:
23//! - `system_resources` will make a call to `/system/resource/print`
24//! - `interfaces` will retrieve a list of interfaces with all their properties (``/interfaces/print``)
25//! - `active_users` returns a `Stream` of events regarding user activity (login & logout)
26//! - `interface_changes` returns a `Stream` of events regarding changes to interfaces (up, down, ...)
27//! - `cancel` cancels a streaming command given its tag
28//! - `generic_oneshot_call` allows to call any endpoint providing a one-off answer. Thanks to type inference, answer is returned in the user's object of choice. Example:
29
30//! ```rust
31//! #[derive(Debug, Deserialize)]
32//! struct Identity {
33//!   pub name: String,
34//! }
35
36//! let identity = api
37//!   .generic_oneshot_call::<Identity>("/system/identity/print", None)
38//!   .await
39//!   .unwrap();
40
41//! println!("Name: '{}'", identity.name);
42//! ```
43
44//! - `generic_array_call` will do the same job but for endpoints providing multiples (but finite) answers
45//! - `generic_streaming_call` will provide a `Stream` of `Response` for any endpoint supporting the `listen` command. Example:
46//! ```rust
47//! #[derive(Debug, Deserialize)]
48//! struct Interface {
49//!   pub name: String,
50//!
51//!   #[serde(default)]
52//!   pub running: bool
53//!}
54//!
55//! let mut tag: u16 = 0;
56//!
57//! let changes = api
58//!   .generic_streaming_call::<Interface>("/interface/listen", None, &mut tag); //`tag` allows us to cancel the stream later on.
59//!
60//! tokio::spawn(changes.for_each(|item| async move {
61//!
62//!   if let Reponse::Reply(iface) = item {
63//!
64//!       let up_down = if iface.running { "up" } else { "down" };
65//!
66//!       println!("Interface {} is {}", iface.name, up_down);
67//!   }
68//!
69//! })).await;
70//! ```
71
72#![deny(missing_docs)]
73use std::io;
74
75use tokio::net::{TcpStream, ToSocketAddrs};
76
77mod api;
78
79pub use api::model::{
80    ActiveUser, Interface, InterfaceChange, InterfaceMTU, Response, SystemResources,
81};
82pub use api::{Authenticated, Disconnected, MikrotikAPI};
83
84/// Given an address, opens a connection to the remote API service
85/// the returned object is in a Disconnected state
86pub async fn connect<A: ToSocketAddrs>(addr: A) -> io::Result<MikrotikAPI<Disconnected>> {
87    let socket = TcpStream::connect(addr).await?;
88
89    Ok(MikrotikAPI::new(socket))
90}