2 This file is part of avahi.
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.
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.
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
22 using System.Threading;
23 using System.Collections;
24 using System.Runtime.InteropServices;
26 using Mono.Unix.Native;
28 using Stdlib = Mono.Unix.Native.Stdlib;
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;
53 private ErrorCode error;
55 public ClientState State
60 public ErrorCode Error
65 public ClientStateArgs (ClientState state, ErrorCode error)
72 public enum Protocol {
78 internal enum ServerState {
85 public enum ClientState {
86 Registering = ServerState.Registering,
87 Running = ServerState.Running,
88 Collision = ServerState.Collision,
94 public enum LookupFlags {
103 public enum LookupResultFlags {
113 public enum ClientFlags {
115 IgnoreUserConfig = 1,
119 public class Client : IDisposable
121 private IntPtr handle;
122 private ClientCallback cb;
123 private PollCallback pollcb;
124 private IntPtr spoll;
126 private Thread thread;
128 [DllImport ("avahi-client")]
129 private static extern IntPtr avahi_client_new (IntPtr poll, ClientFlags flags, ClientCallback handler,
130 IntPtr userData, out int error);
132 [DllImport ("avahi-client")]
133 private static extern void avahi_client_free (IntPtr handle);
135 [DllImport ("avahi-client")]
136 private static extern IntPtr avahi_client_get_version_string (IntPtr handle);
138 [DllImport ("avahi-client")]
139 private static extern IntPtr avahi_client_get_host_name (IntPtr handle);
141 [DllImport ("avahi-client")]
142 private static extern IntPtr avahi_client_get_domain_name (IntPtr handle);
144 [DllImport ("avahi-client")]
145 private static extern IntPtr avahi_client_get_host_name_fqdn (IntPtr handle);
147 [DllImport ("avahi-client")]
148 private static extern ClientState avahi_client_get_state (IntPtr handle);
150 [DllImport ("avahi-client")]
151 private static extern int avahi_client_errno (IntPtr handle);
153 [DllImport ("avahi-common")]
154 private static extern IntPtr avahi_simple_poll_new ();
156 [DllImport ("avahi-common")]
157 private static extern IntPtr avahi_simple_poll_get (IntPtr spoll);
159 [DllImport ("avahi-common")]
160 private static extern void avahi_simple_poll_free (IntPtr spoll);
162 [DllImport ("avahi-common")]
163 private static extern int avahi_simple_poll_loop (IntPtr spoll);
165 [DllImport ("avahi-common")]
166 private static extern void avahi_simple_poll_set_func (IntPtr spoll, PollCallback cb);
168 [DllImport ("avahi-common")]
169 private static extern void avahi_simple_poll_quit (IntPtr spoll);
171 [DllImport ("avahi-client")]
172 private static extern uint avahi_client_get_local_service_cookie (IntPtr client);
174 [DllImport ("avahi-common")]
175 private static extern int avahi_service_name_join (IntPtr buf, int len, byte[] name, byte[] type,
178 [DllImport ("avahi-common")]
179 private static extern int avahi_service_name_split (byte[] service, IntPtr name, int name_len,
180 IntPtr type, int type_len,
181 IntPtr domain, int domain_len);
185 private static extern int poll(IntPtr ufds, uint nfds, int timeout);
187 public event ClientStateHandler StateChanged;
189 internal IntPtr Handle
191 get { return handle; }
194 public string Version
198 return Utility.PtrToString (avahi_client_get_version_string (handle));
203 public string HostName
207 return Utility.PtrToString (avahi_client_get_host_name (handle));
212 public string DomainName
216 return Utility.PtrToString (avahi_client_get_domain_name (handle));
221 public string HostNameFqdn
225 return Utility.PtrToString (avahi_client_get_host_name_fqdn (handle));
230 public ClientState State
234 return (ClientState) avahi_client_get_state (handle);
239 public uint LocalServiceCookie
243 return avahi_client_get_local_service_cookie (handle);
248 internal ErrorCode LastError
252 return (ErrorCode) avahi_client_errno (handle);
257 public Client (ClientFlags flags)
259 spoll = avahi_simple_poll_new ();
261 pollcb = OnPollCallback;
262 avahi_simple_poll_set_func (spoll, pollcb);
263 IntPtr poll = avahi_simple_poll_get (spoll);
264 cb = OnClientCallback;
267 handle = avahi_client_new (poll, flags, cb, IntPtr.Zero, out error);
269 throw new ClientException (error);
271 thread = new Thread (PollLoop);
272 thread.IsBackground = true;
276 public Client () : this (ClientFlags.None) {
284 public void Dispose ()
286 if (handle != IntPtr.Zero) {
288 avahi_client_free (handle);
289 handle = IntPtr.Zero;
291 avahi_simple_poll_quit (spoll);
294 avahi_simple_poll_free (spoll);
299 public static string JoinServiceName (string name, string type, string domain)
301 int len = 4 * (name.Length + type.Length + domain.Length) + 4;
302 IntPtr buf = Stdlib.malloc ((ulong) len);
304 int ret = avahi_service_name_join (buf, len,
305 Utility.StringToBytes (name),
306 Utility.StringToBytes (type),
307 Utility.StringToBytes (domain));
311 return null; // FIXME, should throw exception
314 string service = Utility.PtrToString (buf);
320 public static void SplitServiceName (string service, out string name, out string type, out string domain)
324 IntPtr namePtr = Stdlib.malloc ((ulong) len);
325 IntPtr typePtr = Stdlib.malloc ((ulong) len);
326 IntPtr domainPtr = Stdlib.malloc ((ulong) len);
328 int ret = avahi_service_name_split (Utility.StringToBytes (service), namePtr, len, typePtr, len,
332 Utility.Free (namePtr);
333 Utility.Free (typePtr);
334 Utility.Free (domainPtr);
342 name = Utility.PtrToString (namePtr);
343 type = Utility.PtrToString (typePtr);
344 domain = Utility.PtrToString (domainPtr);
346 Utility.Free (namePtr);
347 Utility.Free (typePtr);
348 Utility.Free (domainPtr);
351 internal void ThrowError ()
353 ErrorCode error = LastError;
355 if (error != ErrorCode.Ok)
356 throw new ClientException (error);
359 private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
361 if (StateChanged != null)
362 StateChanged (this, new ClientStateArgs (state, LastError));
365 private int OnPollCallback (IntPtr ufds, uint nfds, int timeout) {
367 int result = poll (ufds, nfds, timeout);
368 Monitor.Enter (this);
372 private void PollLoop () {
375 avahi_simple_poll_loop (spoll);
376 Monitor.Pulse (this);
378 } catch (Exception e) {
379 Console.Error.WriteLine ("Error in avahi-sharp event loop: " + e);