Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions nvx/src/host/network/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::net::{Ipv4Addr, Ipv6Addr};
///
/// IPv4 orders before IPv6, as Python's IP versions 4 and 6 do.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) enum Family {
pub(super) enum Family {
/// IPv4: 32-bit addresses.
V4,
/// IPv6: 128-bit addresses.
Expand All @@ -24,7 +24,7 @@ pub(crate) enum Family {

impl Family {
/// Both families, in the order in which the compiler lowers them.
pub(crate) const ALL: [Self; 2] = [Self::V4, Self::V6];
pub(super) const ALL: [Self; 2] = [Self::V4, Self::V6];

/// Returns the number of bits of an address of this family.
pub(super) const fn bits(self) -> u32 {
Expand All @@ -43,20 +43,20 @@ impl Family {
}

/// Returns every address of this family as one interval.
pub(crate) const fn everything(self) -> Interval {
pub(super) const fn everything(self) -> Interval {
(0, self.last_address())
}
}

/// An inclusive interval of addresses of one family: its first and its last address.
pub(crate) type Interval = (u128, u128);
pub(super) type Interval = (u128, u128);

/// A network of one family, without host bits.
///
/// Networks order as the compiler sorts its output: by family, then by address, then by prefix
/// length.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct Network {
pub(super) struct Network {
/// Address family.
family: Family,
/// First address of the network, with every host bit clear.
Expand All @@ -81,12 +81,12 @@ impl Network {
}

/// Returns the address family of the network.
pub(crate) fn family(self) -> Family {
pub(super) fn family(self) -> Family {
self.family
}

/// Returns the interval of the network's addresses: its network and broadcast addresses.
pub(crate) fn interval(self) -> Interval {
pub(super) fn interval(self) -> Interval {
(self.address, self.address | self.host_mask())
}

Expand All @@ -101,7 +101,7 @@ impl Network {
}

/// Returns whether `other` lies within this network, as Python's `subnet_of` decides.
pub(crate) fn contains(self, other: Self) -> bool {
pub(super) fn contains(self, other: Self) -> bool {
let (first, last) = self.interval();
let (other_first, other_last) = other.interval();
self.family == other.family && first <= other_first && last >= other_last
Expand Down Expand Up @@ -165,7 +165,7 @@ fn write_ipv6(formatter: &mut fmt::Formatter<'_>, address: u128) -> fmt::Result

/// Why [`parse_interface`] refused a string.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum InterfaceError {
pub(super) enum InterfaceError {
/// The string is neither an IPv4 nor an IPv6 interface.
Invalid,
/// The string is an IPv6 interface that names a scope, such as `fe80::1%eth0/64`.
Expand All @@ -174,7 +174,7 @@ pub(crate) enum InterfaceError {

/// Parses `text` as Python's `ipaddress.ip_interface` does and returns its network, whose host
/// bits are clear.
pub(crate) fn parse_interface(text: &str) -> Result<Network, InterfaceError> {
pub(super) fn parse_interface(text: &str) -> Result<Network, InterfaceError> {
let text = text.as_bytes();
// `ip_interface` tries IPv4 first, and no string is both.
if let Some(network) = parse_ipv4_interface(text) {
Expand Down Expand Up @@ -405,7 +405,7 @@ fn parse_ipv6(text: &[u8]) -> Option<u128> {

/// The networks that Python's `ipaddress.summarize_address_range` yields for an interval: the
/// fewest networks that cover it exactly, from its first address.
pub(crate) struct Summary {
pub(super) struct Summary {
/// Address family of the interval.
family: Family,
/// First address that the networks yielded so far do not cover, or `None` once they cover the
Expand All @@ -417,7 +417,7 @@ pub(crate) struct Summary {

/// Returns the networks that cover the addresses of `family` from `first` through `last`, as
/// `ipaddress.summarize_address_range` yields them.
pub(crate) fn summarize(family: Family, (first, last): Interval) -> Summary {
pub(super) fn summarize(family: Family, (first, last): Interval) -> Summary {
#[cfg(test)]
tests::probe::SUMMARIES.with(|count| count.set(count.get() + 1));
Summary {
Expand Down Expand Up @@ -455,7 +455,7 @@ impl Iterator for Summary {

/// Returns the union of `intervals`, sorted, with overlapping and adjacent intervals merged, as
/// `_merge_intervals` does.
pub(crate) fn merge(intervals: impl IntoIterator<Item = Interval>) -> Vec<Interval> {
pub(super) fn merge(intervals: impl IntoIterator<Item = Interval>) -> Vec<Interval> {
let mut sorted: Vec<Interval> = intervals.into_iter().collect();
#[cfg(test)]
tests::probe::MERGED_INTERVALS.with(|count| count.set(count.get() + sorted.len()));
Expand All @@ -474,7 +474,7 @@ pub(crate) fn merge(intervals: impl IntoIterator<Item = Interval>) -> Vec<Interv

/// Returns the addresses of `sources` that `exclusions` do not hold, as `_subtract_intervals`
/// does. Both lists are sorted and their intervals disjoint.
pub(crate) fn subtract(sources: &[Interval], exclusions: &[Interval]) -> Vec<Interval> {
pub(super) fn subtract(sources: &[Interval], exclusions: &[Interval]) -> Vec<Interval> {
let mut remaining = Vec::new();
let mut first_exclusion = 0;
for &(source_start, source_end) in sources {
Expand Down
38 changes: 19 additions & 19 deletions nvx/src/host/network/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::error::{Error, Result};
///
/// Protocols order by name, as the compiler sorts them.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) enum NativeProtocol {
pub(super) enum NativeProtocol {
/// ICMP, or ICMPv6 for IPv6 networks.
Icmp,
/// TCP.
Expand All @@ -41,7 +41,7 @@ pub(crate) enum NativeProtocol {

impl NativeProtocol {
/// Returns OpenVMM's name of the protocol.
pub(crate) const fn as_str(self) -> &'static str {
pub(super) const fn as_str(self) -> &'static str {
match self {
Self::Icmp => "icmp",
Self::Tcp => "tcp",
Expand All @@ -52,7 +52,7 @@ impl NativeProtocol {

/// The protocol of a rule as written: a native protocol, or `any`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum RuleProtocol {
pub(super) enum RuleProtocol {
/// TCP.
Tcp,
/// UDP.
Expand All @@ -65,7 +65,7 @@ pub(crate) enum RuleProtocol {

impl RuleProtocol {
/// Returns the native protocol, or `None` for `any`.
pub(crate) fn native(self) -> Option<NativeProtocol> {
pub(super) fn native(self) -> Option<NativeProtocol> {
match self {
Self::Tcp => Some(NativeProtocol::Tcp),
Self::Udp => Some(NativeProtocol::Udp),
Expand All @@ -87,33 +87,33 @@ impl RuleProtocol {
}

/// An inclusive range of ports.
pub(crate) type PortRange = (u16, u16);
pub(super) type PortRange = (u16, u16);

/// Traffic that a rule selects at its destinations: a protocol, or every protocol, and a range of
/// its ports, or every port.
pub(crate) type Selector = (Option<NativeProtocol>, Option<PortRange>);
pub(super) type Selector = (Option<NativeProtocol>, Option<PortRange>);

/// A value that should be a port number.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum PortValue {
pub(super) enum PortValue {
/// An integer, saturated to the range of `i128`.
Integer(i128),
}

/// A rule of one action, as the compiler lowers it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Rule {
pub(super) struct Rule {
/// Address family of the rule's addresses.
pub(crate) family: Family,
pub(super) family: Family,
/// Addresses that the rule selects: sorted, disjoint intervals of `family`, possibly none.
///
/// The rules of one rule's selectors share them, as Python's share one tuple, so that many
/// destinations times many selectors cost no more memory than they take to write.
pub(crate) addresses: Rc<[Interval]>,
pub(super) addresses: Rc<[Interval]>,
/// Protocol that the rule selects, or `None` for every protocol.
pub(crate) protocol: Option<NativeProtocol>,
pub(super) protocol: Option<NativeProtocol>,
/// Ports that the rule selects, or `None` for every port. Only TCP and UDP rules have ports.
pub(crate) ports: Option<PortRange>,
pub(super) ports: Option<PortRange>,
}

/// Returns an [`ErrorKind::Unsupported`](crate::ErrorKind::Unsupported) error with `message`.
Expand All @@ -123,7 +123,7 @@ fn invalid(message: impl Into<String>) -> Error {

/// Parses the CIDR `value` of the field `description`, as `_network` does: the network of an
/// IPv4 or IPv6 interface without an IPv6 scope.
pub(crate) fn network(value: &str, description: &str) -> Result<Network> {
pub(super) fn network(value: &str, description: &str) -> Result<Network> {
address::parse_interface(value).map_err(|error| {
invalid(match error {
InterfaceError::Invalid => {
Expand All @@ -139,7 +139,7 @@ pub(crate) fn network(value: &str, description: &str) -> Result<Network> {
/// Returns the family of the destination `description`, whose network is `parent`, and the
/// addresses that it holds outside `exceptions`, as `_subtract_exclusions` does. Each exception,
/// which is parsed when it is read, must lie within `parent`.
pub(crate) fn destination(
pub(super) fn destination(
parent: Network,
exceptions: impl IntoIterator<Item = Result<Network>>,
description: &str,
Expand All @@ -162,7 +162,7 @@ pub(crate) fn destination(

/// Returns the addresses of each family that `destinations`, each a family and its addresses,
/// select, for the families that have destinations, as an MXC rule's `to` selects them.
pub(crate) fn group_destinations(
pub(super) fn group_destinations(
destinations: &[(Family, Vec<Interval>)],
) -> Vec<(Family, Vec<Interval>)> {
Family::ALL
Expand Down Expand Up @@ -209,7 +209,7 @@ fn port_range(start: PortValue, end: Option<PortValue>, description: &str) -> Re
/// Returns the selectors of the port fields, `port` and the optional `endPort`, of
/// `description`, an MXC port selector or a legacy flat rule whose protocol is valid, as the rest
/// of `_parse_mxc_port` does. ICMP takes no port.
pub(crate) fn port_selectors(
pub(super) fn port_selectors(
protocol: RuleProtocol,
port: Option<PortValue>,
end_port: Option<PortValue>,
Expand Down Expand Up @@ -238,7 +238,7 @@ pub(crate) fn port_selectors(
/// `destinations`, which the rules share, as `_parse_mxc_rule` and `_parse_legacy_rule` build
/// them. Without destinations, the rules select every address of both families, and without
/// selectors every protocol and port.
pub(crate) fn combine(
pub(super) fn combine(
destinations: Option<Vec<(Family, Vec<Interval>)>>,
selectors: Option<Vec<Selector>>,
) -> Vec<Rule> {
Expand Down Expand Up @@ -278,14 +278,14 @@ fn too_many_rules(category: &str) -> Error {

/// Lowers the rules of the action `category` into native rules, in the order in which the
/// compiler sorts them, as `_compile_category` does once it has parsed them.
pub(crate) fn lower(rules: &[Rule], category: &str) -> Result<Vec<String>> {
pub(super) fn lower(rules: &[Rule], category: &str) -> Result<Vec<String>> {
lower_within(rules, category, MAX_RULES)
}

/// Lowers the rules of the action `category` as [`lower`] does, but fails once they need more
/// than `limit` native rules rather than [`MAX_RULES`]: tests lift the limit to learn how many
/// native rules an action needs.
pub(crate) fn lower_within(rules: &[Rule], category: &str, limit: usize) -> Result<Vec<String>> {
pub(super) fn lower_within(rules: &[Rule], category: &str, limit: usize) -> Result<Vec<String>> {
// A native rule matches only its own family, so each family is lowered on its own, within
// the action's shared limit.
let mut lowered: Vec<NativeRule> = Vec::new();
Expand Down
Loading