2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 using System.Runtime.InteropServices;
30 public enum PublishFlags {
43 public enum EntryGroupState {
51 public class EntryGroupStateArgs : EventArgs
53 private EntryGroupState state;
55 public EntryGroupState State
60 public EntryGroupStateArgs (EntryGroupState state)
66 internal delegate void EntryGroupCallback (IntPtr group, EntryGroupState state, IntPtr userdata);
67 public delegate void EntryGroupStateHandler (object o, EntryGroupStateArgs args);
69 public class EntryGroup : IDisposable
71 private Client client;
72 private IntPtr handle;
73 private EntryGroupCallback cb;
75 [DllImport ("avahi-client")]
76 private static extern IntPtr avahi_entry_group_new (IntPtr client, EntryGroupCallback cb, IntPtr userdata);
78 [DllImport ("avahi-client")]
79 private static extern int avahi_entry_group_commit (IntPtr group);
81 [DllImport ("avahi-client")]
82 private static extern int avahi_entry_group_reset (IntPtr group);
84 [DllImport ("avahi-client")]
85 private static extern EntryGroupState avahi_entry_group_get_state (IntPtr group);
87 [DllImport ("avahi-client")]
88 private static extern bool avahi_entry_group_is_empty (IntPtr group);
90 [DllImport ("avahi-client")]
91 private static extern int avahi_entry_group_add_service_strlst (IntPtr group, int iface, Protocol proto,
92 PublishFlags flags, byte[] name, byte[] type,
93 byte[] domain, byte[] host, UInt16 port,
96 [DllImport ("avahi-client")]
97 private static extern int avahi_entry_group_update_service_strlst (IntPtr group, int iface, Protocol proto,
98 PublishFlags flags, byte[] name,
99 byte[] type, byte[] domain, IntPtr strlst);
101 [DllImport ("avahi-client")]
102 private static extern int avahi_entry_group_add_service_subtype (IntPtr group, int iface, Protocol proto,
103 PublishFlags flags, byte[] name, byte[] type,
104 byte[] domain, byte[] subtype);
106 [DllImport ("avahi-client")]
107 private static extern int avahi_entry_group_add_address (IntPtr group, int iface, Protocol proto,
108 PublishFlags flags, byte[] name, IntPtr address);
111 [DllImport ("avahi-client")]
112 private static extern int avahi_entry_group_add_record (IntPtr group, int iface, Protocol proto,
113 PublishFlags flags, byte[] name, RecordClass clazz,
114 RecordType type, uint ttl, byte[] rdata, int size);
116 [DllImport ("avahi-client")]
117 private static extern void avahi_entry_group_free (IntPtr group);
119 [DllImport ("avahi-common")]
120 private static extern IntPtr avahi_string_list_new (IntPtr txt);
122 [DllImport ("avahi-common")]
123 private static extern IntPtr avahi_string_list_add (IntPtr list, byte[] txt);
125 [DllImport ("avahi-common")]
126 private static extern void avahi_string_list_free (IntPtr list);
128 [DllImport ("avahi-common")]
129 private static extern IntPtr avahi_alternative_service_name (byte[] name);
131 public event EntryGroupStateHandler StateChanged;
133 public EntryGroupState State
137 return avahi_entry_group_get_state (handle);
146 return avahi_entry_group_is_empty (handle);
151 public EntryGroup (Client client)
153 this.client = client;
154 cb = OnEntryGroupCallback;
157 handle = avahi_entry_group_new (client.Handle, cb, IntPtr.Zero);
158 if (handle == IntPtr.Zero)
159 client.ThrowError ();
168 public void Dispose ()
170 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero) {
172 avahi_entry_group_free (handle);
173 handle = IntPtr.Zero;
178 public void Commit ()
181 if (avahi_entry_group_commit (handle) < 0)
182 client.ThrowError ();
189 if (avahi_entry_group_reset (handle) < 0)
190 client.ThrowError ();
194 public void AddService (string name, string type, string domain,
195 UInt16 port, params string[] txt)
197 AddService (PublishFlags.None, name, type, domain, port, txt);
200 public void AddService (PublishFlags flags, string name, string type, string domain,
201 UInt16 port, params string[] txt)
203 AddService (-1, Protocol.Unspecified, flags, name, type, domain, null, port, txt);
206 public void AddService (int iface, Protocol proto, PublishFlags flags, string name, string type, string domain,
207 string host, UInt16 port, params string[] txt)
209 IntPtr list = avahi_string_list_new (IntPtr.Zero);
212 foreach (string item in txt) {
213 list = avahi_string_list_add (list, Utility.StringToBytes (item));
217 AddService (iface, proto, flags, name, type, domain, host, port, list);
220 public void AddService (int iface, Protocol proto, PublishFlags flags, string name, string type, string domain,
221 string host, UInt16 port, params byte[][] txt)
223 IntPtr list = avahi_string_list_new (IntPtr.Zero);
226 foreach (byte[] item in txt) {
227 list = avahi_string_list_add (list, item);
231 AddService (iface, proto, flags, name, type, domain, host, port, list);
234 private void AddService (int iface, Protocol proto, PublishFlags flags, string name, string type,
235 string domain, string host, UInt16 port, IntPtr list)
240 ret = avahi_entry_group_add_service_strlst (handle, iface, proto, flags,
241 Utility.StringToBytes (name),
242 Utility.StringToBytes (type),
243 Utility.StringToBytes (domain),
244 Utility.StringToBytes (host), port, list);
247 avahi_string_list_free (list);
250 client.ThrowError ();
254 public void UpdateService (string name, string type, string domain, params string[] txt)
256 UpdateService (-1, Protocol.Unspecified, PublishFlags.None, name, type, domain, txt);
259 public void UpdateService (int iface, Protocol proto, PublishFlags flags, string name, string type,
260 string domain, params string[] txt)
262 IntPtr list = avahi_string_list_new (IntPtr.Zero);
265 foreach (string item in txt) {
266 list = avahi_string_list_add (list, Utility.StringToBytes (item));
270 UpdateService (iface, proto, flags, name, type, domain, list);
273 public void UpdateService (int iface, Protocol proto, PublishFlags flags, string name, string type,
274 string domain, params byte[][] txt)
276 IntPtr list = avahi_string_list_new (IntPtr.Zero);
279 foreach (byte[] item in txt) {
280 list = avahi_string_list_add (list, item);
284 UpdateService (iface, proto, flags, name, type, domain, list);
287 private void UpdateService (int iface, Protocol proto, PublishFlags flags, string name, string type,
288 string domain, IntPtr list)
291 int ret = avahi_entry_group_update_service_strlst (handle, iface, proto, flags,
292 Utility.StringToBytes (name),
293 Utility.StringToBytes (type),
294 Utility.StringToBytes (domain),
297 avahi_string_list_free (list);
300 client.ThrowError ();
305 public void AddServiceSubtype (string name, string type, string domain, string subtype)
307 AddServiceSubtype (-1, Protocol.Unspecified, PublishFlags.None, name, type, domain, subtype);
310 public void AddServiceSubtype (int iface, Protocol proto, PublishFlags flags, string name,
311 string type, string domain, string subtype)
314 int ret = avahi_entry_group_add_service_subtype (handle, iface, proto, flags,
315 Utility.StringToBytes (name),
316 Utility.StringToBytes (type),
317 Utility.StringToBytes (domain),
318 Utility.StringToBytes (subtype));
321 client.ThrowError ();
326 public void AddAddress (string name, IPAddress address)
328 AddAddress (-1, Protocol.Unspecified, PublishFlags.None, name, address);
331 public void AddAddress (int iface, Protocol proto, PublishFlags flags, string name, IPAddress address)
333 IntPtr addressPtr = Utility.AddressToPtr (address);
336 int ret = avahi_entry_group_add_address (handle, iface, proto, flags,
337 Utility.StringToBytes (name), addressPtr);
339 Utility.Free (addressPtr);
342 client.ThrowError ();
347 public void AddRecord (string name, RecordClass clazz, RecordType type, uint ttl, byte[] rdata, int length)
349 AddRecord (-1, Protocol.Unspecified, PublishFlags.None, name, clazz, type, ttl, rdata, length);
352 public void AddRecord (int iface, Protocol proto, PublishFlags flags, string name,
353 RecordClass clazz, RecordType type, uint ttl, byte[] rdata, int length)
356 int ret = avahi_entry_group_add_record (handle, iface, proto, flags,
357 Utility.StringToBytes (name),
358 clazz, type, ttl, rdata, length);
361 client.ThrowError ();
366 public static string GetAlternativeServiceName (string name) {
367 return Utility.PtrToStringFree (avahi_alternative_service_name (Utility.StringToBytes (name)));
370 private void OnEntryGroupCallback (IntPtr group, EntryGroupState state, IntPtr userdata)
372 if (StateChanged != null)
373 StateChanged (this, new EntryGroupStateArgs (state));