Class: Yast::NetmaskClass

Inherits:
Module
  • Object
show all
Defined in:
library/types/src/modules/Netmask.rb

Instance Method Summary collapse

Instance Method Details

#Check(netmask) ⇒ Object

Check the netmask

Parameters:

  • netmask (String)

    network mask

Returns:

  • true if correct


103
104
105
# File 'library/types/src/modules/Netmask.rb', line 103

def Check(netmask)
  Check4(netmask) || Check6(netmask)
end

#Check4(netmask) ⇒ Object

Check the IPv4 netmask Note that 0.0.0.0 is not a correct netmask.

Parameters:

  • netmask (String)

    network mask

Returns:

  • true if correct


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
# File 'library/types/src/modules/Netmask.rb', line 54

def Check4(netmask)
  return false if netmask.nil? || netmask == ""

  # 255.255.240.0
  s1 = "(128|192|224|240|248|252|254|255)"
  nm = Ops.add(
    Ops.add(
      Ops.add(
        Ops.add(
          Ops.add(
            Ops.add(
              Ops.add(
                Ops.add(
                  Ops.add(Ops.add(Ops.add("^(", s1), ".0.0.0|"), "255."),
                  s1
                ),
                ".0.0|"
              ),
              "255.255."
            ),
            s1
          ),
          ".0|"
        ),
        "255.255.255."
      ),
      s1
    ),
    ")$"
  )
  Builtins.regexpmatch(netmask, nm)
end

#Check6(netmask) ⇒ Object

Check the IPv6 netmask

Parameters:

  • netmask (String)

    network mask

Returns:

  • true if correct


90
91
92
93
94
95
96
97
98
# File 'library/types/src/modules/Netmask.rb', line 90

def Check6(netmask)
  return false if netmask.nil? || netmask == ""

  # <0,256>
  return false if !Builtins.regexpmatch(netmask, "^[0-9]+$")

  nm = Builtins.tointeger(netmask)
  Ops.greater_or_equal(nm, 0) && Ops.less_or_equal(nm, 256)
end

#CheckPrefix4(prefix) ⇒ Object


41
42
43
44
45
46
47
48
# File 'library/types/src/modules/Netmask.rb', line 41

def CheckPrefix4(prefix)
  return false if prefix.nil? || prefix == ""
  # <0,32>
  return false unless Builtins.regexpmatch(prefix, "^[0-9]+$")

  nm = Builtins.tointeger(prefix)
  Ops.greater_or_equal(nm, 0) && Ops.less_or_equal(nm, 32)
end

#FromBits(bits) ⇒ Object

Convert netmask in bits form (20) to IPv4 netmask string (255.255.240.0)

Parameters:

  • bits

    number of bits in netmask

Returns:

  • netmask string or empty string in case of invalid bits (e.g. when prefix is incompatible with IPv4)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'library/types/src/modules/Netmask.rb', line 114

def FromBits(bits)
  return "" unless bits.between?(0, 32)

  b = Ops.divide(bits, 8)
  d = Ops.modulo(bits, 8)

  l = {
    1 => "255.",
    2 => "255.255.",
    3 => "255.255.255.",
    4 => "255.255.255.255"
  }
  r = { 3 => "0", 2 => "0.0", 1 => "0.0.0", 0 => "0.0.0.0" }
  m = {
    1 => "128",
    2 => "192",
    3 => "224",
    4 => "240",
    5 => "248",
    6 => "252",
    7 => "254",
    8 => "255"
  }

  Ops.add(
    Ops.add(
      Ops.get_string(l, b, ""),
      if d != 0
        Ops.add(Ops.get_string(m, d, ""), (b != 3) ? "." : "")
      else
        ""
      end
    ),
    Ops.get_string(r, (d == 0) ? b : Ops.add(b, 1), "")
  )
end

#mainObject


33
34
35
36
37
38
39
# File 'library/types/src/modules/Netmask.rb', line 33

def main
  textdomain "base"

  @ValidChars = "0123456789."
  @ValidChars4 = "0123456789."
  @ValidChars6 = "0123456789"
end

#ToBits(netmask) ⇒ Object

Convert IPv4 netmask as string (255.255.240.0) to bits form (20)

Parameters:

  • netmask (String)

    netmask as string

Returns:

  • number of bits in netmask; 32 for empty string


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'library/types/src/modules/Netmask.rb', line 154

def ToBits(netmask)
  return 32 if netmask == ""

  bits = 0
  m = {
    "128" => 1,
    "192" => 2,
    "224" => 3,
    "240" => 4,
    "248" => 5,
    "252" => 6,
    "254" => 7,
    "255" => 8
  }
  Builtins.maplist(Builtins.splitstring(netmask, ".")) do |i|
    bits = Ops.add(bits, Ops.get_integer(m, i, 0))
  end
  bits
end