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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     1
/*
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     2
 * Copyright 2011 Tomas Zeman <tzeman@volny.cz>
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     3
 *
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     4
 * Licensed under the Apache License, Version 2.0 (the "License");
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     5
 * you may not use this file except in compliance with the License.
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     6
 * You may obtain a copy of the License at
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     7
 *
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     8
 *     http://www.apache.org/licenses/LICENSE-2.0
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     9
 *
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    10
 * Unless required by applicable law or agreed to in writing, software
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    11
 * distributed under the License is distributed on an "AS IS" BASIS,
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    13
 * See the License for the specific language governing permissions and
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    14
 * limitations under the License.
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    15
 */
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    16
package net.tz.ip
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    17
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    18
import java.net.InetAddress
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    19
import net.liftweb.common._
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    20
import net.liftweb.util.Helpers._
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    21
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    22
case class IpAddress(inet: InetAddress) {
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    23
  lazy val toBigInt = BigInt(1, inet.getAddress)
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    24
  lazy val bytes = inet.getAddress
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    25
  lazy val v: IpVersion.Ver = IpVersion.fromLen(bytes.length)
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    26
  override def toString = inet.getHostAddress
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    27
}
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    28
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    29
object IpAddress {
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    30
  def apply(ip: String): Box[IpAddress] =
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    31
    tryo { IpAddress(InetAddress getByName ip) }
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    32
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    33
  def apply(a: Array[Byte]): Box[IpAddress] =
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    34
    tryo { IpAddress(InetAddress getByAddress a) }
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    35
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    36
  import IpVersion._
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    37
  import scala.collection.mutable.ArrayBuffer
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    38
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    39
  def apply(v: Ver, i: BigInt): Box[IpAddress] = {
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    40
    val a = i.toByteArray
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    41
    val bytes = if (a.length < v.len) {
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    42
      (ArrayBuffer.tabulate[Byte](v.len-a.length) { _ => 0} ++ a).toArray
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    43
    } else if (a.length == v.len + 1) {
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    44
      a drop 1
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    45
    } else {
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    46
      a
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    47
    }
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    48
    tryo { IpAddress(InetAddress.getByAddress(bytes)) }
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    49
  }
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    50
}
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    51
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    52
object IpVersion extends Enumeration {
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    53
  val v4 = Ver(4, "IPv4", 4, 32, BigInt(2).pow(32) - 1)
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    54
  val v6 = Ver(6, "IPv6", 16, 128, BigInt(2).pow(128) - 1)
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    55
  val IPv4 = v4
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    56
  val IPv6 = v6
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    57
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    58
  def fromLen(len: Int): Ver = len match {
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    59
    case  4 => v4
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    60
    case 16 => v6
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    61
    case _ => throw new NoSuchElementException("No element for length " + len)
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    62
  }
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    63
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    64
  case class Ver(val i: Int, val name: String, val len: Int,
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    65
    val bits: Int, val ones: BigInt) extends Val(i, name)
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    66
}
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    67
6ed154cb8b32 IpAddress/Network helpers
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    68
// vim: set ts=2 sw=2 et: