IpAddress/Network helpers
authorTomas Zeman <tzeman@volny.cz>
Sat, 01 Oct 2011 15:28:45 +0200
changeset 25 6ed154cb8b32
parent 24 57624188c7f1
child 26 5679188fa8a0
IpAddress/Network helpers
src/main/scala/net/tz/ip/IpAddress.scala
src/main/scala/net/tz/ip/IpNetwork.scala
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/net/tz/ip/IpAddress.scala	Sat Oct 01 15:28:45 2011 +0200
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2011 Tomas Zeman <tzeman@volny.cz>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.tz.ip
+
+import java.net.InetAddress
+import net.liftweb.common._
+import net.liftweb.util.Helpers._
+
+case class IpAddress(inet: InetAddress) {
+  lazy val toBigInt = BigInt(1, inet.getAddress)
+  lazy val bytes = inet.getAddress
+  lazy val v: IpVersion.Ver = IpVersion.fromLen(bytes.length)
+  override def toString = inet.getHostAddress
+}
+
+object IpAddress {
+  def apply(ip: String): Box[IpAddress] =
+    tryo { IpAddress(InetAddress getByName ip) }
+
+  def apply(a: Array[Byte]): Box[IpAddress] =
+    tryo { IpAddress(InetAddress getByAddress a) }
+
+  import IpVersion._
+  import scala.collection.mutable.ArrayBuffer
+
+  def apply(v: Ver, i: BigInt): Box[IpAddress] = {
+    val a = i.toByteArray
+    val bytes = if (a.length < v.len) {
+      (ArrayBuffer.tabulate[Byte](v.len-a.length) { _ => 0} ++ a).toArray
+    } else if (a.length == v.len + 1) {
+      a drop 1
+    } else {
+      a
+    }
+    tryo { IpAddress(InetAddress.getByAddress(bytes)) }
+  }
+}
+
+object IpVersion extends Enumeration {
+  val v4 = Ver(4, "IPv4", 4, 32, BigInt(2).pow(32) - 1)
+  val v6 = Ver(6, "IPv6", 16, 128, BigInt(2).pow(128) - 1)
+  val IPv4 = v4
+  val IPv6 = v6
+
+  def fromLen(len: Int): Ver = len match {
+    case  4 => v4
+    case 16 => v6
+    case _ => throw new NoSuchElementException("No element for length " + len)
+  }
+
+  case class Ver(val i: Int, val name: String, val len: Int,
+    val bits: Int, val ones: BigInt) extends Val(i, name)
+}
+
+// vim: set ts=2 sw=2 et:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/net/tz/ip/IpNetwork.scala	Sat Oct 01 15:28:45 2011 +0200
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2011 Tomas Zeman <tzeman@volny.cz>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.tz.ip
+
+import net.liftweb.common._
+import net.liftweb.util.Helpers._
+
+case class IpNetwork(from: IpAddress, to: IpAddress) {
+  require(from.v == to.v, "IpAddresses must be of the same version")
+
+  lazy val v = from.v
+  lazy val mask = from.v.ones ^ from.toBigInt ^ to.toBigInt
+  lazy val size = to.toBigInt - from.toBigInt + 1
+  lazy val maskLen = mask bitCount
+  lazy val maskIp = IpAddress(v, mask)
+
+  override def toString = maskIp map { _ => from + "/" + maskLen } openOr
+    (from + "-" + to)
+}
+
+object IpNetwork {
+  def apply(net: String, maskLen: Int): Box[IpNetwork] = for {
+    ip <- IpAddress(net)
+    n <- apply(ip, maskLen)
+  } yield { n }
+
+  def apply(net: IpAddress, maskLen: Int): Box[IpNetwork] = {
+    require(net.v.bits >= maskLen,
+      "MaskLen must be less or equal to IP length")
+    val hm = BigInt(2).pow(net.v.bits - maskLen) - 1 // host mask
+    val m = net.v.ones &~ hm // net mask
+    val f = net.toBigInt & m
+    val t = net.toBigInt | hm
+    for {
+      from <- IpAddress(net.v, f)
+      to <- IpAddress(net.v, t)
+    } yield { IpNetwork(from, to) }
+  }
+}
+
+// vim: set ts=2 sw=2 et: