openconnect_core/
protocols.rs

1use openconnect_sys::{oc_vpn_proto, openconnect_get_supported_protocols};
2
3#[derive(Debug, Clone)]
4pub struct Protocol {
5    pub name: String,
6    pub pretty_name: String,
7    pub description: String,
8    pub flags: u32,
9}
10
11pub fn get_supported_protocols() -> Vec<Protocol> {
12    let mut raw_protocols = std::ptr::null_mut::<oc_vpn_proto>();
13    let mut protocols: Vec<Protocol> = vec![];
14    unsafe {
15        let n = openconnect_get_supported_protocols(&mut raw_protocols);
16        if n < 0 {
17            panic!("openconnect_get_supported_protocols failed");
18        }
19        while !raw_protocols.is_null() && !(*raw_protocols).name.is_null() {
20            let name = std::ffi::CStr::from_ptr((*raw_protocols).name)
21                .to_string_lossy()
22                .to_string();
23            let pretty_name = std::ffi::CStr::from_ptr((*raw_protocols).pretty_name)
24                .to_string_lossy()
25                .to_string();
26            let description = std::ffi::CStr::from_ptr((*raw_protocols).description)
27                .to_string_lossy()
28                .to_string();
29            let flags = (*raw_protocols).flags;
30            protocols.push(Protocol {
31                name,
32                pretty_name,
33                description,
34                flags,
35            });
36            raw_protocols = raw_protocols.offset(1);
37        }
38    }
39    protocols
40}
41
42// TODO: temp solution
43pub fn get_anyconnect_protocol() -> Protocol {
44    get_supported_protocols()
45        .iter()
46        .find(|p| p.name == "anyconnect")
47        .expect("anyconnect protocol not found")
48        .clone()
49}