Skip to main content

nispor/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2
3use std::ffi::CString;
4
5use libc::{c_char, c_int};
6
7#[allow(clippy::not_unsafe_ptr_arg_deref)]
8#[no_mangle]
9pub extern "C" fn nispor_net_state_retrieve(
10    state: *mut *mut c_char,
11    err_kind: *mut *mut c_char,
12    err_msg: *mut *mut c_char,
13) -> c_int {
14    assert!(!state.is_null());
15    assert!(!err_kind.is_null());
16    assert!(!err_msg.is_null());
17
18    unsafe {
19        *state = std::ptr::null_mut();
20        *err_kind = std::ptr::null_mut();
21        *err_msg = std::ptr::null_mut();
22    }
23
24    match nispor::NetState::retrieve() {
25        Ok(s) => unsafe {
26            *state = CString::new(serde_json::to_string(&s).unwrap())
27                .unwrap()
28                .into_raw();
29            libc::EXIT_SUCCESS
30        },
31        Err(e) => unsafe {
32            *err_msg = CString::new(e.msg).unwrap().into_raw();
33            *err_kind =
34                CString::new(format!("{}", &e.kind)).unwrap().into_raw();
35            libc::EXIT_FAILURE
36        },
37    }
38}
39
40#[allow(clippy::not_unsafe_ptr_arg_deref)]
41#[no_mangle]
42pub extern "C" fn nispor_net_state_free(state: *mut c_char) {
43    unsafe {
44        if !state.is_null() {
45            drop(CString::from_raw(state));
46        }
47    }
48}
49
50#[allow(clippy::not_unsafe_ptr_arg_deref)]
51#[no_mangle]
52pub extern "C" fn nispor_err_kind_free(err_kind: *mut c_char) {
53    unsafe {
54        if !err_kind.is_null() {
55            drop(CString::from_raw(err_kind));
56        }
57    }
58}
59
60#[allow(clippy::not_unsafe_ptr_arg_deref)]
61#[no_mangle]
62pub extern "C" fn nispor_err_msg_free(err_msg: *mut c_char) {
63    unsafe {
64        if !err_msg.is_null() {
65            drop(CString::from_raw(err_msg));
66        }
67    }
68}