]> git.meshlink.io Git - catta/blob - avahi-sharp/Utility.cs
first pass at updating to the 0.6 apis
[catta] / avahi-sharp / Utility.cs
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5
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.
10
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.
15
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
19   USA.
20 ***/
21
22 using System;
23 using System.Net;
24 using System.Text;
25 using System.Runtime.InteropServices;
26 using Mono.Unix;
27
28
29 namespace Avahi
30 {
31     internal class Utility
32     {
33         [DllImport ("libc")]
34         private static extern int strlen (IntPtr ptr);
35
36         [DllImport ("avahi-common")]
37         private static extern IntPtr avahi_address_snprint (IntPtr buf, int size, IntPtr address);
38
39         [DllImport ("avahi-common")]
40         private static extern IntPtr avahi_address_parse (IntPtr str, Protocol proto, IntPtr ret);
41
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
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
63         public static IntPtr StringToPtr (string str)
64         {
65             if (str == null)
66                 return IntPtr.Zero;
67
68             byte[] bytes = Encoding.UTF8.GetBytes (str);
69             IntPtr buf = Stdlib.malloc ((uint) bytes.Length + 1);
70             Marshal.Copy (bytes, 0, buf, bytes.Length);
71             Marshal.WriteByte (buf, bytes.Length, 0);
72             return buf;
73         }
74
75         public static void Free (IntPtr ptr)
76         {
77             Stdlib.free (ptr);
78         }
79
80         public static IntPtr AddressToPtr (IPAddress address)
81         {
82             IntPtr straddr = Utility.StringToPtr (address.ToString ());
83             IntPtr addrPtr = Stdlib.malloc (32);
84             avahi_address_parse (straddr, Protocol.Unspecified, addrPtr);
85             Utility.Free (straddr);
86
87             return addrPtr;
88         }
89
90         public static IPAddress PtrToAddress (IntPtr ptr)
91         {
92             IPAddress address = null;
93             
94             if (ptr != IntPtr.Zero) {
95                 IntPtr buf = Stdlib.malloc (256);
96                 IntPtr addrPtr = avahi_address_snprint (buf, 256, ptr);
97                 address = IPAddress.Parse (Utility.PtrToString (addrPtr));
98                 Utility.Free (addrPtr);
99             }
100
101             return address;
102         }
103     }
104 }