Skip to content

Commit

Permalink
Convert System.Net.* over to GeneratedDllImport (#61765)
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronRobinsonMSFT authored Nov 18, 2021
1 parent bd63cfc commit cc44d43
Show file tree
Hide file tree
Showing 33 changed files with 183 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ internal static partial class Interop
{
internal static partial class AndroidCrypto
{
[DllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLGetSupportedProtocols")]
internal static extern SslProtocols SSLGetSupportedProtocols();
[GeneratedDllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLGetSupportedProtocols")]
internal static partial SslProtocols SSLGetSupportedProtocols();

[DllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLSupportsApplicationProtocolsConfiguration")]
[GeneratedDllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLSupportsApplicationProtocolsConfiguration")]
[return:MarshalAs(UnmanagedType.U1)]
internal static extern bool SSLSupportsApplicationProtocolsConfiguration();
internal static partial bool SSLSupportsApplicationProtocolsConfiguration();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ internal enum PAL_SSLStreamStatus
Closed = 4,
};

[DllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamCreate")]
internal static extern SafeSslHandle SSLStreamCreate();
[GeneratedDllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamCreate")]
internal static partial SafeSslHandle SSLStreamCreate();

[DllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamCreateWithCertificates")]
private static extern SafeSslHandle SSLStreamCreateWithCertificates(
[GeneratedDllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamCreateWithCertificates")]
private static partial SafeSslHandle SSLStreamCreateWithCertificates(
ref byte pkcs8PrivateKey,
int pkcs8PrivateKeyLen,
PAL_KeyAlgorithm algorithm,
Expand All @@ -51,8 +51,8 @@ ref MemoryMarshal.GetReference(pkcs8PrivateKey),
certificates.Length);
}

[DllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamInitialize")]
private static extern int SSLStreamInitializeImpl(
[GeneratedDllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamInitialize")]
private static partial int SSLStreamInitializeImpl(
SafeSslHandle sslHandle,
[MarshalAs(UnmanagedType.U1)] bool isServer,
SSLReadCallback streamRead,
Expand All @@ -70,8 +70,8 @@ internal static void SSLStreamInitialize(
throw new SslException();
}

[DllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamSetTargetHost")]
private static extern int SSLStreamSetTargetHostImpl(
[GeneratedDllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamSetTargetHost")]
private static partial int SSLStreamSetTargetHostImpl(
SafeSslHandle sslHandle,
[MarshalAs(UnmanagedType.LPUTF8Str)] string targetHost);
internal static void SSLStreamSetTargetHost(
Expand All @@ -85,8 +85,8 @@ internal static void SSLStreamSetTargetHost(
throw new SslException();
}

[DllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamRequestClientAuthentication")]
internal static extern void SSLStreamRequestClientAuthentication(SafeSslHandle sslHandle);
[GeneratedDllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamRequestClientAuthentication")]
internal static partial void SSLStreamRequestClientAuthentication(SafeSslHandle sslHandle);

[StructLayout(LayoutKind.Sequential)]
private unsafe struct ApplicationProtocolData
Expand All @@ -95,8 +95,8 @@ private unsafe struct ApplicationProtocolData
public int Length;
}

[DllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamSetApplicationProtocols")]
private static unsafe extern int SSLStreamSetApplicationProtocols(SafeSslHandle sslHandle, ApplicationProtocolData[] protocolData, int count);
[GeneratedDllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamSetApplicationProtocols")]
private static unsafe partial int SSLStreamSetApplicationProtocols(SafeSslHandle sslHandle, ApplicationProtocolData[] protocolData, int count);
internal static unsafe void SSLStreamSetApplicationProtocols(SafeSslHandle sslHandle, List<SslApplicationProtocol> protocols)
{
int count = protocols.Count;
Expand Down Expand Up @@ -129,20 +129,20 @@ internal static unsafe void SSLStreamSetApplicationProtocols(SafeSslHandle sslHa
}
}

[DllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamSetEnabledProtocols")]
private static extern int SSLStreamSetEnabledProtocols(SafeSslHandle sslHandle, ref SslProtocols protocols, int length);
[GeneratedDllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamSetEnabledProtocols")]
private static partial int SSLStreamSetEnabledProtocols(SafeSslHandle sslHandle, ref SslProtocols protocols, int length);
internal static void SSLStreamSetEnabledProtocols(SafeSslHandle sslHandle, ReadOnlySpan<SslProtocols> protocols)
{
int ret = SSLStreamSetEnabledProtocols(sslHandle, ref MemoryMarshal.GetReference(protocols), protocols.Length);
if (ret != SUCCESS)
throw new SslException();
}

[DllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamHandshake")]
internal static extern PAL_SSLStreamStatus SSLStreamHandshake(SafeSslHandle sslHandle);
[GeneratedDllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamHandshake")]
internal static partial PAL_SSLStreamStatus SSLStreamHandshake(SafeSslHandle sslHandle);

[DllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamGetApplicationProtocol")]
private static extern int SSLStreamGetApplicationProtocol(SafeSslHandle ssl, [Out] byte[]? buf, ref int len);
[GeneratedDllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamGetApplicationProtocol")]
private static partial int SSLStreamGetApplicationProtocol(SafeSslHandle ssl, byte[]? buf, ref int len);
internal static byte[]? SSLStreamGetApplicationProtocol(SafeSslHandle ssl)
{
int len = 0;
Expand All @@ -158,8 +158,8 @@ internal static void SSLStreamSetEnabledProtocols(SafeSslHandle sslHandle, ReadO
return bytes;
}

[DllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamRead")]
private static unsafe extern PAL_SSLStreamStatus SSLStreamRead(
[GeneratedDllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamRead")]
private static unsafe partial PAL_SSLStreamStatus SSLStreamRead(
SafeSslHandle sslHandle,
byte* buffer,
int length,
Expand All @@ -175,8 +175,8 @@ internal static unsafe PAL_SSLStreamStatus SSLStreamRead(
}
}

[DllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamWrite")]
private static unsafe extern PAL_SSLStreamStatus SSLStreamWrite(
[GeneratedDllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamWrite")]
private static unsafe partial PAL_SSLStreamStatus SSLStreamWrite(
SafeSslHandle sslHandle,
byte* buffer,
int length);
Expand All @@ -190,8 +190,8 @@ internal static unsafe PAL_SSLStreamStatus SSLStreamWrite(
}
}

[DllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamRelease")]
internal static extern void SSLStreamRelease(IntPtr ptr);
[GeneratedDllImport(Interop.Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamRelease")]
internal static partial void SSLStreamRelease(IntPtr ptr);

internal sealed class SslException : Exception
{
Expand All @@ -205,8 +205,8 @@ internal SslException(int errorCode)
}
}

[DllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamGetProtocol")]
private static extern int SSLStreamGetProtocol(SafeSslHandle ssl, out IntPtr protocol);
[GeneratedDllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamGetProtocol")]
private static partial int SSLStreamGetProtocol(SafeSslHandle ssl, out IntPtr protocol);
internal static string SSLStreamGetProtocol(SafeSslHandle ssl)
{
IntPtr protocolPtr;
Expand All @@ -222,11 +222,11 @@ internal static string SSLStreamGetProtocol(SafeSslHandle ssl)
return protocol;
}

[DllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamGetPeerCertificate")]
internal static extern SafeX509Handle SSLStreamGetPeerCertificate(SafeSslHandle ssl);
[GeneratedDllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamGetPeerCertificate")]
internal static partial SafeX509Handle SSLStreamGetPeerCertificate(SafeSslHandle ssl);

[DllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamGetPeerCertificates")]
private static extern void SSLStreamGetPeerCertificates(
[GeneratedDllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamGetPeerCertificates")]
private static partial void SSLStreamGetPeerCertificates(
SafeSslHandle ssl,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] out IntPtr[] certs,
out int count);
Expand All @@ -238,8 +238,8 @@ private static extern void SSLStreamGetPeerCertificates(
return ptrs;
}

[DllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamGetCipherSuite")]
private static extern int SSLStreamGetCipherSuite(SafeSslHandle ssl, out IntPtr cipherSuite);
[GeneratedDllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamGetCipherSuite")]
private static partial int SSLStreamGetCipherSuite(SafeSslHandle ssl, out IntPtr cipherSuite);
internal static string SSLStreamGetCipherSuite(SafeSslHandle ssl)
{
IntPtr cipherSuitePtr;
Expand All @@ -255,13 +255,13 @@ internal static string SSLStreamGetCipherSuite(SafeSslHandle ssl)
return cipherSuite;
}

[DllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamShutdown")]
[GeneratedDllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamShutdown")]
[return: MarshalAs(UnmanagedType.U1)]
internal static extern bool SSLStreamShutdown(SafeSslHandle ssl);
internal static partial bool SSLStreamShutdown(SafeSslHandle ssl);

[DllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamVerifyHostname")]
[GeneratedDllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_SSLStreamVerifyHostname")]
[return: MarshalAs(UnmanagedType.U1)]
internal static extern bool SSLStreamVerifyHostname(
internal static partial bool SSLStreamVerifyHostname(
SafeSslHandle ssl,
[MarshalAs(UnmanagedType.LPUTF8Str)] string hostname);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ internal static partial class AndroidCrypto
private const int INSUFFICIENT_BUFFER = -1;
private const int SUCCESS = 1;

[DllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X509Decode")]
internal static extern SafeX509Handle X509Decode(ref byte buf, int len);
[GeneratedDllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X509Decode")]
internal static partial SafeX509Handle X509Decode(ref byte buf, int len);

[DllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X509Encode")]
private static extern int X509Encode(SafeX509Handle x, [Out] byte[]? buf, ref int len);
[GeneratedDllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X509Encode")]
private static partial int X509Encode(SafeX509Handle x, byte[]? buf, ref int len);
internal static byte[] X509Encode(SafeX509Handle x)
{
int len = 0;
Expand All @@ -33,8 +33,8 @@ internal static byte[] X509Encode(SafeX509Handle x)
return encoded;
}

[DllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X509DecodeCollection")]
private static extern int X509DecodeCollection(ref byte buf, int bufLen, IntPtr[]? ptrs, ref int handlesLen);
[GeneratedDllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X509DecodeCollection")]
private static partial int X509DecodeCollection(ref byte buf, int bufLen, IntPtr[]? ptrs, ref int handlesLen);
internal static SafeX509Handle[] X509DecodeCollection(ReadOnlySpan<byte> data)
{
ref byte buf = ref MemoryMarshal.GetReference(data);
Expand All @@ -60,8 +60,8 @@ internal static SafeX509Handle[] X509DecodeCollection(ReadOnlySpan<byte> data)
return handles;
}

[DllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X509ExportPkcs7")]
private static extern int X509ExportPkcs7(IntPtr[] certs, int certsLen, [Out] byte[]? buf, ref int len);
[GeneratedDllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X509ExportPkcs7")]
private static partial int X509ExportPkcs7(IntPtr[] certs, int certsLen, byte[]? buf, ref int len);
internal static byte[] X509ExportPkcs7(IntPtr[] certHandles)
{
int len = 0;
Expand All @@ -77,8 +77,8 @@ internal static byte[] X509ExportPkcs7(IntPtr[] certHandles)
return encoded;
}

[DllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X509GetContentType")]
private static extern X509ContentType X509GetContentType(ref byte buf, int len);
[GeneratedDllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X509GetContentType")]
private static partial X509ContentType X509GetContentType(ref byte buf, int len);
internal static X509ContentType X509GetContentType(ReadOnlySpan<byte> data)
{
return X509GetContentType(ref MemoryMarshal.GetReference(data), data.Length);
Expand All @@ -92,8 +92,8 @@ internal enum PAL_KeyAlgorithm
UnknownAlgorithm = -1,
}

[DllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X509PublicKey")]
internal static extern IntPtr X509GetPublicKey(SafeX509Handle x, PAL_KeyAlgorithm algorithm);
[GeneratedDllImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X509PublicKey")]
internal static partial IntPtr X509GetPublicKey(SafeX509Handle x, PAL_KeyAlgorithm algorithm);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public readonly struct TcpGlobalStatistics
private readonly int __padding;
}

[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetTcpGlobalStatistics")]
public static unsafe extern int GetTcpGlobalStatistics(TcpGlobalStatistics* statistics);
[GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetTcpGlobalStatistics")]
public static unsafe partial int GetTcpGlobalStatistics(TcpGlobalStatistics* statistics);

[StructLayoutAttribute(LayoutKind.Sequential)]
public readonly struct IPv4GlobalStatistics
Expand All @@ -55,8 +55,8 @@ public readonly struct IPv4GlobalStatistics
public readonly int Forwarding;
}

[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetIPv4GlobalStatistics")]
public static unsafe extern int GetIPv4GlobalStatistics(IPv4GlobalStatistics* statistics);
[GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetIPv4GlobalStatistics")]
public static unsafe partial int GetIPv4GlobalStatistics(IPv4GlobalStatistics* statistics);

[StructLayoutAttribute(LayoutKind.Sequential)]
public readonly struct UdpGlobalStatistics
Expand All @@ -68,8 +68,8 @@ public readonly struct UdpGlobalStatistics
public readonly ulong UdpListeners;
}

[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetUdpGlobalStatistics")]
public static unsafe extern int GetUdpGlobalStatistics(UdpGlobalStatistics* statistics);
[GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetUdpGlobalStatistics")]
public static unsafe partial int GetUdpGlobalStatistics(UdpGlobalStatistics* statistics);

[StructLayoutAttribute(LayoutKind.Sequential)]
public readonly struct Icmpv4GlobalStatistics
Expand Down Expand Up @@ -98,8 +98,8 @@ public readonly struct Icmpv4GlobalStatistics
public readonly ulong TimestampRequestsSent;
}

[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetIcmpv4GlobalStatistics")]
public static unsafe extern int GetIcmpv4GlobalStatistics(Icmpv4GlobalStatistics* statistics);
[GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetIcmpv4GlobalStatistics")]
public static unsafe partial int GetIcmpv4GlobalStatistics(Icmpv4GlobalStatistics* statistics);

[StructLayoutAttribute(LayoutKind.Sequential)]
public readonly struct Icmpv6GlobalStatistics
Expand Down Expand Up @@ -134,8 +134,8 @@ public readonly struct Icmpv6GlobalStatistics
public readonly ulong TimeExceededMessagesSent;
}

[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetIcmpv6GlobalStatistics")]
public static unsafe extern int GetIcmpv6GlobalStatistics(Icmpv6GlobalStatistics* statistics);
[GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetIcmpv6GlobalStatistics")]
public static unsafe partial int GetIcmpv6GlobalStatistics(Icmpv6GlobalStatistics* statistics);

public readonly struct NativeIPInterfaceStatistics
{
Expand All @@ -158,7 +158,7 @@ public readonly struct NativeIPInterfaceStatistics
[GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetNativeIPInterfaceStatistics", CharSet = CharSet.Ansi)]
public static partial int GetNativeIPInterfaceStatistics(string name, out NativeIPInterfaceStatistics stats);

[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetNumRoutes")]
public static extern int GetNumRoutes();
[GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetNumRoutes")]
public static partial int GetNumRoutes();
}
}
Loading

0 comments on commit cc44d43

Please sign in to comment.