]> git.meshlink.io Git - catta/blob - avahi-sharp/HostNameResolver.cs
integrate mono bindings into the build
[catta] / avahi-sharp / HostNameResolver.cs
1 /***
2   This file is part of avahi.
3
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.
8
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.
13
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
17   USA.
18 ***/
19
20 using System;
21 using System.Collections;
22 using System.Net;
23 using System.Runtime.InteropServices;
24 using Mono.Unix;
25
26 namespace Avahi
27 {
28
29     internal delegate void HostNameResolverCallback (IntPtr resolver, int iface, Protocol proto,
30                                                      ResolverEvent revent, IntPtr hostname, IntPtr address,
31                                                      IntPtr userdata);
32
33     public class HostNameResolver : IDisposable
34     {
35         private IntPtr handle;
36         private Client client;
37         private int iface;
38         private Protocol proto;
39         private string hostname;
40         private Protocol aproto;
41
42         private IPAddress currentAddress;
43         private string currentHost;
44
45         private ArrayList foundListeners = new ArrayList ();
46         private ArrayList timeoutListeners = new ArrayList ();
47         
48         [DllImport ("avahi-client")]
49         private static extern IntPtr avahi_host_name_resolver_new (IntPtr client, int iface, Protocol proto,
50                                                                    IntPtr hostname, Protocol aproto,
51                                                                    HostNameResolverCallback cb, IntPtr userdata);
52
53         [DllImport ("avahi-client")]
54         private static extern void avahi_host_name_resolver_free (IntPtr handle);
55
56         public event HostAddressHandler Found
57         {
58             add {
59                 foundListeners.Add (value);
60                 Start ();
61             }
62             remove {
63                 foundListeners.Remove (value);
64                 Stop (false);
65             }
66         }
67         
68         public event EventHandler Timeout
69         {
70             add {
71                 timeoutListeners.Add (value);
72                 Start ();
73             }
74             remove {
75                 timeoutListeners.Remove (value);
76                 Stop (false);
77             }
78         }
79
80         public IPAddress Address
81         {
82             get { return currentAddress; }
83         }
84
85         public string HostName
86         {
87             get { return currentHost; }
88         }
89
90         public HostNameResolver (Client client, string hostname) : this (client, -1, Protocol.Unspecified,
91                                                                          hostname, Protocol.Unspecified)
92         {
93         }
94
95         public HostNameResolver (Client client, int iface, Protocol proto, string hostname,
96                                  Protocol aproto)
97         {
98             this.client = client;
99             this.iface = iface;
100             this.proto = proto;
101             this.hostname = hostname;
102             this.aproto = aproto;
103         }
104
105         ~HostNameResolver ()
106         {
107             Dispose ();
108         }
109
110         public void Dispose ()
111         {
112             Stop (true);
113         }
114
115         private void Start ()
116         {
117             if (handle != IntPtr.Zero || (foundListeners.Count == 0 && timeoutListeners.Count == 0))
118                 return;
119
120             IntPtr hostPtr = Utility.StringToPtr (hostname);
121             handle = avahi_host_name_resolver_new (client.Handle, iface, proto, hostPtr, aproto,
122                                                    OnHostNameResolverCallback, IntPtr.Zero);
123             Utility.Free (hostPtr);
124         }
125
126         private void Stop (bool force)
127         {
128             if (handle != IntPtr.Zero && (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
129                 avahi_host_name_resolver_free (handle);
130                 handle = IntPtr.Zero;
131             }
132         }
133
134         private void OnHostNameResolverCallback (IntPtr resolver, int iface, Protocol proto,
135                                                  ResolverEvent revent, IntPtr hostname, IntPtr address,
136                                                  IntPtr userdata)
137         {
138             if (revent == ResolverEvent.Found) {
139                 currentAddress = Utility.PtrToAddress (address);
140                 currentHost = Utility.PtrToString (hostname);
141
142                 foreach (HostAddressHandler handler in foundListeners)
143                     handler (this, currentHost, currentAddress);
144             } else {
145                 currentAddress = null;
146                 currentHost = null;
147                 
148                 foreach (EventHandler handler in timeoutListeners)
149                     handler (this, new EventArgs ());
150             }
151         }
152     }
153 }