pub struct IPAddress {
    pub ip_bits: &'static IpBits,
    pub host_address: BigUint,
    pub prefix: Prefix,
    pub mapped: Option<Box<IPAddress, Global>>,
    pub vt_is_private: fn(_: &IPAddress) -> bool,
    pub vt_is_loopback: fn(_: &IPAddress) -> bool,
    pub vt_to_ipv6: fn(_: &IPAddress) -> IPAddress,
}

FieldsΒ§

Β§ip_bits: &'static IpBitsΒ§host_address: BigUintΒ§prefix: PrefixΒ§mapped: Option<Box<IPAddress, Global>>Β§vt_is_private: fn(_: &IPAddress) -> boolΒ§vt_is_loopback: fn(_: &IPAddress) -> boolΒ§vt_to_ipv6: fn(_: &IPAddress) -> IPAddress

ImplementationsΒ§

Parse the argument string to create a new IPv4, IPv6 or Mapped IP object

ip = IPAddress.parse β€œ172.16.10.1/24” ip6 = IPAddress.parse β€œ2001:db8::8:800:200c:417a/64” ip_mapped = IPAddress.parse β€œ::ffff:172.16.10.1/128”

All the object created will be instances of the correct class:

ip.class //=> IPAddress::IPv4 ip6.class //=> IPAddress::IPv6 ip_mapped.class //=> IPAddress::IPv6::Mapped

True if the object is an IPv4 address

ip = IPAddress(β€œ192.168.10.100/24”)

ip.ipv4? //-> true

True if the object is an IPv6 address

ip = IPAddress(β€œ192.168.10.100/24”)

ip.ipv6? //-> false

Checks if the given string is a valid IP address, either IPv4 or IPv6

Example:

IPAddress::valid? β€œ2002::1” //=> true

IPAddress::valid? β€œ10.0.0.256” //=> false

Checks if the given string is a valid IPv4 address

Example:

IPAddress::valid_ipv4? β€œ2002::1” //=> false

IPAddress::valid_ipv4? β€œ172.16.10.1” //=> true

Checks if the given string is a valid IPv6 address

Example:

IPAddress::valid_ipv6? β€œ2002::1” //=> true

IPAddress::valid_ipv6? β€œ2002::DEAD::BEEF” // => false

Returns the IP address in in-addr.arpa format for DNS Domain definition entries like SOA Records

ip = IPAddress(β€œ172.17.100.50/15”)

ip.dns_rev_domains // => [β€œ16.172.in-addr.arpa”,β€œ17.172.in-addr.arpa”]

Summarization (or aggregation) is the process when two or more networks are taken together to check if a supernet, including all and only these networks, exists. If it exists then this supernet is called the summarized (or aggregated) network.

It is very important to understand that summarization can only occur if there are no holes in the aggregated network, or, in other words, if the given networks fill completely the address space of the supernet. So the two rules are:

  1. The aggregate network must contain +all+ the IP addresses of the original networks;
  2. The aggregate network must contain +only+ the IP addresses of the original networks;

A few examples will help clarify the above. Let’s consider for instance the following two networks:

ip1 = IPAddress(β€œ172.16.10.0/24”) ip2 = IPAddress(β€œ172.16.11.0/24”)

These two networks can be expressed using only one IP address network if we change the prefix. Let Ruby do the work:

IPAddress::IPv4::summarize(ip1,ip2).to_s /// β€œ172.16.10.0/23”

We note how the network β€œ172.16.10.0/23” includes all the addresses specified in the above networks, and (more important) includes ONLY those addresses.

If we summarized +ip1+ and +ip2+ with the following network:

β€œ172.16.0.0/16”

we would have satisfied rule /// 1 above, but not rule /// 2. So β€œ172.16.0.0/16” is not an aggregate network for +ip1+ and +ip2+.

If it’s not possible to compute a single aggregated network for all the original networks, the method returns an array with all the aggregate networks found. For example, the following four networks can be aggregated in a single /22:

ip1 = IPAddress(β€œ10.0.0.1/24”) ip2 = IPAddress(β€œ10.0.1.1/24”) ip3 = IPAddress(β€œ10.0.2.1/24”) ip4 = IPAddress(β€œ10.0.3.1/24”)

IPAddress::IPv4::summarize(ip1,ip2,ip3,ip4).to_string /// β€œ10.0.0.0/22”,

But the following networks can’t be summarized in a single network:

ip1 = IPAddress(β€œ10.0.1.1/24”) ip2 = IPAddress(β€œ10.0.2.1/24”) ip3 = IPAddress(β€œ10.0.3.1/24”) ip4 = IPAddress(β€œ10.0.4.1/24”)

IPAddress::IPv4::summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string} /// [β€œ10.0.1.0/24”,β€œ10.0.2.0/23”,β€œ10.0.4.0/24”]

Summarization (or aggregation) is the process when two or more networks are taken together to check if a supernet, including all and only these networks, exists. If it exists then this supernet is called the summarized (or aggregated) network.

It is very important to understand that summarization can only occur if there are no holes in the aggregated network, or, in other words, if the given networks fill completely the address space of the supernet. So the two rules are:

  1. The aggregate network must contain +all+ the IP addresses of the original networks;
  2. The aggregate network must contain +only+ the IP addresses of the original networks;

A few examples will help clarify the above. Let’s consider for instance the following two networks:

ip1 = IPAddress(β€œ2000:0::4/32”) ip2 = IPAddress(β€œ2000:1::6/32”)

These two networks can be expressed using only one IP address network if we change the prefix. Let Ruby do the work:

IPAddress::IPv6::summarize(ip1,ip2).to_s /// β€œ2000:0::/31”

We note how the network β€œ2000:0::/31” includes all the addresses specified in the above networks, and (more important) includes ONLY those addresses.

If we summarized +ip1+ and +ip2+ with the following network:

β€œ2000::/16”

we would have satisfied rule /// 1 above, but not rule /// 2. So β€œ2000::/16” is not an aggregate network for +ip1+ and +ip2+.

If it’s not possible to compute a single aggregated network for all the original networks, the method returns an array with all the aggregate networks found. For example, the following four networks can be aggregated in a single /22:

ip1 = IPAddress(β€œ2000:0::/32”) ip2 = IPAddress(β€œ2000:1::/32”) ip3 = IPAddress(β€œ2000:2::/32”) ip4 = IPAddress(β€œ2000:3::/32”)

IPAddress::IPv6::summarize(ip1,ip2,ip3,ip4).to_string /// β€œβ€œ2000:3::/30”,

But the following networks can’t be summarized in a single network:

ip1 = IPAddress(β€œ2000:1::/32”) ip2 = IPAddress(β€œ2000:2::/32”) ip3 = IPAddress(β€œ2000:3::/32”) ip4 = IPAddress(β€œ2000:4::/32”)

IPAddress::IPv4::summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string} /// [β€œ2000:1::/32”,β€œ2000:2::/31”,β€œ2000:4::/32”]

Returns true if the address is an unspecified address

See IPAddress::IPv6::Unspecified for more information

Returns true if the address is a loopback address

See IPAddress::IPv6::Loopback for more information

Returns true if the address is a mapped address

See IPAddress::IPv6::Mapped for more information

Returns the prefix portion of the IPv4 object as a IPAddress::Prefix32 object

ip = IPAddress(β€œ172.16.100.4/22”)

ip.prefix /// 22

ip.prefix.class /// IPAddress::Prefix32

Checks if the argument is a valid IPv4 netmask expressed in dotted decimal format.

IPAddress.valid_ipv4_netmask? β€œ255.255.0.0” /// true

Set a new prefix number for the object

This is useful if you want to change the prefix to an object created with IPv4::parse_u32 or if the object was created using the classful mask.

ip = IPAddress(β€œ172.16.100.4”)

puts ip /// 172.16.100.4/16

ip.prefix = 22

puts ip /// 172.16.100.4/22

Returns a string with the IP address in canonical form.

ip = IPAddress(β€œ172.16.100.4/22”)

ip.to_string /// β€œ172.16.100.4/22”

Returns the address portion of an IP in binary format, as a string containing a sequence of 0 and 1

ip = IPAddress(β€œ127.0.0.1”)

ip.bits /// β€œ01111111000000000000000000000001”

Returns the broadcast address for the given IP.

ip = IPAddress(β€œ172.16.10.64/24”)

ip.broadcast.to_s /// β€œ172.16.10.255”

Checks if the IP address is actually a network

ip = IPAddress(β€œ172.16.10.64/24”)

ip.network? /// false

ip = IPAddress(β€œ172.16.10.64/26”)

ip.network? /// true

Returns a new IPv4 object with the network number for the given IP.

ip = IPAddress(β€œ172.16.10.64/24”)

ip.network.to_s /// β€œ172.16.10.0”

Returns a new IPv4 object with the first host IP address in the range.

Example: given the 192.168.100.0/24 network, the first host IP address is 192.168.100.1.

ip = IPAddress(β€œ192.168.100.0/24”)

ip.first.to_s /// β€œ192.168.100.1”

The object IP doesn’t need to be a network: the method automatically gets the network number from it

ip = IPAddress(β€œ192.168.100.50/24”)

ip.first.to_s /// β€œ192.168.100.1”

Like its sibling method IPv4/// first, this method returns a new IPv4 object with the last host IP address in the range.

Example: given the 192.168.100.0/24 network, the last host IP address is 192.168.100.254

ip = IPAddress(β€œ192.168.100.0/24”)

ip.last.to_s /// β€œ192.168.100.254”

The object IP doesn’t need to be a network: the method automatically gets the network number from it

ip = IPAddress(β€œ192.168.100.50/24”)

ip.last.to_s /// β€œ192.168.100.254”

Iterates over all the hosts IP addresses for the given network (or IP address).

ip = IPAddress(β€œ10.0.0.1/29”)

ip.each_host do |i| p i.to_s end /// β€œ10.0.0.1” /// β€œ10.0.0.2” /// β€œ10.0.0.3” /// β€œ10.0.0.4” /// β€œ10.0.0.5” /// β€œ10.0.0.6”

Iterates over all the IP addresses for the given network (or IP address).

The object yielded is a new IPv4 object created from the iteration.

ip = IPAddress(β€œ10.0.0.1/29”)

ip.each do |i| p i.address end /// β€œ10.0.0.0” /// β€œ10.0.0.1” /// β€œ10.0.0.2” /// β€œ10.0.0.3” /// β€œ10.0.0.4” /// β€œ10.0.0.5” /// β€œ10.0.0.6” /// β€œ10.0.0.7”

Spaceship operator to compare IPv4 objects

Comparing IPv4 addresses is useful to ordinate them into lists that match our intuitive perception of ordered IP addresses.

The first comparison criteria is the u32 value. For example, 10.100.100.1 will be considered to be less than 172.16.0.1, because, in a ordered list, we expect 10.100.100.1 to come before 172.16.0.1.

The second criteria, in case two IPv4 objects have identical addresses, is the prefix. An higher prefix will be considered greater than a lower prefix. This is because we expect to see 10.100.100.0/24 come before 10.100.100.0/25.

Example:

ip1 = IPAddress β€œ10.100.100.1/8” ip2 = IPAddress β€œ172.16.0.1/16” ip3 = IPAddress β€œ10.100.100.1/16”

ip1 < ip2 /// true ip1 > ip3 /// false

[ip1,ip2,ip3].sort.map{|i| i.to_string} /// [β€œ10.100.100.1/8”,β€œ10.100.100.1/16”,β€œ172.16.0.1/16”]

Returns the number of IP addresses included in the network. It also counts the network address and the broadcast address.

ip = IPAddress(β€œ10.0.0.1/29”)

ip.size /// 8

Checks whether a subnet includes the given IP address.

Accepts an IPAddress::IPv4 object.

ip = IPAddress(β€œ192.168.10.100/24”)

addr = IPAddress(β€œ192.168.10.102/24”)

ip.include? addr /// true

ip.include? IPAddress(β€œ172.16.0.48/16”) /// false

Checks whether a subnet includes all the given IPv4 objects.

ip = IPAddress(β€œ192.168.10.100/24”)

addr1 = IPAddress(β€œ192.168.10.102/24”) addr2 = IPAddress(β€œ192.168.10.103/24”)

ip.include_all?(addr1,addr2) /// true

Checks if an IPv4 address objects belongs to a private network RFC1918

Example:

ip = IPAddress β€œ10.1.1.1/24” ip.private? /// true

Returns a new IPv4 object from the supernetting of the instance network.

Supernetting is similar to subnetting, except that you getting as a result a network with a smaller prefix (bigger host space). For example, given the network

ip = IPAddress(β€œ172.16.10.0/24”)

you can supernet it with a new /23 prefix

ip.supernet(23).to_string /// β€œ172.16.10.0/23”

However if you supernet it with a /22 prefix, the network address will change:

ip.supernet(22).to_string /// β€œ172.16.8.0/22”

If +new_prefix+ is less than 1, returns 0.0.0.0/0

This method implements the subnetting function similar to the one described in RFC3531.

By specifying a new prefix, the method calculates the network number for the given IPv4 object and calculates the subnets associated to the new prefix.

For example, given the following network:

ip = IPAddress β€œ172.16.10.0/24”

we can calculate the subnets with a /26 prefix

ip.subnets(26).map(&:to_string) /// [β€œ172.16.10.0/26”, β€œ172.16.10.64/26”, β€œ172.16.10.128/26”, β€œ172.16.10.192/26”]

The resulting number of subnets will of course always be a power of two.

Return the ip address in a format compatible with the IPv6 Mapped IPv4 addresses

Example:

ip = IPAddress(β€œ172.16.10.1/24”)

ip.to_ipv6 /// β€œac10:0a01”

Trait ImplementationsΒ§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait ImplementationsΒ§

Blanket ImplementationsΒ§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more