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