[][src]Crate namaste

Namaste

Project

Features

  • Handling locks amongst processes via TCP.

Design

Unique identifier

A unique identifier (UID) is a 64-byte array, which matches an SHA3-512 hash.

Server and client

  • Server starts on a fixed TCP port, and listens for clients.

  • "Book" command:

    • When a new client connects to, they are expected to send 4 pieces of information:

      • A one-byte "book" command.
      • Global UID. This UID is used within a lifetime of the server, as an identification of the client.
      • Handshake UID - must be different to above UID. This UID is used for the server to communicate with client.
      • Client's local TCP server port, consisting of 2 bytes, in big-endian order.
    • Server then connects to the client's local server, sends the handshake UID, reads a UID and compares it with the first one above.

    • If the verification passes, server stores client UID in memory, and sends back one non-zero byte to client.

    • If the verification does not passes, server sends back one zero byte to client.

  • "Check out" command sends one-byte command, UID and handshake UID.

Limits

  • Server stores all UIDs in memory. So if it crashes, they are lost.
  • Server relies on a fixed loop-back IP address (DEFAULT_IP) as a protection against external Denial-of-service attacks. However, it is not protected against attacks from internal programs running on a same machine.
  • Client side will ask system to grant it a random TCP server port. On most systems, TCP ports are limited.

About crashing issue: we always strive to code carefully. If you find out bugs, we are thankful if you file reports in project repository.

Usage

You should only rely on this library for limited small amount of locks between your program's processes. For instance, this library can help with single-instance designed programs.

Frequently Answered Questions

Why not Unix Domain Socket?

UDS has a good design, but its implementation has some serious flaws:

  • Already-bound file path can be deleted. Any new binding to that path will silently take control of current one.
  • Linux has an extension to UDS, which is called abstract sockets. Its design is very good, and can totally replace this library. However its documentation clearly states that it is not portable.

Why not file locks?

  • On Unix, locked files can still be deleted.

Alternatives

Currently the authors are not aware of any projects similar to this one.

Building server

  • Via Cargo:

    ~> cargo install namaste --version=x.y.z --features=bin
    
  • From source:

    ~> # Clone a specific version via tag name
    ~> git clone --branch=x.y.z --depth=1 -- https://bitbucket.org/haibison/namaste-rs namaste-x.y.z/
    ~> cd namaste-x.y.z/
    ~> cargo build --release --features=bin
    

Examples

It is expected that the system already has Namaste server running!

This example is not tested
use namaste::client::Client;

// UID must be a constant.
const NAMASTE_UID: namaste::Uid = [...];

fn main() {
    // You should generate new handshake UID each time you call Client::book().
    let handshake_uid = ...;
    match Client::book(NAMASTE_UID, handshake_uid, None, namaste::DEFAULT_RW_TIMEOUT) {
        Ok(Some(client)) => {
            run_main_job();
            match client.check_out() {
                Ok(true) => println!("Successfully checked out of Namaste server"),
                Ok(false) => eprintln!("Couldn't check out of Namaste server"),
                Err(err) => eprintln!("Failed checking out of Namaste server: {}", err),
            };
        },
        Ok(None) => eprintln!("Another instance is running"),
        Err(err) => eprintln!("Failed connecting to Namaste server: {}", err),
    };
}

Other notes

To generate a UID, you can get help from Dia-Hammer:

$ hammer sha3-512 --limit=65536 --format=hex-array -- /dev/urandom

To generate a handshake UID, you can use tiny-keccak.

Modules

client

Client

server

Server

version_info

0.6.0 (June 3rd, 2019)

Constants

CODE_NAME

Crate code name

DEFAULT_IP

Default IP address, used for both server and client

DEFAULT_RW_TIMEOUT

Default read/write timeout: 3 seconds

NAME

Crate name

RELEASE_DATE

Crate release date (year/month/day)

TAG

Tag, which can be used for logging...

UUID

Unique universally identifier of this crate

VERSION

Crate version

Functions

cmp_uids

Compares UIDs

read_uid

Reads UID

Type Definitions

Uid

Unique Identifier