Struct warlocks_cauldron::IPAddress
source Β· 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) -> IPAddressImplementationsΒ§
sourceΒ§impl IPAddress
impl IPAddress
sourcepub fn parse<S>(_str: S) -> Result<IPAddress, String>where
S: Into<String>,
pub fn parse<S>(_str: S) -> Result<IPAddress, String>where
S: Into<String>,
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
pub fn split_at_slash(str: &String) -> (String, Option<String>)
pub fn from(&self, addr: &BigUint, prefix: &Prefix) -> IPAddress
sourcepub fn is_ipv4(&self) -> bool
pub fn is_ipv4(&self) -> bool
True if the object is an IPv4 address
ip = IPAddress(β192.168.10.100/24β)
ip.ipv4? //-> true
sourcepub fn is_ipv6(&self) -> bool
pub fn is_ipv6(&self) -> bool
True if the object is an IPv6 address
ip = IPAddress(β192.168.10.100/24β)
ip.ipv6? //-> false
sourcepub fn is_valid<S>(_addr: S) -> boolwhere
S: Into<String>,
pub fn is_valid<S>(_addr: S) -> boolwhere
S: Into<String>,
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
sourcepub fn parse_ipv4_part(i: &str, addr: &String) -> Result<u32, String>
pub fn parse_ipv4_part(i: &str, addr: &String) -> Result<u32, String>
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
pub fn split_to_u32(addr: &String) -> Result<u32, String>
pub fn is_valid_ipv4<S>(addr: S) -> boolwhere
S: Into<String>,
sourcepub fn split_on_colon(addr: &String) -> (Result<BigUint, String>, usize)
pub fn split_on_colon(addr: &String) -> (Result<BigUint, String>, usize)
Checks if the given string is a valid IPv6 address
Example:
IPAddress::valid_ipv6? β2002::1β //=> true
IPAddress::valid_ipv6? β2002::DEAD::BEEFβ // => false
pub fn split_to_num(addr: &String) -> Result<BigUint, String>
pub fn is_valid_ipv6<S>(addr: S) -> boolwhere
S: Into<String>,
pub fn aggregate(networks: &Vec<IPAddress, Global>) -> Vec<IPAddress, Global> β
pub fn parts(&self) -> Vec<u16, Global> β
pub fn parts_hex_str(&self) -> Vec<String, Global> β
sourcepub fn dns_rev_domains(&self) -> Vec<String, Global> β
pub fn dns_rev_domains(&self) -> Vec<String, Global> β
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β]
pub fn dns_reverse(&self) -> String
pub fn dns_parts(&self) -> Vec<u8, Global> β
pub fn dns_networks(&self) -> Vec<IPAddress, Global> β
sourcepub fn summarize(networks: &Vec<IPAddress, Global>) -> Vec<IPAddress, Global> β
pub fn summarize(networks: &Vec<IPAddress, Global>) -> Vec<IPAddress, Global> β
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:
- The aggregate network must contain +all+ the IP addresses of the original networks;
- 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:
- The aggregate network must contain +all+ the IP addresses of the original networks;
- 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β]
pub fn summarize_str<S>(
netstr: Vec<S, Global>
) -> Result<Vec<IPAddress, Global>, String>where
S: Into<String>,
pub fn ip_same_kind(&self, oth: &IPAddress) -> bool
sourcepub fn is_unspecified(&self) -> bool
pub fn is_unspecified(&self) -> bool
Returns true if the address is an unspecified address
See IPAddress::IPv6::Unspecified for more information
sourcepub fn is_loopback(&self) -> bool
pub fn is_loopback(&self) -> bool
Returns true if the address is a loopback address
See IPAddress::IPv6::Loopback for more information
sourcepub fn is_mapped(&self) -> bool
pub fn is_mapped(&self) -> bool
Returns true if the address is a mapped address
See IPAddress::IPv6::Mapped for more information
sourcepub fn prefix(&self) -> &Prefix
pub fn prefix(&self) -> &Prefix
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
sourcepub fn is_valid_netmask<S>(addr: S) -> boolwhere
S: Into<String>,
pub fn is_valid_netmask<S>(addr: S) -> boolwhere
S: Into<String>,
Checks if the argument is a valid IPv4 netmask expressed in dotted decimal format.
IPAddress.valid_ipv4_netmask? β255.255.0.0β /// true
pub fn netmask_to_prefix(nm: &BigUint, bits: usize) -> Result<usize, String>
pub fn parse_netmask_to_prefix<S>(_netmask: S) -> Result<usize, String>where
S: Into<String>,
sourcepub fn change_prefix(&self, num: usize) -> Result<IPAddress, String>
pub fn change_prefix(&self, num: usize) -> Result<IPAddress, String>
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
pub fn change_netmask<S>(&self, str: S) -> Result<IPAddress, String>where
S: Into<String>,
sourcepub fn to_string(&self) -> String
pub fn to_string(&self) -> String
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β
pub fn to_s(&self) -> String
pub fn to_string_uncompressed(&self) -> String
pub fn to_s_uncompressed(&self) -> String
pub fn to_s_mapped(&self) -> String
pub fn to_string_mapped(&self) -> String
sourcepub fn bits(&self) -> String
pub fn bits(&self) -> String
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β
pub fn to_hex(&self) -> String
pub fn netmask(&self) -> IPAddress
sourcepub fn broadcast(&self) -> IPAddress
pub fn broadcast(&self) -> IPAddress
Returns the broadcast address for the given IP.
ip = IPAddress(β172.16.10.64/24β)
ip.broadcast.to_s /// β172.16.10.255β
sourcepub fn is_network(&self) -> bool
pub fn is_network(&self) -> bool
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
sourcepub fn network(&self) -> IPAddress
pub fn network(&self) -> IPAddress
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β
pub fn to_network(adr: &BigUint, host_prefix: usize) -> BigUint
pub fn sub(&self, other: &IPAddress) -> BigUint
pub fn add(&self, other: &IPAddress) -> Vec<IPAddress, Global> β
pub fn to_s_vec(vec: &Vec<IPAddress, Global>) -> Vec<String, Global> β
pub fn to_string_vec(vec: &Vec<IPAddress, Global>) -> Vec<String, Global> β
pub fn to_ipaddress_vec<S>(
vec: Vec<S, Global>
) -> Result<Vec<IPAddress, Global>, String>where
S: Into<String>,
sourcepub fn first(&self) -> IPAddress
pub fn first(&self) -> IPAddress
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β
sourcepub fn last(&self) -> IPAddress
pub fn last(&self) -> IPAddress
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β
sourcepub fn each_host<F>(&self, func: F)where
F: Fn(&IPAddress),
pub fn each_host<F>(&self, func: F)where
F: Fn(&IPAddress),
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β
sourcepub fn each<F>(&self, func: F)where
F: Fn(&IPAddress),
pub fn each<F>(&self, func: F)where
F: Fn(&IPAddress),
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β
sourcepub fn size(&self) -> BigUint
pub fn size(&self) -> BigUint
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
pub fn is_same_kind(&self, oth: &IPAddress) -> bool
sourcepub fn includes(&self, oth: &IPAddress) -> bool
pub fn includes(&self, oth: &IPAddress) -> bool
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
sourcepub fn includes_all(&self, oths: &[IPAddress]) -> bool
pub fn includes_all(&self, oths: &[IPAddress]) -> bool
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
sourcepub fn is_private(&self) -> bool
pub fn is_private(&self) -> bool
Checks if an IPv4 address objects belongs to a private network RFC1918
Example:
ip = IPAddress β10.1.1.1/24β ip.private? /// true
pub fn split(&self, subnets: usize) -> Result<Vec<IPAddress, Global>, String>
sourcepub fn supernet(&self, new_prefix: usize) -> Result<IPAddress, String>
pub fn supernet(&self, new_prefix: usize) -> Result<IPAddress, String>
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
sourcepub fn subnet(&self, subprefix: usize) -> Result<Vec<IPAddress, Global>, String>
pub fn subnet(&self, subprefix: usize) -> Result<Vec<IPAddress, Global>, String>
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.
Trait ImplementationsΒ§
sourceΒ§impl Ord for IPAddress
impl Ord for IPAddress
1.21.0 Β· sourceΒ§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
sourceΒ§impl PartialEq<IPAddress> for IPAddress
impl PartialEq<IPAddress> for IPAddress
sourceΒ§impl PartialOrd<IPAddress> for IPAddress
impl PartialOrd<IPAddress> for IPAddress
1.0.0 Β· sourceΒ§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moreimpl Eq for IPAddress
Auto Trait ImplementationsΒ§
impl RefUnwindSafe for IPAddress
impl Send for IPAddress
impl Sync for IPAddress
impl Unpin for IPAddress
impl UnwindSafe for IPAddress
Blanket ImplementationsΒ§
sourceΒ§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourceΒ§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.