Development discussion of WireGuard
 help / color / mirror / Atom feed
* [PATCH] embeddable-dll-service: csharp: fix peer endpoint bug
@ 2021-08-14  5:13 dotneutron
  2021-08-14 10:18 ` Jason A. Donenfeld
  0 siblings, 1 reply; 2+ messages in thread
From: dotneutron @ 2021-08-14  5:13 UTC (permalink / raw)
  To: wireguard; +Cc: Jason, Neutron

ADDRESS_FAMILY is a typedef for USHORT. ioctlPeer->Endpoint.si_family
evaluates to 1882324994 instead of AF_INET when enum ADDRESS_FAMILY
inherits from UInt32 instead of ushort/UInt16.

IPAddress#NetworkToHostOrder does not have an overload for ushort and
int is chosen instead. This does not play well - e.g.,
NetworkToHostOrder(28722) evaluates to 846200832. An extension method
is proposed to solve this issue.

Signed-off-by: Neutron <dotneutron@protonmail.ch>
---
 .../csharp/Extensions/NumberExtensions.cs          | 14 ++++++++++++++
 embeddable-dll-service/csharp/TunnelDll/Driver.cs  |  9 +++++----
 embeddable-dll-service/csharp/TunnelDll/Win32.cs   |  2 +-
 3 files changed, 20 insertions(+), 5 deletions(-)
 create mode 100644 embeddable-dll-service/csharp/Extensions/NumberExtensions.cs

diff --git a/embeddable-dll-service/csharp/Extensions/NumberExtensions.cs b/embeddable-dll-service/csharp/Extensions/NumberExtensions.cs
new file mode 100644
index 00000000..dfa2f981
--- /dev/null
+++ b/embeddable-dll-service/csharp/Extensions/NumberExtensions.cs
@@ -0,0 +1,14 @@
+using System;
+
+namespace DemoUI.Extensions
+{
+    public static class NumberExtensions
+    {
+        public static ushort ConvertFromNetworkToHostOrder(this ushort value)
+        {
+            var bytes = BitConverter.GetBytes(value);
+            Array.Reverse(bytes);
+            return BitConverter.ToUInt16(bytes);
+        }
+    }
+}
diff --git a/embeddable-dll-service/csharp/TunnelDll/Driver.cs b/embeddable-dll-service/csharp/TunnelDll/Driver.cs
index 857d6a63..d630c6df 100644
--- a/embeddable-dll-service/csharp/TunnelDll/Driver.cs
+++ b/embeddable-dll-service/csharp/TunnelDll/Driver.cs
@@ -7,6 +7,7 @@ using System;
 using System.ComponentModel;
 using System.Net;
 using System.Runtime.InteropServices;
+using DemoUI.Extensions;

 namespace Tunnel
 {
@@ -73,14 +74,14 @@ namespace Tunnel
                             if (ioctlPeer->Endpoint.si_family == Win32.ADDRESS_FAMILY.AF_INET)
                             {
                                 var ip = new byte[4];
-                                Marshal.Copy((IntPtr)ioctlPeer->Endpoint.Ipv4.sin_addr.bytes, ip, 0, 4);
-                                peer.Endpoint = new IPEndPoint(new IPAddress(ip), IPAddress.NetworkToHostOrder(ioctlPeer->Endpoint.Ipv4.sin_port));
+                                Marshal.Copy((IntPtr)ioctlPeer->Endpoint.Ipv4.sin_addr.bytes, ip, 0, ip.Length);
+                                peer.Endpoint = new IPEndPoint(new IPAddress(ip), ioctlPeer->Endpoint.Ipv4.sin_port.ConvertFromNetworkToHostOrder());
                             }
                             else if (ioctlPeer->Endpoint.si_family == Win32.ADDRESS_FAMILY.AF_INET6)
                             {
                                 var ip = new byte[16];
-                                Marshal.Copy((IntPtr)ioctlPeer->Endpoint.Ipv6.sin6_addr.bytes, ip, 0, 16);
-                                peer.Endpoint = new IPEndPoint(new IPAddress(ip), IPAddress.NetworkToHostOrder(ioctlPeer->Endpoint.Ipv6.sin6_port));
+                                Marshal.Copy((IntPtr)ioctlPeer->Endpoint.Ipv6.sin6_addr.bytes, ip, 0, ip.Length);
+                                peer.Endpoint = new IPEndPoint(new IPAddress(ip), ioctlPeer->Endpoint.Ipv6.sin6_port.ConvertFromNetworkToHostOrder());
                             }
                         }
                         peer.TxBytes = ioctlPeer->TxBytes;
diff --git a/embeddable-dll-service/csharp/TunnelDll/Win32.cs b/embeddable-dll-service/csharp/TunnelDll/Win32.cs
index 4987fe8f..b4084ccf 100644
--- a/embeddable-dll-service/csharp/TunnelDll/Win32.cs
+++ b/embeddable-dll-service/csharp/TunnelDll/Win32.cs
@@ -180,7 +180,7 @@ namespace Tunnel
             public ADDRESS_FAMILY si_family;
         }

-        public enum ADDRESS_FAMILY : UInt32
+        public enum ADDRESS_FAMILY : UInt16
         {
             AF_UNSPEC = 0,
             AF_INET = 2,
--
2.32.0.windows.2



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] embeddable-dll-service: csharp: fix peer endpoint bug
  2021-08-14  5:13 [PATCH] embeddable-dll-service: csharp: fix peer endpoint bug dotneutron
@ 2021-08-14 10:18 ` Jason A. Donenfeld
  0 siblings, 0 replies; 2+ messages in thread
From: Jason A. Donenfeld @ 2021-08-14 10:18 UTC (permalink / raw)
  To: dotneutron; +Cc: WireGuard mailing list

Hi Neutron,

Thanks for the report. I fixed this slightly differently here:

https://git.zx2c4.com/wireguard-windows/commit/?id=0ab19b6a457b672bdb613b4c92d9786a48edd605

Note that I would have given a review to your patch instead of fixing
myself, but I can't actually accept patches from you because your
Signed-off-by doesn't specify your name, per the notes here
https://www.kernel.org/doc/html/v4.11/process/submitting-patches.html#developer-s-certificate-of-origin-1-1
. WireGuard tries to stay decently close to Linux kernel guidelines.

Jason

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-08-14 10:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-14  5:13 [PATCH] embeddable-dll-service: csharp: fix peer endpoint bug dotneutron
2021-08-14 10:18 ` Jason A. Donenfeld

Development discussion of WireGuard

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://inbox.vuxu.org/wireguard/0 wireguard/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 wireguard wireguard/ https://inbox.vuxu.org/wireguard \
		wireguard@lists.zx2c4.com
	public-inbox-index wireguard

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.wireguard


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git