1use std::ffi::{CStr, CString};
4
5use libc::{c_char, c_int};
6
7use crate::{
8 NMSTATE_FAIL, NMSTATE_PASS, policy::c_str_to_net_policy,
9 state::c_str_to_net_state,
10};
11
12#[allow(clippy::not_unsafe_ptr_arg_deref)]
13#[unsafe(no_mangle)]
14pub extern "C" fn nmstate_validate(
15 state_or_policy: *const c_char,
16 cur_state: *const c_char,
17 err_kind: *mut *mut c_char,
18 err_msg: *mut *mut c_char,
19) -> c_int {
20 assert!(!state_or_policy.is_null());
21 assert!(!err_kind.is_null());
22 assert!(!err_msg.is_null());
23
24 unsafe {
25 *err_kind = std::ptr::null_mut();
26 *err_msg = std::ptr::null_mut();
27 }
28
29 let cur_state = if cur_state.is_null() {
30 None
31 } else {
32 let cur_state_cstr = unsafe { CStr::from_ptr(cur_state) };
33 if cur_state_cstr.to_str().map(|s| s.is_empty()) == Ok(true) {
34 None
35 } else {
36 match c_str_to_net_state(cur_state, err_kind, err_msg) {
37 Ok(s) => Some(s),
38 Err(rc) => {
39 return rc;
40 }
41 }
42 }
43 };
44
45 if let Ok(mut policy) =
46 c_str_to_net_policy(state_or_policy, err_kind, err_msg)
47 {
48 policy.current = cur_state.clone();
49 if let Err(e) = policy.validate() {
50 unsafe {
51 *err_msg =
52 CString::new(e.msg().to_string()).unwrap().into_raw();
53 *err_kind =
54 CString::new(e.kind().to_string()).unwrap().into_raw();
55 }
56 return NMSTATE_FAIL;
57 } else {
58 return NMSTATE_PASS;
59 }
60 }
61
62 match c_str_to_net_state(state_or_policy, err_kind, err_msg) {
65 Ok(state) => {
66 if let Err(e) = state.validate(cur_state.as_ref()) {
67 unsafe {
68 *err_msg =
69 CString::new(e.msg().to_string()).unwrap().into_raw();
70 *err_kind =
71 CString::new(e.kind().to_string()).unwrap().into_raw();
72 }
73 return NMSTATE_FAIL;
74 }
75 }
76 Err(rc) => {
77 return rc;
78 }
79 }
80
81 NMSTATE_PASS
82}