]> git.meshlink.io Git - catta/blobdiff - avahi-sharp/Utility.cs
fix avahi_netlink_new to allow multiple netlinks per process
[catta] / avahi-sharp / Utility.cs
index 55254ebde96531458e2e94aaab37cbf17e598313..a1d0a669ed66608069eafe0297f9533e2a0ad06e 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id$ */
-
 /***
   This file is part of avahi.
 
@@ -24,7 +22,9 @@ using System.Net;
 using System.Text;
 using System.Runtime.InteropServices;
 using Mono.Unix;
+using Mono.Unix.Native;
 
+using Stdlib = Mono.Unix.Native.Stdlib;
 
 namespace Avahi
 {
@@ -36,11 +36,14 @@ 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)
                 return null;
-            
+
             int len = strlen (ptr);
             byte[] bytes = new byte[len];
             Marshal.Copy (ptr, bytes, 0, len);
@@ -51,18 +54,26 @@ namespace Avahi
         {
             if (ptr == IntPtr.Zero)
                 return null;
-            
+
             string ret = PtrToString (ptr);
             Free (ptr);
             return ret;
         }
 
-        public static IntPtr StringToPtr (string str)
+        public static byte[] StringToBytes (string str)
+        {
+            if (str == null)
+                return null;
+
+            return Encoding.UTF8.GetBytes (str + "\0"); // lame.
+        }
+
+        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);
@@ -74,10 +85,20 @@ 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;
-            
+
             if (ptr != IntPtr.Zero) {
                 IntPtr buf = Stdlib.malloc (256);
                 IntPtr addrPtr = avahi_address_snprint (buf, 256, ptr);