5 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: 
module ip =    
  open System
  open System.Net

  let intOfIp (s : string) = 
    IPAddress.Parse(s.Trim()).GetAddressBytes() 
    |> Array.rev
    |> fun e -> BitConverter.ToUInt32 (e, 0)
    
  let ipOfInt (d : uint32) = 
    BitConverter.GetBytes d
    |> Array.rev 
    |> fun e -> IPAddress e
    |> string

  let slice (d : string) (iden : string array) = 
    d.Split(iden, StringSplitOptions.None)

  let ipArrayOfIntRange start finish =
    [| for i in start .. finish -> ipOfInt i |]

  let ipsOfRange (d : string) = 
    
    let elem = slice d [|"to"; "-"; "and"|]
    let start,finish = intOfIp elem.[0], intOfIp elem.[1]
    
    ipArrayOfIntRange start finish
    
  (* "192.168.1.1/24" -> ["192.168.1.1 .. 192.168.1.254"] *)
  let ipsOfCidrs (d : string) =
    let elem  = slice d [|"/"|]
    
    let lsn x = (1 <<< 32 - x) - 1 |> (~~~)    |> uint32
    let cidr  = Array.get elem 1   |> int
    let mask  = cidr |> int        |> lsn
    let addr  = elem |> Seq.head   |> intOfIp  |> (&&&) mask
    let start,finish = addr + 1u, addr + ~~~mask - 1u

    if cidr > 30 then [| elem |> Seq.head |]
    else
      ipArrayOfIntRange start finish
namespace System
namespace System.Net
val intOfIp : s:string -> uint32

Full name: Script.ip.intOfIp
val s : string
Multiple items
val string : value:'T -> string

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

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
Multiple items
type IPAddress =
  new : newAddress:int64 -> IPAddress + 2 overloads
  member Address : int64 with get, set
  member AddressFamily : AddressFamily
  member Equals : comparand:obj -> bool
  member GetAddressBytes : unit -> byte[]
  member GetHashCode : unit -> int
  member IsIPv6LinkLocal : bool
  member IsIPv6Multicast : bool
  member IsIPv6SiteLocal : bool
  member IsIPv6Teredo : bool
  ...

Full name: System.Net.IPAddress

--------------------
IPAddress(newAddress: int64) : unit
IPAddress(address: byte []) : unit
IPAddress(address: byte [], scopeid: int64) : unit
IPAddress.Parse(ipString: string) : IPAddress
String.Trim() : string
String.Trim([<ParamArray>] trimChars: char []) : string
type Array =
  member Clone : unit -> obj
  member CopyTo : array:Array * index:int -> unit + 1 overload
  member GetEnumerator : unit -> IEnumerator
  member GetLength : dimension:int -> int
  member GetLongLength : dimension:int -> int64
  member GetLowerBound : dimension:int -> int
  member GetUpperBound : dimension:int -> int
  member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
  member Initialize : unit -> unit
  member IsFixedSize : bool
  ...

Full name: System.Array
val rev : array:'T [] -> 'T []

Full name: Microsoft.FSharp.Collections.Array.rev
val e : byte []
type BitConverter =
  static val IsLittleEndian : bool
  static member DoubleToInt64Bits : value:float -> int64
  static member GetBytes : value:bool -> byte[] + 9 overloads
  static member Int64BitsToDouble : value:int64 -> float
  static member ToBoolean : value:byte[] * startIndex:int -> bool
  static member ToChar : value:byte[] * startIndex:int -> char
  static member ToDouble : value:byte[] * startIndex:int -> float
  static member ToInt16 : value:byte[] * startIndex:int -> int16
  static member ToInt32 : value:byte[] * startIndex:int -> int
  static member ToInt64 : value:byte[] * startIndex:int -> int64
  ...

Full name: System.BitConverter
BitConverter.ToUInt32(value: byte [], startIndex: int) : uint32
val ipOfInt : d:uint32 -> string

Full name: Script.ip.ipOfInt
val d : uint32
Multiple items
val uint32 : value:'T -> uint32 (requires member op_Explicit)

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

--------------------
type uint32 = UInt32

Full name: Microsoft.FSharp.Core.uint32
BitConverter.GetBytes(value: float) : byte []
BitConverter.GetBytes(value: float32) : byte []
BitConverter.GetBytes(value: uint64) : byte []
BitConverter.GetBytes(value: uint32) : byte []
BitConverter.GetBytes(value: uint16) : byte []
BitConverter.GetBytes(value: int64) : byte []
BitConverter.GetBytes(value: int) : byte []
BitConverter.GetBytes(value: int16) : byte []
BitConverter.GetBytes(value: char) : byte []
BitConverter.GetBytes(value: bool) : byte []
val slice : d:string -> iden:string array -> string []

Full name: Script.ip.slice
val d : string
val iden : string array
type 'T array = 'T []

Full name: Microsoft.FSharp.Core.array<_>
String.Split([<ParamArray>] separator: char []) : string []
String.Split(separator: string [], options: StringSplitOptions) : string []
String.Split(separator: char [], options: StringSplitOptions) : string []
String.Split(separator: char [], count: int) : string []
String.Split(separator: string [], count: int, options: StringSplitOptions) : string []
String.Split(separator: char [], count: int, options: StringSplitOptions) : string []
type StringSplitOptions =
  | None = 0
  | RemoveEmptyEntries = 1

Full name: System.StringSplitOptions
field StringSplitOptions.None = 0
val ipArrayOfIntRange : start:uint32 -> finish:uint32 -> string []

Full name: Script.ip.ipArrayOfIntRange
val start : uint32
val finish : uint32
val i : uint32
val ipsOfRange : d:string -> string []

Full name: Script.ip.ipsOfRange
val elem : string []
val ipsOfCidrs : d:string -> string []

Full name: Script.ip.ipsOfCidrs
val lsn : (int -> uint32)
val x : int
val cidr : int
val get : array:'T [] -> index:int -> 'T

Full name: Microsoft.FSharp.Collections.Array.get
Multiple items
val int : value:'T -> int (requires member op_Explicit)

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

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val mask : uint32
val addr : uint32
module Seq

from Microsoft.FSharp.Collections
val head : source:seq<'T> -> 'T

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

More information

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