From 6f3ee11404b7a2a6b7fd66362ebb40284fdabae0 Mon Sep 17 00:00:00 2001 From: Kerry Jiang Date: Sat, 10 Aug 2024 20:52:16 -0700 Subject: [PATCH] fixed revered order issue of ipv4 address --- .../ProxyProtocolV2PartReader.cs | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/SuperSocket.ProtoBase/ProxyProtocol/ProxyProtocolV2PartReader.cs b/src/SuperSocket.ProtoBase/ProxyProtocol/ProxyProtocolV2PartReader.cs index 03efc7a8c..a1ab4af50 100644 --- a/src/SuperSocket.ProtoBase/ProxyProtocol/ProxyProtocolV2PartReader.cs +++ b/src/SuperSocket.ProtoBase/ProxyProtocol/ProxyProtocolV2PartReader.cs @@ -1,18 +1,16 @@ using System; using System.Buffers; -using System.Collections; -using System.Collections.Generic; +using System.Buffers.Binary; using System.Net; -using System.Net.Http; using System.Net.Sockets; -using System.Text; -using Microsoft.VisualBasic; namespace SuperSocket.ProtoBase.ProxyProtocol { class ProxyProtocolV2PartReader : ProxyProtocolPackagePartReader { - private static readonly int FIXPART_LEN_AFTER_SIGNATURE = 4; + private const int FIXPART_LEN_AFTER_SIGNATURE = 4; + + private const int IPV6_ADDRESS_LEN = 32; private static readonly ArrayPool _bufferPool = ArrayPool.Shared; @@ -74,9 +72,17 @@ public override bool Process(TPackageInfo package, object filterContext, ref Seq if (proxyInfo.AddressFamily == AddressFamily.InterNetwork) { reader.TryReadBigEndian(out UInt32 sourceIpData); + + if (BitConverter.IsLittleEndian) + sourceIpData = BinaryPrimitives.ReverseEndianness(sourceIpData); + proxyInfo.SourceIPAddress = new IPAddress(sourceIpData); reader.TryReadBigEndian(out UInt32 destinationIpData); + + if (BitConverter.IsLittleEndian) + destinationIpData = BinaryPrimitives.ReverseEndianness(destinationIpData); + proxyInfo.DestinationIPAddress = new IPAddress(destinationIpData); reader.TryReadBigEndian(out UInt16 sourcePort); @@ -87,19 +93,19 @@ public override bool Process(TPackageInfo package, object filterContext, ref Seq } else if (proxyInfo.AddressFamily == AddressFamily.InterNetworkV6) { - var addressBuffer = _bufferPool.Rent(32); + var addressBuffer = _bufferPool.Rent(IPV6_ADDRESS_LEN); try { - var addressBufferSpan = addressBuffer.AsSpan()[..32]; + var addressBufferSpan = addressBuffer.AsSpan()[..IPV6_ADDRESS_LEN]; - reader.Sequence.Slice(0, 32).CopyTo(addressBufferSpan); - reader.Advance(32); + reader.Sequence.Slice(0, IPV6_ADDRESS_LEN).CopyTo(addressBufferSpan); + reader.Advance(IPV6_ADDRESS_LEN); proxyInfo.SourceIPAddress = new IPAddress(addressBufferSpan); - reader.Sequence.Slice(0, 32).CopyTo(addressBufferSpan); - reader.Advance(32); + reader.Sequence.Slice(0, IPV6_ADDRESS_LEN).CopyTo(addressBufferSpan); + reader.Advance(IPV6_ADDRESS_LEN); proxyInfo.DestinationIPAddress = new IPAddress(addressBufferSpan); }