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: 
(* 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 *)
  (* Thanks for neatening this up Paks from #fsharp :) *)
  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 nAddr = ip_toint(elem.[0]) &&& mask
    
    if cidrBits > 30 then
      [d] |> List.toSeq
    else
      {nAddr + 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"; ...]*)
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 elems : string []
namespace System
type StringSplitOptions =
  | None = 0
  | RemoveEmptyEntries = 1

Full name: System.StringSplitOptions
field System.StringSplitOptions.None = 0
module Array

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

Full name: Microsoft.FSharp.Collections.Array.map
val s : string
System.String.Trim() : string
System.String.Trim([<System.ParamArray>] trimChars: char []) : string
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 nAddr : uint32
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val toSeq : list:'T list -> seq<'T>

Full name: Microsoft.FSharp.Collections.List.toSeq

More information

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