Class: Yast::NetmaskClass

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) Check(netmask)

Check the netmask

Parameters:

  • netmask (String)

    network mask

Returns:

  • true if correct



106
107
108
# File '../../src/modules/Netmask.rb', line 106

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

- (Object) Check4(netmask)

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

Parameters:

  • netmask (String)

    network mask

Returns:

  • true if correct



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

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

- (Object) Check6(netmask)

Check the IPv6 netmask

Parameters:

  • netmask (String)

    network mask

Returns:

  • true if correct



94
95
96
97
98
99
100
101
# File '../../src/modules/Netmask.rb', line 94

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

- (Object) CheckPrefix4(prefix)



43
44
45
46
47
48
49
50
51
52
# File '../../src/modules/Netmask.rb', line 43

def CheckPrefix4(prefix)
  return false if prefix.nil? || prefix == ""
  # <0,32>
  if Builtins.regexpmatch(prefix, "^[0-9]+$")
    nm = Builtins.tointeger(prefix)
    return Ops.greater_or_equal(nm, 0) && Ops.less_or_equal(nm, 32)
  else
    return false
  end
end

- (Object) FromBits(bits)

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)



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
150
151
152
# File '../../src/modules/Netmask.rb', line 117

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

- (Object) main



35
36
37
38
39
40
41
# File '../../src/modules/Netmask.rb', line 35

def main
  textdomain "base"

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

- (Object) ToBits(netmask)

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



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File '../../src/modules/Netmask.rb', line 157

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