Skip to contentSkip to conversation

embeddable-dll-service: csharp: fix peer endpoint bug

PATCH In review dotneutron 2 participants 2 messages Aug 14, 2021

lore
Share & tools
lore

dotneutron

author
#0 lore raw

PATCH 1/1 embeddable-dll-service: csharp: fix peer endpoint bug

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.

---
 .../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
embeddable-dll-service/csharp/Extensions/NumberExtensions.cs new file +14 -0
new file mode 100644
index 00000000..dfa2f981
@@ -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);
+        }
+    }
+}
embeddable-dll-service/csharp/TunnelDll/Driver.cs +5 -4
index 857d6a63..d630c6df 100644
@@ -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;
embeddable-dll-service/csharp/TunnelDll/Win32.cs +1 -1
index 4987fe8f..b4084ccf 100644
@@ -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

#1 lore raw

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