]> git.meshlink.io Git - catta/blob - avahi-sharp/AddressResolver.cs
Complete AVAHI_LOOKUP_NO_ADDRESS fix
[catta] / avahi-sharp / AddressResolver.cs
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5
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.
10
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.
15
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
19   USA.
20 ***/
21
22 using System;
23 using System.Collections;
24 using System.Net;
25 using System.Runtime.InteropServices;
26 using Mono.Unix;
27
28 namespace Avahi
29 {
30
31     internal delegate void AddressResolverCallback (IntPtr resolver, int iface, Protocol proto,
32                                                     ResolverEvent revent, Protocol aproto, IntPtr address,
33                                                     IntPtr hostname, LookupResultFlags flags, IntPtr userdata);
34
35     public delegate void HostAddressHandler (object o, string host, IPAddress address);
36     
37     public class AddressResolver : ResolverBase, IDisposable
38     {
39         private IntPtr handle;
40         private Client client;
41         private int iface;
42         private Protocol proto;
43         private IPAddress address;
44         private LookupFlags flags;
45         private AddressResolverCallback cb;
46
47         private IPAddress currentAddress;
48         private string currentHost;
49
50         private ArrayList foundListeners = new ArrayList ();
51         private ArrayList timeoutListeners = new ArrayList ();
52         
53         [DllImport ("avahi-client")]
54         private static extern IntPtr avahi_address_resolver_new (IntPtr client, int iface, Protocol proto,
55                                                                  IntPtr address, LookupFlags flags,
56                                                                  AddressResolverCallback cb,
57                                                                  IntPtr userdata);
58
59         [DllImport ("avahi-client")]
60         private static extern void avahi_address_resolver_free (IntPtr handle);
61
62         public event HostAddressHandler Found
63         {
64             add {
65                 foundListeners.Add (value);
66                 Start ();
67             }
68             remove {
69                 foundListeners.Remove (value);
70                 Stop (false);
71             }
72         }
73         
74         public event EventHandler Timeout
75         {
76             add {
77                 timeoutListeners.Add (value);
78                 Start ();
79             }
80             remove {
81                 timeoutListeners.Remove (value);
82                 Stop (false);
83             }
84         }
85
86         public IPAddress Address
87         {
88             get { return currentAddress; }
89         }
90
91         public string HostName
92         {
93             get { return currentHost; }
94         }
95
96         public AddressResolver (Client client, IPAddress address) : this (client, -1, Protocol.Unspecified,
97                                                                           address, LookupFlags.None)
98         {
99         }
100
101         public AddressResolver (Client client, int iface, Protocol proto, IPAddress address, LookupFlags flags)
102         {
103             this.client = client;
104             this.iface = iface;
105             this.proto = proto;
106             this.address = address;
107             this.flags = flags;
108             cb = OnAddressResolverCallback;
109         }
110
111         ~AddressResolver ()
112         {
113             Dispose ();
114         }
115
116         public void Dispose ()
117         {
118             Stop (true);
119         }
120
121         private void Start ()
122         {
123             if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
124                 (foundListeners.Count == 0 && timeoutListeners.Count == 0))
125                 return;
126
127             IntPtr addrPtr = Utility.StringToPtr (address.ToString ());
128
129             lock (client) {
130                 handle = avahi_address_resolver_new (client.Handle, iface, proto, addrPtr, flags,
131                                                      cb, IntPtr.Zero);
132             }
133             
134             Utility.Free (addrPtr);
135         }
136
137         private void Stop (bool force)
138         {
139             if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
140                 (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
141
142                 lock (client) {
143                     avahi_address_resolver_free (handle);
144                     handle = IntPtr.Zero;
145                 }
146             }
147         }
148
149         private void OnAddressResolverCallback (IntPtr resolver, int iface, Protocol proto,
150                                                 ResolverEvent revent, Protocol aproto, IntPtr address,
151                                                 IntPtr hostname, LookupResultFlags flags, IntPtr userdata)
152         {
153             switch (revent) {
154             case ResolverEvent.Found:
155                 currentAddress = Utility.PtrToAddress (address);
156                 currentHost = Utility.PtrToString (hostname);
157
158                 foreach (HostAddressHandler handler in foundListeners)
159                     handler (this, currentHost, currentAddress);
160                 break;
161             case ResolverEvent.Timeout:
162                 currentAddress = null;
163                 currentHost = null;
164                 
165                 foreach (EventHandler handler in timeoutListeners)
166                     handler (this, new EventArgs ());
167                 break;
168             default:
169                 EmitResolverEvent (revent);
170                 break;
171             }
172         }
173     }
174 }