Class: Yast::IPClass
- Inherits:
-
Module
- Object
- Module
- Yast::IPClass
- Defined in:
- ../../src/modules/IP.rb
Instance Method Summary (collapse)
-
- (String) BitsToIPv4(bits)
Converts 32 bit binary number to its IPv4 repserentation.
-
- (Object) Check(ip)
Check syntax of IP address.
-
- (Object) Check4(ip)
Check syntax of IPv4 address.
-
- (Object) Check6(ip)
Check syntax of IPv6 address.
-
- (Object) CheckNetwork(network)
Checks the given network entry which can be defined in several formats: - Single IPv4 or IPv6, e.g., 192.168.0.1 or 2001:db8:0::1 - IP/Netmask, e.g., 192.168.0.0/255.255.255.0 or 2001:db8:0::1/ffff:ffff::0 - IP/CIDR, e.g., 192.168.0.0/20 or 2001:db8:0::1/56.
-
- (Object) CheckNetwork4(network)
Checks the given IPv4 network entry.
-
- (Object) CheckNetwork6(network)
Checks the given IPv6 network entry.
- - (Object) CheckNetworkShared(network)
-
- (Object) ComputeBroadcast(ip, mask)
Compute IPv4 broadcast address from ip4 address and network mask.
-
- (Object) ComputeNetwork(ip, mask)
Compute IPv4 network address from ip4 address and network mask.
-
- (String) IPv4ToBits(ipv4)
Converts IPv4 into its 32 bit binary representation.
- - (Object) main
-
- (Object) reserved4(ip)
Checks if given IPv4 address is reserved by any related RFC.
-
- (String) ToHex(ip)
Converts IPv4 address from string to hex format.
-
- (Object) ToInteger(ip)
Convert IPv4 address from string to integer.
-
- (Object) ToString(ip)
Convert IPv4 address from integer to string.
-
- (Object) UndecorateIPv6(ip)
If param contains IPv6 in one of its various forms, extracts it.
-
- (String) Valid4
Describe a valid IPv4 address.
-
- (String) Valid6
Describe a valid IPv6 address.
-
- (String) ValidNetwork
Returns string of valid network definition.
Instance Method Details
- (String) BitsToIPv4(bits)
Converts 32 bit binary number to its IPv4 repserentation.
238 239 240 241 242 243 244 245 246 247 248 249 |
# File '../../src/modules/IP.rb', line 238 def BitsToIPv4(bits) if Builtins.size(bits) != 32 Builtins.y2error("Not a valid IPv4 in Bits: %1", bits) return nil end if !Builtins.regexpmatch(bits, "^[01]+$") Builtins.y2error("Not a valid IPv4 in Bits: %1", bits) return nil end ToString(bits.to_i(2)) end |
- (Object) Check(ip)
Check syntax of IP address
128 129 130 |
# File '../../src/modules/IP.rb', line 128 def Check(ip) Check4(ip) || Check6(ip) end |
- (Object) Check4(ip)
Check syntax of IPv4 address
61 62 63 64 65 |
# File '../../src/modules/IP.rb', line 61 def Check4(ip) return false if ip.nil? !Resolv::IPv4::Regex.match(ip).nil? end |
- (Object) Check6(ip)
Check syntax of IPv6 address
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File '../../src/modules/IP.rb', line 81 def Check6(ip) return false if ip.nil? res = !Resolv::IPv6::Regex.match(ip).nil? # workaround for compressed address as it is hard to check correct number # in compressed ip using regexp only if res && ip.include?("::") prefix, suffix = ip.split("::") elements = prefix.split(":") contain_ipv4 = ip.include? "." elements += suffix.split(":") if suffix max_elements = contain_ipv4 ? 6 : 7 return elements.size <= max_elements end res end |
- (Object) CheckNetwork(network)
Checks the given network entry which can be defined in several formats: - Single IPv4 or IPv6, e.g., 192.168.0.1 or 2001:db8:0::1 - IP/Netmask, e.g., 192.168.0.0/255.255.255.0 or 2001:db8:0::1/ffff:ffff::0 - IP/CIDR, e.g., 192.168.0.0/20 or 2001:db8:0::1/56
373 374 375 |
# File '../../src/modules/IP.rb', line 373 def CheckNetwork(network) CheckNetwork4(network) || CheckNetwork6(network) end |
- (Object) CheckNetwork4(network)
Checks the given IPv4 network entry.
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File '../../src/modules/IP.rb', line 272 def CheckNetwork4(network) generic_check = CheckNetworkShared(network) if !generic_check.nil? return generic_check # 192.168.0.1, 0.8.55.999 elsif Check4(network) return true # 192.168.0.0/20, 0.8.55/158 elsif Builtins.regexpmatch( network, Ops.add(Ops.add("^[", @ValidChars4), "]+/[0-9]+$") ) net_parts = Builtins.splitstring(network, "/") return Check4(Ops.get(net_parts, 0, "")) && Netmask.CheckPrefix4(Ops.get(net_parts, 1, "")) # 192.168.0.0/255.255.255.0, 0.8.55/10.258.12 elsif Builtins.regexpmatch( network, Ops.add( Ops.add(Ops.add(Ops.add("^[", @ValidChars4), "]+/["), @ValidChars4), "]+$" ) ) net_parts = Builtins.splitstring(network, "/") return Check4(Ops.get(net_parts, 0, "")) && Netmask.Check4(Ops.get(net_parts, 1, "")) end false end |
- (Object) CheckNetwork6(network)
Checks the given IPv6 network entry.
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File '../../src/modules/IP.rb', line 315 def CheckNetwork6(network) generic_check = CheckNetworkShared(network) if !generic_check.nil? return generic_check # 2001:db8:0::1 elsif Check6(network) return true # 2001:db8:0::1/64 elsif Builtins.regexpmatch( network, Ops.add( Ops.add( Ops.add(Ops.add("^[", @ValidChars6), "]+/["), Netmask.ValidChars6 ), "]+$" ) ) net_parts = Builtins.splitstring(network, "/") return Check6(Ops.get(net_parts, 0, "")) && Netmask.Check6(Ops.get(net_parts, 1, "")) # 2001:db8:0::1/ffff:ffff::0 elsif Builtins.regexpmatch( network, Ops.add( Ops.add(Ops.add(Ops.add("^[", @ValidChars6), "]+/["), @ValidChars6), "]+$" ) ) net_parts = Builtins.splitstring(network, "/") return Check6(Ops.get(net_parts, 0, "")) && Check6(Ops.get(net_parts, 1, "")) end false end |
- (Object) CheckNetworkShared(network)
251 252 253 254 255 256 257 258 259 260 261 |
# File '../../src/modules/IP.rb', line 251 def CheckNetworkShared(network) if network.nil? || network == "" return false # all networks elsif network == "0/0" return true end nil end |
- (Object) ComputeBroadcast(ip, mask)
Compute IPv4 broadcast address from ip4 address and network mask.
The broadcast address is the highest address of network address range.
200 201 202 203 204 205 206 |
# File '../../src/modules/IP.rb', line 200 def ComputeBroadcast(ip, mask) i = ToInteger(ip) m = ToInteger(mask) ToString( Ops.bitwise_and(Ops.bitwise_or(i, Ops.bitwise_not(m)), 4_294_967_295) ) end |
- (Object) ComputeNetwork(ip, mask)
Compute IPv4 network address from ip4 address and network mask.
188 189 190 191 192 |
# File '../../src/modules/IP.rb', line 188 def ComputeNetwork(ip, mask) i = ToInteger(ip) m = ToInteger(mask) ToString(Ops.bitwise_and(Ops.bitwise_and(i, m), 4_294_967_295)) end |
- (String) IPv4ToBits(ipv4)
Converts IPv4 into its 32 bit binary representation.
218 219 220 221 222 223 224 225 226 |
# File '../../src/modules/IP.rb', line 218 def IPv4ToBits(ipv4) unless Check4(ipv4) Builtins.y2error("Not a valid IPv4: %1", ipv4) return nil end bits = ToInteger(ipv4).to_s(2) "0" * (32 - bits.size) + bits end |
- (Object) main
36 37 38 39 40 41 42 43 44 45 46 47 |
# File '../../src/modules/IP.rb', line 36 def main textdomain "base" Yast.import "Netmask" @ValidChars = "0123456789abcdefABCDEF.:" @ValidChars4 = "0123456789." @ValidChars6 = "0123456789abcdefABCDEF:" # helper list, each bit has its decimal representation @bit_weight_row = [128, 64, 32, 16, 8, 4, 2, 1] end |
- (Object) reserved4(ip)
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File '../../src/modules/IP.rb', line 386 def reserved4(ip) raise "Invalid IP address passed '#{ip}'" unless Check4(ip) # RFC#1700 return true if ip.start_with?("0.") # RFC#6598 return true if private_carrier_grade_nat?(ip) # RFC#5735 return true if ip.start_with?("127.") return true if ip.start_with?("169.254") # RFC#1918 return true if private_network?(ip) # RFC#6333 return true if ds_lite_address?(ip) # RFC#5737 return true if ip.start_with?("192.0.2.") return true if ip.start_with?("198.51.100.") return true if ip.start_with?("203.0.113.") # RFC#3068 return true if ip.start_with?("192.88.99.") # RFC#2544 return true if ip.start_with?("192.18.") return true if ip.start_with?("192.19.") # all from 224. is covered by RFC#5771 and RFC#5735 return true if (224..255).include?(ip.split(".").first.to_i) false end |
- (String) ToHex(ip)
Converts IPv4 address from string to hex format
175 176 177 178 179 180 181 182 |
# File '../../src/modules/IP.rb', line 175 def ToHex(ip) int = ToInteger(ip) return nil unless int hex = int.to_s(16) ("0" * (8 - hex.size)) + hex.upcase end |
- (Object) ToInteger(ip)
Convert IPv4 address from string to integer
152 153 154 155 156 157 |
# File '../../src/modules/IP.rb', line 152 def ToInteger(ip) return nil unless Check4(ip) parts = ip.split(".") parts.reduce(0) { |a, e| (a << 8) + e.to_i } end |
- (Object) ToString(ip)
Convert IPv4 address from integer to string
162 163 164 165 166 167 168 |
# File '../../src/modules/IP.rb', line 162 def ToString(ip) parts = [16_777_216, 65_536, 256, 1].map do |b| (ip / b) & 255 end parts.join(".") end |
- (Object) UndecorateIPv6(ip)
If param contains IPv6 in one of its various forms, extracts it.
if ip is closed in [ ] or contain % then it can be special case of IPv6 syntax, so extract ipv6 (see description later) and continue with check.
IPv6 syntax: - pure ipv6 blob (e.g. f008::1) - ipv6 blob with link local suffix (e.g. f008::1%eth0) - dtto in square brackets (e.g. [f008::1%eth0] )
112 113 114 115 116 117 118 119 120 121 122 123 |
# File '../../src/modules/IP.rb', line 112 def UndecorateIPv6(ip) if Builtins.regexpmatch(ip, "^\\[.*\\]") || Builtins.regexpmatch(ip, "^[^][%]+(%[^][%]+){0,1}$") ip = Builtins.regexpsub( ip, "^\\[?([^][%]+)(%[^][%]+){0,1}(\\]|$)", "\\1" ) end ip end |
- (String) Valid4
Describe a valid IPv4 address
51 52 53 54 55 56 |
# File '../../src/modules/IP.rb', line 51 def Valid4 # Translators: dot: "." _( "A valid IPv4 address consists of four integers\nin the range 0-255 separated by dots." ) end |
- (String) Valid6
Describe a valid IPv6 address
69 70 71 72 73 74 75 76 |
# File '../../src/modules/IP.rb', line 69 def Valid6 # Translators: colon: ":" _( "A valid IPv6 address consists of up to eight\n" \ "hexadecimal numbers in the range 0 - FFFF separated by colons.\n" \ "It can contain up to one double colon." ) end |
- (String) ValidNetwork
Returns string of valid network definition. Both IPv4 and IPv6.
136 137 138 139 140 141 142 143 144 145 146 147 |
# File '../../src/modules/IP.rb', line 136 def ValidNetwork # TRANSLATORS: description of the valid network definition _( "A valid network definition can contain the IP,\n" \ "IP/Netmask, IP/Netmask_Bits, or 0/0 for all networks.\n" \ "\n" \ "Examples:\n" \ "IP: 192.168.0.1 or 2001:db8:0::1\n" \ "IP/Netmask: 192.168.0.0/255.255.255.0 or 2001:db8:0::1/56\n" \ "IP/Netmask_Bits: 192.168.0.0/24 or 192.168.0.1/32 or 2001:db8:0::1/ffff::0\n" ) end |