1 /*** 2 This file is part of avahi. 3 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. 8 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. 13 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 17 USA. 18 ***/ 19 20 using System; 21 using System.Net; 22 using System.Text; 23 using System.Runtime.InteropServices; 24 using Mono.Unix; 25 using Mono.Unix.Native; 26 27 using Stdlib = Mono.Unix.Native.Stdlib; 28 29 namespace Avahi 30 { 31 internal class Utility 32 { 33 [DllImport ("libc")] strlen(IntPtr ptr)34 private static extern int strlen (IntPtr ptr); 35 36 [DllImport ("avahi-common")] avahi_address_snprint(IntPtr buf, int size, IntPtr address)37 private static extern IntPtr avahi_address_snprint (IntPtr buf, int size, IntPtr address); 38 39 [DllImport ("avahi-common")] avahi_address_parse(IntPtr str, Protocol proto, IntPtr ret)40 private static extern IntPtr avahi_address_parse (IntPtr str, Protocol proto, IntPtr ret); 41 PtrToString(IntPtr ptr)42 public static string PtrToString (IntPtr ptr) 43 { 44 if (ptr == IntPtr.Zero) 45 return null; 46 47 int len = strlen (ptr); 48 byte[] bytes = new byte[len]; 49 Marshal.Copy (ptr, bytes, 0, len); 50 return Encoding.UTF8.GetString (bytes); 51 } 52 PtrToStringFree(IntPtr ptr)53 public static string PtrToStringFree (IntPtr ptr) 54 { 55 if (ptr == IntPtr.Zero) 56 return null; 57 58 string ret = PtrToString (ptr); 59 Free (ptr); 60 return ret; 61 } 62 StringToBytes(string str)63 public static byte[] StringToBytes (string str) 64 { 65 if (str == null) 66 return null; 67 68 return Encoding.UTF8.GetBytes (str + "\0"); // lame. 69 } 70 StringToPtr(string str)71 private static IntPtr StringToPtr (string str) 72 { 73 if (str == null) 74 return IntPtr.Zero; 75 76 byte[] bytes = Utility.StringToBytes (str); 77 IntPtr buf = Stdlib.malloc ((uint) bytes.Length + 1); 78 Marshal.Copy (bytes, 0, buf, bytes.Length); 79 Marshal.WriteByte (buf, bytes.Length, 0); 80 return buf; 81 } 82 Free(IntPtr ptr)83 public static void Free (IntPtr ptr) 84 { 85 Stdlib.free (ptr); 86 } 87 AddressToPtr(IPAddress address)88 public static IntPtr AddressToPtr (IPAddress address) 89 { 90 IntPtr straddr = Utility.StringToPtr (address.ToString ()); 91 IntPtr addrPtr = Stdlib.malloc (32); 92 avahi_address_parse (straddr, Protocol.Unspecified, addrPtr); 93 Utility.Free (straddr); 94 95 return addrPtr; 96 } 97 PtrToAddress(IntPtr ptr)98 public static IPAddress PtrToAddress (IntPtr ptr) 99 { 100 IPAddress address = null; 101 102 if (ptr != IntPtr.Zero) { 103 IntPtr buf = Stdlib.malloc (256); 104 IntPtr addrPtr = avahi_address_snprint (buf, 256, ptr); 105 address = IPAddress.Parse (Utility.PtrToString (addrPtr)); 106 Utility.Free (addrPtr); 107 } 108 109 return address; 110 } 111 } 112 } 113