Skip to content

Commit

Permalink
changed to other way to read ipv6 addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryjiang committed Aug 17, 2024
1 parent b026974 commit 19d7e36
Showing 1 changed file with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class ProxyProtocolV2PartReader<TPackageInfo> : ProxyProtocolPackagePartReader<T

private const int IPV6_ADDRESS_LEN = 16;

private const int IPV6_ADDRESS_ALL_LEN = 16 * 2;

private static readonly ArrayPool<byte> _bufferPool = ArrayPool<byte>.Shared;

public override bool Process(TPackageInfo package, object filterContext, ref SequenceReader<byte> reader, out IPackagePartReader<TPackageInfo> nextPartReader, out bool needMoreData)
Expand Down Expand Up @@ -93,21 +95,18 @@ public override bool Process(TPackageInfo package, object filterContext, ref Seq
}
else if (proxyInfo.AddressFamily == AddressFamily.InterNetworkV6)
{
var addressBuffer = _bufferPool.Rent(IPV6_ADDRESS_LEN);
var addressBuffer = _bufferPool.Rent(IPV6_ADDRESS_ALL_LEN);

try
{
var addressBufferSpan = addressBuffer.AsSpan().Slice(0, IPV6_ADDRESS_LEN);

var sequenceToRead = reader.UnreadSequence;
var addressBufferSpan = addressBuffer.AsSpan().Slice(0, IPV6_ADDRESS_ALL_LEN);

sequenceToRead.Slice(0, IPV6_ADDRESS_LEN).CopyTo(addressBufferSpan);
proxyInfo.SourceIPAddress = new IPAddress(addressBufferSpan);
reader.TryCopyTo(addressBufferSpan);

sequenceToRead.Slice(IPV6_ADDRESS_LEN, IPV6_ADDRESS_LEN).CopyTo(addressBufferSpan);
proxyInfo.DestinationIPAddress = new IPAddress(addressBufferSpan);
proxyInfo.SourceIPAddress = new IPAddress(addressBufferSpan.Slice(0, IPV6_ADDRESS_LEN));
proxyInfo.DestinationIPAddress = new IPAddress(addressBufferSpan.Slice(IPV6_ADDRESS_LEN));

reader.Advance(IPV6_ADDRESS_LEN * 2);
reader.Advance(IPV6_ADDRESS_ALL_LEN);
}
finally
{
Expand Down

0 comments on commit 19d7e36

Please sign in to comment.