[PATCH] embeddable-dll-service: csharp: fix peer endpoint bug

dotneutron dotneutron at protonmail.ch
Sat Aug 14 05:13:54 UTC 2021


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 at 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




More information about the WireGuard mailing list