Class: Yast::IPClass
- Inherits:
-
Module
- Object
- Module
- Yast::IPClass
- Includes:
- Logger
- Defined in:
- library/types/src/modules/IP.rb
Instance Method Summary collapse
-
#BitsToIPv4(bits) ⇒ String
Converts 32 bit binary number to its IPv4 representation.
-
#Check(ip) ⇒ Object
Check syntax of IP address.
-
#Check4(ip) ⇒ Object
Check syntax of IPv4 address.
-
#Check6(ip) ⇒ Object
Check syntax of IPv6 address.
-
#CheckNetwork(network) ⇒ Object
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.
-
#CheckNetwork4(network) ⇒ Object
Checks the given IPv4 network entry.
-
#CheckNetwork6(network) ⇒ Object
Checks the given IPv6 network entry.
- #CheckNetworkShared(network) ⇒ Object
-
#ComputeBroadcast(ip, mask) ⇒ Object
Compute IPv4 broadcast address from ip4 address and network mask.
-
#ComputeNetwork(ip, mask) ⇒ Object
Compute IPv4 network address from ip4 address and network mask.
-
#IPv4ToBits(ipv4) ⇒ String
Converts IPv4 into its 32 bit binary representation.
- #main ⇒ Object
-
#reserved4(ip) ⇒ Object
Checks if given IPv4 address is reserved by any related RFC.
-
#ToHex(ip) ⇒ String
Converts IPv4 address from string to hex format.
-
#ToInteger(ip) ⇒ Object
Convert IPv4 address from string to integer.
-
#ToString(ip) ⇒ Object
Convert IPv4 address from integer to string.
-
#UndecorateIPv6(ip) ⇒ Object
If param contains IPv6 in one of its various forms, extracts it.
-
#Valid4 ⇒ String
Describe a valid IPv4 address.
-
#Valid6 ⇒ String
Describe a valid IPv6 address.
-
#ValidNetwork ⇒ String
Returns string of valid network definition.
Instance Method Details
#BitsToIPv4(bits) ⇒ String
Converts 32 bit binary number to its IPv4 representation.
216 217 218 219 220 |
# File 'library/types/src/modules/IP.rb', line 216 def BitsToIPv4(bits) return nil unless /\A[01]{32}\z/ =~ bits ToString(bits.to_i(2)) end |
#Check(ip) ⇒ Object
Check syntax of IP address
116 117 118 |
# File 'library/types/src/modules/IP.rb', line 116 def Check(ip) Check4(ip) || Check6(ip) end |
#Check4(ip) ⇒ Object
Check syntax of IPv4 address
62 63 64 65 66 |
# File 'library/types/src/modules/IP.rb', line 62 def Check4(ip) IPAddr.new(ip).ipv4? rescue StandardError false end |
#Check6(ip) ⇒ Object
Check syntax of IPv6 address
82 83 84 85 86 |
# File 'library/types/src/modules/IP.rb', line 82 def Check6(ip) IPAddr.new(ip).ipv6? rescue StandardError false end |
#CheckNetwork(network) ⇒ Object
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
309 310 311 |
# File 'library/types/src/modules/IP.rb', line 309 def CheckNetwork(network) CheckNetwork4(network) || CheckNetwork6(network) end |
#CheckNetwork4(network) ⇒ Object
Checks the given IPv4 network entry.
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'library/types/src/modules/IP.rb', line 238 def CheckNetwork4(network) generic_check = CheckNetworkShared(network) return generic_check unless generic_check.nil? # 192.168.0.0/20, 0.8.55/158 if network =~ Regexp.new("^[" + @ValidChars4 + "]+/[0-9]+$") net_parts = network.split("/") return Check4(net_parts[0]) && Netmask.CheckPrefix4(net_parts[1]) # 192.168.0.0/255.255.255.0, 0.8.55/10.258.12 elsif network =~ Regexp.new("^[" + @ValidChars4 + "]+/[" + @ValidChars4 + "]+$") net_parts = network.split("/") return Check4(net_parts[0]) && Netmask.Check4(net_parts[1]) # 192.168.0.1, 0.8.55.999 elsif Check4(network) return true end false end |
#CheckNetwork6(network) ⇒ Object
Checks the given IPv6 network entry.
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'library/types/src/modules/IP.rb', line 269 def CheckNetwork6(network) generic_check = CheckNetworkShared(network) return generic_check unless generic_check.nil? # 2001:db8:0::1/64 if network =~ Regexp.new("^[" + @ValidChars6 + "]+/[" + Netmask.ValidChars6 + "]*$") net_parts = network.split("/") return Check6(net_parts[0]) && Netmask.Check6(net_parts[1]) # 2001:db8:0::1/ffff:ffff::0 elsif network =~ Regexp.new("^[" + @ValidChars6 + "]+/[" + @ValidChars6 + "]+$") net_parts = network.split("/") return Check6(net_parts[0]) && Check6(net_parts[1]) # 2001:db8:0::1 elsif Check6(network) return true end false end |
#CheckNetworkShared(network) ⇒ Object
222 223 224 225 226 227 |
# File 'library/types/src/modules/IP.rb', line 222 def CheckNetworkShared(network) return false if network.nil? || network == "" # all networks (network == "0/0") ? true : nil end |
#ComputeBroadcast(ip, mask) ⇒ Object
Compute IPv4 broadcast address from ip4 address and network mask.
The broadcast address is the highest address of network address range.
181 182 183 184 185 186 187 |
# File 'library/types/src/modules/IP.rb', line 181 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 |
#ComputeNetwork(ip, mask) ⇒ Object
Compute IPv4 network address from ip4 address and network mask.
169 170 171 172 173 |
# File 'library/types/src/modules/IP.rb', line 169 def ComputeNetwork(ip, mask) i = ToInteger(ip) m = ToInteger(mask) ToString(Ops.bitwise_and(Ops.bitwise_and(i, m), 4_294_967_295)) end |
#IPv4ToBits(ipv4) ⇒ String
Converts IPv4 into its 32 bit binary representation.
199 200 201 202 203 204 |
# File 'library/types/src/modules/IP.rb', line 199 def IPv4ToBits(ipv4) int = ToInteger(ipv4) return nil unless int format("%032b", int) end |
#main ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'library/types/src/modules/IP.rb', line 37 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 |
#reserved4(ip) ⇒ Object
Checks if given IPv4 address is reserved by any related RFC.
RFCs covered by this method are #1700, #1918, #2544, #3068, #5735, #5737, 5771, #6333 and #6598
@param[String] ip IPv4 address @return[true,false] if address is reserved
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 354 355 356 357 |
# File 'library/types/src/modules/IP.rb', line 322 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).cover?(ip.split(".").first.to_i) false end |
#ToHex(ip) ⇒ String
Converts IPv4 address from string to hex format
158 159 160 161 162 163 |
# File 'library/types/src/modules/IP.rb', line 158 def ToHex(ip) int = ToInteger(ip) return nil unless int format("%08X", int) end |
#ToInteger(ip) ⇒ Object
Convert IPv4 address from string to integer
140 141 142 143 144 |
# File 'library/types/src/modules/IP.rb', line 140 def ToInteger(ip) return nil unless Check4(ip) IPAddr.new(ip).to_i end |
#ToString(ip) ⇒ Object
Convert IPv4 address from integer to string
149 150 151 |
# File 'library/types/src/modules/IP.rb', line 149 def ToString(ip) IPAddr.new(ip, Socket::AF_INET).to_s end |
#UndecorateIPv6(ip) ⇒ Object
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] )
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'library/types/src/modules/IP.rb', line 100 def UndecorateIPv6(ip) if Builtins.regexpmatch(ip, "^\\[.*\\]") || Builtins.regexpmatch(ip, "^[^][%]+(%[^][%]+){0,1}$") ip = Builtins.regexpsub( ip, "^\\[?([^][%]+)(%[^][%]+){0,1}(\\]|$)", "\\1" ) end ip end |
#Valid4 ⇒ String
Describe a valid IPv4 address
52 53 54 55 56 57 |
# File 'library/types/src/modules/IP.rb', line 52 def Valid4 # Translators: dot: "." _( "A valid IPv4 address consists of four integers\nin the range 0-255 separated by dots." ) end |
#Valid6 ⇒ String
Describe a valid IPv6 address
70 71 72 73 74 75 76 77 |
# File 'library/types/src/modules/IP.rb', line 70 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 |
#ValidNetwork ⇒ String
Returns string of valid network definition. Both IPv4 and IPv6.
124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'library/types/src/modules/IP.rb', line 124 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 |