diff --git a/src/Enclave.FastPacket/Generated/net6.0/Enclave.FastPacket.Generator/Enclave.FastPacket.Generator.PacketParserGenerator/FastPacket_PacketFieldAttribute.cs b/src/Enclave.FastPacket/Generated/net6.0/Enclave.FastPacket.Generator/Enclave.FastPacket.Generator.PacketParserGenerator/FastPacket_PacketFieldAttribute.cs index 423cee5..0498402 100644 --- a/src/Enclave.FastPacket/Generated/net6.0/Enclave.FastPacket.Generator/Enclave.FastPacket.Generator.PacketParserGenerator/FastPacket_PacketFieldAttribute.cs +++ b/src/Enclave.FastPacket/Generated/net6.0/Enclave.FastPacket.Generator/Enclave.FastPacket.Generator.PacketParserGenerator/FastPacket_PacketFieldAttribute.cs @@ -30,7 +30,7 @@ public PacketFieldAttribute() /// to indicate the exact length of the buffer slice returned. /// /// - /// The start position of all fields following this one will be adjusted to accomodate this size. + /// The start position of all fields following this one will be adjusted to accommodate this size. /// /// /// If you set this on a primitive/numeric field with a return type smaller (in bytes) than the provided size, @@ -66,7 +66,7 @@ public PacketFieldAttribute() /// *before* this one. /// /// - /// The start position of all fields following this one will be adjusted to accomodate the size returned by the function. + /// The start position of all fields following this one will be adjusted to accommodate the size returned by the function. /// /// public string? SizeFunction { get; set; } @@ -79,7 +79,7 @@ public PacketFieldAttribute() /// The field in question should be a numeric field prior to this field in the byte order. /// /// - /// The start position of all fields following this one will be adjusted to accomodate the size returned by that field. + /// The start position of all fields following this one will be adjusted to accommodate the size returned by that field. /// /// public string? SizeField { get; set; } @@ -92,7 +92,7 @@ public PacketFieldAttribute() /// In most cases you should avoid using this property; position can usually be automatically determined from the other fields in the packet. /// /// - /// The start position of all fields following this one will be adjusted to accomodate this size. + /// The start position of all fields following this one will be adjusted to accommodate this size. /// /// public int Position { get; set; } @@ -111,7 +111,7 @@ public PacketFieldAttribute() /// /// /// - /// The start position of all fields following this one will be adjusted to accomodate the size returned by the function. + /// The start position of all fields following this one will be adjusted to accommodate the size returned by the function. /// /// public string? PositionFunction { get; set; } diff --git a/src/Enclave.FastPacket/Generated/netstandard2.1/Enclave.FastPacket.Generator/Enclave.FastPacket.Generator.PacketParserGenerator/FastPacket_PacketFieldAttribute.cs b/src/Enclave.FastPacket/Generated/netstandard2.1/Enclave.FastPacket.Generator/Enclave.FastPacket.Generator.PacketParserGenerator/FastPacket_PacketFieldAttribute.cs index 423cee5..0498402 100644 --- a/src/Enclave.FastPacket/Generated/netstandard2.1/Enclave.FastPacket.Generator/Enclave.FastPacket.Generator.PacketParserGenerator/FastPacket_PacketFieldAttribute.cs +++ b/src/Enclave.FastPacket/Generated/netstandard2.1/Enclave.FastPacket.Generator/Enclave.FastPacket.Generator.PacketParserGenerator/FastPacket_PacketFieldAttribute.cs @@ -30,7 +30,7 @@ public PacketFieldAttribute() /// to indicate the exact length of the buffer slice returned. /// /// - /// The start position of all fields following this one will be adjusted to accomodate this size. + /// The start position of all fields following this one will be adjusted to accommodate this size. /// /// /// If you set this on a primitive/numeric field with a return type smaller (in bytes) than the provided size, @@ -66,7 +66,7 @@ public PacketFieldAttribute() /// *before* this one. /// /// - /// The start position of all fields following this one will be adjusted to accomodate the size returned by the function. + /// The start position of all fields following this one will be adjusted to accommodate the size returned by the function. /// /// public string? SizeFunction { get; set; } @@ -79,7 +79,7 @@ public PacketFieldAttribute() /// The field in question should be a numeric field prior to this field in the byte order. /// /// - /// The start position of all fields following this one will be adjusted to accomodate the size returned by that field. + /// The start position of all fields following this one will be adjusted to accommodate the size returned by that field. /// /// public string? SizeField { get; set; } @@ -92,7 +92,7 @@ public PacketFieldAttribute() /// In most cases you should avoid using this property; position can usually be automatically determined from the other fields in the packet. /// /// - /// The start position of all fields following this one will be adjusted to accomodate this size. + /// The start position of all fields following this one will be adjusted to accommodate this size. /// /// public int Position { get; set; } @@ -111,7 +111,7 @@ public PacketFieldAttribute() /// /// /// - /// The start position of all fields following this one will be adjusted to accomodate the size returned by the function. + /// The start position of all fields following this one will be adjusted to accommodate the size returned by the function. /// /// public string? PositionFunction { get; set; } diff --git a/src/Enclave.FastPacket/ValueIpAddress.cs b/src/Enclave.FastPacket/ValueIpAddress.cs index 5e09499..877ee2b 100644 --- a/src/Enclave.FastPacket/ValueIpAddress.cs +++ b/src/Enclave.FastPacket/ValueIpAddress.cs @@ -2,6 +2,7 @@ using System.Buffers.Binary; using System.Net; using System.Net.Sockets; +using System.Numerics; namespace Enclave.FastPacket; @@ -216,6 +217,40 @@ public override string ToString() return ToIpAddress().ToString(); } + /// + /// Converts an IPv4 address to a unsigned 32-bit integer. + /// + /// Thrown if is . + public uint ToUInt() + { + if (_addrFamily == AddressFamily.InterNetwork) + { + return unchecked((uint)_addr1); + } + + throw new InvalidOperationException("Only IPv4 addresses can be converted to a UInt32"); + } + + /// + /// Convert this IP address to a big-integer representation. + /// + public BigInteger ToBigInteger() + { + if (_addrFamily == AddressFamily.InterNetworkV6) + { + Span destSpan = stackalloc byte[16]; + + BinaryPrimitives.WriteUInt64BigEndian(destSpan, _addr2); + BinaryPrimitives.WriteUInt64BigEndian(destSpan.Slice(sizeof(ulong)), _addr1); + + return new BigInteger(destSpan, isUnsigned: true, isBigEndian: true); + } + else + { + return new BigInteger(_addr1); + } + } + /// /// Equals operator. /// diff --git a/tests/Enclave.FastPacket.Tests/ValueIpAddressTests.cs b/tests/Enclave.FastPacket.Tests/ValueIpAddressTests.cs index 8e3acf7..c176816 100644 --- a/tests/Enclave.FastPacket.Tests/ValueIpAddressTests.cs +++ b/tests/Enclave.FastPacket.Tests/ValueIpAddressTests.cs @@ -1,5 +1,7 @@ using FluentAssertions; +using System; using System.Net; +using System.Numerics; using Xunit; namespace Enclave.FastPacket.Tests; @@ -62,6 +64,31 @@ public void EmptyIpv6AddressNotSameAsEmptyIpv4() valueIpv6.Should().NotBe(valueIpv4); } + [Theory] + [InlineData("81.152.41.187", 1368926651u)] + [InlineData("100.154.122.4", 1687845380u)] + [InlineData("4.122.154.100", 75143780u)] + public void CanConvertToUInt(string ip, uint expected) + { + Assert.Equal(expected, ValueIpAddress.Create(IPAddress.Parse(ip)).ToUInt()); + } + + [Fact] + public void CannotConvertIpv6ToUInt() + { + Assert.Throws(() => ValueIpAddress.Create(IPAddress.Parse("2001:db8:3333:4444:5555:6666:7777:8889")).ToUInt()); + } + + [Fact] + public void CanConvertToBigInt() + { + var ip = ValueIpAddress.Create(IPAddress.Parse("2001:db8:3333:4444:5555:6666:7777:8889")); + + var expected = BigInteger.Parse("42540766427128305956041295149173016713"); + + Assert.Equal(expected, ip.ToBigInteger()); + } + [Theory] [InlineData("100.1.1.1", "101.1.1.1")] [InlineData("1.255.255.255", "2.0.0.0")]