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 AddressResolverCallback (IntPtr resolver, int iface, Protocol proto,
32 ResolverEvent revent, IntPtr address,
33 IntPtr hostname, LookupResultFlags flags, IntPtr userdata);
35 public delegate void HostAddressHandler (object o, HostAddressArgs args);
37 public class HostAddressArgs : EventArgs
40 private IPAddress address;
47 public IPAddress Address
49 get { return address; }
52 public HostAddressArgs (string host, IPAddress address)
55 this.address = address;
59 public class AddressResolver : ResolverBase, IDisposable
61 private IntPtr handle;
62 private Client client;
64 private Protocol proto;
65 private IPAddress address;
66 private LookupFlags flags;
67 private AddressResolverCallback cb;
69 private IPAddress currentAddress;
70 private string currentHost;
72 private ArrayList foundListeners = new ArrayList ();
73 private ArrayList timeoutListeners = new ArrayList ();
75 [DllImport ("avahi-client")]
76 private static extern IntPtr avahi_address_resolver_new (IntPtr client, int iface, Protocol proto,
77 IntPtr address, LookupFlags flags,
78 AddressResolverCallback cb,
81 [DllImport ("avahi-client")]
82 private static extern void avahi_address_resolver_free (IntPtr handle);
84 public event HostAddressHandler Found
87 foundListeners.Add (value);
91 foundListeners.Remove (value);
96 public event EventHandler Timeout
99 timeoutListeners.Add (value);
103 timeoutListeners.Remove (value);
108 public IPAddress Address
110 get { return currentAddress; }
113 public string HostName
115 get { return currentHost; }
118 public AddressResolver (Client client, IPAddress address) : this (client, -1, Protocol.Unspecified,
119 address, LookupFlags.None)
123 public AddressResolver (Client client, int iface, Protocol proto, IPAddress address, LookupFlags flags)
125 this.client = client;
128 this.address = address;
130 cb = OnAddressResolverCallback;
138 public void Dispose ()
143 private void Start ()
145 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
146 (foundListeners.Count == 0 && timeoutListeners.Count == 0))
149 IntPtr addrPtr = Utility.AddressToPtr (address);
152 handle = avahi_address_resolver_new (client.Handle, iface, proto, addrPtr, flags,
155 if (handle == IntPtr.Zero)
156 client.ThrowError ();
159 Utility.Free (addrPtr);
162 private void Stop (bool force)
164 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
165 (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
168 avahi_address_resolver_free (handle);
169 handle = IntPtr.Zero;
174 private void OnAddressResolverCallback (IntPtr resolver, int iface, Protocol proto,
175 ResolverEvent revent, IntPtr address,
176 IntPtr hostname, LookupResultFlags flags, IntPtr userdata)
179 case ResolverEvent.Found:
180 currentAddress = Utility.PtrToAddress (address);
181 currentHost = Utility.PtrToString (hostname);
183 foreach (HostAddressHandler handler in foundListeners)
184 handler (this, new HostAddressArgs (currentHost, currentAddress));
186 case ResolverEvent.Failure:
187 EmitFailure (client.LastError);