-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split range operators of IPv4 and IPv6
- Loading branch information
Showing
6 changed files
with
586 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Collections.Generic; | ||
using System.Net; | ||
|
||
namespace NetTools.Internals | ||
{ | ||
internal class IPv4RangeOperator : IRangeOperator | ||
{ | ||
private IPAddressRange Range { get; } | ||
|
||
public IPv4RangeOperator(IPAddressRange range) | ||
{ | ||
this.Range = range; | ||
} | ||
|
||
public bool Contains(IPAddress ipaddress) | ||
{ | ||
var offset = 0; | ||
if (Range.Begin.IsIPv4MappedToIPv6 && ipaddress.IsIPv4MappedToIPv6) | ||
{ | ||
offset = 12; //ipv4 has prefix of 10 zero bytes and two 255 bytes. | ||
} | ||
|
||
var adrBytes = ipaddress.GetAddressBytes(); | ||
return Bits.LtECore(this.Range.Begin.GetAddressBytes(), adrBytes, offset) && Bits.GtECore(this.Range.End.GetAddressBytes(), adrBytes, offset); | ||
} | ||
|
||
public bool Contains(IPAddressRange range) | ||
{ | ||
var offset = 0; | ||
if (Range.Begin.IsIPv4MappedToIPv6 && range.Begin.IsIPv4MappedToIPv6) | ||
{ | ||
offset = 12; //ipv4 has prefix of 10 zero bytes and two 255 bytes. | ||
} | ||
|
||
return | ||
Bits.LtECore(Range.Begin.GetAddressBytes(), range.Begin.GetAddressBytes(), offset) && | ||
Bits.GtECore(Range.End.GetAddressBytes(), range.End.GetAddressBytes(), offset); | ||
} | ||
|
||
public IEnumerator<IPAddress> GetEnumerator() | ||
{ | ||
var first = Range.Begin.GetAddressBytes(); | ||
var last = Range.End.GetAddressBytes(); | ||
for (var ip = first; Bits.LtECore(ip, last); ip = Bits.Increment(ip)) | ||
yield return new IPAddress(ip); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Collections.Generic; | ||
using System.Net; | ||
|
||
namespace NetTools.Internals | ||
{ | ||
internal class IPv6RangeOperator : IRangeOperator | ||
{ | ||
private IPAddressRange Range { get; } | ||
|
||
public IPv6RangeOperator(IPAddressRange range) | ||
{ | ||
this.Range = range; | ||
} | ||
|
||
public bool Contains(IPAddress ipaddress) | ||
{ | ||
var offset = 0; | ||
if (Range.Begin.IsIPv4MappedToIPv6 && ipaddress.IsIPv4MappedToIPv6) | ||
{ | ||
offset = 12; //ipv4 has prefix of 10 zero bytes and two 255 bytes. | ||
} | ||
|
||
var adrBytes = ipaddress.GetAddressBytes(); | ||
return Bits.LtECore(this.Range.Begin.GetAddressBytes(), adrBytes, offset) && Bits.GtECore(this.Range.End.GetAddressBytes(), adrBytes, offset); | ||
} | ||
|
||
public bool Contains(IPAddressRange range) | ||
{ | ||
var offset = 0; | ||
if (Range.Begin.IsIPv4MappedToIPv6 && range.Begin.IsIPv4MappedToIPv6) | ||
{ | ||
offset = 12; //ipv4 has prefix of 10 zero bytes and two 255 bytes. | ||
} | ||
|
||
return | ||
Bits.LtECore(Range.Begin.GetAddressBytes(), range.Begin.GetAddressBytes(), offset) && | ||
Bits.GtECore(Range.End.GetAddressBytes(), range.End.GetAddressBytes(), offset); | ||
} | ||
|
||
public IEnumerator<IPAddress> GetEnumerator() | ||
{ | ||
var first = Range.Begin.GetAddressBytes(); | ||
var last = Range.End.GetAddressBytes(); | ||
for (var ip = first; Bits.LtECore(ip, last); ip = Bits.Increment(ip)) | ||
yield return new IPAddress(ip); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Collections.Generic; | ||
using System.Net; | ||
|
||
namespace NetTools.Internals | ||
{ | ||
internal interface IRangeOperator | ||
{ | ||
bool Contains(IPAddress ipaddress); | ||
bool Contains(IPAddressRange range); | ||
IEnumerator<IPAddress> GetEnumerator(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Net.Sockets; | ||
|
||
namespace NetTools.Internals | ||
{ | ||
internal static class RangeOperatorFactory | ||
{ | ||
public static IRangeOperator Create(IPAddressRange range) | ||
{ | ||
return range.Begin.AddressFamily == AddressFamily.InterNetwork ? | ||
new IPv4RangeOperator(range) : | ||
new IPv6RangeOperator(range) as IRangeOperator; | ||
} | ||
} | ||
} |
Oops, something went wrong.