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