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
21 using System.Collections;
23 using System.Runtime.InteropServices;
30 internal delegate void ServiceResolverCallback (IntPtr resolver, int iface, Protocol proto,
31 ResolverEvent revent, IntPtr name, IntPtr type,
32 IntPtr domain, IntPtr host, IntPtr address,
33 UInt16 port, IntPtr txt, LookupResultFlags flags,
36 public class ServiceResolver : ResolverBase, IDisposable
38 private IntPtr handle;
39 private ServiceInfo currentInfo;
40 private Client client;
42 private Protocol proto;
45 private string domain;
46 private Protocol aproto;
47 private LookupFlags flags;
48 private ServiceResolverCallback cb;
50 private ArrayList foundListeners = new ArrayList ();
51 private ArrayList timeoutListeners = new ArrayList ();
53 [DllImport ("avahi-client")]
54 private static extern IntPtr avahi_service_resolver_new (IntPtr client, int iface, Protocol proto,
55 byte[] name, byte[] type, byte[] domain,
56 Protocol aproto, LookupFlags flags,
57 ServiceResolverCallback cb,
60 [DllImport ("avahi-common")]
61 private static extern IntPtr avahi_string_list_get_next (IntPtr list);
63 [DllImport ("avahi-common")]
64 private static extern IntPtr avahi_string_list_get_text (IntPtr list);
66 [DllImport ("avahi-common")]
67 private static extern int avahi_string_list_get_size (IntPtr list);
69 [DllImport ("avahi-client")]
70 private static extern void avahi_service_resolver_free (IntPtr handle);
72 public event ServiceInfoHandler Found
75 foundListeners.Add (value);
79 foundListeners.Remove (value);
84 public event EventHandler Timeout
87 timeoutListeners.Add (value);
91 timeoutListeners.Remove (value);
96 public ServiceInfo Service
98 get { return currentInfo; }
101 public ServiceResolver (Client client, string name, string type, string domain) : this (client, -1,
102 Protocol.Unspecified,
104 Protocol.Unspecified,
109 public ServiceResolver (Client client, ServiceInfo service) : this (client, service.NetworkInterface,
110 service.Protocol, service.Name,
111 service.ServiceType, service.Domain,
112 Protocol.Unspecified,
113 GetLookupFlags (service.Flags))
117 public ServiceResolver (Client client, int iface, Protocol proto, string name,
118 string type, string domain, Protocol aproto, LookupFlags flags)
120 this.client = client;
125 this.domain = domain;
126 this.aproto = aproto;
128 cb = OnServiceResolverCallback;
136 public void Dispose ()
141 private void Start ()
143 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
144 (foundListeners.Count == 0 && timeoutListeners.Count == 0))
148 handle = avahi_service_resolver_new (client.Handle, iface, proto,
149 Utility.StringToBytes (name), Utility.StringToBytes (type),
150 Utility.StringToBytes (domain), aproto, flags, cb, IntPtr.Zero);
152 if (handle == IntPtr.Zero)
153 client.ThrowError ();
157 private void Stop (bool force)
159 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
160 (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
163 avahi_service_resolver_free (handle);
164 handle = IntPtr.Zero;
169 private void OnServiceResolverCallback (IntPtr resolver, int iface, Protocol proto,
170 ResolverEvent revent, IntPtr name, IntPtr type,
171 IntPtr domain, IntPtr host, IntPtr address,
172 UInt16 port, IntPtr txt, LookupResultFlags flags,
176 info.NetworkInterface = iface;
177 info.Protocol = proto;
178 info.Domain = Utility.PtrToString (domain);
179 info.ServiceType = Utility.PtrToString (type);
180 info.Name = Utility.PtrToString (name);
181 info.HostName = Utility.PtrToString (host);
182 info.Address = Utility.PtrToAddress (address);
185 if (proto == Protocol.IPv6)
186 info.Address.ScopeId = iface;
188 ArrayList txtlist = new ArrayList ();
189 for (IntPtr l = txt; l != IntPtr.Zero; l = avahi_string_list_get_next (l)) {
190 IntPtr buf = avahi_string_list_get_text (l);
191 int len = avahi_string_list_get_size (l);
193 byte[] txtbuf = new byte[len];
194 Marshal.Copy (buf, txtbuf, 0, len);
195 txtlist.Add (txtbuf);
198 info.Text = (byte[][]) txtlist.ToArray (typeof (byte[]));
202 case ResolverEvent.Found:
205 foreach (ServiceInfoHandler handler in foundListeners)
206 handler (this, new ServiceInfoArgs (info));
208 case ResolverEvent.Failure:
209 EmitFailure (client.LastError);
214 private static LookupFlags GetLookupFlags (LookupResultFlags rflags) {
215 LookupFlags ret = LookupFlags.None;
217 if ((rflags & LookupResultFlags.Multicast) > 0)
218 ret |= LookupFlags.UseMulticast;
219 if ((rflags & LookupResultFlags.WideArea) > 0)
220 ret |= LookupFlags.UseWideArea;