summaryrefslogtreecommitdiffstats
path: root/lib/network/default.nix
blob: 822abc9abf53580454ee38e12c5711ed22b46802 (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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
{ lib }:
let
  inherit (import ./internal.nix { inherit lib; }) _ipv6;
  inherit (lib.strings) match concatStringsSep toLower;
  inherit (lib.trivial)
    pipe
    bitXor
    fromHexString
    toHexString
    ;
  inherit (lib.lists) elemAt;
in
{
  ipv6 = {
    /**
      Creates an `IPv6Address` object from an IPv6 address as a string. If
      the prefix length is omitted, it defaults to 64. The parser is limited
      to the first two versions of IPv6 addresses addressed in RFC 4291.
      The form "x:x:x:x:x:x:d.d.d.d" is not yet implemented. Addresses are
      NOT compressed, so they are not always the same as the canonical text
      representation of IPv6 addresses defined in RFC 5952.

      # Type

      ```
      fromString :: String -> IPv6Address
      ```

      # Examples

      ```nix
      fromString "2001:DB8::ffff/32"
      => {
        address = "2001:db8:0:0:0:0:0:ffff";
        prefixLength = 32;
      }
      ```

      # Arguments

      - [addr] An IPv6 address with optional prefix length.
    */
    fromString =
      addr:
      let
        splittedAddr = _ipv6.split addr;

        addrInternal = splittedAddr.address;
        prefixLength = splittedAddr.prefixLength;

        address = _ipv6.toStringFromExpandedIp addrInternal;
      in
      {
        inherit address prefixLength;
      };

    /**
      Converts a 48-bit MAC address into a EUI-64 IPv6 address suffix.

      # Example

      ```nix
      mkEUI64Suffix "66:75:63:6B:20:75"
      => "6475:63ff:fe6b:2075"
      ```

      # Type

      ```
      mkEUI64Suffix :: String -> String
      ```

      # Inputs

      mac
      : The MAC address (may contain these delimiters: `:`, `-` or `.` but it's not necessary)
    */
    mkEUI64Suffix =
      mac:
      pipe mac [
        # match mac address
        (match "^([0-9A-Fa-f]{2})[-:.]?([0-9A-Fa-f]{2})[-:.]?([0-9A-Fa-f]{2})[-:.]?([0-9A-Fa-f]{2})[-:.]?([0-9A-Fa-f]{2})[-:.]?([0-9A-Fa-f]{2})$")

        # check if there are matches
        (
          matches:
          if matches == null then
            throw ''"${mac}" is not a valid MAC address (expected 6 octets of hex digits)''
          else
            matches
        )

        # transform to result hextets
        (octets: [
          # combine 1st and 2nd octets into first hextet, flip U/L bit, 512 = 0x200
          (toHexString (bitXor 512 (fromHexString ((elemAt octets 0) + (elemAt octets 1)))))

          # combine 3rd and 4th octets, combine them, insert fffe pattern in between to get next two hextets
          "${elemAt octets 2}ff"
          "fe${elemAt octets 3}"

          # combine 5th and 6th octets into the last hextet
          ((elemAt octets 4) + (elemAt octets 5))
        ])

        # concat to result suffix
        (concatStringsSep ":")

        toLower
      ];
  };
}