Repository navigation
feat(http): add http_client_set_local_addr to bind the source address before connecting - #917
Open
wang-guannan wants to merge 2 commits into
Open
wang-guannan wants to merge 2 commits into
wang-guannan wants to merge 2 commits into
Conversation
… before connecting On a multi-NIC host the kernel picks the source address of outgoing packets by the routing table, so the peer may see an unexpected source address. hv::TcpClient already offers bind(local_port, local_host), but hv::HttpClient is a thin wrapper over the opaque C API http_client_t, so there is no way to bind a local address before connecting. Add ConnectTimeoutWithLocalAddr() in base/hsocket, expose it through http_client_set_local_addr() and hv::HttpClient::setLocalAddr(), and add a unit test verifying that the peer sees the bound source address. Behaviour is unchanged unless setLocalAddr() is called.
PR ithewei#917 added http_client_set_local_addr() and wired it into the synchronous connect path (http_client_connect). The asynchronous path was left unchanged, so hv::HttpClient::sendAsync() silently ignored the local address and the kernel still picked the source address by the routing table. This extends the feature to the async path: - HttpClientTask carries the local address from the http_client_t to the connection setup. - AsyncHttpClient::startConnect() binds the socket to that address before connecting. The bind happens before hio_get() so that a failure can close the socket without racing the hloop cleanup. A failed bind fails the connection, matching the behaviour of the synchronous path. - http_client_exec_async() forwards cli->local_host/local_port. Adds unittest/http_client_local_addr_async_test.cpp, which checks that the peer sees the bound source address and that binding an unavailable address fails.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
On a multi-NIC host the kernel chooses the source address of outgoing packets by the routing table, so the peer may see an unexpected source address, or the connection may fail when the route does not leave through the intended interface.
hv::TcpClientalready offersbind(local_port, local_host), but the HTTP client does not:hv::HttpClientis a thin wrapper over the opaque C APIhttp_client_t, so there is no way to bind a local address before connecting. Applications that need a fixed source address have to drop down to the raw socket layer.Changes
base/hsocket.[ch]: addConnectTimeoutWithLocalAddr(), which binds the local address before connecting.http/client/HttpClient.[ch]: addhttp_client_set_local_addr()andhv::HttpClient::setLocalAddr(). When a local address is set, the connection binds it (port0lets the kernel choose the source port).http/client/AsyncHttpClient.[ch]: the same feature for the asynchronous path.HttpClientTaskcarries the local address from thehttp_client_tover to the connection setup.AsyncHttpClient::startConnect()binds the socket before connecting. The bind is done beforehio_get(), so that a failure can close the socket without racing the hloop cleanup. A failed bind fails the connection, matching the behaviour of the synchronous path.http_client_exec_async()forwardscli->local_host/cli->local_port.unittest/http_client_local_addr_test.cpp: verify that the peer sees the bound source address (synchronous path).unittest/http_client_local_addr_async_test.cpp: the same check for the asynchronous path.docs/cn/HttpClient.md: document the new API.Usage
Compatibility
Purely additive: no behaviour change unless
setLocalAddr()is called.Note
setLocalAddr()should be called before sending any request -- connections that were alreadyestablished keep the source address they were created with.
Test
Output: