4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 using System.Runtime.InteropServices;
32 public enum PublishFlags {
45 public enum EntryGroupState {
53 public class EntryGroupStateArgs : EventArgs
55 private EntryGroupState state;
57 public EntryGroupState State
62 public EntryGroupStateArgs (EntryGroupState state)
68 internal delegate void EntryGroupCallback (IntPtr group, EntryGroupState state, IntPtr userdata);
69 public delegate void EntryGroupStateHandler (object o, EntryGroupStateArgs args);
71 public class EntryGroup : IDisposable
73 private Client client;
74 private IntPtr handle;
75 private EntryGroupCallback cb;
77 [DllImport ("avahi-client")]
78 private static extern IntPtr avahi_entry_group_new (IntPtr client, EntryGroupCallback cb, IntPtr userdata);
80 [DllImport ("avahi-client")]
81 private static extern int avahi_entry_group_commit (IntPtr group);
83 [DllImport ("avahi-client")]
84 private static extern int avahi_entry_group_reset (IntPtr group);
86 [DllImport ("avahi-client")]
87 private static extern EntryGroupState avahi_entry_group_get_state (IntPtr group);
89 [DllImport ("avahi-client")]
90 private static extern bool avahi_entry_group_is_empty (IntPtr group);
92 [DllImport ("avahi-client")]
93 private static extern int avahi_entry_group_add_service_strlst (IntPtr group, int iface, Protocol proto,
94 PublishFlags flags, byte[] name, byte[] type,
95 byte[] domain, byte[] host, UInt16 port,
98 [DllImport ("avahi-client")]
99 private static extern int avahi_entry_group_update_service_strlst (IntPtr group, int iface, Protocol proto,
100 PublishFlags flags, byte[] name,
101 byte[] type, byte[] domain, IntPtr strlst);
103 [DllImport ("avahi-client")]
104 private static extern int avahi_entry_group_add_service_subtype (IntPtr group, int iface, Protocol proto,
105 PublishFlags flags, byte[] name, byte[] type,
106 byte[] domain, byte[] subtype);
108 [DllImport ("avahi-client")]
109 private static extern int avahi_entry_group_add_address (IntPtr group, int iface, Protocol proto,
110 PublishFlags flags, byte[] name, IntPtr address);
113 [DllImport ("avahi-client")]
114 private static extern int avahi_entry_group_add_record (IntPtr group, int iface, Protocol proto,
115 PublishFlags flags, byte[] name, RecordClass clazz,
116 RecordType type, uint ttl, byte[] rdata, int size);
118 [DllImport ("avahi-client")]
119 private static extern void avahi_entry_group_free (IntPtr group);
121 [DllImport ("avahi-common")]
122 private static extern IntPtr avahi_string_list_new (IntPtr txt);
124 [DllImport ("avahi-common")]
125 private static extern IntPtr avahi_string_list_add (IntPtr list, byte[] txt);
127 [DllImport ("avahi-common")]
128 private static extern void avahi_string_list_free (IntPtr list);
130 [DllImport ("avahi-common")]
131 private static extern int avahi_service_name_join (IntPtr buf, int len, byte[] name, byte[] type,
134 [DllImport ("avahi-common")]
135 private static extern int avahi_service_name_split (byte[] service, IntPtr name, int name_len,
136 IntPtr type, int type_len,
137 IntPtr domain, int domain_len);
140 [DllImport ("avahi-common")]
141 private static extern IntPtr avahi_alternative_service_name (byte[] name);
143 public event EntryGroupStateHandler StateChanged;
145 public EntryGroupState State
149 return avahi_entry_group_get_state (handle);
158 return avahi_entry_group_is_empty (handle);
163 public EntryGroup (Client client)
165 this.client = client;
166 cb = OnEntryGroupCallback;
169 handle = avahi_entry_group_new (client.Handle, cb, IntPtr.Zero);
170 if (handle == IntPtr.Zero)
171 client.ThrowError ();
180 public void Dispose ()
182 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero) {
184 avahi_entry_group_free (handle);
185 handle = IntPtr.Zero;
190 public void Commit ()
193 if (avahi_entry_group_commit (handle) < 0)
194 client.ThrowError ();
201 if (avahi_entry_group_reset (handle) < 0)
202 client.ThrowError ();
206 public void AddService (string name, string type, string domain,
207 UInt16 port, params string[] txt)
209 AddService (PublishFlags.None, name, type, domain, port, txt);
212 public void AddService (PublishFlags flags, string name, string type, string domain,
213 UInt16 port, params string[] txt)
215 AddService (-1, Protocol.Unspecified, flags, name, type, domain, null, port, txt);
218 public void AddService (int iface, Protocol proto, PublishFlags flags, string name, string type, string domain,
219 string host, UInt16 port, params string[] txt)
221 IntPtr list = avahi_string_list_new (IntPtr.Zero);
224 foreach (string item in txt) {
225 list = avahi_string_list_add (list, Utility.StringToBytes (item));
229 AddService (iface, proto, flags, name, type, domain, host, port, list);
232 public void AddService (int iface, Protocol proto, PublishFlags flags, string name, string type, string domain,
233 string host, UInt16 port, params byte[][] txt)
235 IntPtr list = avahi_string_list_new (IntPtr.Zero);
238 foreach (byte[] item in txt) {
239 list = avahi_string_list_add (list, item);
243 AddService (iface, proto, flags, name, type, domain, host, port, list);
246 private void AddService (int iface, Protocol proto, PublishFlags flags, string name, string type,
247 string domain, string host, UInt16 port, IntPtr list)
249 int ret = avahi_entry_group_add_service_strlst (handle, iface, proto, flags,
250 Utility.StringToBytes (name),
251 Utility.StringToBytes (type),
252 Utility.StringToBytes (domain),
253 Utility.StringToBytes (host), port, list);
255 avahi_string_list_free (list);
258 client.ThrowError ();
262 public void UpdateService (string name, string type, string domain, params string[] txt)
264 UpdateService (-1, Protocol.Unspecified, PublishFlags.None, name, type, domain, txt);
267 public void UpdateService (int iface, Protocol proto, PublishFlags flags, string name, string type,
268 string domain, params string[] txt)
270 IntPtr list = avahi_string_list_new (IntPtr.Zero);
273 foreach (string item in txt) {
274 list = avahi_string_list_add (list, Utility.StringToBytes (item));
278 UpdateService (iface, proto, flags, name, type, domain, list);
281 public void UpdateService (int iface, Protocol proto, PublishFlags flags, string name, string type,
282 string domain, params byte[][] txt)
284 IntPtr list = avahi_string_list_new (IntPtr.Zero);
287 foreach (byte[] item in txt) {
288 list = avahi_string_list_add (list, item);
292 UpdateService (iface, proto, flags, name, type, domain, list);
295 private void UpdateService (int iface, Protocol proto, PublishFlags flags, string name, string type,
296 string domain, IntPtr list)
299 int ret = avahi_entry_group_update_service_strlst (handle, iface, proto, flags,
300 Utility.StringToBytes (name),
301 Utility.StringToBytes (type),
302 Utility.StringToBytes (domain),
305 avahi_string_list_free (list);
308 client.ThrowError ();
313 public void AddServiceSubtype (string name, string type, string domain, string subtype)
315 AddServiceSubtype (-1, Protocol.Unspecified, PublishFlags.None, name, type, domain, subtype);
318 public void AddServiceSubtype (int iface, Protocol proto, PublishFlags flags, string name,
319 string type, string domain, string subtype)
322 int ret = avahi_entry_group_add_service_subtype (handle, iface, proto, flags,
323 Utility.StringToBytes (name),
324 Utility.StringToBytes (type),
325 Utility.StringToBytes (domain),
326 Utility.StringToBytes (subtype));
329 client.ThrowError ();
334 public void AddAddress (string name, IPAddress address)
336 AddAddress (-1, Protocol.Unspecified, PublishFlags.None, name, address);
339 public void AddAddress (int iface, Protocol proto, PublishFlags flags, string name, IPAddress address)
341 IntPtr addressPtr = Utility.AddressToPtr (address);
344 int ret = avahi_entry_group_add_address (handle, iface, proto, flags,
345 Utility.StringToBytes (name), addressPtr);
347 Utility.Free (addressPtr);
350 client.ThrowError ();
355 public void AddRecord (string name, RecordClass clazz, RecordType type, uint ttl, byte[] rdata, int length)
357 AddRecord (-1, Protocol.Unspecified, PublishFlags.None, name, clazz, type, ttl, rdata, length);
360 public void AddRecord (int iface, Protocol proto, PublishFlags flags, string name,
361 RecordClass clazz, RecordType type, uint ttl, byte[] rdata, int length)
364 int ret = avahi_entry_group_add_record (handle, iface, proto, flags,
365 Utility.StringToBytes (name),
366 clazz, type, ttl, rdata, length);
369 client.ThrowError ();
374 public static string JoinServiceName (string name, string type, string domain)
376 int len = 4 * (name.Length + type.Length + domain.Length) + 4;
377 IntPtr buf = Stdlib.malloc ((ulong) len);
379 int ret = avahi_service_name_join (buf, len,
380 Utility.StringToBytes (name),
381 Utility.StringToBytes (type),
382 Utility.StringToBytes (domain));
386 return null; // FIXME, should throw exception
389 string service = Utility.PtrToString (buf);
395 public static void SplitServiceName (string service, out string name, out string type, out string domain)
399 IntPtr namePtr = Stdlib.malloc ((ulong) len);
400 IntPtr typePtr = Stdlib.malloc ((ulong) len);
401 IntPtr domainPtr = Stdlib.malloc ((ulong) len);
403 int ret = avahi_service_name_split (Utility.StringToBytes (service), namePtr, len, typePtr, len,
407 Utility.Free (namePtr);
408 Utility.Free (typePtr);
409 Utility.Free (domainPtr);
417 name = Utility.PtrToString (namePtr);
418 type = Utility.PtrToString (typePtr);
419 domain = Utility.PtrToString (domainPtr);
421 Utility.Free (namePtr);
422 Utility.Free (typePtr);
423 Utility.Free (domainPtr);
426 public static string GetAlternativeServiceName (string name) {
427 return Utility.PtrToStringFree (avahi_alternative_service_name (Utility.StringToBytes (name)));
430 private void OnEntryGroupCallback (IntPtr group, EntryGroupState state, IntPtr userdata)
432 if (StateChanged != null)
433 StateChanged (this, new EntryGroupStateArgs (state));