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,
33 LookupResultFlags flags, IntPtr userdata);
35 public class HostNameResolver : ResolverBase, IDisposable
37 private IntPtr handle;
38 private Client client;
40 private Protocol proto;
41 private string hostname;
42 private Protocol aproto;
43 private LookupFlags flags;
44 private HostNameResolverCallback cb;
46 private IPAddress currentAddress;
47 private string currentHost;
49 private ArrayList foundListeners = new ArrayList ();
50 private ArrayList timeoutListeners = new ArrayList ();
52 [DllImport ("avahi-client")]
53 private static extern IntPtr avahi_host_name_resolver_new (IntPtr client, int iface, Protocol proto,
54 IntPtr hostname, Protocol aproto, LookupFlags flags,
55 HostNameResolverCallback cb, IntPtr userdata);
57 [DllImport ("avahi-client")]
58 private static extern void avahi_host_name_resolver_free (IntPtr handle);
60 public event HostAddressHandler Found
63 foundListeners.Add (value);
67 foundListeners.Remove (value);
72 public event EventHandler Timeout
75 timeoutListeners.Add (value);
79 timeoutListeners.Remove (value);
84 public IPAddress Address
86 get { return currentAddress; }
89 public string HostName
91 get { return currentHost; }
94 public HostNameResolver (Client client, string hostname) : this (client, -1, Protocol.Unspecified,
95 hostname, Protocol.Unspecified,
100 public HostNameResolver (Client client, int iface, Protocol proto, string hostname,
101 Protocol aproto, LookupFlags flags)
103 this.client = client;
106 this.hostname = hostname;
107 this.aproto = aproto;
109 cb = OnHostNameResolverCallback;
117 public void Dispose ()
122 private void Start ()
124 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
125 (foundListeners.Count == 0 && timeoutListeners.Count == 0))
128 IntPtr hostPtr = Utility.StringToPtr (hostname);
131 handle = avahi_host_name_resolver_new (client.Handle, iface, proto, hostPtr, aproto, flags,
135 Utility.Free (hostPtr);
138 private void Stop (bool force)
140 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
141 (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
144 avahi_host_name_resolver_free (handle);
145 handle = IntPtr.Zero;
150 private void OnHostNameResolverCallback (IntPtr resolver, int iface, Protocol proto,
151 ResolverEvent revent, IntPtr hostname, IntPtr address,
152 LookupResultFlags flags, IntPtr userdata)
155 case ResolverEvent.Found:
156 currentAddress = Utility.PtrToAddress (address);
157 currentHost = Utility.PtrToString (hostname);
159 foreach (HostAddressHandler handler in foundListeners)
160 handler (this, currentHost, currentAddress);
162 case ResolverEvent.Timeout:
163 currentAddress = null;
166 foreach (EventHandler handler in timeoutListeners)
167 handler (this, new EventArgs ());
170 EmitResolverEvent (revent);