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