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

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).