Skip to content
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

Add ip networks for interface #1218

Merged
merged 12 commits into from Mar 8, 2024

Conversation

gongzhengyang
Copy link
Contributor

Add IP networks for interface for #1104

@gongzhengyang
Copy link
Contributor Author

图片
rust nightly on freebsd 13 fails occasionally ?

@GuillaumeGomez
Copy link
Owner

Yes, it's "normal". Don't worry about it. ^^'

@GuillaumeGomez
Copy link
Owner

Thanks for working on this! How complicated would it be to not use crates for getting this feature? And also, please add a test.

@gongzhengyang
Copy link
Contributor Author

gongzhengyang commented Mar 5, 2024

I will try for not use crates and add tests

@GuillaumeGomez
Copy link
Owner

If it's too tricky, it can be done as a second step. The most important part here is the test.

@gongzhengyang
Copy link
Contributor Author

It will be wonderful if not use crates for getting this feature. Worth to try.

@GuillaumeGomez
Copy link
Owner

Definitely agree. 👍

@gongzhengyang
Copy link
Contributor Author

@GuillaumeGomez crates removed. I added some tests for thie feature. Not sure tests is enough or not.

)
)
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test to ensure that supported platforms have at least one IP network for an interface. You can filter out using:

if !crate::IS_SUPPORTED_SYSTEM {
    return;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Check added.


// Safety: addrs.as_mut_ptr() is valid, it points to addrs.
if unsafe { libc::getifaddrs(addrs.as_mut_ptr()) } != 0 {
return ifaces;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a sysinfo_debug call here to display the returned error saying that getifaddrs failed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope this call will help for debug when need

@@ -116,3 +126,261 @@ pub(crate) fn get_interface_address() -> Result<InterfaceAddressIterator, String
}
}
}

pub(crate) fn get_interface_ip_networks() -> HashMap<String, HashSet<IpNetwork>> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be better to mark the entire function as unsafe: unsafe isn't only limited to a C call or a pointer access but "lives" until the unsafe data has been converted into Rust so to speak. So having local unsafe blocks doesn't make much sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now use inner function for unsafe code.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make the function itself unsafe, don't wrap, it's giving the impression that the function is safe although it's not. ^^'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

#[cfg(test)]
mod tests {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are very appreciated, well done! 👍

@GuillaumeGomez
Copy link
Owner

A few small things to update and a test to add and then it'll be ready for merge. :)

@gongzhengyang
Copy link
Contributor Author

Update and tests all pushed.Thanks for your advice, much learned.

src/common.rs Outdated

/// return the addr of the IpNetwork
pub fn addr(&self) -> IpAddr {
self.addr
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, better make the fields public (and document what they are).

src/common.rs Outdated

impl IpNetwork {
/// build new IpNetwork
pub fn new(addr: IpAddr, prefix: u8) -> Self {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the fields public, this constructor won't be needed anymore too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I removed this constructor.

src/lib.rs Outdated
return;
}
let networks = Networks::new_with_refreshed_list();
if networks.is_empty() || networks.iter().any(|(_, n)| !n.ip_networks().is_empty()) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why checking networks.is_empty()? Isn't it an issue if no networks are present?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I once encountered a situation where there was no network interface in vmware windows virtual machine.Not checking networks.is_empty() is better for find problems.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind removing this test to check if it fails in the CI please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it.

@gongzhengyang
Copy link
Contributor Author

All updates pushed.

@GuillaumeGomez
Copy link
Owner

Great work! Thanks a lot for this new feature!

@GuillaumeGomez GuillaumeGomez merged commit fcdac16 into GuillaumeGomez:master Mar 8, 2024
65 of 67 checks passed
@ravenclaw900
Copy link

I've been waiting for this feature, thanks for implementing it! Is there any possibility of a release on crates.io in the near future?

@GuillaumeGomez
Copy link
Owner

Since it's an API addition, I'll make a new big release. I'm taking this opportunity to make other breaking changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants