2 releases (0 unstable)

2.0.0-alpha1+1… Nov 12, 2021
2.0.0-alpha1+1… Jun 23, 2021

#2 in #high-availability

30 downloads per month

LGPL-2.1+

165KB
5K SLoC

Copyright (C) 2021 Red Hat, Inc. All rights reserved.

Author: Christine Caulfield ccaulfie@redhat.com

This software licensed under GPL-2.0+

This crate contains Rust bindings for the kronosnet (knet) library libknet: https://kronosnet.org/

Kronosnet, often referred to as knet, is a network abstraction layer designed for High Availability use cases, where redundancy, security, fault tolerance and fast fail-over are the core requirements of your application.


lib.rs:

This crate provides access to the kronosnet library 'libknet' from Rust. They are a fairly thin layer around the actual API calls but with Rust data types and iterators.

No more information about knet itself will be provided here, it is expected that if you feel you need access to the knet API calls, you know what they do :)

Example

use knet_bindings::knet_bindings as knet;
use std::net::{SocketAddr, IpAddr, Ipv4Addr};
use std::thread::spawn;
use std::sync::mpsc::Receiver;
use std::sync::mpsc::channel;
use std::io::{Result, ErrorKind, Error};
use std::{thread, time};

const CHANNEL: i8 = 1;

pub fn main() -> Result<()>
{
    let host_id = knet::HostId::new(1);
    let other_host_id = knet::HostId::new(2);

    let (log_sender, log_receiver) = channel::<knet::LogMsg>();
    spawn(move || logging_thread(log_receiver));

    let knet_handle = match knet::handle_new(&our_hostid, Some(log_sender),
                                             knet::LogLevel::Debug, knet::HandleFlags::NONE) {
        Ok(h) => h,
        Err(e) => {
            return Err(e);
        }
    };

    if let Err(e) = knet::host_add(knet_handle, &other_hostid) {
        return Err(e);
    }
    if let Err(e) = knet::link_set_config(knet_handle, &other_hostid, 0,
                                knet::TransportId::Udp,
                                &SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8000+(our_hostid.to_u16())),
                                &SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8000+(other_hostid.to_u16())),
                                knet::LinkFlags::NONE) {
        return Err(e);
    }
    if let Err(e) = knet::handle_add_datafd(knet_handle, 0, CHANNEL) {
        return Err(e);
    }

    if let Err(e) = knet::handle_crypto_rx_clear_traffic(knet_handle, knet::RxClearTraffic::Allow) {
        return Err(e);
    }

    if let Err(e) = knet::link_set_enable(knet_handle, &other_hostid, 0, true) {
        return Err(e);
    }

    if let Err(e) = knet::handle_setfwd(knet_handle, true) {
        return Err(e);
    }

    Ok()
}

Dependencies

~115–355KB