Skip to content

Commit

Permalink
fixed revered order issue of ipv4 address
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryjiang committed Aug 11, 2024
1 parent 5149616 commit 6f3ee11
Showing 1 changed file with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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<TPackageInfo> : ProxyProtocolPackagePartReader<TPackageInfo>
{
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<byte> _bufferPool = ArrayPool<byte>.Shared;

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down

0 comments on commit 6f3ee11

Please sign in to comment.