summaryrefslogtreecommitdiffstats
path: root/avahi-sharp/Utility.cs
diff options
context:
space:
mode:
authorJames Willcox <snopr@snorp.net>2005-09-09 17:12:57 +0000
committerJames Willcox <snopr@snorp.net>2005-09-09 17:12:57 +0000
commitba12decc4413dedf22c06545d1ec5938efa8954a (patch)
tree6bd8edc20ffabbbd9300264677a983695787dd7f /avahi-sharp/Utility.cs
parente1d06346134b3051878c8080694068517faf3f16 (diff)
add initial mono bindings
git-svn-id: file:///home/lennart/svn/public/avahi/trunk@538 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe
Diffstat (limited to 'avahi-sharp/Utility.cs')
-rw-r--r--avahi-sharp/Utility.cs70
1 files changed, 70 insertions, 0 deletions
diff --git a/avahi-sharp/Utility.cs b/avahi-sharp/Utility.cs
new file mode 100644
index 0000000..c621bed
--- /dev/null
+++ b/avahi-sharp/Utility.cs
@@ -0,0 +1,70 @@
+using System;
+using System.Net;
+using System.Text;
+using System.Runtime.InteropServices;
+using Mono.Unix;
+
+
+namespace Avahi
+{
+ internal class Utility
+ {
+ [DllImport ("libc")]
+ private static extern int strlen (IntPtr ptr);
+
+ [DllImport ("avahi-common")]
+ private static extern IntPtr avahi_address_snprint (IntPtr buf, int size, IntPtr address);
+
+ 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);
+ return Encoding.UTF8.GetString (bytes);
+ }
+
+ public static string PtrToStringFree (IntPtr ptr)
+ {
+ if (ptr == IntPtr.Zero)
+ return null;
+
+ string ret = PtrToString (ptr);
+ Free (ptr);
+ return ret;
+ }
+
+ public static IntPtr StringToPtr (string str)
+ {
+ if (str == null)
+ return IntPtr.Zero;
+
+ byte[] bytes = Encoding.UTF8.GetBytes (str);
+ IntPtr buf = Stdlib.malloc ((uint) bytes.Length + 1);
+ Marshal.Copy (bytes, 0, buf, bytes.Length);
+ Marshal.WriteByte (buf, bytes.Length, 0);
+ return buf;
+ }
+
+ public static void Free (IntPtr ptr)
+ {
+ Stdlib.free (ptr);
+ }
+
+ 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);
+ address = IPAddress.Parse (Utility.PtrToString (addrPtr));
+ Utility.Free (addrPtr);
+ }
+
+ return address;
+ }
+ }
+}