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
use std::{
io,
os::raw::{
c_char,
c_void,
},
pin::Pin,
task::{
Context,
Poll,
},
};
use crate::{
cstr,
ffi,
inner,
interface::Interface,
};
type CallbackStream = crate::stream::ServiceStream<inner::OwnedService, BrowseResult>;
bitflags::bitflags! {
#[derive(Default)]
pub struct BrowsedFlags: ffi::DNSServiceFlags {
const MORE_COMING = ffi::FLAGS_MORE_COMING;
const ADD = ffi::FLAGS_ADD;
}
}
#[must_use = "streams do nothing unless polled"]
pub struct Browse {
stream: crate::fused_err_stream::FusedErrorStream<CallbackStream>,
}
impl Browse {
pin_utils::unsafe_pinned!(stream: crate::fused_err_stream::FusedErrorStream<CallbackStream>);
}
impl futures_core::Stream for Browse {
type Item = io::Result<BrowseResult>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.stream().poll_next(cx)
}
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct BrowseResult {
pub flags: BrowsedFlags,
pub interface: Interface,
pub service_name: String,
pub reg_type: String,
pub domain: String,
}
impl BrowseResult {
pub fn resolve(&self) -> crate::Resolve {
crate::resolve(
self.interface,
&self.service_name,
&self.reg_type,
&self.domain,
)
}
}
unsafe extern "C" fn browse_callback(
_sd_ref: ffi::DNSServiceRef,
flags: ffi::DNSServiceFlags,
interface_index: u32,
error_code: ffi::DNSServiceErrorType,
service_name: *const c_char,
reg_type: *const c_char,
reply_domain: *const c_char,
context: *mut c_void,
) {
CallbackStream::run_callback(context, error_code, || {
let service_name = cstr::from_cstr(service_name)?;
let reg_type = cstr::from_cstr(reg_type)?;
let reply_domain = cstr::from_cstr(reply_domain)?;
Ok(BrowseResult {
flags: BrowsedFlags::from_bits_truncate(flags),
interface: Interface::from_raw(interface_index),
service_name: service_name.to_string(),
reg_type: reg_type.to_string(),
domain: reply_domain.to_string(),
})
});
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct BrowseData<'a> {
pub interface: Interface,
pub domain: Option<&'a str>,
#[doc(hidden)]
pub _non_exhaustive: crate::non_exhaustive_struct::NonExhaustiveMarker,
}
impl<'a> Default for BrowseData<'a> {
fn default() -> Self {
Self {
interface: Interface::default(),
domain: None,
_non_exhaustive: crate::non_exhaustive_struct::NonExhaustiveMarker,
}
}
}
fn _browse_extended(reg_type: &str, data: BrowseData<'_>) -> io::Result<Browse> {
crate::init();
let reg_type = cstr::CStr::from(®_type)?;
let domain = cstr::NullableCStr::from(&data.domain)?;
let stream = CallbackStream::new(move |sender| {
inner::OwnedService::browse(
0,
data.interface.into_raw(),
®_type,
&domain,
Some(browse_callback),
sender,
)
})
.into();
Ok(Browse { stream })
}
#[doc(alias = "DNSServiceBrowse")]
pub fn browse_extended(reg_type: &str, data: BrowseData<'_>) -> Browse {
match _browse_extended(reg_type, data) {
Ok(r) => r,
Err(e) => Browse {
stream: Err(e).into(),
},
}
}
#[doc(alias = "DNSServiceBrowse")]
pub fn browse(reg_type: &str) -> Browse {
browse_extended(reg_type, BrowseData::default())
}