]> git.meshlink.io Git - catta/blob - avahi-sharp/HostNameResolver.cs
* update to the latest avahi-client API (LookupFlags)
[catta] / avahi-sharp / HostNameResolver.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 HostNameResolverCallback (IntPtr resolver, int iface, Protocol proto,
32                                                      ResolverEvent revent, IntPtr hostname, IntPtr address,
33                                                      LookupResultFlags flags, IntPtr userdata);
34
35     public class HostNameResolver : IDisposable
36     {
37         private IntPtr handle;
38         private Client client;
39         private int iface;
40         private Protocol proto;
41         private string hostname;
42         private Protocol aproto;
43         private LookupFlags flags;
44         private HostNameResolverCallback cb;
45
46         private IPAddress currentAddress;
47         private string currentHost;
48
49         private ArrayList foundListeners = new ArrayList ();
50         private ArrayList timeoutListeners = new ArrayList ();
51         
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);
56
57         [DllImport ("avahi-client")]
58         private static extern void avahi_host_name_resolver_free (IntPtr handle);
59
60         public event HostAddressHandler Found
61         {
62             add {
63                 foundListeners.Add (value);
64                 Start ();
65             }
66             remove {
67                 foundListeners.Remove (value);
68                 Stop (false);
69             }
70         }
71         
72         public event EventHandler Timeout
73         {
74             add {
75                 timeoutListeners.Add (value);
76                 Start ();
77             }
78             remove {
79                 timeoutListeners.Remove (value);
80                 Stop (false);
81             }
82         }
83
84         public IPAddress Address
85         {
86             get { return currentAddress; }
87         }
88
89         public string HostName
90         {
91             get { return currentHost; }
92         }
93
94         public HostNameResolver (Client client, string hostname) : this (client, -1, Protocol.Unspecified,
95                                                                          hostname, Protocol.Unspecified,
96                                                                          LookupFlags.None)
97         {
98         }
99
100         public HostNameResolver (Client client, int iface, Protocol proto, string hostname,
101                                  Protocol aproto, LookupFlags flags)
102         {
103             this.client = client;
104             this.iface = iface;
105             this.proto = proto;
106             this.hostname = hostname;
107             this.aproto = aproto;
108             this.flags = flags;
109             cb = OnHostNameResolverCallback;
110         }
111
112         ~HostNameResolver ()
113         {
114             Dispose ();
115         }
116
117         public void Dispose ()
118         {
119             Stop (true);
120         }
121
122         private void Start ()
123         {
124             if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
125                 (foundListeners.Count == 0 && timeoutListeners.Count == 0))
126                 return;
127
128             IntPtr hostPtr = Utility.StringToPtr (hostname);
129
130             lock (client) {
131                 handle = avahi_host_name_resolver_new (client.Handle, iface, proto, hostPtr, aproto, flags,
132                                                        cb, IntPtr.Zero);
133             }
134             
135             Utility.Free (hostPtr);
136         }
137
138         private void Stop (bool force)
139         {
140             if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
141                 (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
142
143                 lock (client) {
144                     avahi_host_name_resolver_free (handle);
145                     handle = IntPtr.Zero;
146                 }
147             }
148         }
149
150         private void OnHostNameResolverCallback (IntPtr resolver, int iface, Protocol proto,
151                                                  ResolverEvent revent, IntPtr hostname, IntPtr address,
152                                                  LookupResultFlags flags, IntPtr userdata)
153         {
154             if (revent == 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             } else {
161                 currentAddress = null;
162                 currentHost = null;
163                 
164                 foreach (EventHandler handler in timeoutListeners)
165                     handler (this, new EventArgs ());
166             }
167         }
168     }
169 }