std: net: Add function to return the system hostname#135141
std: net: Add function to return the system hostname#135141orowith2os wants to merge 2 commits intorust-lang:mainfrom
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.
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 |
ChrisDenton
left a comment
There was a problem hiding this comment.
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.
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.
|
☔ The latest upstream changes (presumably #136572) made this pull request unmergeable. Please resolve the merge conflicts. |
|
This needs to be rebased but then I think it'll be good to be merged. |
|
@orowith2os any updates on this? this just requires a rebase and seems good to merge. |
|
I don't have a dev environment available to rebase with, but if someone else wants to, go for it. |
|
This looks like it's superseded by #146937 and can be closed. |
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.