Uh oh!
There was an error while loading. Please reload this page.
std: net: Add function to return the system hostname - #135141
Conversation
rustbot
commented
Jan 6, 2025
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.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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
2748acdCompareorowith2os
commented
Jan 6, 2025
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
a6af22fCompare
This comment has been minimized.
This comment has been minimized.
a6af22f to
518f66cCompare
This comment has been minimized.
This comment has been minimized.
b942cde to
2cfa066Compare
This comment has been minimized.
This comment has been minimized.
2cfa066 to
9761a27Compare
This comment has been minimized.
This comment has been minimized.
ff9bb72 to
141870cCompare
This comment has been minimized.
This comment has been minimized.
5f1419e to
6d9b400Compareorowith2os
commented
Jan 16, 2025
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. |
joboet
commented
Jan 17, 2025
Right, that covers most platforms. Some targets are still missing, though (to find these I basically went through the directories in
|
joshtriplett
commented
Jan 17, 2025
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. |
orowith2os
commented
Jan 17, 2025
@joboet would it be fine to mark them as TODO in the tracking issue? |
joboet
commented
Jan 17, 2025
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). |
joboet
commented
Jan 21, 2025
T-libs-api, what do you think? I think this should be clarified before merging, since @rustbot label +I-libs-api-nominated |
Amanieu
commented
Jan 21, 2025
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 |
orowith2os
commented
Jan 21, 2025
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. |
joboet
commented
Jan 24, 2025
@orowith2os I've opened orowith2os#1 on your fork and implemented the Windows bit there. |
…code resilient against bad platform behaviour
joboet
commented
Jan 24, 2025
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.
bors
commented
Feb 6, 2025
☔ The latest upstream changes (presumably #136572) made this pull request unmergeable. Please resolve the merge conflicts. |
ChrisDenton
commented
Feb 16, 2025
This needs to be rebased but then I think it'll be good to be merged. |
Dylan-DPC
commented
Aug 12, 2025
@orowith2os any updates on this? this just requires a rebase and seems good to merge. |
orowith2os
commented
Aug 12, 2025
I don't have a dev environment available to rebase with, but if someone else wants to, go for it. |
tbu-
commented
Dec 11, 2025
This looks like it's superseded by #146937 and can be closed. |
Resolvesrust-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.