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;
29 internal delegate void HostNameResolverCallback (IntPtr resolver, int iface, Protocol proto,
30 ResolverEvent revent, IntPtr hostname, IntPtr address,
33 public class HostNameResolver : IDisposable
35 private IntPtr handle;
36 private Client client;
38 private Protocol proto;
39 private string hostname;
40 private Protocol aproto;
42 private IPAddress currentAddress;
43 private string currentHost;
45 private ArrayList foundListeners = new ArrayList ();
46 private ArrayList timeoutListeners = new ArrayList ();
48 [DllImport ("avahi-client")]
49 private static extern IntPtr avahi_host_name_resolver_new (IntPtr client, int iface, Protocol proto,
50 IntPtr hostname, Protocol aproto,
51 HostNameResolverCallback cb, IntPtr userdata);
53 [DllImport ("avahi-client")]
54 private static extern void avahi_host_name_resolver_free (IntPtr handle);
56 public event HostAddressHandler Found
59 foundListeners.Add (value);
63 foundListeners.Remove (value);
68 public event EventHandler Timeout
71 timeoutListeners.Add (value);
75 timeoutListeners.Remove (value);
80 public IPAddress Address
82 get { return currentAddress; }
85 public string HostName
87 get { return currentHost; }
90 public HostNameResolver (Client client, string hostname) : this (client, -1, Protocol.Unspecified,
91 hostname, Protocol.Unspecified)
95 public HostNameResolver (Client client, int iface, Protocol proto, string hostname,
101 this.hostname = hostname;
102 this.aproto = aproto;
110 public void Dispose ()
115 private void Start ()
117 if (handle != IntPtr.Zero || (foundListeners.Count == 0 && timeoutListeners.Count == 0))
120 IntPtr hostPtr = Utility.StringToPtr (hostname);
121 handle = avahi_host_name_resolver_new (client.Handle, iface, proto, hostPtr, aproto,
122 OnHostNameResolverCallback, IntPtr.Zero);
123 Utility.Free (hostPtr);
126 private void Stop (bool force)
128 if (handle != IntPtr.Zero && (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
129 avahi_host_name_resolver_free (handle);
130 handle = IntPtr.Zero;
134 private void OnHostNameResolverCallback (IntPtr resolver, int iface, Protocol proto,
135 ResolverEvent revent, IntPtr hostname, IntPtr address,
138 if (revent == ResolverEvent.Found) {
139 currentAddress = Utility.PtrToAddress (address);
140 currentHost = Utility.PtrToString (hostname);
142 foreach (HostAddressHandler handler in foundListeners)
143 handler (this, currentHost, currentAddress);
145 currentAddress = null;
148 foreach (EventHandler handler in timeoutListeners)
149 handler (this, new EventArgs ());