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 AddressResolverCallback (IntPtr resolver, int iface, Protocol proto,
30 ResolverEvent revent, IntPtr address,
31 IntPtr hostname, LookupResultFlags flags, IntPtr userdata);
33 public delegate void HostAddressHandler (object o, HostAddressArgs args);
35 public class HostAddressArgs : EventArgs
38 private IPAddress address;
45 public IPAddress Address
47 get { return address; }
50 public HostAddressArgs (string host, IPAddress address)
53 this.address = address;
57 public class AddressResolver : ResolverBase, IDisposable
59 private IntPtr handle;
60 private Client client;
62 private Protocol proto;
63 private IPAddress address;
64 private LookupFlags flags;
65 private AddressResolverCallback cb;
67 private IPAddress currentAddress;
68 private string currentHost;
70 private ArrayList foundListeners = new ArrayList ();
71 private ArrayList timeoutListeners = new ArrayList ();
73 [DllImport ("avahi-client")]
74 private static extern IntPtr avahi_address_resolver_new (IntPtr client, int iface, Protocol proto,
75 IntPtr address, LookupFlags flags,
76 AddressResolverCallback cb,
79 [DllImport ("avahi-client")]
80 private static extern void avahi_address_resolver_free (IntPtr handle);
82 public event HostAddressHandler Found
85 foundListeners.Add (value);
89 foundListeners.Remove (value);
94 public event EventHandler Timeout
97 timeoutListeners.Add (value);
101 timeoutListeners.Remove (value);
106 public IPAddress Address
108 get { return currentAddress; }
111 public string HostName
113 get { return currentHost; }
116 public AddressResolver (Client client, IPAddress address) : this (client, -1, Protocol.Unspecified,
117 address, LookupFlags.None)
121 public AddressResolver (Client client, int iface, Protocol proto, IPAddress address, LookupFlags flags)
123 this.client = client;
126 this.address = address;
128 cb = OnAddressResolverCallback;
136 public void Dispose ()
141 private void Start ()
143 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
144 (foundListeners.Count == 0 && timeoutListeners.Count == 0))
147 IntPtr addrPtr = Utility.AddressToPtr (address);
150 handle = avahi_address_resolver_new (client.Handle, iface, proto, addrPtr, flags,
153 if (handle == IntPtr.Zero)
154 client.ThrowError ();
157 Utility.Free (addrPtr);
160 private void Stop (bool force)
162 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
163 (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
166 avahi_address_resolver_free (handle);
167 handle = IntPtr.Zero;
172 private void OnAddressResolverCallback (IntPtr resolver, int iface, Protocol proto,
173 ResolverEvent revent, IntPtr address,
174 IntPtr hostname, LookupResultFlags flags, IntPtr userdata)
177 case ResolverEvent.Found:
178 currentAddress = Utility.PtrToAddress (address);
179 currentHost = Utility.PtrToString (hostname);
181 foreach (HostAddressHandler handler in foundListeners)
182 handler (this, new HostAddressArgs (currentHost, currentAddress));
184 case ResolverEvent.Failure:
185 EmitFailure (client.LastError);