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
Jonathan Hart
2014-09-19 10:42:27 -0700
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
b7a2ac3a1ceb38d40f4409acb38d909bbdbce8f5
b7a2ac3a
1 parent
5bcc9467
Added a contains method to IpAddress
Hide 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 @
b7a2ac3
...
...
@@ -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 @
b7a2ac3
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