Toggle navigation
Toggle navigation
This project
Loading...
Sign in
홍길동
/
onos
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
tom
2014-09-19 10:57:58 -0700
Browse Files
Options
Browse Files
Download
Plain Diff
Commit
d01f37ae7dedcabb4cc0fe302d7e8a6263f092e5
d01f37ae
2 parents
c104d281
980c1e9d
Merge remote-tracking branch 'origin/master'
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
0 deletions
utils/misc/src/main/java/org/onlab/packet/IpAddress.java
utils/misc/src/test/java/org/onlab/packet/IPAddressTest.java
utils/misc/src/main/java/org/onlab/packet/IpAddress.java
View file @
d01f37a
...
...
@@ -234,6 +234,30 @@ public final class IpAddress {
return
mask
()
!=
0
;
}
/**
* Determines whether a given address is contained within this IpAddress'
* network.
*
* @param other another IP address that could be contained in this network
* @return true if the other IP address is contained in this address'
* network, otherwise false
*/
public
boolean
contains
(
IpAddress
other
)
{
if
(
this
.
netmask
<=
other
.
netmask
)
{
// Special case where they're both /32 addresses
if
(
this
.
netmask
==
MAX_INET_MASK
)
{
return
Arrays
.
equals
(
octets
,
other
.
octets
);
}
// Mask the other address with our network mask
IpAddress
otherMasked
=
IpAddress
.
valueOf
(
other
.
octets
,
netmask
).
network
();
return
network
().
equals
(
otherMasked
);
}
return
false
;
}
@Override
public
int
hashCode
()
{
final
int
prime
=
31
;
...
...
utils/misc/src/test/java/org/onlab/packet/IPAddressTest.java
View file @
d01f37a
package
org
.
onlab
.
packet
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
java.util.Arrays
;
...
...
@@ -73,4 +74,26 @@ public class IPAddressTest {
assertTrue
(
"incorrect netmask"
,
Arrays
.
equals
(
IpAddress
.
ANY
,
ip2
.
netmask
().
toOctets
()));
}
@Test
public
void
testContains
()
{
IpAddress
slash31
=
IpAddress
.
valueOf
(
BYTES1
,
31
);
IpAddress
slash32
=
IpAddress
.
valueOf
(
BYTES1
,
32
);
IpAddress
differentSlash32
=
IpAddress
.
valueOf
(
BYTES2
,
32
);
assertTrue
(
slash31
.
contains
(
differentSlash32
));
assertFalse
(
differentSlash32
.
contains
(
slash31
));
assertTrue
(
slash31
.
contains
(
slash32
));
assertFalse
(
slash32
.
contains
(
differentSlash32
));
assertFalse
(
differentSlash32
.
contains
(
slash32
));
IpAddress
zero
=
IpAddress
.
valueOf
(
"0.0.0.0/0"
);
assertTrue
(
zero
.
contains
(
differentSlash32
));
assertFalse
(
differentSlash32
.
contains
(
zero
));
IpAddress
slash8
=
IpAddress
.
valueOf
(
"10.0.0.0/8"
);
assertTrue
(
slash8
.
contains
(
slash31
));
assertFalse
(
slash31
.
contains
(
slash8
));
}
}
...
...
Please
register
or
login
to post a comment