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