4 This file is part of avahi.
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.
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.
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
24 using System.Threading;
25 using System.Collections;
26 using System.Runtime.InteropServices;
28 using Mono.Unix.Native;
30 using Stdlib = Mono.Unix.Native.Stdlib;
34 internal enum ResolverEvent {
39 internal enum BrowserEvent {
47 internal delegate int PollCallback (IntPtr ufds, uint nfds, int timeout);
48 internal delegate void ClientCallback (IntPtr client, ClientState state, IntPtr userData);
50 public delegate void ClientStateHandler (object o, ClientStateArgs state);
52 public class ClientStateArgs : EventArgs
54 private ClientState state;
56 public ClientState State
61 public ClientStateArgs (ClientState state)
67 public enum Protocol {
73 internal enum ServerState {
80 public enum ClientState {
81 Registering = ServerState.Registering,
82 Running = ServerState.Running,
83 Collision = ServerState.Collision,
89 public enum LookupFlags {
98 public enum LookupResultFlags {
108 public enum ClientFlags {
110 IgnoreUserConfig = 1,
114 public class Client : IDisposable
116 private IntPtr handle;
117 private ClientCallback cb;
118 private PollCallback pollcb;
119 private IntPtr spoll;
121 private Thread thread;
123 [DllImport ("avahi-client")]
124 private static extern IntPtr avahi_client_new (IntPtr poll, ClientFlags flags, ClientCallback handler,
125 IntPtr userData, out int error);
127 [DllImport ("avahi-client")]
128 private static extern void avahi_client_free (IntPtr handle);
130 [DllImport ("avahi-client")]
131 private static extern IntPtr avahi_client_get_version_string (IntPtr handle);
133 [DllImport ("avahi-client")]
134 private static extern IntPtr avahi_client_get_host_name (IntPtr handle);
136 [DllImport ("avahi-client")]
137 private static extern IntPtr avahi_client_get_domain_name (IntPtr handle);
139 [DllImport ("avahi-client")]
140 private static extern IntPtr avahi_client_get_host_name_fqdn (IntPtr handle);
142 [DllImport ("avahi-client")]
143 private static extern ClientState avahi_client_get_state (IntPtr handle);
145 [DllImport ("avahi-client")]
146 private static extern int avahi_client_errno (IntPtr handle);
148 [DllImport ("avahi-common")]
149 private static extern IntPtr avahi_simple_poll_new ();
151 [DllImport ("avahi-common")]
152 private static extern IntPtr avahi_simple_poll_get (IntPtr spoll);
154 [DllImport ("avahi-common")]
155 private static extern void avahi_simple_poll_free (IntPtr spoll);
157 [DllImport ("avahi-common")]
158 private static extern int avahi_simple_poll_loop (IntPtr spoll);
160 [DllImport ("avahi-common")]
161 private static extern void avahi_simple_poll_set_func (IntPtr spoll, PollCallback cb);
163 [DllImport ("avahi-common")]
164 private static extern void avahi_simple_poll_quit (IntPtr spoll);
166 [DllImport ("avahi-client")]
167 private static extern uint avahi_client_get_local_service_cookie (IntPtr client);
169 [DllImport ("avahi-common")]
170 private static extern int avahi_service_name_join (IntPtr buf, int len, byte[] name, byte[] type,
173 [DllImport ("avahi-common")]
174 private static extern int avahi_service_name_split (byte[] service, IntPtr name, int name_len,
175 IntPtr type, int type_len,
176 IntPtr domain, int domain_len);
180 private static extern int poll(IntPtr ufds, uint nfds, int timeout);
182 public event ClientStateHandler StateChanged;
184 internal IntPtr Handle
186 get { return handle; }
189 public string Version
193 return Utility.PtrToString (avahi_client_get_version_string (handle));
198 public string HostName
202 return Utility.PtrToString (avahi_client_get_host_name (handle));
207 public string DomainName
211 return Utility.PtrToString (avahi_client_get_domain_name (handle));
216 public string HostNameFqdn
220 return Utility.PtrToString (avahi_client_get_host_name_fqdn (handle));
225 public ClientState State
229 return (ClientState) avahi_client_get_state (handle);
234 public uint LocalServiceCookie
238 return avahi_client_get_local_service_cookie (handle);
243 internal ErrorCode LastError
247 return (ErrorCode) avahi_client_errno (handle);
252 public Client (ClientFlags flags)
254 spoll = avahi_simple_poll_new ();
256 pollcb = OnPollCallback;
257 avahi_simple_poll_set_func (spoll, pollcb);
258 IntPtr poll = avahi_simple_poll_get (spoll);
259 cb = OnClientCallback;
262 handle = avahi_client_new (poll, flags, cb, IntPtr.Zero, out error);
264 throw new ClientException (error);
266 thread = new Thread (PollLoop);
267 thread.IsBackground = true;
271 public Client () : this (ClientFlags.None) {
279 public void Dispose ()
281 if (handle != IntPtr.Zero) {
282 avahi_client_free (handle);
283 avahi_simple_poll_quit (spoll);
284 avahi_simple_poll_free (spoll);
285 handle = IntPtr.Zero;
289 public static string JoinServiceName (string name, string type, string domain)
291 int len = 4 * (name.Length + type.Length + domain.Length) + 4;
292 IntPtr buf = Stdlib.malloc ((ulong) len);
294 int ret = avahi_service_name_join (buf, len,
295 Utility.StringToBytes (name),
296 Utility.StringToBytes (type),
297 Utility.StringToBytes (domain));
301 return null; // FIXME, should throw exception
304 string service = Utility.PtrToString (buf);
310 public static void SplitServiceName (string service, out string name, out string type, out string domain)
314 IntPtr namePtr = Stdlib.malloc ((ulong) len);
315 IntPtr typePtr = Stdlib.malloc ((ulong) len);
316 IntPtr domainPtr = Stdlib.malloc ((ulong) len);
318 int ret = avahi_service_name_split (Utility.StringToBytes (service), namePtr, len, typePtr, len,
322 Utility.Free (namePtr);
323 Utility.Free (typePtr);
324 Utility.Free (domainPtr);
332 name = Utility.PtrToString (namePtr);
333 type = Utility.PtrToString (typePtr);
334 domain = Utility.PtrToString (domainPtr);
336 Utility.Free (namePtr);
337 Utility.Free (typePtr);
338 Utility.Free (domainPtr);
341 internal void ThrowError ()
343 ErrorCode error = LastError;
345 if (error != ErrorCode.Ok)
346 throw new ClientException (error);
349 private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
351 if (StateChanged != null)
352 StateChanged (this, new ClientStateArgs (state));
355 private int OnPollCallback (IntPtr ufds, uint nfds, int timeout) {
357 int result = poll (ufds, nfds, timeout);
358 Monitor.Enter (this);
362 private void PollLoop () {
365 avahi_simple_poll_loop (spoll);
367 } catch (Exception e) {
368 Console.Error.WriteLine ("Error in avahi-sharp event loop: " + e);