-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
std.net additions #3538
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 additions #3538
Conversation
|
|
||
| pub const IPPORT_RESERVED = 1024; | ||
|
|
||
| pub const IPPROTO_IP = 0; |
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.
We already have these as the misnamed PROTO_ constants see
Line 230 in 634f56e
| pub const PROTO_ip = 0o000; |
lib/std/os/bits/linux.zig
Outdated
| pub const EAI_SYSTEM = -11; | ||
| pub const EAI_OVERFLOW = -12; | ||
|
|
||
| pub const EAI_NODATA = -5; |
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.
These (and AI_ and NI_) are libc constants and don't belong under os/bits
lib/std/special/start.zig
Outdated
| } | ||
| } | ||
|
|
||
| const main_thread_tls_align = 32; |
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.
This change should probably go straight to master and not in this PR?
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.
Yeah, sure, I'll cherry pick it out and rebase before merging.
...address selection from musl libc
* delete the std/event/net directory * `std.event.Loop.waitUntilFdReadable` and related functions no longer have possibility of failure. On Linux, they fall back to poll() and then fall back to sleep(). * add some missing `noasync` decorations in `std.event.Loop` * redo the `std.net.Server` API. it's quite nice now, but shutdown does not work cleanly. There is a race condition with close() that I am actively working on. * move `std.io.OutStream` to its own file to match `std.io.InStream`. I started working on making `write` integrated with evented I/O, but it got tricky so I backed off and filed #3557. However I did integrate `std.os.writev` and `std.os.pwritev` with evented I/O. * add `std.Target.stack_align` * move networking tests to `lib/std/net/test.zig` * add `std.net.tcpConnectToHost` and `std.net.tcpConnectToAddress`. * rename `error.UnknownName` to `error.UnknownHostName` within the context of DNS resolution. * add `std.os.readv`, which is integrated with evented I/O. * `std.os.preadv`, is now integrated with evented I/O. * `std.os.accept4` now asserts that ENOTSOCK and EOPNOTSUPP never occur (misuse of API), instead of returning errors. * `std.os.connect` is now integrated with evented I/O. `std.os.connect_async` is gone. Just use `std.os.connect`. * fix false positive dependency loop regarding async function frames * add more compile notes to help when dependency loops occur in determining whether a function is async. * ir: change an assert to ir_assert to make it easier to find workarounds for when such an assert is triggered. In this case it was trying to parse an IPv4 address at comptime.
|
Some notes on the progress, I refreshed the "listen on a port, send bytes, receive bytes" test. example std.net.TcpServer API usage(this example only works in evented I/O mode): Lines 45 to 71 in b3c8041
std.net.TcpServer implementation(impl doesn't care if evented or blocking I/O, and notice there are no async or await keywords anywhere): Lines 1255 to 1348 in 1639724
stracenext stepsI'm planning to get as far as ability to download a file to disk over a http 1.0 connection, before trying to start merging the branch. Other POSIX's I expect to be relatively easy, however I know next to nothing about Windows networking and would certainly appreciate help with that. I'll probably be looking at libuv for inspiration. |
| } || os.UnexpectedError; | ||
|
|
||
| /// If this function succeeds, the returned `fs.File` is a caller-managed resource. | ||
| pub fn accept(self: *TcpServer) AcceptError!fs.File { |
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.
It's handy to know the IP address of the accepted socket for a TCP server. Not sure if you already plan to change this return type to include that information or not.
|
I noticed some changes to IP address parsing in this PR, and thought I'd add some thoughts in case you plan to make more changes here. It's worth noting all of the following are valid IPv6 addresses that I don't believe will parse as it stands due to the check here:
I also don't know if you intend to support IPv4 mapped or IPv4 compatible IPv6 addresses, but it would be nice to do so as they're quite common:
|
* Delete `std.net.TmpWinAddr`. I don't think that was ever meant to be a real thing. * Delete `std.net.OsAddress`. This abstraction was not helpful. * Rename `std.net.Address` to `std.net.IpAddress`. It is now an extern union of IPv4 and IPv6 addresses. * Move `std.net.parseIp4` and `std.net.parseIp6` to the `std.net.IpAddress` namespace. They now return `IpAddress` instead of `u32` and `std.net.Ip6Addr`, which is deleted. * Add `std.net.IpAddress.parse` which accepts a port and parses either an IPv4 or IPv6 address. * Add `std.net.IpAddress.parseExpectingFamily` which additionally accepts a `family` parameter. * `std.net.IpAddress.initIp4` and `std.net.IpAddress.initIp6` are improved to directly take the address fields instead of a weird in-between type. * `std.net.IpAddress.port` is renamed to `std.net.IpAddress.getPort`. * Added `std.net.IpAddress.setPort`. * `os.sockaddr` struct on all targets is improved to match the corresponding system struct. Previously I had made it a union of sockaddr_in, sockaddr_in6, and sockaddr_un. The new abstraction for this is now `std.net.IpAddress`. * `os.sockaddr` and related bits are added for Windows. * `os.sockaddr` and related bits now have the `zero` fields default to zero initialization, and `len` fields default to the correct size. This is enough to abstract the differences across targets, and so no more switch on the target OS is needed in `std.net.IpAddress`. * Add the missing `os.sockaddr_un` on FreeBSD and NetBSD. * `std.net.IpAddress.initPosix` now takes a pointer to `os.sockaddr`.
So far this is DNS resolution stuff.