Merge pull request #2274 from esdrubal/udpclientreceive
[mono.git] / mcs / class / System / System / AndroidPlatform.cs
1 //
2 // System.AndroidPlatform.cs
3 //
4 // Author:
5 //   Jonathan Pryor (jonp@xamarin.com)
6 //
7 // Copyright (C) 2012 Xamarin Inc (http://xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 #if MONODROID
29 using System;
30 using System.Collections.Generic;
31 using System.Net;
32 using System.Net.Security;
33 using System.Security.Cryptography.X509Certificates;
34 #if SECURITY_DEP
35 using MSX = Mono.Security.X509;
36 #endif
37
38 namespace System {
39
40         internal static class AndroidPlatform {
41                 delegate int GetInterfaceAddressesDelegate (out IntPtr ifap);
42                 delegate void FreeInterfaceAddressesDelegate (IntPtr ifap);
43                 
44 #if SECURITY_DEP
45                 static readonly Converter<List <byte[]>, bool> trustEvaluateSsl;
46 #endif  // SECURITY_DEP
47                 static readonly Func<IWebProxy> getDefaultProxy;
48                 static readonly GetInterfaceAddressesDelegate getInterfaceAddresses;
49                 static readonly FreeInterfaceAddressesDelegate freeInterfaceAddresses;
50
51                 static AndroidPlatform ()
52                 {
53                         var t = Type.GetType ("Android.Runtime.AndroidEnvironment, Mono.Android", throwOnError:true);
54 #if SECURITY_DEP
55                         trustEvaluateSsl = (Converter<List<byte[]>, bool>)
56                                 Delegate.CreateDelegate (typeof (Converter<List<byte[]>, bool>),
57                                                         t,
58                                                         "TrustEvaluateSsl",
59                                                         ignoreCase:false,
60                                                         throwOnBindFailure:true);
61 #endif  // SECURITY_DEP
62                         getDefaultProxy = (Func<IWebProxy>)Delegate.CreateDelegate (
63                                 typeof (Func<IWebProxy>), t, "GetDefaultProxy",
64                                 ignoreCase:false,
65                                 throwOnBindFailure:true);
66
67                         getInterfaceAddresses = (GetInterfaceAddressesDelegate)Delegate.CreateDelegate (
68                                 typeof (GetInterfaceAddressesDelegate), t, "GetInterfaceAddresses",
69                                 ignoreCase: false,
70                                 throwOnBindFailure: false);
71                         
72                         freeInterfaceAddresses = (FreeInterfaceAddressesDelegate)Delegate.CreateDelegate (
73                                 typeof (FreeInterfaceAddressesDelegate), t, "FreeInterfaceAddresses",
74                                 ignoreCase: false,
75                                 throwOnBindFailure: false);
76                 }
77
78 #if SECURITY_DEP
79                 internal static bool TrustEvaluateSsl (X509CertificateCollection collection)
80                 {
81                         var certsRawData = new List <byte[]> (collection.Count);
82                         foreach (var cert in collection)
83                                 certsRawData.Add (cert.GetRawCertData ());
84                         return trustEvaluateSsl (certsRawData);
85                 }
86 #endif  // SECURITY_DEP
87
88                 internal static IWebProxy GetDefaultProxy ()
89                 {
90                         return getDefaultProxy ();
91                 }
92
93                 internal static int GetInterfaceAddresses (out IntPtr ifap)
94                 {
95                         ifap = IntPtr.Zero;
96                         if (getInterfaceAddresses == null)
97                                 return -1;
98
99                         return getInterfaceAddresses (out ifap);
100                 }
101
102                 internal static void FreeInterfaceAddresses (IntPtr ifap)
103                 {
104                         if (freeInterfaceAddresses == null)
105                                 return;
106
107                         freeInterfaceAddresses (ifap);
108                 }
109         }
110 }
111 #endif  // MONODROID