#scada #iec-104 #power-systems #iec-60870 #ffi

sys lib60870-sys

Raw FFI bindings to lib60870-C, an IEC 60870-5-101/104 protocol implementation

2 releases

0.5.1 Dec 7, 2025
0.5.0 Dec 7, 2025

#14 in #scada

GPL-3.0 license

420KB
9K SLoC

lib60870-sys

CI Crates.io Documentation License: GPL-3.0

Raw FFI bindings to lib60870-C, an IEC 60870-5-101/104 protocol implementation.

This is a -sys crate that provides low-level, unsafe bindings to the C library. For a safe, idiomatic Rust API, consider using the lib60870 crate.

Features

  • Raw FFI bindings - Direct access to all lib60870-C functions

Platform Support

Platform Status
Linux ✅ Fully supported
macOS ✅ Fully supported
Windows ⚠️ Experimental (build works, runtime issues)

Note: Windows support is experimental. The library compiles successfully but may have runtime issues related to DLL dependencies. Contributions to improve Windows support are welcome!

Usage

Add this to your Cargo.toml:

[dependencies]
lib60870-sys = "0.4"

# With TLS support:
lib60870-sys = { version = "0.4", features = ["tls"] }

Example

use lib60870_sys::*;
use std::ffi::CString;

fn main() {
    // Get library version
    let version = unsafe { Lib60870_getLibraryVersionInfo() };
    println!(
        "lib60870 v{}.{}.{}",
        version.major, version.minor, version.patch
    );

    // Create a connection
    let ip = CString::new("127.0.0.1").unwrap();
    let port = 2404;
    let conn = unsafe { CS104_Connection_create(ip.as_ptr(), port) };

    if !conn.is_null() {
        println!("Connection created successfully");

        // Clean up
        unsafe { CS104_Connection_destroy(conn) };
        println!("Connection destroyed");
    } else {
        println!("Failed to create connection");
    }
}

Run the example:

cargo run --example version

Safety: All functions in this crate are unsafe because they directly call C code. Users must ensure proper memory management, null pointer handling, and correct C string formatting.

Cargo Features

Feature Description
tls Enable TLS support (downloads mbedtls 2.28)
debug Enable printf debug output
no-threads Disable threading (for embedded systems)
tcp-keepalive Enable TCP keep-alive

How the Build Works

The build script (build.rs) automatically:

  1. Downloads lib60870 v2.3.6 from GitHub releases
  2. Downloads mbedtls v2.28.9 (only if tls feature is enabled)
  3. Compiles with CMake - builds the static library
  4. Generates Rust bindings with bindgen

All downloads are cached in target/ so subsequent builds are fast.

Pre-generated Bindings

For docs.rs and environments without network access, this crate includes pre-generated bindings in src/bindings_pregenerated.rs. These are used automatically when building with the docsrs cfg flag.

Building from Source

Requirements:

  • Rust 1.70 or later
  • CMake 3.10 or later
  • C compiler (GCC, Clang, or MSVC)
  • Internet connection (for first build only)
cargo build
cargo build --features tls

License

lib60870-C is dual-licensed under GPLv3 and a commercial license. This crate inherits the GPL-3.0 license. See the lib60870 repository for commercial licensing options.

Dependencies

~0.2–5MB
~96K SLoC