Skip to content

quic: the client can choose the local address and device to send from - #56

Open
pgit wants to merge 2 commits into
masterfrom
quic-local-endpoint
Open

pgit wants to merge 2 commits into
masterfrom
quic-local-endpoint

Conversation

@pgit

@pgit pgit commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Closes #54. Of the two ways the issue leaves open, this takes the fields in quic::Config, not a hook on the socket: they are what c-ares has (ares_set_local_ip4()/_ip6(), ares_set_local_dev()), and the descriptor stays inside the library. The hook can still come if arbitrary socket options are ever needed.

API

  • quic::Config::local_endpoint, an optional UdpEndpoint, is bound before the connect, with port 0 for any port. Only the server's endpoints of its family are tried; if none is, connect() fails with errc::address_family_not_supported.
  • quic::Config::device is the interface to send through, SO_BINDTODEVICE, Linux only (errc::operation_not_supported elsewhere). No interface of that name is errc::no_such_device.
  • quic::Connection::remote_endpoint() tells the server where a connection comes from, and the client where it goes. Without it, only the coloured log_prefix() showed that.

A socket that cannot be set up now fails connect() with what the system said, errc::address_not_available for an address that is not this host's, say. Every failure used to be errc::connection_refused, which is still what a failed handshake reports.

Implementation

  • QuicClientSession::init() is split in two: open() sets up the socket (open, device, bind, connect) and returns its error, init() makes the connection on it as before. quic::async_connect() skips the endpoints of the other family and throws what the last attempt failed with.
  • io::bind_to_device() is new in net.hpp, defined once on the native handle. It refuses a name with a NUL in it or longer than 15 characters: the kernel would cut it short silently, and bind to whatever interface has the shorter name.
  • QuicSession::remote_endpoint() reads ngtcp2's current path, on either side.
  • The HTTP/3 and TCP clients are unchanged: client::Config has no local endpoint for TCP either.

Tests

test/test_quic.cpp, both write modes:

  • From a local address, 127.0.0.3: the server sees the connection come from it (it is at 127.0.0.2 itself, and the system would send from 127.0.0.1).
  • From a local address and port: the server sees both.
  • An address that is not this host's (192.0.2.1) fails with address_not_available, one of the other family (::1) with address_family_not_supported.
  • Through lo, a connection works; nonexistent0, "lo\0x" (which the kernel would take as lo) and a 16-character name fail with no_such_device.

No test proves that the device changes where packets go. A UDP socket bound to eth0 does not reach a server on loopback, but it sends via the default gateway, so such a test would put packets on the network.

Run locally: the whole suite on build/ and build-corosio/ (gtest-parallel); the QUIC tests on build-gcc/; the docs target; check_package.sh (the public quic.hpp includes <optional> now and has new Config fields); the include-boundary checks of CLAUDE.md, which print nothing. Not run: the sanitizer and h2v2 trees.

🤖 Generated with Claude Code

pgit and others added 2 commits November 24, 2026 13:58
quic::connect() opened and connected its UDP socket itself, with nothing
to bind it to, so a caller that had been told where to send from could
not pass that on. c-ares-async, for DNS over QUIC, accepts c-ares' local
address and device and drops them (#54).

- Config::local_endpoint is bound before the connect, with port 0 for
  any port. Only the server's endpoints of its family are tried, and
  without one of them, connect() fails with address_family_not_supported.
- Config::device is SO_BINDTODEVICE, Linux only. A name that no
  interface can have fails with no_such_device: the kernel would cut it
  short at a NUL or at 15 characters and bind to another interface.
- A socket that cannot be set up fails connect() with what the system
  said, address_not_available for an address that is not this host's,
  say, where every failure used to be connection_refused.
- Connection::remote_endpoint() tells the server where a connection
  comes from, and the client where it goes.

The HTTP/3 and TCP clients are unchanged.

Closes #54

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	include/anyhttp/quic.hpp
#	src/quic/connection.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

QUIC client: no way to choose the local address or device to send from

1 participant