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;
31 internal delegate void HostNameResolverCallback (IntPtr resolver, int iface, Protocol proto,
32 ResolverEvent revent, IntPtr hostname, IntPtr address,
35 public class HostNameResolver : IDisposable
37 private IntPtr handle;
38 private Client client;
40 private Protocol proto;
41 private string hostname;
42 private Protocol aproto;
44 private IPAddress currentAddress;
45 private string currentHost;
47 private ArrayList foundListeners = new ArrayList ();
48 private ArrayList timeoutListeners = new ArrayList ();
50 [DllImport ("avahi-client")]
51 private static extern IntPtr avahi_host_name_resolver_new (IntPtr client, int iface, Protocol proto,
52 IntPtr hostname, Protocol aproto,
53 HostNameResolverCallback cb, IntPtr userdata);
55 [DllImport ("avahi-client")]
56 private static extern void avahi_host_name_resolver_free (IntPtr handle);
58 public event HostAddressHandler Found
61 foundListeners.Add (value);
65 foundListeners.Remove (value);
70 public event EventHandler Timeout
73 timeoutListeners.Add (value);
77 timeoutListeners.Remove (value);
82 public IPAddress Address
84 get { return currentAddress; }
87 public string HostName
89 get { return currentHost; }
92 public HostNameResolver (Client client, string hostname) : this (client, -1, Protocol.Unspecified,
93 hostname, Protocol.Unspecified)
97 public HostNameResolver (Client client, int iface, Protocol proto, string hostname,
100 this.client = client;
103 this.hostname = hostname;
104 this.aproto = aproto;
112 public void Dispose ()
117 private void Start ()
119 if (handle != IntPtr.Zero || (foundListeners.Count == 0 && timeoutListeners.Count == 0))
122 IntPtr hostPtr = Utility.StringToPtr (hostname);
123 handle = avahi_host_name_resolver_new (client.Handle, iface, proto, hostPtr, aproto,
124 OnHostNameResolverCallback, IntPtr.Zero);
125 Utility.Free (hostPtr);
128 private void Stop (bool force)
130 if (handle != IntPtr.Zero && (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
131 avahi_host_name_resolver_free (handle);
132 handle = IntPtr.Zero;
136 private void OnHostNameResolverCallback (IntPtr resolver, int iface, Protocol proto,
137 ResolverEvent revent, IntPtr hostname, IntPtr address,
140 if (revent == ResolverEvent.Found) {
141 currentAddress = Utility.PtrToAddress (address);
142 currentHost = Utility.PtrToString (hostname);
144 foreach (HostAddressHandler handler in foundListeners)
145 handler (this, currentHost, currentAddress);
147 currentAddress = null;
150 foreach (EventHandler handler in timeoutListeners)
151 handler (this, new EventArgs ());