Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Network] Improve bindings for NWProtocolMetadata.#6389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
[Network] Improve bindings for NWProtocolMetadata.
It turns out the NWProtocolMetadata can contain metadata for differentprotocols (Ip/Tls/Tcp). This is important; if someone tries to get a value forone protocol and the metadata is for another protocol, then they invoke thewrath of superior beings who will smite that poor someone with uninitializedmemory.At that point there's not much left but to pray.I don't like to depend on divine intervention, so I've modified the API hereto check if the metadata's protocol is the required type for the native APIwe're calling, and if the check fails, we throw a nice and dependable managedexception.This is a functional breaking change; but if there are any lost souls wholikes to pray, they can always re-implement the P/Invokes themselves and skipthe sanity checks.In addition I've renamed a few properties whose name didn't clearly specifywhich protocol type they operate on.Ref: Apple feedback FB6155967.Ref:https://trello.com/c/1TW0BSKJ/145-fb6155967-nwipcreatemetadata-returns-uninitialized-metadata-in-ios-13
  • Loading branch information
@rolfbjarne
rolfbjarne committedJun 20, 2019
commitd1a100fefb298df4806f6ac84429156a969c01cb
75 changes: 67 additions & 8 deletionssrc/Network/NWProtocolMetadata.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -87,7 +87,35 @@ public NWProtocolMetadata (IntPtr handle, bool owns) : base (handle, owns) {}
[DllImport (Constants.NetworkLibrary)]
static extern IntPtr nw_tls_copy_sec_protocol_metadata (IntPtr handle);

public SecProtocolMetadata SecProtocolMetadata => new SecProtocolMetadata (nw_tls_copy_sec_protocol_metadata (GetCheckedHandle ()), owns: true);
void CheckIsIP ()
{
if (!IsIP)
throw new InvalidOperationException ("This metadata is not IP metadata.");
}

void CheckIsTcp ()
{
if (!IsTcp)
throw new InvalidOperationException ("This metadata is not Tcp metadata.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

TCP in the text itself

}

void CheckIsTls ()
{
if (!IsTls)
throw new InvalidOperationException ("This metadata is not TLS metadata.");
}

#if !XAMCORE_4_0
[Obsolete ("Use 'SecTlsProtocolMetadata' instead.")]
public SecProtocolMetadata SecProtocolMetadata => TlsSecProtocolMetadata;
#endif

public SecProtocolMetadata TlsSecProtocolMetadata {
get {
CheckIsTls ();
return new SecProtocolMetadata (nw_tls_copy_sec_protocol_metadata (GetCheckedHandle ()), owns: true);
}
}

[DllImport (Constants.NetworkLibrary)]
static extern void nw_ip_metadata_set_ecn_flag (OS_nw_protocol_metadata metadata, NWIPEcnFlag ecn_flag);
Expand All@@ -96,15 +124,24 @@ public NWProtocolMetadata (IntPtr handle, bool owns) : base (handle, owns) {}
static extern NWIPEcnFlag nw_ip_metadata_get_ecn_flag (OS_nw_protocol_metadata metadata);

public NWIPEcnFlag IPMetadataEcnFlag {
get => nw_ip_metadata_get_ecn_flag (GetCheckedHandle ());
set => nw_ip_metadata_set_ecn_flag (GetCheckedHandle (), value);
get {
CheckIsIP ();
return nw_ip_metadata_get_ecn_flag (GetCheckedHandle ());
}
set {
CheckIsIP ();
nw_ip_metadata_set_ecn_flag (GetCheckedHandle (), value);
}
}

[DllImport (Constants.NetworkLibrary)]
static extern /* uint64_t */ ulong nw_ip_metadata_get_receive_time (OS_nw_protocol_metadata metadata);

public ulong IPMetadataReceiveTime {
get => nw_ip_metadata_get_receive_time (GetCheckedHandle ());
get {
CheckIsIP ();
return nw_ip_metadata_get_receive_time (GetCheckedHandle ());
}
}

[DllImport (Constants.NetworkLibrary)]
Expand All@@ -113,19 +150,41 @@ public ulong IPMetadataReceiveTime {
[DllImport (Constants.NetworkLibrary)]
static extern NWServiceClass nw_ip_metadata_get_service_class (OS_nw_protocol_metadata metadata);

#if !XAMCORE_4_0
[Obsolete ("Use 'IPServiceClass' instead.")]
public NWServiceClass ServiceClass {
get => nw_ip_metadata_get_service_class (GetCheckedHandle ());
set => nw_ip_metadata_set_service_class (GetCheckedHandle (), value);
get => IPServiceClass;
set => IPServiceClass = value;
}
#endif

public NWServiceClass IPServiceClass {
get {
CheckIsIP ();
return nw_ip_metadata_get_service_class (GetCheckedHandle ());
}
set {
CheckIsIP ();
nw_ip_metadata_set_service_class (GetCheckedHandle (), value);
}
}

[DllImport (Constants.NetworkLibrary)]
extern static /* uint32_t */ uint nw_tcp_get_available_receive_buffer (IntPtr handle);

public uint TcpGetAvailableReceiveBuffer () => nw_tcp_get_available_receive_buffer (GetCheckedHandle ());
public uint TcpGetAvailableReceiveBuffer ()
{
CheckIsTcp ();
return nw_tcp_get_available_receive_buffer (GetCheckedHandle ());
}

[DllImport (Constants.NetworkLibrary)]
extern static /* uint32_t */ uint nw_tcp_get_available_send_buffer (IntPtr handle);

public uint TcpGetAvailableSendBuffer () => nw_tcp_get_available_send_buffer (GetCheckedHandle ());
public uint TcpGetAvailableSendBuffer ()
{
CheckIsTcp ();
return nw_tcp_get_available_send_buffer (GetCheckedHandle ());
}
}
}
16 changes: 10 additions & 6 deletionstests/monotouch-test/Network/NWProtocolMetadataTest.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,25 +28,29 @@ public void IP ()
Assert.False (m.IsTcp, "IsTcp");
Assert.False (m.IsUdp, "IsUdp");
Assert.NotNull (m.ProtocolDefinition, "ProtocolDefinition");
Assert.NotNull (m.SecProtocolMetadata, "SecProtocolMetadata");
Assert.Throws<InvalidOperationException> (() => { var x = m.SecProtocolMetadata; }, "SecProtocolMetadata");
Assert.Throws<InvalidOperationException> (() => { var x = m.TlsSecProtocolMetadata; }, "TlsSecProtocolMetadata");
Assert.That (m.ServiceClass, Is.EqualTo (NWServiceClass.BestEffort), "ServiceClass");
Assert.That (m.IPServiceClass, Is.EqualTo (NWServiceClass.BestEffort), "IPServiceClass");
}
}

[Test]
public void Udp ()
{
using (var m = NWProtocolMetadata.CreateUdpMetadata ()) {
Assert.That (m.IPMetadataEcnFlag, Is.EqualTo (NWIPEcnFlag.NonEct), "IPMetadataEcnFlag");
Assert.That (m.IPMetadataReceiveTime, Is.EqualTo (0), "IPMetadataReceiveTime");
Assert.Throws<InvalidOperationException> (() => { var x =m.IPMetadataEcnFlag; }, "IPMetadataEcnFlag");
Assert.Throws<InvalidOperationException> (() => { var x =m.IPMetadataReceiveTime; }, "IPMetadataReceiveTime");
Assert.False (m.IsIP, "IsIP");
Assert.False (m.IsTcp, "IsTcp");
Assert.True (m.IsUdp, "IsUdp");
Assert.NotNull (m.ProtocolDefinition, "ProtocolDefinition");
Assert.NotNull (m.SecProtocolMetadata, "SecProtocolMetadata");
Assert.That (m.ServiceClass, Is.EqualTo (NWServiceClass.BestEffort), "ServiceClass");
Assert.Throws<InvalidOperationException> (() => { var x = m.SecProtocolMetadata; }, "SecProtocolMetadata");
Assert.Throws<InvalidOperationException> (() => { var x = m.TlsSecProtocolMetadata; }, "TlsSecProtocolMetadata");
Assert.Throws<InvalidOperationException> (() => { var x = m.ServiceClass; }, "ServiceClass");
Assert.Throws<InvalidOperationException> (() => { var x = m.IPServiceClass; }, "IPServiceClass");
}
}
}
}
#endif
#endif

[8]ページ先頭

©2009-2025 Movatter.jp