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
25 using System.Runtime.InteropServices;
31 internal class Utility
34 private static extern int strlen (IntPtr ptr);
36 [DllImport ("avahi-common")]
37 private static extern IntPtr avahi_address_snprint (IntPtr buf, int size, IntPtr address);
39 public static string PtrToString (IntPtr ptr)
41 if (ptr == IntPtr.Zero)
44 int len = strlen (ptr);
45 byte[] bytes = new byte[len];
46 Marshal.Copy (ptr, bytes, 0, len);
47 return Encoding.UTF8.GetString (bytes);
50 public static string PtrToStringFree (IntPtr ptr)
52 if (ptr == IntPtr.Zero)
55 string ret = PtrToString (ptr);
60 public static IntPtr StringToPtr (string str)
65 byte[] bytes = Encoding.UTF8.GetBytes (str);
66 IntPtr buf = Stdlib.malloc ((uint) bytes.Length + 1);
67 Marshal.Copy (bytes, 0, buf, bytes.Length);
68 Marshal.WriteByte (buf, bytes.Length, 0);
72 public static void Free (IntPtr ptr)
77 public static IPAddress PtrToAddress (IntPtr ptr)
79 IPAddress address = null;
81 if (ptr != IntPtr.Zero) {
82 IntPtr buf = Stdlib.malloc (256);
83 IntPtr addrPtr = avahi_address_snprint (buf, 256, ptr);
84 address = IPAddress.Parse (Utility.PtrToString (addrPtr));
85 Utility.Free (addrPtr);