aboutsummaryrefslogtreecommitdiff
path: root/lib/ipv4.nix
blob: c843cb6a8522e2d5b5a5ce692367ceab2263637b (plain)
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
lib:
with builtins;
with lib; rec {
  # Converts string representation of IPv4 address to 32 bits
  ip2bits = ip: let
    perBits = map (x: int2bits 8 (toInt x)) (splitString "." ip);
  in
    flatten perBits;
  # Converts 32 bits to IPv4
  bits2ip = bits: let
    bts = i: toString (bits2int (sublist (i * 8) 8 bits));
  in "${bts 0}.${bts 1}.${bts 2}.${bts 3}";

  # Convert IPv4 to number
  ip2int = ip: bits2int (ip2bits ip);
  # Convert number to IPv4
  int2ip = ip: bits2ip (int2bits 32 ip);

  # Generate bits for netmas of gitven prefix length
  netmaskBits = prefixLength: genList (x: x < prefixLength) 32;
  # Convert IP network prefix length to network mask
  prefix2netmask = prefixLength: bits2ip (netmaskBits prefixLength);
  # Mask IP by network mask specified by given network prefix length
  prefix2ip = ip: prefixLength: let
    a = netmaskBits prefixLength;
    b = ip2bits ip;
  in
    bits2ip (zipListsWith (a: b: a && b) a b);
  # Last address in the range
  prefix2broadcast = ip: prefixLength: let
    a = netmaskBits prefixLength;
    b = ip2bits ip;
  in
    bits2ip (zipListsWith (a: b: !a || b) a b);

  # Offset address in network
  ipAdd = ip: prefixLength: off: int2ip ((ip2int (prefix2ip ip prefixLength)) + off);
}