-
Notifications
You must be signed in to change notification settings - Fork 691
Added if_nametoindex (and necessary module based on Cs net/if.h) #245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
f3d1030
8972270
71c6836
5ef9689
55b460b
2cb7878
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//! Network interface name resolution. | ||
//! | ||
//! Uses Linux and/or POSIX functions to resolve interface names like "eth0" | ||
//! or "socan1" into device numbers. | ||
|
||
use libc::{c_char, c_uint}; | ||
use std::ffi::{CString, NulError}; | ||
use std::io; | ||
|
||
/// Error that can occur during interface name resolution. | ||
#[derive(Debug)] | ||
pub enum NameToIndexError { | ||
/// Failed to allocate a C-style string to for the syscall | ||
NulError, | ||
IOError(io::Error), | ||
} | ||
|
||
impl From<NulError> for NameToIndexError { | ||
fn from(_: NulError) -> NameToIndexError { | ||
NameToIndexError::NulError | ||
} | ||
} | ||
|
||
impl From<io::Error> for NameToIndexError { | ||
fn from(e: io::Error) -> NameToIndexError { | ||
NameToIndexError::IOError(e) | ||
} | ||
} | ||
|
||
extern { | ||
fn if_nametoindex(ifname: *const c_char) -> c_uint; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use this from libc instead of making our own extern: http://rust-lang-nursery.github.io/libc/x86_64-unknown-linux-gnu/libc/fn.if_nametoindex.html There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Damn, I could've sworn I already replaced it. Fixed in 8972270 |
||
} | ||
|
||
/// Resolve an interface into a interface number. | ||
pub fn name_to_index(name: &str) -> Result<c_uint, NameToIndexError> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A few things:
This would look like: pub fn if_nametoindex<S: ?Sized + NixPath>(target: &P) -> Result<c_uint> {
let res = try!(name.with_nix_path(|name| { /* rest of body here */ }));
from_ffi(res)
} The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Fixed in 2cb7878
Fixed in 5ef9689
That currently seems a little messy and missing an implementation for
If i am reading this right, won't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is a good consideration. I've copied your note over to to #221 where we're discussing string handling in nix. |
||
let name = try!(CString::new(name)); | ||
|
||
let if_index; | ||
unsafe { | ||
if_index = if_nametoindex(name.as_ptr()); | ||
} | ||
|
||
if if_index == 0 { | ||
return Err(NameToIndexError::from(io::Error::last_os_error())); | ||
} | ||
|
||
Ok(if_index) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod if_; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you add a tiny comment explaining the name is to avoid the keyword issue? (I'm assuming that's why, anyway) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you use
nix:Error
instead of your own error?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked into it before implementing my own, but there's the NulError to take care of. I believe it may be worthwhile to add that to nix::Error though? It's documented in https://doc.rust-lang.org/std/ffi/struct.NulError.html.
Alternatives are just returning not found if a NulError occured or, as a last resort, unpacking (I'd like to avoid that).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A fourth option is now in 71c6836: Just returned Error::invalid_argument() if a string containig a null-byte is passed.