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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
use super::enums::*;
use super::types::*;
use libc::{c_char, c_int, c_void, size_t};
#[link(name = "avahi-common")]
#[link(name = "avahi-client")]
#[link(name = "dbus-1")]
#[allow(improper_ctypes, dead_code)]
extern "C" {
/// Create a new main loop object.
///
/// # Return value
///
/// Main loop object - `AvahiSimplePoll`.
pub fn avahi_simple_poll_new() -> *mut AvahiSimplePoll;
/// Return the abstracted poll API object for this main loop object.
/// The is will return the same pointer each time it is called.
///
/// # Arguments
///
/// * `simple_poll` - Main loop object returned from `avahi_simple_poll_new`.
///
/// # Return value
///
/// Abstracted poll API object - `AvahiPoll`.
pub fn avahi_simple_poll_get(simple_poll: *mut AvahiSimplePoll) -> *mut AvahiPoll;
/// Call `avahi_simple_poll_iterate` in a loop and return if it
/// returns non-zero.
///
/// # Arguments
///
/// * `simple_poll` - Main loop object returned from `avahi_simple_poll_new`.
///
/// # Return value
///
/// Non-zero if `avahi_simple_poll_iterate` return non-zero value.
pub fn avahi_simple_poll_loop(simple_poll: *mut AvahiSimplePoll) -> c_int;
/// Free a main loop object.
///
/// # Arguments
///
/// * `simple_poll` - Main loop object returned from `avahi_simple_poll_new`.
pub fn avahi_simple_poll_free(simple_poll: *mut AvahiSimplePoll);
/// Creates a new client instance.
///
/// # Arguments
/// * `poll_api` - The abstract event loop API to use.
/// * `flags` - Some flags to modify the behaviour of the client library.
/// * `callback` - A callback that is called whenever the state of the client changes.
/// This may be NULL. Please note that this function is called for the
/// first time from within the avahi_client_new() context! Thus, in the
/// callback you should not make use of global variables that are initialized
/// only after your call to avahi_client_new(). A common mistake is to store
/// the AvahiClient pointer returned by avahi_client_new() in a global
/// variable and assume that this global variable already contains the valid
/// pointer when the callback is called for the first time. A work-around for
/// this is to always use the AvahiClient pointer passed to the callback
/// function instead of the global pointer.
/// * `userdata` - Some arbitrary user data pointer that will be passed to the callback.
/// * `error` - If creation of the client fails, this integer will contain the error cause.
/// May be NULL if you aren't interested in the reason why `avahi_client_new()`
/// has failed.
///
/// # Return value
///
/// New client instance - `AvahiClient`.
pub fn avahi_client_new(
poll_api: *const AvahiPoll,
flags: AvahiClientFlags,
callback: ClientCallback,
userdata: *mut c_void,
error: *mut c_int,
) -> *mut AvahiClient;
/// Free a client instance.
/// This will automatically free all associated browser, resolve and entry group objects.
/// All pointers to such objects become invalid!
///
/// # Arguments
///
/// * `client` - Active `AvahiClient` instance.
pub fn avahi_client_free(client: *mut AvahiClient);
pub fn avahi_client_get_host_name(client: *mut AvahiClient) -> *const c_char;
pub fn avahi_client_set_host_name(client: *mut AvahiClient, name: *const c_char) -> c_int;
pub fn avahi_client_get_host_name_fqdn(client: *mut AvahiClient) -> *const c_char;
pub fn avahi_client_get_state(client: *mut AvahiClient) -> AvahiClientState;
/// Get the last error number.
/// See avahi_strerror() for converting this error code into a human readable string.
///
/// # Arguments
///
/// * `client` - Active `AvahiClient` instance.
///
/// # Return value
///
/// Non-zero error code if any.
pub fn avahi_client_errno(client: *const AvahiClient) -> c_int;
pub fn avahi_is_valid_host_name(host_name: *const c_char) -> c_int;
pub fn avahi_alternative_host_name(host_name: *const c_char) -> *const c_char;
/// Browse for domains on the local network.
///
/// # Arguments
///
/// * `client` - Active `AvahiClient` instance.
/// * `interface` - Numeric network interface index. Takes OS dependent values and
/// the special constant AVAHI_IF_UNSPEC (-1).
/// * `protocol` - Protocol family specification `AvahiProtocol`.
/// * `domain` - Domain to look for.
/// * `service_type` - The type of domain to browse for `AvahiDomainBrowserType`.
/// * `flags` - Flags for lookup functions `AvahiLookupFlags`.
/// * `callback` - `AvahiDomainBrowserCallback` callback to be called for every new
/// found service.
/// * `userdata` - Some arbitrary user data pointer that will be passed to the callback.
///
/// # Return value
///
/// A domain browser `AvahiServiceBrowser` object.
pub fn avahi_service_browser_new(
client: *mut AvahiClient,
interface: AvahiIfIndex,
protocol: AvahiProtocol,
service_type: *const c_char,
domain: *const c_char,
flags: AvahiLookupFlags,
callback: ServiceBrowserCallback,
userdata: *mut c_void,
) -> *mut AvahiServiceBrowser;
/// Cleans up and frees an `AvahiServiceBrowser` object.
///
/// # Arguments
///
/// * `service_browser` - instance of `AvahiServiceBrowser`.
pub fn avahi_service_browser_free(service_browser: *mut AvahiServiceBrowser) -> c_int;
/// Create a new service resolver object.
///
/// Please make sure to pass all the service data you received via
/// `avahi_service_browser_new()` callback function, especially `interface`
/// and `protocol`.
///
/// # Arguments
///
/// * `client` - Active `AvahiClient` instance.
/// * `interface` - Interface argument received in `AvahiServiceBrowserCallback`.
/// * `protocol` - The protocol argument specifies the protocol (IPv4 or IPv6)
/// to use as transport for the queries which are sent out by this
/// resolver. Generally, on `protocol` you should only pass what was
/// supplied to you as parameter to your `AvahiServiceBrowserCallback`.
/// Or, more technically speaking: protocol specifies if the mDNS
/// queries should be sent as UDP/IPv4 resp. UDP/IPv6 packets.
/// * `name` - Name argument received in `AvahiServiceBrowserCallback`.
/// * `service_type` - Service type argument received in `AvahiServiceBrowserCallback`.
/// * `domain` - Domain argument received in `AvahiServiceBrowserCallback`.
/// * `aprotocol` - The `aprotocol` argument specifies the adress family
/// (IPv4 or IPv6) of the address of the service we are looking for.
/// In `aprotocol` you should pass what your application code can deal
/// with when connecting to the service. Or, more technically speaking:
/// `aprotocol` specifies whether the query is for a A resp. AAAA
/// resource record. AVAHI_PROTO_UNSPEC if your application can deal
/// with both IPv4 and IPv6
/// * `flags` - Flags for lookup functions `AvahiLookupFlags`.
/// * `callback` - `ServiceResolverCallback` callback to be called for every new
/// resolved service.
/// * `userdata` - Some arbitrary user data pointer that will be passed to the callback.
///
/// # Return value
///
/// A service resolver `AvahiServiceResolver` object.
pub fn avahi_service_resolver_new(
client: *mut AvahiClient,
interface: c_int,
protocol: AvahiProtocol,
name: *const c_char,
service_type: *const c_char,
domain: *const c_char,
aprotocol: AvahiProtocol,
flags: AvahiLookupFlags,
callback: ServiceResolverCallback,
userdata: *mut c_void,
) -> *mut AvahiServiceResolver;
pub fn avahi_service_resolver_free(resolver: *mut AvahiServiceResolver) -> c_int;
pub fn avahi_address_snprint(ret_s: *const c_char, length: size_t, a: *const AvahiAddress);
/// Convert the string list object to a single character string, seperated by spaces
/// and enclosed in "". `avahi_free` should always be called for the result!
/// This function doesn't work well with strings that contain NUL bytes.
///
/// # Arguments
///
/// * `string_list` - string list instance.
///
/// # Return value
///
/// Single character string, seperated by spaces and enclosed in "".
pub fn avahi_string_list_to_string(string_list: *mut AvahiStringList) -> *const c_char;
/// Free some memory.
///
/// # Arguments
///
/// * `pointer` - pointer to free memory for.
pub fn avahi_free(pointer: *mut c_void);
/// Return a human readable error string for the specified error code.
///
/// # Arguments
///
/// * `error` - Integer error code used by avahi.
///
/// # Return value
///
/// Human readable error string.
pub fn avahi_strerror(error: c_int) -> *const c_char;
/// Create a new main loop object (will be performed in a separate thread).
///
/// # Return value
///
/// Main loop object - `AvahiThreadedPoll`.
pub fn avahi_threaded_poll_new() -> *mut AvahiThreadedPoll;
/// Return the abstracted poll API object for this main loop object.
/// The is will return the same pointer each time it is called.
///
/// # Arguments
///
/// * `threaded_poll` - Main loop object returned from `avahi_threaded_poll_new`.
///
/// # Return value
///
/// Abstracted poll API object - `AvahiPoll`.
pub fn avahi_threaded_poll_get(threaded_poll: *mut AvahiThreadedPoll) -> *mut AvahiPoll;
/// Start the event loop helper thread.
///
/// After the thread has started you must make sure to access the event loop object
/// (`AvahiThreadedPoll`, `AvahiPoll` and all its associated objects) synchronized,
/// i.e. with proper locking. You may want to use `avahi_threaded_poll_lock` and
/// `avahi_threaded_poll_unlock` for this, which will lock the the entire event loop.
/// Please note that event loop callback functions are called from the event loop
/// helper thread with that lock held, i.e. `avahi_threaded_poll_lock` calls are not
/// required from event callbacks.
///
/// # Arguments
///
/// * `threaded_poll` - Main loop object returned from `avahi_threaded_poll_new`.
pub fn avahi_threaded_poll_start(threaded_poll: *mut AvahiThreadedPoll) -> c_int;
/// Request that the event loop quits and the associated thread stops.
///
/// Call this from outside the helper thread if you want to shut it down.
///
/// # Arguments
///
/// * `threaded_poll` - Main loop object returned from `avahi_threaded_poll_new`.
pub fn avahi_threaded_poll_stop(threaded_poll: *mut AvahiThreadedPoll) -> c_int;
pub fn avahi_threaded_poll_quit(threaded_poll: *mut AvahiThreadedPoll) -> c_void;
/// Free an event loop object.
///
/// This will stop the associated event loop thread (if it is running).
///
/// # Arguments
///
/// * `threaded_poll` - Main loop object returned from `avahi_threaded_poll_new`.
pub fn avahi_threaded_poll_free(threaded_poll: *mut AvahiThreadedPoll) -> c_void;
pub fn avahi_entry_group_new(
client: *mut AvahiClient,
callback: AvahiEntryGroupCallback,
userdata: *mut c_void,
) -> *mut AvahiEntryGroup;
pub fn avahi_entry_group_add_record(
group: *mut AvahiEntryGroup,
interface: c_int,
protocol: AvahiProtocol,
flags: AvahiPublishFlags,
name: *const c_char,
record_class: AvahiRecordClass,
record_type: AvahiRecordType,
ttl: u32,
rdata: *const c_void,
size: usize,
) -> c_int;
pub fn avahi_entry_group_add_service(
group: *mut AvahiEntryGroup,
interface: AvahiIfIndex,
protocol: AvahiProtocol,
flags: AvahiPublishFlags,
name: *const c_char,
record_type: *const c_char,
domain: *const c_char,
host: *const c_char,
port: u16,
text: *const c_void,
) -> c_int;
pub fn avahi_entry_group_commit(group: *mut AvahiEntryGroup) -> c_int;
pub fn avahi_entry_group_get_state(group: *mut AvahiEntryGroup) -> c_int;
}