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
23 using System.Collections;
25 using System.Runtime.InteropServices;
32 internal delegate void HostNameResolverCallback (IntPtr resolver, int iface, Protocol proto,
33 ResolverEvent revent, IntPtr hostname, IntPtr address,
34 LookupResultFlags flags, IntPtr userdata);
36 public class HostNameResolver : ResolverBase, IDisposable
38 private IntPtr handle;
39 private Client client;
41 private Protocol proto;
42 private string hostname;
43 private Protocol aproto;
44 private LookupFlags flags;
45 private HostNameResolverCallback cb;
47 private IPAddress currentAddress;
48 private string currentHost;
50 private ArrayList foundListeners = new ArrayList ();
51 private ArrayList timeoutListeners = new ArrayList ();
53 [DllImport ("avahi-client")]
54 private static extern IntPtr avahi_host_name_resolver_new (IntPtr client, int iface, Protocol proto,
55 byte[] hostname, Protocol aproto, LookupFlags flags,
56 HostNameResolverCallback cb, IntPtr userdata);
58 [DllImport ("avahi-client")]
59 private static extern void avahi_host_name_resolver_free (IntPtr handle);
61 public event HostAddressHandler Found
64 foundListeners.Add (value);
68 foundListeners.Remove (value);
73 public event EventHandler Timeout
76 timeoutListeners.Add (value);
80 timeoutListeners.Remove (value);
85 public IPAddress Address
87 get { return currentAddress; }
90 public string HostName
92 get { return currentHost; }
95 public HostNameResolver (Client client, string hostname) : this (client, -1, Protocol.Unspecified,
96 hostname, Protocol.Unspecified,
101 public HostNameResolver (Client client, int iface, Protocol proto, string hostname,
102 Protocol aproto, LookupFlags flags)
104 this.client = client;
107 this.hostname = hostname;
108 this.aproto = aproto;
110 cb = OnHostNameResolverCallback;
118 public void Dispose ()
123 private void Start ()
125 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
126 (foundListeners.Count == 0 && timeoutListeners.Count == 0))
130 handle = avahi_host_name_resolver_new (client.Handle, iface, proto,
131 Utility.StringToBytes (hostname), aproto, flags,
134 if (handle == IntPtr.Zero)
135 client.ThrowError ();
139 private void Stop (bool force)
141 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
142 (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
145 avahi_host_name_resolver_free (handle);
146 handle = IntPtr.Zero;
151 private void OnHostNameResolverCallback (IntPtr resolver, int iface, Protocol proto,
152 ResolverEvent revent, IntPtr hostname, IntPtr address,
153 LookupResultFlags flags, IntPtr userdata)
156 case ResolverEvent.Found:
157 currentAddress = Utility.PtrToAddress (address);
158 currentHost = Utility.PtrToString (hostname);
160 foreach (HostAddressHandler handler in foundListeners)
161 handler (this, new HostAddressArgs (currentHost, currentAddress));
163 case ResolverEvent.Failure:
164 EmitFailure (client.LastError);