|
25
|
1 |
/*
|
|
|
2 |
* Copyright 2011 Tomas Zeman <tzeman@volny.cz>
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
package net.tz.ip
|
|
|
17 |
|
|
|
18 |
import net.liftweb.common._
|
|
|
19 |
import net.liftweb.util.Helpers._
|
|
|
20 |
|
|
|
21 |
case class IpNetwork(from: IpAddress, to: IpAddress) {
|
|
|
22 |
require(from.v == to.v, "IpAddresses must be of the same version")
|
|
|
23 |
|
|
|
24 |
lazy val v = from.v
|
|
|
25 |
lazy val mask = from.v.ones ^ from.toBigInt ^ to.toBigInt
|
|
|
26 |
lazy val size = to.toBigInt - from.toBigInt + 1
|
|
|
27 |
lazy val maskLen = mask bitCount
|
|
|
28 |
lazy val maskIp = IpAddress(v, mask)
|
|
|
29 |
|
|
|
30 |
override def toString = maskIp map { _ => from + "/" + maskLen } openOr
|
|
|
31 |
(from + "-" + to)
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
object IpNetwork {
|
|
|
35 |
def apply(net: String, maskLen: Int): Box[IpNetwork] = for {
|
|
|
36 |
ip <- IpAddress(net)
|
|
|
37 |
n <- apply(ip, maskLen)
|
|
|
38 |
} yield { n }
|
|
|
39 |
|
|
|
40 |
def apply(net: IpAddress, maskLen: Int): Box[IpNetwork] = {
|
|
|
41 |
require(net.v.bits >= maskLen,
|
|
|
42 |
"MaskLen must be less or equal to IP length")
|
|
|
43 |
val hm = BigInt(2).pow(net.v.bits - maskLen) - 1 // host mask
|
|
|
44 |
val m = net.v.ones &~ hm // net mask
|
|
|
45 |
val f = net.toBigInt & m
|
|
|
46 |
val t = net.toBigInt | hm
|
|
|
47 |
for {
|
|
|
48 |
from <- IpAddress(net.v, f)
|
|
|
49 |
to <- IpAddress(net.v, t)
|
|
|
50 |
} yield { IpNetwork(from, to) }
|
|
|
51 |
}
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
// vim: set ts=2 sw=2 et:
|