Skip to content

Commit

Permalink
[NetworkExtension] Implement Xcode 16.0 beta 1-6 changes.
Browse files Browse the repository at this point in the history
Note: there were no changes in beta 2, beta 3, beta 4, beta 5 or beta 6.
  • Loading branch information
rolfbjarne committed Sep 3, 2024
1 parent 8b5b27e commit 1706235
Show file tree
Hide file tree
Showing 31 changed files with 681 additions and 656 deletions.
6 changes: 3 additions & 3 deletions src/CoreFoundation/DispatchData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

namespace CoreFoundation {

public partial class DispatchData : DispatchObject {
public class DispatchData : DispatchObject {
#if !COREBUILD
[Preserve (Conditional = true)]
#if NET
Expand Down Expand Up @@ -71,7 +71,7 @@ public static DispatchData FromByteBuffer (byte [] buffer)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (buffer));
var b = Marshal.AllocHGlobal (buffer.Length);
Marshal.Copy (buffer, 0, b, buffer.Length);
var dd = dispatch_data_create (b, (nuint) buffer.Length, IntPtr.Zero, destructor: free);
var dd = dispatch_data_create (b, (nuint) buffer.Length, IntPtr.Zero, destructor: CoreFoundationFields.free);
return new DispatchData (dd, owns: true);
}

Expand All @@ -88,7 +88,7 @@ public static DispatchData FromByteBuffer (byte [] buffer, int start, int length

var b = Marshal.AllocHGlobal (length);
Marshal.Copy (buffer, start, b, length);
var dd = dispatch_data_create (b, (nuint) length, IntPtr.Zero, destructor: free);
var dd = dispatch_data_create (b, (nuint) length, IntPtr.Zero, destructor: CoreFoundationFields.free);
return new DispatchData (dd, owns: true);
}

Expand Down
27 changes: 27 additions & 0 deletions src/Network/NWConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#nullable enable

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using ObjCRuntime;
using Foundation;
Expand Down Expand Up @@ -157,6 +158,9 @@ static void TrampolineBooleanChangeHandler (IntPtr block, byte value)
[DllImport (Constants.NetworkLibrary)]
static extern unsafe void nw_connection_set_viability_changed_handler (IntPtr handle, void* callback);

#if !XAMCORE_5_0
[Obsolete ("Use 'SetViabilityChangeHandler' instead.")]
[EditorBrowsable (EditorBrowsableState.Never)]
[BindingImpl (BindingImplOptions.Optimizable)]
public unsafe void SetBooleanChangeHandler (Action<bool> callback)
{
Expand All @@ -176,6 +180,29 @@ public unsafe void SetBooleanChangeHandler (Action<bool> callback)
nw_connection_set_viability_changed_handler (GetCheckedHandle (), &block);
}
}
#endif

/// <summary>Set a handler that is called when data can be sent or received.</summary>
/// <param name="callback">The callback to call when data can be sent or received.</param>
[BindingImpl (BindingImplOptions.Optimizable)]
public unsafe void SetViabilityChangeHandler (Action<bool> callback)
{
if (callback is null) {
nw_connection_set_viability_changed_handler (GetCheckedHandle (), null);
return;
}

unsafe {
#if NET
delegate* unmanaged<IntPtr, byte, void> trampoline = &TrampolineBooleanChangeHandler;
using var block = new BlockLiteral (trampoline, callback, typeof (NWConnection), nameof (TrampolineBooleanChangeHandler));
#else
using var block = new BlockLiteral ();
block.SetupBlockUnsafe (static_BooleanChangeHandler, callback);
#endif
nw_connection_set_viability_changed_handler (GetCheckedHandle (), &block);
}
}

[DllImport (Constants.NetworkLibrary)]
static extern unsafe void nw_connection_set_better_path_available_handler (IntPtr handle, void* callback);
Expand Down
18 changes: 18 additions & 0 deletions src/Network/NWEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ internal NWEndpoint (NativeHandle handle, bool owns) : base (handle, owns) {}
public NWEndpoint (NativeHandle handle, bool owns) : base (handle, owns) { }
#endif

#if !COREBUILD
[DllImport (Constants.NetworkLibrary)]
extern static NWEndpointType nw_endpoint_get_type (OS_nw_endpoint handle);

Expand Down Expand Up @@ -271,5 +272,22 @@ public NWTxtRecord? TxtRecord {
}
}

internal NWEndpoint []? FromNSArrayHandle (IntPtr handle)
{
return NSArray.ArrayFromHandle<NWEndpoint> (handle);
}

/// <summary>Returns an autoreleased NSArray handle.</summary>
internal IntPtr ToNSArrayHandle (NWEndpoint [] array)
{
using var rv = NSArray.FromNSObjects (array);
if (rv is null)
return IntPtr.Zero;
rv.DangerousRetain ();
rv.DangerousAutorelease ();
return rv.Handle;
}
#endif // !COREBUILD

}
}
2 changes: 2 additions & 0 deletions src/Network/NWInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ internal NWInterface (NativeHandle handle, bool owns) : base (handle, owns) {}
public NWInterface (NativeHandle handle, bool owns) : base (handle, owns) { }
#endif

#if !COREBUILD
[DllImport (Constants.NetworkLibrary)]
static extern NWInterfaceType nw_interface_get_type (OS_nw_interface iface);

Expand All @@ -56,5 +57,6 @@ public NWInterface (NativeHandle handle, bool owns) : base (handle, owns) { }
static extern /* uint32_t */ uint nw_interface_get_index (OS_nw_interface iface);

public uint Index => nw_interface_get_index (GetCheckedHandle ());
#endif // !COREBUILD
}
}
2 changes: 2 additions & 0 deletions src/Network/NWParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ internal NWParameters (NativeHandle handle, bool owns) : base (handle, owns) {}
public NWParameters (NativeHandle handle, bool owns) : base (handle, owns) { }
#endif

#if !COREBUILD
#if NET
[SupportedOSPlatform ("tvos16.0")]
[SupportedOSPlatform ("macos13.0")]
Expand Down Expand Up @@ -733,5 +734,6 @@ public bool RequiresDnssecValidation {
get => nw_parameters_requires_dnssec_validation (GetCheckedHandle ()) != 0;
set => nw_parameters_set_requires_dnssec_validation (GetCheckedHandle (), value.AsByte ());
}
#endif // COREBUILD
}
}
18 changes: 16 additions & 2 deletions src/NetworkExtension/NEEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,15 @@ public enum NEOnDemandRuleAction : long {
}

[MacCatalyst (13, 1)]
[TV (17, 0)]
[Native]
public enum NEOnDemandRuleInterfaceType : long {
Any = 0,
[NoiOS, NoMacCatalyst]
Ethernet = 1,
WiFi = 2,
[NoTV]
Cellular = 3
[NoTV, NoMac]
Cellular = 3,
}

[MacCatalyst (13, 1)]
Expand Down Expand Up @@ -209,6 +211,10 @@ public enum NEProviderStopReason : long {
AppUpdate,
}

[Deprecated (PlatformName.iOS, 18, 0, message: "Use 'Network.NWPathStatus' instead.")]
[Deprecated (PlatformName.MacCatalyst, 18, 0, message: "Use 'Network.NWPathStatus' instead.")]
[Deprecated (PlatformName.MacOSX, 15, 0, message: "Use 'Network.NWPathStatus' instead.")]
[Deprecated (PlatformName.TvOS, 18, 0, message: "Use 'Network.NWPathStatus' instead.")]
[MacCatalyst (13, 1)]
[Native]
public enum NWPathStatus : long {
Expand All @@ -218,6 +224,10 @@ public enum NWPathStatus : long {
Satisfiable = 3
}

[Deprecated (PlatformName.iOS, 18, 0, message: "Use 'Network.NWConnectionState' instead.")]
[Deprecated (PlatformName.MacCatalyst, 18, 0, message: "Use 'Network.NWConnectionState' instead.")]
[Deprecated (PlatformName.MacOSX, 15, 0, message: "Use 'Network.NWConnectionState' instead.")]
[Deprecated (PlatformName.TvOS, 18, 0, message: "Use 'Network.NWConnectionState' instead.")]
[MacCatalyst (13, 1)]
[Native]
public enum NWTcpConnectionState : long {
Expand All @@ -229,6 +239,10 @@ public enum NWTcpConnectionState : long {
Cancelled = 5
}

[Deprecated (PlatformName.iOS, 18, 0, message: "Use 'Network.NWConnectionState' instead.")]
[Deprecated (PlatformName.MacCatalyst, 18, 0, message: "Use 'Network.NWConnectionState' instead.")]
[Deprecated (PlatformName.MacOSX, 15, 0, message: "Use 'Network.NWConnectionState' instead.")]
[Deprecated (PlatformName.TvOS, 18, 0, message: "Use 'Network.NWConnectionState' instead.")]
[MacCatalyst (13, 1)]
[Native]
public enum NWUdpSessionState : long {
Expand Down
73 changes: 73 additions & 0 deletions src/Security/SecProtocolOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -592,5 +592,78 @@ public void SetTlsPreSharedKeyIdentityHint (DispatchData pskIdentityHint)
sec_protocol_options_set_tls_pre_shared_key_identity_hint (GetCheckedHandle (), pskIdentityHint.Handle);
}
#endif

#if NET
#if !COREBUILD
[SupportedOSPlatform ("tvos12.0")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("ios12.0")]
[SupportedOSPlatform ("maccatalyst")]
[DllImport (Constants.SecurityLibrary)]
unsafe static extern void sec_protocol_options_set_challenge_block (sec_protocol_options_t options, BlockLiteral* /* sec_protocol_challenge_t */ challenge_block, IntPtr /* dispatch_queue_t */ challenge_queue);

/// <summary>Set the challenge callback.</summary>
/// <param name="challenge">The callback to call during a challenge.</param>
/// <param name="queue">The queue where the callback is called.</param>
[SupportedOSPlatform ("tvos12.0")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("ios12.0")]
[SupportedOSPlatform ("maccatalyst")]
public void SetChallengeBlock (SecProtocolChallenge challenge, DispatchQueue queue)
{
unsafe {
delegate* unmanaged<IntPtr, NativeHandle, NativeHandle, void> trampoline = &Trampolines.SDSecProtocolChallenge.Invoke;
using var block = new BlockLiteral (trampoline, challenge, typeof (Trampolines.SDSecProtocolChallenge), nameof (Trampolines.SDSecProtocolChallenge.Invoke));
sec_protocol_options_set_challenge_block (GetCheckedHandle (), &block, queue.GetNonNullHandle (nameof (queue)));
}
}

[SupportedOSPlatform ("tvos12.0")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("ios12.0")]
[SupportedOSPlatform ("maccatalyst")]
[DllImport (Constants.SecurityLibrary)]
unsafe static extern void sec_protocol_options_set_verify_block (sec_protocol_options_t options, BlockLiteral* /* sec_protocol_verify_t */ verify_block, IntPtr /* dispatch_queue_t */ verify_block_queue);

/// <summary>Set the verify callback.</summary>
/// <param name="verify">The callback to call during verification.</param>
/// <param name="queue">The queue where the callback is called.</param>
[SupportedOSPlatform ("tvos12.0")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("ios12.0")]
[SupportedOSPlatform ("maccatalyst")]
public void SetVerifyBlock (SecProtocolVerify verify, DispatchQueue queue)
{
unsafe {
delegate* unmanaged<IntPtr, NativeHandle, NativeHandle, NativeHandle, void> trampoline = &Trampolines.SDSecProtocolVerify.Invoke;
using var block = new BlockLiteral (trampoline, verify, typeof (Trampolines.SDSecProtocolVerify), nameof (Trampolines.SDSecProtocolVerify.Invoke));
sec_protocol_options_set_verify_block (GetCheckedHandle (), &block, queue.GetNonNullHandle (nameof (queue)));
}
}

[SupportedOSPlatform ("tvos13.0")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
[DllImport (Constants.SecurityLibrary)]
unsafe static extern void sec_protocol_options_set_pre_shared_key_selection_block (sec_protocol_options_t options, BlockLiteral* /* sec_protocol_pre_shared_key_selection_t */ psk_selection_block, IntPtr /* dispatch_queue_t */ psk_selection_queue);

/// <summary>Set the pre-shared key selection callback.</summary>
/// <param name="selection">The callback to call during pre-shared key selection.</param>
/// <param name="queue">The queue where the callback is called.</param>
[SupportedOSPlatform ("tvos13.0")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("ios13.0")]
[SupportedOSPlatform ("maccatalyst")]
public void SetPreSharedKeySelectionBlock (SecProtocolPreSharedKeySelection selection, DispatchQueue queue)
{
unsafe {
delegate* unmanaged<IntPtr, NativeHandle, NativeHandle, NativeHandle, void> trampoline = &Trampolines.SDSecProtocolPreSharedKeySelection.Invoke;
using var block = new BlockLiteral (trampoline, selection, typeof (Trampolines.SDSecProtocolPreSharedKeySelection), nameof (Trampolines.SDSecProtocolPreSharedKeySelection.Invoke));
sec_protocol_options_set_pre_shared_key_selection_block (GetCheckedHandle (), &block, queue.GetNonNullHandle (nameof (queue)));
}
}
#endif // !COREBUILD
#endif // NET
}
}
5 changes: 5 additions & 0 deletions src/bgen/Caches/NamespaceCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class NamespaceCache {
public ICollection<string> UINamespaces { get; private set; }
public ICollection<string> ImplicitNamespaces { get; private set; }
public ICollection<string> NamespacesThatConflictWithTypes { get; private set; }
public ICollection<string> TypesInMultipleNamespaces { get; private set; }

public NamespaceCache (PlatformName currentPlatform, string customObjCRuntimeNS, bool skipSystemDrawing)
{
Expand Down Expand Up @@ -154,6 +155,10 @@ public NamespaceCache (PlatformName currentPlatform, string customObjCRuntimeNS,
"AudioUnit",
};

TypesInMultipleNamespaces = new HashSet<string> {
"NWEndpoint", // Both in Network and NetworkExtension
};

if (!skipSystemDrawing)
ImplicitNamespaces.Add ("System.Drawing");
}
Expand Down
8 changes: 8 additions & 0 deletions src/bgen/Caches/TypeCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ public class TypeCache {
// optional if MediaToolbox is present
public Type? MTAudioProcessingTap { get; }

public Type NWEndpoint { get; }
public Type NWInterface { get; }
public Type NWParameters { get; }

// optional if UIKit is present
public Type? UIOffset { get; }
public Type? UIEdgeInsets { get; }
Expand Down Expand Up @@ -288,6 +292,10 @@ public TypeCache (MetadataLoadContext universe, Frameworks frameworks, PlatformN
UIEdgeInsets = ConditionalLookup (platformAssembly, "UIKit", "UIEdgeInsets");
NSDirectionalEdgeInsets = ConditionalLookup (platformAssembly, "UIKit", "NSDirectionalEdgeInsets");
}
NWEndpoint = Lookup (platformAssembly, "Network", "NWEndpoint");
NWInterface = Lookup (platformAssembly, "Network", "NWInterface");
NWParameters = Lookup (platformAssembly, "Network", "NWParameters");

// init the NSValueCreateMap
NSValueCreateMap = BuildNSValueCreateMap (frameworks);
}
Expand Down
5 changes: 5 additions & 0 deletions src/bgen/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,11 @@ public TrampolineInfo MakeTrampoline (Type t)
invoke.AppendFormat ("CFArray.ArrayFromHandle<{0}> ({1})!", TypeManager.FormatType (null, et), safe_name);
continue;
}
if (TypeCache.INativeObject.IsAssignableFrom (et)) {
pars.Add (new TrampolineParameterInfo (NativeHandleType, safe_name));
invoke.AppendFormat ("NSArray.ArrayFromHandle<{0}> ({1})!", TypeManager.FormatType (t, et), safe_name);
continue;
}
}

if (pi.ParameterType.IsSubclassOf (TypeCache.System_Delegate)) {
Expand Down
3 changes: 3 additions & 0 deletions src/bgen/Models/MarshalTypeList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public void Load (TypeCache typeCache, Frameworks frameworks)
if (frameworks.HaveAudioUnit) {
Add (typeCache.AURenderEventEnumerator);
}
Add (typeCache.NWEndpoint);
Add (typeCache.NWInterface);
Add (typeCache.NWParameters);
}

void Add (Type? type)
Expand Down
18 changes: 17 additions & 1 deletion src/bgen/TypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,17 @@ public string FormatTypeUsedIn (string? usedInNamespace, Type? type)

if (t.Namespace is not null) {
string ns = t.Namespace;
if (NamespaceCache.ImplicitNamespaces.Contains (ns) || t.IsGenericType) {
var isImplicitNamespace = NamespaceCache.ImplicitNamespaces.Contains (ns);
var isInMultipleNamespaces = IsInMultipleNamespaces (t);
var nonGlobalCandidate = isImplicitNamespace && !isInMultipleNamespaces;
if (nonGlobalCandidate || t.IsGenericType) {
var targs = t.GetGenericArguments ();
if (targs.Length == 0)
return t.Name + nullable;
return $"global::{t.Namespace}." + t.Name.RemoveArity () + "<" + string.Join (", ", targs.Select (l => FormatTypeUsedIn (null, l)).ToArray ()) + ">" + nullable;
}
if (isInMultipleNamespaces)
return "global::" + t.FullName + nullable;
if (NamespaceCache.NamespacesThatConflictWithTypes.Contains (ns))
return "global::" + t.FullName + nullable;
if (t.Name == t.Namespace)
Expand All @@ -317,6 +322,17 @@ public string FormatTypeUsedIn (string? usedInNamespace, Type? type)
return t.FullName + nullable;
}

bool IsInMultipleNamespaces (Type? type)
{
if (type is null)
return false;

if (NamespaceCache.TypesInMultipleNamespaces.Contains (type.Name))
return true;

return IsInMultipleNamespaces (type.GetElementType ());
}

// TODO: If we ever have an API with nested properties of the same name more than
// 2 deep, we'll need to have this return a list of PropertyInfo and comb through them all.
public PropertyInfo? GetParentTypeWithSameNamedProperty (BaseTypeAttribute bta, string propertyName)
Expand Down
3 changes: 2 additions & 1 deletion src/corefoundation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ interface CFRunLoop {
NSString ModeCommon { get; }
}

[Internal]
[Partial]
interface DispatchData {
interface CoreFoundationFields {
[Internal]
[Field ("_dispatch_data_destructor_free", "/usr/lib/system/libdispatch.dylib")]
IntPtr free { get; }
Expand Down
Loading

0 comments on commit 1706235

Please sign in to comment.