X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-sharp%2FUtility.cs;h=b15161c19b035b38edcf145581240c0fc1d46846;hb=2bfc136b58cc239f170f3b247257c5369b085570;hp=d5bdeeb500f5737397c90d534eb7725574df7c3d;hpb=a21074b9ea4b11b74d114e2669248f979caf0d3a;p=catta diff --git a/avahi-sharp/Utility.cs b/avahi-sharp/Utility.cs index d5bdeeb..b15161c 100644 --- a/avahi-sharp/Utility.cs +++ b/avahi-sharp/Utility.cs @@ -1,3 +1,5 @@ +/* $Id$ */ + /*** This file is part of avahi. @@ -34,6 +36,9 @@ namespace Avahi [DllImport ("avahi-common")] private static extern IntPtr avahi_address_snprint (IntPtr buf, int size, IntPtr address); + [DllImport ("avahi-common")] + private static extern IntPtr avahi_address_parse (IntPtr str, Protocol proto, IntPtr ret); + public static string PtrToString (IntPtr ptr) { if (ptr == IntPtr.Zero) @@ -55,12 +60,20 @@ namespace Avahi return ret; } - public static IntPtr StringToPtr (string str) + public static byte[] StringToBytes (string str) + { + if (str == null) + return null; + + return Encoding.UTF8.GetBytes (str); + } + + private static IntPtr StringToPtr (string str) { if (str == null) return IntPtr.Zero; - byte[] bytes = Encoding.UTF8.GetBytes (str); + byte[] bytes = Utility.StringToBytes (str); IntPtr buf = Stdlib.malloc ((uint) bytes.Length + 1); Marshal.Copy (bytes, 0, buf, bytes.Length); Marshal.WriteByte (buf, bytes.Length, 0); @@ -72,6 +85,16 @@ namespace Avahi Stdlib.free (ptr); } + public static IntPtr AddressToPtr (IPAddress address) + { + IntPtr straddr = Utility.StringToPtr (address.ToString ()); + IntPtr addrPtr = Stdlib.malloc (32); + avahi_address_parse (straddr, Protocol.Unspecified, addrPtr); + Utility.Free (straddr); + + return addrPtr; + } + public static IPAddress PtrToAddress (IntPtr ptr) { IPAddress address = null;