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;
32 internal enum ResolverEvent {
37 internal enum BrowserEvent {
45 internal delegate int PollCallback (IntPtr ufds, uint nfds, int timeout);
46 internal delegate void ClientCallback (IntPtr client, ClientState state, IntPtr userData);
48 public delegate void ClientStateHandler (object o, ClientStateArgs state);
50 public class ClientStateArgs : EventArgs
52 private ClientState state;
54 public ClientState State
59 public ClientStateArgs (ClientState state)
65 public enum Protocol {
71 internal enum ServerState {
78 public enum ClientState {
79 Registering = ServerState.Registering,
80 Running = ServerState.Running,
81 Collision = ServerState.Collision,
87 public enum LookupFlags {
96 public enum LookupResultFlags {
106 public enum ClientFlags {
108 IgnoreUserConfig = 1,
112 public class Client : IDisposable
114 private IntPtr handle;
115 private ClientCallback cb;
116 private PollCallback pollcb;
117 private IntPtr spoll;
119 private Thread thread;
121 [DllImport ("avahi-client")]
122 private static extern IntPtr avahi_client_new (IntPtr poll, ClientFlags flags, ClientCallback handler,
123 IntPtr userData, out int error);
125 [DllImport ("avahi-client")]
126 private static extern void avahi_client_free (IntPtr handle);
128 [DllImport ("avahi-client")]
129 private static extern IntPtr avahi_client_get_version_string (IntPtr handle);
131 [DllImport ("avahi-client")]
132 private static extern IntPtr avahi_client_get_host_name (IntPtr handle);
134 [DllImport ("avahi-client")]
135 private static extern IntPtr avahi_client_get_domain_name (IntPtr handle);
137 [DllImport ("avahi-client")]
138 private static extern IntPtr avahi_client_get_host_name_fqdn (IntPtr handle);
140 [DllImport ("avahi-client")]
141 private static extern ClientState avahi_client_get_state (IntPtr handle);
143 [DllImport ("avahi-client")]
144 private static extern int avahi_client_errno (IntPtr handle);
146 [DllImport ("avahi-common")]
147 private static extern IntPtr avahi_simple_poll_new ();
149 [DllImport ("avahi-common")]
150 private static extern IntPtr avahi_simple_poll_get (IntPtr spoll);
152 [DllImport ("avahi-common")]
153 private static extern void avahi_simple_poll_free (IntPtr spoll);
155 [DllImport ("avahi-common")]
156 private static extern int avahi_simple_poll_loop (IntPtr spoll);
158 [DllImport ("avahi-common")]
159 private static extern void avahi_simple_poll_set_func (IntPtr spoll, PollCallback cb);
161 [DllImport ("avahi-common")]
162 private static extern void avahi_simple_poll_quit (IntPtr spoll);
164 [DllImport ("avahi-client")]
165 private static extern uint avahi_client_get_local_service_cookie (IntPtr client);
167 [DllImport ("avahi-common")]
168 private static extern int avahi_service_name_join (IntPtr buf, int len, byte[] name, byte[] type,
171 [DllImport ("avahi-common")]
172 private static extern int avahi_service_name_split (byte[] service, IntPtr name, int name_len,
173 IntPtr type, int type_len,
174 IntPtr domain, int domain_len);
178 private static extern int poll(IntPtr ufds, uint nfds, int timeout);
180 public event ClientStateHandler StateChanged;
182 internal IntPtr Handle
184 get { return handle; }
187 public string Version
191 return Utility.PtrToString (avahi_client_get_version_string (handle));
196 public string HostName
200 return Utility.PtrToString (avahi_client_get_host_name (handle));
205 public string DomainName
209 return Utility.PtrToString (avahi_client_get_domain_name (handle));
214 public string HostNameFqdn
218 return Utility.PtrToString (avahi_client_get_host_name_fqdn (handle));
223 public ClientState State
227 return (ClientState) avahi_client_get_state (handle);
232 public uint LocalServiceCookie
236 return avahi_client_get_local_service_cookie (handle);
241 internal ErrorCode LastError
245 return (ErrorCode) avahi_client_errno (handle);
250 public Client (ClientFlags flags)
252 spoll = avahi_simple_poll_new ();
254 pollcb = OnPollCallback;
255 avahi_simple_poll_set_func (spoll, pollcb);
256 IntPtr poll = avahi_simple_poll_get (spoll);
257 cb = OnClientCallback;
260 handle = avahi_client_new (poll, flags, cb, IntPtr.Zero, out error);
262 throw new ClientException (error);
264 thread = new Thread (PollLoop);
265 thread.IsBackground = true;
269 public Client () : this (ClientFlags.None) {
277 public void Dispose ()
279 if (handle != IntPtr.Zero) {
280 avahi_client_free (handle);
281 avahi_simple_poll_quit (spoll);
282 avahi_simple_poll_free (spoll);
283 handle = IntPtr.Zero;
287 public static string JoinServiceName (string name, string type, string domain)
289 int len = 4 * (name.Length + type.Length + domain.Length) + 4;
290 IntPtr buf = Stdlib.malloc ((ulong) len);
292 int ret = avahi_service_name_join (buf, len,
293 Utility.StringToBytes (name),
294 Utility.StringToBytes (type),
295 Utility.StringToBytes (domain));
299 return null; // FIXME, should throw exception
302 string service = Utility.PtrToString (buf);
308 public static void SplitServiceName (string service, out string name, out string type, out string domain)
312 IntPtr namePtr = Stdlib.malloc ((ulong) len);
313 IntPtr typePtr = Stdlib.malloc ((ulong) len);
314 IntPtr domainPtr = Stdlib.malloc ((ulong) len);
316 int ret = avahi_service_name_split (Utility.StringToBytes (service), namePtr, len, typePtr, len,
320 Utility.Free (namePtr);
321 Utility.Free (typePtr);
322 Utility.Free (domainPtr);
330 name = Utility.PtrToString (namePtr);
331 type = Utility.PtrToString (typePtr);
332 domain = Utility.PtrToString (domainPtr);
334 Utility.Free (namePtr);
335 Utility.Free (typePtr);
336 Utility.Free (domainPtr);
339 internal void ThrowError ()
341 ErrorCode error = LastError;
343 if (error != ErrorCode.Ok)
344 throw new ClientException (error);
347 private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
349 if (StateChanged != null)
350 StateChanged (this, new ClientStateArgs (state));
353 private int OnPollCallback (IntPtr ufds, uint nfds, int timeout) {
355 int result = poll (ufds, nfds, timeout);
356 Monitor.Enter (this);
360 private void PollLoop () {
363 avahi_simple_poll_loop (spoll);
365 } catch (Exception e) {
366 Console.Error.WriteLine ("Error in avahi-sharp event loop: " + e);