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;
55 private ErrorCode error;
57 public ClientState State
62 public ErrorCode Error
67 public ClientStateArgs (ClientState state, ErrorCode error)
74 public enum Protocol {
80 internal enum ServerState {
87 public enum ClientState {
88 Registering = ServerState.Registering,
89 Running = ServerState.Running,
90 Collision = ServerState.Collision,
96 public enum LookupFlags {
105 public enum LookupResultFlags {
115 public enum ClientFlags {
117 IgnoreUserConfig = 1,
121 public class Client : IDisposable
123 private IntPtr handle;
124 private ClientCallback cb;
125 private PollCallback pollcb;
126 private IntPtr spoll;
128 private Thread thread;
130 [DllImport ("avahi-client")]
131 private static extern IntPtr avahi_client_new (IntPtr poll, ClientFlags flags, ClientCallback handler,
132 IntPtr userData, out int error);
134 [DllImport ("avahi-client")]
135 private static extern void avahi_client_free (IntPtr handle);
137 [DllImport ("avahi-client")]
138 private static extern IntPtr avahi_client_get_version_string (IntPtr handle);
140 [DllImport ("avahi-client")]
141 private static extern IntPtr avahi_client_get_host_name (IntPtr handle);
143 [DllImport ("avahi-client")]
144 private static extern IntPtr avahi_client_get_domain_name (IntPtr handle);
146 [DllImport ("avahi-client")]
147 private static extern IntPtr avahi_client_get_host_name_fqdn (IntPtr handle);
149 [DllImport ("avahi-client")]
150 private static extern ClientState avahi_client_get_state (IntPtr handle);
152 [DllImport ("avahi-client")]
153 private static extern int avahi_client_errno (IntPtr handle);
155 [DllImport ("avahi-common")]
156 private static extern IntPtr avahi_simple_poll_new ();
158 [DllImport ("avahi-common")]
159 private static extern IntPtr avahi_simple_poll_get (IntPtr spoll);
161 [DllImport ("avahi-common")]
162 private static extern void avahi_simple_poll_free (IntPtr spoll);
164 [DllImport ("avahi-common")]
165 private static extern int avahi_simple_poll_loop (IntPtr spoll);
167 [DllImport ("avahi-common")]
168 private static extern void avahi_simple_poll_set_func (IntPtr spoll, PollCallback cb);
170 [DllImport ("avahi-common")]
171 private static extern void avahi_simple_poll_quit (IntPtr spoll);
173 [DllImport ("avahi-client")]
174 private static extern uint avahi_client_get_local_service_cookie (IntPtr client);
176 [DllImport ("avahi-common")]
177 private static extern int avahi_service_name_join (IntPtr buf, int len, byte[] name, byte[] type,
180 [DllImport ("avahi-common")]
181 private static extern int avahi_service_name_split (byte[] service, IntPtr name, int name_len,
182 IntPtr type, int type_len,
183 IntPtr domain, int domain_len);
187 private static extern int poll(IntPtr ufds, uint nfds, int timeout);
189 public event ClientStateHandler StateChanged;
191 internal IntPtr Handle
193 get { return handle; }
196 public string Version
200 return Utility.PtrToString (avahi_client_get_version_string (handle));
205 public string HostName
209 return Utility.PtrToString (avahi_client_get_host_name (handle));
214 public string DomainName
218 return Utility.PtrToString (avahi_client_get_domain_name (handle));
223 public string HostNameFqdn
227 return Utility.PtrToString (avahi_client_get_host_name_fqdn (handle));
232 public ClientState State
236 return (ClientState) avahi_client_get_state (handle);
241 public uint LocalServiceCookie
245 return avahi_client_get_local_service_cookie (handle);
250 internal ErrorCode LastError
254 return (ErrorCode) avahi_client_errno (handle);
259 public Client (ClientFlags flags)
261 spoll = avahi_simple_poll_new ();
263 pollcb = OnPollCallback;
264 avahi_simple_poll_set_func (spoll, pollcb);
265 IntPtr poll = avahi_simple_poll_get (spoll);
266 cb = OnClientCallback;
269 handle = avahi_client_new (poll, flags, cb, IntPtr.Zero, out error);
271 throw new ClientException (error);
273 thread = new Thread (PollLoop);
274 thread.IsBackground = true;
278 public Client () : this (ClientFlags.None) {
286 public void Dispose ()
288 if (handle != IntPtr.Zero) {
290 avahi_client_free (handle);
291 handle = IntPtr.Zero;
293 avahi_simple_poll_quit (spoll);
296 avahi_simple_poll_free (spoll);
301 public static string JoinServiceName (string name, string type, string domain)
303 int len = 4 * (name.Length + type.Length + domain.Length) + 4;
304 IntPtr buf = Stdlib.malloc ((ulong) len);
306 int ret = avahi_service_name_join (buf, len,
307 Utility.StringToBytes (name),
308 Utility.StringToBytes (type),
309 Utility.StringToBytes (domain));
313 return null; // FIXME, should throw exception
316 string service = Utility.PtrToString (buf);
322 public static void SplitServiceName (string service, out string name, out string type, out string domain)
326 IntPtr namePtr = Stdlib.malloc ((ulong) len);
327 IntPtr typePtr = Stdlib.malloc ((ulong) len);
328 IntPtr domainPtr = Stdlib.malloc ((ulong) len);
330 int ret = avahi_service_name_split (Utility.StringToBytes (service), namePtr, len, typePtr, len,
334 Utility.Free (namePtr);
335 Utility.Free (typePtr);
336 Utility.Free (domainPtr);
344 name = Utility.PtrToString (namePtr);
345 type = Utility.PtrToString (typePtr);
346 domain = Utility.PtrToString (domainPtr);
348 Utility.Free (namePtr);
349 Utility.Free (typePtr);
350 Utility.Free (domainPtr);
353 internal void ThrowError ()
355 ErrorCode error = LastError;
357 if (error != ErrorCode.Ok)
358 throw new ClientException (error);
361 private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
363 if (StateChanged != null)
364 StateChanged (this, new ClientStateArgs (state, LastError));
367 private int OnPollCallback (IntPtr ufds, uint nfds, int timeout) {
369 int result = poll (ufds, nfds, timeout);
370 Monitor.Enter (this);
374 private void PollLoop () {
377 avahi_simple_poll_loop (spoll);
378 Monitor.Pulse (this);
380 } catch (Exception e) {
381 Console.Error.WriteLine ("Error in avahi-sharp event loop: " + e);