Skip to content

Commit 2d18f69

Browse files
Add UWS_REMOTE_ADDRESS_USERSPACE macro for userspace remote address caching (#1917)
* Initial plan * Add UWS_REMOTE_ADDRESS_USERSPACE macro for caching remote address in per-socket data Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com> * Remove test binaries and add .gitignore Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com> * Delete .gitignore --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com>
1 parent 8956c97 commit 2d18f69

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/AsyncSocket.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,15 @@ struct AsyncSocket {
219219

220220
/* Returns the remote IP address or empty string on failure */
221221
std::string_view getRemoteAddress() {
222+
#ifdef UWS_REMOTE_ADDRESS_USERSPACE
223+
AsyncSocketData<SSL> *data = getAsyncSocketData();
224+
return std::string_view(data->remoteAddress, (unsigned int) data->remoteAddressLength);
225+
#else
222226
static thread_local char buf[16];
223227
int ipLength = 16;
224228
us_socket_remote_address(SSL, (us_socket_t *) this, buf, &ipLength);
225229
return std::string_view(buf, (unsigned int) ipLength);
230+
#endif
226231
}
227232

228233
/* Returns the text representation of IP */

src/AsyncSocketData.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ struct AsyncSocketData {
7474
/* This will do for now */
7575
BackPressure buffer;
7676

77+
#ifdef UWS_REMOTE_ADDRESS_USERSPACE
78+
/* Cache for remote address, populated on socket open */
79+
char remoteAddress[16];
80+
int remoteAddressLength = 0;
81+
#endif
82+
7783
/* Allow move constructing us */
7884
AsyncSocketData(BackPressure &&backpressure) : buffer(std::move(backpressure)) {
7985

src/HttpContext.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,27 @@ struct HttpContext {
6969
/* Init the HttpContext by registering libusockets event handlers */
7070
HttpContext<SSL> *init() {
7171
/* Handle socket connections */
72-
us_socket_context_on_open(SSL, getSocketContext(), [](us_socket_t *s, int /*is_client*/, char */*ip*/, int /*ip_length*/) {
72+
us_socket_context_on_open(SSL, getSocketContext(), [](us_socket_t *s, int /*is_client*/, char *ip, int ip_length) {
7373
/* Any connected socket should timeout until it has a request */
7474
us_socket_timeout(SSL, s, HTTP_IDLE_TIMEOUT_S);
7575

7676
/* Init socket ext */
7777
new (us_socket_ext(SSL, s)) HttpResponseData<SSL>;
7878

79+
#ifdef UWS_REMOTE_ADDRESS_USERSPACE
80+
/* Copy remote address into per-socket cache for later retrieval */
81+
AsyncSocketData<SSL> *asyncSocketData = (AsyncSocketData<SSL> *) us_socket_ext(SSL, s);
82+
if (ip_length > 0 && ip_length <= 16) {
83+
memcpy(asyncSocketData->remoteAddress, ip, (size_t) ip_length);
84+
asyncSocketData->remoteAddressLength = ip_length;
85+
} else {
86+
asyncSocketData->remoteAddressLength = 0;
87+
}
88+
#else
89+
(void) ip;
90+
(void) ip_length;
91+
#endif
92+
7993
/* Call filter */
8094
HttpContextData<SSL> *httpContextData = getSocketContextDataS(s);
8195
for (auto &f : httpContextData->filterHandlers) {

0 commit comments

Comments
 (0)