Skip to content

Commit

Permalink
IP输入检查
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Oct 17, 2022
1 parent 016f30b commit 527b63e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions WindivertDotnet/IPV4Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ private unsafe static IPAddress GetIPAddress(void* ptr)

private unsafe static void SetIPAddress(void* ptr, IPAddress value)
{
if (value.AddressFamily != AddressFamily.InterNetwork)
{
throw new ArgumentException($"AddressFamily要求必须为{AddressFamily.InterNetwork}", nameof(value));
}

var span = new Span<byte>(ptr, IPV4_SIZE);
value.TryWriteBytes(span, out _);
}
Expand Down
6 changes: 6 additions & 0 deletions WindivertDotnet/IPV6Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Buffers.Binary;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;

namespace WindivertDotnet
{
Expand Down Expand Up @@ -148,6 +149,11 @@ private unsafe static IPAddress GetIPAddress(void* ptr)

private unsafe static void SetIPAddress(void* ptr, IPAddress value)
{
if (value.AddressFamily != AddressFamily.InterNetworkV6)
{
throw new ArgumentException($"AddressFamily要求必须为{AddressFamily.InterNetworkV6}", nameof(value));
}

var span = new Span<byte>(ptr, IPV6_SIZE);
value.TryWriteBytes(span, out _);
}
Expand Down
40 changes: 40 additions & 0 deletions WindivertDotnet/WinDivertRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public unsafe class WinDivertRouter
/// WinDivert路由
/// </summary>
/// <param name="dstAddr">目标地址</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="NetworkInformationException"></exception>
/// <exception cref="NotSupportedException"></exception>
public WinDivertRouter(IPAddress dstAddr)
Expand All @@ -53,6 +54,7 @@ public WinDivertRouter(IPAddress dstAddr)
/// </summary>
/// <param name="dstAddr">目标地址</param>
/// <param name="srcAddr">源地址</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="NetworkInformationException"></exception>
/// <exception cref="NotSupportedException"></exception>
public WinDivertRouter(IPAddress dstAddr, IPAddress srcAddr)
Expand All @@ -67,6 +69,8 @@ public WinDivertRouter(IPAddress dstAddr, IPAddress srcAddr)
/// <param name="dstAddr">目标地址</param>
/// <param name="srcAddr">源地址</param>
/// <param name="interfaceIndex">网络接口索引</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <exception cref="NetworkInformationException"></exception>
/// <exception cref="NotSupportedException"></exception>
public WinDivertRouter(IPAddress dstAddr, IPAddress srcAddr, int interfaceIndex)
Expand All @@ -80,10 +84,25 @@ public WinDivertRouter(IPAddress dstAddr, IPAddress srcAddr, int interfaceIndex)
/// <param name="dstAddr">目标地址</param>
/// <param name="srcAddr">源地址</param>
/// <param name="interfaceIndex">网络接口索引</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <exception cref="NetworkInformationException"></exception>
/// <exception cref="NotSupportedException"></exception>
private WinDivertRouter(IPAddress dstAddr, IPAddress? srcAddr, int? interfaceIndex)
{
EnsureNotAny(dstAddr, nameof(dstAddr));
EnsureNotAny(srcAddr, nameof(srcAddr));

if (srcAddr != null && srcAddr.AddressFamily != dstAddr.AddressFamily)
{
throw new ArgumentException($"{srcAddr}{dstAddr}的AddressFamily不一致", nameof(srcAddr));
}

if (InterfaceIndex < 0)
{
throw new ArgumentOutOfRangeException(nameof(interfaceIndex));
}

byte* srcSockAddr = null;
if (srcAddr != null)
{
Expand Down Expand Up @@ -125,14 +144,35 @@ private WinDivertRouter(IPAddress dstAddr, IPAddress? srcAddr, int? interfaceInd
/// </summary>
/// <param name="dstAddr">目标地址</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="NetworkInformationException"></exception>
public static int GetInterfaceIndex(IPAddress dstAddr)
{
EnsureNotAny(dstAddr, nameof(dstAddr));

var dstSockAddr = stackalloc byte[SocketAddress_SIZE];
SetIPAddress(dstSockAddr, dstAddr);
return GetInterfaceIndex(dstSockAddr);
}

/// <summary>
/// 确保不是任意Ip
/// </summary>
/// <param name="address"></param>
/// <param name="name"></param>
/// <exception cref="ArgumentException"></exception>
private static void EnsureNotAny(IPAddress? address, string name)
{
if (address != null)
{
if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
{
throw new ArgumentException($"值不能为{address}", name);
}
}
}


/// <summary>
/// 获取网络接口索引
/// </summary>
Expand Down

0 comments on commit 527b63e

Please sign in to comment.