async_dnssd/
ffi.rs

1// #![allow(dead_code)]
2
3use std::os::raw::{
4	c_char,
5	c_int,
6	c_void,
7};
8
9// type without an instance
10pub enum DNSServiceT {}
11pub type DNSServiceRef = *mut DNSServiceT;
12
13// type without an instance
14pub enum DNSRecordT {}
15pub type DNSRecordRef = *mut DNSRecordT;
16
17pub type DNSServiceFlags = u32;
18// pub const FLAGS_NONE                 : DNSServiceFlags = 0x0;
19pub const FLAGS_MORE_COMING: DNSServiceFlags = 0x1;
20pub const FLAGS_ADD: DNSServiceFlags = 0x2;
21pub const FLAGS_DEFAULT: DNSServiceFlags = 0x4;
22pub const FLAGS_NO_AUTO_RENAME: DNSServiceFlags = 0x8;
23pub const FLAGS_SHARED: DNSServiceFlags = 0x10;
24pub const FLAGS_UNIQUE: DNSServiceFlags = 0x20;
25pub const FLAGS_BROWSE_DOMAINS: DNSServiceFlags = 0x40;
26pub const FLAGS_REGISTRATION_DOMAINS: DNSServiceFlags = 0x80;
27// unix only?
28#[cfg(unix)]
29pub const FLAGS_LONG_LIVED_QUERY: DNSServiceFlags = 0x100;
30#[cfg(not(unix))]
31pub const FLAGS_LONG_LIVED_QUERY: DNSServiceFlags = 0;
32// avahi only?
33// pub const FLAGS_ALLOW_REMOTE_QUERY: DNSServiceFlags = 0x200;
34// pub const FLAGS_FORCE_MULTICAS: DNSServiceFlags = 0x400;
35// pub const FLAGS_RETURN_CNAME: DNSServiceFlags = 0x800;
36
37/// Maximum length of full name including trailing dot and terminating NULL
38///
39/// See [`kDNSServiceMaxDomainName`](https://developer.apple.com/documentation/dnssd/kdnsservicemaxdomainname)
40pub const MAX_DOMAIN_NAME: usize = 1009;
41
42pub const INTERFACE_INDEX_ANY: u32 = 0;
43pub const INTERFACE_INDEX_LOCAL_ONLY: u32 = !0;
44pub const INTERFACE_INDEX_UNICAST: u32 = !1;
45pub const INTERFACE_INDEX_P2P: u32 = !2;
46
47macro_rules! c_api_enum {
48	($(#[$m:meta])* $name:ident : $ty:tt => $($case:ident = $val:expr,)* ) => (
49		#[derive(Clone,Copy,Eq,PartialEq,Ord,PartialOrd,Hash,Debug)]
50		#[repr($ty)]
51		$(#[$m])*
52		pub enum $name {
53			$($case = $val,)*
54		}
55		impl $name {
56			pub fn try_from(value: $ty) -> Option<Self> {
57				$(if value == $val {
58					Some(Self::$case)
59				} else)* {
60					None
61				}
62			}
63		}
64	)
65}
66
67pub type DNSServiceErrorType = i32;
68
69c_api_enum! {
70#[non_exhaustive]
71DNSServiceNoError: i32 =>
72	NoError               = 0,
73	// windows "TCP Connection Status"
74	ConnectionPending     = -65570,
75	ConnectionFailed      = -65571,
76	ConnectionEstablished = -65572,
77	// windows "Non-error values"
78	GrowCache             = -65790,
79	ConfigChanged         = -65791,
80	MemFree               = -65792,
81}
82
83c_api_enum! {
84/// Known error codes
85///
86/// See [`DNSServiceErrorType`](https://developer.apple.com/documentation/dnssd/1823426-anonymous)
87#[non_exhaustive]
88DNSServiceError: i32 =>
89	Unknown               = -65537,
90	NoSuchName            = -65538,
91	NoMemory              = -65539,
92	BadParam              = -65540,
93	BadReference          = -65541,
94	BadState              = -65542,
95	BadFlags              = -65543,
96	Unsupported           = -65544,
97	NotInitialized        = -65545,
98	NoCache               = -65546,
99	AlreadyRegistered     = -65547,
100	NameConflict          = -65548,
101	Invalid               = -65549,
102	Incompatible          = -65551,
103	BadInterfaceIndex     = -65552,
104	Refused               = -65553,
105	NoSuchRecord          = -65554,
106	NoAuth                = -65555,
107	NoSuchKey             = -65556,
108	NoValue               = -65557,
109	BufferTooSmall        = -65558,
110}
111
112pub type DNSServiceDomainEnumReply = Option<
113	unsafe extern "C" fn(
114		sd_ref: DNSServiceRef,
115		flags: DNSServiceFlags,
116		interface_index: u32,
117		error_code: DNSServiceErrorType,
118		reply_domain: *const c_char,
119		context: *mut c_void,
120	),
121>;
122pub type DNSServiceRegisterReply = Option<
123	unsafe extern "C" fn(
124		sd_ref: DNSServiceRef,
125		flags: DNSServiceFlags,
126		error_code: DNSServiceErrorType,
127		name: *const c_char,
128		reg_type: *const c_char,
129		domain: *const c_char,
130		context: *mut c_void,
131	),
132>;
133pub type DNSServiceBrowseReply = Option<
134	unsafe extern "C" fn(
135		sd_ref: DNSServiceRef,
136		flags: DNSServiceFlags,
137		interface_index: u32,
138		error_code: DNSServiceErrorType,
139		service_name: *const c_char,
140		reg_type: *const c_char,
141		reply_domain: *const c_char,
142		context: *mut c_void,
143	),
144>;
145pub type DNSServiceResolveReply = Option<
146	unsafe extern "C" fn(
147		sd_ref: DNSServiceRef,
148		flags: DNSServiceFlags,
149		interface_index: u32,
150		error_code: DNSServiceErrorType,
151		fullname: *const c_char,
152		host_target: *const c_char,
153		port: u16,
154		txt_len: u16,
155		txt_record: *const u8,
156		context: *mut c_void,
157	),
158>;
159pub type DNSServiceRegisterRecordReply = Option<
160	unsafe extern "C" fn(
161		sd_ref: DNSServiceRef,
162		record_ref: DNSRecordRef,
163		flags: DNSServiceFlags,
164		error_code: DNSServiceErrorType,
165		context: *mut c_void,
166	),
167>;
168pub type DNSServiceQueryRecordReply = Option<
169	unsafe extern "C" fn(
170		sd_ref: DNSServiceRef,
171		flags: DNSServiceFlags,
172		interface_index: u32,
173		error_code: DNSServiceErrorType,
174		fullname: *const c_char,
175		rr_type: u16,
176		rr_class: u16,
177		rd_len: u16,
178		rdata: *const u8,
179		ttl: u32,
180		context: *mut c_void,
181	),
182>;
183
184extern "C" {
185	pub fn DNSServiceRefSockFD(sd_ref: DNSServiceRef) -> c_int;
186	pub fn DNSServiceProcessResult(sd_ref: DNSServiceRef) -> DNSServiceErrorType;
187	pub fn DNSServiceRefDeallocate(sd_ref: DNSServiceRef);
188	pub fn DNSServiceEnumerateDomains(
189		sd_ref: *mut DNSServiceRef,
190		flags: DNSServiceFlags,
191		interface_index: u32,
192		callback: DNSServiceDomainEnumReply,
193		context: *mut c_void,
194	) -> DNSServiceErrorType;
195	pub fn DNSServiceRegister(
196		sd_ref: *mut DNSServiceRef,
197		flags: DNSServiceFlags,
198		interface_index: u32,
199		name: *const c_char,
200		reg_type: *const c_char,
201		domain: *const c_char,
202		host: *const c_char,
203		port: u16,
204		txt_len: u16,
205		txt_record: *const u8,
206		callback: DNSServiceRegisterReply,
207		context: *mut c_void,
208	) -> DNSServiceErrorType;
209	pub fn DNSServiceAddRecord(
210		sd_ref: DNSServiceRef,
211		record_ref: *mut DNSRecordRef,
212		flags: DNSServiceFlags,
213		rr_type: u16,
214		rd_len: u16,
215		rdata: *const u8,
216		ttl: u32,
217	) -> DNSServiceErrorType;
218	pub fn DNSServiceUpdateRecord(
219		sd_ref: DNSServiceRef,
220		record_ref: DNSRecordRef,
221		flags: DNSServiceFlags,
222		rd_len: u16,
223		rdata: *const u8,
224		ttl: u32,
225	) -> DNSServiceErrorType;
226	pub fn DNSServiceRemoveRecord(
227		sd_ref: DNSServiceRef,
228		record_ref: DNSRecordRef,
229		flags: DNSServiceFlags,
230	) -> DNSServiceErrorType;
231	pub fn DNSServiceBrowse(
232		sd_ref: *mut DNSServiceRef,
233		flags: DNSServiceFlags,
234		interface_index: u32,
235		reg_type: *const c_char,
236		domain: *const c_char,
237		callback: DNSServiceBrowseReply,
238		context: *mut c_void,
239	) -> DNSServiceErrorType;
240	pub fn DNSServiceResolve(
241		sd_ref: *mut DNSServiceRef,
242		flags: DNSServiceFlags,
243		interface_index: u32,
244		name: *const c_char,
245		reg_type: *const c_char,
246		domain: *const c_char,
247		callback: DNSServiceResolveReply,
248		context: *mut c_void,
249	) -> DNSServiceErrorType;
250	pub fn DNSServiceCreateConnection(sd_ref: *mut DNSServiceRef) -> DNSServiceErrorType;
251	pub fn DNSServiceRegisterRecord(
252		sd_ref: DNSServiceRef,
253		record_ref: *mut DNSRecordRef,
254		flags: DNSServiceFlags,
255		interface_index: u32,
256		fullname: *const c_char,
257		rr_type: u16,
258		rr_class: u16,
259		rd_len: u16,
260		rdata: *const u8,
261		ttl: u32,
262		callback: DNSServiceRegisterRecordReply,
263		context: *mut c_void,
264	) -> DNSServiceErrorType;
265	pub fn DNSServiceQueryRecord(
266		sd_ref: *mut DNSServiceRef,
267		flags: DNSServiceFlags,
268		interface_index: u32,
269		fullname: *const c_char,
270		rr_type: u16,
271		rr_class: u16,
272		callback: DNSServiceQueryRecordReply,
273		context: *mut c_void,
274	) -> DNSServiceErrorType;
275	pub fn DNSServiceReconfirmRecord(
276		flags: DNSServiceFlags,
277		interface_index: u32,
278		fullname: *const c_char,
279		rr_type: u16,
280		rr_class: u16,
281		rd_len: u16,
282		rdata: *const u8,
283	) -> DNSServiceErrorType;
284	pub fn DNSServiceConstructFullName(
285		fullName: *mut c_char,
286		service: *const c_char,
287		reg_type: *const c_char,
288		domain: *const c_char,
289	) -> c_int;
290}
291
292// TXTRecordRef utils not wrapped - should be easy enough to implement
293// in pure rust
294
295/* Not used so far:
296#[cfg(windows)]
297mod ffi_windows {
298	use super::DNSServiceErrorType;
299	use std::os::raw::{
300		c_int,
301		c_void,
302	};
303
304	pub type DNSServiceInitializeFlags = u32;
305	pub const INITIALIZE_FLAGS_NONE: DNSServiceInitializeFlags = 0x0;
306	pub const INITIALIZE_FLAGS_ADVERTISE: DNSServiceInitializeFlags = 0x1;
307	pub const INITIALIZE_FLAGS_NO_SERVER_CHECK: DNSServiceInitializeFlags = 0x2;
308
309	pub type DNSPropertyCode = u32;
310
311	pub const PROPERTY_CODE_VERSION: DNSPropertyCode = 0x76657273;
312	#[repr(C)]
313	pub struct DnsPropertyVersion {
314		pub code: DNSPropertyCode,
315
316		pub client_current_version: u32,
317		pub client_oldest_server_version: u32,
318		pub server_current_version: u32,
319		pub server_oldest_client_version: u32,
320	}
321
322	extern "C" {
323		pub fn DNSServiceInitialize(
324			inFlags: DNSServiceInitializeFlags,
325			inCacheEntryCount: c_int,
326		) -> DNSServiceErrorType;
327		pub fn DNSServiceFinalize();
328		pub fn DNSServiceCheckVersion() -> DNSServiceErrorType;
329
330		// TODO? DNSPropertyData on windows.
331		// the c API uses a union... - using void instead here
332		pub fn DNSServiceCopyProperty(
333			inCode: DNSPropertyCode,
334			outData: *mut c_void,
335		) -> DNSServiceErrorType;
336		pub fn DNSServiceReleaseProperty(
337			inData: *mut c_void,
338		) -> DNSServiceErrorType;
339	}
340}
341#[cfg(windows)]
342pub use self::ffi_windows::*;
343*/