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 IntPtr avahi_alternative_service_name (byte[] name);
133 public event EntryGroupStateHandler StateChanged;
135 public EntryGroupState State
139 return avahi_entry_group_get_state (handle);
148 return avahi_entry_group_is_empty (handle);
153 public EntryGroup (Client client)
155 this.client = client;
156 cb = OnEntryGroupCallback;
159 handle = avahi_entry_group_new (client.Handle, cb, IntPtr.Zero);
160 if (handle == IntPtr.Zero)
161 client.ThrowError ();
170 public void Dispose ()
172 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero) {
174 avahi_entry_group_free (handle);
175 handle = IntPtr.Zero;
180 public void Commit ()
183 if (avahi_entry_group_commit (handle) < 0)
184 client.ThrowError ();
191 if (avahi_entry_group_reset (handle) < 0)
192 client.ThrowError ();
196 public void AddService (string name, string type, string domain,
197 UInt16 port, params string[] txt)
199 AddService (PublishFlags.None, name, type, domain, port, txt);
202 public void AddService (PublishFlags flags, string name, string type, string domain,
203 UInt16 port, params string[] txt)
205 AddService (-1, Protocol.Unspecified, flags, name, type, domain, null, port, txt);
208 public void AddService (int iface, Protocol proto, PublishFlags flags, string name, string type, string domain,
209 string host, UInt16 port, params string[] txt)
211 IntPtr list = avahi_string_list_new (IntPtr.Zero);
214 foreach (string item in txt) {
215 list = avahi_string_list_add (list, Utility.StringToBytes (item));
219 AddService (iface, proto, flags, name, type, domain, host, port, list);
222 public void AddService (int iface, Protocol proto, PublishFlags flags, string name, string type, string domain,
223 string host, UInt16 port, params byte[][] txt)
225 IntPtr list = avahi_string_list_new (IntPtr.Zero);
228 foreach (byte[] item in txt) {
229 list = avahi_string_list_add (list, item);
233 AddService (iface, proto, flags, name, type, domain, host, port, list);
236 private void AddService (int iface, Protocol proto, PublishFlags flags, string name, string type,
237 string domain, string host, UInt16 port, IntPtr list)
242 ret = avahi_entry_group_add_service_strlst (handle, iface, proto, flags,
243 Utility.StringToBytes (name),
244 Utility.StringToBytes (type),
245 Utility.StringToBytes (domain),
246 Utility.StringToBytes (host), port, list);
249 avahi_string_list_free (list);
252 client.ThrowError ();
256 public void UpdateService (string name, string type, string domain, params string[] txt)
258 UpdateService (-1, Protocol.Unspecified, PublishFlags.None, name, type, domain, txt);
261 public void UpdateService (int iface, Protocol proto, PublishFlags flags, string name, string type,
262 string domain, params string[] txt)
264 IntPtr list = avahi_string_list_new (IntPtr.Zero);
267 foreach (string item in txt) {
268 list = avahi_string_list_add (list, Utility.StringToBytes (item));
272 UpdateService (iface, proto, flags, name, type, domain, list);
275 public void UpdateService (int iface, Protocol proto, PublishFlags flags, string name, string type,
276 string domain, params byte[][] txt)
278 IntPtr list = avahi_string_list_new (IntPtr.Zero);
281 foreach (byte[] item in txt) {
282 list = avahi_string_list_add (list, item);
286 UpdateService (iface, proto, flags, name, type, domain, list);
289 private void UpdateService (int iface, Protocol proto, PublishFlags flags, string name, string type,
290 string domain, IntPtr list)
293 int ret = avahi_entry_group_update_service_strlst (handle, iface, proto, flags,
294 Utility.StringToBytes (name),
295 Utility.StringToBytes (type),
296 Utility.StringToBytes (domain),
299 avahi_string_list_free (list);
302 client.ThrowError ();
307 public void AddServiceSubtype (string name, string type, string domain, string subtype)
309 AddServiceSubtype (-1, Protocol.Unspecified, PublishFlags.None, name, type, domain, subtype);
312 public void AddServiceSubtype (int iface, Protocol proto, PublishFlags flags, string name,
313 string type, string domain, string subtype)
316 int ret = avahi_entry_group_add_service_subtype (handle, iface, proto, flags,
317 Utility.StringToBytes (name),
318 Utility.StringToBytes (type),
319 Utility.StringToBytes (domain),
320 Utility.StringToBytes (subtype));
323 client.ThrowError ();
328 public void AddAddress (string name, IPAddress address)
330 AddAddress (-1, Protocol.Unspecified, PublishFlags.None, name, address);
333 public void AddAddress (int iface, Protocol proto, PublishFlags flags, string name, IPAddress address)
335 IntPtr addressPtr = Utility.AddressToPtr (address);
338 int ret = avahi_entry_group_add_address (handle, iface, proto, flags,
339 Utility.StringToBytes (name), addressPtr);
341 Utility.Free (addressPtr);
344 client.ThrowError ();
349 public void AddRecord (string name, RecordClass clazz, RecordType type, uint ttl, byte[] rdata, int length)
351 AddRecord (-1, Protocol.Unspecified, PublishFlags.None, name, clazz, type, ttl, rdata, length);
354 public void AddRecord (int iface, Protocol proto, PublishFlags flags, string name,
355 RecordClass clazz, RecordType type, uint ttl, byte[] rdata, int length)
358 int ret = avahi_entry_group_add_record (handle, iface, proto, flags,
359 Utility.StringToBytes (name),
360 clazz, type, ttl, rdata, length);
363 client.ThrowError ();
368 public static string GetAlternativeServiceName (string name) {
369 return Utility.PtrToStringFree (avahi_alternative_service_name (Utility.StringToBytes (name)));
372 private void OnEntryGroupCallback (IntPtr group, EntryGroupState state, IntPtr userdata)
374 if (StateChanged != null)
375 StateChanged (this, new EntryGroupStateArgs (state));