Skip to content

Commit add3587

Browse files
authored
[SystemConfiguration] Fix all availability attributes in this framework. (#22709)
* Add/adjust availability attributes. * Remove !.NET availability attributes / code. This is a step towards fixing #21185.
1 parent 5ebebd5 commit add3587

File tree

4 files changed

+185
-353
lines changed

4 files changed

+185
-353
lines changed

src/SystemConfiguration/CaptiveNetwork.cs

+67-137
Original file line numberDiff line numberDiff line change
@@ -23,104 +23,56 @@ namespace SystemConfiguration {
2323
// CaptiveNetwork.h
2424
public static partial class CaptiveNetwork {
2525

26-
#if __TVOS__
27-
// in Xcode 10 the CaptiveNetwork API are marked as prohibited on tvOS
28-
#if !NET
29-
[Obsolete ("Always return 'null'.")]
30-
[Unavailable (PlatformName.TvOS)]
31-
public static Foundation.NSString? NetworkInfoKeyBSSID => null;
32-
33-
[Obsolete ("Always return 'null'.")]
34-
[Unavailable (PlatformName.TvOS)]
35-
public static Foundation.NSString? NetworkInfoKeySSID => null;
36-
37-
[Obsolete ("Always return 'null'.")]
38-
[Unavailable (PlatformName.TvOS)]
39-
public static Foundation.NSString? NetworkInfoKeySSIDData => null;
40-
41-
[Obsolete ("Throw a 'NotSupportedException'.")]
42-
[Unavailable (PlatformName.TvOS)]
43-
public static bool MarkPortalOffline (string iface) => throw new NotSupportedException ();
44-
45-
[Obsolete ("Throw a 'NotSupportedException'.")]
46-
[Unavailable (PlatformName.TvOS)]
47-
public static bool MarkPortalOnline (string iface) => throw new NotSupportedException ();
48-
49-
[Obsolete ("Throw a 'NotSupportedException'.")]
50-
[Unavailable (PlatformName.TvOS)]
51-
public static bool SetSupportedSSIDs (string[] ssids) => throw new NotSupportedException ();
52-
53-
[Obsolete ("Throw a 'NotSupportedException'.")]
54-
[Unavailable (PlatformName.TvOS)]
55-
public static StatusCode TryCopyCurrentNetworkInfo (string interfaceName, out Foundation.NSDictionary currentNetworkInfo) => throw new NotSupportedException ();
56-
57-
[Obsolete ("Throw a 'NotSupportedException'.")]
58-
[Unavailable (PlatformName.TvOS)]
59-
public static StatusCode TryGetSupportedInterfaces (out string[] supportedInterfaces) => throw new NotSupportedException ();
60-
#endif // !NET
61-
#else
62-
26+
#if !__TVOS__
6327
#if !MONOMAC
64-
65-
#if NET
6628
[SupportedOSPlatform ("ios")]
6729
[SupportedOSPlatform ("maccatalyst")]
6830
[ObsoletedOSPlatform ("ios14.0")]
6931
[ObsoletedOSPlatform ("maccatalyst14.0")]
70-
#else
71-
[Deprecated (PlatformName.iOS, 14, 0)]
72-
#endif
7332
[DllImport (Constants.SystemConfigurationLibrary)]
7433
extern static IntPtr /* CFDictionaryRef __nullable */ CNCopyCurrentNetworkInfo (
7534
/* CFStringRef __nonnull */ IntPtr interfaceName);
7635

77-
#if NET
78-
/// <param name="interfaceName">To be added.</param>
79-
/// <param name="currentNetworkInfo">To be added.</param>
80-
/// <summary>To be added.</summary>
81-
/// <returns>To be added.</returns>
82-
/// <remarks>To be added.</remarks>
36+
/// <summary>Copy the current network information into an <see cref="NSDictionary" />.</summary>
37+
/// <param name="interfaceName">The name of the interface with the network information to copy.</param>
38+
/// <param name="currentNetworkInfo">The copied network information upon return.</param>
39+
/// <returns><see cref="StatusCode.OK" /> if successful, an error status otherwise.</returns>
8340
[SupportedOSPlatform ("ios")]
8441
[SupportedOSPlatform ("maccatalyst")]
8542
[ObsoletedOSPlatform ("ios14.0")]
8643
[ObsoletedOSPlatform ("maccatalyst14.0")]
87-
#else
88-
[Deprecated (PlatformName.iOS, 14, 0)]
89-
#endif
9044
static public StatusCode TryCopyCurrentNetworkInfo (string interfaceName, out NSDictionary? currentNetworkInfo)
9145
{
92-
using (var nss = new NSString (interfaceName)) {
93-
var ni = CNCopyCurrentNetworkInfo (nss.Handle);
94-
if (ni == IntPtr.Zero) {
95-
currentNetworkInfo = null;
96-
return StatusCodeError.SCError ();
97-
}
98-
99-
currentNetworkInfo = new NSDictionary (ni);
100-
101-
// Must release since the IntPtr constructor calls Retain
102-
currentNetworkInfo.DangerousRelease ();
103-
return StatusCode.OK;
46+
if (string.IsNullOrEmpty (interfaceName))
47+
ThrowHelper.ThrowArgumentNullException (nameof (interfaceName));
48+
49+
using var nss = new TransientCFString (interfaceName);
50+
var ni = CNCopyCurrentNetworkInfo (nss);
51+
if (ni == IntPtr.Zero) {
52+
currentNetworkInfo = null;
53+
return StatusCodeError.SCError ();
10454
}
55+
56+
currentNetworkInfo = new NSDictionary (ni);
57+
58+
// Must release since the IntPtr constructor calls Retain
59+
currentNetworkInfo.DangerousRelease ();
60+
return StatusCode.OK;
10561
}
10662

10763
#endif
10864
[DllImport (Constants.SystemConfigurationLibrary)]
10965
extern static IntPtr /* CFArrayRef __nullable */ CNCopySupportedInterfaces ();
11066

111-
#if NET
112-
/// <param name="supportedInterfaces">To be added.</param>
113-
/// <summary>To be added.</summary>
114-
/// <returns>To be added.</returns>
115-
/// <remarks>To be added.</remarks>
67+
/// <summary>Fetch a list of the currently supported network interfaces.</summary>
68+
/// <param name="supportedInterfaces">The list of supported network interfaces upon return.</param>
69+
/// <returns><see cref="StatusCode.OK" /> if successful, an error status otherwise.</returns>
11670
[SupportedOSPlatform ("macos")]
11771
[SupportedOSPlatform ("ios")]
11872
[SupportedOSPlatform ("maccatalyst")]
119-
[ObsoletedOSPlatform ("maccatalyst13.1", "Use 'NEHotspotNetwork.FetchCurrent' instead.")]
73+
[ObsoletedOSPlatform ("maccatalyst", "Use 'NEHotspotNetwork.FetchCurrent' instead.")]
12074
[ObsoletedOSPlatform ("ios14.0", "Use 'NEHotspotNetwork.FetchCurrent' instead.")]
121-
#else
122-
[Deprecated (PlatformName.iOS, 14, 0, message: "Use 'NEHotspotNetwork.FetchCurrent' instead.")]
123-
#endif
75+
// Not deprecated on macOS
12476
static public StatusCode TryGetSupportedInterfaces (out string? []? supportedInterfaces)
12577
{
12678
IntPtr array = CNCopySupportedInterfaces ();
@@ -129,111 +81,89 @@ static public StatusCode TryGetSupportedInterfaces (out string? []? supportedInt
12981
return StatusCodeError.SCError ();
13082
}
13183

132-
supportedInterfaces = CFArray.StringArrayFromHandle (array);
133-
CFObject.CFRelease (array);
84+
supportedInterfaces = CFArray.StringArrayFromHandle (array, true);
13485
return StatusCode.OK;
13586
}
13687

137-
#if NET
13888
[SupportedOSPlatform ("ios")]
13989
[SupportedOSPlatform ("macos")]
14090
[SupportedOSPlatform ("maccatalyst")]
141-
[ObsoletedOSPlatform ("ios9.0")]
142-
[ObsoletedOSPlatform ("maccatalyst13.1")]
143-
#else
144-
[Deprecated (PlatformName.iOS, 9, 0)]
145-
#endif
91+
[ObsoletedOSPlatform ("ios", "Use 'NetworkExtension.NEHotspotHelper' instead.")]
92+
[ObsoletedOSPlatform ("maccatalyst", "Use 'NetworkExtension.NEHotspotHelper' instead.")]
93+
// Not deprecated on macOS
14694
[DllImport (Constants.SystemConfigurationLibrary)]
14795
extern static byte CNMarkPortalOffline (IntPtr /* CFStringRef __nonnull */ interfaceName);
14896

149-
#if NET
15097
[SupportedOSPlatform ("ios")]
15198
[SupportedOSPlatform ("macos")]
15299
[SupportedOSPlatform ("maccatalyst")]
153-
[ObsoletedOSPlatform ("ios9.0")]
154-
[ObsoletedOSPlatform ("maccatalyst13.1")]
155-
#else
156-
[Deprecated (PlatformName.iOS, 9, 0)]
157-
#endif
100+
[ObsoletedOSPlatform ("ios", "Use 'NetworkExtension.NEHotspotHelper' instead.")]
101+
[ObsoletedOSPlatform ("maccatalyst", "Use 'NetworkExtension.NEHotspotHelper' instead.")]
102+
// Not deprecated on macOS
158103
[DllImport (Constants.SystemConfigurationLibrary)]
159104
extern static byte CNMarkPortalOnline (IntPtr /* CFStringRef __nonnull */ interfaceName);
160105

161-
#if NET
162-
/// <param name="iface">To be added.</param>
163-
/// <summary>To be added.</summary>
164-
/// <returns>To be added.</returns>
165-
/// <remarks>This API is only available on devices. An EntryPointNotFoundException will be thrown on the simulator</remarks>
106+
/// <summary>Let the system know that the specified interface has now been authenticated and is now a viable network interface for the rest of the system.</summary>
107+
/// <param name="iface">The network interface that is now authenticated.</param>
108+
/// <returns>A boolean value indicating whether the operation was successful or not.</returns>
109+
/// <remarks>This API is only available on devices. An EntryPointNotFoundException will be thrown on the simulator</remarks>
166110
[SupportedOSPlatform ("ios")]
167111
[SupportedOSPlatform ("macos")]
168112
[SupportedOSPlatform ("maccatalyst")]
169-
[ObsoletedOSPlatform ("maccatalyst13.1")]
170-
[ObsoletedOSPlatform ("ios9.0")]
171-
#else
172-
[Deprecated (PlatformName.iOS, 9, 0)]
173-
#endif
113+
[ObsoletedOSPlatform ("ios", "Use 'NetworkExtension.NEHotspotHelper' instead.")]
114+
[ObsoletedOSPlatform ("maccatalyst", "Use 'NetworkExtension.NEHotspotHelper' instead.")]
115+
// Not deprecated on macOS
174116
static public bool MarkPortalOnline (string iface)
175117
{
176-
using (var nss = new NSString (iface)) {
177-
bool result = CNMarkPortalOnline (nss.Handle) != 0;
178-
GC.KeepAlive (nss);
179-
return result;
180-
}
118+
if (string.IsNullOrEmpty (iface))
119+
ThrowHelper.ThrowArgumentNullException (nameof (iface));
120+
121+
using var nss = new TransientCFString (iface);
122+
return CNMarkPortalOnline (nss) != 0;
181123
}
182124

183-
#if NET
184-
/// <param name="iface">To be added.</param>
185-
/// <summary>To be added.</summary>
186-
/// <returns>To be added.</returns>
187-
/// <remarks>This API is only available on devices. An EntryPointNotFoundException will be thrown on the simulator</remarks>
125+
/// <summary>Let the system know that the specified interface has not authenticated and is not a viable network interface for the rest of the system.</summary>
126+
/// <param name="iface">The network interface that is not authenticated.</param>
127+
/// <returns>A boolean value indicating whether the operation was successful or not.</returns>
128+
/// <remarks>This API is only available on devices. An EntryPointNotFoundException will be thrown on the simulator</remarks>
188129
[SupportedOSPlatform ("ios")]
189130
[SupportedOSPlatform ("macos")]
190131
[SupportedOSPlatform ("maccatalyst")]
191-
[ObsoletedOSPlatform ("maccatalyst13.1")]
192-
[ObsoletedOSPlatform ("ios9.0")]
193-
#else
194-
[Deprecated (PlatformName.iOS, 9, 0)]
195-
#endif
132+
[ObsoletedOSPlatform ("ios", "Use 'NetworkExtension.NEHotspotHelper' instead.")]
133+
[ObsoletedOSPlatform ("maccatalyst", "Use 'NetworkExtension.NEHotspotHelper' instead.")]
134+
// Not deprecated on macOS
196135
static public bool MarkPortalOffline (string iface)
197136
{
198-
using (var nss = new NSString (iface)) {
199-
bool result = CNMarkPortalOffline (nss.Handle) != 0;
200-
GC.KeepAlive (nss);
201-
return result;
202-
}
137+
if (string.IsNullOrEmpty (iface))
138+
ThrowHelper.ThrowArgumentNullException (nameof (iface));
139+
140+
using var nss = new TransientCFString (iface);
141+
return CNMarkPortalOffline (nss) != 0;
203142
}
204143

205-
#if NET
206144
[SupportedOSPlatform ("ios")]
207145
[SupportedOSPlatform ("macos")]
208146
[SupportedOSPlatform ("maccatalyst")]
209-
[ObsoletedOSPlatform ("maccatalyst13.1")]
210-
[ObsoletedOSPlatform ("ios9.0")]
211-
#else
212-
[Deprecated (PlatformName.iOS, 9, 0)]
213-
#endif
147+
[ObsoletedOSPlatform ("ios", "Use 'NetworkExtension.NEHotspotHelper' instead.")]
148+
[ObsoletedOSPlatform ("maccatalyst", "Use 'NetworkExtension.NEHotspotHelper' instead.")]
149+
// Not deprecated on macOS
214150
[DllImport (Constants.SystemConfigurationLibrary)]
215151
extern static byte CNSetSupportedSSIDs (IntPtr /* CFArrayRef __nonnull */ ssidArray);
216152

217-
#if NET
218-
/// <param name="ssids">To be added.</param>
219-
/// <summary>To be added.</summary>
220-
/// <returns>To be added.</returns>
221-
/// <remarks>This API is only available on devices. An EntryPointNotFoundException will be thrown on the simulator</remarks>
153+
/// <summary>Set an array of SSIDs the application will authenticate.</summary>
154+
/// <param name="ssids">The array of SSIDs the application will authenticate.</param>
155+
/// <returns>A boolean value indicating whether the operation was successful or not.</returns>
156+
/// <remarks>This API is only available on devices. An EntryPointNotFoundException will be thrown on the simulator</remarks>
222157
[SupportedOSPlatform ("ios")]
223158
[SupportedOSPlatform ("macos")]
224159
[SupportedOSPlatform ("maccatalyst")]
225-
[ObsoletedOSPlatform ("maccatalyst13.1")]
226-
[ObsoletedOSPlatform ("ios9.0")]
227-
#else
228-
[Deprecated (PlatformName.iOS, 9, 0)]
229-
#endif
160+
[ObsoletedOSPlatform ("ios", "Use 'NetworkExtension.NEHotspotHelper' instead.")]
161+
[ObsoletedOSPlatform ("maccatalyst", "Use 'NetworkExtension.NEHotspotHelper' instead.")]
162+
// Not deprecated on macOS
230163
static public bool SetSupportedSSIDs (string [] ssids)
231164
{
232-
using (var arr = NSArray.FromStrings (ssids)) {
233-
bool result = CNSetSupportedSSIDs (arr.Handle) != 0;
234-
GC.KeepAlive (arr);
235-
return result;
236-
}
165+
using var arr = NSArray.FromStrings (ssids);
166+
return CNSetSupportedSSIDs (arr.Handle) != 0;
237167
}
238168
#endif // __TVOS__
239169
}

0 commit comments

Comments
 (0)