diff options
-rw-r--r-- | src/net/if_.rs | 29 |
1 files changed, 7 insertions, 22 deletions
diff --git a/src/net/if_.rs b/src/net/if_.rs index d62af4fa..7edfc998 100644 --- a/src/net/if_.rs +++ b/src/net/if_.rs @@ -5,30 +5,15 @@ use libc; use libc::c_uint; -use std::ffi::{CString, NulError}; -use ::{Result, Error}; +use {Result, Error, NixPath}; /// Resolve an interface into a interface number. -pub fn if_nametoindex(name: &str) -> Result<c_uint> { - let name = match CString::new(name) { - Err(e) => match e { NulError(..) => { - // A NulError indicates that a '\0' was found inside the string, - // making it impossible to create valid C-String. To avoid having - // to create a new error type for this rather rare case, - // nix::Error's invalid_argument() constructor is used. - // - // We match the NulError individually here to ensure to avoid - // accidentally returning invalid_argument() for errors other than - // NulError (which currently don't exist). - return Err(Error::invalid_argument()); - }}, - Ok(s) => s - }; +pub fn if_nametoindex<P: ?Sized + NixPath>(name: &P) -> Result<c_uint> { + let if_index = try!(name.with_nix_path(|name| unsafe { libc::if_nametoindex(name.as_ptr()) })); - let if_index; - unsafe { - if_index = libc::if_nametoindex(name.as_ptr()); + if if_index == 0 { + Err(Error::last()) + } else { + Ok(if_index) } - - if if_index == 0 { Err(Error::last()) } else { Ok(if_index) } } |