librecast_sys/
lib.rs

1// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
2// Copyright (c) 2025 Gavin Henry <ghenry@sentrypeer.org>
3
4// Ignore Rust's style conventions for our C FFI bindings
5#![allow(non_upper_case_globals)]
6#![allow(non_camel_case_types)]
7#![allow(non_snake_case)]
8
9// Use the include! macro to dump our generated bindings right into our crate's main entry point,
10// src/lib.rs:
11include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
12
13// Some basic tests to ensure that our bindings are working as expected with
14// the librecast C API
15
16#[cfg(test)]
17mod tests {
18    use super::*;
19    use std::ffi::{CStr, CString};
20
21    #[test]
22    fn test_lc_calls() {
23        unsafe {
24            // Create our context
25            let lctx = lc_ctx_new();
26            assert!(!lctx.is_null());
27
28            // Create our socket
29            let sock = lc_socket_new(lctx);
30            assert!(!sock.is_null());
31
32            // Create a Librecast channel
33            let chan_name = CString::new("my very first Librecast channel")
34                .expect("Failed to create CString for channel name");
35
36            // Convert to a raw pointer for C API, which will be nicer in
37            // our wrapper librecast crate
38            let chan_name_c_string = chan_name.into_raw();
39
40            let chan = lc_channel_new(lctx, chan_name_c_string);
41            assert!(!chan.is_null());
42
43            // Bind the channel to the socket
44            lc_channel_bind(sock, chan);
45
46            // Enable lookback, so we can receive our own packets on the same host
47            lc_socket_loop(sock, 1);
48
49            if !chan_name_c_string.is_null() {
50                // Convert the raw pointer back to a CString to free it
51                let _ = CStr::from_ptr(chan_name_c_string);
52            }
53
54            // Free the context. This also frees all Sockets, Channels, Routers etc.
55            // created with that Context
56            lc_ctx_free(lctx);
57        }
58    }
59}