Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pcap-Net
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Pcap-Net
Commits
6dbe3788
Commit
6dbe3788
authored
Jan 28, 2012
by
Brickner_cp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
DNS
parent
87a83787
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
168 additions
and
8 deletions
+168
-8
UInt128Tests.cs
PcapDotNet/src/PcapDotNet.Base.Test/UInt128Tests.cs
+84
-0
UInt128.cs
PcapDotNet/src/PcapDotNet.Base/UInt128.cs
+38
-1
DnsTests.cs
PcapDotNet/src/PcapDotNet.Packets.Test/DnsTests.cs
+7
-1
RandomDnsExtensions.cs
...t/src/PcapDotNet.Packets.TestUtils/RandomDnsExtensions.cs
+3
-3
DnsResourceDataA6.cs
.../PcapDotNet.Packets/Dns/ResourceData/DnsResourceDataA6.cs
+21
-2
IpV6Address.cs
PcapDotNet/src/PcapDotNet.Packets/IpV6/IpV6Address.cs
+6
-0
RandomExtensions.cs
PcapDotNet/src/PcapDotNet.TestUtils/RandomExtensions.cs
+9
-1
No files found.
PcapDotNet/src/PcapDotNet.Base.Test/UInt128Tests.cs
View file @
6dbe3788
...
@@ -131,6 +131,90 @@ namespace PcapDotNet.Base.Test
...
@@ -131,6 +131,90 @@ namespace PcapDotNet.Base.Test
Assert
.
AreEqual
(
expectedValue
,
value
>>
(
i
/
2
)
>>
(
i
/
2
),
i
.
ToString
());
Assert
.
AreEqual
(
expectedValue
,
value
>>
(
i
/
2
)
>>
(
i
/
2
),
i
.
ToString
());
Assert
.
AreEqual
(
expectedValue
,
value
>>
(
i
/
4
)
>>
(
i
/
4
)
>>
(
i
/
4
)
>>
(
i
/
4
),
i
.
ToString
());
Assert
.
AreEqual
(
expectedValue
,
value
>>
(
i
/
4
)
>>
(
i
/
4
)
>>
(
i
/
4
)
>>
(
i
/
4
),
i
.
ToString
());
}
}
Assert
.
AreEqual
<
UInt128
>(
value
>>
128
,
0
);
}
[
TestMethod
]
public
void
SumTest
()
{
UInt128
value1
=
0
;
UInt128
value2
=
0
;
Assert
.
AreEqual
<
UInt128
>(
0
,
value1
+
value2
);
value1
=
1
;
Assert
.
AreEqual
<
UInt128
>(
1
,
value1
+
value2
);
value2
=
1
;
Assert
.
AreEqual
<
UInt128
>(
2
,
value1
+
value2
);
value1
=
100
;
Assert
.
AreEqual
<
UInt128
>(
101
,
value1
+
value2
);
value2
=
1000
;
Assert
.
AreEqual
<
UInt128
>(
1100
,
value1
+
value2
);
value1
=
ulong
.
MaxValue
;
value2
=
0
;
Assert
.
AreEqual
(
ulong
.
MaxValue
,
value1
+
value2
);
value2
=
1
;
Assert
.
AreEqual
(
new
UInt128
(
1
,
0
),
value1
+
value2
);
value2
=
2
;
Assert
.
AreEqual
(
new
UInt128
(
1
,
1
),
value1
+
value2
);
value2
=
ulong
.
MaxValue
;
Assert
.
AreEqual
(
new
UInt128
(
1
,
ulong
.
MaxValue
-
1
),
value1
+
value2
);
value1
=
2
;
value2
=
new
UInt128
(
1000
,
ulong
.
MaxValue
);
Assert
.
AreEqual
(
new
UInt128
(
1001
,
1
),
value1
+
value2
);
value1
=
new
UInt128
(
100
,
ulong
.
MaxValue
/
2
+
1
);
value2
=
new
UInt128
(
1000
,
ulong
.
MaxValue
/
2
+
2
);
Assert
.
AreEqual
(
new
UInt128
(
1101
,
1
),
value1
+
value2
);
value1
=
new
UInt128
(
ulong
.
MaxValue
/
2
,
ulong
.
MaxValue
/
2
+
1
);
value2
=
new
UInt128
(
ulong
.
MaxValue
/
2
,
ulong
.
MaxValue
/
2
+
2
);
Assert
.
AreEqual
(
new
UInt128
(
ulong
.
MaxValue
,
1
),
value1
+
value2
);
value1
=
new
UInt128
(
ulong
.
MaxValue
/
2
+
1
,
ulong
.
MaxValue
/
2
+
1
);
value2
=
new
UInt128
(
ulong
.
MaxValue
/
2
,
ulong
.
MaxValue
/
2
+
2
);
Assert
.
AreEqual
(
new
UInt128
(
0
,
1
),
value1
+
value2
);
}
[
TestMethod
]
public
void
Substract
()
{
UInt128
value1
=
0
;
UInt128
value2
=
0
;
Assert
.
AreEqual
<
UInt128
>(
0
,
value1
-
value2
);
value1
=
1
;
Assert
.
AreEqual
<
UInt128
>(
1
,
value1
-
value2
);
value2
=
1
;
Assert
.
AreEqual
<
UInt128
>(
0
,
value1
-
value2
);
value1
=
100
;
Assert
.
AreEqual
<
UInt128
>(
99
,
value1
-
value2
);
value1
=
new
UInt128
(
1
,
0
);
value2
=
0
;
Assert
.
AreEqual
<
UInt128
>(
value1
,
value1
-
value2
);
value2
=
1
;
Assert
.
AreEqual
<
UInt128
>(
ulong
.
MaxValue
,
value1
-
value2
);
value2
=
2
;
Assert
.
AreEqual
<
UInt128
>(
ulong
.
MaxValue
-
1
,
value1
-
value2
);
value1
=
new
UInt128
(
100
,
1
);
Assert
.
AreEqual
<
UInt128
>(
new
UInt128
(
99
,
ulong
.
MaxValue
),
value1
-
value2
);
value1
=
1
;
Assert
.
AreEqual
<
UInt128
>(
UInt128
.
MaxValue
,
value1
-
value2
);
}
}
[
TestMethod
]
[
TestMethod
]
...
...
PcapDotNet/src/PcapDotNet.Base/UInt128.cs
View file @
6dbe3788
...
@@ -31,6 +31,8 @@ namespace PcapDotNet.Base
...
@@ -31,6 +31,8 @@ namespace PcapDotNet.Base
/// </summary>
/// </summary>
public
static
readonly
UInt128
Zero
=
0
;
public
static
readonly
UInt128
Zero
=
0
;
public
static
readonly
UInt128
One
=
1
;
/// <summary>
/// <summary>
/// Creates a value using two 64 bit values.
/// Creates a value using two 64 bit values.
/// </summary>
/// </summary>
...
@@ -387,6 +389,16 @@ namespace PcapDotNet.Base
...
@@ -387,6 +389,16 @@ namespace PcapDotNet.Base
return
!(
value1
==
value2
);
return
!(
value1
==
value2
);
}
}
public
static
bool
operator
<(
UInt128
value1
,
UInt128
value2
)
{
return
value1
.
Smaller
(
value2
);
}
public
static
bool
operator
>(
UInt128
value1
,
UInt128
value2
)
{
return
value2
.
Smaller
(
value1
);
}
public
static
bool
operator
<=(
UInt128
value1
,
UInt128
value2
)
public
static
bool
operator
<=(
UInt128
value1
,
UInt128
value2
)
{
{
return
!
value2
.
Smaller
(
value1
);
return
!
value2
.
Smaller
(
value1
);
...
@@ -427,7 +439,8 @@ namespace PcapDotNet.Base
...
@@ -427,7 +439,8 @@ namespace PcapDotNet.Base
/// <returns>The value after it was shifted by the given number of bits.</returns>
/// <returns>The value after it was shifted by the given number of bits.</returns>
public
static
UInt128
RightShift
(
UInt128
value
,
int
numberOfBits
)
public
static
UInt128
RightShift
(
UInt128
value
,
int
numberOfBits
)
{
{
numberOfBits
%=
128
;
if
(
numberOfBits
>=
128
)
return
Zero
;
if
(
numberOfBits
>=
64
)
if
(
numberOfBits
>=
64
)
return
new
UInt128
(
0
,
value
.
_mostSignificant
>>
(
numberOfBits
-
64
));
return
new
UInt128
(
0
,
value
.
_mostSignificant
>>
(
numberOfBits
-
64
));
if
(
numberOfBits
==
0
)
if
(
numberOfBits
==
0
)
...
@@ -473,6 +486,30 @@ namespace PcapDotNet.Base
...
@@ -473,6 +486,30 @@ namespace PcapDotNet.Base
return
new
UInt128
(
value1
.
_mostSignificant
&
value2
.
_mostSignificant
,
value1
.
_leastSignificant
&
value2
.
_leastSignificant
);
return
new
UInt128
(
value1
.
_mostSignificant
&
value2
.
_mostSignificant
,
value1
.
_leastSignificant
&
value2
.
_leastSignificant
);
}
}
public
static
UInt128
operator
+(
UInt128
value1
,
UInt128
value2
)
{
return
Sum
(
value1
,
value2
);
}
public
static
UInt128
Sum
(
UInt128
value1
,
UInt128
value2
)
{
ulong
leastSignificant
=
value1
.
_leastSignificant
+
value2
.
_leastSignificant
;
bool
overflow
=
(
leastSignificant
<
Math
.
Max
(
value1
.
_leastSignificant
,
value2
.
_leastSignificant
));
return
new
UInt128
(
value1
.
_mostSignificant
+
value2
.
_mostSignificant
+
(
ulong
)(
overflow
?
1
:
0
),
leastSignificant
);
}
public
static
UInt128
operator
-(
UInt128
value1
,
UInt128
value2
)
{
return
Substract
(
value1
,
value2
);
}
public
static
UInt128
Substract
(
UInt128
value1
,
UInt128
value2
)
{
ulong
leastSignificant
=
value1
.
_leastSignificant
-
value2
.
_leastSignificant
;
bool
overflow
=
(
leastSignificant
>
value1
.
_leastSignificant
);
return
new
UInt128
(
value1
.
_mostSignificant
-
value2
.
_mostSignificant
-
(
ulong
)(
overflow
?
1
:
0
),
leastSignificant
);
}
/// <summary>
/// <summary>
/// Returns the hash code for this instance.
/// Returns the hash code for this instance.
/// </summary>
/// </summary>
...
...
PcapDotNet/src/PcapDotNet.Packets.Test/DnsTests.cs
View file @
6dbe3788
...
@@ -415,6 +415,12 @@ namespace PcapDotNet.Packets.Test
...
@@ -415,6 +415,12 @@ namespace PcapDotNet.Packets.Test
{
{
Assert
.
AreEqual
(
types
.
Contains
((
DnsType
)
type
),
resourceData
.
IsTypePresentForOwner
((
DnsType
)
type
));
Assert
.
AreEqual
(
types
.
Contains
((
DnsType
)
type
),
resourceData
.
IsTypePresentForOwner
((
DnsType
)
type
));
}
}
TestResourceRecordIsNotCreatedWithNewLength
(
DnsType
.
NSec
,
new
DnsResourceDataNextDomainSecure
(
DnsDomainName
.
Root
,
new
DnsType
[
0
]),
-
1
);
TestResourceRecordIsNotCreatedWithNewLength
(
DnsType
.
NSec
,
new
DnsResourceDataNextDomainSecure
(
DnsDomainName
.
Root
,
new
DnsType
[
0
]),
9000
);
TestResourceRecordIsNotCreatedWithNewLength
(
DnsType
.
NSec
,
new
DnsResourceDataNextDomainSecure
(
DnsDomainName
.
Root
,
new
[]
{
DnsType
.
A
}),
-
1
);
TestResourceRecordIsNotCreatedWithNewLength
(
DnsType
.
NSec
,
new
DnsResourceDataNextDomainSecure
(
DnsDomainName
.
Root
,
new
[]
{
DnsType
.
A
,
DnsType
.
Any
}),
-
1
);
}
}
[
TestMethod
]
[
TestMethod
]
...
@@ -530,7 +536,7 @@ namespace PcapDotNet.Packets.Test
...
@@ -530,7 +536,7 @@ namespace PcapDotNet.Packets.Test
{
{
var
resourceRecord
=
new
DnsDataResourceRecord
(
DnsDomainName
.
Root
,
dnsType
,
DnsClass
.
In
,
0
,
resourceData
);
var
resourceRecord
=
new
DnsDataResourceRecord
(
DnsDomainName
.
Root
,
dnsType
,
DnsClass
.
In
,
0
,
resourceData
);
var
paddingResourceRecord
=
new
DnsDataResourceRecord
(
DnsDomainName
.
Root
,
DnsType
.
Null
,
DnsClass
.
In
,
0
,
var
paddingResourceRecord
=
new
DnsDataResourceRecord
(
DnsDomainName
.
Root
,
DnsType
.
Null
,
DnsClass
.
In
,
0
,
new
DnsResourceDataAnything
(
new
DataSegment
(
new
byte
[
100
])));
new
DnsResourceDataAnything
(
new
DataSegment
(
new
byte
[
100
+
Math
.
Abs
(
dataLengthDiff
)
])));
Packet
packet
=
PacketBuilder
.
Build
(
DateTime
.
Now
,
new
EthernetLayer
(),
new
IpV4Layer
(),
new
UdpLayer
(),
Packet
packet
=
PacketBuilder
.
Build
(
DateTime
.
Now
,
new
EthernetLayer
(),
new
IpV4Layer
(),
new
UdpLayer
(),
new
DnsLayer
new
DnsLayer
{
{
...
...
PcapDotNet/src/PcapDotNet.Packets.TestUtils/RandomDnsExtensions.cs
View file @
6dbe3788
...
@@ -240,9 +240,9 @@ namespace PcapDotNet.Packets.TestUtils
...
@@ -240,9 +240,9 @@ namespace PcapDotNet.Packets.TestUtils
case
DnsType
.
A6
:
case
DnsType
.
A6
:
byte
prefixLength
=
random
.
NextByte
(
DnsResourceDataA6
.
MaxPrefixLength
+
1
);
byte
prefixLength
=
random
.
NextByte
(
DnsResourceDataA6
.
MaxPrefixLength
+
1
);
UInt128
addressSuffixValue
=
prefixLength
==
0
UInt128
addressSuffixValue
=
random
.
NextUInt128
()
>>
prefixLength
;
?
random
.
NextUInt128
(
)
if
(
prefixLength
<
DnsResourceDataA6
.
MaxPrefixLength
)
:
random
.
NextUInt128
(((
UInt128
)
1
)
<<
(
128
-
prefixLength
));
addressSuffixValue
=
(
addressSuffixValue
>>
1
)
+
(
UInt128
.
One
<<
(
127
-
prefixLength
));
return
new
DnsResourceDataA6
(
prefixLength
,
return
new
DnsResourceDataA6
(
prefixLength
,
new
IpV6Address
(
addressSuffixValue
),
new
IpV6Address
(
addressSuffixValue
),
random
.
NextDnsDomainName
());
random
.
NextDnsDomainName
());
...
...
PcapDotNet/src/PcapDotNet.Packets/Dns/ResourceData/DnsResourceDataA6.cs
View file @
6dbe3788
...
@@ -25,9 +25,25 @@ namespace PcapDotNet.Packets.Dns
...
@@ -25,9 +25,25 @@ namespace PcapDotNet.Packets.Dns
}
}
public
const
int
ConstantPartLength
=
Offset
.
AddressSuffix
;
public
const
int
ConstantPartLength
=
Offset
.
AddressSuffix
;
public
const
int
MinimumLength
=
ConstantPartLength
+
DnsDomainName
.
RootLength
;
private
static
bool
IsAddressSuffixTooBig
(
byte
prefixLength
,
IpV6Address
addressSuffix
)
{
return
(
prefixLength
<
128
&&
(
addressSuffix
.
ToValue
()
<
(
UInt128
.
One
<<
(
127
-
prefixLength
))));
}
private
static
bool
IsAddressSuffixTooSmall
(
byte
prefixLength
,
IpV6Address
addressSuffix
)
{
return
(
prefixLength
>
0
&&
(
addressSuffix
.
ToValue
()
>=
(
UInt128
.
One
<<
(
128
-
prefixLength
))));
}
public
DnsResourceDataA6
(
byte
prefixLength
,
IpV6Address
addressSuffix
,
DnsDomainName
prefixName
)
public
DnsResourceDataA6
(
byte
prefixLength
,
IpV6Address
addressSuffix
,
DnsDomainName
prefixName
)
{
{
if
(
IsAddressSuffixTooBig
(
prefixLength
,
addressSuffix
))
throw
new
ArgumentOutOfRangeException
(
"addressSuffix"
,
string
.
Format
(
"Value is too small for prefix length {0}"
,
prefixLength
));
if
(
IsAddressSuffixTooSmall
(
prefixLength
,
addressSuffix
))
throw
new
ArgumentOutOfRangeException
(
"addressSuffix"
,
string
.
Format
(
"Value is too big for prefix length {0}"
,
prefixLength
));
PrefixLength
=
prefixLength
;
PrefixLength
=
prefixLength
;
AddressSuffix
=
addressSuffix
;
AddressSuffix
=
addressSuffix
;
PrefixName
=
prefixName
;
PrefixName
=
prefixName
;
...
@@ -70,7 +86,7 @@ namespace PcapDotNet.Packets.Dns
...
@@ -70,7 +86,7 @@ namespace PcapDotNet.Packets.Dns
}
}
internal
DnsResourceDataA6
()
internal
DnsResourceDataA6
()
:
this
(
0
,
IpV6Address
.
Zero
,
DnsDomainName
.
Root
)
:
this
(
0
,
IpV6Address
.
MaxValue
,
DnsDomainName
.
Root
)
{
{
}
}
...
@@ -90,7 +106,7 @@ namespace PcapDotNet.Packets.Dns
...
@@ -90,7 +106,7 @@ namespace PcapDotNet.Packets.Dns
internal
override
DnsResourceData
CreateInstance
(
DnsDatagram
dns
,
int
offsetInDns
,
int
length
)
internal
override
DnsResourceData
CreateInstance
(
DnsDatagram
dns
,
int
offsetInDns
,
int
length
)
{
{
if
(
length
<
ConstantPart
Length
)
if
(
length
<
Minimum
Length
)
return
null
;
return
null
;
byte
prefixLength
=
dns
[
offsetInDns
+
Offset
.
PrefixLength
];
byte
prefixLength
=
dns
[
offsetInDns
+
Offset
.
PrefixLength
];
...
@@ -106,6 +122,9 @@ namespace PcapDotNet.Packets.Dns
...
@@ -106,6 +122,9 @@ namespace PcapDotNet.Packets.Dns
offsetInDns
+=
addressSuffixLength
;
offsetInDns
+=
addressSuffixLength
;
length
-=
addressSuffixLength
;
length
-=
addressSuffixLength
;
if
(
IsAddressSuffixTooBig
(
prefixLength
,
addressSuffix
)
||
IsAddressSuffixTooSmall
(
prefixLength
,
addressSuffix
))
return
null
;
DnsDomainName
prefixName
;
DnsDomainName
prefixName
;
int
prefixNameLength
;
int
prefixNameLength
;
if
(!
DnsDomainName
.
TryParse
(
dns
,
offsetInDns
,
length
,
out
prefixName
,
out
prefixNameLength
))
if
(!
DnsDomainName
.
TryParse
(
dns
,
offsetInDns
,
length
,
out
prefixName
,
out
prefixNameLength
))
...
...
PcapDotNet/src/PcapDotNet.Packets/IpV6/IpV6Address.cs
View file @
6dbe3788
...
@@ -26,6 +26,11 @@ namespace PcapDotNet.Packets.IpV6
...
@@ -26,6 +26,11 @@ namespace PcapDotNet.Packets.IpV6
get
{
return
_zero
;
}
get
{
return
_zero
;
}
}
}
public
static
IpV6Address
MaxValue
{
get
{
return
_maxValue
;
}
}
/// <summary>
/// <summary>
/// Create an address from a 128 bit integer.
/// Create an address from a 128 bit integer.
/// 0 -> ::
/// 0 -> ::
...
@@ -149,5 +154,6 @@ namespace PcapDotNet.Packets.IpV6
...
@@ -149,5 +154,6 @@ namespace PcapDotNet.Packets.IpV6
private
readonly
UInt128
_value
;
private
readonly
UInt128
_value
;
private
static
readonly
IpV6Address
_zero
=
new
IpV6Address
(
0
);
private
static
readonly
IpV6Address
_zero
=
new
IpV6Address
(
0
);
private
static
readonly
IpV6Address
_maxValue
=
new
IpV6Address
(
UInt128
.
MaxValue
);
}
}
}
}
\ No newline at end of file
PcapDotNet/src/PcapDotNet.TestUtils/RandomExtensions.cs
View file @
6dbe3788
...
@@ -128,11 +128,19 @@ namespace PcapDotNet.TestUtils
...
@@ -128,11 +128,19 @@ namespace PcapDotNet.TestUtils
public
static
UInt128
NextUInt128
(
this
Random
random
,
UInt128
maximumValue
)
public
static
UInt128
NextUInt128
(
this
Random
random
,
UInt128
maximumValue
)
{
{
if
(
maximumValue
<=
(
UInt128
)
ulong
.
MaxValue
)
if
(
maximumValue
<=
ulong
.
MaxValue
)
return
random
.
NextULong
((
ulong
)
maximumValue
);
return
random
.
NextULong
((
ulong
)
maximumValue
);
return
new
UInt128
(
random
.
NextULong
((
ulong
)(
maximumValue
>>
64
)),
random
.
NextULong
());
return
new
UInt128
(
random
.
NextULong
((
ulong
)(
maximumValue
>>
64
)),
random
.
NextULong
());
}
}
public
static
UInt128
NextUInt128
(
this
Random
random
,
UInt128
minimumValue
,
UInt128
maximumValue
)
{
if
(
minimumValue
==
0
)
return
random
.
NextUInt128
(
maximumValue
);
return
minimumValue
+
random
.
NextUInt128
(
maximumValue
-
minimumValue
);
}
public
static
DateTime
NextDateTime
(
this
Random
random
,
DateTime
minimumValue
,
DateTime
maximumValue
)
public
static
DateTime
NextDateTime
(
this
Random
random
,
DateTime
minimumValue
,
DateTime
maximumValue
)
{
{
return
new
DateTime
(
random
.
NextLong
(
minimumValue
.
ToUniversalTime
().
Ticks
,
return
new
DateTime
(
random
.
NextLong
(
minimumValue
.
ToUniversalTime
().
Ticks
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment