Skip to content

Commit

Permalink
Fix UDP plugin server not accepting packets
Browse files Browse the repository at this point in the history
  • Loading branch information
compujuckel committed Dec 21, 2023
1 parent 45b3433 commit 2b5950b
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions AssettoServer/Utils/SocketAddressExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System;
using System.Net;
using System.Reflection;

namespace AssettoServer.Utils;

public static class SocketAddressExtensions
{
private static readonly GetIPv4AddressMethod GetIPv4AddressDelegate;
private delegate uint GetIPv4AddressMethod(ReadOnlySpan<byte> buffer);

public static SocketAddress Clone(this SocketAddress address)
{
var clone = new SocketAddress(address.Family, address.Size);
Expand All @@ -14,7 +18,19 @@ public static SocketAddress Clone(this SocketAddress address)

public static bool IpEquals(this SocketAddress address, SocketAddress other)
{
// This works for IPv4 only. First two bytes = port, next 4 bytes = address
return address.Buffer.Span[2..6].SequenceEqual(other.Buffer.Span[2..6]);
return address.GetIPv4Address() == other.GetIPv4Address();
}

public static uint GetIPv4Address(this SocketAddress address)
{
return GetIPv4AddressDelegate(address.Buffer.Span);
}

static SocketAddressExtensions()
{
GetIPv4AddressDelegate = Assembly.GetAssembly(typeof(SocketAddress))!
.GetType("System.Net.SocketAddressPal")!
.GetMethod("GetIPv4Address", BindingFlags.Public | BindingFlags.Static)!
.CreateDelegate<GetIPv4AddressMethod>();
}
}

0 comments on commit 2b5950b

Please sign in to comment.