WIP
This commit is contained in:
parent
cfef14fe14
commit
cf0b3b81ac
47
CIDR.cs
47
CIDR.cs
|
@ -12,6 +12,7 @@ using System.Net;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace ln.types
|
namespace ln.types
|
||||||
{
|
{
|
||||||
|
@ -34,7 +35,7 @@ namespace ln.types
|
||||||
return new CIDR(ip, w);
|
return new CIDR(ip, w);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new CIDR(ip);
|
return new CIDR(ip,32);
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly uint _ip;
|
private readonly uint _ip;
|
||||||
|
@ -42,6 +43,7 @@ namespace ln.types
|
||||||
|
|
||||||
public int MaskWidth => (int)getNetWidth(_netmask);
|
public int MaskWidth => (int)getNetWidth(_netmask);
|
||||||
public CIDR Network => new CIDR(_ip & _netmask, _netmask);
|
public CIDR Network => new CIDR(_ip & _netmask, _netmask);
|
||||||
|
public CIDR Host => new CIDR(_ip, 0xffffffff);
|
||||||
|
|
||||||
public byte[] IPBytes
|
public byte[] IPBytes
|
||||||
{
|
{
|
||||||
|
@ -77,11 +79,11 @@ namespace ln.types
|
||||||
this._netmask = maskFromWidth(maskwidth);
|
this._netmask = maskFromWidth(maskwidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CIDR(IPAddress ip, uint maskwidth)
|
//public CIDR(IPAddress ip, uint maskwidth)
|
||||||
{
|
//{
|
||||||
_ip = BitConverter.ToUInt32(ip.GetAddressBytes().Reverse().ToArray(), 0);
|
// _ip = BitConverter.ToUInt32(ip.GetAddressBytes().Reverse().ToArray(), 0);
|
||||||
_netmask = maskFromWidth(maskwidth);
|
// _netmask = maskFromWidth(maskwidth);
|
||||||
}
|
//}
|
||||||
|
|
||||||
public CIDR(IPAddress ip, IPAddress mask)
|
public CIDR(IPAddress ip, IPAddress mask)
|
||||||
{
|
{
|
||||||
|
@ -134,11 +136,11 @@ namespace ln.types
|
||||||
|
|
||||||
public static implicit operator IPAddress(CIDR cidr)
|
public static implicit operator IPAddress(CIDR cidr)
|
||||||
{
|
{
|
||||||
return new IPAddress(cidr._ip);
|
return new IPAddress( cidr.IPBytes );
|
||||||
}
|
}
|
||||||
public static implicit operator CIDR(IPAddress iPAddress)
|
public static implicit operator CIDR(IPAddress iPAddress)
|
||||||
{
|
{
|
||||||
return new CIDR(iPAddress);
|
return new CIDR(BitConverter.ToUInt32(iPAddress.GetAddressBytes().Reverse().ToArray(),0),0xFFFFFFFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Contains(CIDR you)
|
public bool Contains(CIDR you)
|
||||||
|
@ -203,5 +205,34 @@ namespace ln.types
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static bool ___init = ____init();
|
||||||
|
static bool ____init()
|
||||||
|
{
|
||||||
|
List<JsonConverter> converters = new List<JsonConverter>();
|
||||||
|
converters.Add(new CIDRJsonConverter());
|
||||||
|
JsonConvert.DefaultSettings = () => new JsonSerializerSettings { Converters = converters };
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CIDRJsonConverter : JsonConverter
|
||||||
|
{
|
||||||
|
public override bool CanConvert(Type objectType)
|
||||||
|
{
|
||||||
|
return (objectType == typeof(CIDR));
|
||||||
|
}
|
||||||
|
|
||||||
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||||
|
{
|
||||||
|
return CIDR.Parse(reader.ReadAsString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||||
|
{
|
||||||
|
writer.WriteValue((value as CIDR).ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,9 @@
|
||||||
<Reference Include="System.Xml.Serialization" />
|
<Reference Include="System.Xml.Serialization" />
|
||||||
<Reference Include="System.Runtime.Serialization" />
|
<Reference Include="System.Runtime.Serialization" />
|
||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
|
<Reference Include="Newtonsoft.Json">
|
||||||
|
<HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
@ -58,5 +61,8 @@
|
||||||
<Name>ln.logging</Name>
|
<Name>ln.logging</Name>
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="packages.config" />
|
||||||
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||||
</Project>
|
</Project>
|
4
packages.config
Normal file
4
packages.config
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<packages>
|
||||||
|
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net47" />
|
||||||
|
</packages>
|
|
@ -50,8 +50,12 @@ namespace ln.types.serialize
|
||||||
{
|
{
|
||||||
ListReplacement listReplacement = (ListReplacement)replacement;
|
ListReplacement listReplacement = (ListReplacement)replacement;
|
||||||
|
|
||||||
object list = Activator.CreateInstance(typeof(List<>).MakeGenericType(new Type[] { listReplacement.elementType }), new object[] { listReplacement.elements });
|
IList ilist = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(new Type[] { listReplacement.elementType }));
|
||||||
return list;
|
|
||||||
|
foreach (object e in listReplacement.elements)
|
||||||
|
ilist.Add(e);
|
||||||
|
|
||||||
|
return ilist;
|
||||||
|
|
||||||
} else if (replacement is DictionaryReplacement)
|
} else if (replacement is DictionaryReplacement)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue