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: 
(* IPv4 conversion snippets FTF*)
open System.Net
open System.Text.RegularExpressions

let args = System.Environment.GetCommandLineArgs()

module ipv4Convert = 

  let ip_toint (d : string) = 
    d.Split '.' 
    |> Seq.map uint32 
    |> Seq.map2 (fun bits ele -> ele <<< bits) [ 24; 16; 8; 0 ] 
    |> Seq.sum

  let int_toip d =
    let vals = 
      [| 24; 16; 8; 0 |] 
      |> Array.map(fun bits -> (d &&& (0xffu <<< bits)) >>> bits)
    
    sprintf "%d.%d.%d.%d" vals.[0] vals.[1] vals.[2] vals.[3]

  (* convert range notation to a list of all ranges *)
  let range_toip (d : string) = 
  
    let elems = 
      d.Split([|"-";"to"|], System.StringSplitOptions.None) 
      |> Array.map (fun s -> s.Trim()) 
      
    { 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)
    let nwAddress = ip_toint(elem.[0]) &&& mask
    
    if cidrBits > 30 then
      Seq.empty
    else
      {nwAddress + 1u .. (nAddr + ~~~mask) - 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"; ...]*)
namespace System
namespace System.Net
namespace System.Text
namespace System.Text.RegularExpressions
val args : string []

Full name: Script.args
type Environment =
  static member CommandLine : string
  static member CurrentDirectory : string with get, set
  static member Exit : exitCode:int -> unit
  static member ExitCode : int with get, set
  static member ExpandEnvironmentVariables : name:string -> string
  static member FailFast : message:string -> unit + 1 overload
  static member GetCommandLineArgs : unit -> string[]
  static member GetEnvironmentVariable : variable:string -> string + 1 overload
  static member GetEnvironmentVariables : unit -> IDictionary + 1 overload
  static member GetFolderPath : folder:SpecialFolder -> string + 1 overload
  ...
  nested type SpecialFolder
  nested type SpecialFolderOption

Full name: System.Environment
System.Environment.GetCommandLineArgs() : string []
module ipv4Convert

from Script
val ip_toint : d:string -> uint32

Full name: Script.ipv4Convert.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
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 []
module Seq

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

Full name: Microsoft.FSharp.Collections.Seq.map
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 map2 : mapping:('T1 -> 'T2 -> 'U) -> source1:seq<'T1> -> source2:seq<'T2> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map2
val bits : int32
val ele : uint32
val sum : source:seq<'T> -> 'T (requires member ( + ) and member get_Zero)

Full name: Microsoft.FSharp.Collections.Seq.sum
val int_toip : d:uint32 -> string

Full name: Script.ipv4Convert.int_toip
val d : uint32
val vals : uint32 []
module Array

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
val bits : int
val sprintf : format:Printf.StringFormat<'T> -> 'T

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

Full name: Script.ipv4Convert.range_toip
val elems : string []
type StringSplitOptions =
  | None = 0
  | RemoveEmptyEntries = 1

Full name: System.StringSplitOptions
field System.StringSplitOptions.None = 0
val s : string
System.String.Trim() : string
System.String.Trim([<System.ParamArray>] trimChars: char []) : string
val expand_cidr : d:string -> seq<string>

Full name: Script.ipv4Convert.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 empty<'T> : seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.empty

More information

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