src/main/scala/net/tz/ip/IpAddress.scala
changeset 25 6ed154cb8b32
equal deleted inserted replaced
24:57624188c7f1 25:6ed154cb8b32
       
     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 java.net.InetAddress
       
    19 import net.liftweb.common._
       
    20 import net.liftweb.util.Helpers._
       
    21 
       
    22 case class IpAddress(inet: InetAddress) {
       
    23   lazy val toBigInt = BigInt(1, inet.getAddress)
       
    24   lazy val bytes = inet.getAddress
       
    25   lazy val v: IpVersion.Ver = IpVersion.fromLen(bytes.length)
       
    26   override def toString = inet.getHostAddress
       
    27 }
       
    28 
       
    29 object IpAddress {
       
    30   def apply(ip: String): Box[IpAddress] =
       
    31     tryo { IpAddress(InetAddress getByName ip) }
       
    32 
       
    33   def apply(a: Array[Byte]): Box[IpAddress] =
       
    34     tryo { IpAddress(InetAddress getByAddress a) }
       
    35 
       
    36   import IpVersion._
       
    37   import scala.collection.mutable.ArrayBuffer
       
    38 
       
    39   def apply(v: Ver, i: BigInt): Box[IpAddress] = {
       
    40     val a = i.toByteArray
       
    41     val bytes = if (a.length < v.len) {
       
    42       (ArrayBuffer.tabulate[Byte](v.len-a.length) { _ => 0} ++ a).toArray
       
    43     } else if (a.length == v.len + 1) {
       
    44       a drop 1
       
    45     } else {
       
    46       a
       
    47     }
       
    48     tryo { IpAddress(InetAddress.getByAddress(bytes)) }
       
    49   }
       
    50 }
       
    51 
       
    52 object IpVersion extends Enumeration {
       
    53   val v4 = Ver(4, "IPv4", 4, 32, BigInt(2).pow(32) - 1)
       
    54   val v6 = Ver(6, "IPv6", 16, 128, BigInt(2).pow(128) - 1)
       
    55   val IPv4 = v4
       
    56   val IPv6 = v6
       
    57 
       
    58   def fromLen(len: Int): Ver = len match {
       
    59     case  4 => v4
       
    60     case 16 => v6
       
    61     case _ => throw new NoSuchElementException("No element for length " + len)
       
    62   }
       
    63 
       
    64   case class Ver(val i: Int, val name: String, val len: Int,
       
    65     val bits: Int, val ones: BigInt) extends Val(i, name)
       
    66 }
       
    67 
       
    68 // vim: set ts=2 sw=2 et: