sanpii / lxc-rs

Linux Containers API for rustaceans

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

lxc::Container::get_ips() design improvement

koutheir opened this issue · comments

The lxc::Container::get_ips() API encapsulates the get_ips() of lxc_sys::lxc_container.

There are multiples issues with the design of lxc::Container::get_ips():

  • It returns Vec<String> instead of Vec<std::net::IpAddr>. IP addresses are rarely used simply as strings. They're often parsed before becoming useful.
  • The first argument is interfaces: &str:
    • The raw get_ips() gets an interface: *const c_char. Using the plural form "interfaces" is misleading.
    • The raw get_ips() can accept NULL as the value for this parameter, which has its own semantics in the implementation of get_ips() in liblxc.
  • The second argument is family: &str: the raw get_ips() can accept NULL as the value for this parameter, which has its own semantics in the implementation of get_ips() in liblxc.
  • The third argument is scope: i32, but the raw get_ips() gets a scope: c_int. Assuming that a C int maps always to i32 is a bad idea, and is not always true.

Given all these points, I suggest changing the prototype of lxc::Container::get_ips() to the following:

pub fn get_ips(
    &self,
    interface: Option<&str>,
    family: Option<&str>,
    scope: std::os::raw::c_int)
    -> Result<Vec<std::net::IpAddr>>
{
    /*... */
}
commented

I made the PR but I didn’t try it: #14

commented

It’s work fine:

Container state: RUNNING
Container PID: 32254
Interfaces: ["eth0", "lo"]
IPs: [10.0.3.206]

The only issue remaining is the unwrap() calls that panic if liblxc generates an error. This is not reasonable for a library.

commented

The only issue remaining is the unwrap() calls that panic if liblxc generates an error. This is not reasonable for a library.

There is many unwrap in my code, I work on a redesign of errors handling.

Until panics are reduced to a minimum, the only crate I can currently use is lxc-sys, basically reimplementing the ergonomic structures while reporting liblxc errors as normal Results.

commented

@koutheir The PR is here: #16