6 people like it.

ipv4 conversion snippet

ipv4 conversion snippet, updated based on current version, changed a lot to make it a bit more .NETish

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
72: 
73: 
(* IPv4 conversion snippets FTF*)
module ipv4Conversions = 

  let ip_toint (d : string) = 
      let ele = d.Split '.'
      ((uint32 ele.[0]) <<< 24) 
    + ((uint32 ele.[1]) <<< 16)    
    + ((uint32 ele.[2]) <<< 8)    
    + ((uint32 ele.[3]) )

  let int_toip d =
    let val1 = uint32(d &&& 0xff000000u) >>> 24
    let val2 = uint32(d &&& 0x00ff0000u) >>> 16
    let val3 = uint32(d &&& 0x0000ff00u) >>> 8
    let val4 = uint32(d &&& 0x000000ffu)
    sprintf "%d.%d.%d.%d" val1 val2 val3 val4
    
  (* convert range notation to a list of all ranges *)
  (* possibl replace with pattern matching for a more exhaustive match*)
  let range_toip (d : string) = 
    let range_elem = 
      if d.Contains("-") then d.Split '-'
      else (d.Replace("to"," ")).Split ' '
    
    let elems = range_elem |> Array.filter(fun x -> x.Length > 5)    
    [ip_toint elems.[0] .. ip_toint elems.[1]]
      |> Seq.map int_toip
    
  (* "192.168.1.1/24" -> ["192.168.1.1 .. 192.168.1.254"] *)
  let expand_cidr (d : string) =
    let elem = d.Split '/'
    let cidrBits = int32 elem.[1]
    let mask = uint32 ~~~((1 <<< (32 - cidrBits)) - 1)
    
    (* Network Address *)
    let nwAddress = ip_toint(elem.[0]) &&& mask
    let startAddr = (int_toip(nwAddress + 1u))
    
    (* Broadcast Address *)
    let bcAddress = nwAddress + ~~~mask
    let endinAddr = (int_toip(bcAddress - 1u))
    
    let usableIps = 
      if (cidrBits > 30) 
        then 0u
      else 
        (bcAddress - nwAddress -  1u) 
        
    if usableIps <= 0u 
      then Seq.empty
    else 
      {nwAddress + 1u .. bcAddress - 1u} |> Seq.map int_toip
      
      
(* Examples *)
(*
> ipv4Conversions.ip_toint "192.168.1.1";;
val it : uint32 = 3232235777u

> ipv4Conversions.int_toip 3232235777u;;
val it : string = "192.168.1.1"

> ipv4Conversions.range_toip "192.168.1.10 -192.168.1.20";;
val it : seq<string> =
  seq ["192.168.1.10"; "192.168.1.11"; "192.168.1.12"; "192.168.1.13"; ...]
  
> ipv4Conversions.range_toip "192.168.1.10 to 192.168.1.20";;
val it : seq<string> =
  seq ["192.168.1.10"; "192.168.1.11"; "192.168.1.12"; "192.168.1.13"; ...]
  
> ipv4Conversions.expand_cidr "192.168.1.1/24";;
val it : seq<string> =
  seq ["192.168.1.1"; "192.168.1.2"; "192.168.1.3"; "192.168.1.4"; ...]*)
val ip_toint : d:string -> uint32

Full name: Script.ipv4Conversions.ip_toint
val d : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val ele : string []
System.String.Split([<System.ParamArray>] separator: char []) : string []
System.String.Split(separator: string [], options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], count: int) : string []
System.String.Split(separator: string [], count: int, options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], count: int, options: System.StringSplitOptions) : string []
Multiple items
val uint32 : value:'T -> uint32 (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.uint32

--------------------
type uint32 = System.UInt32

Full name: Microsoft.FSharp.Core.uint32
val int_toip : d:uint32 -> string

Full name: Script.ipv4Conversions.int_toip
val d : uint32
val val1 : uint32
val val2 : uint32
val val3 : uint32
val val4 : uint32
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val range_toip : d:string -> seq<string>

Full name: Script.ipv4Conversions.range_toip
val range_elem : string []
System.String.Contains(value: string) : bool
System.String.Replace(oldValue: string, newValue: string) : string
System.String.Replace(oldChar: char, newChar: char) : string
val elems : string []
module Array

from Microsoft.FSharp.Collections
val filter : predicate:('T -> bool) -> array:'T [] -> 'T []

Full name: Microsoft.FSharp.Collections.Array.filter
val x : string
property System.String.Length: int
module Seq

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val expand_cidr : d:string -> seq<string>

Full name: Script.ipv4Conversions.expand_cidr
val elem : string []
val cidrBits : int32
Multiple items
val int32 : value:'T -> int32 (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int32

--------------------
type int32 = System.Int32

Full name: Microsoft.FSharp.Core.int32
val mask : uint32
val nwAddress : uint32
val startAddr : string
val bcAddress : uint32
val endinAddr : string
val usableIps : uint32
val empty<'T> : seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.empty
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/8K
Posted:12 years ago
Author:david klein
Tags: ipv4 , bitwise , map