1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
//! Rust friendly `AvahiEntryGroup` wrappers/helpers
use std::sync::Arc;
use super::{client::ManagedAvahiClient, string_list::ManagedAvahiStringList};
use crate::avahi::avahi_util;
use crate::ffi::UnwrapMutOrNull;
use crate::Result;
use avahi_sys::{
avahi_client_errno, avahi_entry_group_add_service_strlst,
avahi_entry_group_add_service_subtype, avahi_entry_group_commit, avahi_entry_group_free,
avahi_entry_group_is_empty, avahi_entry_group_new, avahi_entry_group_reset, AvahiClient,
AvahiEntryGroup, AvahiEntryGroupCallback, AvahiIfIndex, AvahiProtocol, AvahiPublishFlags,
};
use libc::{c_char, c_void};
/// Wraps the `AvahiEntryGroup` type from the raw Avahi bindings.
///
/// This struct allocates a new `*mut AvahiEntryGroup` when `ManagedAvahiEntryGroup::new()` is
/// invoked and calls the Avahi function responsible for freeing the group on `trait Drop`.
#[derive(Debug)]
pub struct ManagedAvahiEntryGroup {
inner: *mut AvahiEntryGroup,
_client: Arc<ManagedAvahiClient>,
}
impl ManagedAvahiEntryGroup {
/// Initializes the underlying `*mut AvahiEntryGroup` and verifies it was created; returning
/// `Err(String)` if unsuccessful.
pub fn new(
ManagedAvahiEntryGroupParams {
client,
callback,
userdata,
}: ManagedAvahiEntryGroupParams,
) -> Result<Self> {
let inner = unsafe { avahi_entry_group_new(client.inner, callback, userdata) };
if inner.is_null() {
let err = avahi_util::get_error(unsafe { avahi_client_errno(client.inner) });
Err(format!("could not initialize AvahiEntryGroup: {}", err).into())
} else {
Ok(Self {
inner,
_client: client,
})
}
}
/// Delegate function for [`avahi_entry_group_is_empty()`].
///
/// [`avahi_entry_group_is_empty()`]: https://avahi.org/doxygen/html/publish_8h.html#af5a78ee1fda6678970536889d459d85c
pub fn is_empty(&self) -> bool {
unsafe { avahi_entry_group_is_empty(self.inner) != 0 }
}
/// Delegate function for [`avahi_entry_group_add_service()`].
///
/// Also propagates any error returned into a `Result`.
///
/// [`avahi_entry_group_add_service()`]: https://avahi.org/doxygen/html/publish_8h.html#acb05a7d3d23a3b825ca77cb1c7d00ce4
pub fn add_service(
&mut self,
AddServiceParams {
interface,
protocol,
flags,
name,
kind,
domain,
host,
port,
txt,
}: AddServiceParams,
) -> Result<()> {
avahi_util::sys_exec(
|| unsafe {
avahi_entry_group_add_service_strlst(
self.inner,
interface,
protocol,
flags,
name,
kind,
domain,
host,
port,
txt.map(|t| t.inner()).unwrap_mut_or_null(),
)
},
"could not register service",
)
}
/// Delegate function for [`avahi_entry_group_add_service_subtype()`].
///
/// Also propagates any error returned into a `Result`.
///
/// [`avahi_entry_group_add_service_subtype()`]: https://avahi.org/doxygen/html/publish_8h.html#a93841be69a152d3134b408c25bb4d5d5
pub fn add_service_subtype(
&mut self,
AddServiceSubtypeParams {
interface,
protocol,
flags,
name,
kind,
domain,
subtype,
}: AddServiceSubtypeParams,
) -> Result<()> {
avahi_util::sys_exec(
|| unsafe {
avahi_entry_group_add_service_subtype(
self.inner, interface, protocol, flags, name, kind, domain, subtype,
)
},
"could not register service subtype",
)
}
/// Delegate function for [`avahi_entry_group_commit()`].
///
/// Also propagates any error returned into a `Result`.
///
/// [`avahi_entry_group_commit()`]: https://avahi.org/doxygen/html/publish_8h.html#a2375338d23af4281399404758840a2de
pub fn commit(&mut self) -> Result<()> {
avahi_util::sys_exec(
|| unsafe { avahi_entry_group_commit(self.inner) },
"could not commit service",
)
}
/// Delegate function for [`avahi_entry_group_reset()`].
///
/// [`avahi_entry_group_reset()`]: https://avahi.org/doxygen/html/publish_8h.html#a1293bbccf878dbeb9916660022bc71b2
pub fn reset(&mut self) {
unsafe { avahi_entry_group_reset(self.inner) };
}
/// Delegate function for [`avahi_entry_group_get_client()`].
///
/// # Safety
/// This function is unsafe because it returns a raw pointer to
/// the underlying `AvahiClient`.
pub unsafe fn get_client(&self) -> *mut AvahiClient {
avahi_sys::avahi_entry_group_get_client(self.inner)
}
}
impl Drop for ManagedAvahiEntryGroup {
fn drop(&mut self) {
unsafe { avahi_entry_group_free(self.inner) };
}
}
/// Holds parameters for initializing a new `ManagedAvahiEntryGroup` with
/// `ManagedAvahiEntryGroup::new()`.
///
/// See [`avahi_entry_group_new()`] for more information about these parameters.
///
/// [avahi_entry_group_new()]: https://avahi.org/doxygen/html/publish_8h.html#abb17598f2b6ec3c3f69defdd488d568c
#[derive(Builder, BuilderDelegate)]
pub struct ManagedAvahiEntryGroupParams {
client: Arc<ManagedAvahiClient>,
callback: AvahiEntryGroupCallback,
userdata: *mut c_void,
}
/// Holds parameters for `ManagedAvahiEntryGroup::add_service()`.
///
/// See [`avahi_entry_group_add_service()`] for more information about these parameters.
///
/// [`avahi_entry_group_add_service()`]: https://avahi.org/doxygen/html/publish_8h.html#acb05a7d3d23a3b825ca77cb1c7d00ce4
#[derive(Builder, BuilderDelegate)]
pub struct AddServiceParams<'a> {
interface: AvahiIfIndex,
protocol: AvahiProtocol,
flags: AvahiPublishFlags,
name: *const c_char,
kind: *const c_char,
domain: *const c_char,
host: *const c_char,
port: u16,
txt: Option<&'a ManagedAvahiStringList>,
}
/// Holds parameters for `ManagedAvahiEntryGroup::add_service_subtype()`.
///
/// See [`avahi_entry_group_add_service_subtype()`] for more information about these parameters.
///
/// [`avahi_entry_group_add_service_subtype()`]: https://www.avahi.org/doxygen/html/publish_8h.html#a93841be69a152d3134b408c25bb4d5d5
#[derive(Builder, BuilderDelegate)]
pub struct AddServiceSubtypeParams {
interface: AvahiIfIndex,
protocol: AvahiProtocol,
flags: AvahiPublishFlags,
name: *const c_char,
kind: *const c_char,
domain: *const c_char,
subtype: *const c_char,
}