Skip to content

Commit

Permalink
Merge pull request containers#269 from Luap99/ipnet
Browse files Browse the repository at this point in the history
bridge/macvlan: use IpNet::new() to create subnet
  • Loading branch information
flouthoc authored Mar 28, 2022
2 parents 0e4440a + 1522b20 commit 4e2f6af
Showing 1 changed file with 22 additions and 52 deletions.
74 changes: 22 additions & 52 deletions src/network/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,33 +93,18 @@ impl Core {
for (idx, subnet) in network.subnets.iter().flatten().enumerate() {
let subnet_mask_cidr = subnet.subnet.prefix_len();
if let Some(gw) = subnet.gateway {
let gw_net = match gw {
IpAddr::V4(gw4) => match ipnet::Ipv4Net::new(gw4, subnet_mask_cidr) {
Ok(dest) => ipnet::IpNet::from(dest),
Err(err) => {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"failed to parse address {}/{}: {}",
gw4, subnet_mask_cidr, err
),
))
}
},
IpAddr::V6(gw6) => match ipnet::Ipv6Net::new(gw6, subnet_mask_cidr) {
Ok(dest) => ipnet::IpNet::from(dest),
Err(err) => {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"failed to parse address {}/{}: {}",
gw6, subnet_mask_cidr, err
),
))
}
},
let gw_net = match ipnet::IpNet::new(gw, subnet_mask_cidr) {
Ok(dest) => dest,
Err(err) => {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"failed to parse address {}/{}: {}",
gw, subnet_mask_cidr, err
),
))
}
};

gw_ipaddr_vector.push(gw_net)
}

Expand Down Expand Up @@ -394,33 +379,18 @@ impl Core {
// Only add gateway to route if macvlan is not marked as an internal network
if !network.internal {
if let Some(gw) = subnet.gateway {
let gw_net = match gw {
IpAddr::V4(gw4) => match ipnet::Ipv4Net::new(gw4, subnet_mask_cidr) {
Ok(dest) => ipnet::IpNet::from(dest),
Err(err) => {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"failed to parse address gateway address while configuring macvlan {}/{}: {}",
gw4, subnet_mask_cidr, err
),
))
}
},
IpAddr::V6(gw6) => match ipnet::Ipv6Net::new(gw6, subnet_mask_cidr) {
Ok(dest) => ipnet::IpNet::from(dest),
Err(err) => {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"failed to parse address gateway address while configuring macvlan {}/{}: {}",
gw6, subnet_mask_cidr, err
),
))
}
},
let gw_net = match ipnet::IpNet::new(gw, subnet_mask_cidr) {
Ok(dest) => dest,
Err(err) => {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"failed to parse address {}/{}: {}",
gw, subnet_mask_cidr, err
),
))
}
};

gw_ipaddr_vector.push(gw_net)
}
}
Expand Down

0 comments on commit 4e2f6af

Please sign in to comment.