about summary refs log tree commit diff
path: root/library/std/src/net
AgeCommit message (Collapse)AuthorLines
2025-09-29Rollup merge of #146937 - joboet:gethostname, r=Mark-SimulacrumMatthias Krüger-1/+27
std: implement `hostname` Resolves https://github.com/rust-lang/libs-team/issues/330 Tracking issue: https://github.com/rust-lang/rust/issues/135142 This is based on rust-lang/rust#135141, but I've reimplemented the UNIX version, which now: * uses `sysconf(_SC_HOST_NAME_MAX)` as an initial buffer length * returns `OutOfMemory` if the `Vec` allocation fails * retries the operation if it detects that the name returned by `gethostname` was truncated Additionally, as part of the rebase, I had to move some WinSock abstractions (initialisation and error access) to `sys::pal` so that they can be accessed from `sys::net::hostname`. CC ``@orowith2os`` (and thank you for your work!)
2025-09-29std: implement `hostname`joboet-1/+27
2025-09-24Explicitly note `&[SocketAddr]` impl of `ToSocketAddrs`.Adam Harvey-0/+2
Although the examples below this list do imply that there's an impl of `ToSocketAddrs` for `&[SocketAddr]`, it's not actually noted in the list of default implementations.
2025-09-19std: simplify host lookupjoboet-21/+20
2025-09-08std: make address resolution weirdness local to SGXjoboet-26/+6
2025-06-16Set MSG_NOSIGNAL for UnixSteamMichał Łowicki-0/+6
https://github.com/rust-lang/rust/issues/139956 fix
2025-04-04ToSocketAddrs: fix typoRalf Jung-1/+1
2025-03-17Update test for SGX now implementing read_bufThalia Archibald-6/+2
In #108326, `read_buf` was implemented for a variety of types, but SGX was saved for later. Update a test from then, now that #137355 implemented it for SGX types.
2025-03-13Rollup merge of #136842 - randomPoison:trusty-libstd-v3, r=ChrisDentonMatthias Krüger-2/+4
Add libstd support for Trusty targets This PR adds support for `alloc` and `std` for the Trusty targets based on the internal patches used in Android. The original patches can be seen [here](https://android.googlesource.com/toolchain/android_rust/+/refs/heads/main/patches/development/rustc-0023-Add-Trusty-OS-support-to-Rust-std.patch) and [here](https://android.googlesource.com/toolchain/android_rust/+/refs/heads/main/patches/development/rustc-0054-Add-std-os-fd-support-for-Trusty.patch). Please let me know if there's any additional context I need to add.
2025-03-10Disable unsupported testsNicole LeGare-2/+4
Unclear why this needs to be done manually and is not done by the existing Trusty patches.
2025-03-04Fix test hangs on AIXHenry Jiang-4/+19
2025-02-12std: replace the `FromInner` implementation for addresses with private ↵joboet-73/+2
conversion functions Having these implementation available crate-wide means that platforms not using sockets for their networking code have to stub out the libc definitions required to support them. This PR moves the conversions to private helper functions that are only available where actually needed. I also fixed the signature of the function converting from a C socket address to a Rust one: taking a reference to a `sockaddr_storage` resulted in unsound usage inside `LookupHost::next`, which could create a reference to a structure smaller than `sockaddr_storage`. Thus I've replaced the argument type with a pointer and made the function `unsafe`.
2025-02-06tests(std/net): remove outdated `base_port` calculationJieyou Xu-30/+3
This was never modified since `std::net` was originally introduced, when each CI job was running multiple jobs concurrently which caused issues with fighting over the same ports. This is not the case in the current CI infrastructure, so remove this relic.
2025-02-02std: move network code into `sys`joboet-4/+5
As per #117276, this PR moves `sys_common::net` and the `sys::pal::net` into the newly created `sys::net` module. In order to support #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.
2024-11-26std: update internal uses of `io::const_error!`joboet-4/+2
2024-09-30Rollup merge of #129638 - nickrum:wasip2-net, r=alexcrichtonTrevor Gross-6/+55
Hook up std::net to wasi-libc on wasm32-wasip2 target One of the improvements of the `wasm32-wasip2` target over `wasm32-wasip1` is better support for networking. Right now, p2 is just re-using the `std::net` implementation from p1. This PR adds a new net module for p2 that makes use of net from `sys_common` and calls wasi-libc functions directly. There are currently a few limitations: - Duplicating a socket is not supported by WASIp2 (directly returns an error) - Peeking is not yet implemented in wasi-libc (we could let wasi-libc handle this, but I opted to directly return an error instead) - Vectored reads/writes are not supported by WASIp2 (the necessary functions are available in wasi-libc, but they call WASIp1 functions which do not support sockets, so I opted to directly return an error instead) - Getting/setting `TCP_NODELAY` is faked in wasi-libc (uses the fake implementation instead of returning an error) - Getting/setting `SO_LINGER` is not supported by WASIp2 (directly returns an error) - Setting `SO_REUSEADDR` is faked in wasi-libc (since this is done from `sys_common`, the fake implementation is used instead of returning an error) - Getting/setting `IPV6_V6ONLY` is not supported by WASIp2 and will always be set for IPv6 sockets (since this is done from `sys_common`, wasi-libc will return an error) - UDP broadcast/multicast is not supported by WASIp2 (since this is configured from `sys_common`, wasi-libc will return appropriate errors) - The `MSG_NOSIGNAL` send flag is a no-op because there are no signals in WASIp2 (since explicitly setting this flag would require a change to `sys_common` and the result would be exactly the same, I opted to not set it) Do those decisions make sense? While working on this PR, I noticed that there is a `std::os::wasi::net::TcpListenerExt` trait that adds a `sock_accept()` method to `std::net::TcpListener`. Now that WASIp2 supports standard accept, would it make sense to remove this? cc `@alexcrichton`
2024-09-29Rollup merge of #130743 - YohDeadfall:net-nonblocking-doc, r=Mark-SimulacrumGuillaume Gomez-2/+2
Clarifications for set_nonblocking methods Closes #129903. The issue mentions that `send`, `recv` and other operations are interpreted by some users as methods of `TcpSocket` which led to confusion since it hasn't them. To fix it I added "system" into the documentation as being more precise for two reasons: * it's makes it clear that these names are system operations; * it doesn't point to the location of these methods like `libc` because not every system is POSIX compatible.
2024-09-29Fix std tests for wasm32-wasip2 targetNicola Krumschmidt-6/+55
2024-09-27Reference UNSPECIFIED instead of INADDR_ANY in join_multicast_v4Asger Hautop Drewsen-2/+2
2024-09-23Clarifications for set_nonblocking methodsYoh Deadfall-2/+2
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-3/+3
2024-07-29Reformat `use` declarations.Nicholas Nethercote-26/+15
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-06-24Replace `MaybeUninit::uninit_array()` with array repeat expression.Kevin Reid-1/+1
This is possible now that inline const blocks are stable; the idea was even mentioned as an alternative when `uninit_array()` was added: <https://github.com/rust-lang/rust/pull/65580#issuecomment-544200681> > if it’s stabilized soon enough maybe it’s not worth having a > standard library method that will be replaceable with > `let buffer = [MaybeUninit::<T>::uninit(); $N];` Const array repetition and inline const blocks are now stable (in the next release), so that circumstance has come to pass, and we no longer have reason to want `uninit_array()` other than convenience. Therefore, let’s evaluate the inconvenience by not using `uninit_array()` in the standard library, before potentially deleting it entirely.
2024-06-21fix issue numberTristan Guichaoua-5/+5
2024-03-20SeqCst->Relaxed in std::net::test.Mara Bos-2/+2
Relaxed is enough to have fetch_add(1) return each value only once (until it wraps around).
2024-03-06fix `close_read_wakes_up` testLukas Markeffsky-15/+18
2023-12-10remove redundant importssurechen-2/+1
detects redundant imports that can be eliminated. for #117772 : In order to facilitate review and modification, split the checking code and removing redundant imports code into two PR.
2023-10-03Auto merge of #105394 - Patiga:improve-udpsocket-docs, r=workingjubileebors-1/+20
Improve UdpSocket documentation I tried working with `UdpSocket` and ran into `EINVAL` errors with no clear indication of what causes the error. Also, it was uncharacteristically hard to figure this module out, compared to other Rust `std` modules. 1. `send` and `send_to` return a `usize` This one is just clarity. Usually, returned `usize`s indicate that the buffer might have only been sent partially. This is not the case with UDP. Since that `usize` must always be `buffer.len()`, I have documented that. 2. `bind` limits `connect` and `send_to` When you bind to a limited address space like localhost, you can only `connect` to addresses in that same address space. Error kind: `AddrNotAvailable`. 3. `connect`ing to localhost locks you to localhost On Linux, if you first `connect` to localhost, subsequent `connect`s to non-localhost addresses fail. Error kind: `InvalidInput`. For debugging the third one, it was really hard to find someone else who already had that problem. I only managed to find this thread: https://www.mail-archive.com/netdev@vger.kernel.org/msg159519.html
2023-08-22Improve UdpSocket documentationPatiga-1/+20
I tried working with `UdpSocket` and ran into `EINVAL` errors with no clear indication of what causes the error. Also, it was uncharacteristically hard to figure this module out, compared to other Rust `std` modules. 1. `send` and `send_to` return a `usize` This one is just clarity. Usually, returned `usize`s indicate that the buffer might have only been sent partially. This is not the case with UDP. Since that `usize` must always be `buffer.len()`, I have documented that. 2. `bind` limits `connect` and `send_to` When you bind to a limited address space like localhost, you can only `connect` to addresses in that same address space. Error kind: `AddrNotAvailable`. 3. `connect`ing to localhost locks you to localhost On Linux, if you first `connect` to localhost, subsequent `connect`s to non-localhost addresses fail. Error kind: `InvalidInput`. Co-authored-by: Jubilee <46493976+workingjubilee@users.noreply.github.com>
2023-08-22std: net: skip tests on xousSean Cross-2/+2
Network functionality is not yet ready for merging. Signed-off-by: Sean Cross <sean@xobs.io>
2023-07-28inline trivial (noop) flush callsThom Chiovoloni-0/+2
2023-06-20Rollup merge of #112464 - eval-exec:exec/fix-connect_timeout-overflow, ↵Guillaume Gomez-0/+11
r=ChrisDenton Fix windows `Socket::connect_timeout` overflow This PR want to close #112405 - [x] add unit test
2023-06-20Remove useless unit testsEval EXEC-21/+0
2023-06-20Ignore `connect_timeout` unit test on SGX platformEval EXEC-0/+1
Co-authored-by: Chris Denton <christophersdenton@gmail.com>
2023-06-18Add unit test to connect to an unreachable addressEval EXEC-0/+11
Signed-off-by: Eval EXEC <execvy@gmail.com>
2023-06-18Add unit test for `TcpStream::connect_timeout`Eval EXEC-0/+20
Signed-off-by: Eval EXEC <execvy@gmail.com>
2023-06-14Fix `SocketAddrV6: Display` testsltdk-1/+1
2023-05-13add examples of port 0 binding behaviorkirk-0/+18
2023-05-01Inline AsInner implementationsKonrad Borowski-0/+3
2023-04-08fix(tcp): remove redundant `format!` macro callSergei Belokon-1/+1
2023-04-06chore(tcp): change the hardcoded port number to `port` varSergei Belokon-1/+1
The `listen_on` function in the example has a `port` option but doesn't use it
2023-03-06Implement read_buf for a few more typesTomasz Miąsko-2/+36
Implement read_buf for TcpStream, Stdin, StdinLock, ChildStdout, ChildStderr (and internally for AnonPipe, Handle, Socket), so that it skips buffer initialization. The other provided methods like read_to_string and read_to_end are implemented in terms of read_buf and so benefit from the optimization as well. This commit also implements read_vectored and is_read_vectored where applicable.
2023-03-02Auto merge of #106673 - flba-eb:add_qnx_nto_stdlib, r=workingjubileebors-2/+8
Add support for QNX Neutrino to standard library This change: - adds standard library support for QNX Neutrino (7.1). - upgrades `libc` to version `0.2.139` which supports QNX Neutrino `@gh-tr` ⚠️ Backtraces on QNX require https://github.com/rust-lang/backtrace-rs/pull/507 which is not yet merged! (But everything else works without these changes) ⚠️ Tested mainly with a x86_64 virtual machine (see qnx-nto.md) and partially with an aarch64 hardware (some tests fail due to constrained resources).
2023-02-28Add QNX Neutrino support to libstdFlorian Bartels-2/+8
Co-authored-by: gh-tr <troach@qnx.com>
2023-02-26Move IpAddr and SocketAddr to coreLinus Färnstrand-4468/+26
2023-01-31Replace unwrap with ? in TcpListener docDaniel Chmielewski-1/+1
2023-01-25Stabilize the const_socketaddr featureLinus Färnstrand-13/+13
2023-01-21Remove unnecessary `&format!`Nikolai Vazquez-8/+8
These were likely from before the `PartialEq<str>` impl for `&String`.
2022-12-30Rollup merge of #104182 - gabhijit:ipv6-in6addr-any-doc-fix, r=m-ou-seMatthias Krüger-0/+9
`IN6ADDR_ANY_INIT` and `IN6ADDR_LOOPBACK_INIT` documentation. Added documentation for IPv6 Addresses `IN6ADDR_ANY_INIT` also known as `in6addr_any` and `IN6ADDR_LOOPBACK_INIT` also known as `in6addr_loopback` similar to `INADDR_ANY` for IPv4 Addresses.
2022-11-13Remove unused diagnostic itemsMaybe Waffle-2/+0