crakjie / scala-ip-ranges

Simple mini lib that allow to iterate and generate ip.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

#Scala IP Range is a light lib that allows to iterate over ips.

  • Scala IP Range doesn't use any dependency and doesn't pollute your dependencies.

#Example Instanciate a new ip.

  import ipranges._
  val myIp = v4.IP(192,0,0,2)

Build a new range.

  val myRange = v4.IP(192,0,0,1) to v4.IP(192,0,0,15)
  myRange: v4.IPRange = IPRange(192.0.0.2, 192.0.0.3, 192.0.0.4, 192.0.0.5, 192.0.0.6, 192.0.0.7, 192.0.0.8, 192.0.0.9, 192.0.0.10, 192.0.0.11, 192.0.0.12, 192.0.0.13, 192.0.0.14)

Iterate over this range is easy as one two tree.

  for(ip <- v4.IP(192,0,0,1) to v4.IP(192,0,0,255)) {
    println(ip)
  }

You can also change the steps

  val myRange = IP(192,0,0,0) to IP(192,0,15,0) by 256
  myRange: v4.IPRange = IPRange(192.0.1.0, 192.0.2.0, 192.0.3.0, 192.0.4.0, 192.0.5.0, 192.0.6.0, 192.0.7.0, 192.0.8.0, 192.0.9.0, 192.0.10.0, 192.0.11.0, 192.0.12.0, 192.0.13.0, 192.0.14.0, 192.0.15.0)

Generate a range with the subnetMask

val range : IPRange = IP(10,9,8,7) / 16
range.head  //print IP(10,9,0,0)
range.last  //print IP(10,9,255,255)

IPv6

  import ipranges._
  val myIpv6 = v6.IP(0x0, 0x0, 0xff00, 0x2442, 0x00af, 0xffff, 0xffff, 0xff42)

Syntaxic sugar, ip v6 can be express in natural way.

  import ipranges.v6._
  val myIpv6 = 0xffff::0xffff::0xffff::0xffff::0xffff::0xffff::0xffff::0xffff

It's also possible to express range with short cut.

  import ipranges.v6._
  //ipv6 private network.
  val ULA = 0xFD00::/7
  ULA ipranges.IPRange[ipranges.v6.IP] = fc00:0:0:0:0:0:0:0 to fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff by 1

#Generator The Gen object allows you to easily get random generated IP.

  Gen.randomIp()
  res1: ipranges.v4.IP = 76.78.201.206

Or generate an IP between to IP.

  Gen.randomIpBetween(IP(192,0,0,1),IP(192,0,0,255))
  res1: v4.IP = 192.0.0.201

About

Simple mini lib that allow to iterate and generate ip.


Languages

Language:Scala 100.0%