src/main/scala/net/tz/ip/IpAddress.scala
author Tomas Zeman <tzeman@volny.cz>
Sat, 01 Oct 2011 15:28:45 +0200
changeset 25 6ed154cb8b32
permissions -rw-r--r--
IpAddress/Network helpers

/*
 * 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: