Skip to content

Commit

Permalink
add FragmentFlag
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Oct 28, 2022
1 parent e1b3b25 commit 0c6ab3c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
11 changes: 7 additions & 4 deletions WindivertDotnet.Test/IPHeaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public unsafe void IPV4ReadTest()
Assert.Equal(packet.TimeToLive, header.TTL);
Assert.Equal((byte)packet.Protocol, (byte)header.Protocol);
Assert.Equal(packet.SourceAddress, header.SrcAddr);
Assert.Equal(packet.DestinationAddress, header.DstAddr);
Assert.Equal(packet.DestinationAddress, header.DstAddr);
}
}

Expand All @@ -36,12 +36,13 @@ public unsafe void IPV4WriteTest()
DstAddr = dstAddr,
SrcAddr = srcAddr,
TOS = 3,
FragOff0 = 4,
HdrLength = 5,
Id = 6,
Length = 7,
Protocol = ProtocolType.Ggp,
TTL = 8
TTL = 8,
FragmentFlags = FragmentFlag.MoreFragments,
FragmentOffset = 9,
};


Expand All @@ -51,13 +52,15 @@ public unsafe void IPV4WriteTest()
Assert.Equal(dstAddr, header.DstAddr);
Assert.Equal(srcAddr, header.SrcAddr);
Assert.Equal(3, header.TOS);
Assert.Equal(4, header.FragOff0);
Assert.Equal(5, header.HdrLength);
Assert.Equal(6, header.Id);

Assert.Equal(7, header.Length);
Assert.Equal(ProtocolType.Ggp, header.Protocol);
Assert.Equal(8, header.TTL);

Assert.Equal(FragmentFlag.MoreFragments, header.FragmentFlags);
Assert.Equal(9, header.FragmentOffset);
}

[Fact]
Expand Down
26 changes: 26 additions & 0 deletions WindivertDotnet/FragmentFlag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;

namespace WindivertDotnet
{
/// <summary>
/// IPv4标记位
/// </summary>
[Flags]
public enum FragmentFlag : ushort
{
/// <summary>
/// 有分片
/// </summary>
MoreFragments = 0x1,

/// <summary>
/// 无分片
/// </summary>
DontFragment = 0x02,

/// <summary>
/// 保护
/// </summary>
Reserved = 0x04,
}
}
45 changes: 45 additions & 0 deletions WindivertDotnet/IPV4Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,51 @@ public ushort FragOff0
set => fragOff0 = BinaryPrimitives.ReverseEndianness(value);
}

/// <summary>
/// 标记位
/// </summary>
public FragmentFlag FragmentFlags
{
get => (FragmentFlag)MoveRight(fragOff0, 13);
set => fragOff0 = (ushort)(MoveLeft((ushort)value, 13) | MoveLeftRight(fragOff0, 3));
}

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private ushort FragmentOffsetRaw
{
get => MoveLeftRight(fragOff0, 3);
set => fragOff0 = (ushort)(MoveRightLeft(fragOff0, 13) | MoveLeftRight(value, 3));
}

/// <summary>
/// 分片偏移量13位
/// </summary>
public ushort FragmentOffset
{
get => BinaryPrimitives.ReverseEndianness(this.FragmentOffsetRaw);
set => this.FragmentOffsetRaw = BinaryPrimitives.ReverseEndianness(value);
}

private static ushort MoveLeft(ushort value, byte count)
{
return (ushort)(value << count);
}

private static ushort MoveRight(ushort value, byte count)
{
return (ushort)(value >> count);
}

private static ushort MoveLeftRight(ushort value, byte count)
{
return MoveRight(MoveLeft(value, count), count);
}

private static ushort MoveRightLeft(ushort value, byte count)
{
return MoveLeft(MoveRight(value, count), count);
}

/// <summary>
/// 获取或设置生存时间
/// </summary>
Expand Down

0 comments on commit 0c6ab3c

Please sign in to comment.