# <!-- rdoc-file=lib/ipaddr.rb -->
# IPAddr provides a set of methods to manipulate an IP address.  Both IPv4 and
# IPv6 are supported.
#
# ## Example
#
#     require 'ipaddr'
#
#     ipaddr1 = IPAddr.new "3ffe:505:2::1"
#
#     p ipaddr1                   #=> #<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0001/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
#
#     p ipaddr1.to_s              #=> "3ffe:505:2::1"
#
#     ipaddr2 = ipaddr1.mask(48)  #=> #<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0000/ffff:ffff:ffff:0000:0000:0000:0000:0000>
#
#     p ipaddr2.to_s              #=> "3ffe:505:2::"
#
#     ipaddr3 = IPAddr.new "192.168.2.0/24"
#
#     p ipaddr3                   #=> #<IPAddr: IPv4:192.168.2.0/255.255.255.0>
#
class IPAddr
  include Comparable

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - new_ntoh(addr)
  # -->
  # Creates a new ipaddr containing the given network byte ordered string form of
  # an IP address.
  #
  def self.new_ntoh: (String addr) -> IPAddr

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - ntop(addr)
  # -->
  # Convert a network byte ordered string form of an IP address into human
  # readable form. It expects the string to be encoded in Encoding::ASCII_8BIT
  # (BINARY).
  #
  def self.ntop: (String addr) -> String

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - new(addr = '::', family = Socket::AF_UNSPEC)
  # -->
  # Creates a new ipaddr object either from a human readable IP address
  # representation in string, or from a packed in_addr value followed by an
  # address family.
  #
  # In the former case, the following are the valid formats that will be
  # recognized: "address", "address/prefixlen" and "address/mask", where IPv6
  # address may be enclosed in square brackets (`[' and `]').  If a prefixlen or a
  # mask is specified, it returns a masked IP address.  Although the address
  # family is determined automatically from a specified string, you can specify
  # one explicitly by the optional second argument.
  #
  # Otherwise an IP address is generated from a packed in_addr value and an
  # address family.
  #
  # The IPAddr class defines many methods and operators, and some of those, such
  # as &, |, include? and ==, accept a string, or a packed in_addr value instead
  # of an IPAddr object.
  #
  def initialize: (?String addr, ?untyped family) -> void

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - &(other)
  # -->
  # Returns a new ipaddr built by bitwise AND.
  #
  def &: (untyped other) -> IPAddr

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - <<(num)
  # -->
  # Returns a new ipaddr built by bitwise left shift.
  #
  def <<: (Integer num) -> IPAddr

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - <=>(other)
  # -->
  # Compares the ipaddr with another.
  #
  def <=>: (untyped other) -> Integer?

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - ==(other)
  # -->
  # Returns true if two ipaddrs are equal.
  #
  def ==: (untyped other) -> bool

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - ===(other)
  # -->
  #
  alias === include?

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - >>(num)
  # -->
  # Returns a new ipaddr built by bitwise right-shift.
  #
  def >>: (Integer num) -> IPAddr

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - eql?(other)
  # -->
  # Checks equality used by Hash.
  #
  def eql?: (untyped other) -> bool

  # <!-- rdoc-file=lib/ipaddr.rb -->
  # Returns the address family of this IP address.
  #
  attr_reader family: Integer

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - hash()
  # -->
  # Returns a hash value used by Hash, Set, and Array classes
  #
  def hash: () -> Integer

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - hton()
  # -->
  # Returns a network byte ordered string form of the IP address.
  #
  def hton: () -> String

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - include?(other)
  # -->
  # Returns true if the given ipaddr is in the range.
  #
  # e.g.:
  #     require 'ipaddr'
  #     net1 = IPAddr.new("192.168.2.0/24")
  #     net2 = IPAddr.new("192.168.2.100")
  #     net3 = IPAddr.new("192.168.3.0")
  #     net4 = IPAddr.new("192.168.2.0/16")
  #     p net1.include?(net2)     #=> true
  #     p net1.include?(net3)     #=> false
  #     p net1.include?(net4)     #=> false
  #     p net4.include?(net1)     #=> true
  #
  def include?: (untyped other) -> bool

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - inspect()
  # -->
  # Returns a string containing a human-readable representation of the ipaddr.
  # ("#<IPAddr: family:address/mask>")
  #
  def inspect: () -> String

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - ip6_arpa()
  # -->
  # Returns a string for DNS reverse lookup compatible with RFC3172.
  #
  def ip6_arpa: () -> String

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - ip6_int()
  # -->
  # Returns a string for DNS reverse lookup compatible with RFC1886.
  #
  def ip6_int: () -> String

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - ipv4?()
  # -->
  # Returns true if the ipaddr is an IPv4 address.
  #
  def ipv4?: () -> bool

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - ipv4_compat()
  # -->
  # Returns a new ipaddr built by converting the native IPv4 address into an
  # IPv4-compatible IPv6 address.
  #
  def ipv4_compat: () -> IPAddr

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - ipv4_compat?()
  # -->
  # Returns true if the ipaddr is an IPv4-compatible IPv6 address.
  #
  def ipv4_compat?: () -> bool

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - ipv4_mapped()
  # -->
  # Returns a new ipaddr built by converting the native IPv4 address into an
  # IPv4-mapped IPv6 address.
  #
  def ipv4_mapped: () -> IPAddr

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - ipv4_mapped?()
  # -->
  # Returns true if the ipaddr is an IPv4-mapped IPv6 address.
  #
  def ipv4_mapped?: () -> bool

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - ipv6?()
  # -->
  # Returns true if the ipaddr is an IPv6 address.
  #
  def ipv6?: () -> bool

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - link_local?()
  # -->
  # Returns true if the ipaddr is a link-local address.  IPv4 addresses in
  # 169.254.0.0/16 reserved by RFC 3927 and link-local IPv6 Unicast Addresses in
  # fe80::/10 reserved by RFC 4291 are considered link-local. Link-local IPv4
  # addresses in the IPv4-mapped IPv6 address range are also considered
  # link-local.
  #
  def link_local?: () -> bool

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - loopback?()
  # -->
  # Returns true if the ipaddr is a loopback address. Loopback IPv4 addresses in
  # the IPv4-mapped IPv6 address range are also considered as loopback addresses.
  #
  def loopback?: () -> bool

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - mask(prefixlen)
  # -->
  # Returns a new ipaddr built by masking IP address with the given
  # prefixlen/netmask. (e.g. 8, 64, "255.255.255.0", etc.)
  #
  def mask: (String | Integer prefixlen) -> IPAddr

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - native()
  # -->
  # Returns a new ipaddr built by converting the IPv6 address into a native IPv4
  # address.  If the IP address is not an IPv4-mapped or IPv4-compatible IPv6
  # address, returns self.
  #
  def native: () -> IPAddr

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - prefix()
  # -->
  # Returns the prefix length in bits for the ipaddr.
  #
  def prefix: () -> Integer

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - prefix=(prefix)
  # -->
  # Sets the prefix length in bits
  #
  def prefix=: (Integer prefix) -> self

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - private?()
  # -->
  # Returns true if the ipaddr is a private address.  IPv4 addresses in
  # 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 as defined in RFC 1918 and IPv6
  # Unique Local Addresses in fc00::/7 as defined in RFC 4193 are considered
  # private. Private IPv4 addresses in the IPv4-mapped IPv6 address range are also
  # considered private.
  #
  def private?: () -> bool

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - reverse()
  # -->
  # Returns a string for DNS reverse lookup.  It returns a string in RFC3172 form
  # for an IPv6 address.
  #
  def reverse: () -> String

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - succ()
  # -->
  # Returns the successor to the ipaddr.
  #
  def succ: () -> IPAddr

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - to_i()
  # -->
  # Returns the integer representation of the ipaddr.
  #
  def to_i: () -> Integer

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - to_range()
  # -->
  # Creates a Range object for the network address.
  #
  def to_range: () -> Range[IPAddr]

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - to_s()
  # -->
  # Returns a string containing the IP address representation.
  #
  def to_s: () -> String

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - to_string()
  # -->
  # Returns a string containing the IP address representation in canonical form.
  #
  def to_string: () -> String

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - |(other)
  # -->
  # Returns a new ipaddr built by bitwise OR.
  #
  def |: (untyped other) -> IPAddr

  # <!--
  #   rdoc-file=lib/ipaddr.rb
  #   - ~()
  # -->
  # Returns a new ipaddr built by bitwise negation.
  #
  def ~: () -> IPAddr
end

# <!-- rdoc-file=lib/ipaddr.rb -->
# Generic IPAddr related error. Exceptions raised in this class should inherit
# from Error.
#
class IPAddr::Error < ArgumentError
end

# <!-- rdoc-file=lib/ipaddr.rb -->
# Raised when the provided IP address is an invalid address.
#
class IPAddr::InvalidAddressError < IPAddr::Error
end

# <!-- rdoc-file=lib/ipaddr.rb -->
# Raised when the address family is invalid such as an address with an
# unsupported family, an address with an inconsistent family, or an address
# who's family cannot be determined.
#
class IPAddr::AddressFamilyError < IPAddr::Error
end

# <!-- rdoc-file=lib/ipaddr.rb -->
# Raised when the address is an invalid length.
#
class IPAddr::InvalidPrefixError < IPAddr::InvalidAddressError
end

# <!-- rdoc-file=lib/ipaddr.rb -->
# 32 bit mask for IPv4
#
IPAddr::IN4MASK: Integer

# <!-- rdoc-file=lib/ipaddr.rb -->
# Format string for IPv6
#
IPAddr::IN6FORMAT: String

# <!-- rdoc-file=lib/ipaddr.rb -->
# 128 bit mask for IPv6
#
IPAddr::IN6MASK: Integer

# <!-- rdoc-file=lib/ipaddr.rb -->
# Regexp *internally* used for parsing IPv4 address.
#
IPAddr::RE_IPV4ADDRLIKE: Regexp

# <!-- rdoc-file=lib/ipaddr.rb -->
# Regexp *internally* used for parsing IPv6 address.
#
IPAddr::RE_IPV6ADDRLIKE_COMPRESSED: Regexp

# <!-- rdoc-file=lib/ipaddr.rb -->
# Regexp *internally* used for parsing IPv6 address.
#
IPAddr::RE_IPV6ADDRLIKE_FULL: Regexp
