-
Notifications
You must be signed in to change notification settings - Fork 13k
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
std: net: Add function to return the system hostname #135141
base: master
Are you sure you want to change the base?
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @joboet (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
64aec6c
to
2748acd
Compare
forgive all the updates, I was having troubles updating my branch properly. Figured it out. |
This comment has been minimized.
This comment has been minimized.
2748acd
to
a6af22f
Compare
This comment has been minimized.
This comment has been minimized.
a6af22f
to
518f66c
Compare
This comment has been minimized.
This comment has been minimized.
b942cde
to
2cfa066
Compare
This comment has been minimized.
This comment has been minimized.
2cfa066
to
9761a27
Compare
This comment has been minimized.
This comment has been minimized.
ff9bb72
to
141870c
Compare
This comment has been minimized.
This comment has been minimized.
10ff018
to
5f1419e
Compare
@rustbot ready |
5f1419e
to
6d9b400
Compare
Sorry for that last push - I updated the doc comment on gethostname to specify when it should be able to error out, so it's not just a black box. |
Right, that covers most platforms. Some targets are still missing, though (to find these I basically went through the directories in
|
DNS names can only be ASCII, but hostnames are not just DNS names. I've seen hostnames for phones and computers that have Unicode characters in them, and I would be unsurprised to discover hostnames that use local character sets. That said, if we define this to be a form of hostname that works as a valid DNS component, then 👍 for making it String. |
@joboet would it be fine to mark them as TODO in the tracking issue? |
For the tier 3 targets, yeah that's fine. The tier 2 targets are built in the merge CI, so they'll need to compile (stubbing the function out is fine). |
T-libs-api, what do you think? I think this should be clarified before merging, since @rustbot label +I-libs-api-nominated |
We discussed this in the libs-api meeting: on Windows this should call GetHostByNameW to get a UTF-16 string, which can then be converted to an |
Then I'll keep it as OsString, but I won't implement the Windows bits. I'll leave that to someone willing to pick it up. |
@orowith2os I've opened orowith2os#1 on your fork and implemented the Windows bit there. |
…code resilient against bad platform behaviour
I shouldn't review my own code, so |
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.
The code looks fine. Note though that Windows 7 doesn't have GetHostNameW
so it'd need. a fallback, However, it's also tier 3 so can be left to a follow up by the target maintainer.
// always enough. | ||
let mut buffer = [0; 256]; | ||
// SAFETY: these parameters specify a valid, writable region of memory. | ||
let ret = unsafe { c::GetHostNameW(buffer.as_mut_ptr(), buffer.len() as i32) }; |
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 guess my one nitpick is that maybe the max buffer len could be a constant instead of using as i32
, which is often a code smell but in this case it's narrowly scoped enough that I don't feel strongly about it.
Didn't know that there is a dedicated GetHostName function in WSA, would have expected GetComputerNameExW. Implemented since Windows 2000, and doesn't require WS2 init. |
As per rust-lang#117276, this PR moves `sys_common::net` and the `sys::pal::net` into the newly created `sys::net` module. In order to support rust-lang#135141, I've moved all the current network code into a separate `connection` module, future functions like `hostname` can live in separate modules. I'll probably do a follow-up PR and clean up some of the actual code, this is mostly just a reorganization.
As per rust-lang#117276, this PR moves `sys_common::net` and the `sys::pal::net` into the newly created `sys::net` module. In order to support rust-lang#135141, I've moved all the current network code into a separate `connection` module, future functions like `hostname` can live in separate modules. I'll probably do a follow-up PR and clean up some of the actual code, this is mostly just a reorganization.
Resolves rust-lang/libs-team#330
Tracking issue: #135142
Now you can retrieve the system hostname, without relying on anything other than
std
!This is my first pull request to the Rust standard library (or anything inside rust-lang/rust, period, quite possible rust-lang itself), so there are bound to be issues. I'll try my best to resolve them.